制作SD启动卡,从SD卡启动系统

简介: 制作SD启动卡,从SD卡启动系统

  • 芯片需要支持SD卡这种启动方式。
  • 准备好u-boot.imx,dtb,zImage,rootfs压缩包

使用fdisk命令对sd卡分区

输入fdisk /dev/mmcblk0.按照提示输入相关选项进行操作。

/#fdisk/dev/mmcblk0
Device contains neither a valid DOS partition table, nor Sun, SGI, OSF or GPT disklabel Building a new Dos disklabel. Changes will remain in memory only, until you decide to write them. After that the previous content won't be recoverable.
The number of cylinders for this disk is set to 486192.
There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of Lilo)2) booting and partitioning software from other 0Ss
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): help  输入help提示  
he unknown command  带助信息  
Command Action
a toggle a bootable flag  
edit bsd disklabel
toggle the dos compatibility flag delete a partition
list known partition types add a new partition
create a new empty D0S partition table print the partition table quit without saving changes
create a new empty Sun disklabel change a partition's system id change display/entry units verify the partition table write table to disk and exit
extra functionality (experts only)
Command (m for help): p 输入p打印分区表信息  
Disk /dev/mmcblk0: 15 GB,15931539456 bytes, 31116288 sectors
486192 cylinders, 4 heads, 16 sectors/track
Units: cylinders of 64 * 512 = 32768 bytes

可以看到我这张SD卡的相关信息,总容量为15931539456字节,31116288个sectors,即一个sector为512字节。

创建第一个分区

Command (m for help):n  
Command action
extended
e
p primary partition (1-4)
Partition number (1-4):1
First cylinder (1-486192,default 1):32
Last cylinder or +size or +sizeM or +sizeK (32-486192,default 486192):2080
Command (m for help): p
Disk/dev/mmcblk0: 15 GB, 15931539456 bvtes, 31116288 sectors486192 cylinders, 4 heads, 16 sectors/track Units: cylinders of 64 * 512 = 32768 bytes
Device  Boot StartCHS EndCHS  StartLBA  EndLBA  Sectors Size Id Type  
/dev/mmcblk0p1  31,0,1  1023,3,16 1984  133119  131136 64.0M 83 Linux
Command (m for help): 0

输入n,添加一个新分区,这里提示输入分区号,范围1 ~ 4. 然后输入1,提示我们选择第一个柱面(一个柱面的大小是32KB)。 这里我选择32 ~(32+2048)作为第一个分区作boot分区(用来烧写dtb和kernel分区)。即32KB * 2048 = 64M大小。之所以从32开始,是把前1M(32K*32=1M)空间留给uboot,uboot是裸分区

创建第二个分区

Command (m for help): n Command action
e extended  
p primary partition (1-4)
Partition number (1-4):2
First cvlinder (1-486192,default 1): 2081
Last cylinder or +size or +sizeM or +sizek (2081-486192, default 486192):486192
Command (m for help): p
Disk/dev/mmcblk0: 15 GB,15931539456 bytes, 31116288 sectors486192 cylinders, 4 heads, 16 sectors/track Units: cylinders of 64 * 512 = 32768 bytes
Device  Boot StartCHS EndCHS  StartLBA  EndLBA  Sectors Size Id Type  
/dev/mmcblk0p1  31,日,1  1023,3,16 1984  133119  131136 64.0M 83 Linux 
/dev/mmcblk0p2  1023,3,16 1023,3,16 133120  31116287  30983168 14.7G 83 Linux 
Command (m for help): 1

同样地,创建第二个分区,从2081~486192都划分给第二个分区。 以下是我设置的分区。

分区名 分区号 大小
boot 1 64M
文件系统分区 2 剩余空间

640.png

设置p1为boot分区

