内核移植和文件系统制作(2):linux内核最小系统和initramfs文件系统

时间:2022-11-06 09:05:19

linux内核最小系统,使用内核版本:https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.8.1.tar.bz2

1,FL2440板子的基本硬件:晶振12MHZ

CPU

 型号为S3C2440,基于ARM920T,指令集ARMV4,时钟主频400MHz

SDRAM                             

 H57V2562GTR-75C  

 2片*32MB=64MB,挂载于nGCS6 (0x3000 0000)

NANDFLASH

型号:K9F2G08U0B  大小:256MB

Network Adapter  

 DM9000AEP   10/100M自适应,挂载于nGCS4 (0x2000 0000)

LED  

 5个LED(LED0~3)I/O口指示灯,LED5电源指示灯,LED9核心板3.3V指示灯

LED0~LED3分别接GPB6,GPB6,GPB8,GPB10

BEEP(蜂鸣器)       

 接在GPB0/TOUT0



2,首先说明make menuconfig,Kconfig,.config之间的关系:

1)make menuconfig首先读取顶层Kconfig文件,生成选择界面。

2)使能arch/$SRCARCH/Kconfig,最终生成.config(控制编译)

Kconfig文件:

#
# For a description of the syntax of this configuration file,
# see Documentation/kbuild/kconfig-language.txt.
#
mainmenu "Linux/$ARCH $KERNELVERSION Kernel Configuration"


config SRCARCH
    string
    option env="SRCARCH"


source "arch/$SRCARCH/Kconfig"


相当于使能arch/arm/Kconfig,并且递归进入各个目录Kconfig,生成选项界面。

3)当make编译,就会读出生成的.config文件,根据其中的选项编译相应模块。

若选择<y>编译入内核中;<M>编译成驱动模块,需要的时候加载 ;否则不编译。


3,linux-3.8内核配置

1)修改顶层Makefile 

# CROSS_COMPILE specify the prefix used for all executables used
# during compilation. Only gcc and related bin-utils executables
# are prefixed with $(CROSS_COMPILE).
# CROSS_COMPILE can be set on the command line
# make CROSS_COMPILE=ia64-linux-
# Alternatively CROSS_COMPILE can be set in the environment.
# A third alternative is to store a setting in .config so that plain
# "make" in the configured kernel build directory always uses that.
# Default value for CROSS_COMPILE is not to prefix executables
# Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
export KBUILD_BUILDHOST := $(SUBARCH)
ARCH        ?= arm
CROSS_COMPILE   ?= /opt/buildroot-2011.11/arm920t/usr/bin/arm-linux-

# Architecture as present in compile.h
UTS_MACHINE     := $(ARCH)
SRCARCH     := $(ARCH)


# Additional ARCH settings for x86
ifeq ($(ARCH),i386)


2)修改分区arch/arm/mach-s3c24xx/common-smdk.c,以下供参考,也可按自己想法修改。修改分区要与根文件系统指定快设备对应。

static struct mtd_partition smdk_default_nand_part[] = {
    [0] = {
        .name   = "mtdblock0_u-Boot 1MB ",
        .size   = SZ_1M,
        .offset = 0,
    },
    [1] = {
        .name   = "mtdbolck1_kernel 15MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 15,
    },
    [2] = {
        .name   = "mtdbolck2_ramdisk 20MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 20,
    },
    [3] = {
        .name   = "mtdblock3_cramfs 20MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 20,
    },

 [4] = {
        .name   = "mtdblock4_jffs2 40MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 40,
    },
    [5] = {
        .name   = "mtdblock5_yaffs2 40MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 40,
    },
    [6] = {
        .name   = "mtdblock6_ubifs 40MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 40,
    },
    [7] = {
        .name   = "mtdblock7_apps 40MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 40,
    },
    [8] = {
        .name   = "mtdblock8_data 40MB",
        .offset = MTDPART_OFS_NXTBLK,
        .size   = SZ_1M * 40,
    },
};


4)修改板子时钟参数arch/arm/mach-s3c24xx/mach-smdk2440.c

s3c24xx_init_clocks(16934400)修改为s3c24xx_init_clocks(12000000);


5)修改机器代码和u-boot相对应。有u-boot使用的是MACH_TYPE_MINI2440,

机器代码号为1999。修改arch/arm/tools/mach-types如下:

s3c2440            ARCH_S3C2440        S3C2440         1999

mini2440           MACH_MINI2440       MINI2440        362


6)修改drivers/mtd/onenand/onenand_bbt.c如下:

static struct nand_bbt_descr largepage_memorybased = {
    .options = 0,
    .offs = 0,

    .len = 1,

    .pattern = scan_ff_pattern,
 };


7) 修改串口:drivers/tty/serial/samsung.c

#define S3C24XX_SERIAL_NAME    "ttyS"


