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