ubuntu下使用qemu模擬ARM(三)------編譯內核

上一章將u-boot編譯完成並用Qemu進行了測試,這一單我們來編譯內核

Linux內核編譯

下載Linux內核

官方下載地址:https://www.kernel.org/pub/linux/kernel/
我們下載最新內核:linux-4.9.tar.xz

下載補丁文件件:patch-4.9.gz

1.建立放內核的文件夾

sudo mkdir linux-kernel

sudo chmod 777 linux-kernel/


將內核文件和補丁都放入到 /qemu_arm/linux-kernel中

2.tar.xz文件的解壓:

XZ壓縮最新壓縮率之王

xz壓縮文件方法或命令
xz -z 要壓縮的文件

如果要保留被壓縮的文件加上參數 -k ,如果要設置壓縮率加入參數 -0 到 -9調節壓縮率。如果不設置,默認壓縮等級是6.
xz解壓文件方法或命令
xz -d 要解壓的文件

同樣使用 -k 參數來保留被解壓縮的文件。

創建或解壓tar.xz文件的方法
tar.xz文件的壓縮或解壓縮必須分兩步來完成,如下示例:
創建tar.xz文件:只要先 tar cvf xxx.tar xxx/ 這樣創建xxx.tar文件先,然後使用 xz -z xxx.tar 來將 xxx.tar壓縮成爲 xxx.tar.xz
解壓tar.xz文件:先 xz -d xxx.tar.xz 將 xxx.tar.xz解壓成 xxx.tar 然後,再用 tar xvf xxx.tar來解包。


book@book-virtual-machine:/qemu_arm/linux-kernel$ ls
linux-4.9.tar.xz  patch-4.9


將linux-4.9.tar.xz解壓到/qemu_arm/linux-kernel中

gz文件的解壓:

gunzip patch-4.9.gz 


book@book-virtual-machine:/qemu_arm/linux-kernel$ xz -d linux-4.9.tar.xz 
book@book-virtual-machine:/qemu_arm/linux-kernel$ ls
linux-4.9.tar  patch-4.9

book@book-virtual-machine:/qemu_arm/linux-kernel$ tar xf linux-4.9.tar

book@book-virtual-machine:/qemu_arm/linux-kernel$ ls
linux-4.9  linux-4.9.tar  patch-4.9

3)打最新的補丁

 cd linux-4.9/

patch -p1 <../patch-4.9

上述打補丁命令解析patch -p1 <../patch-4.9

patch:聲明補丁命令

-p1:表示忽略掉補丁文件內位置信息中的第幾個“/”之前的內容,這裏是1就表示忽略第一個/之前的內容。補丁文件內容:


 由打補丁命令 patch -p1 <../patch-4.9可以看出此時已經在/linux-4.9目錄下了,因此忽略掉第一個反斜槓


../patch-4.9:“..”表示回到上一級目錄下,找到u-boot-1.1.6_jz2440.patch文件


因爲打補丁要選一大堆,還是用下載的內核執行操作了.

3.編譯內核 

將頂層的MAKEFILE打開找到下面的內容(255和256行)

ARCH ?= $(SUBARCH)
CROSS_COMPILE ?= $(CONFIG_CROSS_COMPILE:"%"=%)

將上面的內容修改如下:

ARCH ?= arm
CROSS_COMPILE ?= arm-none-linux-gnueabi-

在/qemu_arm/linux-kernel/linux-4.9/arch/arm/configs目錄中找到vexpress_defconfig所以可以用

make vexpress_defconfig

//若沒有修改Makefile文件中的內容則用下面的命令:

// make CROSS_COMPILE=arm-none-linux-gnueabi- ARCH=arm vexpress_defconfig

//調整一些具體細節項  

make menuconfig //然後就可以編譯了  

//若沒有修改makefile內容則用下面的命令: make menuconfig ARCH=arm

make

//若沒有修改makefile內容則用下面的命令:make CROSS_COMPILE=arm-none-linux-gnueabi- ARCH=arm

生成的內核鐿像位於arch/arm/boot/zImage



4.用Qemu測試內核

qemu-system-arm -M vexpress-a9 -m 512M -kernel  /qemu_arm/linux-kernel/linux-4.9/arch/arm/boot/zImage -nographic -append "console=ttyAMA0"


