野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9085|回复: 1

实战5 SD卡启动pcDuino板

[复制链接]
发表于 2016-3-19 21:04:22 | 显示全部楼层 |阅读模式
本帖最后由 xcoder 于 2016-3-19 21:06 编辑

实战5 SD卡启动pcDuino板

在前面几章中,分别介绍了u-boot、linux以及根文件系统的编译或制作过程,这一章中,我们将这些文件部署在SD卡中,并从SD卡启动pcDuino板。

5.1 SD启动卡布局
在编译u-boot镜像一章中,讲解了当从SD卡启动时,A20在上电后会从默认区域寻找boot并运行。详见表2.2,下图从使用的视角描绘了SD卡分布情况。
          image001.png                     
图5.1 SD启动卡布局
SD卡被分为4个主要部分,它们的作用分别是:
分区表:对SD分区的描述,指明各分区的起止地址等信息
boot区:保存SPL和u-boot以及环境变量等,其实就是保存u-boot-sunxi-with-spl.bin
启动分区:被格式化为文件系统使用,保存linux、dtb镜像等。
根文件系统分区:被格式化为文件系统使用,保存根文件系统内容。

5.2 SD启动卡分区
将SD卡插入PC机,在虚拟机上会识别为/dev/sdb或/dev/sdc(也可能是其它名称),以/dev/sdc为例。
1. 向SD卡起始写入1M的0来清空原本可能存在的分区表信息。
# sudo dd if=/dev/zero of=/dev/sdc bs=1M count=1
  
1+0 records in
  
1+0 records out
  
1048576 bytes (1.0 MB) copied, 0.000638668 s, 1.6  GB/s


2. 使用fdisk命令将SD卡分成两个区,一个作为启动分区(/dev/sdc1),大小为16M,另一个作为根文件系统分区(/dev/sdc2),占用剩余空间。
# sudo fdisk /dev/sdc
  
Device contains neither a valid DOS partition  table, nor Sun, SGI or OSF disklabel
  
Building a new DOS disklabel with disk identifier  0x63581385.
  
Changes will remain in memory only, until you  decide to write them.
  
After that, of course, the previous content won't  be recoverable.
  
  
Warning: invalid flag 0x0000 of partition table 4  will be corrected by w(rite)
  
  
Command (m for help): n
  
Partition type:
  
   p   primary (0 primary, 0 extended, 4 free)
  
   e   extended
  
Select (default p): p
  
Partition number (1-4, default 1): 1
  
First sector (2048-2097151, default 2048): 2048
  
Last sector, +sectors or +size{K,M,G}  (2048-2097151, default 2097151): +15M
  
  
Command (m for help): n
  
Partition type:
  
   p   primary (1 primary, 0 extended, 3 free)
  
   e   extended
  
Select (default p): p
  
Partition number (1-4, default 2): 2
  
First sector (32768-2097151, default 32768):
  
Using default value 32768
  
Last sector, +sectors or +size{K,M,G}  (32768-2097151, default 2097151):
  
Using default value 2097151
  
  
Command (m for help): p
  
  
Disk /dev/sdc: 1073 MB, 1073741824 bytes
  
255 heads, 63 sectors/track, 130 cylinders, total  2097152 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
  
Disk identifier: 0x63581385
  
  
   Device  Boot      Start         End      Blocks    Id  System
  
/dev/sdc1            2048       32767       15360    83  Linux
  
/dev/sdc2           32768     2097151     1032192    83  Linux
  
  
Command (m for help): w
  
The partition table has been altered!
  


3. 根据SD卡布局,从SD卡8KB偏移处开始将u-boot-sunxi-with-spl.bin写入。
  # dd  if=u-boot-sunxi-with-spl.bin of=/dev/sdc   bs=1024 seek=8
  

4.准备启动分区。
格式化为vfat格式,好处是PC即和pcDuino都可以识别,便于文件拷贝。挂载到/mnt目录。
  # mkfs.vfat /dev/sdc1
  mkfs.fat 3.0.26  (2014-03-07)
  # mount /dev/sdc1 /mnt
  # df
  Filesystem     1K-blocks     Used Available Use% Mounted on
  /dev/sda1       64891708 10454744  51117620   17% /
  none                   4        0         4    0% /sys/fs/cgroup
  udev             2057876  1986564      71312  97% /dev
  tmpfs             413576     1116     412460   1% /run
  none                5120        0       5120   0% /run/lock
  none             2067864        0    2067864   0% /run/shm
  none              102400        0     102400   0% /run/user
  /dev/sdc1         16334         0      16334   0% /mnt

