U-Boot 启动过程和源码分析(第二阶段)

1.1.2             U-Boot启动第二阶段代码分析

       start_armboot函数在lib_arm/board.c中定义,是U-Boot第二阶段代码的入口。U-Boot启动第二阶段流程如下:

 

图 2.3 U-Boot第二阶段执行流程

       在分析start_armboot函数前先来看看一些重要的数据结构:

(1)gd_t结构体

       U-Boot使用了一个结构体gd_t来存储全局数据区的数据,这个结构体在include/asm-arm/global_data.h中定义如下:

typedef  struct     global_data {

       bd_t              *bd;

       unsigned long      flags;

       unsigned long      baudrate;

       unsigned long      have_console;      /* serial_init() was called */

       unsigned long      env_addr;     /* Address  of Environment struct */

       unsigned long      env_valid;    /* Checksum of Environment valid? */

       unsigned long      fb_base; /* base address of frame buffer */

       void              **jt;              /* jump table */

gd_t;

       U-Boot使用了一个存储在寄存器中的指针gd来记录全局数据区的地址:

#define DECLARE_GLOBAL_DATA_PTR     register volatile gd_t *gd asm ("r8")

       DECLARE_GLOBAL_DATA_PTR定义一个gd_t全局数据结构的指针,这个指针存放在指定的寄存器r8中。这个声明也避免编译器把r8分配给其它的变量。任何想要访问全局数据区的代码,只要代码开头加入“DECLARE_GLOBAL_DATA_PTR”一行代码,然后就可以使用gd指针来访问全局数据区了。

       根据U-Boot内存使用图中可以计算gd的值:

gd = TEXT_BASE -CONFIG_SYS_MALLOC_LEN - sizeof(gd_t)

(2)bd_t结构体

       bd_t在include/asm-arm.u/u-boot.h中定义如下:

typedef struct bd_info {

    int                bi_baudrate;               /* 串口通讯波特率 */

    unsigned long     bi_ip_addr;          /* IP 地址*/

    struct environment_s        *bi_env;              /* 环境变量开始地址 */

    ulong            bi_arch_number;      /* 开发板的机器码 */

    ulong            bi_boot_params;       /* 内核参数的开始地址 */

    struct                         /* RAM配置信息 */

    {

              ulong start;

              ulong size;

    }bi_dram[CONFIG_NR_DRAM_BANKS]; 

bd_t;

       U-Boot启动内核时要给内核传递参数,这时就要使用gd_t,bd_t结构体中的信息来设置标记列表。

       第一阶段调用start_armboot指向C语言执行代码区,首先它要从内存上的重定位数据获得不完全配置的全局数据表格和板级信息表格,即获得gd_tbd_t

这两个类型变量记录了刚启动时的信息,并将要记录作为引导内核和文件系统的参数,如bootargs等等,并且将来还会在启动内核时,由uboot交由kernel时会有所用。

(3)init_sequence数组

       U-Boot使用一个数组init_sequence来存储对于大多数开发板都要执行的初始化函数的函数指针。init_sequence数组中有较多的编译选项,去掉编译选项后init_sequence数组如下所示:

typedef int (init_fnc_t) (void);

init_fnc_t *init_sequence[] = {

       board_init,         /*开发板相关的配置--board/samsung/mini2440/mini2440.c */

       timer_init,            /* 时钟初始化-- cpu/arm920t/s3c24x0/timer.c */

       env_init,            /*初始化环境变量--common/env_flash.c 或common/env_nand.c*/

       init_baudrate,      /*初始化波特率-- lib_arm/board.c */

       serial_init,            /* 串口初始化-- drivers/serial/serial_s3c24x0.c */

       console_init_f,    /* 控制通讯台初始化阶段1-- common/console.c */

       display_banner,   /*打印U-Boot版本、编译的时间-- gedit lib_arm/board.c */

       dram_init,            /*配置可用的RAM-- board/samsung/mini2440/mini2440.c */

       display_dram_config,              /* 显示RAM大小-- lib_arm/board.c */

       NULL,

};

       其中的board_init函数在board/samsung/mini2440/mini2440.c中定义,该函数设置了MPLLCOM,UPLLCON,以及一些GPIO寄存器的值,还设置了U-Boot机器码和内核启动参数地址 :

      /* MINI2440开发板的机器码 */

     gd->bd->bi_arch_number = MACH_TYPE_MINI2440;

     /* 内核启动参数地址 */

     gd->bd->bi_boot_params = 0x30000100;  

     其中的dram_init函数在board/samsung/mini2440/mini2440.c中定义如下:

int dram_init (void)

{

      /* 由于mini2440只有 */

      gd->bd->bi_dram[0].start = PHYS_SDRAM_1;

      gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;

 

      return 0;

}

       mini2440使用2片32MB的SDRAM组成了64MB的内存,接在存储控制器的BANK6,地址空间是0x30000000~0x34000000。

       在include/configs/mini2440.h中PHYS_SDRAM_1和PHYS_SDRAM_1_SIZE 分别被定义为0x30000000和0x04000000(64M)。

       分析完上述的数据结构,下面来分析start_armboot函数:

void start_armboot (void)

{

       init_fnc_t **init_fnc_ptr;

       char *s;

       … …

       /* 计算全局数据结构的地址gd */

       gd = (gd_t*)(_armboot_start - CONFIG_SYS_MALLOC_LEN - sizeof(gd_t));

       … …

       memset ((void*)gd, 0, sizeof (gd_t));

       gd->bd = (bd_t*)((char*)gd - sizeof(bd_t));

       memset (gd->bd, 0, sizeof (bd_t));

       gd->flags |= GD_FLG_RELOC;

 

       monitor_flash_len = _bss_start - _armboot_start;

 

       /* 逐个调用init_sequence数组中的初始化函数  */

       for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {

              if ((*init_fnc_ptr)() != 0) {

                     hang ();

              }

       }

 

       /* armboot_start 在cpu/arm920t/start.S 中被初始化为u-boot.lds连接脚本中的_start */

       mem_malloc_init (_armboot_start - CONFIG_SYS_MALLOC_LEN,

                     CONFIG_SYS_MALLOC_LEN);

 

       /* NOR Flash初始化 */

#ifndef CONFIG_SYS_NO_FLASH

       /* configure available FLASH banks */

       display_flash_config (flash_init ());

#endif /* CONFIG_SYS_NO_FLASH */

 

       … … 

       /* NAND Flash 初始化*/

#if defined(CONFIG_CMD_NAND)

       puts ("NAND:  ");

       nand_init();         /* go init the NAND */

#endif

       … …

       /*配置环境变量,重新定位 */

       env_relocate ();

       … …

       /* 从环境变量中获取IP地址 */

       gd->bd->bi_ip_addr = getenv_IPaddr ("ipaddr");

       stdio_init (); /* get the devices list going. */

       jumptable_init ();

       … …

       console_init_r (); /* fully init console as a device */

       … …

       /* enable exceptions */

       enable_interrupts ();

 

#ifdef CONFIG_USB_DEVICE

       usb_init_slave();

#endif

 

       /* Initialize from environment */

       if ((s = getenv ("loadaddr")) != NULL) {

              load_addr = simple_strtoul (s, NULL, 16);

       }

#if defined(CONFIG_CMD_NET)

       if ((s = getenv ("bootfile")) != NULL) {

              copy_filename (BootFile, s, sizeof (BootFile));

       }

#endif

       … …

       /* 网卡初始化 */

#if defined(CONFIG_CMD_NET)

#if defined(CONFIG_NET_MULTI)

       puts ("Net:   ");

#endif

       eth_initialize(gd->bd);

… …

#endif

 

       /* main_loop() can return to retry autoboot, if so just run it again. */

       for (;;) {

              main_loop ();

       }

       /* NOTREACHED - no way out of command loop except booting */

}

       main_loop函数在common/main.c中定义。一般情况下,进入main_loop函数若干秒内没有

       U-Boot启动Linux过程

       U-Boot使用标记列表(tagged list)的方式向Linux传递参数。标记的数据结构式是tag,在U-Boot源代码目录include/asm-arm/setup.h中定义如下:

struct tag_header {

       u32 size;       /* 表示tag数据结构的联合u实质存放的数据的大小*/

       u32 tag;        /* 表示标记的类型 */

};

 

struct tag {

       struct tag_header hdr;

       union {

              struct tag_core           core;

              struct tag_mem32      mem;

              struct tag_videotext   videotext;

              struct tag_ramdisk     ramdisk;

              struct tag_initrd  initrd;

              struct tag_serialnr       serialnr;

              struct tag_revision      revision;

              struct tag_videolfb     videolfb;

              struct tag_cmdline     cmdline;

 

              /*

               * Acorn specific

               */

              struct tag_acorn  acorn;

              /*

               * DC21285 specific

               */

              struct tag_memclk      memclk;

       } u;

};

       U-Boot使用命令bootm来启动已经加载到内存中的内核。而bootm命令实际上调用的是do_bootm函数。对于Linux内核,do_bootm函数会调用do_bootm_linux函数来设置标记列表和启动内核。do_bootm_linux函数在lib_arm/bootm.c 中定义如下:

int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)

{

       bd_t       *bd = gd->bd;

       char       *s;

       int   machid = bd->bi_arch_number;

       void       (*theKernel)(int zero, int arch, uint params);

  

#ifdef CONFIG_CMDLINE_TAG

       char *commandline = getenv ("bootargs");   /* U-Boot环境变量bootargs */

#endif

       … …

       theKernel = (void (*)(int, int, uint))images->ep; /* 获取内核入口地址 */

       … …

#if   defined (CONFIG_SETUP_MEMORY_TAGS) || \

       defined (CONFIG_CMDLINE_TAG) || \

       defined (CONFIG_INITRD_TAG) || \

       defined (CONFIG_SERIAL_TAG) || \

       defined (CONFIG_REVISION_TAG) || \

       defined (CONFIG_LCD) || \

       defined (CONFIG_VFD)

       setup_start_tag (bd);                                     /* 设置ATAG_CORE标志 */

       … …

#ifdef CONFIG_SETUP_MEMORY_TAGS

      setup_memory_tags (bd);                             /* 设置内存标记 */

#endif

 #ifdef CONFIG_CMDLINE_TAG

      setup_commandline_tag (bd, commandline);      /* 设置命令行标记 */

#endif

       … …

      setup_end_tag (bd);                               /* 设置ATAG_NONE标志 */          

#endif

      /* we assume that the kernel is in place */

      printf ("\nStarting kernel ...\n\n");

       … …

      cleanup_before_linux ();          /* 启动内核前对CPU作最后的设置 */

 

      theKernel (0, machid, bd->bi_boot_params);      /* 调用内核 */

 

      /* does not return */

      return 1;

}

       其中的setup_start_tag,setup_memory_tags,setup_end_tag函数在lib_arm/bootm.c中定义如下:

      (1)setup_start_tag函数

static void setup_start_tag (bd_t *bd)

{

       params = (struct tag *) bd->bi_boot_params;  /* 内核的参数的开始地址 */

 

       params->hdr.tag = ATAG_CORE;

       params->hdr.size = tag_size (tag_core);

 

       params->u.core.flags = 0;

       params->u.core.pagesize = 0;

       params->u.core.rootdev = 0;

 

       params = tag_next (params);

}

       标记列表必须以ATAG_CORE开始,setup_start_tag函数在内核的参数的开始地址设置了一个ATAG_CORE标记。

      (2)setup_memory_tags函数

static void setup_memory_tags (bd_t *bd)

{

       int i;

       /*设置一个内存标记 */

       for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {   

              params->hdr.tag = ATAG_MEM;

              params->hdr.size = tag_size (tag_mem32);

 

              params->u.mem.start = bd->bi_dram[i].start;

              params->u.mem.size = bd->bi_dram[i].size;

 

              params = tag_next (params);

       }

}

       setup_memory_tags函数设置了一个ATAG_MEM标记,该标记包含内存起始地址,内存大小这两个参数。

      (3)setup_end_tag函数

static void setup_end_tag (bd_t *bd)

{

       params->hdr.tag = ATAG_NONE;

       params->hdr.size = 0;

}

       标记列表必须以标记ATAG_NONE结束,setup_end_tag函数设置了一个ATAG_NONE标记,表示标记列表的结束。

       U-Boot设置好标记列表后就要调用内核了。但调用内核前,CPU必须满足下面的条件:

     (1) CPU寄存器的设置

      Ø  r0=0

      Ø  r1=机器码

      Ø  r2=内核参数标记列表在RAM中的起始地址

    (2) CPU工作模式

      Ø  禁止IRQ与FIQ中断

      Ø  CPU为SVC模式

(3)    使数据Cache与指令Cache失效

       do_bootm_linux中调用的cleanup_before_linux函数完成了禁止中断和使Cache失效的功能。cleanup_before_linux函数在cpu/arm920t/cpu.中定义:

int cleanup_before_linux (void)

{

       /*

        * this function is called just before we call linux

        * it prepares the processor for linux

        *

        * we turn off caches etc ...

        */

       disable_interrupts ();         /* 禁止FIQ/IRQ中断 */

 

       /* turn off I/D-cache */

       icache_disable();               /* 使指令Cache失效 */

       dcache_disable();              /* 使数据Cache失效 */

       /* flush I/D-cache */

       cache_flush();                    /* 刷新Cache */

 

       return 0;

}

       由于U-Boot启动以来就一直工作在SVC模式,因此CPU的工作模式就无需设置了。

do_bootm_linux中:

       void       (*theKernel)(int zero, int arch, uint params);

… …

       theKernel = (void (*)(int, int, uint))images->ep;

… …

      theKernel (0, machid, bd->bi_boot_params);

       第73行代码将内核的入口地址“images->ep”强制类型转换为函数指针。根据ATPCS规则,函数的参数个数不超过4个时,使用r0~r3这4个寄存器来传递参数。因此第128行的函数调用则会将0放入r0,机器码machid放入r1,内核参数地址bd->bi_boot_params放入r2,从而完成了寄存器的设置,最后转到内核的入口地址。

       到这里,U-Boot的工作就结束了,系统跳转到Linux内核代码执行。

       U-Boot添加命令的方法及U-Boot命令执行过程

       下面以添加menu命令(启动菜单)为例讲解U-Boot添加命令的方法。

(1) 建立common/cmd_menu.c

       习惯上通用命令源代码放在common目录下,与开发板专有命令源代码则放在board/<board_dir>目录下,并且习惯以“cmd_<命令名>.c”为文件名。

(2)定义“menu”命令

       在cmd_menu.c中使用如下的代码定义“menu”命令:

_BOOT_CMD(

       menu,    3,    0,    do_menu,

       "menu - display a menu, to select the items to do something\n",

       " - display a menu, to select the items to do something"

);

       其中U_BOOT_CMD命令格式如下:

        U_BOOT_CMD(name,maxargs,rep,cmd,usage,help)

       各个参数的意义如下:

       name:命令名,非字符串,但在U_BOOT_CMD中用“#”符号转化为字符串

       maxargs:命令的最大参数个数

       rep:是否自动重复(按Enter键是否会重复执行)

       cmd:该命令对应的响应函数

       usage:简短的使用说明(字符串)

       help:较详细的使用说明(字符串)

       在内存中保存命令的help字段会占用一定的内存,通过配置U-Boot可以选择是否保存help字段。若在include/configs/mini2440.h中定义了CONFIG_SYS_LONGHELP宏,则在U-Boot中使用help命令查看某个命令的帮助信息时将显示usage和help字段的内容,否则就只显示usage字段的内容。

       U_BOOT_CMD宏在include/command.h中定义:

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

       “##”与“#”都是预编译操作符,“##”有字符串连接的功能,“#”表示后面紧接着的是一个字符串。

       其中的cmd_tbl_t在include/command.h中定义如下:

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

};

