一、BootLoader引导内核过程
1、Bootloader的工作
1.1、将内核读入内存1.2、保存内核启动参数到指定位置,内核启动时去这个位置解析参数1.3、启动内核、传入机器ID
二、内核的启动流程
内核首要目的是挂载根文件系统,启动应用程序,内核启动的过程大致为以下几步:
1.检查CPU和机器类型2.进行堆栈、MMU等其他程序运行关键的东西进行初始化3.打印内核信息4.执行各种模块的初始化5.挂接根文件系统6.启动第一个init进程
对于ARM的处理器,内核第一个启动的文件是arc/arm/kernel下面的head.S文件
第一阶段:
首先截取部分head.S文件
ENTRY(stext) THUMB( adr r9, BSYM(1f) ) @ Kernel is always entered in ARM. THUMB( bx r9 ) @ If this is a Thumb-2 kernel, THUMB( .thumb ) @ switch to Thumb now. THUMB(1: ) setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9 @ ensure svc mode @ and irqs disabled mrc p15, 0, r9, c0, c0 @ get processor id bl __lookup_processor_type @ r5=procinfo r9=cpuid movs r10, r5 @ invalid processor (r5=0)? THUMB( it eq ) @ force fixup-able long branch encoding beq __error_p @ yes, error 'p' #ifdef CONFIG_ARM_LPAE mrc p15, 0, r3, c0, c1, 4 @ read ID_MMFR0 and r3, r3, #0xf @ extract VMSA support cmp r3, #5 @ long-descriptor translation table format? THUMB( it lo ) @ force fixup-able long branch encoding blo __error_p @ only classic page table format #endif
第一步,执行的是__lookup_processor_type,这个函数是检查处理器型号,它读取你的板子的CPU型号与内核支持的处理器进行比较看是否能够处理。
第二步,检查机器型号,它会读取你bootloader传进来的机器ID和他能够处 理的机器ID进行比较看是否能够处理。内核的ID号定义在arc/arm/tool/mach_types文件中MACH_TYPE_xxxx宏定义。内 核究竟就如何检查是否是它支持的机器的呢?实际上每个机器都会在/arc/arm/mach-xxxx/smdk-xxxx.c文件中有个描述特定机器的 数据结构,
MACHINE_START(S3C2440, "SMDK2440") /* Maintainer: Ben Dooks*/ .atag_offset = 0x100, .init_irq = s3c24xx_init_irq, .map_io = smdk2440_map_io, .init_machine = smdk2440_machine_init, .timer = &s3c24xx_timer, .restart = s3c244x_restart, MACHINE_END
MACHINE_START和 MACHINE_END实际上被展开成一个结构体
#defineMACHINE_START(_type,_name) \ staticconst struct machine_desc __mach_desc_##_type \ __used \ __attribute__((__section__(".arch.info.init")))= { \ .nr =MACH_TYPE_##_type, \ .name =_name, #defineMACHINE_END \ };
于是上面的数据结构就被展开为
staticconst struct machine_desc __mach_desc_S3C2440 \ __used \ __attribute__((__section__(".arch.info.init")))= { \ .nr =MACH_TYPE_S3C2440, \ .name =”SMDK2440”,}; .phys_io = S3C2410_PA_UART, .io_pg_offst = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc, .boot_params = S3C2410_SDRAM_PA + 0x100, .init_irq =s3c24xx_init_irq, .map_io =smdk2440_map_io, .init_machine = smdk2440_machine_init, .timer =&s3c24xx_timer, }
每个机器都会有一个machine_desc__mach_desc结构,内核通过检查每个machine_desc__mach_desc的nr 号和bootloader传上来的ID进行比较,如果相同,内核就认为支持该机器,而且内核在后面的工作中会调用该机器的 machine_desc__mach_desc_结构中的方法进行一些初始化工作。
第三步,创建一级页表
第四步,在R13中保存__switch_data 这个函数的地址,在第四步使能mmu完成后会跳到该函数执行。
第五步,执行的是__enable_mmu,它是使能MMU,这个函数调用了__turn_mmu_on函数,让后在_turn_mmu_on在最 后将第三步赋给R13的值传给了PC指针 (mov pc, r13),于是内核开始跳到__switch_data这个函数开始执行。
我们再来看arch/arm/kenel/head-common.S这个文件中的__switch_data函数
/* * The following fragment of code is executed with the MMU on in MMU mode, * and uses absolute addresses; this is not position independent. * * r0 = cp#15 control register * r1 = machine ID * r2 = atags/dtb pointer * r9 = processor ID */ __INIT __mmap_switched: adr r3, __mmap_switched_data ldmia r3!, {r4, r5, r6, r7} cmp r4, r5 @ Copy data segment if needed 1: cmpne r5, r6 ldrne fp, [r4], #4 strne fp, [r5], #4 bne 1b mov fp, #0 @ Clear BSS (and zero fp) 1: cmp r6, r7 strcc fp, [r6],#4 bcc 1b ARM( ldmia r3, {r4, r5, r6, r7, sp}) THUMB( ldmia r3, {r4, r5, r6, r7} ) THUMB( ldr sp, [r3, #16] ) str r9, [r4] @ Save processor ID str r1, [r5] @ Save machine type str r2, [r6] @ Save atags pointer bic r4, r0, #CR_A @ Clear 'A' bit stmia r7, {r0, r4} @ Save control register values b start_kernel ENDPROC(__mmap_switched) .align 2 .type __mmap_switched_data, %object __mmap_switched_data: .long __data_loc @ r4 .long _sdata @ r5 .long __bss_start @ r6 .long _end @ r7 .long processor_id @ r4 .long __machine_arch_type @ r5 .long __atags_pointer @ r6 .long cr_alignment @ r7 .long init_thread_union + THREAD_START_SP @ sp .size __mmap_switched_data, . - __mmap_switched_data
这个函数做的工作是,复制数据段清楚BBS段,设置堆在指针,然后保存处理器内核和机器内核等工作,最后跳到start_kernel函数。于是内核开始执行第二阶段。
第二阶段:
init/目录下的main.c的start_kernel函数
asmlinkage void __init start_kernel(void)
在start_kernel首先是打印内核信息,然后对bootloader传进来的一些参数进行处理,再接着执行各种各样的初始化,在这其中会初始化控制台。最后会调用rest_init();
我们再来看rest_init()函数
static noinline void __init_refok rest_init(void)
他启动了kernel_init这个函数,再来看kerne_init函数
static int __init kernel_init(void * unused) { /* * Wait until kthreadd is all set-up. */ wait_for_completion(&kthreadd_done); /* Now the scheduler is fully set up and can do blocking allocations */ gfp_allowed_mask = __GFP_BITS_MASK; /* * init can allocate pages on any node */ set_mems_allowed(node_states[N_HIGH_MEMORY]); /* * init can run on any cpu. */ set_cpus_allowed_ptr(current, cpu_all_mask); cad_pid = task_pid(current); smp_prepare_cpus(setup_max_cpus); do_pre_smp_initcalls(); lockup_detector_init(); smp_init(); sched_init_smp(); do_basic_setup(); /* Open the /dev/console on the rootfs, this should never fail */ if (sys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0) printk(KERN_WARNING "Warning: unable to open an initial console.\n"); (void) sys_dup(0); (void) sys_dup(0); /* * check if there is an early userspace init. If yes, let it do all * the work */ if (!ramdisk_execute_command) ramdisk_execute_command = "/init"; if (sys_access((const char __user *) ramdisk_execute_command, 0) != 0) { ramdisk_execute_command = NULL; prepare_namespace(); } /* * Ok, we have completed the initial bootup, and * we're essentially up and running. Get rid of the * initmem segments and start the user-mode stuff.. */ init_post(); return 0; }
kernel_init先调用了prepare_namespace();然后调用了init_post函数
在prepare_namespace()函数里 调用mount_root()函数,挂载根文件系统;
三、移植linux3.4.2到JZ2440
1、解压tar xjf linux-3.4.2.tar.bz2
2、进入解压后的文件目录,修改顶层Makefile
vim Makefile |
修改架构为 ARM 以及编译器
ARCH=arm
CROSS_COMPILE=arm-linux-
3、选择默认配置
find -name"*defconfig"
4、在解压后文件目录下,配置,生成.config文件
make s3c2410_defconfig
5、查看支持的单板
vim .config
6、编译
make uImage
7、u-boot2012里默认的是193机器ID,设置机器ID为362使用SMDK2440,在uboot中设置机器ID
set machid 16a save
8、在uboot中设置启动行参数并修改smdk2440单板的晶振信息12M
bootargs noinitrd root=/dev/nfs nfsroot=192.168.1.112:/opt/filesystem ip=192.168.1.130:192.168.1.112:192.168.1.1:255,255,255,0::eth0:off init=/linuxrc console=ttySAC0,115200
修改文件mach-smdk2440.c的晶振信息12M
static void __init smdk2440_map_io(void) { s3c24xx_init_io(smdk2440_iodesc, ARRAY_SIZE(smdk2440_iodesc)); s3c24xx_init_clocks(12000000); s3c24xx_init_uarts(smdk2440_uartcfgs, ARRAY_SIZE(smdk2440_uartcfgs)); }
四、修改分区
我们经常用的内核打印分区信息如下
Creating 4 MTD partitions on "NAND": 0x000000000000-0x000000040000 : "bootloader" 0x000000040000-0x000000060000 : "params" 0x000000060000-0x000000460000 : "kernel" 0x000000460000-0x000010000000 : "rootfs"
这些分区是通过在文件linux-2.6.22.6\arch\arm\plat-s3c24xx/Common-smdk.c设置的
/* NAND parititon from 2.4.18-swl5 */ static struct mtd_partition smdk_default_nand_part[] = { [0] = { .name = "bootloader", .size = SZ_256K, .offset = 0, }, [1] = { .name = "params", .offset = MTDPART_OFS_APPEND, .size = SZ_128K, }, [2] = { .name = "kernel", .offset = MTDPART_OFS_APPEND, .size = SZ_4M, }, [3] = { .name = "rootfs", .offset = MTDPART_OFS_APPEND, .size = MTDPART_SIZ_FULL, }, };
五、添加网卡驱动
修改arch/arm/mach-s3c24xx/mach-smdk2440.c
1 添加头文件#include
2 网卡基地址
#define MACH_SMDK2440_DM9K_BASE (S3C2410_CS4 + 0x300)
3 添加资源和设备
/* DM9000AEP 10/100 ethernet controller */ static struct resource smdk2440_dm9k_resource[] = { [0] = { .start = MACH_SMDK2440_DM9K_BASE, .end = MACH_SMDK2440_DM9K_BASE + 3, .flags = IORESOURCE_MEM }, [1] = { .start = MACH_SMDK2440_DM9K_BASE + 4, .end = MACH_SMDK2440_DM9K_BASE + 7, .flags = IORESOURCE_MEM }, [2] = { .start = IRQ_EINT7, .end = IRQ_EINT7, .flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHEDGE, } }; /* * The DM9000 has no eeprom, and it's MAC address is set by * the bootloader before starting the kernel. */ static struct dm9000_plat_data smdk2440_dm9k_pdata = { .flags = (DM9000_PLATF_16BITONLY | DM9000_PLATF_NO_EEPROM), }; static struct platform_device smdk2440_device_eth = { .name = "dm9000", .id = -1, .num_resources = ARRAY_SIZE(smdk2440_dm9k_resource), .resource = smdk2440_dm9k_resource, .dev = { .platform_data = &smdk2440_dm9k_pdata, }, }; static struct platform_device *smdk2440_devices[] __initdata = { &s3c_device_ohci, &s3c_device_lcd, &s3c_device_wdt, &s3c_device_i2c0, &s3c_device_iis, &smdk2440_device_eth, };
综上,make uImage 完成Linux3.4.2的移植,并添加了网卡驱动。