sdcard_image bbclass: add timestamp to image filename
[glsdk/meta-ti-glsdk.git] / classes / sdcard_image.bbclass
1 inherit image
3 # Add the fstypes we need
4 IMAGE_FSTYPES_append = " tar.bz2 sdimg"
6 # Change this to match your host distro
7 LOSETUP ?= "/sbin/losetup"
9 # Since these need to go in /etc/fstab we can hardcode them
10 # Since the vars are weakly assigned, you can override them from your local.conf
11 LOOPDEV ?= "/dev/loop1"
12 LOOPDEV_BOOT ?= "/dev/loop2"
13 LOOPDEV_FS ?= "/dev/loop3"
15 # Default to 4GiB images
16 SDIMG_SIZE ?= "444" 
18 BOOTPARTNAME_beaglebone = "BEAGLE_BONE"
19 BOOTPARTNAME ?= "${MACHINE}"
21 IMAGEDATESTAMP = "${@time.strftime('%Y.%m.%d',time.gmtime())}"
23 IMAGE_CMD_sdimg () {
24         SDIMG=${WORKDIR}/sd.img
26         # sanity check fstab entry for boot partition mounting
27         if [ "x$(cat /etc/fstab | grep ${LOOPDEV_BOOT} | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then
28                 echo "/etc/fstab entries need to be created with the user flag for the loop devices like:"
29                 echo "${LOOPDEV_BOOT} ${WORKDIR}/tmp-mnt-boot msdos user 0 0"
30         false
31         fi
33         # cleanup loops
34         for loop in ${LOOPDEV} ${LOOPDEV_BOOT} ${LOOPDEV_FS} ; do
35                 ${LOSETUP} -d $loop || true
36         done
38         # If an SD image is already present, reuse and reformat it
39         if [ ! -e ${SDIMG} ] ; then
40                 dd if=/dev/zero of=${SDIMG} bs=$(echo '255 * 63 * 512' | bc) count=${SDIMG_SIZE}
41         fi
43         ${LOSETUP} ${LOOPDEV} ${SDIMG}
45         # Create partition table
46         dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024
47         SIZE=$(/sbin/fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}')
48         CYLINDERS=$(echo $SIZE/255/63/512 | bc)
49         {
50         echo ,9,0x0C,*
51         echo ,,,-
52         } | /sbin/sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV}
54         # Prepare loop devices for boot and filesystem partitions
55         BOOT_OFFSET=32256
56         FS_OFFSET_SECT=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2)
57         FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc)
58         FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ 
59         |cut -d " " -f 4 | cut -d "+" -f 1)
60  
61         LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5)
62         LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc)
64         ${LOSETUP} -d ${LOOPDEV}
66         ${LOSETUP} ${LOOPDEV_BOOT} ${SDIMG} -o ${BOOT_OFFSET} 
68         /sbin/mkfs.msdos ${LOOPDEV_BOOT} -n ${BOOTPARTNAME} $LOOPDEV_BLOCKS
70         # Prepare filesystem partition
71         # Copy ubi used by flashing scripts
72         if [ -e  ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ] ; then
73                 echo "Copying UBIFS image to file system"
74                 cp ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ${IMAGE_ROOTFS}/boot/fs.ubi
75         fi
77         # Prepare boot partion. First mount the boot partition, and copy the boot loader and supporting files
78         # from the root filesystem
80         mkdir -p ${WORKDIR}/tmp-mnt-boot
81         mount $LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot
83         echo "Copying bootloaders into the boot partition"
84         if [ -e ${IMAGE_ROOTFS}/boot/MLO ] ; then
85                 cp -v ${IMAGE_ROOTFS}/boot/MLO ${WORKDIR}/tmp-mnt-boot 
86         else
87                 cp -v ${DEPLOY_DIR_IMAGE}/MLO ${WORKDIR}/tmp-mnt-boot
88         fi
90         # Check for u-boot SPL
91         if [ -e ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.img ] ; then
92                 suffix=img
93         else
94                 suffix=bin
95         fi
97         cp -v ${IMAGE_ROOTFS}/boot/uEnv.txt ${WORKDIR}/tmp-mnt-boot || true
98         cp -v ${IMAGE_ROOTFS}/boot/user.txt ${WORKDIR}/tmp-mnt-boot || true
100         if [ -e ${IMAGE_ROOTFS}/boot/u-boot.$suffix ] ; then
101                 cp -v ${IMAGE_ROOTFS}/boot/{u-boot.$suffix} ${WORKDIR}/tmp-mnt-boot || true
102         else
103                 cp -v ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.$suffix ${WORKDIR}/tmp-mnt-boot/u-boot.$suffix 
104         fi
106         # Cleanup VFAT mount
107         echo "Cleaning up VFAT mount"
108         umount ${WORKDIR}/tmp-mnt-boot
109         ${LOSETUP} -d ${LOOPDEV_BOOT} || true
111         # Prepare ext3 parition
112         echo "Creating ext3 loopback"
113         ${LOSETUP} ${LOOPDEV_FS} ${SDIMG} -o ${FS_OFFSET}
115         # should use fdisk info
116         echo "Creating ext3 image"
117         touch ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
118         # lots of small files, so use 8k per inode, not 64k
119         genext2fs -i 8192 -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
120         tune2fs -L ${IMAGE_NAME} -j ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
122         dd if=${WORKDIR}/${IMAGE_NAME}.rootfs.ext3 of=${LOOPDEV_FS}
124         ${LOSETUP} -d ${LOOPDEV_FS} || true
126         gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${IMAGEDATESTAMP}.img.gz
127         rm -f ${WORKDIR}/sd.img