sdcard_image: Helper class to create SD Card images
[glsdk/meta-ti-glsdk.git] / classes / sdcard_image.bbclass
1 inherit image
3 IMAGE_CMD_sdimg () {
4         SDIMG=${WORKDIR}/sd.img
6         # cleanup loops
7         for loop in $(losetup -j ${SDIMG}); do
8                 loop_dev=$(echo $loop|cut -d ":" -f 1)
9                 umount $loop_dev || true
10                 losetup -d $loop_dev || true
11         done
13         dd if=/dev/zero of=${SDIMG} bs=4k seek=$(echo '256 * 1024' | bc) count=1
14         losetup -f ${SDIMG}
15         LOOPDEV=$(losetup -j ${SDIMG} -o 0 | cut -d ":" -f 1)
17         # Create partition table
18         dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024
19         SIZE=`fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}'`
20         CYLINDERS=`echo $SIZE/255/63/512 | bc`
21         {
22         echo ,9,0x0C,*
23         echo ,,,-
24         } | sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV}
26         # Prepare loop devices for boot and filesystem partitions
27         BOOT_OFFSET=32256
28         FS_OFFSET_SECT=$(/sbin/fdisk -l -u $LOOPDEV 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2)
29         FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc)
30         FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u $LOOPDEV 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ 
31         |cut -d " " -f 4 | cut -d "+" -f 1)
32  
33         LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u $LOOPDEV 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5)
34         LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc)
36         losetup -f ${SDIMG} -o ${BOOT_OFFSET} --sizelimit=$LOOPDEV_BYTES
38         LOOPDEV_BOOT=$(losetup -j ${SDIMG} -o ${BOOT_OFFSET} | cut -d ":" -f 1)
39         mkfs.msdos ${LOOPDEV_BOOT} -n boot
41         losetup -f ${SDIMG} -o ${FS_OFFSET}     
42         LOOPDEV_FS=$(losetup -j ${SDIMG} -o ${FS_OFFSET} | cut -d ":" -f 1)
44         # Prepare filesystem partition
45         # Copy ubi used by flashing scripts
46         if [ -e  ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ] ; then
47                 echo "Copying UBIFS image to file system"
48                 cp ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ${IMAGE_ROOTFS}/boot/fs.ubi
49         fi
50         ROOTFS_SIZE="$(du -ks ${IMAGE_ROOTFS} | awk '{print 65536 + $1}')"
51         genext2fs -b ${FS_SIZE_BLOCKS} -d ${IMAGE_ROOTFS} ${LOOPDEV_FS}
52         tune2fs -j ${LOOPDEV_FS}
54         # Prepare boot partion. First mount the boot partition, and copy the boot loader and supporting files
55         # from the root filesystem
57         # sanity check fstab entry for boot partition mounting
58         if [ "x$(cat /etc/fstab | grep $LOOPDEV_BOOT | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then
59                 echo "/etc/fstab entries need to be created with the user flag for $LOOPDEV_BOOT like:"
60                 echo "$LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot msdos user 0 0"
61                 false
62         fi
64         mkdir -p ${WORKDIR}/tmp-mnt-boot
65         mount $LOOPDEV_BOOT
67         echo "Copying bootloaders into the boot partition"
68         cp -v ${IMAGE_ROOTFS}/boot/MLO ${WORKDIR}/tmp-mnt-boot 
69         cp -v ${IMAGE_ROOTFS}/boot/{u-boot.bin,user.txt,uEnv.txt} ${WORKDIR}/tmp-mnt-boot || true
71         # cleanup
72         umount ${LOOPDEV_BOOT}
73         /sbin/losetup -d ${LOOPDEV}
74         /sbin/losetup -d ${LOOPDEV_BOOT}
75         /sbin/losetup -d ${LOOPDEV_FS}
77         gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${PR}.img.gz
78 }