t ypedef struct cmd_tbl_s  cmd_tbl_t;

       一个cmd_tbl_t结构体变量包含了调用一条命令的所需要的信息。

       其中Struct_Section在include/command.h中定义如下:

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

       凡是带有__attribute__ ((unused,section (".u_boot_cmd"))属性声明的变量都将被存放在".u_boot_cmd"段中,并且即使该变量没有在代码中显式的使用编译器也不产生警告信息。

       在U-Boot连接脚本u-boot.lds中定义了".u_boot_cmd"段:

       . = .;

       __u_boot_cmd_start = .;          /*将 __u_boot_cmd_start指定为当前地址 */

       .u_boot_cmd : { *(.u_boot_cmd) }

       __u_boot_cmd_end = .;           /*  将__u_boot_cmd_end指定为当前地址  */

       这表明带有“.u_boot_cmd”声明的函数或变量将存储在“u_boot_cmd”段。这样只要将U-Boot所有命令对应的cmd_tbl_t变量加上“.u_boot_cmd”声明,编译器就会自动将其放在“u_boot_cmd”段,查找cmd_tbl_t变量时只要在__u_boot_cmd_start与__u_boot_cmd_end之间查找就可以了。

       因此“menu”命令的定义经过宏展开后如下:

cmd_tbl_t __u_boot_cmd_menu __attribute__ ((unused,section (".u_boot_cmd"))) = {menu, 3, 0, do_menu, "menu - display a menu, to select the items to do something\n", " - display a menu, to select the items to do something"}

       实质上就是用U_BOOT_CMD宏定义的信息构造了一个cmd_tbl_t类型的结构体。编译器将该结构体放在“u_boot_cmd”段,执行命令时就可以在“u_boot_cmd”段查找到对应的cmd_tbl_t类型结构体。

     (3)实现命令的函数

       在cmd_menu.c中添加“menu”命令的响应函数的实现。具体的实现代码略:

int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])