此時無法啓動內核,不知是什麼原因.將linux-4.9內核換成linux-3.16然後按上面的步驟做下來,再用Qemu來測試,結果如下:


qemu-system-arm -M vexpress-a9 -m 512M -kernel  /qemu_arm/linux-kernel/linux-3.16.39/arch/arm/boot/zImage -nographic -append "console=ttyAMA0"


pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument
Booting Linux on physical CPU 0x0
Initializing cgroup subsys cpuset
Linux version 3.16.39 (book@book-virtual-machine) (gcc version 4.8.3 20140320 (prerelease) (Sourcery CodeBench Lite 2014.05-29) ) #1 SMP Mon Jan 23 16:32:37 CST 2017
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c53c7d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
Machine: ARM-Versatile Express
Memory policy: Data cache writeback
CPU: All CPU(s) started in SVC mode.
sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 178956969942ns
PERCPU: Embedded 7 pages/cpu @9fbed000 s6976 r8192 d13504 u32768
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 130048
Kernel command line: console=ttyAMA0
PID hash table entries: 2048 (order: 1, 8192 bytes)
Dentry cache hash table entries: 65536 (order: 6, 262144 bytes)
Inode-cache hash table entries: 32768 (order: 5, 131072 bytes)
Memory: 513184K/524288K available (4649K kernel code, 191K rwdata, 1292K rodata, 246K init, 150K bss, 11104K reserved)
Virtual kernel memory layout:
    vector  : 0xffff0000 - 0xffff1000   (   4 kB)
    fixmap  : 0xffc00000 - 0xffe00000   (2048 kB)
    vmalloc : 0xa0800000 - 0xff000000   (1512 MB)
    lowmem  : 0x80000000 - 0xa0000000   ( 512 MB)
    modules : 0x7f000000 - 0x80000000   (  16 MB)
      .text : 0x80008000 - 0x805d5a18   (5943 kB)
      .init : 0x805d6000 - 0x80613b40   ( 247 kB)
      .data : 0x80614000 - 0x80643f20   ( 192 kB)
       .bss : 0x80643f28 - 0x806697ac   ( 151 kB)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
Hierarchical RCU implementation.
        RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=1.
