Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 1 | inherit image_types |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 2 | |
| 3 | # |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 4 | # Create an image that can be written onto a SD card using dd. |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 5 | # |
| 6 | # The disk layout used is: |
| 7 | # |
| 8 | # 0 -> IMAGE_ROOTFS_ALIGNMENT - reserved for other data |
| 9 | # IMAGE_ROOTFS_ALIGNMENT -> BOOT_SPACE - bootloader and kernel |
| 10 | # BOOT_SPACE -> SDIMG_SIZE - rootfs |
| 11 | # |
| 12 | |
| 13 | # Default Free space = 1.3x |
| 14 | # Use IMAGE_OVERHEAD_FACTOR to add more space |
| 15 | # <---------> |
| 16 | # 4MiB 40MiB SDIMG_ROOTFS |
| 17 | # <-----------------------> <----------> <----------------------> |
| 18 | # ------------------------ ------------ ------------------------ |
| 19 | # | IMAGE_ROOTFS_ALIGNMENT | BOOT_SPACE | ROOTFS_SIZE | |
| 20 | # ------------------------ ------------ ------------------------ |
| 21 | # ^ ^ ^ ^ |
| 22 | # | | | | |
| 23 | # 0 4MiB 4MiB + 40MiB 4MiB + 40Mib + SDIMG_ROOTFS |
| 24 | |
| 25 | # This image depends on the rootfs image |
| 26 | IMAGE_TYPEDEP_rpi-sdimg = "${SDIMG_ROOTFS_TYPE}" |
| 27 | |
| 28 | # Set kernel and boot loader |
| 29 | IMAGE_BOOTLOADER ?= "bcm2835-bootfiles" |
| 30 | |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 31 | # Kernel image name |
| 32 | SDIMG_KERNELIMAGE_raspberrypi ?= "kernel.img" |
| 33 | SDIMG_KERNELIMAGE_raspberrypi2 ?= "kernel7.img" |
Brad Bishop | 3a5fbf5 | 2017-12-04 02:17:07 -0500 | [diff] [blame] | 34 | SDIMG_KERNELIMAGE_raspberrypi3-64 ?= "kernel8.img" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 35 | |
| 36 | # Boot partition volume id |
| 37 | BOOTDD_VOLUME_ID ?= "${MACHINE}" |
| 38 | |
| 39 | # Boot partition size [in KiB] (will be rounded up to IMAGE_ROOTFS_ALIGNMENT) |
| 40 | BOOT_SPACE ?= "40960" |
| 41 | |
| 42 | # Set alignment to 4MB [in KiB] |
| 43 | IMAGE_ROOTFS_ALIGNMENT = "4096" |
| 44 | |
| 45 | # Use an uncompressed ext3 by default as rootfs |
| 46 | SDIMG_ROOTFS_TYPE ?= "ext3" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 47 | SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${SDIMG_ROOTFS_TYPE}" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 48 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 49 | # For the names of kernel artifacts |
| 50 | inherit kernel-artifact-names |
| 51 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 52 | do_image_rpi_sdimg[depends] = " \ |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 53 | parted-native:do_populate_sysroot \ |
| 54 | mtools-native:do_populate_sysroot \ |
| 55 | dosfstools-native:do_populate_sysroot \ |
| 56 | virtual/kernel:do_deploy \ |
| 57 | ${IMAGE_BOOTLOADER}:do_deploy \ |
| 58 | ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot:do_deploy', '',d)} \ |
| 59 | ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'rpi-u-boot-scr:do_deploy', '',d)} \ |
| 60 | " |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 61 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 62 | do_image_rpi_sdimg[recrdeps] = "do_build" |
| 63 | |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 64 | # SD card image name |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 65 | SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.rpi-sdimg" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 66 | |
| 67 | # Compression method to apply to SDIMG after it has been created. Supported |
| 68 | # compression formats are "gzip", "bzip2" or "xz". The original .rpi-sdimg file |
| 69 | # is kept and a new compressed file is created if one of these compression |
| 70 | # formats is chosen. If SDIMG_COMPRESSION is set to any other value it is |
| 71 | # silently ignored. |
| 72 | #SDIMG_COMPRESSION ?= "" |
| 73 | |
| 74 | # Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS. |
| 75 | FATPAYLOAD ?= "" |
| 76 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | # SD card vfat partition image name |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 78 | SDIMG_VFAT_DEPLOY ?= "${RPI_USE_U_BOOT}" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 79 | SDIMG_VFAT = "${IMAGE_NAME}.vfat" |
| 80 | SDIMG_LINK_VFAT = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.vfat" |
| 81 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 82 | def split_overlays(d, out, ver=None): |
| 83 | dts = d.getVar("KERNEL_DEVICETREE") |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 84 | # Device Tree Overlays are assumed to be suffixed by '-overlay.dtb' (4.1.x) or by '.dtbo' (4.4.9+) string and will be put in a dedicated folder |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 85 | if out: |
| 86 | overlays = oe.utils.str_filter_out('\S+\-overlay\.dtb$', dts, d) |
| 87 | overlays = oe.utils.str_filter_out('\S+\.dtbo$', overlays, d) |
| 88 | else: |
| 89 | overlays = oe.utils.str_filter('\S+\-overlay\.dtb$', dts, d) + \ |
| 90 | " " + oe.utils.str_filter('\S+\.dtbo$', dts, d) |
| 91 | |
| 92 | return overlays |
| 93 | |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 94 | IMAGE_CMD_rpi-sdimg () { |
| 95 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 96 | # Align partitions |
| 97 | BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1) |
| 98 | BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT}) |
| 99 | SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE) |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 100 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 101 | echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 102 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 103 | # Check if we are building with device tree support |
| 104 | DTS="${KERNEL_DEVICETREE}" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 105 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 106 | # Initialize sdcard image file |
| 107 | dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE} |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 108 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 109 | # Create partition table |
| 110 | parted -s ${SDIMG} mklabel msdos |
| 111 | # Create boot partition and mark it as bootable |
| 112 | parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) |
| 113 | parted -s ${SDIMG} set 1 boot on |
| 114 | # Create rootfs partition to the end of disk |
| 115 | parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s |
| 116 | parted ${SDIMG} print |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 117 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 118 | # Create a vfat image with boot files |
| 119 | BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }') |
| 120 | rm -f ${WORKDIR}/boot.img |
| 121 | mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS |
| 122 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ::/ |
| 123 | if test -n "${DTS}"; then |
| 124 | # Copy board device trees to root folder |
| 125 | for dtbf in ${@split_overlays(d, True)}; do |
| 126 | dtb=`basename $dtbf` |
| 127 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/$dtb ::$dtb |
| 128 | done |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 129 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 130 | # Copy device tree overlays to dedicated folder |
| 131 | mmd -i ${WORKDIR}/boot.img overlays |
| 132 | for dtbf in ${@split_overlays(d, False)}; do |
| 133 | dtb=`basename $dtbf` |
| 134 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/$dtb ::overlays/$dtb |
| 135 | done |
| 136 | fi |
| 137 | if [ "${RPI_USE_U_BOOT}" = "1" ]; then |
| 138 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE} |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 139 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/boot.scr ::boot.scr |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 140 | if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then |
| 141 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin ::${KERNEL_IMAGETYPE} |
| 142 | else |
| 143 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} ::${KERNEL_IMAGETYPE} |
| 144 | fi |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 145 | else |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 146 | if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then |
| 147 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin ::${SDIMG_KERNELIMAGE} |
| 148 | else |
| 149 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} ::${SDIMG_KERNELIMAGE} |
| 150 | fi |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 151 | fi |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 152 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 153 | if [ -n ${FATPAYLOAD} ] ; then |
| 154 | echo "Copying payload into VFAT" |
| 155 | for entry in ${FATPAYLOAD} ; do |
| 156 | # add the || true to stop aborting on vfat issues like not supporting .~lock files |
| 157 | mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true |
| 158 | done |
| 159 | fi |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 160 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 161 | # Add stamp file |
| 162 | echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info |
| 163 | mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}/image-version-info :: |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 164 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 165 | # Deploy vfat partition |
| 166 | if [ "${SDIMG_VFAT_DEPLOY}" = "1" ]; then |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 167 | cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT} |
| 168 | ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT} |
| 169 | fi |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 170 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 171 | # Burn Partitions |
| 172 | dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) |
| 173 | # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat |
| 174 | if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz" |
| 175 | then |
| 176 | xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) |
| 177 | else |
| 178 | dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) |
| 179 | fi |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 180 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 181 | # Optionally apply compression |
| 182 | case "${SDIMG_COMPRESSION}" in |
| 183 | "gzip") |
| 184 | gzip -k9 "${SDIMG}" |
| 185 | ;; |
| 186 | "bzip2") |
| 187 | bzip2 -k9 "${SDIMG}" |
| 188 | ;; |
| 189 | "xz") |
| 190 | xz -k "${SDIMG}" |
| 191 | ;; |
| 192 | esac |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; " |
| 196 | |
| 197 | rpi_generate_sysctl_config() { |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 198 | # systemd sysctl config |
| 199 | test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \ |
| 200 | echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 201 | |
Brad Bishop | f27bdd5 | 2018-05-07 15:42:31 +0200 | [diff] [blame] | 202 | # sysv sysctl config |
| 203 | IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf" |
| 204 | test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \ |
| 205 | sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF} |
| 206 | echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF} |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 207 | } |