{

       /* 实现代码略 */

}

    (4)将common/cmd_menu.c编译进u-boot.bin

       在common/Makefile中加入如下代码:

COBJS-$(CONFIG_BOOT_MENU) += cmd_menu.o

       在include/configs/mini2440.h加入如代码:

#define CONFIG_BOOT_MENU 1

       重新编译下载U-Boot就可以使用menu命令了

     (5)menu命令执行的过程

       在U-Boot中输入“menu”命令执行时,U-Boot接收输入的字符串“menu”,传递给run_command函数。run_command函数调用common/command.c中实现的find_cmd函数在__u_boot_cmd_start与__u_boot_cmd_end间查找命令,并返回menu命令的cmd_tbl_t结构。然后run_command函数使用返回的cmd_tbl_t结构中的函数指针调用menu命令的响应函数do_menu,从而完成了命令的执行。

    (6)例子:USB下载,命令很简单。

复制代码
复制代码
 
  1 #include <common.h>
  2 #include <command.h>
  3 extern char console_buffer[];
  4 extern int readline (const char *const prompt);
  5 extern char awaitkey(unsigned long delay, int* error_p);
  6 extern void download_nkbin_to_flash(void);
  7 /**

  8  * Parses a string into a number.  The number stored at ptr is

  9  * potentially suffixed with K (for kilobytes, or 1024 bytes),

 10  * M (for megabytes, or 1048576 bytes), or G (for gigabytes, or

 11  * 1073741824).  If the number is suffixed with K, M, or G, then

 12  * the return value is the number multiplied by one kilobyte, one

 13  * megabyte, or one gigabyte, respectively.

 14  *

 15  * @param ptr where parse begins

 16  * @param retptr output pointer to next char after parse completes (output)

 17  * @return resulting unsigned int

 18  */

 19 static unsigned long memsize_parse2 (const char *const ptr, const char **retptr)
 20 {
 21     unsigned long ret = simple_strtoul(ptr, (char **)retptr, 0);
 22     int sixteen = 1;
 23     switch (**retptr) {
 24     case 'G':
 25     case 'g':
 26         ret <<= 10;
 27     case 'M':
 28     case 'm':
 29         ret <<= 10;
 30     case 'K':
 31     case 'k':
 32         ret <<= 10;
 33         (*retptr)++;
 34         sixteen = 0;
 35     default:
 36         break;
 37     }
 38     if (sixteen)
 39         return simple_strtoul(ptr, NULL, 16);
 40    
 41     return ret;
 42 }
 43 
 44 void param_menu_usage()
 45 {
 46     printf("\r\n##### Parameter Menu #####\r\n");
 47     printf("[v] View the parameters\r\n");
 48     printf("[s] Set parameter \r\n");
 49     printf("[d] Delete parameter \r\n");
 50     printf("[w] Write the parameters to flash memeory \r\n");
 51     printf("[q] Quit \r\n");
 52     printf("Enter your selection: ");
 53 }
 54 
 55 void param_menu_shell(void)
 56 {
 57     char c;
 58     char cmd_buf[256];
 59     char name_buf[20];
 60     char val_buf[256];
 61    
 62      while (1)
 63     {
 64         param_menu_usage();
 65         c = awaitkey(-1, NULL);
 66         printf("%c\n", c);
 67         switch (c)
 68         {
 69             case 'v':
 70             {
 71                 strcpy(cmd_buf, "printenv ");
 72                 printf("Name(enter to view all paramters): ");
 73                 readline(NULL);
 74                 strcat(cmd_buf, console_buffer);
 75                 run_command(cmd_buf, 0);
 76                 break;
 77             }
 78            
 79              case 's':
 80             {
 81                 sprintf(cmd_buf, "setenv ");
 82                 printf("Name: ");
 83                 readline(NULL);
 84                 strcat(cmd_buf, console_buffer);
 85                 printf("Value: ");
 86                 readline(NULL);
 87                 strcat(cmd_buf, " ");
 88                 strcat(cmd_buf, console_buffer);
 89                 run_command(cmd_buf, 0);
 90                 break;
 91             }
 92            
 93              case 'd':
 94             {
 95                 sprintf(cmd_buf, "setenv ");
 96                 printf("Name: ");
 97                 readline(NULL);
 98                 strcat(cmd_buf, console_buffer);
 99                 run_command(cmd_buf, 0);
100                 break;
101             }
102            
103              case 'w':
104             {
105                 sprintf(cmd_buf, "saveenv");
106                 run_command(cmd_buf, 0);
107                 break;
108             }
109            
110              case 'q':
111             {
112                 return;
113                 break;
114             }
115         }
116     }
117 }
118 
119 void main_menu_usage(void)
120 {
121     printf("\r\n##### 100ask Bootloader for OpenJTAG #####\r\n");
122     printf("[n] Download u-boot to Nand Flash\r\n");
123     if (bBootFrmNORFlash())
124          printf("[o] Download u-boot to Nor Flash\r\n");
125     printf("[k] Download Linux kernel uImage\r\n");
126     printf("[j] Download root_jffs2 image\r\n");
127 //    printf("[c] Download root_cramfs image\r\n");

128     printf("[y] Download root_yaffs image\r\n");
129     printf("[d] Download to SDRAM & Run\r\n");
130     printf("[z] Download zImage into RAM\r\n");
131     printf("[g] Boot linux from RAM\r\n");
132     printf("[f] Format the Nand Flash\r\n");
133     printf("[s] Set the boot parameters\r\n");
134     printf("[b] Boot the system\r\n");
135     printf("[r] Reboot u-boot\r\n");
136     printf("[q] Quit from menu\r\n");
137     printf("Enter your selection: ");
138 }
139 
140 void menu_shell(void)
141 {
142     char c;
143     char cmd_buf[200];
144     char *p = NULL;
145     unsigned long size;
146     unsigned long offset;
147     struct mtd_info *mtd = &nand_info[nand_curr_device];
148     while (1)
149     {
150         main_menu_usage();
151         c = awaitkey(-1, NULL);
152         printf("%c\n", c);
153         switch (c)
154         {
155            case 'n':
156            {
157                 strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase bootloader; nand write.jffs2 0x30000000 bootloader $(filesize)");
158                 run_command(cmd_buf, 0);
159                 break;
160             }
161             case 'o':
162             {
163                 if (bBootFrmNORFlash())
164                 {
165                     strcpy(cmd_buf, "usbslave 1 0x30000000; protect off all; erase 0 +$(filesize); cp.b 0x30000000 0 $(filesize)");
166                     run_command(cmd_buf, 0);
167                 }
168                 break;
169             }
170            
171             case 'k':
172             {
173                 strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase kernel; nand write.jffs2 0x30000000 kernel $(filesize)");
174                 run_command(cmd_buf, 0);
175                 break;
176             }
177             case 'j':
178             {
179                 strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase root; nand write.jffs2 0x30000000 root $(filesize)");
180                 run_command(cmd_buf, 0);
181                 break;
182             }
183 #if 0
184             case 'c':
185             {
186                 strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase root; nand write.jffs2 0x30000000 root $(filesize)");
187                 run_command(cmd_buf, 0);
188                 break;
189             }
190 #endif
191             case 'y':
192             {
193                 strcpy(cmd_buf, "usbslave 1 0x30000000; nand erase root; nand write.yaffs 0x30000000 root $(filesize)");
194                 run_command(cmd_buf, 0);
195                 break;
196             }
197             case 'd':
198             {
199                 extern volatile U32 downloadAddress;
200                 extern int download_run;
201                
202                  download_run = 1;
203                 strcpy(cmd_buf, "usbslave 1");
204                 run_command(cmd_buf, 0);
205                 download_run = 0;
206                 sprintf(cmd_buf, "go %x", downloadAddress);
207                 run_command(cmd_buf, 0);
208                 break;
209             }
210             case 'z':
211             {
212                 strcpy(cmd_buf, "usbslave 1 0x30008000");
213                 run_command(cmd_buf, 0);
214                 break;
215             }
216             case 'g':
217             {
218                 extern void do_bootm_rawLinux (ulong addr);
219                 do_bootm_rawLinux(0x30008000);
220             }
221             case 'b':
222             {
223                 printf("Booting Linux ...\n");
224                 strcpy(cmd_buf, "nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0");
225                 run_command(cmd_buf, 0);
226                 break;
227             }
228             case 'f':
229             {
230                 strcpy(cmd_buf, "nand erase ");
231                 printf("Start address: ");
232                 readline(NULL);
233                 strcat(cmd_buf, console_buffer);
234                 printf("Size(eg. 4000000, 0x4000000, 64m and so on): ");
235                 readline(NULL);
236                 p = console_buffer;
237                 size = memsize_parse2(p, &p);
238                 sprintf(console_buffer, " %x", size);
239                 strcat(cmd_buf, console_buffer);
240                 run_command(cmd_buf, 0);
241                 break;
242             }
243             case 's':
244             {
245                 param_menu_shell();
246                 break;
247             }
248             case 'r':
249             {
250                 strcpy(cmd_buf, "reset");
251                 run_command(cmd_buf, 0);
252                 break;
253             }
254            
255              case 'q':
256             {
257                 return;   
258                  break;
259             }
260         }
261                
262      }
263 }
264 int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
265 {
266     menu_shell();
267     return 0;
268 }
269 U_BOOT_CMD(
270  menu, 3, 0, do_menu,
271  "menu - display a menu, to select the items to do something\n",
272  " - display a menu, to select the items to do something"

273 );
 
