学习主要参考https://rocketboards.org/foswiki/Documentation/EmbeddedLinuxBeginnerSGuide
我们将创建一个512兆字节的映像,其中FAT分区将占用256兆字节,根文件系统将使用254兆字节,原始A2分区将使用1兆字节(最后1兆字节由MBR占用)。每个嵌入式Linux设备都有不同的大小要求。如果您发现您的根文件系统需要更多的空间,请稍后回到这一节,用更大的分区重新创建SD卡。
**注意:
**创建顺序必须按照分区号为3 2 1的顺序进行,跟着本文顺序即可。
dyq@ubuntu:$ sudo dd if=/dev/zero of=sdcard.img bs=512M count=1
其功能是备份/dev/zero
开始的512个字节大小的Master Boot Record (MBR)信息到指定文件,count=1指仅拷贝一个块;bs=512指块大小为512个字节。
具体解释参考:
http://blog.sina.com.cn/s/blog_8b5bb24f01016y3o.html
https://www.cnblogs.com/patf/articles/3733259.html
dyq@ubuntu:$ sudo losetup --show -f sdcard.img
/dev/loop0 #这个是终端自动弹出的,不同设备不一定一致,后面的所有/dev/loop0都要改成自己终端显示的
由上面显示/dev/loop0
,输入以下指令开始对映像进行分区。
dyq@ubuntu:$ sudo fdisk /dev/loop0
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x56937e18.
Command (m for help):
输入p可查看该分区的大小:
Command (m for help): p
Disk /dev/loop1: 512 MiB, 536870912 bytes, 1048576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x56937e18
主分区存放的是预加载程序映像。每个预加载程序映像(在预加载程序部分生成)将包含四个冗余的64KB预加载程序映像(以防闪存损坏),因此该分区至少需要256KB。
输入n
,开始创建分区,然后输入p
将该分区设为主分区,分区号设为3,然后启动地址为默认直接回车(MBR占用1M空间,所以起始空间为2048bit),结束地址处,输入+1M,即创建该分区为1MB。
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 3
First sector (2048-1048575, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-1048575, default 1048575): +1M
Created a new partition 3 of type 'Linux' and of size 1 MiB.
很明显,默认情况下,fdisk用“Linux”类型创建每个分区。我们需要再进行设置一下,改为a2类型。输入t进行修改,然后输入a2
进行设置,因为a2是altera的专属设置,因此idisk识别不了,即显示unknown
。
Command (m for help): t
Selected partition 3
Partition type (type L to list all types): a2
Changed type of partition 'Linux' to 'unknown'.
此时,Master Boot Record(MBR)和A2分区将用完2mb。现在,我们将为根文件系统添加254MB的分区。将分区2创建为254MB的主分区,默认起始扇区为4096。
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1,2,4, default 1): 2
First sector (4096-1048575, default 4096):
Last sector, +sectors or +size{K,M,G,T,P} (4096-1048575, default 1048575): +254M
Created a new partition 2 of type 'Linux' and of size 254 MiB.
默认情况下,fdisk实用程序将分区类型设置为“Linux ”,因此我们不需要更改根文件系统的分区类型(我们仍然需要在以后实际创建文件系统)。
FAT分区存放boot文件。该分区为主分区,为1号分区位置。
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1,4, default 1): 1
First sector (524288-1048575, default 524288):
Last sector, +sectors or +size{K,M,G,T,P} (524288-1048575, default 1048575):
Created a new partition 1 of type 'Linux' and of size 256 MiB.
将分区1更改为FAT分区(这不会创建文件系统,只是通知读取SD卡的设备在这个分区上将有一个FAT文件系统)。
Command (m for help): t
Partition number (1-3, default 3): 1
Partition type (type L to list all types): b
Changed type of partition 'Linux' to 'W95 FAT32'.
打印出分区表,并验证它如下所示。
Command (m for help): p
Disk /dev/loop1: 512 MiB, 536870912 bytes, 1048576 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xf37aca44
Device Boot Start End Sectors Size Id Type
/dev/loop0p1 524288 1048575 524288 256M b W95 FAT32
/dev/loop0p2 4096 524287 520192 254M 83 Linux
/dev/loop0p3 2048 4095 2048 1M a2 unknown
Partition table entries are not in disk order.
输入w,保存对sdcar.img的设置
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument
The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
最后输入如下指令告诉内核进行更新,否则内核找不到刚才设立的分区。
sudo partprobe /dev/loop0
在此之前需要准备以下文件:
/home/dyq/software/spl_bsp/preloader-mkpimage.bin
/home/dyq/software/u-boot-socfpga/u-boot.img
/home/dyq/software/u-boot-socfpga/u-boot.scr
/home/dyq/software/soc_system.dtb
/home/dyq/software/soc_system.rbf
将预加载程序放入a2分区。注意前面a2分区为3号,因此为of=/dev/loop1p3。
每个预加载程序映像(在预加载程序部分生成)将包含四个冗余的64KB预加载程序映像(以防闪存损坏),因此该分区至少需要256KB。
sudo dd if=/home/dyq/software/spl_bsp/preloader-mkpimage.bin of=/dev/loop0p3 bs=64k seek=0
在分区1(我们设置为“FAT”类型的分区)上创建FAT文件系统。
sudo mkfs -t vfat /dev/loop0p1
终端反馈如下为正常。
mkfs.fat 3.0.28 (2015-05-16)
unable to get drive geometry, using default 255/63
终端输入如下,在分区2创建ext4文件系统。也就是后面的根文件系统
sudo mkfs.ext4 /dev/loop0p2
终端反馈如下
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 260096 1k blocks and 65024 inodes
Filesystem UUID: cc86faf1-dd59-4978-a4ae-43dd1163c3e2
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
mkdir temp_mount
sudo mount /dev/loop0p1 ./temp_mount
sudo cp /home/dyq/software/u-boot-socfpga/u-boot.img /home/dyq/software/u-boot-socfpga/u-boot.scr /home/dyq/software/soc_system.dtb /home/dyq/software/soc_system.rbf temp_mount
sync
sudo umount temp_mount
lsblk
**查看USB设备。dyq@ubuntu:/dev$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 1 7.3G 0 disk
├─sdb2 8:18 1 2.1G 0 part /media/dyq/586a3d9d-5074-4bfb-a18a-46ede4bcb7
├─sdb3 8:19 1 2M 0 part
└─sdb1 8:17 1 100M 0 part /media/dyq/de10-nano
sr0 11:0 1 1024M 0 rom
loop0 7:0 0 512M 0 loop
├─loop0p3 259:2 0 1M 0 loop
├─loop0p1 259:0 0 256M 0 loop
└─loop0p2 259:1 0 254M 0 loop
sda 8:0 0 20G 0 disk
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 975M 0 part [SWAP]
└─sda1 8:1 0 19G 0 part /
可看到sdb为sdcard设备,如果不确定是否是这个,可以将sdcard弹出,再输入**lsblk
**看看哪一个设备消失了以确定设备的正确性。
dyq@ubuntu:/dev$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
loop0 7:0 0 512M 0 loop
├─loop0p3 259:2 0 1M 0 loop
├─loop0p1 259:0 0 256M 0 loop
└─loop0p2 259:1 0 254M 0 loop
sda 8:0 0 20G 0 disk
├─sda2 8:2 0 1K 0 part
├─sda5 8:5 0 975M 0 part [SWAP]
└─sda1 8:1 0 19G 0 part /
拔出后发现sdb消失了。因此可确定sdb为sdcard设备。
dyq@ubuntu:~$ sudo dd if=sdcard.img of=/dev/sdb bs=2048
262144+0 records in
262144+0 records out
536870912 bytes (537 MB, 512 MiB) copied, 285.906 s, 1.9 MB/s
dyq@ubuntu:~$ sync
如果提示找不到sdcard.img文件,应该是当前终端的路径不在sdcard.img所在的空间,可以把if=sdcard.img
改为绝对路径再执行命令,或者把路径调整到该文件所在路径即可。
至此SDcard映像已经完成制作。
将SDcard插到开发板,上电启动通过putty查看打印的数据:
U-Boot SPL 2013.01.01 (Mar 29 2022 - 13:42:46)
BOARD : Altera SOCFPGA Cyclone V Board
CLOCK: EOSC1 clock 25000 KHz
CLOCK: EOSC2 clock 25000 KHz
CLOCK: F2S_SDR_REF clock 0 KHz
CLOCK: F2S_PER_REF clock 0 KHz
CLOCK: MPU clock 800 MHz
CLOCK: DDR clock 400 MHz
CLOCK: UART clock 100000 KHz
CLOCK: MMC clock 50000 KHz
CLOCK: QSPI clock 3125 KHz
RESET: COLD
INFO : Watchdog enabled
SDRAM: Initializing MMR registers
SDRAM: Calibrating PHY
SEQ.C: Preparing to start memory calibration
SEQ.C: CALIBRATION PASSED
SDRAM: 1024 MiB
ALTERA DWMMC: 0
reading u-boot.img
reading u-boot.img
U-Boot 2013.01.01-00126-g353283b (Mar 27 2022 - 23:12:24)
CPU : Altera SOCFPGA Platform
BOARD : Altera SOCFPGA Cyclone V Board
I2C: ready
DRAM: 1 GiB
MMC: ALTERA DWMMC: 0
*** Warning - bad CRC, using default environment
In: serial
Out: serial
Err: serial
Skipped ethaddr assignment due to invalid EMAC address in EEPROM
Net: mii0
Warning: failed to set MAC address
Hit any key to stop autoboot: 0
reading u-boot.scr
232 bytes read in 4 ms (56.6 KiB/s)
## Executing script at 02000000
reading soc_system.rbf
2202852 bytes read in 107 ms (19.6 MiB/s)
## Starting application at 0x3FF79524 ...
## Application terminated, rc = 0x0
reading zImage
** Unable to read file zImage **
reading soc_system.dtb
24514 bytes read in 7 ms (3.3 MiB/s)
Bad Linux ARM zImage magic!
reading zImage
** Unable to read file zImage **
reading soc_system.dtb
24514 bytes read in 7 ms (3.3 MiB/s)
Bad Linux ARM zImage magic!
SOCFPGA_CYCLONE5 #