1. 程式人生 > >imx6 android SD卡啟動

imx6 android SD卡啟動

工作中需要將imx6的android系統從SD卡啟動,所以就分析了MfgTool中的指令碼,分析android的分割槽情況,並嘗試自己操作,竟然成功了,記錄於此。

參考文件

sd卡重新分割槽

  分割槽使用MfgTool中的mksdcard-android.sh指令碼。下面對其進行分析。

  需要將SD卡umount才能夠從新進行分割槽。

#!/bin/bash
# 執行 sh mksdcard-android.sh /dev/mmcblk0
# partition size in MB
BOOTLOAD_RESERVE=8  # bootloader
BOOT_ROM_SIZE=8     
SYSTEM_ROM_SIZE=512 # system.img
CACHE_SIZE=512 # cache RECOVERY_ROM_SIZE=8 # recovery VENDER_SIZE=8 # vendor MISC_SIZE=8 #顯示幫助資訊 help() { bn=`basename $0` cat << EOF usage $bn <option> device_node options: -h displays this help message -s only get partition size -np not partition. -f
flash android image. EOF } # check the if root? userid=`id -u` if [ $userid -ne "0" ]; then echo "you're not root?" exit fi # parse command line moreoptions=1 node="na" cal_only=0 flash_images=0 not_partition=0 not_format_fs=0 while [ "$moreoptions" = 1 -a $# -gt 0 ]; do case
$1 in -h) help; exit ;; -s) cal_only=1 ;; -f) flash_images=1 ;; -np) not_partition=1 ;; -nf) not_format_fs=1 ;; *) moreoptions=0; node=$1 ;; esac [ "$moreoptions" = 0 ] && [ $# -gt 1 ] && help && exit [ "$moreoptions" = 1 ] && shift done if [ ! -e ${node} ]; then help exit fi # node /dev/mmcblk0 # call sfdisk to create partition table # get total card size seprate=40 # 檢視分割槽大小,位元組數 total_size=`sfdisk -s ${node}` # 將位元組轉為M total_size=`expr ${total_size} / 1024` boot_rom_sizeb=`expr ${BOOT_ROM_SIZE} + ${BOOTLOAD_RESERVE}` # 8M + 8M = 16M # 512M + 512M + 8M + 8M + 40M = 1240M extend_size=`expr ${SYSTEM_ROM_SIZE} + ${CACHE_SIZE} + ${VENDER_SIZE} + ${MISC_SIZE} + ${seprate}` # data_size = total_size - 16M - 8M - 1240M + 40M = total_size - 1224M data_size=`expr ${total_size} - ${boot_rom_sizeb} - ${RECOVERY_ROM_SIZE} - ${extend_size} + ${seprate}` # 這一部分只是顯示計算的大小 # create partitions if [ "${cal_only}" -eq "1" ]; then cat << EOF BOOT : ${boot_rom_sizeb}MB RECOVERY: ${RECOVERY_ROM_SIZE}MB SYSTEM : ${SYSTEM_ROM_SIZE}MB CACHE : ${CACHE_SIZE}MB DATA : ${data_size}MB MISC : ${MISC_SIZE}MB EOF exit fi # 刪除分割槽表 # destroy the partition table dd if=/dev/zero of=${node} bs=1024 count=1 # 建立分割槽 # sfdisk 命令 man 文件中有記錄。 # Id is given in hex, without the 0x prefix, or is [E|S|L|X], # where L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), # E is EXTENDED_PARTITION (5), and X is LINUX_EXTENDED (85). # 指定分割槽大小,以及分割槽的型別,83,LINUX_NATIVE,5:擴充套件分割槽 # 查案每種檔案系統型別,可以通過命令 sfdisk -T 檢視 sfdisk --force -uM ${node} << EOF ,${boot_rom_sizeb},83 # 16M ,${RECOVERY_ROM_SIZE},83 # 8M ,${extend_size},5 # 1240M ,${data_size},83 # total_size - 1224M ,${SYSTEM_ROM_SIZE},83 # 512M ,${CACHE_SIZE},83 # 512M ,${VENDER_SIZE},83 # 8M ,${MISC_SIZE},83 # 8M EOF # adjust the partition reserve for bootloader. # if you don't put the uboot on same device, you can remove the BOOTLOADER_ERSERVE # to have 8M space. # the minimal sylinder for some card is 4M, maybe some was 8M # just 8M for some big eMMC 's sylinder # 將/dev/mmcblk0分割槽之後的第一個分割槽進行調整 sfdisk --force -uM ${node} -N1 << EOF ${BOOTLOAD_RESERVE},${BOOT_ROM_SIZE},83 EOF # For MFGTool Notes: # MFGTool use mksdcard-android.tar store this script # if you want change it. # do following: # tar xf mksdcard-android.sh.tar # vi mksdcard-android.sh # [ edit want you want to change ] # rm mksdcard-android.sh.tar; tar cf mksdcard-android.sh.tar mksdcard-android.sh

映象拷貝到SD卡

  這些內容也是從Mfgtool的ucl2.mxl檔案中提取出來的。

  製作成指令碼repartition.sh,如下所示:

#!/bin/sh
# Tony Liu  2016-8-3

IMAGE_DIR="./image/"            #映象檔案存放的目錄
UBOOT=$IMAGE_DIR/u-boot.bin
BOOT_IMG=$IMAGE_DIR/boot.img
SYSTEM_IMG=$IMAGE_DIR/system.img
RECOVERY_IMG=$IMAGE_DIR/recovery.img

DEVICE=/dev/sdb                 #SD卡在linux中的裝置節點,視實際情況而定

MKSDCARD_SCRIPT=./mksdcard-android.sh   # android分割槽的指令碼
# 將SD卡分割槽   
sh $MKSDCARD_SCRIPT $DEVICE
# 對裝置寫0,每次塊大小是512位元組,從2 block的位置開始寫,寫2000次
# 這裡我猜想前面的1024位元組應該是留給分割槽表的。
dd if=/dev/zero of=$DEVICE bs=512 seek=2 count=2000         #Clean U-Bootenvironment
# 寫入u-boot.img。從u-boot.img開始2 block(skip=2)的位置開始讀取資料
# 寫入的地址是裝置偏移2block(seek=2)的位置(1M),塊大小512位元組。
dd if=$UBOOT of=$DEVICE bs=512 seek=2 skip=2    #write U-Boot to sdcard
# 寫入boot.img
dd if=$BOOT_IMG of=${DEVICE}1            #write boot.img

# 將SD卡的第4個分割槽格式化位ext4檔案系統,並指定卷標名稱為data
mkfs.ext4 -L data ${DEVICE}4             #Formatting sd partition

mkfs.ext4 -L system ${DEVICE}5           #Formatting system partition

mkfs.ext4 -L cache -O^extent ${DEVICE}6  #Formatting cache partition

mkfs.ext4 -L vender ${DEVICE}7           #Formatting data partition

mkfs.ext4 ${DEVICE}8                     #Formatting misc partition
# 寫入system.img
dd if=$SYSTEM_IMG of=${DEVICE}5 bs=512   #Sending and writting system.img
# 寫入recovery.img
dd if=$RECOVERY_IMG of=${DEVICE}2 bs=512 #Sending and writting recovery.img</CMD>

更改kernel啟動位置

  imx6從SD卡uboot啟動之後,需要在SD卡中的uboot指定核心執行的SD卡序號,否者會執行開發板的emmc中。

  可以在uboot中通過"mmc list",檢視有幾塊mmc。使用"booti mmc1",更改mmc序號,看是否能從SD卡啟動,來判斷SD卡的編號。

  我的板子上SD卡的序號是mmc1。將uboot配置檔案中mmc2更改為mmc1。這樣一來,就選擇從SD卡的核心啟動。

  vi mx6dl_sabresd_android.h

#define CONFIG_ANDROID_RECOVERY_BOOTCMD_MMC  \
"booti mmc1 recovery"
// Tony 2016-8-3
//"booti mmc2 recovery"
#define CONFIG_ANDROID_RECOVERY_CMD_FILE "/recovery/command"
#define CONFIG_INITRD_TAG

#undef CONFIG_LOADADDR
#undef CONFIG_RD_LOADADDR
#undef CONFIG_EXTRA_ENV_SETTINGS


#define CONFIG_LOADADDR     0x10800000  /* loadaddr env var */
#define CONFIG_RD_LOADADDR      0x11000000

#define CONFIG_INITRD_TAG
#define CONFIG_EXTRA_ENV_SETTINGS                   \
        "netdev=eth0\0"                     \
        "ethprime=FEC0\0"                   \
        "fastboot_dev=mmc1\0"                   \
        "bootcmd=booti mmc1\0"                  \
        "splashimage=0x30000000\0"              \
        "splashpos=m,m\0"                   \
        "android_test=keyvalue\0"                   \
        "bootargs=console=ttymxc0,115200 init=/init video=mxcfb0:dev=ldb,bpp=32 video=mxcfb1:off video=mxcfb2:off fbmem=40M fb0base=0x27b00000 vmalloc=400M androidboot.console=ttymxc0 androidboot.hardware=freescale\0" \
        "lvds_num=1\0"         
#endif

更改ramdisk掛載位置

  核心更改之後,需要將檔案系統掛載到SD卡的分割槽上。

  根據自己SD卡編號進行更改/dev/block/mmcblk後面的序號,我的是1。

  vi fstab.freescale

# Android fstab file.
# <src>             <mnt_point>  <type>  <mnt_flags>                                                                         <fs_mgr_flags>
# The filesystem that contains the filesystem checker binary (typically /system) cannot
# specify MF_CHECK, and must come before any filesystems that do specify MF_CHECK

#Tony add for SD card boot
/dev/block/mmcblk1p5    /system  ext4    ro                                                                               wait
/dev/block/mmcblk1p4    /data    ext4    nosuid,nodev,nodiratime,noatime,nomblk_io_submit,noauto_da_alloc,errors=panic    wait,encryptable=footer
/dev/block/mmcblk1p6    /cache   ext4    nosuid,nodev,nomblk_io_submit                                        wait
/dev/block/mmcblk1p7    /device  ext4    ro,nosuid,nodev                                                  wait

  執行指令碼輸出內容:

[email protected]:~/imx6_sdcard_boot$ sudo ./repartition.sh 
###  首先進行SD卡分割槽
1+0 records in
1+0 records out
1024 bytes (1.0 kB) copied, 0.00902186 s, 114 kB/s
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 1022 cylinders, 245 heads, 62 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sdb: unrecognized partition table type
Old situation:
No partitions found
Warning: given size (6520) exceeds max allowable size (6460)
New situation:
Units = mebibytes of 1048576 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start   End    MiB    #blocks   Id  System
/dev/sdb1         0+    22-    23-     22784+  83  Linux
/dev/sdb2        22+    37-    15-     15190   83  Linux
/dev/sdb3        37+  1119-  1083-   1108870    5  Extended
/dev/sdb4      1119+  7639-  6520-   6676005   83  Linux
/dev/sdb5        37+   556-   520-    531649+  83  Linux
/dev/sdb6       556+  1075-   520-    531649+  83  Linux
/dev/sdb7      1075+  1090-    15-     15189+  83  Linux
/dev/sdb8      1090+  1105-    15-     15189+  83  Linux
Warning: partition 4 extends past end of disk
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
Checking that no-one is using this disk right now ...
OK

Disk /dev/sdb: 1022 cylinders, 245 heads, 62 sectors/track
Old situation:
Units = mebibytes of 1048576 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start   End    MiB    #blocks   Id  System
/dev/sdb1         0+    22-    23-     22784+  83  Linux
/dev/sdb2        22+    37-    15-     15190   83  Linux
/dev/sdb3        37+  1119-  1083-   1108870    5  Extended
/dev/sdb4      1119+  7639-  6520-   6676005   83  Linux
/dev/sdb5        37+   556-   520-    531649+  83  Linux
/dev/sdb6       556+  1075-   520-    531649+  83  Linux
/dev/sdb7      1075+  1090-    15-     15189+  83  Linux
/dev/sdb8      1090+  1105-    15-     15189+  83  Linux
New situation:
Units = mebibytes of 1048576 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start   End    MiB    #blocks   Id  System
/dev/sdb1         7+    22-    15-     15190   83  Linux
/dev/sdb2        22+    37-    15-     15190   83  Linux
/dev/sdb3        37+  1119-  1083-   1108870    5  Extended
/dev/sdb4      1119+  7639-  6520-   6676005   83  Linux
/dev/sdb5        37+   556-   520-    531649+  83  Linux
/dev/sdb6       556+  1075-   520-    531649+  83  Linux
/dev/sdb7      1075+  1090-    15-     15189+  83  Linux
/dev/sdb8      1090+  1105-    15-     15189+  83  Linux
Warning: partition 4 extends past end of disk
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)
#-------------------------------------------------------------------------------
# 上面的內容是SD卡從新分割槽,下面進行dd檔案拷貝
2000+0 records in
2000+0 records out
1024000 bytes (1.0 MB) copied, 2.21602 s, 462 kB/s   # dd命令還能夠測讀寫速度
533+1 records in
533+1 records out
273172 bytes (273 kB) copied, 1.33912 s, 204 kB/s
9916+0 records in
9916+0 records out
5076992 bytes (5.1 MB) copied, 11.7538 s, 432 kB/s
mke2fs 1.42 (29-Nov-2011)
Filesystem label=data
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
413712 inodes, 1654536 blocks
82726 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1694498816
51 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

mke2fs 1.42 (29-Nov-2011)
Filesystem label=system
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
33280 inodes, 132912 blocks
6645 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=138412032
5 block groups
32768 blocks per group, 32768 fragments per group
6656 inodes per group
Superblock backups stored on blocks: 
    32768, 98304

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

mke2fs 1.42 (29-Nov-2011)
Filesystem label=cache
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
33280 inodes, 132912 blocks
6645 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=138412032
5 block groups
32768 blocks per group, 32768 fragments per group
6656 inodes per group
Superblock backups stored on blocks: 
    32768, 98304

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

mke2fs 1.42 (29-Nov-2011)
Filesystem label=vender
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
3808 inodes, 15188 blocks
759 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=15728640
2 block groups
8192 blocks per group, 8192 fragments per group
1904 inodes per group
Superblock backups stored on blocks: 
    8193

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

mke2fs 1.42 (29-Nov-2011)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
3808 inodes, 15188 blocks
759 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=15728640