日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

u-boot分析之命令实现(四)

發布時間:2025/4/5 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 u-boot分析之命令实现(四) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

u-boot(四)命令實現

  • 分析run_command
  • 小結
  • 自定義一個命令
  • 代碼
  • makefile

u-boot(四)命令實現

命令是如何實現的?

  • 輸入命令
  • 執行函數,根據命令去尋找函數
  • 所以會有一個命令的結構體[name,fun],該結構體含有命令的name和對應的fun函數。

    分析run_command

    函數原型如下?int run_command (const char *cmd, int flag)

  • 處理, 空格,;等
  • 解析參數parse_line (finaltoken, argv)
  • example: md.w 0 ------>argv[0]= "md.w", argv[1]=" 0"?``` /* Extract arguments */ if ((argc = parse_line (finaltoken, argv)) == 0) {rc = -1; /* no command at all */continue; } ?```

    命令搜索if ((cmdtp = find_cmd(argv[0])) == NULL),可以發現結構體

    struct cmd_tbl_s {char *name; /* Command Name */int maxargs; /* maximum number of arguments */int repeatable; /* autorepeat allowed? *//* Implementation function */int (*cmd)(struct cmd_tbl_s *, int, int, char *[]);char *usage; /* Usage message (short) */ #ifdef CFG_LONGHELPchar *help; /* Help message (long) */ #endif #ifdef CONFIG_AUTO_COMPLETE/* do auto completion on the arguments */int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]); #endif };

    查看函數,可以發現是在__u_boot_cmd_start和__u_boot_cmd_end中遍歷,這個地址是在鏈接腳本中定義的,也就是命令這個東西,有一個特殊的屬性,定位到某個地址.

    . = .;__u_boot_cmd_start = .;.u_boot_cmd : { *(.u_boot_cmd) }__u_boot_cmd_end = .;

    搜索這個段屬性.u_boot_cmd,在include\command.h有這么一個宏

    #define Struct_Section __attribute__ ((unused,section (".u_boot_cmd")))

    再搜索下這個宏

    #define U_BOOT_CMD(name,maxargs,rep,cmd,usage,help) \ cmd_tbl_t __u_boot_cmd_##name Struct_Section = {#name, maxargs, rep, cmd, usage, help}

    再搜索一下這個U_BOOT_CMD,可以發現其實就是命令了,搜索下命令bootm,在common\cmd_bootm.c中

    U_BOOT_CMD(bootm, CFG_MAXARGS, 1, do_bootm,"bootm - boot application image from memory\n",//注意,下面的幾個是沒有逗號,是整體"[addr [arg ...]]\n - boot application image stored in memory\n""\tpassing arguments 'arg ...'; when booting a Linux kernel,\n""\t'arg' can be the address of an initrd image\n" );

    嘗試著展開這個宏,可以發現就是定義了一個段屬性特殊的結構體,也就是命令結構體

    cmd_tbl_t __u_boot_cmd_bootm Struct_Section= {"bootm",CFG_MAXARGS,1,do_bootm,"bootm - boot application image from memory\n",//下面的字符串是一個整體"[addr [arg ...]]\n - boot application image stored in memory\n""\tpassing arguments 'arg ...'; when booting a Linux kernel,\n""\t'arg' can be the address of an initrd image\n" }
    • repeatable 可重復,指的是直接按回車是否繼續執行上次命令
    • usage,短的help,指的是直接輸入help查看的所有命令顯示的幫助
    • help,具體的help,指的是help cmd 查看的具體的信息

    小結

  • U-boot 的命令是用結構體存儲的,這些結構體是用特殊的段屬性集合到一塊區域里面去,分散在各個文件中
  • 命令解析的時候是去這個段去搜索的,這個段屬性的地址是從__u_boot_cmd_start到__u_boot_cmd_end,在鏈接腳本中定義的.
  • 命令結構體
  • struct cmd_tbl_s ;

    自定義一個命令

    參考common/cmd_bootm.c的頭文件,編寫源代碼cmd_hello.c

    代碼

    #include <common.h> #include <watchdog.h> #include <command.h> #include <image.h> #include <malloc.h> #include <zlib.h> #include <bzlib.h> #include <environment.h> #include <asm/byteorder.h> int do_hello (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) {int i ;printf ("hello world %d \n,", argc);//打印下參數for(i=0;i<argc;i++){printf ("argv[%d] is %s \n",i,argv[i]);}return 0; }U_BOOT_CMD(hello, CFG_MAXARGS, 1, do_hello,"this is short help for hello just test\n","this is long help for hello just test\n" );

    makefile

    修改common的makefile,只需要在COBJS上加上cmd_hello.o

    轉載:https://www.cnblogs.com/zongzi10010/p/10023679.html

    總結

    以上是生活随笔為你收集整理的u-boot分析之命令实现(四)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。