用QEMU模拟运行uboot从SD卡启动Linux

时间:2023-03-08 20:09:33
用QEMU模拟运行uboot从SD卡启动Linux
平台:Qemu + vexpress-a9
u-boot:u-boot-2019.10
Linux:linux-4.14.13
    之前介绍过用Qemu模拟运行uboot,然后从网络启动linux(用Qemu模拟vexpress-a9 (三)--- 实现用u-boot引导Linux内核),下面介绍用Qemu运行uboot,然后将存放在虚拟出来的SD卡里加载linux kernel、设备树,并运行。

一、编译uboot

    下载最新的uboot,编译vexpress-a9:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- vexpress_ca9x4_defconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j8
    得到编译生成的u-boot

二、编译linux内核

cross_compile=arm-linux-gnueabihf-
make CROSS_COMPILE=$cross_compile ARCH=arm vexpress_defconfig
make CROSS_COMPILE=$cross_compile ARCH=arm menuconfig
make CROSS_COMPILE=$cross_compile ARCH=arm zImage -j4
make CROSS_COMPILE=$cross_compile ARCH=arm dtbs

三、制作SD卡镜像

1、生成一个空的SD卡镜像
dd if=/dev/zero of=uboot.disk bs=1M count=
2、创建GPT分区,下面创建了两个分区,一个用来存放kernel和设备树,另一个存放根文件系统
sgdisk -n ::+10M -c :kernel uboot.disk
sgdisk -n :: -c :rootfs uboot.disk
查看分区:
$ sgdisk -p uboot.disk
Disk uboot.disk: sectors, 1024.0 MiB
Sector size (logical): bytes
Disk identifier (GUID): 04963A5B-34CF-4DEE-B610-F40257C45F6D
Partition table holds up to entries
Main partition table begins at sector and ends at sector
First usable sector is , last usable sector is
Partitions will be aligned on -sector boundaries
Total free space is sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name
10.0 MiB kernel
1013.0 MiB rootfs
3、寻找一个空闲的loop设备
$ losetup -f
/dev/loop16
4、将SD卡镜像映射到loop设备上
sudo losetup /dev/loop16 uboot.disk
sudo partprobe /dev/loop16
此时会看到/dev/loop16p1和/dev/loop16p2两个节点
5、格式化
sudo mkfs.ext4 /dev/loop16p1
sudo mkfs.ext4 /dev/loop16p2
6、挂载
sudo mount -t ext4 /dev/loop16p1 p1/
sudo mount -t ext4 /dev/loop16p2 p2/
7、拷贝文件
sudo cp linux-4.14./arch/arm/boot/zImage p1/
sudo cp linux-4.14./arch/arm/boot/dts/vexpress-v2*.dtb p1/
sudo cp -raf ../rootfs/rootfs/* ./p2
8、umount
sudo umount p1 p2
sudo losetup -d /dev/loop16

四、启动uboot

uboot_image=./u-boot-2019.10/u-boot
qemu_path=/home/pengdl/work/Qemu/qemu-4.1./build/arm-softmmu ${qemu_path}/qemu-system-arm \
-M vexpress-a9 \
-m 1024M \
-smp \
-nographic \
-kernel ${uboot_image} \
-sd ./uboot.disk
启动log
U-Boot 2019.10-g61a73d0f-dirty (Jan   - :: +)

DRAM:  GiB
WARNING: Caches not enabled
Flash: MiB
MMC: MMC:
*** Warning - bad CRC, using default environment In: serial
Out: serial
Err: serial
Net: smc911x-
Hit any key to stop autoboot:
默认SD卡就是出于可用状态,也可以用下面的命令查看:
=> mmc dev
switch to partitions #, OK
mmc0 is current device
=> mmc info
Device: MMC
Manufacturer ID: aa
OEM:
Name: QEMU!
Bus Speed:
Mode: SD Legacy
Rd Block Len:
SD version 2.0
High Capacity: No
Capacity: GiB
Bus Width: -bit
Erase Group Size: Bytes
查看分区内容:
=> part list mmc
Partition Map for MMC device -- Partition Type: EFI
Part Start LBA End LBA Name
Attributes
Type GUID
Partition GUID
0x00000800 0x000057ff "kernel"
attrs: 0x0000000000000000
type: 0fc63daf---8e79-3d69d8477de4
guid: 0de91381-f36c-4ed7-97d9-2fd234dcef93
0x00005800 0x001fffde "rootfs"
attrs: 0x0000000000000000
type: 0fc63daf---8e79-3d69d8477de4
guid: 6b0004e2-26a1-428e-abfc-f3a648e7419e
=> ls mmc : 或者 ext4ls mmc :
<DIR> .
<DIR> ..
<DIR> lost+found
zImage
vexpress-v2p-ca15_a7.dtb
vexpress-v2p-ca15-tc1.dtb
vexpress-v2p-ca5s.dtb
vexpress-v2p-ca9.dtb
=> ls mmc : 或者 ext4ls mmc :
<DIR> .
<DIR> ..
<DIR> lost+found
<DIR> bin
<DIR> dev
<DIR> etc
<DIR> home
<DIR> include
<SYM> init
<DIR> lib
<SYM> linuxrc
<DIR> proc
<DIR> root
<DIR> sbin
<DIR> share
<DIR> sys
<DIR> tmp
<DIR> usr
<DIR> var
.ash_history

五、加载kernel、设备树

=> load mmc : 0x60008000 zImage  或者ext4load mmc : 0x60008000 zImage
bytes read in ms (7.1 MiB/s)
=> load mmc : 0x61000000 vexpress-v2p-ca9.dtb 或者 ext4load mmc : 0x61000000 vexpress-v2p-ca9.dtb
bytes read in ms (213.9 KiB/s)

六、设置bootargs

setenv bootargs 'root=/dev/mmcblk0p2 rw rootfstype=ext4 rootwait earlycon console=tty0 console=ttyAMA0 init=/linuxrc ignore_loglevel'

七、引导内核

=> bootz 0x60008000 - 0x61000000

八、启动信息

=> bootz 0x60008000 - 0x61000000
Kernel image @ 0x60008000 [ 0x000000 - 0x7532d0 ]
## Flattened Device Tree blob at
Booting using the fdt blob at 0x61000000
Loading Device Tree to 7fe82000, end 7fe8898f ... OK Starting kernel ... Uncompressing Linux... done, booting the kernel.
[ 0.000000] Booting Linux on physical CPU 0x0
[ 0.000000] Linux version 4.14.+ (pengdl@pengdl-dell) (gcc version 7.3. [linaro-7.3-2018.05 revision d29120a424ecfbc167ef90065c0eeb7f91977701] (Linaro GCC 7.3-2018.05)) # SMP Mon Jan :: CST
[ 0.000000] CPU: ARMv7 Processor [410fc090] revision (ARMv7), cr=10c5387d
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
[ 0.000000] OF: fdt: Machine model: V2P-CA9
[ 0.000000] param_setup_earlycon enter, buf: (null)
[ 0.000000] earlycon: pl11 at MMIO 0x10009000 (options '115200n8')
[ 0.000000] bootconsole [pl11] enabled
[ 0.000000] param_setup_earlycon enter, buf: tty0
[ 0.000000] param_setup_earlycon enter, buf: ttyAMA0
[ 0.000000] debug: ignoring loglevel setting.
[ 0.000000] Memory policy: Data cache writeback
[ 0.000000] On node totalpages:
[ 0.000000] free_area_init_node: node , pgdat 80d5f300, node_mem_map bf7f9000
[ 0.000000] Normal zone: pages used for memmap
[ 0.000000] Normal zone: pages reserved
[ 0.000000] Normal zone: pages, LIFO batch:
[ 0.000000] CPU: All CPU(s) started in SVC mode.
[ 0.000000] random: fast init done
[ 0.000000] percpu: Embedded pages/cpu @bf7ac000 s36428 r8192 d20916 u65536
[ 0.000000] pcpu-alloc: s36428 r8192 d20916 u65536 alloc=*
[ 0.000000] pcpu-alloc: [] [] [] []
[ 0.000000] Built zonelists, mobility grouping on. Total pages:
[ 0.000000] Kernel command line: root=/dev/mmcblk0p2 rw rootfstype=ext4 rootwait earlycon console=tty0 console=ttyAMA0 init=/linuxrc ignore_loglevel
[ 0.000000] console_setup enter, str: tty0
[ 0.000000] console_setup enter, str: ttyAMA0
[ 0.000000] log_buf_len individual max cpu contribution: bytes
[ 0.000000] log_buf_len total cpu_extra contributions: bytes
[ 0.000000] log_buf_len min size: bytes
[ 0.000000] log_buf_len: bytes
[ 0.000000] early log buf free: (%)
[ 0.000000] PID hash table entries: (order: , bytes)
[ 0.000000] Dentry cache hash table entries: (order: , bytes)
[ 0.000000] Inode-cache hash table entries: (order: , bytes)
[ 0.000000] Memory: 1026280K/1048576K available (6144K kernel code, 439K rwdata, 1456K rodata, 4096K init, 170K bss, 22296K reserved, 0K cma-reserved)
[ 0.000000] Virtual kernel memory layout:
[ 0.000000] vector : 0xffff0000 - 0xffff1000 ( kB)
[ 0.000000] fixmap : 0xffc00000 - 0xfff00000 ( kB)
[ 0.000000] vmalloc : 0xc0800000 - 0xff800000 ( MB)
[ 0.000000] lowmem : 0x80000000 - 0xc0000000 ( MB)
[ 0.000000] modules : 0x7f000000 - 0x80000000 ( MB)
[ 0.000000] .text : 0x80008000 - 0x80700000 ( kB)
[ 0.000000] .init : 0x80900000 - 0x80d00000 ( kB)
[ 0.000000] .data : 0x80d00000 - 0x80d6dd48 ( kB)
[ 0.000000] .bss : 0x80d74e2c - 0x80d9f634 ( kB)
[ 0.000000] SLUB: HWalign=, Order=-, MinObjects=, CPUs=, Nodes=
[ 0.000000] Hierarchical RCU implementation.
[ 0.000000] RCU event tracing is enabled.
[ 0.000000] RCU restricting CPUs from NR_CPUS= to nr_cpu_ids=.
[ 0.000000] RCU: Adjusting geometry for rcu_fanout_leaf=, nr_cpu_ids=
[ 0.000000] NR_IRQS: , nr_irqs: , preallocated irqs:
[ 0.000000] GIC CPU mask not found - kernel will fail to boot.
[ 0.000000] GIC CPU mask not found - kernel will fail to boot.
[ 0.000000] L2C: platform modifies aux control register: 0x02020000 -> 0x02420000
[ 0.000000] L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000
[ 0.000000] L2C- enabling early BRESP for Cortex-A9
[ 0.000000] L2C- full line of zeros enabled for Cortex-A9
[ 0.000000] L2C- dynamic clock gating disabled, standby mode disabled
[ 0.000000] L2C- cache controller enabled, ways, kB
[ 0.000000] L2C-: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
[ 0.000000] smp_twd: clock not found -
[ 0.000236] sched_clock: bits at 24MHz, resolution 41ns, wraps every 89478484971ns
[ 0.003014] clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: ns
[ 0.004034] Failed to initialize '/smb@4000000/motherboard/iofpga@7,00000000/timer@12000': -
[ 0.007878] Console: colour dummy device 80x30
[ 0.009804] console [tty0] enabled
[ 0.010213] Calibrating local timer... .50MHz.
[ 0.063660] Calibrating delay loop... 726.63 BogoMIPS (lpj=)
[ 0.163367] pid_max: default: minimum:
[ 0.164371] Mount-cache hash table entries: (order: , bytes)
[ 0.164640] Mountpoint-cache hash table entries: (order: , bytes)
[ 0.173301] CPU: Testing write buffer coherency: ok
[ 0.180395] CPU0: thread -, cpu , socket , mpidr
[ 0.184217] Setting up static identity map for 0x60100000 - 0x60100060
[ 0.185567] Hierarchical SRCU implementation.
[ 0.190526] smp: Bringing up secondary CPUs ...
[ 0.195085] smp: Brought up node, CPU
[ 0.195340] SMP: Total of processors activated (726.63 BogoMIPS).
[ 0.195628] CPU: All CPU(s) started in SVC mode.
[ 0.226950] devtmpfs: initialized
[ 0.237195] VFP support v0.: implementor architecture part variant rev
[ 0.249451] clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: ns
[ 0.250611] futex hash table entries: (order: , bytes)
[ 0.286424] NET: Registered protocol family
[ 0.289887] DMA: preallocated KiB pool for atomic coherent allocations
[ 0.411870] cpuidle: using governor ladder
[ 0.412536] hw-breakpoint: debug architecture 0x4 unsupported.
[ 0.413148] Serial: AMBA PL011 UART driver
[ 0.417687] OF: amba_device_add() failed (-) for /memory-controller@100e0000
[ 0.418518] OF: amba_device_add() failed (-) for /memory-controller@100e1000
[ 0.419075] OF: amba_device_add() failed (-) for /watchdog@100e5000
[ 0.428162] .uart: ttyAMA0 at MMIO 0x10009000 (irq = , base_baud = ) is a PL011 rev1
[ 0.429393] console [ttyAMA0] enabled
[ 0.429393] console [ttyAMA0] enabled
[ 0.429830] bootconsole [pl11] disabled
[ 0.429830] bootconsole [pl11] disabled
[ 0.437874] 1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = , base_baud = ) is a PL011 rev1
[ 0.439865] 1000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = , base_baud = ) is a PL011 rev1
[ 0.441651] 1000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = , base_baud = ) is a PL011 rev1
[ 0.443990] OF: amba_device_add() failed (-) for /smb@/motherboard/iofpga@,/wdt@f000
[ 0.492049] SCSI subsystem initialized
[ 0.494187] libata version 3.00 loaded.
[ 0.495179] usbcore: registered new interface driver usbfs
[ 0.495583] usbcore: registered new interface driver hub
[ 0.496019] usbcore: registered new device driver usb
[ 0.500818] Advanced Linux Sound Architecture Driver Initialized.
[ 0.514158] clocksource: Switched to clocksource arm,sp804
[ 0.661421] NET: Registered protocol family
[ 0.665243] TCP established hash table entries: (order: , bytes)
[ 0.665645] TCP bind hash table entries: (order: , bytes)
[ 0.666075] TCP: Hash tables configured (established bind )
[ 0.667823] UDP hash table entries: (order: , bytes)
[ 0.668211] UDP-Lite hash table entries: (order: , bytes)
[ 0.669427] NET: Registered protocol family
[ 0.672771] RPC: Registered named UNIX socket transport module.
[ 0.673010] RPC: Registered udp transport module.
[ 0.673167] RPC: Registered tcp transport module.
[ 0.673316] RPC: Registered tcp NFSv4. backchannel transport module.
[ 1.032936] hw perfevents: enabled with armv7_cortex_a9 PMU driver, counters available
[ 1.039079] workingset: timestamp_bits= max_order= bucket_order=
[ 1.056808] squashfs: version 4.0 (//) Phillip Lougher
[ 1.059624] jffs2: version 2.2. (NAND) © - Red Hat, Inc.
[ 1.062208] 9p: Installing v9fs 9p2000 file system support
[ 1.066233] io scheduler noop registered (default)
[ 1.066479] io scheduler mq-deadline registered
[ 1.066735] io scheduler kyber registered
[ 1.071064] clcd-pl11x .clcd: PL111 designer rev2 at 0x10020000
[ 1.078507] clcd-pl11x .clcd: /clcd@ hardware, 1024x768@ display
[ 1.161773] Console: switching to colour frame buffer device 128x48
[ 1.167184] clcd-pl11x 1001f000.clcd: PL111 designer rev2 at 0x1001f000
[ 1.168631] clcd-pl11x 1001f000.clcd: /smb@/motherboard/iofpga@,/clcd@1f000 hardware, 640x480@ display
[ 1.477077] brd: module loaded
[ 1.495912] loop: module loaded
[ 1.500177] .flash: Found x16 devices at 0x0 in -bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[ 1.501104] Intel/Sharp Extended Query Table at 0x0031
[ 1.501768] Using buffer write method
[ 1.502266] erase region : offset=0x0,size=0x40000,blocks=
[ 1.504422] .flash: Found x16 devices at 0x0 in -bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
[ 1.504906] Intel/Sharp Extended Query Table at 0x0031
[ 1.505391] Using buffer write method
[ 1.505629] erase region : offset=0x0,size=0x40000,blocks=
[ 1.505986] Concatenating MTD devices:
[ 1.509464] (): "40000000.flash"
[ 1.511999] (): "40000000.flash"
[ 1.514592] into device "40000000.flash"
[ 1.579495] libphy: Fixed MDIO Bus: probed
[ 1.654665] libphy: smsc911x-mdio: probed
[ 1.659191] smsc911x 4e000000.ethernet eth0: MAC Address: :::::
[ 1.765966] isp1760 4f000000.usb: bus width: , oc: digital
[ 1.777548] isp1760 4f000000.usb: NXP ISP1760 USB Host Controller
[ 1.781625] isp1760 4f000000.usb: new USB bus registered, assigned bus number
[ 1.784568] isp1760 4f000000.usb: Scratch test failed.
[ 1.787257] isp1760 4f000000.usb: can't setup: -19
[ 1.789669] isp1760 4f000000.usb: USB bus deregistered
[ 1.793814] usbcore: registered new interface driver usb-storage
[ 1.802617] rtc-pl031 .rtc: rtc core: registered pl031 as rtc0
[ 1.806070] IR NEC protocol handler initialized
[ 1.808391] IR RC5(x/sz) protocol handler initialized
[ 1.810806] IR RC6 protocol handler initialized
[ 1.813412] IR JVC protocol handler initialized
[ 1.815587] IR Sony protocol handler initialized
[ 1.817861] IR SANYO protocol handler initialized
[ 1.820138] IR Sharp protocol handler initialized
[ 1.822689] IR MCE Keyboard/mouse protocol handler initialized
[ 1.824910] IR XMP protocol handler initialized
[ 1.832118] mmci-pl18x .mmci: Got CD GPIO
[ 1.834664] mmci-pl18x .mmci: Got WP GPIO
[ 1.838372] mmci-pl18x .mmci: mmc0: PL181 manf rev0 at 0x10005000 irq , (pio)
[ 1.913351] input: AT Raw Set keyboard as /devices/platform/smb@/smb@:motherboard/smb@:motherboard:i
ofpga@,/.kmi/serio0/input/input0
[ 1.928777] ledtrig-cpu: registered to indicate activity on CPUs
[ 1.933517] usbcore: registered new interface driver usbhid
[ 1.935808] usbhid: USB HID core driver
[ 1.945013] mmc0: new SD card at address
[ 1.952266] mmcblk0: mmc0: QEMU! 1.00 GiB
[ 1.986464] mmcblk0: p1 p2
[ 1.992031] aaci-pl041 .aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
[ 1.994594] aaci-pl041 .aaci: FIFO entries
[ 1.997482] oprofile: using arm/armv7-ca9
[ 2.000510] NET: Registered protocol family
[ 2.003305] 9pnet: Installing 9P2000 support
[ 2.005828] Registering SWP/SWPB emulation handler
[ 2.017119] rtc-pl031 .rtc: setting system clock to -- :: UTC ()
[ 2.020519] ALSA device list:
[ 2.022710] #: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
[ 2.025497] Warning: unable to open an initial console.
[ 2.063902] Freeing unused kernel memory: 4096K
[ 2.500280] input: ImExPS/ Generic Explorer Mouse as /devices/platform/smb@/smb@:motherboard/smb@:motherboard:iofpga@,/.kmi/serio1/input/input2
[ 3.017505] EXT4-fs (mmcblk0p2): recovery complete
[ 3.021581] EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
[ 3.076962] Generic PHY 4e000000.ethernet-ffffffff:: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.ethernet-ffffffff:, irq=POLL)
[ 3.083703] smsc911x 4e000000.ethernet eth0: SMSC911x/921x identified at 0xc1310000, IRQ: Please press Enter to activate this console.
[root@vexpress ]#
[root@vexpress ]# mount
rootfs on / type rootfs (rw)
proc on /proc type proc (rw,relatime)
tmpfs on /tmp type tmpfs (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
tmpfs on /dev type tmpfs (rw,relatime)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=000)
/dev/mmcblk0p2 on /root type ext4 (rw,relatime,data=ordered)
[root@vexpress ]# ls
bin      dev      home     init     linuxrc  root     share    tmp      var
d        etc      include  lib      proc     sbin     sys      usr

完。