f7251f50cd38b96b39866fe8d53224e4dba62f99
[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
16 SDIMG_SIZE ?= "444" 
18 # FS type for rootfs
19 ROOTFSTYPE ?= "ext3"
21 BOOTPARTNAME_beaglebone = "BEAGLE_BONE"
22 BOOTPARTNAME ?= "${MACHINE}"
24 IMAGEDATESTAMP = "${@time.strftime('%Y.%m.%d',time.gmtime())}"
26 # Files and/or directories to be copied into the vfat partition
27 FATPAYLOAD ?= ""
29 IMAGE_CMD_sdimg () {
30         SDIMG=${WORKDIR}/sd.img
32         # sanity check fstab entry for boot partition mounting
33         if [ "x$(cat /etc/fstab | grep ${LOOPDEV_BOOT} | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then
34                 echo "/etc/fstab entries need to be created with the user flag for the loop devices like:"
35                 echo "${LOOPDEV_BOOT} ${WORKDIR}/tmp-mnt-boot vfat user 0 0"
36         false
37         fi
39         # cleanup loops
40         for loop in ${LOOPDEV} ${LOOPDEV_BOOT} ${LOOPDEV_FS} ; do
41                 ${LOSETUP} -d $loop || true
42         done
44         # If an SD image is already present, reuse and reformat it
45         if [ ! -e ${SDIMG} ] ; then
46                 dd if=/dev/zero of=${SDIMG} bs=$(echo '255 * 63 * 512' | bc) count=${SDIMG_SIZE}
47         fi
49         ${LOSETUP} ${LOOPDEV} ${SDIMG}
51         # Create partition table
52         dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024
53         SIZE=$(/sbin/fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}')
54         CYLINDERS=$(echo $SIZE/255/63/512 | bc)
55         {
56         echo ,9,0x0C,*
57         echo ,,,-
58         } | /sbin/sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV}
60         # Prepare loop devices for boot and filesystem partitions
61         BOOT_OFFSET=32256
62         FS_OFFSET_SECT=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2)
63         FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc)
64         FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ 
65         |cut -d " " -f 4 | cut -d "+" -f 1)
66  
67         LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5)
68         LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc)
70         ${LOSETUP} -d ${LOOPDEV}
72         ${LOSETUP} ${LOOPDEV_BOOT} ${SDIMG} -o ${BOOT_OFFSET} 
74         /sbin/mkfs.vfat ${LOOPDEV_BOOT} -n ${BOOTPARTNAME} $LOOPDEV_BLOCKS
76         # Prepare filesystem partition
77         # Copy ubi used by flashing scripts
78         if [ -e  ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ] ; then
79                 echo "Copying UBIFS image to file system"
80                 cp ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.ubi ${IMAGE_ROOTFS}/boot/fs.ubi
81         fi
83         # Prepare boot partion. First mount the boot partition, and copy the boot loader and supporting files
84         # from the root filesystem
86         mkdir -p ${WORKDIR}/tmp-mnt-boot
87         mount $LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot
89         echo "Copying bootloaders into the boot partition"
90         if [ -e ${IMAGE_ROOTFS}/boot/MLO ] ; then
91                 cp -v ${IMAGE_ROOTFS}/boot/MLO ${WORKDIR}/tmp-mnt-boot 
92         else
93                 cp -v ${DEPLOY_DIR_IMAGE}/MLO ${WORKDIR}/tmp-mnt-boot
94         fi
96         # Check for u-boot SPL
97         if [ -e ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.img ] ; then
98                 suffix=img
99         else
100                 suffix=bin
101         fi
103         cp -v ${IMAGE_ROOTFS}/boot/uEnv.txt ${WORKDIR}/tmp-mnt-boot || true
104         cp -v ${IMAGE_ROOTFS}/boot/user.txt ${WORKDIR}/tmp-mnt-boot || true
105         cp -v ${IMAGE_ROOTFS}/boot/uImage ${WORKDIR}/tmp-mnt-boot || true
107         if [ -e ${IMAGE_ROOTFS}/boot/u-boot.$suffix ] ; then
108                 cp -v ${IMAGE_ROOTFS}/boot/{u-boot.$suffix} ${WORKDIR}/tmp-mnt-boot || true
109         else
110                 cp -v ${DEPLOY_DIR_IMAGE}/u-boot-${MACHINE}.$suffix ${WORKDIR}/tmp-mnt-boot/u-boot.$suffix 
111         fi
113         if [ -n ${FATPAYLOAD} ] ; then
114                 echo "Copying payload into VFAT"
115                 for entry in ${FATPAYLOAD} ; do
116                                 # add the || true to stop aborting on vfat issues like not supporting .~lock files
117                                 cp -av ${IMAGE_ROOTFS}$entry ${WORKDIR}/tmp-mnt-boot || true
118                 done
119         fi
121         # Cleanup VFAT mount
122         echo "Cleaning up VFAT mount"
123         umount ${WORKDIR}/tmp-mnt-boot
124         ${LOSETUP} -d ${LOOPDEV_BOOT} || true
126         # Prepare rootfs parition
127         echo "Creating rootfs loopback"
128         ${LOSETUP} ${LOOPDEV_FS} ${SDIMG} -o ${FS_OFFSET}
130         # should use fdisk info
131         echo "Creating rootfs image"
132         touch ${WORKDIR}/${IMAGE_NAME}.rootfs.img
134         FS_NUM_INODES=$(echo $FS_SIZE_BLOCKS / 4 | bc)
136         case "${ROOTFSTYPE}" in
137                 ext3)
138                                 genext2fs -z -N $FS_NUM_INODES -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${WORKDIR}/${IMAGE_NAME}.rootfs.img
139                                 tune2fs -L ${IMAGE_NAME} -j ${WORKDIR}/${IMAGE_NAME}.rootfs.img
140                                 ;;
141                 ext4)
142                                 genext2fs -z -N $FS_NUM_INODES -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${WORKDIR}/${IMAGE_NAME}.rootfs.img
143                                 tune2fs -L ${IMAGE_NAME} -j -O extents,uninit_bg,dir_index ${WORKDIR}/${IMAGE_NAME}.rootfs.img
144                                 ;;
145                 *)
146                                 echo "Please set ROOTFSTYPE to something supported"
147                                 exit 1
148                                 ;;
149         esac
151         dd if=${WORKDIR}/${IMAGE_NAME}.rootfs.img of=${LOOPDEV_FS}
153         ${LOSETUP} -d ${LOOPDEV_FS} || true
155         gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${IMAGEDATESTAMP}.img.gz
156         rm -f ${WORKDIR}/sd.img