(一)u-boot常用命令
u-boot的主要目的是启动内核,在启动内核之前,我们一般使用u-boot的命令来下载内核到内存、擦除、读写Flash,运行内存、NOR Flash、NAND Flash中的程序,查看、修改、比较内存中的数据等。
help命令可以查看u-boot支持的命令有哪些:
<span style="font-size:14px;">? - alias for 'help' autoscr - run script from memory base - print or set address offset bdinfo - print Board Info structure boot - boot default, i.e., run 'bootcmd' bootd - boot default, i.e., run 'bootcmd' bootelf - Boot from an ELF image in memory bootm - boot application image from memory bootp - boot image via network using BootP/TFTP protocol bootvx - Boot vxWorks from an ELF image chpart - change active partition cmp - memory compare coninfo - print console devices and information cp - memory copy crc32 - checksum calculation date - get/set/reset date & time dcache - enable or disable data cache echo - echo args to console erase - erase FLASH memory flinfo - print FLASH memory information fsinfo - print information about filesystems fsload - load binary file from a filesystem image go - start application at address 'addr' help - print online help icache - enable or disable instruction cache iminfo - print header information for application image imls - list all images found in flash itest - return true/false on integer compare loadb - load binary file over serial line (kermit mode) loads - load S-Record file over serial line loadx - load binary file over serial line (xmodem mode) loady - load binary file over serial line (ymodem mode) loop - infinite loop on address range ls - list files in a directory (default /) md - memory display menu - display a menu, to select the items to do something mm - memory modify (auto-incrementing) mtdparts- define flash/nand partitions mtest - simple RAM test mw - memory write (fill) nand - NAND sub-system nboot - boot from NAND device nfs - boot image via network using NFS protocol nm - memory modify (constant address) ping - send ICMP ECHO_REQUEST to network host printenv- print environment variables protect - enable or disable FLASH write protection rarpboot- boot image via network using RARP/TFTP protocol reset - Perform RESET of the CPU run - run commands in an environment variable saveenv - save environment variables to persistent storage setenv - set environment variables sleep - delay execution for some time tftpboot- boot image via network using TFTP protocol usbslave - get file from host(PC) version - print monitor version</span>
这些命令包括下载文件到内存,擦除、读写Flash,运行内存、NOR Flash、NAND Flash中的程序,查看、修改、比较内存中的数据等。使用各种命令时,可以使用其开头的若干个字母代替它。比如tftpboot命令,可以使用t、tf、tftp等字母代替,只要其他命令不以这些字母开头即可。
1、帮助命令help
2、下载命令,u-boot支持串口下载、网络下载,相关命令有loadb、loads、loadx、loady和tftpboot、nfs。前几个支持串口下载。tftpboot命令使用TFTP协议从服务器下载文件,服务器IP地址为环境变量serverip。nfs命令使用NFS协议下载文件。
3、内存操作命令。常用的命令有:查看内存命令md、修改内存命令mm、填充内存命令mw、复制命令cp。
4、NOR Flash操作命令。常用的命令有查看Flash信息的finfo命令、加/解写保护命令protect、擦除命令erase。由于NOR Flash的接口与一般内存相似,所以一些内存命令可以在NOR Flash上使用,比如mm、cp命令。
5、NAND Flash操作命令只有一个:nand,它根据不同的参数进行不同操作,比如擦除、读取、烧写等
6、环境变量命令:printenv、setenv等。
7、启动命令:不带参数的boot、bootm命令都是执行环境变量bootcmd所指定的命令。
u-boot命令使用实例:制作内核映像文件(使用mkimage)、烧写内核映像文件uImage、烧写yaffs文件系统映像、烧写jffs2文件系统映像、执行程序(go)等,这些都会在以后构建嵌入式系统时用到。
(二)u-boot命令的添加
u-boot中每个命令都通过U_BOOT_CMD宏来定义,其格式如下:
<span style="font-size:14px;">#define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage}</span>各项参数意义如下:
1、name:命令的名字,注意,它不是一个字符串(不要使用双引号括起来)。
2、maxargs:最大的参数个数。
3、repeatable:命令是否可重复,可重复是指运行一个命令后,下次敲回车即可再次运行。
4、command:对应的函数指针,类型为(*cmd)(struct cmd_tbl_s*, int, int, char *[])。
5、usage:简短的使用说明,这是个字符串。
6、help:较详细的使用说明,这是个字符串。
<span style="font-size:14px;">Struct_Section结构定义如下:</span>
<span style="font-size:14px;">#define Struct_Section __attribute__ ((unused,section (".u_boot_cmd"))) </span>对于每个使用U_BOOT_CMD宏来定义的命令,其实都是在“.u_boot_cmd”段中定义一个cmd_tbl_t结构。链接脚本u-boot.lds中有如下代码:
<span style="font-size:14px;"> . = .; __u_boot_cmd_start = .; .u_boot_cmd : { *(.u_boot_cmd) } __u_boot_cmd_end = .;</span>程序中就是根据命令的名字在内存段__u_boot_cmd_start~__u_boot_cmd_end找到它的cmd_tbl_t结构,然后调用它的函数。
因此添加u-boot命令就可以分为两步:添加实现命令的文件、修改Makefile。以clear命令为例。
1、在common目录下添加cmd_clear.c文件,其内容如下:
<span style="font-size:14px;">#include <common.h> #include <command.h> #include <net.h> void do_clear (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) { printf("clear the screen.\n"); printf("%c", '\f'); } U_BOOT_CMD( clear, 1, 1, do_clear, "clear the uart com screen", "" );</span>2、修改common目录下的Makefile文件,在COBJS变量最后添加cmd_clear.o。
3、重新配置编译u-boot,将生成的u-boot.bin文件烧写到开发板上,即可使用clear命令。
相应的,可以添加任意的想要添加的命令。
参考:韦东山 《嵌入式Linux应用开发完全手册》
http://blog.csdn.net/linucos/article/details/5259061