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" |
| 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" |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 50 | SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.${SDIMG_ROOTFS_TYPE}" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 51 | |
| 52 | IMAGE_DEPENDS_rpi-sdimg = " \ |
| 53 | parted-native \ |
| 54 | mtools-native \ |
| 55 | dosfstools-native \ |
| 56 | virtual/kernel:do_deploy \ |
| 57 | ${IMAGE_BOOTLOADER} \ |
| 58 | ${@bb.utils.contains('KERNEL_IMAGETYPE', 'uImage', 'u-boot', '',d)} \ |
| 59 | " |
| 60 | |
| 61 | # SD card image name |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 62 | SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.rpi-sdimg" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 63 | |
| 64 | # Compression method to apply to SDIMG after it has been created. Supported |
| 65 | # compression formats are "gzip", "bzip2" or "xz". The original .rpi-sdimg file |
| 66 | # is kept and a new compressed file is created if one of these compression |
| 67 | # formats is chosen. If SDIMG_COMPRESSION is set to any other value it is |
| 68 | # silently ignored. |
| 69 | #SDIMG_COMPRESSION ?= "" |
| 70 | |
| 71 | # Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS. |
| 72 | FATPAYLOAD ?= "" |
| 73 | |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 74 | IMAGE_CMD_rpi-sdimg () { |
| 75 | |
| 76 | # Align partitions |
| 77 | BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1) |
| 78 | BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT}) |
| 79 | SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE) |
| 80 | |
| 81 | echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB" |
| 82 | |
| 83 | # Check if we are building with device tree support |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 84 | DTS="${@get_dts(d)}" |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 85 | |
| 86 | # Initialize sdcard image file |
| 87 | dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE} |
| 88 | |
| 89 | # Create partition table |
| 90 | parted -s ${SDIMG} mklabel msdos |
| 91 | # Create boot partition and mark it as bootable |
| 92 | parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) |
| 93 | parted -s ${SDIMG} set 1 boot on |
| 94 | # Create rootfs partition to the end of disk |
| 95 | parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s |
| 96 | parted ${SDIMG} print |
| 97 | |
| 98 | # Create a vfat image with boot files |
| 99 | BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }') |
| 100 | rm -f ${WORKDIR}/boot.img |
| 101 | mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS |
| 102 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ::/ |
| 103 | if test -n "${DTS}"; then |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 104 | # 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] | 105 | DT_OVERLAYS="${@split_overlays(d, 0)}" |
| 106 | DT_ROOT="${@split_overlays(d, 1)}" |
| 107 | |
| 108 | # Copy board device trees to root folder |
| 109 | for DTB in ${DT_ROOT}; do |
| 110 | DTB_BASE_NAME=`basename ${DTB} .dtb` |
| 111 | |
| 112 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${DTB_BASE_NAME}.dtb ::${DTB_BASE_NAME}.dtb |
| 113 | done |
| 114 | |
| 115 | # Copy device tree overlays to dedicated folder |
| 116 | mmd -i ${WORKDIR}/boot.img overlays |
| 117 | for DTB in ${DT_OVERLAYS}; do |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 118 | DTB_EXT=${DTB##*.} |
| 119 | DTB_BASE_NAME=`basename ${DTB} ."${DTB_EXT}"` |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 120 | |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 121 | 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] | 122 | done |
| 123 | fi |
| 124 | case "${KERNEL_IMAGETYPE}" in |
| 125 | "uImage") |
| 126 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE} |
| 127 | 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] | 128 | 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] | 129 | ;; |
| 130 | *) |
| 131 | mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${SDIMG_KERNELIMAGE} |
| 132 | ;; |
| 133 | esac |
| 134 | |
| 135 | if [ -n ${FATPAYLOAD} ] ; then |
| 136 | echo "Copying payload into VFAT" |
| 137 | for entry in ${FATPAYLOAD} ; do |
| 138 | # add the || true to stop aborting on vfat issues like not supporting .~lock files |
| 139 | mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true |
| 140 | done |
| 141 | fi |
| 142 | |
| 143 | # Add stamp file |
Patrick Williams | b2e6a9b | 2017-02-23 20:31:26 -0600 | [diff] [blame] | 144 | echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info |
| 145 | mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}/image-version-info :: |
Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 146 | |
| 147 | # Burn Partitions |
| 148 | dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync |
| 149 | # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat |
| 150 | if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz" |
| 151 | then |
| 152 | xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync |
| 153 | else |
| 154 | dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync |
| 155 | fi |
| 156 | |
| 157 | # Optionally apply compression |
| 158 | case "${SDIMG_COMPRESSION}" in |
| 159 | "gzip") |
| 160 | gzip -k9 "${SDIMG}" |
| 161 | ;; |
| 162 | "bzip2") |
| 163 | bzip2 -k9 "${SDIMG}" |
| 164 | ;; |
| 165 | "xz") |
| 166 | xz -k "${SDIMG}" |
| 167 | ;; |
| 168 | esac |
| 169 | } |
| 170 | |
| 171 | ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; " |
| 172 | |
| 173 | rpi_generate_sysctl_config() { |
| 174 | # systemd sysctl config |
| 175 | test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \ |
| 176 | echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf |
| 177 | |
| 178 | # sysv sysctl config |
| 179 | IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf" |
| 180 | test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \ |
| 181 | sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF} |
| 182 | echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF} |
| 183 | } |