9aed4f846b40391772206f373d9881961c0faf47
[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 LOOPDEV ?= "/dev/loop1"
11 LOOPDEV_BOOT ?= "/dev/loop2"
12 LOOPDEV_FS ?= "/dev/loop3"
14 IMAGE_CMD_sdimg () {
15         SDIMG=${WORKDIR}/sd.img
17         # sanity check fstab entry for boot partition mounting
18         if [ "x$(cat /etc/fstab | grep ${LOOPDEV_BOOT} | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then
19                 echo "/etc/fstab entries need to be created with the user flag for the loop devices like:"
20                 echo "${LOOPDEV_BOOT} ${WORKDIR}/tmp-mnt-boot msdos user 0 0"
21         false
22         fi
24         # cleanup loops
25         for loop in ${LOOPDEV} ${LOOPDEV_BOOT} ${LOOPDEV_FS} ; do
26                 ${LOSETUP} -d $loop || true
27         done
29         # If an SD image is already present, reuse and reformat it
30         if [ ! -e ${SDIMG} ] ; then
31                 dd if=/dev/zero of=${SDIMG} bs=$(echo '255 * 63 * 512' | bc) count=444
32         fi
34         ${LOSETUP} ${LOOPDEV} ${SDIMG}
36         # Create partition table
37         dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024
38         SIZE=$(/sbin/fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}')
39         CYLINDERS=$(echo $SIZE/255/63/512 | bc)
40         {
41         echo ,9,0x0C,*
42         echo ,,,-
43         } | /sbin/sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV}
45         # Prepare loop devices for boot and filesystem partitions
46         BOOT_OFFSET=32256
47         FS_OFFSET_SECT=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2)
48         FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc)
49         FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ 
50         |cut -d " " -f 4 | cut -d "+" -f 1)
51  
52         LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5)
53         LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc)
55         ${LOSETUP} -d ${LOOPDEV}
57         ${LOSETUP} ${LOOPDEV_BOOT} ${SDIMG} -o ${BOOT_OFFSET} 
59         /sbin/mkfs.msdos ${LOOPDEV_BOOT} -n boot $LOOPDEV_BLOCKS
61         # Prepare filesystem partition
62         # Copy ubi used by flashing scripts
63         if [ -e  ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ] ; then
64                 echo "Copying UBIFS image to file system"
65                 cp ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ${IMAGE_ROOTFS}/boot/fs.ubi
66         fi
68         # Prepare boot partion. First mount the boot partition, and copy the boot loader and supporting files
69         # from the root filesystem
71         mkdir -p ${WORKDIR}/tmp-mnt-boot
72         mount $LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot
74         echo "Copying bootloaders into the boot partition"
75         if [ -e ${IMAGE_ROOTFS}/boot/MLO ] ; then
76                 cp -v ${IMAGE_ROOTFS}/boot/MLO ${WORKDIR}/tmp-mnt-boot 
77         else
78                 cp -v ${DEPLOY_DIR_IMAGE}/MLO ${WORKDIR}/tmp-mnt-boot
79         fi
81         # Check for u-boot SPL
82         if [ -e ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.img ] ; then
83                 suffix=img
84         else
85                 suffix=bin
86         fi
88         if [ -e ${IMAGE_ROOTFS}/boot/u-boot.$suffix ] ; then
89                 cp -v ${IMAGE_ROOTFS}/boot/{u-boot.$suffix,user.txt,uEnv.txt} ${WORKDIR}/tmp-mnt-boot || true
90         else
91                 cp -v ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.$suffix ${WORKDIR}/tmp-mnt-boot/u-boot.$suffix 
92         fi
94         # Cleanup VFAT mount
95         echo "Cleaning up VFAT mount"
96         umount ${WORKDIR}/tmp-mnt-boot
97         ${LOSETUP} -d ${LOOPDEV_BOOT} || true
99         # Prepare ext3 parition
100         echo "Creating ext3 loopback"
101         ${LOSETUP} ${LOOPDEV_FS} ${SDIMG} -o ${FS_OFFSET}
103         # should use fdisk info
104         echo "Creating ext3 image"
105         touch ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
106         genext2fs -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
107         tune2fs -L ${IMAGE_NAME} -j ${WORKDIR}/${IMAGE_NAME}.rootfs.ext3
109         dd if=${WORKDIR}/${IMAGE_NAME}.rootfs.ext3 of=${LOOPDEV_FS}
111         ${LOSETUP} -d ${LOOPDEV_FS} || true
113         gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${PR}.img.gz
114         rm -f ${WORKDIR}/sd.img