(一)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 offsetbdinfo - print Board Info structureboot - boot default, i.e., run 'bootcmd' bootd - boot default, i.e., run 'bootcmd' bootelf - Boot from an ELF image in memorybootm - boot application image from memory bootp - boot image via network using BootP/TFTP protocolbootvx - Boot vxWorks from an ELF imagechpart - change active partitioncmp - memory compareconinfo - print console devices and informationcp - memory copycrc32 - checksum calculationdate - get/set/reset date & timedcache - enable or disable data cacheecho - echo args to consoleerase - erase FLASH memoryflinfo - print FLASH memory informationfsinfo - print information about filesystemsfsload - load binary file from a filesystem imagego - start application at address 'addr'help - print online helpicache - enable or disable instruction cacheiminfo - print header information for application imageimls - list all images found in flashitest - return true/false on integer compareloadb - load binary file over serial line (kermit mode)loads - load S-Record file over serial lineloadx - load binary file over serial line (xmodem mode)loady - load binary file over serial line (ymodem mode)loop - infinite loop on address rangels - list files in a directory (default /)md - memory displaymenu - display a menu, to select the items to do somethingmm - memory modify (auto-incrementing)mtdparts- define flash/nand partitionsmtest - simple RAM testmw - memory write (fill)nand - NAND sub-systemnboot - boot from NAND devicenfs - boot image via network using NFS protocolnm - memory modify (constant address)ping - send ICMP ECHO_REQUEST to network hostprintenv- print environment variablesprotect - enable or disable FLASH write protectionrarpboot- boot image via network using RARP/TFTP protocolreset - Perform RESET of the CPUrun - run commands in an environment variablesaveenv - save environment variables to persistent storagesetenv - set environment variablessleep - delay execution for some timetftpboot- boot image via network using TFTP protocolusbslave - 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>voiddo_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