在 Linux 下制作 SD 卡镜像

目录

一、制作SD卡镜像

1.1  创建空白的SD卡镜像

1.2 在SD卡镜像上创建两个分区

1.3 映射SD卡镜像到空闲loop设备

1.4 格式化SD卡的两个分区

1.5 挂载两个分区到用户目录

1.6 拷贝文件到挂载目录

1.7 取消挂载和SD卡的映射

二、测试SD卡镜像

2.1  uboot 启动 SD 卡镜像

2.2  uboot 加载内核&设备树

2.3  uboot 中固化启动参数

2.4  uboot 启动的完整参数


一、制作SD卡镜像

       
       使用 QEMU 模拟开发板的时候,需要存储设备,因此也需要在本地磁盘上创建一个SD卡,用来配合开发板进行测试。下面的内容主要是记录如何在 Ubuntu20.04上创建SD卡,分成两个分区,并将生成的 kernel、dtb 和 rootfs 文件分别存入这两个分区。

       具体步骤如下:
 

 1.1  创建空白的SD卡镜像

dd if=/dev/zero of=demo.img bs=1M count=300
# 注意自己控制 count 大小 ,总大小 1M * 300 = 300M

  命令参数说明:

  # if=输入文件名:指定源文件或设备,默认为标准输入。
  # of=文件名:输出的文件名(即生成的目标文件),默认为标准输出。
  # bs=bytes:同时设置读入/输出的块大小为bytes个字节。
  # count=blocks:拷贝blocks个块,块大小等于bs指定的字节数。

  # 其中 if=/dev/zero 设置的是 zero 设备
  # /dev/zero: 零设备,可无限提供空字符(0x00,ASCII代码NUL)。

 1.2 在SD卡镜像上创建两个分区

# 1. 创建两个分区(一个分区存放 kernel 和 dtb,另一个存放 rootfs)
  sgdisk -n 0:0:+10M -c 0:kernel mysd.disk
  sgdisk -n 0:0:0    -c 0:rootfs mysd.disk

     # 命令:sgdisk --help | grep new
     # 帮助:-n, --new=partnum:start:end   # create new partition
     #      -c, --change-name=partnum:name

# 2. 查看创建的分区,命令行:
  sgdisk -p mysd.disk

 

1.3 映射SD卡镜像到空闲loop设备

# 1. 查看空闲设备
LOOP_DEV=`losetup -f`
echo $LOOP_DEV

# 2. 映射SD卡到loop设备
sudo losetup $LOOP_DEV mysd.disk
sudo partprobe $LOOP_DEV
sudo losetup -l

# 3. 查看映射情况
ls /dev/loop*

注意:取消映射命令 ‘sudo losetup -d $LOOP_DEV’,
     在最后再取消映射,操作过程中不要取消映射。



1.4 格式化SD卡的两个分区

# 格式化 
sudo mkfs.ext4 /dev/loop19p1    # 格式化分区1
sudo mkfs.ext4 /dev/loop19p2    # 格式化分区2

1.5 挂载两个分区到用户目录

# 1.挂载设备
mkdir p1 p2
sudo mount -t ext4 /dev/loop19p1 p1   # 存放 kernel 和设备树
sudo mount -t ext4 /dev/loop19p2 p2   # 存放 rootfs

# 2. 查看挂载
df -h

1.6 拷贝文件到挂载目录

# 1. 将 zImage 和 dtb 拷贝到 p1
sudo cp arch/arm/boot/zImage /home/hello/QEMU/p1
sudo cp arch/arm/boot/dts/vexpress-v2p-ca9.dtb /home/hello/QEMU/p1

