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