ILD

kernel initramfs 加载过程
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2022-5-25 站点:Inside Linux Development

对于fit image。包含了kernel, initramfs, dtb。

uboot在bootm的时候会调用fdt_initrd()将initramfs的地址start和end设置到device tree的

chosen.linux,initrd-start 和 linux,initrd-end。


进入到内核后:

init/main.c start_kernel()

    setup_arch(&command_line);


arch/arm/kernel/setup.c setup_arch()

1099         if (__atags_pointer)

1100                 atags_vaddr = FDT_VIRT_BASE(__atags_pointer);

1101

1102         setup_processor();

1103         if (atags_vaddr) {

1104                 mdesc = setup_machine_fdt(atags_vaddr);

    

arch/arm/kernel/devtree.c setup_machine_fdt()

253         early_init_dt_scan_nodes();


drivers/of/fdt.c

    early_init_dt_scan_nodes()

        early_init_dt_scan_chosen()

            early_init_dt_check_for_initrd()

             读取设置到initrd_start、initrd_end。phys_initrd_start、phys_initrd_size。


initramfs安装过程

671 static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)

672 {

673         /* Load the built in initramfs */

674         char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);

675         if (err)

676                 panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */

677

678         if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))

679                 goto done;

680

681         if (IS_ENABLED(CONFIG_BLK_DEV_RAM))

682                 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");

683         else

684                 printk(KERN_INFO "Unpacking initramfs...\n");

685

686         err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);

687         if (err) {

688 #ifdef CONFIG_BLK_DEV_RAM

689                 populate_initrd_image(err);

690 #else

691                 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);

692 #endif

693         }


727 static int __init populate_rootfs(void)

728 {

729         initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL,

730                                                  &initramfs_domain);

731         usermodehelper_enable();

732         if (!initramfs_async)

733                 wait_for_initramfs();

734         return 0;

735 }

736 rootfs_initcall(populate_rootfs);


[    0.140753] Unpacking initramfs...

[    0.140809] Initramfs unpacking failed: Input was encoded with settings that are not supported by this XZ decoder


看启动日志,如果有 Unpacking initramfs 则证明initramfs已经传递设置了。


开启xz压缩后,initramfs失败了,原因是xz没有指定校验方法,内核只支持crc32校验方法,所以xz加一个-C crc32选项,就可以:

"$KERNEL_SRC"/usr/gen_initramfs.sh -o "$cpiofile" -u 0 -g 0 "$ROOTFS"

xz -C crc32 -c "$cpiofile" > "$xzfile"


参考:

https://blog.csdn.net/yiyeguzhou100/article/details/78426292

https://insidelinuxdev.net/article/a0datc.html

https://bugzilla.kernel.org/show_bug.cgi?id=50211


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.