Command (m for help): m Command Action
a toggle a bootable flag  
b edit bsd disklabel  
toggle the dos compatibility flag delete a partition
list known partition types add a new partition
create a new empty Dos partition table print the partition table quit without saving changes
create a new empty Sun disklabel change a partition's system id change display/entry units verify the partition table
W write table to disk and exit  
X extra functionality (experts only)  
Command (m for help): t 
Partition number (1-4):1
Hex code  (type list  codes): 
日 Empty 1b Hidden Win95 FAT32 9f BSD/0S 
FAT12 1c Hidden W95 FAT32 (LBA) a0 Thinkpad hibernation 
4 FAT16 <32M  le Hidden W95 FAT16 (LBA) a5 FreeBSD  
5 Extended  3c Part.Magic recovery  a6 0penBSD  
6 FAT16 41 PPC PReP Boot  a8 Darwin UFS 
7 HPFS/NTFS 42 SFS  a9 NetBSD 
a 0S/2 Boot Manager 63 GNU HURD or SysV ab Darwin boot  
b Win95 FAT32 80 0ld Minix  b7 BSDI fs  
Win95FAT32(LBA) 81 Minix / old Linux  b8 BSDI swap  
e Win95 FAT16 (LBA) 82 Linux swap be Solaris boot 
f Win95 Ext'd(LBA)  83 Linux  eb BeOS fs  
11 Hidden FAT12 84 0S/2 hidden C: drive ee EFI GPT  
12 Compag diagnostics 85 Linux extended ef EFI (FAT-12/16/32) 
14 Hidden FAT16 <32M  86 NTFS volume set  f0 Linux/PA-RISC boot 
16 Hidden FAT16 87 NTFS volume set  f2 DOS secondary  
17 Hidden HPES/NTES 8e Linux LVM  fd Linux raid autodetect  
Hex code (type L to list codes): c
Changed system type of partition 1 to c (Win95 FAT32 (LBA)
Command (m for help): m Command Action
toggle a bootable flag edit bsd disklabel
toggle the dos compatibility flag delete a partition
list known partition types
n add a new partition 
create a new empty D0S partition table print the partition table quit without saving changes
S create a new empty Sun disklabel  
change a partition's system id change display/entry units
U
verify the partition table write table to disk and exit
extra functionality (experts only)
Command (m for help): a
Partition number (1-4):1  

使用t选项,选择第一个分区,然后选择c,把分区system id类型设置为fat。然后使用a,选择第一个分区为boot类型。

Command (m for help): p
Disk/dev/mmcblk0: 15 GB,15931539456 bytes, 31116288 sectors486192 cylinders, 4 heads, 16 sectors/track Units: cylinders of 64 * 512 = 32768 bytes
Device/dev/mmcblk0p1 Boot31,日,1 StartCHS EndCHS1023,3,16 StartLBA1984133119 EndLBA Secto1311 Size Id Type哆哆的Lnux存储杂记
/dev/mmcblk0p2  1023,3,16 1023,3,16 13312日  31116287  30983168 14.7G 83 Linux

可以看到p1后面boot那列多了个*。

格式化p1 p2

创建完分区之后,还需要对分区进行格式化。把p1的fstype格式化为fat,p2的fstype格式化为ext3分区。mke2fs可以用-t选项指定为ext3或者ext4。

mkfs.vfat -F 32 -n "boot" /dev/mmcblk0p1
mke2fs -t ext4 -F -L "rootfs" /dev/mmcblk0p2

烧录uboot.imx

dd if=u-boot.imx of=/dev/mmcblk0 bs=1024 seek=1 conv=fsync

通过dd命令烧录uboot.imx到SD卡。这里需要使用seek跳过前1K,这个跟这款芯片有关。

640.png

640.png做到这一步,已经可以从SD启动u-boot了,不过里面还没有kernel和根文件系统。

烧录kernel

mkdir -p /tmp/kernel
mount /dev/mmcblk0p1 /tmp/kernel
cp -r /mnt/dfos.dtb /tmp/kernel
cp -r /mnt/zImage /tmp/kernel
umount /tmp/kernel

烧录根文件系统

mkdir -p /tmp/rootfs
mount /dev/mmcblk0p2 /tmp/rootfs
tar -vxf /mnt/rootfs.tar.bz2 -C /tmp/rootfs/
umount /tmp/rootfs

至此,就可以从SD卡启动系统了。

关于根文件系统打包

压缩

sudo tar -cvzf rootfs.tar.gz *  
sudo tar -cvjf rootfs.tar.bz2 *

解压

tar -vxf /mnt/rootfs.tar.gz -C /tmp/rootfs/ --strip-components 1
tar -vxf /mnt/rootfs.tar.bz2 -C /tmp/rootfs/

做成shell脚本一键烧录

使用方法:sd_burn.sh /dev/mmcblk0

#!/bin/sh
#execute执行语句成功与否打印
execute ()
{
    $* >/dev/null
    if [ $? -ne 0 ]; then
        echo
        echo "错误: 执行 $*"
        echo
        exit 1
    fi
}
device=$1
dfos_dtb=imx6ull-14x14-evk.dtb
uboot='u-boot.imx'
rootfs_img=rootfs.tar.bz2
#rootfs_img=rootfs.tar.gz
#执行格式化$device
execute "dd if=/dev/zero of=$device bs=1024 count=1024"
#cat << END | fdisk -H 255 -S 63 $device
cat << END | fdisk $device
n
p
1
32
2080
n
p
2
2081  
486192
t
1
c
a
1
w
END
PARTITION1=${device}p1
PARTITION2=${device}p2
#第一个分区创建为Fat32格式
echo "格式化${device}p1 ..."
if [ -b ${PARTITION1} ]; then
  echo "${PARTITION1} ..."
  mkfs.vfat -F 32 -n "boot" /dev/mmcblk0p1
else
  echo "error: no ${PARTITION1}"
fi
#第二个分区创建为ext4格式
echo "格式化${device}p2 ..."
if [ -b ${PARITION2} ]; then
  echo "${PARTITION2} ..."
  /mnt/mke2fs -F -L "rootfs" /dev/mmcblk0p2
else
  echo "错误: /dev下找不到 SD卡 rootfs分区"
fi
echo "正在烧写${Uboot}到${device}"
execute "dd if=/mnt/${uboot} of=$device bs=1024 seek=1 conv=fsync"
sync
echo "烧写${Uboot}到${device}完成!"
echo "正在准备复制..."
echo "正在复制设备树与内核到${PARTITION1},请稍候..."
execute "mkdir -p /tmp/kernel"
execute "mount ${PARTITION1} /tmp/kernel"
execute "cp -r /mnt/${dfos_dtb} /tmp/kernel"
execute "cp -r /mnt/zImage /tmp/kernel"
sync
echo "复制设备树与内核到${device}p1完成!"
echo "卸载${PARTITION1}"
execute "umount /tmp/kernel"
sleep 1
#解压文件系统到文件系统分区
#挂载文件系统分区
execute "mkdir -p /tmp/rootfs"
execute "mount ${PARTITION2} /tmp/rootfs"
echo "正在解压文件系统到${PARTITION2} ,请稍候..."
#execute "tar -vxf /mnt/${rootfs_img} -C /tmp/rootfs/ --strip-components 1"
execute "tar -vxf /mnt/${rootfs_img} -C /tmp/rootfs/"
sync
echo "解压文件系统到${PARTITION2}完成!"
echo "卸载${PARTITION2}"
execute "umount /tmp/rootfs"
execute "rm -rf /tmp/rootfs"
sync
echo "SD卡启动系统烧写完成!"

号主:一枚机械专业本科生,经历了转行,从外包逆袭到芯片原厂的Linux驱动开发工程师,深入操作系统的世界,贯彻终身学习、终身成长的理念。平时喜欢折腾,寒冬之下,抱团取暖,期待你来一起探讨技术、搞自媒体副业,程序员接单和投资理财。【对了,不定期送闲置开发板、书籍、键盘等等】。

如果你想了解我的转行经验,欢迎找我交流~gongzhong号【哆哆jarvis】

一起不断探索自我、走出迷茫、找到热爱,希望和你成为朋友,一起成长~

相关文章
|
24天前
win10更换系统启动时候的图片
win10更换系统启动时候的图片
8 0
|
1月前
|
存储 API 开发工具
【ZYNQ】SD 卡读写及文件扫描实验
【ZYNQ】SD 卡读写及文件扫描实验
|
1月前
|
Linux 内存技术 Perl
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
【ZYNQ】制作从 QSPI Flash 启动 Linux 的启动文件
|
10月前
|
存储 固态存储 Linux
E2000从eMMC或SD启动U-boot和系统
E2000从eMMC或SD启动U-boot和系统
509 0
E2000从eMMC或SD启动U-boot和系统
制作U盘启动时【usb-hdd和usb-zip的区别】
制作U盘启动时【usb-hdd和usb-zip的区别】
680 0
制作U盘启动时【usb-hdd和usb-zip的区别】
|
Android开发
android手机操作SD的使用方法
android手机操作SD的使用方法
69 0
以前256MB的手机存储卡,可以用来安装运行一个完整的电脑操作系统
以前256MB的手机存储卡,可以用来安装运行一个完整的电脑操作系统
以前256MB的手机存储卡,可以用来安装运行一个完整的电脑操作系统
配置开机脚本后服务器启动卡在“正在应用计算机设置”
分享一个配置开机脚本后服务器启动卡在“正在应用计算机设置”的案例
配置开机脚本后服务器启动卡在“正在应用计算机设置”
|
虚拟化 内存技术
制作 ESXi 5.1 的USB闪存"引导安装"盘
制作 ESXi 5.1 的USB闪存"引导安装"盘 1 准备工作 linux 系统    //我这里是ubuntu 12.04 U盘一个       //8GB kingston ESXi ISO文件  //vmware 官网下载 VMware-VMvisor-Installer-5.
1272 0