复制代码
复制代码

 TFTP下载

复制代码
复制代码
 
 1 #include <common.h>
 2 #include <command.h>
 3 /**功能:等待键盘输入***/
 4 static char awaitkey(unsigned long delay, int* error_p)
 5 {
 6     int i;
 7     char c;
 8     if (delay == -1) {
 9         while (1) {
10             if (tstc()) /* we got a key press */
11                 return getc();
12         }
13     }
14     else {       
15          for (i = 0; i < delay; i++) {
16       if (tstc()) /* we got a key press */
17        return getc();
18             udelay (10*1000);
19         }
20     }
21     if (error_p)
22         *error_p = -1;
23     return 0;
24 }      
25 /*****提示符,功能说明****/ 
26 void main_menu_usage(void)
27 {
28  printf("\r\n######## Hotips TFTP DownLoad for SMDK2440 ########\r\n");
29  printf("\r\n");
30  printf("[1] 下载 u-boot.bin       写入 Nand Flash\r\n");
31  printf("[2] 下载 Linux(uImage)    内核镜像写入 Nand Flash\r\n");
32  printf("[3] 下载 yaffs2(fs.yaffs) 文件系统镜像写入 Nand Flash\r\n");
33  printf("[4] 下载 Linux(uImage)    内核镜像到内存并运行\r\n");
34  printf("[5] 重启设备\r\n");
35  printf("[q] 退出菜单\r\n");
36  printf("\r\n");
37  printf("输入选择: ");
38 }
39 /***do_menu()的调用函数,命令的具体实现***/
40  
41 void menu_shell(void)
42 {
43     char c;
44     char cmd_buf[200];
45     while (1)
46     {
47       main_menu_usage();
48       c = awaitkey(-1, NULL);
49       printf("%c\n", c);
50       switch (c)
51       {
52   case '1':
53   {
54           strcpy(cmd_buf, "tftp 0x32000000 u-boot.bin; nand erase 0x0 0x60000; nand write 0x32000000 0x0 0x60000");
55           run_command(cmd_buf, 0);
56           break;
57   }
58         case '2':
59         {
60           strcpy(cmd_buf, "tftp 0x32000000 uImage; nand erase 0x80000 0x200000; nand write 0x32000000 0x80000 0x200000");
61           run_command(cmd_buf, 0);
62      break;
63         }
64         case '3':
65         {
66           strcpy(cmd_buf, "tftp 0x32000000 fs.yaffs; nand erase 0x280000; nand write.yaffs2 0x32000000 0x280000 $(filesize)");
67           run_command(cmd_buf, 0);
68           break;
69         }
70   case '4':
71         {
72           strcpy(cmd_buf, "tftp 0x32000000 uImage; bootm 0x32000000");
73           run_command(cmd_buf, 0);
74           break;
75         }
76   case '5':
77         {
78           strcpy(cmd_buf, "reset");
79           run_command(cmd_buf, 0);
80           break;
81         }
82         case 'q':
83         {
84           return;   
85            break;
86         }
87       }
88     }
89 }
90 int do_menu (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
91 {
92     menu_shell();
93     return 0;
94 }
95 U_BOOT_CMD(
96  menu, 1, 0, do_menu,
97  "Download Menu",
98  "U-boot Download Menu by Hotips\n"
99 );
 
复制代码
复制代码

 对比两种下载方式我们清楚命令的添加和执行方式了

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值