6、根據(jù)U_BOOT_CMD信息進(jìn)入u-boot-2016.05\cmd\bootm.c:do_bootm函數(shù)
int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
。。。
return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
BOOTM_STATE_LOADOS |
#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
BOOTM_STATE_OS_CMDLINE |
#endif
BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
BOOTM_STATE_OS_GO, &images, 1);
}
1234567891011121314
其中BOOTM_STATE_START 、BOOTM_STATE_FINDOS 、BOOTM_STATE_FINDOTHER 、BOOTM_STATE_LOADOS 、BOOTM_STATE_OS_PREP 、BOOTM_STATE_OS_FAKE_GO 這些在u-boot-2016.05\include\image.h中bootm_headers結(jié)構(gòu)體中指定:
#define BOOTM_STATE_START (0x00000001)
#define BOOTM_STATE_FINDOS (0x00000002)
#define BOOTM_STATE_FINDOTHER (0x00000004)
#define BOOTM_STATE_LOADOS (0x00000008)
#define BOOTM_STATE_RAMDISK (0x00000010)
#define BOOTM_STATE_FDT (0x00000020)
#define BOOTM_STATE_OS_CMDLINE (0x00000040)
#define BOOTM_STATE_OS_BD_T (0x00000080)
#define BOOTM_STATE_OS_PREP (0x00000100)
#define BOOTM_STATE_OS_FAKE_GO (0x00000200) /* ‘Almost’ run the OS */
#define BOOTM_STATE_OS_GO (0x00000400)1234567891011
7、從do_bootm進(jìn)入u-boot-2016.05\common\bootm.c:do_bootm_states函數(shù),Now run the OS!
int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
int states, bootm_headers_t *images, int boot_progress)
{
boot_os_fn *boot_fn;
ulong iflag = 0;
int ret = 0, need_boot_fn;
images-》state |= states;
/*
* Work through the states and see how far we get. We stop on
* any error.
*/
if (states & BOOTM_STATE_START)
ret = bootm_start(cmdtp, flag, argc, argv);
if (!ret && (states & BOOTM_STATE_FINDOS))
ret = bootm_find_os(cmdtp, flag, argc, argv);
if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
ret = bootm_find_other(cmdtp, flag, argc, argv);
argc = 0; /* consume the args */
}
/* Load the OS */
if (!ret && (states & BOOTM_STATE_LOADOS)) {
ulong load_end;
iflag = bootm_disable_interrupts();
ret = bootm_load_os(images, &load_end, 0);
if (ret == 0)
lmb_reserve(&images-》lmb, images-》os.load,
?。╨oad_end - images-》os.load));
else if (ret && ret != BOOTM_ERR_OVERLAP)
goto err;
else if (ret == BOOTM_ERR_OVERLAP)
ret = 0;
#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
if (images-》os.os == IH_OS_LINUX)
fixup_silent_linux();
#endif
}
/* Relocate the ramdisk */
#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
if (!ret && (states & BOOTM_STATE_RAMDISK)) {
ulong rd_len = images-》rd_end - images-》rd_start;
ret = boot_ramdisk_high(&images-》lmb, images-》rd_start,
rd_len, &images-》initrd_start, &images-》initrd_end);
if (!ret) {
setenv_hex(“initrd_start”, images-》initrd_start);
setenv_hex(“initrd_end”, images-》initrd_end);
}
}
#endif
#if IMAGE_ENABLE_OF_LIBFDT && defined(CONFIG_LMB)
if (!ret && (states & BOOTM_STATE_FDT)) {
boot_fdt_add_mem_rsv_regions(&images-》lmb, images-》ft_addr);
ret = boot_relocate_fdt(&images-》lmb, &images-》ft_addr,
&images-》ft_len);
}
#endif
/* From now on, we need the OS boot function */
if (ret)
return ret;
boot_fn = bootm_os_get_boot_func(images-》os.os);
need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
if (boot_fn == NULL && need_boot_fn) {
if (iflag)
enable_interrupts();
printf(“ERROR: booting os ‘%s’ (%d) is not supported\n”,
genimg_get_os_name(images-》os.os), images-》os.os);
bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
return 1;
}
/* Call various other states that are not generally used */
if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
if (!ret && (states & BOOTM_STATE_OS_BD_T))
ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
if (!ret && (states & BOOTM_STATE_OS_PREP))
ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
#ifdef CONFIG_TRACE
/* Pretend to run the OS, then run a user command */
if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
char *cmd_list = getenv(“fakegocmd”);
ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
images, boot_fn);
if (!ret && cmd_list)
ret = run_command_list(cmd_list, -1, flag);
}
#endif
/* Check for unsupported subcommand. */
if (ret) {
puts(“subcommand not supported\n”);
return ret;
}
/* Now run the OS! We hope this doesn‘t return */
if (!ret && (states & BOOTM_STATE_OS_GO))
ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
images, boot_fn);
/* Deal with any fallout */
err:
if (iflag)
enable_interrupts();
if (ret == BOOTM_ERR_UNIMPLEMENTED)
bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
else if (ret == BOOTM_ERR_RESET)
do_reset(cmdtp, flag, argc, argv);
return ret;
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
do_bootm_states函數(shù)總共分8個(gè)部分:
⑴、 Work through the states and see how far we get. We stop on any error.
其中主要函數(shù)bootm_find_os實(shí)現(xiàn)三個(gè)功能:get kernel image header, start address and length,get image parameters。大概過(guò)程是:bootm_find_os -》 boot_get_kernel -》 image_get_kernel 。
static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
const void *os_hdr;
bool ep_found = false;
int ret;
/* get kernel image header, start address and length */
os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
&images, &images.os.image_start, &images.os.image_len);
。。。
/* get image parameters */
switch (genimg_get_format(os_hdr)) {
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
case IMAGE_FORMAT_LEGACY: /*舊系統(tǒng)格式的內(nèi)核映像*/
images.os.type = image_get_type(os_hdr);
images.os.comp = image_get_comp(os_hdr);
images.os.os = image_get_os(os_hdr);
images.os.end = image_get_image_end(os_hdr);
images.os.load = image_get_load(os_hdr);
images.os.arch = image_get_arch(os_hdr);
break;
#endif
#if IMAGE_ENABLE_FIT
case IMAGE_FORMAT_FIT:
。。。
#endif
#ifdef CONFIG_ANDROID_BOOT_IMAGE
case IMAGE_FORMAT_ANDROID:
。。。
#endif
default:
puts(“ERROR: unknown image format type!\n”);
return 1;
}
。。。
if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
images.os.load = images.os.image_start;
images.ep += images.os.load;
}
。。.123456789101112131415161718192021222324252627282930313233343536373839404142434445
關(guān)于boot_get_kernel 、image_get_kernel 的說(shuō)明:
boot_get_kernel - find kernel image(returns:
pointer to image header if valid image was found, plus kernel start address and length, otherwise NULL)
image_get_kernel - verify legacy format kernel image(returns:
pointer to a legacy image header if valid image was found otherwise return NULL)
?、啤oad the OS
?、?、Relocate the ramdisk
?、?、From now on, we need the OS boot function
由boot_fn = bootm_os_get_boot_func(images-》os.os);得到boot處理函數(shù)指針并賦給boot_fn。
?、佟㈥P(guān)于參數(shù)images-》os.os,可以由下列定義得知它是系統(tǒng)內(nèi)核的類型,并在(2)中被賦值,若系統(tǒng)類型為linux,則images-》os.os=5。
typedef struct bootm_headers {
。。。
#ifndef USE_HOSTCC /*USE_HOSTCC 沒(méi)有宏定義*/
image_info_t os; /* os image info */
ulong ep; /* entry point of OS */
ulong rd_start, rd_end;/* ramdisk start/end */
char *ft_addr; /* flat dev tree address */
ulong ft_len; /* length of flat device tree */
ulong initrd_start;
ulong initrd_end;
ulong cmdline_start;
ulong cmdline_end;
bd_t *kbd;
#endif
。。。
} bootm_headers_t;1234567891011121314151617181920212223
bootm_headers_t images;1
typedef struct image_info {
ulong start, end; /* start/end of blob */
ulong image_start, image_len; /* start of image within blob, len of image */
ulong load; /* load addr for the image */
uint8_t comp, type, os; /* compression, type of image, os type */
uint8_t arch; /* CPU architecture */
} image_info_t;1234567
得到images.os.os的值:
static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
const void *os_hdr;
。。。
/* get kernel image header, start address and length */
os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
&images, &images.os.image_start, &images.os.image_len);
。。。
/* get image parameters */
switch (genimg_get_format(os_hdr)) {
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
case IMAGE_FORMAT_LEGACY: /*舊系統(tǒng)格式的內(nèi)核映像*/
。。。
images.os.os = image_get_os(os_hdr);
。。。
break;
#endif12345678910111213141516171819202122
?、?、bootm_os_get_boot_func中會(huì)用到函數(shù)指針數(shù)組boot_os,該數(shù)組利用傳入的images.os.os=5的值得到boot處理函數(shù)指針do_bootm_linux返回給boot_fn 。
boot_fn = bootm_os_get_boot_func(images-》os.os);1
boot_os_fn *bootm_os_get_boot_func(int os)
{
。。。
return boot_os[os];
}
123456
static boot_os_fn *boot_os[] = {
[IH_OS_U_BOOT] = do_bootm_standalone,
#ifdef CONFIG_BOOTM_LINUX
?。跧H_OS_LINUX] = do_bootm_linux,
#endif
#ifdef CONFIG_BOOTM_NETBSD
?。跧H_OS_NETBSD] = do_bootm_netbsd,
#endif
。。。
};12345678910
操作系統(tǒng)代號(hào)可在u-boot-2016.05\include\image.h中查看
/*
* Operating System Codes
*/
#define IH_OS_INVALID 0 /* Invalid OS */
#define IH_OS_OPENBSD 1 /* OpenBSD */
#define IH_OS_NETBSD 2 /* NetBSD */
#define IH_OS_FREEBSD 3 /* FreeBSD */
#define IH_OS_4_4BSD 4 /* 4.4BSD */
#define IH_OS_LINUX 5 /* Linux */
。。。
123456789101112
?、?、Call various other states that are not generally used
?、省heck for unsupported subcommand
?、恕ow run the OS! We hope this doesn’t return
if (!ret && (states & BOOTM_STATE_OS_GO))
ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
images, boot_fn);
1234
從do_bootm_states進(jìn)入u-boot-2016.05\common\bootm_os.c:boot_selected_os函數(shù),執(zhí)行boot_fn(state, argc, argv, images);
int boot_selected_os(int argc, char * const argv[], int state,
bootm_headers_t *images, boot_os_fn *boot_fn)
{
。。。
boot_fn(state, argc, argv, images);
。。。
}1234567
?、?、Deal with any fallout
8、執(zhí)行boot_fn(state, argc, argv, images),因?yàn)閎oot_fn=do_bootm_linux,所以相當(dāng)于執(zhí)行do_bootm_linux(state, argc, argv, images),程序跳到u-boot-2016.05\arch\arm\lib\bootm.c:
/* Main Entry point for arm bootm implementation*/
int do_bootm_linux(int flag, int argc, char * const argv[],
bootm_headers_t *images)
{
。。。
boot_jump_linux(images, flag);
。。。
}12345678
do_bootm_linux -》 boot_jump_linux -》 kernel_entry(0, machid, r2);
static void boot_jump_linux(bootm_headers_t *images, int flag)
{
。。。
unsigned long machid = gd-》bd-》bi_arch_number;
char *s;
void (*kernel_entry)(int zero, int arch, uint params);
unsigned long r2;
int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
kernel_entry = (void (*)(int, int, uint))images-》ep; /* ep:entry point of OS*/
s = getenv(“machid”);
if (s) {
if (strict_strtoul(s, 16, &machid) 《 0) {
debug(“strict_strtoul failed!\n”);
return;
}
printf(“Using machid 0x%lx from environment\n”, machid);
}
。。。
if (IMAGE_ENABLE_OF_LIBFDT && images-》ft_len)
r2 = (unsigned long)images-》ft_addr;
else
r2 = gd-》bd-》bi_boot_params;
if (!fake) {
。。。
kernel_entry(0, machid, r2);
}
#endif
}12345678910111213141516171819202122232425262728293031323334
run the OS!
說(shuō)明:
關(guān)于kernel_entry = (void (*)(int, int, uint))images-》ep;中的images-》ep在u-boot-2016.05\common\bootm.c:bootm_find_os函數(shù)中被賦值。
static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
。。。
if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
images.os.load = images.os.image_start;
images.ep += images.os.load;
}
。。。
}
評(píng)論
查看更多