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