I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard-android.sh hacking

时间:2022-10-04 09:27:09
#!/bin/bash

# 参考文章:
#   1. Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
#       http://c.biancheng.net/cpp/view/2739.html

# <CMD state="Updater" type="push" body="send" file="mksdcard-android.sh.tar">Sending partition shell</CMD>
# <CMD state="Updater" type="push" body="$ tar xf $FILE "> Partitioning...</CMD>
# <CMD state="Updater" type="push" body="$ sh mksdcard-android.sh /dev/mmcblk0"> Partitioning...</CMD>
# <CMD state="Updater" type="push" body="$ ls -l /dev/mmc* ">Formatting sd partition</CMD>

# partition size in MB
BOOTLOAD_RESERVE=8          # bootload  8MB 
BOOT_ROM_SIZE=8             # 启动rom   8MB
SYSTEM_ROM_SIZE=512         # 系统rom   512MB
CACHE_SIZE=512              # 缓存      512MB
RECOVERY_ROM_SIZE=8         # 恢复rom   8MB
VENDER_SIZE=8               # 供货商    8MB
MISC_SIZE=8                 # 杂项      8MB

help() {

# basename String [ Suffix ]
# basename 命令读取 String 参数,删除以 /(斜杠) 结尾的前缀以及任何指定的 Suffix 参数,
# 并将剩余的基本文件名称写至标准输出。
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?
# 检查当前是否拥有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
# $#: 传递给脚本或函数的参数个数。
#
# $ sh mksdcard-android.sh /dev/mmcblk0
# $1: /dev/mmcblk0
#
# moreoptions: 1
# node: /dev/mmcblk0
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

# 如果node设备节点不存在,那么就退出程序
if [ ! -e ${node} ]; then
    help
    exit
fi


# call sfdisk to create partition table
# 调用sfdisk来创建分区表
# get total card size
# extend_size 增加了这个分量,同时data_size减小了这个分量,所以磁盘总量不变
seprate=40         
# -s [or --show-size]: 显示一个分区的大小,单位是KB。
total_size=`sfdisk -s ${node}`
total_size=`expr ${total_size} / 1024`      # 重新计算,将单位换算成MB
boot_rom_sizeb=`expr ${BOOT_ROM_SIZE} + ${BOOTLOAD_RESERVE}`
extend_size=`expr ${SYSTEM_ROM_SIZE} + ${CACHE_SIZE} + ${VENDER_SIZE} + ${MISC_SIZE} + ${seprate}`
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
# 删除以前的分区表,从这里可以看出,分区表的大小貌似是1024字节
dd if=/dev/zero of=${node} bs=1024 count=1

sfdisk --force -uM ${node} << EOF
,${boot_rom_sizeb},83
,${RECOVERY_ROM_SIZE},83
,${extend_size},5
,${data_size},83
,${SYSTEM_ROM_SIZE},83
,${CACHE_SIZE},83
,${VENDER_SIZE},83
,${MISC_SIZE},83
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
# -N# : 只改变分区的编号 #
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