RCU: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=1
NR_IRQS:16 nr_irqs:16 16
GIC CPU mask not found - kernel will fail to boot.
GIC CPU mask not found - kernel will fail to boot.
smp_twd: clock not found -2
L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000
L2C-310 enabling early BRESP for Cortex-A9
L2C-310 full line of zeros enabled for Cortex-A9
L2C-310 dynamic clock gating disabled, standby mode disabled
L2C-310 cache controller enabled, 8 ways, 128 kB
L2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
Console: colour dummy device 80x30
Calibrating local timer... 105.35MHz.
Calibrating delay loop... 272.79 BogoMIPS (lpj=1363968)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 1024 (order: 0, 4096 bytes)
Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes)
CPU: Testing write buffer coherency: ok
missing device node for CPU 0
CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
Setting up static identity map for 0x6046bae0 - 0x6046bb38
Brought up 1 CPUs
SMP: Total of 1 processors activated (272.79 BogoMIPS).
CPU: All CPU(s) started in SVC mode.
devtmpfs: initialized
VFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 0
regulator-dummy: no parameters
NET: Registered protocol family 16
DMA: preallocated 256 KiB pool for atomic coherent allocations
cpuidle: using governor ladder
cpuidle: using governor menu
hw-breakpoint: debug architecture 0x4 unsupported.
Serial: AMBA PL011 UART driver
mb:uart0: ttyAMA0 at MMIO 0x10009000 (irq = 37, base_baud = 0) is a PL011 rev1
console [ttyAMA0] enabled
mb:uart1: ttyAMA1 at MMIO 0x1000a000 (irq = 38, base_baud = 0) is a PL011 rev1
mb:uart2: ttyAMA2 at MMIO 0x1000b000 (irq = 39, base_baud = 0) is a PL011 rev1
mb:uart3: ttyAMA3 at MMIO 0x1000c000 (irq = 40, base_baud = 0) is a PL011 rev1
fixed-dummy: no parameters
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
Advanced Linux Sound Architecture Driver Initialized.
Switched to clocksource v2m-timer1
NET: Registered protocol family 2
TCP established hash table entries: 4096 (order: 2, 16384 bytes)
TCP bind hash table entries: 4096 (order: 3, 32768 bytes)
TCP: Hash tables configured (established 4096 bind 4096)
TCP: reno registered
UDP hash table entries: 256 (order: 1, 8192 bytes)
UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
NET: Registered protocol family 1
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
CPU PMU: probing PMU on CPU 0
hw perfevents: enabled with ARMv7 Cortex-A9 PMU driver, 1 counters available
futex hash table entries: 256 (order: 2, 16384 bytes)
squashfs: version 4.0 (2009/01/31) Phillip Lougher
jffs2: version 2.2. (NAND) 漏 2001-2006 Red Hat, Inc.
9p: Installing v9fs 9p2000 file system support
msgmni has been set to 1002
io scheduler noop registered (default)
clcd-pl11x ct:clcd: PL111 rev2 at 0x10020000
clcd-pl11x ct:clcd: CT-CA9X4 hardware, XVGA display
Console: switching to colour frame buffer device 128x48
physmap platform flash device: 04000000 at 40000000
physmap-flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
physmap platform flash device: 04000000 at 44000000
physmap-flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
Concatenating MTD devices:
(0): "physmap-flash"
(1): "physmap-flash"
into device "physmap-flash"
libphy: smsc911x-mdio: probed
smsc911x smsc911x eth0: attached PHY driver [Generic PHY] (mii_bus:phy_addr=smsc911x-fffffff:01, irq=-1)
smsc911x smsc911x eth0: MAC Address: 52:54:00:12:34:56
isp1760 isp1760: NXP ISP1760 USB Host Controller
isp1760 isp1760: new USB bus registered, assigned bus number 1
isp1760 isp1760: Scratch test failed.
isp1760 isp1760: can't setup: -19
isp1760 isp1760: USB bus 1 deregistered
isp1760: Failed to register the HCD device
usbcore: registered new interface driver usb-storage
mousedev: PS/2 mouse device common for all mice
rtc-pl031 mb:rtc: rtc core: registered pl031 as rtc0
mmci-pl18x mb:mmci: mmc0: PL181 manf 41 rev0 at 0x10005000 irq 41,42 (pio)
ledtrig-cpu: registered to indicate activity on CPUs
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
input: AT Raw Set 2 keyboard as /devices/mb:kmi0/serio0/input/input0
aaci-pl041 mb:aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 43
aaci-pl041 mb:aaci: FIFO 512 entries
oprofile: using arm/armv7-ca9
TCP: cubic registered
NET: Registered protocol family 17
9pnet: Installing 9P2000 support
rtc-pl031 mb:rtc: setting system clock to 2017-01-23 08:40:30 UTC (1485160830)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 43
input: ImExPS/2 Generic Explorer Mouse as /devices/mb:kmi1/serio1/input/input2
VFS: Cannot open root device "(null)" or unknown-block(0,0): error -6
Please append a correct "root=" boot option; here are the available partitions:
1f00          131072 mtdblock0  (driver?)
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 3.16.39 #1
[<8001474c>] (unwind_backtrace) from [<8001162c>] (show_stack+0x10/0x14)
[<8001162c>] (show_stack) from [<80466cc8>] (dump_stack+0x94/0xa8)
[<80466cc8>] (dump_stack) from [<80463e48>] (panic+0xa0/0x200)
[<80463e48>] (panic) from [<805d7194>] (mount_block_root+0x1d0/0x260)
[<805d7194>] (mount_block_root) from [<805d732c>] (mount_root+0x108/0x110)
[<805d732c>] (mount_root) from [<805d748c>] (prepare_namespace+0x158/0x1a0)
[<805d748c>] (prepare_namespace) from [<805d6e20>] (kernel_init_freeable+0x264/0x274)
[<805d6e20>] (kernel_init_freeable) from [<804622bc>] (kernel_init+0x8/0xf0)
[<804622bc>] (kernel_init) from [<8000e3c0>] (ret_from_fork+0x14/0x34)
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)


最後的提示爲不能掛接根文件系統,由此可見內核編譯後能正確執行.










發佈了21 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章