From 7186ede68b2a96cac80d16b3d2e95cdc5b8a2c5f Mon Sep 17 00:00:00 2001 From: "sam.xiang" Date: Sat, 11 Mar 2023 00:05:58 +0800 Subject: [PATCH] Add a script to package the SD card boot image Change-Id: Ia9f31c72725a25abdd55fb1bd114e4f4dd466f4c --- build/common_functions.sh | 5 ++ .../common/sd_tools/sd_gen_burn_image.sh | 84 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100755 build/tools/common/sd_tools/sd_gen_burn_image.sh diff --git a/build/common_functions.sh b/build/common_functions.sh index 90b1cdf84..474b7dcdf 100644 --- a/build/common_functions.sh +++ b/build/common_functions.sh @@ -244,6 +244,11 @@ function pack_upgrade command rm -rf "$TMPDIR" )} +function pack_sd_image +{( + "$COMMON_TOOLS_PATH"/sd_tools/sd_gen_burn_image.sh "$OUTPUT_DIR" +)} + function pack_prog_img {( local tmp_dir diff --git a/build/tools/common/sd_tools/sd_gen_burn_image.sh b/build/tools/common/sd_tools/sd_gen_burn_image.sh new file mode 100755 index 000000000..0f5994648 --- /dev/null +++ b/build/tools/common/sd_tools/sd_gen_burn_image.sh @@ -0,0 +1,84 @@ +#!/bin/bash +# a sd image generator for sophpi +# usage +if [ "$#" -ne "1" ] +then + echo "usage: sudo ./sd_gen_burn_image.sh OUTPUT_DIR" + echo "" + echo " The script is used to create a sdcard image with two partitions, " + echo " one is fat32 with 128MB, the other is ext4 with 256MB." + echo " You can modify the capacities in this script as you wish!" + echo "" + echo "Note: Please backup you sdcard files before using this image!" + + exit +fi + +vfat_cap=128M +vfat_label="boot" +ext4_cap=256M +ext4_label="rootfs" + +output_dir=$1 +echo ${output_dir} +pushd ${output_dir} + +# gen a empty image +image=sophpi-duo-`date +%Y%m%d-%H%M`.img +echo ${image} +dd if=/dev/zero of=./${image} bs=1M count=512 + +################################ +# Note: do not change this flow +################################ +sudo fdisk ./${image} << EOF +n +p +1 + ++${vfat_cap} +n +p +2 + ++${ext4_cap} +w +EOF +# Note end +################################ + +dev_name=`sudo losetup -f` +echo ${dev_name} +echo "" + +sudo losetup ${dev_name} ./${image} +sudo partprobe ${dev_name} + +sudo mkfs.vfat -F 32 -n ${vfat_label} ${dev_name}p1 +sudo mkfs.ext4 -L ${ext4_label} ${dev_name}p2 + +# mount partitions +rm ./tmp1 ./tmp2 -rf +mkdir tmp1 tmp2 +sudo mount -t vfat ${dev_name}p1 tmp1/ +sudo mount -t ext4 ${dev_name}p2 tmp2/ + +# copy boot file and rootfs +sudo cp ${output_dir}/fip.bin ./tmp1/ +sudo cp ${output_dir}/rawimages/boot.sd ./tmp1/ +sudo cp -raf ${output_dir}/rootfs/* ./tmp2 + +sync + +# umount +sudo umount tmp1 tmp2 +sudo losetup -d ${dev_name} +rmdir tmp1 tmp2 + +# tar image +tar zcvf ${image}.tar.gz ${image} + +echo "Gen image successful: ${image}" +echo "" + +popd