8)make menuconfig选项配置。FL2440板子使用的s3c2440的cpu与s3c2410类似,

所以选择内核中arch/arm/configs目录下的s3c2410_defconfig为模板修改。

[zhouguangfeng@localhost linux-3.8]$ cp arch/arm/configs/s3c2410_defconfig .
[zhouguangfeng@localhost linux-3.8]$ mv s3c2410_defconfig .config
[zhouguangfeng@localhost linux-3.8]$ make menuconfig



内核移植和文件系统制作(2):linux内核最小系统和initramfs文件系统


在根文件系统已经制作好的前提下,将根文件系统编译入内核:

    General setup  --->  

[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support

(../../rootfs/rootfs) Initramfs source file(s)

红色字体指明根文件系统的目录。make编译。。。(机器不给力的,这时候可以喝杯咖啡。。。)


当编译结束之后,在arch/arm/boot下有一个zImage文件,它是ARM Linux常用的一种压缩

映像文件,uImage是U-boot专用的映像文件,它是在zImage之前加上一个长度为0x40的“头”,

说明这个映像文件的类型、加载位置、生成时间、大小等信息。就是,直接从uImage的

0x40位置开始执行,zImage和uImage没有任何区别。由于最后是由u-boot引导启动,

需要使用u-boot编译后tools目录下mkimage工具,加上一个header。也可以将mkimage放到

/usr/bin下,这里就直接指定它的路径。

修改在Makefile

 # This allow a user to issue only 'make' to build a kernel including modules
 # Defaults to vmlinux, but the arch makefile usually adds further targets
 all: vmlinux
  

cp -f arch/arm/boot/zImage . 
 
../../systools/mkimage -A arm -O linux -T kernel -C none -a 30008000 -e 30008040

-n "Linux Kernel" -d zImage linuxrom-s3c2440_zhou.bin
  rm -f zImage


 ifdef CONFIG_CC_OPTIMIZE_FOR_SIZE
 KBUILD_CFLAGS  += -Os

蓝色标记为mkimage路径,具体每个参数说明,参考http://blog.csdn.net/lusehu/article/details/6438512

完成之后将其拷贝到tftp目录下

[zhouguangfeng@localhost linux-3.8]$ du -h /tftp/linuxrom-s3c2440_zhou.bin
3.4M    /tftp/linuxrom-s3c2440_zhou.bin

[zhouguangfeng@localhost linux-3.8]$ mv linuxrom-s3c2440_zhou.bin /tftp


4,添加u-boot参数支持(u-boot-2010.09):

[ s3c2440@zhou]# set bkr 'tftp 30008000 linuxrom-s3c2440_zhou.bin;nanderase 100000 800000;nand write 30008000 100000 800000'

[ s3c2440@zhou]# set bootcmd_initramfs 'nand read 30008000 100000 800000;bootm 30008000'

[ s3c2440@zhou]# set bootargs 'console=ttyS0,115200 mem=64M init=/linuxrc rw loglevel=7'

[ s3c2440@zhou]# set bootcmd 'run bootcmd_initramfs'

[ s3c2440@zhou]# save

Saving Environment to NAND...

Erasing Nand...

Erasing at 0x60000 -- 100% complete.

Writing to Nand... Done

[ s3c2440@zhou ]# pri

cpu=s3c2440
bbl=tftp 30008000 u-boot-$cpu.bin;nand erase 0 100000;nand write 30008000 0 $filesize
norbbl=erase bank 1; tftp 30008000 u-boot-$cpu.bin;cp.b 30008000 0 $filesize
blx=tftp 30008000 linuxrom-$cpu.bin;nand erase 100000 F00000;nand write 30008000 100000 $filesize
bramdisk=tftp 30800000 ramdisk-$cpu.rootfs;nand erase 1000000 1400000;nand write 30800000 1000000 $filesize
bcramfs=tftp 30800000 cramfs-$cpu.rootfs;nand erase 2400000 1400000;nand write 30800000 2400000 $filesize
bubifs=tftp 30800000 ubifs-$cpu.rootfs;nand erase 8800000 2800000;nand write 30800000 8800000 $filesize
bootcmd_ramdisk=nand read 30008000 100000 F00000;nand read 30800000 1000000 1400000;bootm 30008000
tb=tftp 30008000 linuxrom-$cpu.bin;bootm 30008000 
bootargs_ramdisk=console=ttyS0,115200 root=/dev/ram0 initrd=0x30800000,16M init=/linuxrc mem=64M rw loglevel=7
bootargs_cramfs=console=ttyS0,115200 root=/dev/mtdblock3 rootfstype=cramfs init=/linuxrc mem=64M noinitrd        loglevel=7
bootargs_jffs2=console=ttyS0,115200 root=/dev/mtdblock4 rootfstype=jffs2 init=/linuxrc mem=64M rw noinitrd         loglevel=7
bootargs_ubifs=console=ttyS0,115200 ubi.mtd=6 root=ubi0:rootfs rootwait rootfstype=ubifs init=/linuxrc mem=64M     noinitrd rw loglevel=7
mtdparts=mtdparts=nand0:1m(uboot),15m(kernel),20m(ramdisk),20m(cramfs),20m(jffs2),20m(yaffs2),20m(ubifs),-(users)
baudrate=115200
ethaddr=08:08:11:18:12:27
ethact=dm9000
bootcmd_yaffs=nand read 30008000 100000 800000;bootm 30008000
bootcmd_yaffs2=nand read 30008000 100000 800000;bootm 30008000
bootdelay=1
bootcmd=run bootcmd_rootfs
bootcmd_rootfs=nand read 30008000 100000 800000;bootm 30008000
byaffs2=tftp 30008000 rootfs_yaffs2.bin;nand erase 6000000 2800000;nand write 30008000 6000000 400000
bkr=tftp 30008000 linuxrom-s3c2440_zhou.bin;nand erase 100000 f00000;nand write 30008000 100000 800000
filesize=350440
fileaddr=30008000
gatewayip=192.168.1.1
netmask=255.255.255.0
ipaddr=192.168.1.224
serverip=192.168.1.3
bootargs=console=ttyS0,115200 mem=64M rw loglevel=7 init=/linuxrc
bootargs_initramfs=console=ttyS0,115200 mem=64M rw loglevel=7 init=/linuxrc
stdin=serial
stdout=serial
stderr=serial


Environment size: 2144/131068 bytes
[ s3c2440@zhou ]# OK!

[ s3c2440@zhou ]# run bkr 
dm9000 i/o: 0x20000300, id: 0x90000a46 
DM9000: running in 16 bit mode
MAC: 08:08:11:18:12:27
operating at 100M full duplex mode
Using dm9000 device
TFTP from server 192.168.1.3; our IP address is 192.168.1.224
Filename 'linuxrom-s3c2440_zhou.bin'.
Load address: 0x30008000
Loading: #################################################################
         #################################################################
         #################################################################
         ##########################################
done
Bytes transferred = 3474496 (350440 hex)


NAND erase: device 0 offset 0x100000, size 0xf00000
Erasing at 0xfe0000 -- 100% complete.
OK


NAND write: device 0 offset 0x100000, size 0x800000
 8388608 bytes written: OK
[ s3c2440@zhou ]# boot



NAND read: device 0 offset 0x100000, size 0x800000
 8388608 bytes read: OK
## Booting kernel from Legacy Image at 30008000 ...
   Image Name:   Linux Kernel
   Created:      2014-08-04  10:24:47 UTC
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    3474432 Bytes = 3.3 MiB
   Load Address: 30008000
   Entry Point:  30008040
   Verifying Checksum ... OK
   XIP Kernel Image ... OK
OK
OS entry point: 30008040
Image entry point=30008040


Starting kernel ...


Uncompressing Linux... done, booting the kernel.
Booting Linux on physical CPU 0x0

。。。。。。

Scanning device for bad blocks
Bad eraseblock 236 at 0x000001d80000
Bad eraseblock 704 at 0x000005800000
Bad eraseblock 705 at 0x000005820000
Bad eraseblock 717 at 0x0000059a0000
Bad eraseblock 1251 at 0x000009c60000
Bad eraseblock 1631 at 0x00000cbe0000
Bad eraseblock 1771 at 0x00000dd60000
Bad eraseblock 1947 at 0x00000f360000
Creating 9 MTD partitions on "NAND":
0x000000000000-0x000000100000 : "mtdblock0_u-Boot 1MB "
0x000000100000-0x000001000000 : "mtdbolck1_kernel 15MB"
0x000001000000-0x000002400000 : "mtdbolck2_ramdisk 20MB"
0x000002400000-0x000003800000 : "mtdblock3_cramfs 20MB"
0x000003800000-0x000006000000 : "mtdblock4_jffs2 40MB"
0x000006000000-0x000008800000 : "mtdblock5_yaffs2 40MB"
0x000008800000-0x00000b000000 : "mtdblock6_ubifs 40MB"
0x00000b000000-0x00000d800000 : "mtdblock7_apps 40MB"
0x00000d800000-0x000010000000 : "mtdblock8_data 40MB"

。。。。。。

Copyright (C) 2014 zhouguangfeng<zhouguangfeng@gmail.com>
zhouxiaoxing login: root
>: ls
apps     data     etc      init     linuxrc  proc     sbin     tmp      var
bin      dev      info     lib      mnt      root     sys      usr
>: OK


其中u-boot参数设置参考U-boot的环境变量值得注意的有两个: bootcmd 和bootargs