sdcard_image bbclass: make it work with older losetup versions among other things
[glsdk/meta-ti-glsdk.git] / classes / sdcard_image.bbclass
index b3885df62e47e91be4d63f4c051b70af0cc1238f..7ab565a609ad3dcd9c1600dd05cfe4f547892064 100644 (file)
@@ -1,45 +1,62 @@
 inherit image
 
 inherit image
 
+# Add the fstypes we need
+IMAGE_FSTYPES_append = " tar.bz2 sdimg"
+
+# Change this to match your host distro
+LOSETUP ?= "/sbin/losetup"
+
+# Since these need to go in /etc/fstab we can hardcode them
+LOOPDEV ?= "/dev/loop1"
+LOOPDEV_BOOT ?= "/dev/loop2"
+LOOPDEV_FS ?= "/dev/loop3"
+
 IMAGE_CMD_sdimg () {
        SDIMG=${WORKDIR}/sd.img
 
 IMAGE_CMD_sdimg () {
        SDIMG=${WORKDIR}/sd.img
 
+       # sanity check fstab entry for boot partition mounting
+       if [ "x$(cat /etc/fstab | grep ${LOOPDEV_BOOT} | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then
+               echo "/etc/fstab entries need to be created with the user flag for the loop devices like:"
+               echo "${LOOPDEV_BOOT} ${WORKDIR}/tmp-mnt-boot msdos user 0 0"
+        false
+       fi
+
        # cleanup loops
        # cleanup loops
-       for loop in $(losetup -j ${SDIMG}); do
-               loop_dev=$(echo $loop|cut -d ":" -f 1)
-               umount $loop_dev || true
-               losetup -d $loop_dev || true
+       for loop in ${LOOPDEV} ${LOOPDEV_BOOT} ${LOOPDEV_FS} ; do
+               ${LOSETUP} -d $loop || true
        done
 
        done
 
-       dd if=/dev/zero of=${SDIMG} bs=4k seek=$(echo '256 * 1024' | bc) count=1
-       losetup -f ${SDIMG}
-       LOOPDEV=$(losetup -j ${SDIMG} -o 0 | cut -d ":" -f 1)
+       # If an SD image is already present, reuse and reformat it
+       if [ ! -e ${SDIMG} ] ; then
+               dd if=/dev/zero of=${SDIMG} bs=$(echo '255 * 63 * 512' | bc) count=444
+       fi
+
+       ${LOSETUP} ${LOOPDEV} ${SDIMG}
 
        # Create partition table
        dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024
 
        # Create partition table
        dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024
-       SIZE=`fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}'`
-       CYLINDERS=`echo $SIZE/255/63/512 | bc`
+       SIZE=$(/sbin/fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}')
+       CYLINDERS=$(echo $SIZE/255/63/512 | bc)
        {
        echo ,9,0x0C,*
        echo ,,,-
        {
        echo ,9,0x0C,*
        echo ,,,-
-       } | sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV}
+       } | /sbin/sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV}
 
        # Prepare loop devices for boot and filesystem partitions
        BOOT_OFFSET=32256
 
        # Prepare loop devices for boot and filesystem partitions
        BOOT_OFFSET=32256
-       FS_OFFSET_SECT=$(/sbin/fdisk -l -u $LOOPDEV 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2)
+       FS_OFFSET_SECT=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2)
        FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc)
        FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc)
-       FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u $LOOPDEV 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ 
+       FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ 
        |cut -d " " -f 4 | cut -d "+" -f 1)
  
        |cut -d " " -f 4 | cut -d "+" -f 1)
  
-       LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u $LOOPDEV 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5)
+       LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5)
        LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc)
 
        LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc)
 
-       losetup -f ${SDIMG} -o ${BOOT_OFFSET} --sizelimit=$LOOPDEV_BYTES
+       ${LOSETUP} -d ${LOOPDEV}
 
 
-       LOOPDEV_BOOT=$(losetup -j ${SDIMG} -o ${BOOT_OFFSET} | cut -d ":" -f 1)
-        mkfs.msdos ${LOOPDEV_BOOT} -n boot
+       ${LOSETUP} ${LOOPDEV_BOOT} ${SDIMG} -o ${BOOT_OFFSET} 
 
 
-       losetup -f ${SDIMG} -o ${FS_OFFSET}     
-       LOOPDEV_FS=$(losetup -j ${SDIMG} -o ${FS_OFFSET} | cut -d ":" -f 1)
+       /sbin/mkfs.msdos ${LOOPDEV_BOOT} -n boot $LOOPDEV_BLOCKS
 
        # Prepare filesystem partition
        # Copy ubi used by flashing scripts
 
        # Prepare filesystem partition
        # Copy ubi used by flashing scripts
