#!/bin/bash # # Reference: <> # # SD Card partition layout: 1 Sector= 512B # # +-------------------------------+-----------------------+---------------------------------+ # | Start Address | Size | Usage | # +-------------------------------+-----------------------+---------------------------------+ # | 0x0 | 2 sectors(1K) | Reserved for partition table | # +-------------------------------+-----------------------+---------------------------------+ # | 2 sector(1K, 0x400) | 20414 sectors(9M+) | i.MX6ULL u-boot image | # +-------------------------------+-----------------------+---------------------------------+ # | 20480 sector(10M, 0xa00000) | 204800 sectors(100MB) | FAT32 Boot partition | # +-------------------------------+-----------------------+---------------------------------+ # | 225280 sector(110M,0x6e00000) | Remaining Space | EXT4 filesystem for rootfs | # +-------------------------------+-----------------------+---------------------------------+ # source ../scripts//setup_env.sh IMAGE_NAME=$(eval echo `jq -r ".IMAGE_CONF.NAME" $JSON_CONF`) IMAGE_SIZE=`jq -r ".IMAGE_CONF.SIZE" $JSON_CONF` IMG_COMPRESS=`jq -r ".IMAGE_CONF.COMPRESS" $JSON_CONF` # uboot name and MMC sector offset and size IMG_UBOOT=$(eval echo `jq -r ".BOOT_CONF.IMG_UBOOT" $JSON_CONF`) MMC_SECSIZE=`jq -r ".BOOT_CONF.SECSIZE" $JSON_CONF` UBOOT_OFSET=`jq -r ".BOOT_CONF.OFFSET" $JSON_CONF` # U-boot space Size 10MB UBOOT_SIZE=`jq -r ".BOOT_CONF.UBOOT_SIZE" $JSON_CONF` # vfat boot partition 100MB BOOT_SIZE=`jq -r ".BOOT_CONF.BOOT_PART" $JSON_CONF` MNT_POINT=./mnt LOOP_DEV=`losetup -f | cut -d/ -f3` function exit_handler() { echo "Shell script exit now, do some clean work" echo "" set +e mountpoint $MNT_POINT > /dev/null 2>&1 if [ $? == 0 ] ; then echo "umount ${MNT_POINT}" umount ${MNT_POINT} fi rm -rf ${MNT_POINT} if [ -L /dev/mapper/${LOOP_DEV}p1 ] ; then echo "kpartx -dv /dev/${LOOP_DEV}" kpartx -dv /dev/${LOOP_DEV} fi losetup -a | grep "${LOOP_DEV}" > /dev/null 2>&1 if [ $? == 0 ] ; then echo "losetup -d /dev/${LOOP_DEV}" losetup -d /dev/${LOOP_DEV} fi } function generate_image() { # FAT32 boot partition start/end address in MB BOOT_START=${UBOOT_SIZE} BOOT_END=`expr ${BOOT_START} + ${BOOT_SIZE}` pr_warn "Generate system image " dd if=/dev/zero of=${IMAGE_NAME} bs=1024k count=${IMAGE_SIZE} && sync chmod a+x ${IMAGE_NAME} pr_warn "Partition system image" parted ${IMAGE_NAME} mklabel msdos parted ${IMAGE_NAME} mkpart primary fat32 ${BOOT_START}M ${BOOT_END}M parted ${IMAGE_NAME} mkpart primary ext4 ${BOOT_END}M 100% sync } function format_partition() { pr_warn "losetup image on $LOOP_DEV" losetup /dev/${LOOP_DEV} ${IMAGE_NAME} kpartx -av /dev/${LOOP_DEV} pr_warn "format system image partition" mkfs.vfat /dev/mapper/${LOOP_DEV}p1 mkfs.ext4 /dev/mapper/${LOOP_DEV}p2 sync } function install_sysimg() { pr_warn "install u-boot image" sudo dd if=${IMG_UBOOT} of=${IMAGE_NAME} bs=${MMC_SECSIZE} seek=${UBOOT_OFSET} conv=notrunc,sync pr_warn "install linux kernel image" mkdir -p ${MNT_POINT} mount -t vfat /dev/mapper/${LOOP_DEV}p1 ${MNT_POINT} do_install_json $JSON_CONF "BOOT_IMAGES" umount ${MNT_POINT} } function install_rootfs() { pr_warn "install root filesystem " mount -t ext4 /dev/mapper/${LOOP_DEV}p2 ${MNT_POINT} do_install_json $JSON_CONF "BOOT_ROOTFS" sync umount ${MNT_POINT} } function do_distclean() { pr_warn "clean images files " rm -f *.img *.imx *.bz2 *.gz rm -rf boot drivers exit 0; } function do_image() { mkdir -p ${MNT_POINT} generate_image format_partition install_sysimg install_rootfs if [ $IMG_COMPRESS == yes ] ;then pr_warn " gzip compress system image " rm -f ${IMAGE_NAME}*.gz gzip ${IMAGE_NAME} fi rm -rf ${MNT_POINT} pr_warn "generate system image done" } function do_root() { echo "" if [[ $1 == "yes" ]] && [ `id -u` != 0 ] ; then echo "ERROR: This action must run as root!" echo "" exit; elif [[ $1 != "yes" ]] && [ `id -u` == 0 ] ; then echo "ERROR: This action cannot run as root!" echo "" exit; fi } function do_usage() { echo "" echo "Usage:" echo " $0 [-b] [-c] [-h]" echo " -b: download and build $PRJ_NAME" echo " -c: clean all the source code" echo " -h: show this help message" echo "" echo " WARNNING: This shell script must run as sudo" echo "" exit; } trap 'exit_handler' EXIT while getopts "bch" OPTNAME do case "${OPTNAME}" in "b") break; ;; "c") do_distclean ;; "*") do_usage ;; esac done do_root "yes" do_image exit;