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