# 2. 将 文件系统中的文件拷贝到 p2
sudo cp -arf rootfs/* /home/hello/QEMU/p2

# 3. 查看 p1 p2 是否有对应的文件
ls /home/hello/QEMU/p1
ls /home/hello/QEMU/p2

1.7 取消挂载和SD卡的映射

# 1. 取消挂载
sudo umount p1 p2

# 2. 取消映射
sudo losetup -d /dev/loop19

    至些,完成了SD卡镜像的创建、分区、挂载、存放文件( kernel、dtb 和 rootfs )等操作。
    接下来,就可以使用 QEMU 加载这个 SD 镜像 mysd.disk 了。

二、测试SD卡镜像

2.1  uboot 启动 SD 卡镜像

# 1. 使用 uboot 启动 SD 卡镜像:
qemu-system-arm \
	-M vexpress-a9 \
	-m 512M \
	-kernel ./u-boot-2022.10/u-boot \
	-sd mysd.disk \
	-nographic

# 2. 在 uboot 启动之后,按回车,不要让其进入自主模式:
  
 U-Boot 2022.10 (Dec 22 2023 - 17:31:30 +0800)

 DRAM:  512 MiB
 WARNING: Caches not enabled
 Core:  18 devices, 10 uclasses, devicetree: embed
 Flash: 64 MiB
 MMC:   mmci@5000: 0
 Loading Environment from Flash... *** Warning - bad CRC, using default environment

 In:    serial
 Out:   serial
 Err:   serial
 Net:   eth0: ethernet@3,02000000
 Hit any key to stop autoboot:  1
 =>

# 3. 查看 SD卡的情况:
   设备信息:mmc dev 0
   设备信息:mmc info
   分区信息:part list mmc 0

 # 查看 SD 卡两个分区:
   分区1: ls mmc 0:1
   分区2: ls mmc 0:2

2.2 uboot 加载内核 & 设备树

1. 加载SD分区中的 “内核” 到指定的内存:
   load mmc 0:1 0x60008000 zImage 
   # 或者
   ext4load mmc 0:1 0x60008000 zImage

2. 加载SD分区中的 “设备树” 到指定的内存:
   load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb 
   # 或者 
   ext4load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb

3. 设置 uboot 的 bootargs 参数
   # rootwait 是无限期等待, 而 rootdelay 可以指定等待的时间
   setenv bootargs 'root=/dev/mmcblk0p2 rw rootfstype=ext4 rootwait earlycon console=tty0 console=ttyAMA0 init=/linuxrc ignore_loglevel'

4. 引导内核启动
   # bootz 启动 zImage
   bootz 0x60008000 - 0x61000000

2.3 uboot 中固化启动参数

# 1. 编译 menuconfig 命令: 
make menuconfig ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-

# 2. 查找 bootcmd value 位置:
     Location:                                                                                                                       
       │--> Boot options                                                                                                           
            │ --> Enable a default value for bootcmd (USE_BOOTCOMMAND [=y])                                                                                                                                                                          
            │ --> bootcmd value

# 3. 设置 bootcmd value 参数:
load mmc 0:1 0x60008000 zImage; load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb; setenv bootargs \"root=/dev/mmcblk0p2 earlycon console=ttyAMA0\"; bootz 0x60008000 - 0x61000000

# 4. 保存 .config :
     设置好 bootcmd value 后保存、退出 menuconfig。

# 5. 重新编译 uboot:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j6

2.4 uboot 启动的完整测试

qemu-system-arm \
	-M vexpress-a9 \
	-m 512M \
	-kernel ./u-boot-2022.10/u-boot \
	-sd mysd.disk \
	-nographic

# 注意:上面的 u-boot 是设置了 bootargs 后重新编译的 uboot。

  结果:执行完 uboot 后,会自动加载 zImage 和 dtb,然后进入 rootfs 文件系统。

  完整的输出信息如下:

WARNING: Image format was not specified for 'mysd.disk' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument


U-Boot 2022.10 (Dec 22 2023 - 17:31:30 +0800)

DRAM:  512 MiB
WARNING: Caches not enabled
Core:  18 devices, 10 uclasses, devicetree: embed
Flash: 64 MiB
MMC:   mmci@5000: 0
Loading Environment from Flash... *** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   eth0: ethernet@3,02000000
Hit any key to stop autoboot:  0 
3454648 bytes read in 559 ms (5.9 MiB/s)
14708 bytes read in 11 ms (1.3 MiB/s)
Kernel image @ 0x60008000 [ 0x000000 - 0x34b6b8 ]
## Flattened Device Tree blob at 61000000
   Booting using the fdt blob at 0x61000000
   Loading Device Tree to 7fb1c000, end 7fb22973 ... OK

Starting kernel ...

Booting Linux on physical CPU 0x0
Linux version 4.8.0 (hello@Hello) (gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) ) #1 SMP Tue Dec 19 17:16:24 CST 2023
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
OF: fdt:Machine model: V2P-CA9
Malformed early option 'earlycon'
Memory policy: Data cache writeback
CPU: All CPU(s) started in SVC mode.
percpu: Embedded 13 pages/cpu @9fbb8000 s22604 r8192 d22452 u53248
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 130048
Kernel command line: root=/dev/mmcblk0p2 earlycon console=ttyAMA0
log_buf_len individual max cpu contribution: 4096 bytes
log_buf_len total cpu_extra contributions: 12288 bytes
log_buf_len min size: 16384 bytes
log_buf_len: 32768 bytes
early log buf free: 14944(91%)
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: 510940K/524288K available (5120K kernel code, 162K rwdata, 1188K rodata, 1024K init, 154K bss, 13348K reserved, 0K cma-reserved)
Virtual kernel memory layout:
    vector  : 0xffff0000 - 0xffff1000   (   4 kB)
    fixmap  : 0xffc00000 - 0xfff00000   (3072 kB)
    vmalloc : 0xa0800000 - 0xff800000   (1520 MB)
    lowmem  : 0x80000000 - 0xa0000000   ( 512 MB)
    modules : 0x7f000000 - 0x80000000   (  16 MB)
      .text : 0x80008000 - 0x80600000   (6112 kB)
      .init : 0x80800000 - 0x80900000   (1024 kB)
      .data : 0x80900000 - 0x80928be0   ( 163 kB)
       .bss : 0x8092a000 - 0x80950878   ( 155 kB)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
Hierarchical RCU implementation.
	Build-time adjustment of leaf fanout to 32.
	RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=4.
RCU: Adjusting geometry for rcu_fanout_leaf=32, nr_cpu_ids=4
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.
L2C: platform modifies aux control register: 0x02020000 -> 0x02420000
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
smp_twd: clock not found -2
sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns
clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
Failed to initialize '/smb@04000000/motherboard/iofpga@7,00000000/timer@12000': -22
Console: colour dummy device 80x30
Calibrating local timer... 92.56MHz.
Calibrating delay loop... 772.50 BogoMIPS (lpj=3862528)
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
CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
Setting up static identity map for 0x60100000 - 0x60100058
Brought up 1 CPUs
SMP: Total of 1 processors activated (772.50 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
clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
NET: Registered protocol family 16
DMA: preallocated 256 KiB pool for atomic coherent allocations
cpuidle: using governor ladder
hw-breakpoint: debug architecture 0x4 unsupported.
Serial: AMBA PL011 UART driver
OF: amba_device_add() failed (-19) for /memory-controller@100e0000
OF: amba_device_add() failed (-19) for /memory-controller@100e1000
OF: amba_device_add() failed (-19) for /watchdog@100e5000
OF: amba_device_add() failed (-19) for /smb@04000000/motherboard/iofpga@7,00000000/sysctl@01000
10009000.uart: ttyAMA0 at MMIO 0x10009000 (irq = 35, base_baud = 0) is a PL011 rev1
console [ttyAMA0] enabled
1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = 36, base_baud = 0) is a PL011 rev1
1000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = 37, base_baud = 0) is a PL011 rev1
1000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = 38, base_baud = 0) is a PL011 rev1
OF: amba_device_add() failed (-19) for /smb@04000000/motherboard/iofpga@7,00000000/wdt@0f000
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.
clocksource: Switched to clocksource arm,sp804
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)
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.
hw perfevents: enabled with armv7_cortex_a9 PMU driver, 5 counters available
futex hash table entries: 1024 (order: 4, 65536 bytes)
workingset: timestamp_bits=30 max_order=17 bucket_order=0
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
io scheduler noop registered (default)
clcd-pl11x 10020000.clcd: PL111 rev2 at 0x10020000
clcd-pl11x 10020000.clcd: /clcd@10020000 hardware, 1024x768@59 display
Console: switching to colour frame buffer device 128x48
clcd-pl11x 1001f000.clcd: PL111 rev2 at 0x1001f000
clcd-pl11x 1001f000.clcd: /smb@04000000/motherboard/iofpga@7,00000000/clcd@1f000 hardware, 640x480@59 display
40000000.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
40000000.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): "40000000.flash"
(1): "40000000.flash"
into device "40000000.flash"
libphy: Fixed MDIO Bus: probed
libphy: smsc911x-mdio: probed
smsc911x 4e000000.ethernet eth0: MAC Address: 52:54:00:12:34:56
isp1760 4f000000.usb: bus width: 32, oc: digital
isp1760 4f000000.usb: NXP ISP1760 USB Host Controller
isp1760 4f000000.usb: new USB bus registered, assigned bus number 1
isp1760 4f000000.usb: Scratch test failed.
isp1760 4f000000.usb: can't setup: -19
isp1760 4f000000.usb: USB bus 1 deregistered
usbcore: registered new interface driver usb-storage
mousedev: PS/2 mouse device common for all mice
rtc-pl031 10017000.rtc: rtc core: registered pl031 as rtc0
mmci-pl18x 10005000.mmci: Got CD GPIO
mmci-pl18x 10005000.mmci: Got WP GPIO
mmci-pl18x 10005000.mmci: mmc0: PL181 manf 41 rev0 at 0x10005000 irq 31,32 (pio)
input: AT Raw Set 2 keyboard as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0
mmc0: new SD card at address 4567
mmcblk0: mmc0:4567 QEMU! 300 MiB 
ledtrig-cpu: registered to indicate activity on CPUs
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
random: fast init done
 mmcblk0: p1 p2
aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
aaci-pl041 10004000.aaci: FIFO 512 entries
oprofile: using arm/armv7-ca9
NET: Registered protocol family 17
9pnet: Installing 9P2000 support
Registering SWP/SWPB emulation handler
rtc-pl031 10017000.rtc: setting system clock to 2023-12-22 11:00:38 UTC (1703242838)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 30
input: ImExPS/2 BYD TouchPad as /devices/platform/smb@04000000/smb@04000000:motherboard/smb@04000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
EXT4-fs (mmcblk0p2): couldn't mount as ext3 due to feature incompatibilities
EXT4-fs (mmcblk0p2): mounted filesystem with ordered data mode. Opts: (null)
VFS: Mounted root (ext4 filesystem) readonly on device 179:2.
Freeing unused kernel memory: 1024K (80800000 - 80900000)
random: crng init done
mount: mounting tmpfs on /tmp failed: No such file or directory
mkdir: can't create directory '/var/lock': Read-only file system

Please press Enter to activate this console. 
[root@vexpress ]# ls
bin      etc      linuxrc  proc     sbin     usr
dev      lib      mnt      root     sys      var
[root@vexpress ]# 
[root@vexpress ]# 

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值