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