U-boot 分析4

<common/main.c>

    274 void main_loop (void)

    275 {

    276 #ifndef CONFIG_SYS_HUSH_PARSER

    277     static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };

    278     int len;

    279     int rc = 1;

    280     int flag;

    281 #endif

    282

    283 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)

    284     char *s;

    285     int bootdelay;

    286 #endif

    287 #ifdef CONFIG_PREBOOT

    288     char *p;

    289 #endif

    290 #ifdef CONFIG_BOOTCOUNT_LIMIT

    291     unsigned long bootcount = 0;

    292     unsigned long bootlimit = 0;

    293     char *bcs;

    294     char bcs_set[16];

    295 #endif

    296

    297 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)

    298     ulong bmp = 0;     

    299     extern int trab_vfd (ulong bitmap);

    300

    301 #ifdef CONFIG_MODEM_SUPPORT

    302     if (do_mdm_init)

    303         bmp = 1;   

    304 #endif

    305     trab_vfd (bmp);

    306 #endif 

    307

    308 #ifdef CONFIG_BOOTCOUNT_LIMIT

309     bootcount = bootcount_load();

    310     bootcount++;

    311     bootcount_store (bootcount);

    312     sprintf (bcs_set, "%lu", bootcount);

    313     setenv ("bootcount", bcs_set);

    314     bcs = getenv ("bootlimit");

    315     bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;

    316 #endif

    317

    318 #ifdef CONFIG_MODEM_SUPPORT

    319     debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);

    320     if (do_mdm_init) {

    321         char *str = strdup(getenv("mdm_cmd"));

    322         setenv ("preboot", str);  

    323         if (str != NULL)

    324             free (str);

    325         mdm_init();

    326     }

    327 #endif 

    328

    329 #ifdef CONFIG_VERSION_VARIABLE

    330     {

    331         extern char version_string[];

    332

    333         setenv ("ver", version_string); 

    334     }

    335 #endif

    336

    337 #ifdef CONFIG_SYS_HUSH_PARSER

    338     u_boot_hush_start ();

    339 #endif

    340

    341 #if defined(CONFIG_HUSH_INIT_VAR)

    342     hush_init_var ();

    343 #endif

    344

    345 #ifdef CONFIG_AUTO_COMPLETE

    346     install_auto_complete();

    347 #endif

    348    

    349 #ifdef CONFIG_PREBOOT

    350     if ((p = getenv ("preboot")) != NULL) {

    351 # ifdef CONFIG_AUTOBOOT_KEYED

    352         int prev = disable_ctrlc(1);   

    353 # endif

    354    

    355 # ifndef CONFIG_SYS_HUSH_PARSER

    356         run_command (p, 0);

    357 # else 

    358         parse_string_outer(p, FLAG_PARSE_SEMICOLON |

    359                     FLAG_EXIT_FROM_LOOP);

    360 # endif

    361    

    362 # ifdef CONFIG_AUTOBOOT_KEYED

    363         disable_ctrlc(prev);   

    364 # endif

    365     }

    366 #endif

    367

    368 #if defined(CONFIG_UPDATE_TFTP)

    369     update_tftp ();

    370 #endif

    371

    372 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)

    373     s = getenv ("bootdelay");

    374     bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;

    375

    376     debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);

    377    

    378 # ifdef CONFIG_BOOT_RETRY_TIME

    379     init_cmd_timeout ();

    380 # endif

    381

    382 #ifdef CONFIG_POST

    383     if (gd->flags & GD_FLG_POSTFAIL) {

    384         s = getenv("failbootcmd");

    385     }

    386     else

    387 #endif

    388 #ifdef CONFIG_BOOTCOUNT_LIMIT

    389     if (bootlimit && (bootcount > bootlimit)) {

    390         printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",

    391                 (unsigned)bootlimit);

    392         s = getenv ("altbootcmd");

    393     }

    394     else

    395 #endif

    396         s = getenv ("bootcmd");

    397

    398     debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");

    399

    400     if (bootdelay >= 0 && s && !abortboot (bootdelay)) {

    401 # ifdef CONFIG_AUTOBOOT_KEYED

    402         int prev = disable_ctrlc(1);   

    403 # endif

    404

    405 # ifndef CONFIG_SYS_HUSH_PARSER

    406         run_command (s, 0);

    407 # else

    408         parse_string_outer(s, FLAG_PARSE_SEMICOLON |

    409                     FLAG_EXIT_FROM_LOOP);

    410 # endif

    411

    412 # ifdef CONFIG_AUTOBOOT_KEYED

    413         disable_ctrlc(prev);   

    414 # endif

    415     }

    416

    417 # ifdef CONFIG_MENUKEY

    418     if (menukey == CONFIG_MENUKEY) {

    419         s = getenv("menucmd");

    420         if (s) {

    421 # ifndef CONFIG_SYS_HUSH_PARSER

    422         run_command (s, 0);

    423 # else

    424         parse_string_outer(s, FLAG_PARSE_SEMICOLON |

    425                     FLAG_EXIT_FROM_LOOP);

    426 # endif        

    427         }

    428     }

    429 #endif

    430 #endif 

    431        

    432 #ifdef CONFIG_AMIGAONEG3SE

    433     {

    434         extern void video_banner(void);

    435         video_banner();

    436     }

    437 #endif 

    438

    439    

    442 #ifdef CONFIG_SYS_HUSH_PARSER

    443     parse_file_outer();

    444    

    445     for (;;);

    446 #else

    447     for (;;) {

    448 #ifdef CONFIG_BOOT_RETRY_TIME  

    449         if (rc >= 0) {

    450            

    453             reset_cmd_timeout();

    454         }

    455 #endif

    456         len = readline (CONFIG_SYS_PROMPT);

    457

    458         flag = 0;  

    459         if (len > 0)

    460             strcpy (lastcommand, console_buffer);

    461         else if (len == 0)

    462             flag |= CMD_FLAG_REPEAT;

    463 #ifdef CONFIG_BOOT_RETRY_TIME

    464         else if (len == -2) {

    465            

    467             puts ("\nTimed out waiting for command\n");

    468 # ifdef CONFIG_RESET_TO_RETRY

    469            

    470             do_reset (NULL, 0, 0, NULL);

    471 # else

    472             return;    

    473 # endif

    474         }

    475 #endif

    476

    477         if (len == -1)

    478             puts ("<INTERRUPT>\n");

    479         else

    480             rc = run_command (lastcommand, flag);

    481

    482         if (rc <= 0) {

    483            

    484             lastcommand[0] = 0;

    485         }  

    486     }

    487 #endif

    488 }