拷贝linux镜像到启动分区
  # cp uImage /mnt/

拷贝dtb文件到启动分区
  # cp sun7i-a20-pcduino3-nano.dtb  /mnt/

在启动分区中新建文件uEnv.txt,文件内容如下:
  fdt_high=ffffffff
  loadkernel=fatload  mmc 0 0x46000000 uImage
  loaddtb=fatload  mmc 0 0x49000000 sun7i-a20-pcduino3-nano.dtb
  bootargs=console=ttyS0,115200  earlyprintk root=/dev/mmcblk0p2 rootwait
  uenvcmd=run  loadkernel && run loaddtb && bootm 0x46000000 - 0x49000000

这个文件会被u-boot读取,其中loadkernel和loaddtb分别将uImage和sun7i-a20-pcduino3-nano.dtb加载到内存中。bootargs最终传递给linux作为启动命令行参数,需要提一下的是root=/dev/mmcblk0p2,起始就是上述SD卡的/dev/sdc2分区,只不过虚拟机和pcDuino识别分区后命名方式不同而已,这样告诉linux从这个分区挂载根文件系统。uenvcmd是u-boot最终调用的启动linux的命令,它集成了前面2个命令,以及bootm命令。

5. 将根文件系统镜像写入SD卡根文件系统分区。
  # dd if=rootfs.img  of=/dev/sdc2


完成了SD启动卡的全部准备工作之后,将SD卡插入pcDuino板的卡槽,上电启动,串口打印如下:
  U-Boot SPL  2014.04-10733-gea1ac32 (Nov 23 2015 - 20:23:40)
  Board:  Linksprite_pcDuino3
  DRAM: 1024 MiB
  CPU: 960000000Hz,  AXI/AHB/APB: 3/2/2
  spl: not an uImage at  1600
  
  
  U-Boot 2014.04-10733-gea1ac32  (Nov 23 2015 - 20:23:40) Allwinner Technology
  
  CPU:   Allwinner A20 (SUN7I)
  Board:  Linksprite_pcDuino3
  I2C:   ready
  DRAM:  1 GiB
  MMC:   SUNXI SD/MMC: 0
  *** Warning - bad CRC,  using default environment
  
  In:    serial
  Out:   serial
  Err:   serial
  Net:   dwmac.1c50000
  Hit any key to stop  autoboot:  0
  reading uEnv.txt
  264 bytes read in 17 ms  (14.6 KiB/s)
  Loaded environment from  uEnv.txt
  Running uenvcmd ...
  reading uImage
  3174744 bytes read in  176 ms (17.2 MiB/s)
  reading  sun7i-a20-pcduino3-nano.dtb
  28877 bytes read in 30  ms (939.5 KiB/s)
  ## Booting kernel from  Legacy Image at 46000000 ...
     Image Name:   Linux-4.4.0-rc2
     Image Type:   ARM Linux Kernel Image (uncompressed)
     Data Size:    3174680 Bytes = 3 MiB
     Load Address: 40008000
     Entry Point:  40008000
     Verifying Checksum ... OK
  ## Flattened Device  Tree blob at 49000000
     Booting using the fdt blob at 0x49000000
     Loading Kernel Image ... OK
     Using Device Tree in place at 49000000,  end 4900a0cc
  
  Starting kernel ...
  …
  [省略部分打印]
  …
  [    1.501284] VFS: Mounted root (ext2  filesystem) readonly on device 179:2.
  [    1.508746] devtmpfs: mounted
  [    1.512026] Freeing unused kernel memory:  284K (c0593000 - c05da000)
  #

进入了Linux shell,至此,我们成功的实现了从SD卡启动pcDuino板。



回复

使用道具 举报

发表于 2016-3-23 10:45:58 | 显示全部楼层
帮顶                              
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-20 17:19 , Processed in 0.029394 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表