diff options
-rw-r--r-- | classes/sdcard_image-rpi.bbclass | 194 |
1 files changed, 81 insertions, 113 deletions
diff --git a/classes/sdcard_image-rpi.bbclass b/classes/sdcard_image-rpi.bbclass index 1ac3e84..ef27ac6 100644 --- a/classes/sdcard_image-rpi.bbclass +++ b/classes/sdcard_image-rpi.bbclass | |||
@@ -1,131 +1,99 @@ | |||
1 | inherit image | 1 | # |
2 | 2 | # Create an image that can by written onto a SD card using dd. | |
3 | # Add the fstypes we need | 3 | # |
4 | IMAGE_FSTYPES_append = " tar.bz2 rpi-sdimg" | 4 | # The disk layout used is: |
5 | 5 | # | |
6 | # Ensure required utilities are present | 6 | # 0 - 1M - reserved for other data |
7 | IMAGE_DEPENDS_rpi-sdimg = "genext2fs-native e2fsprogs-native bcm2835-bootfiles bcm2835-kernel-image" | 7 | # 1M - BOOT_SPACE - bootloader and kernel |
8 | 8 | # BOOT_SPACE - SDIMG_SIZE - rootfs | |
9 | # Register this as an avalable type of image. | 9 | # |
10 | IMAGE_TYPES_append = " rpi-sdimg" | 10 | |
11 | 11 | inherit image_types | |
12 | # Change this to match your host distro | 12 | |
13 | LOSETUP ?= "/sbin/losetup" | 13 | # Set kernel and boot loader |
14 | 14 | IMAGE_BOOTLOADER ?= "bcm2835-bootfiles" | |
15 | # Since these need to go in /etc/fstab we can hardcode them | 15 | |
16 | # Since the vars are weakly assigned, you can override them from your local.conf | 16 | # Default to 1.4GiB images |
17 | LOOPDEV ?= "/dev/loop1" | 17 | SDIMG_SIZE ?= "4000" |
18 | LOOPDEV_BOOT ?= "/dev/loop2" | 18 | |
19 | LOOPDEV_FS ?= "/dev/loop3" | 19 | # Boot partition volume id |
20 | 20 | BOOTDD_VOLUME_ID ?= "${MACHINE}" | |
21 | # Default to 4GiB images | 21 | |
22 | SDIMG_SIZE ?= "444" | 22 | # Addional space for boot partition |
23 | 23 | BOOT_SPACE ?= "20MiB" | |
24 | # FS type for rootfs | 24 | |
25 | ROOTFSTYPE ?= "ext4" | 25 | # Use an ext3 by default as rootfs |
26 | 26 | SDIMG_ROOTFS_TYPE ?= "ext3" | |
27 | BOOTPARTNAME ?= "${MACHINE}" | 27 | SDIMG_ROOTFS = "${IMAGE_NAME}.rootfs.${SDIMG_ROOTFS_TYPE}" |
28 | 28 | ||
29 | IMAGEDATESTAMP = "${@time.strftime('%Y.%m.%d',time.gmtime())}" | 29 | # Set GPU firmware image to be used |
30 | # arm128 : 128M ARM, 128M GPU split | ||
31 | # arm192 : 192M ARM, 64M GPU split | ||
32 | # arm224 : 224M ARM, 32M GPU split | ||
33 | RPI_GPU_FIRMWARE ?= "arm192" | ||
34 | |||
35 | IMAGE_DEPENDS_rpi-sdimg = " \ | ||
36 | parted-native \ | ||
37 | mtools-native \ | ||
38 | dosfstools-native \ | ||
39 | virtual/kernel \ | ||
40 | ${IMAGE_BOOTLOADER} \ | ||
41 | " | ||
42 | |||
43 | # SD card image name | ||
44 | SDIMG = "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.rpi-sdimg" | ||
30 | 45 | ||
31 | # Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS. | 46 | # Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS. |
32 | FATPAYLOAD ?= "" | 47 | FATPAYLOAD ?= "" |
33 | 48 | ||
34 | IMAGE_CMD_rpi-sdimg () { | 49 | IMAGEDATESTAMP = "${@time.strftime('%Y.%m.%d',time.gmtime())}" |
35 | SDIMG=${WORKDIR}/sd.img | ||
36 | |||
37 | # sanity check fstab entry for boot partition mounting | ||
38 | if [ "x$(cat /etc/fstab | grep ${LOOPDEV_BOOT} | grep ${WORKDIR}/tmp-mnt-boot | grep user || true)" = "x" ]; then | ||
39 | echo "/etc/fstab entries need to be created with the user flag for the loop devices like:" | ||
40 | echo "${LOOPDEV_BOOT} ${WORKDIR}/tmp-mnt-boot vfat user 0 0" | ||
41 | false | ||
42 | fi | ||
43 | |||
44 | # cleanup loops | ||
45 | for loop in ${LOOPDEV} ${LOOPDEV_BOOT} ${LOOPDEV_FS} ; do | ||
46 | ${LOSETUP} -d $loop || true | ||
47 | done | ||
48 | |||
49 | # If an SD image is already present, reuse and reformat it | ||
50 | if [ ! -e ${SDIMG} ] ; then | ||
51 | dd if=/dev/zero of=${SDIMG} bs=$(echo '255 * 63 * 512' | bc) count=${SDIMG_SIZE} | ||
52 | fi | ||
53 | 50 | ||
54 | ${LOSETUP} ${LOOPDEV} ${SDIMG} | 51 | IMAGE_CMD_rpi-sdimg () { |
52 | # Initialize sdcard image file | ||
53 | dd if=/dev/zero of=${SDIMG} bs=1 count=0 seek=$(expr 1000 \* 1000 \* ${SDIMG_SIZE}) | ||
55 | 54 | ||
56 | # Create partition table | 55 | # Create partition table |
57 | dd if=/dev/zero of=${LOOPDEV} bs=1024 count=1024 | 56 | parted -s ${SDIMG} mklabel msdos |
58 | SIZE=$(/sbin/fdisk -l ${LOOPDEV} | grep Disk | grep bytes | awk '{print $5}') | 57 | # Create boot partition and mark it as bootable |
59 | CYLINDERS=$(echo $SIZE/255/63/512 | bc) | 58 | parted -s ${SDIMG} mkpart primary fat32 1MiB ${BOOT_SPACE} |
60 | { | 59 | parted -s ${SDIMG} set 1 boot on |
61 | echo ,9,0x0C,* | 60 | # Create rootfs partition |
62 | echo ,,,- | 61 | parted -s ${SDIMG} mkpart primary ext2 ${BOOT_SPACE} 100% |
63 | } | /sbin/sfdisk -D -H 255 -S 63 -C ${CYLINDERS} ${LOOPDEV} | 62 | parted ${SDIMG} print |
64 | 63 | ||
65 | # Prepare loop devices for boot and filesystem partitions | 64 | # Create a vfat image with boot files |
66 | BOOT_OFFSET=32256 | 65 | BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }') |
67 | FS_OFFSET_SECT=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /"|cut -d " " -f 2) | 66 | mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS |
68 | FS_OFFSET=$(echo "$FS_OFFSET_SECT * 512" | bc) | 67 | case "${RPI_GPU_FIRMWARE}" in |
69 | FS_SIZE_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep Linux | perl -p -i -e "s/\s+/ /g" \ | 68 | "arm128" | "arm192" | "arm224") |
70 | |cut -d " " -f 4 | cut -d "+" -f 1) | 69 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/${RPI_GPU_FIRMWARE}_start.elf ::start.elf |
71 | 70 | ;; | |
72 | LOOPDEV_BLOCKS=$(/sbin/fdisk -l -u ${LOOPDEV} 2>&1 | grep FAT | perl -p -i -e "s/\s+/ /g"|cut -d " " -f 5) | 71 | *) |
73 | LOOPDEV_BYTES=$(echo "$LOOPDEV_BLOCKS * 1024" | bc) | 72 | bberror "RPI_GPU_FIRMWARE is undefined or value not recongnised. Possible values: arm128, arm192 or arm224." |
74 | 73 | exit 1 | |
75 | ${LOSETUP} -d ${LOOPDEV} | 74 | ;; |
76 | 75 | esac | |
77 | ${LOSETUP} ${LOOPDEV_BOOT} ${SDIMG} -o ${BOOT_OFFSET} | ||
78 | |||
79 | /sbin/mkfs.vfat ${LOOPDEV_BOOT} -n ${BOOTPARTNAME} $LOOPDEV_BLOCKS | ||
80 | |||
81 | # Prepare boot partion. First mount the boot partition, and copy the bootloader and supporting files. | ||
82 | |||
83 | mkdir -p ${WORKDIR}/tmp-mnt-boot | ||
84 | mount $LOOPDEV_BOOT ${WORKDIR}/tmp-mnt-boot | ||
85 | 76 | ||
86 | echo "Copying bootloader and prepended kernel.img into the boot partition" | 77 | # To do |
87 | cp -v ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ${WORKDIR}/tmp-mnt-boot || true | 78 | # Copy here a cmdline.txt file generated taking into consideration the partition type |
79 | # of the rootfs | ||
80 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/bootcode.bin :: | ||
81 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/loader.bin :: | ||
82 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/kernel.img :: | ||
88 | 83 | ||
89 | if [ -n ${FATPAYLOAD} ] ; then | 84 | if [ -n ${FATPAYLOAD} ] ; then |
90 | echo "Copying payload into VFAT" | 85 | echo "Copying payload into VFAT" |
91 | for entry in ${FATPAYLOAD} ; do | 86 | for entry in ${FATPAYLOAD} ; do |
92 | # add the || true to stop aborting on vfat issues like not supporting .~lock files | 87 | # add the || true to stop aborting on vfat issues like not supporting .~lock files |
93 | cp -av ${IMAGE_ROOTFS}$entry ${WORKDIR}/tmp-mnt-boot || true | 88 | mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true |
94 | done | 89 | done |
95 | fi | 90 | fi |
96 | 91 | ||
97 | echo "${IMAGE_NAME}-${IMAGEDATESTAMP}" > ${IMAGE_ROOTFS}/etc/image-version-info | 92 | # Add stamp file |
98 | 93 | echo "${IMAGE_NAME}-${IMAGEDATESTAMP}" > ${WORKDIR}/image-version-info | |
99 | cp -v ${IMAGE_ROOTFS}/etc/image-version-info ${WORKDIR}/tmp-mnt-boot || true | 94 | mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}//image-version-info :: |
100 | |||
101 | # Cleanup VFAT mount | ||
102 | echo "Cleaning up VFAT mount" | ||
103 | umount ${WORKDIR}/tmp-mnt-boot | ||
104 | ${LOSETUP} -d ${LOOPDEV_BOOT} || true | ||
105 | |||
106 | # Prepare rootfs parition | ||
107 | echo "Creating rootfs loopback" | ||
108 | ${LOSETUP} ${LOOPDEV_FS} ${SDIMG} -o ${FS_OFFSET} | ||
109 | |||
110 | FS_NUM_INODES=$(echo $FS_SIZE_BLOCKS / 4 | bc) | ||
111 | |||
112 | case "${ROOTFSTYPE}" in | ||
113 | ext3) | ||
114 | genext2fs -z -N $FS_NUM_INODES -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${LOOPDEV_FS} | ||
115 | tune2fs -L ${IMAGE_NAME} -j ${LOOPDEV_FS} | ||
116 | ;; | ||
117 | ext4) | ||
118 | genext2fs -z -N $FS_NUM_INODES -b $FS_SIZE_BLOCKS -d ${IMAGE_ROOTFS} ${LOOPDEV_FS} | ||
119 | tune2fs -L ${IMAGE_NAME} -j -O extents,uninit_bg,dir_index ${LOOPDEV_FS} | ||
120 | ;; | ||
121 | *) | ||
122 | echo "Please set ROOTFSTYPE to something supported" | ||
123 | exit 1 | ||
124 | ;; | ||
125 | esac | ||
126 | |||
127 | ${LOSETUP} -d ${LOOPDEV_FS} || true | ||
128 | 95 | ||
129 | gzip -c ${WORKDIR}/sd.img > ${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}-${IMAGEDATESTAMP}.img.gz | 96 | # Burn Partitions |
130 | rm -f ${WORKDIR}/sd.img | 97 | dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=1M && sync && sync |
98 | dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=${BOOT_SPACE} && sync && sync | ||
131 | } | 99 | } |