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