make 之后会生成三个文件,一个vmlinux ,一个Image,一个zImage,vmlinux是ELF格式的不能直接运行,Image 就是使用 objcopy 取消掉 vmlinux 中的一些其他信息得到的,而zImage为内核的一种进一步映像压缩文件,Image大约为4M,而zImage不到2M。
然后make uImage就会生成 uImage文件,uImage顾名思义,u代表uboot,
,
如果要引导zImage镜像需要修改uboot do_bootm函数
,
typedef struct image_header {
uint32_t ih_magic; /* Image Header Magic Number */
uint32_t ih_hcrc; /* Image Header CRC Checksum */
uint32_t ih_time; /* Image Creation Timestamp */
uint32_t ih_size; /* Image Data Size */
uint32_t ih_load; /* Data Load Address加载地址 */
uint32_t ih_ep; /* Entry Point Address入口地址 */
uint32_t ih_dcrc; /* Image Data CRC Checksum */
uint8_t ih_os; /* Operating System */
uint8_t ih_arch; /* CPU architecture */
uint8_t ih_type; /* Image Type */
uint8_t ih_comp; /* Compression Type */
uint8_t ih_name[IH_NMLEN]; /* Image Name */
} image_header_t;
我们在uboot环境变量里面一般会有这么一句,
它的意思就是把内核下载(读)到0x46000000地方,0x46000000就是下载地址
typedef struct image_header {
uint32_t ih_load; /* Data Load Address加载地址 */
uint32_t ih_ep; /* Entry Point Address入口地址 */
} image_header_t;
编译的时候可以指定加载地址
图中可以看出加载地址 = 入口地址
1、在加载地址和入口地址相同 且 下载地址和加载地址不同情况下:
此时可以正常启动
2、在加载地址和入口地址相同 且 下载地址和加载地址相同情况下:
此时无法正常启动,因为此时uboot不会去掉头部64字节,如果直接从入口地址(==加载地址)运行肯定会失败,正确的做法就是不要让下载地址和加载地址相同
图片来源:https://www.cnblogs.com/Oude/p/12217750.html
1、入口地址和加载地址不同(正常是相差64直接),下载地址和加载地址相同
正常启动,因为此时uboot不会去掉头部也不做拷贝,然后正好从加载地址+64字节的入口地址启动内核
2、入口地址和加载地址不同,下载地址和加载地址不同的情况
无法正常启动,因为此时uboot会去掉头部64字节拷贝到加载地址,但是去掉64字节已经就是zImage了,在从加载地址+64字节地方启动就不是正在的入口地址了
/tools/mkimage.c解析
-A ==> set architecture to ‘arch’
-O ==> set operating system to ‘os’
-T ==> set image type to ‘type’ “kernel或是ramdisk”
-C ==> set compression type ‘comp’
-n ==> set image name to ‘name’
-d==> use image data from ‘datafile’
-x ==> set XIP (execute in place,即不进行文件的拷贝,在当前位置执行)
vim ./scripts/Makefile.lib 修改Makfile.lib文件
编译信息如下:
说明上面的改法是对的
int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
#ifdef CONFIG_NEEDS_MANUAL_RELOC
static int relocated = 0;
return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
BOOTM_STATE_LOADOS |
#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
BOOTM_STATE_RAMDISK |
#endif
#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);
}
bootm_start(cmdtp, flag, argc, argv); 对内核头部分析
bootm_find_other(cmdtp, flag, argc, argv); 查找内存中是否有设备树文件或者是文件系统
bootm_load_os(images, &load_end, 0); 加载内核到加载地址处,如果下载地址和加载地址相同则不需要加载
int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
int states, bootm_headers_t *images, int boot_progress)
{
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); //包括查找设备树,和文件系统
/* Load the OS */
if (!ret && (states & BOOTM_STATE_LOADOS)) {
ret = bootm_load_os(images, &load_end, 0);
}
/* Relocate the ramdisk */
#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
if (!ret && (states & BOOTM_STATE_RAMDISK)) {
ret = boot_ramdisk_high(&images->lmb, images->rd_start,
}
}
#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);
ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images); //调用do_bootm_linux函数跳转
}
}
int do_bootm_linux(int flag, int argc, char * const argv[],
bootm_headers_t *images)
{
boot_prep_linux(images);
boot_jump_linux(images, flag);
return 0;
}
static void boot_jump_linux(bootm_headers_t *images, int flag)
{
#ifdef CONFIG_ARM64
void (*kernel_entry)(void *fdt_addr, void *res0, void *res1,
void *res2);
int fake = (flag & BOOTM_STATE_OS_FAKE_GO);
kernel_entry = (void (*)(void *fdt_addr, void *res0, void *res1,
void *res2))images->ep; //得到入口函数地址
if (IMAGE_ENABLE_OF_LIBFDT && images->ft_len)
r2 = (unsigned long)images->ft_addr; //得到设备树地址
else
r2 = gd->bd->bi_boot_params;
kernel_entry(0, machid, r2); //给内核传递三个参数,机器码,设备树地址
}
#endif
}