@@ -47,32 +64,59 @@ IMAGE_CMD_sdimg () {
                echo "Copying UBIFS image to file system"
                cp ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ${IMAGE_ROOTFS}/boot/fs.ubi
        fi
                echo "Copying UBIFS image to file system"
                cp ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ${IMAGE_ROOTFS}/boot/fs.ubi
        fi
-       ROOTFS_SIZE="$(du -ks ${IMAGE_ROOTFS} | awk '{print 65536 + $1}')"
-       genext2fs -b ${FS_SIZE_BLOCKS} -d ${IMAGE_ROOTFS} ${LOOPDEV_FS}
-       tune2fs -j ${LOOPDEV_FS}
 
        # Prepare boot partion. First mount the boot partition, and copy the boot loader and supporting files
        # from the root filesystem
 
 
        # Prepare boot partion. First mount the boot partition, and copy the boot loader and supporting files
        # from the root filesystem
 
-       # sanity check fstab entry for boot partition mounting
-       if [ "x$(cat /etc/fstab | grep $LOOPDEV_BOOT | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then
-               echo "/etc/fstab entries need to be created with the user flag for $LOOPDEV_BOOT like:"
-               echo "$LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot msdos user 0 0"
-               false
-       fi
-
        mkdir -p ${WORKDIR}/tmp-mnt-boot
        mkdir -p ${WORKDIR}/tmp-mnt-boot
-       mount $LOOPDEV_BOOT
+       mount $LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot
 
        echo "Copying bootloaders into the boot partition"
 
        echo "Copying bootloaders into the boot partition"
-       cp -v ${IMAGE_ROOTFS}/boot/MLO ${WORKDIR}/tmp-mnt-boot 
-       cp -v ${IMAGE_ROOTFS}/boot/{u-boot.bin,user.txt,uEnv.txt} ${WORKDIR}/tmp-mnt-boot || true
+       if [ -e ${IMAGE_ROOTFS}/boot/MLO ] ; then
+               cp -v ${IMAGE_ROOTFS}/boot/MLO ${WORKDIR}/tmp-mnt-boot 
+       else
+               cp -v ${DEPLOY_DIR_IMAGE}/MLO ${WORKDIR}/tmp-mnt-boot
+       fi
+
+       # Check for u-boot SPL
+       if [ -e ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.img ] ; then
+               suffix=img
+       else
+               suffix=bin
+       fi
+
+       if [ -e ${IMAGE_ROOTFS}/boot/u-boot.$suffix ] ; then
+               cp -v ${IMAGE_ROOTFS}/boot/{u-boot.$suffix,user.txt,uEnv.txt} ${WORKDIR}/tmp-mnt-boot || true
+       else
+               cp -v ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.$suffix ${WORKDIR}/tmp-mnt-boot/u-boot.$suffix 
+       fi
+
+       # Deprecated, u-boot should load it from ext3 instead
+       if [ -e ${IMAGE_ROOTFS}/boot/uImage ] ; then
+               cp -v ${IMAGE_ROOTFS}/boot/uImage ${WORKDIR}/tmp-mnt-boot
+       else
+               cp -v ${DEPLOY_DIR_IMAGE}/uImage-${MACHINE}.bin ${WORKDIR}/tmp-mnt-boot/uImage
+       fi
+
+       # Cleanup VFAT mount
+       echo "Cleaning up VFAT mount"
+       umount ${WORKDIR}/tmp-mnt-boot
+       ${LOSETUP} -d ${LOOPDEV_BOOT} || true
+
+       # Prepare ext3 parition
+       echo "Creating ext3 loopback"
+       ${LOSETUP} ${LOOPDEV_FS} ${SDIMG} -o ${FS_OFFSET}
+
+       # should use fdisk info
+       echo "Creating ext3 image"
+       touch ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
+       genext2fs -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
+       tune2fs -L ${IMAGE_NAME} -j ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
+
+       dd if=${WORKDIR}/${IMAGE_NAME}.rootfs.ext3 of=${LOOPDEV_FS}
 
 
-       # cleanup
-       umount ${LOOPDEV_BOOT}
-       /sbin/losetup -d ${LOOPDEV}
-       /sbin/losetup -d ${LOOPDEV_BOOT}
-       /sbin/losetup -d ${LOOPDEV_FS}
+       ${LOSETUP} -d ${LOOPDEV_FS} || true
 
        gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${PR}.img.gz
 
        gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${PR}.img.gz
+       rm -f ${WORKDIR}/sd.img
 }
 }