真的是很长,每个函数读下去涉及到的东西会很多,我也不知道我有没有毅力写完。

开始了。。。

1. 首先看main_loop()函数,无返回值,无参数

2. 里面的宏定义很多,不好取舍,还是一个一个的来

3. 很多都是流程的东西,挑几个出来说

4. <common/main.c>

   1293 int run_command (const char *cmd, int flag)

         cmd_tbl_t *find_cmd (const char *cmd)

{

                  int len = &__u_boot_cmd_end - &__u_boot_cmd_start;

                  return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);

}

<cpu/arm920t/u-boot.lds>

         __u_boot_cmd_start = .;

         .u_boot_cmd : { *(.u_boot_cmd) }

         __u_boot_cmd_end = .;

__u_boot_cmd_start__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}

         数据结构cmd_tbl_t __u_boot_cmd_##name 专门存放于段.u_boot_cmd

struct cmd_tbl_s {

         char          *name;             

         int              maxargs;

         int              repeatable;     

                                              

         int              (*cmd)(struct cmd_tbl_s *, int, int, char *[]);

         char          *usage;            

#ifdef        CONFIG_SYS_LONGHELP

         char          *help;               

#endif

#ifdef CONFIG_AUTO_COMPLETE

        

         int              (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);

#endif

};

         U_BOOT_CMD宏可以添加命令

         <common/command.c>

cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)

         从命令段中匹配命令,通过命令的名字进行匹配

update_tftp() > update_load() > NetLoop()

里面进行网口收发数据包的工作

static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)

NetLoop()中调用TftpStart()filename下载到addr位置

检查文件后,调用update_flash()写入flash

static int update_flash(ulong addr_source, ulong addr_first, ulong size)

addr_source要写入flash的数据在内存中的位置

addr_first要写入的flash的首地址

size要写入flash数据大小

> int flash_sect_roundb (ulong *addr) 数据的sector对齐

> static int update_flash_protect(int prot, ulong addr_first, ulong addr_last) 关闭数据块保护

> int flash_sect_erase (ulong addr_first, ulong addr_last) 擦除flash数据块

> int flash_write (char *src, ulong addr, ulong cnt) 数据写入flash

> static int update_flash_protect(int prot, ulong addr_first, ulong addr_last) 开启数据块保护

逻辑很清晰

下面的部分通过

int readline (const char *const prompt)

获取指令

int run_command (const char *cmd, int flag)

运行相应指令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值