自动解包ROM 文件获取uboot,uboot-spl ,uImage, rootfs.tar.gz 并烧写SD卡
1 #!/bin/bash 2 ## Author: Guohua.Wu <Guohua.Wu@nufront.com> 3 ## Rev: v0.1, Mon Jun 11 16:47:20 CST 2018 4 ## Description: Shell script to install SD card with images. 5 6 ## Open it for debug 7 set -x 8 ## partition size in MB 9 BOOTLOAD_RESERVE=16 10 BOOT_ROM_SIZE=16 11 EXT4_FS_SIZE=512 12 ##FAT32=TOTAL - EXT 13 FAT32_FS_SIZE=1024 14 15 help() { 16 17 bn=`basename $0` 18 cat << EOF 19 usage $bn <option> device_node 20 21 options: 22 -h displays this help message 23 -s only get partition size 24 -np not partition. 25 -f flash image. 26 EOF 27 } 28 29 echo " check the if root?" 30 userid=`id -u` 31 if [ $userid -ne "0" ]; then 32 echo "you're not root?" 33 exit 34 fi 35 36 37 # parse command line 38 moreoptions=1 39 node="na" 40 cal_only=0 41 flash_images=0 42 not_partition=0 43 while [ "$moreoptions" = 1 -a $# -gt 0 ]; do 44 case $1 in 45 -h) help; exit ;; 46 -s) cal_only=1 ;; 47 -f) flash_images=1 ;; 48 -np) not_partition=1 ;; 49 *) moreoptions=0; node=$1 ;; 50 esac 51 [ "$moreoptions" = 0 ] && [ $# -gt 1 ] && help && exit 52 [ "$moreoptions" = 1 ] && shift 53 done 54 55 if [ ! -e ${node} ]; then 56 help 57 exit 58 fi 59 60 61 # call sfdisk to create partition table 62 # get total card size 63 seprate=0 64 total_size=`sfdisk -s ${node}` 65 total_size=`expr ${total_size} / 1024` 66 boot_rom_sizeb=`expr ${BOOT_ROM_SIZE} + ${BOOTLOAD_RESERVE}` 67 fat32_size=`expr ${total_size} - ${boot_rom_sizeb} - ${EXT4_FS_SIZE} + ${seprate}` 68 69 # create partitions 70 if [ "${cal_only}" -eq "1" ]; then 71 cat << EOF 72 BOOT : ${boot_rom_sizeb}MB 73 FAT32 : ${fat32_size}MB 74 EXT4 : ${EXT4_FS_SIZE}MB 75 EOF 76 exit 77 fi 78 79 function format_partitions 80 { 81 echo "formating partitions" 82 mkfs.vfat ${node}${part}1 -n"vfat" 83 mkfs.ext4 ${node}${part}2 -L"rootfs" 84 # mkdir /media/tmp 85 # mount ${node}${part}2 /media/tmp 86 # amount=$(df -k | grep ${node}${part}2 | awk '{print $2}') 87 # stag=$amount 88 # stag=$((stag-32)) 89 # kilo=K 90 # amountkilo=$stag$kilo 91 # sleep 1s 92 # umount /media/tmp 93 # rm -rf /media/tmp 94 # e2fsck -f ${node}${part}2 95 # resize2fs ${node}${part}2 $amountkilo 96 } 97 98 function flash_filesystem 99 { 100 if [ "${flash_images}" -eq "1" ]; then 101 #TODO:: unpack the ROM image 102 OUTDIR=/tmp/nufront-sdcard-provision 103 POSTFIX='.img' 104 [ -d ${OUTDIR} ] && rm -rf ${OUTDIR} 105 mkdir ${OUTDIR} 106 romfile=$(find ./ -name "*.ROM" | sort -r | head -n 1) 107 unzip ${romfile} -d ${OUTDIR} 108 if [ $? == '0' ]; then 109 POSTFIX='' 110 cd ${OUTDIR} 111 fi 112 113 echo "flashing images..." 114 dd if=u-boot-spl${POSTFIX} of=${node} bs=512 seek=4096 115 if [ ${POSTFIX}"FROMROM" != "FROMROM" ]; then 116 dd if=u-boot${POSTFIX} of=${node} bs=512 seek=4608 117 else 118 dd if=uboot${POSTFIX} of=${node} bs=512 seek=4608 119 fi 120 dd if=/dev/zero of=${node} bs=512 seek=1536 count=16 121 122 #dd if=rootfs.img of=${node}${part}2 123 echo "Unpack the rootfs" 124 mkdir /media/tmp 125 mount -t ext4 ${node}${part}2 /media/tmp 126 tar zxvf rootfs.tar.gz -C /media/tmp 127 sync 128 umount -f ${node}${part}2 129 rm -rf /media/tmp 130 131 echo "Install kernel" 132 mkdir /media/tmp 133 mount -t vfat ${node}${part}1 /media/tmp 134 cp -rf uImage /media/tmp 135 sync 136 umount -f ${node}${part}1 137 rm -rf /media/tmp 138 139 if [ ${POSTFIX}"FROMROM" == "FROMROM" ]; then 140 cd - 141 fi 142 rm -rf ${OUTDIR} 143 fi 144 } 145 146 #try to umount the filesystem 147 df | grep ${node} 148 if [ $? == 0 ]; then 149 echo "Umounting the nodes" 150 umount -f ${node}* 151 fi 152 #umount -f ${node}* 153 154 if [[ "${not_partition}" -eq "1" && "${flash_images}" -eq "1" ]] ; then 155 flash_filesystem 156 exit 157 fi 158 159 echo "Destroy the partition table" 160 dd if=/dev/zero of=${node} bs=1024 count=1 161 162 boot_start=0 163 fat32_start=`expr ${boot_start} + ${boot_rom_sizeb} \* 2048` 164 ext4_start=`expr ${fat32_start} + ${fat32_size} \* 2048` 165 166 echo "Making the Partition" 167 sfdisk --force ${node} << EOF 168 ${fat32_start},${fat32_size}M,83 169 ${ext4_start},${EXT4_FS_SIZE}M,83 170 EOF 171 172 echo "Format the partitions" 173 part="" 174 echo ${node} | grep mmcblk > /dev/null 175 if [ "$?" -eq "0" ]; then 176 part="p" 177 fi 178 179 format_partitions 180 flash_filesystem 181 182 sync 183 sleep 1 184 echo "Reload the partition table" 185 partprobe 186 echo "Finished!!!"