blob: 8197978a3a700cd39ba6558060f92060eb428895 [file] [log] [blame]
Patrick Williams8b8bc412016-08-17 15:02:23 -05001inherit image_types
Patrick Williams8b8bc412016-08-17 15:02:23 -05002
3#
Brad Bishop316dfdd2018-06-25 12:45:53 -04004# Create an image that can be written onto a SD card using dd.
Patrick Williams8b8bc412016-08-17 15:02:23 -05005#
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
26IMAGE_TYPEDEP_rpi-sdimg = "${SDIMG_ROOTFS_TYPE}"
27
28# Set kernel and boot loader
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -050029IMAGE_BOOTLOADER ?= "bootfiles"
Patrick Williams8b8bc412016-08-17 15:02:23 -050030
Patrick Williams8b8bc412016-08-17 15:02:23 -050031# Kernel image name
32SDIMG_KERNELIMAGE_raspberrypi ?= "kernel.img"
33SDIMG_KERNELIMAGE_raspberrypi2 ?= "kernel7.img"
Brad Bishop3a5fbf52017-12-04 02:17:07 -050034SDIMG_KERNELIMAGE_raspberrypi3-64 ?= "kernel8.img"
Patrick Williams8b8bc412016-08-17 15:02:23 -050035
36# Boot partition volume id
37BOOTDD_VOLUME_ID ?= "${MACHINE}"
38
39# Boot partition size [in KiB] (will be rounded up to IMAGE_ROOTFS_ALIGNMENT)
40BOOT_SPACE ?= "40960"
41
42# Set alignment to 4MB [in KiB]
43IMAGE_ROOTFS_ALIGNMENT = "4096"
44
45# Use an uncompressed ext3 by default as rootfs
46SDIMG_ROOTFS_TYPE ?= "ext3"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${SDIMG_ROOTFS_TYPE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050048
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080049# For the names of kernel artifacts
50inherit kernel-artifact-names
51
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -050052RPI_SDIMG_EXTRA_DEPENDS ?= ""
53
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054do_image_rpi_sdimg[depends] = " \
Brad Bishopf27bdd52018-05-07 15:42:31 +020055 parted-native:do_populate_sysroot \
56 mtools-native:do_populate_sysroot \
57 dosfstools-native:do_populate_sysroot \
58 virtual/kernel:do_deploy \
59 ${IMAGE_BOOTLOADER}:do_deploy \
Brad Bishopf76da502019-10-10 08:51:21 -040060 rpi-config:do_deploy \
Brad Bishop26bdd442019-08-16 17:08:17 -040061 ${@bb.utils.contains('MACHINE_FEATURES', 'armstub', 'armstubs:do_deploy', '' ,d)} \
Brad Bishopf27bdd52018-05-07 15:42:31 +020062 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot:do_deploy', '',d)} \
Andrew Geissler062316f2020-05-15 14:19:14 -050063 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot-default-script:do_deploy', '',d)} \
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -050064 ${RPI_SDIMG_EXTRA_DEPENDS} \
Brad Bishopf27bdd52018-05-07 15:42:31 +020065"
Patrick Williams8b8bc412016-08-17 15:02:23 -050066
Brad Bishop316dfdd2018-06-25 12:45:53 -040067do_image_rpi_sdimg[recrdeps] = "do_build"
68
Patrick Williams8b8bc412016-08-17 15:02:23 -050069# SD card image name
Brad Bishop31121382019-12-06 18:34:07 -050070SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.rpi-sdimg"
Patrick Williams8b8bc412016-08-17 15:02:23 -050071
Patrick Williams8b8bc412016-08-17 15:02:23 -050072# Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS.
73FATPAYLOAD ?= ""
74
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075# SD card vfat partition image name
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080076SDIMG_VFAT_DEPLOY ?= "${RPI_USE_U_BOOT}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077SDIMG_VFAT = "${IMAGE_NAME}.vfat"
78SDIMG_LINK_VFAT = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.vfat"
79
Brad Bishopd7bf8c12018-02-25 22:55:05 -050080def split_overlays(d, out, ver=None):
81 dts = d.getVar("KERNEL_DEVICETREE")
Brad Bishopf27bdd52018-05-07 15:42:31 +020082 # 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 Bishopd7bf8c12018-02-25 22:55:05 -050083 if out:
84 overlays = oe.utils.str_filter_out('\S+\-overlay\.dtb$', dts, d)
85 overlays = oe.utils.str_filter_out('\S+\.dtbo$', overlays, d)
86 else:
87 overlays = oe.utils.str_filter('\S+\-overlay\.dtb$', dts, d) + \
88 " " + oe.utils.str_filter('\S+\.dtbo$', dts, d)
89
90 return overlays
91
Patrick Williams8b8bc412016-08-17 15:02:23 -050092IMAGE_CMD_rpi-sdimg () {
93
Brad Bishopf27bdd52018-05-07 15:42:31 +020094 # Align partitions
95 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
96 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
97 SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE)
Patrick Williams8b8bc412016-08-17 15:02:23 -050098
Brad Bishopf27bdd52018-05-07 15:42:31 +020099 echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
Patrick Williams8b8bc412016-08-17 15:02:23 -0500100
Brad Bishopf27bdd52018-05-07 15:42:31 +0200101 # Check if we are building with device tree support
102 DTS="${KERNEL_DEVICETREE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -0500103
Brad Bishopf27bdd52018-05-07 15:42:31 +0200104 # Initialize sdcard image file
105 dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
Patrick Williams8b8bc412016-08-17 15:02:23 -0500106
Brad Bishopf27bdd52018-05-07 15:42:31 +0200107 # Create partition table
108 parted -s ${SDIMG} mklabel msdos
109 # Create boot partition and mark it as bootable
110 parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT})
111 parted -s ${SDIMG} set 1 boot on
112 # Create rootfs partition to the end of disk
113 parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s
114 parted ${SDIMG} print
Patrick Williams8b8bc412016-08-17 15:02:23 -0500115
Brad Bishopf27bdd52018-05-07 15:42:31 +0200116 # Create a vfat image with boot files
117 BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }')
118 rm -f ${WORKDIR}/boot.img
Andrew Geissler1c349142019-02-19 19:49:56 -0600119 mkfs.vfat -F32 -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -0500120 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${BOOTFILES_DIR_NAME}/* ::/ || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/${BOOTFILES_DIR_NAME}/* into boot.img"
Brad Bishop26bdd442019-08-16 17:08:17 -0400121 if [ "${@bb.utils.contains("MACHINE_FEATURES", "armstub", "1", "0", d)}" = "1" ]; then
122 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/armstubs/${ARMSTUB} ::/ || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/armstubs/${ARMSTUB} into boot.img"
123 fi
Brad Bishopf27bdd52018-05-07 15:42:31 +0200124 if test -n "${DTS}"; then
125 # Copy board device trees to root folder
126 for dtbf in ${@split_overlays(d, True)}; do
127 dtb=`basename $dtbf`
Brad Bishop26bdd442019-08-16 17:08:17 -0400128 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/$dtb ::$dtb || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/$dtb into boot.img"
Brad Bishopf27bdd52018-05-07 15:42:31 +0200129 done
Patrick Williams8b8bc412016-08-17 15:02:23 -0500130
Brad Bishopf27bdd52018-05-07 15:42:31 +0200131 # Copy device tree overlays to dedicated folder
132 mmd -i ${WORKDIR}/boot.img overlays
133 for dtbf in ${@split_overlays(d, False)}; do
134 dtb=`basename $dtbf`
Brad Bishop26bdd442019-08-16 17:08:17 -0400135 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/$dtb ::overlays/$dtb || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/$dtb into boot.img"
Brad Bishopf27bdd52018-05-07 15:42:31 +0200136 done
137 fi
138 if [ "${RPI_USE_U_BOOT}" = "1" ]; then
Brad Bishop26bdd442019-08-16 17:08:17 -0400139 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE} || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/u-boot.bin into boot.img"
140 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/boot.scr ::boot.scr || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/boot.scr into boot.img"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800141 if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
Brad Bishop26bdd442019-08-16 17:08:17 -0400142 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin ::${KERNEL_IMAGETYPE} || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin into boot.img"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800143 else
Brad Bishop26bdd442019-08-16 17:08:17 -0400144 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} ::${KERNEL_IMAGETYPE} || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} into boot.img"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800145 fi
Brad Bishopf27bdd52018-05-07 15:42:31 +0200146 else
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800147 if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
Brad Bishop26bdd442019-08-16 17:08:17 -0400148 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin ::${SDIMG_KERNELIMAGE} || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${INITRAMFS_LINK_NAME}.bin into boot.img"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800149 else
Brad Bishop26bdd442019-08-16 17:08:17 -0400150 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} ::${SDIMG_KERNELIMAGE} || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE} into boot.img"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800151 fi
Brad Bishopf27bdd52018-05-07 15:42:31 +0200152 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500153
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -0500154 # Add files (eg. hypervisor binaries) from the deploy dir
155 if [ -n "${DEPLOYPAYLOAD}" ] ; then
156 echo "Copying deploy file payload into VFAT"
157 for entry in ${DEPLOYPAYLOAD} ; do
158 # Split entry at optional ':' to enable file renaming for the destination
159 if [ $(echo "$entry" | grep -c :) = "0" ] ; then
160 DEPLOY_FILE="$entry"
161 DEST_FILENAME="$entry"
162 else
163 DEPLOY_FILE="$(echo "$entry" | cut -f1 -d:)"
164 DEST_FILENAME="$(echo "$entry" | cut -f2- -d:)"
165 fi
166 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${DEPLOY_FILE} ::${DEST_FILENAME} || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/${DEPLOY_FILE} into boot.img"
167 done
168 fi
169
Brad Bishop15ae2502019-06-18 21:44:24 -0400170 if [ -n "${FATPAYLOAD}" ] ; then
Brad Bishopf27bdd52018-05-07 15:42:31 +0200171 echo "Copying payload into VFAT"
172 for entry in ${FATPAYLOAD} ; do
Brad Bishop26bdd442019-08-16 17:08:17 -0400173 # use bbwarn instead of bbfatal to stop aborting on vfat issues like not supporting .~lock files
174 mcopy -v -i ${WORKDIR}/boot.img -s ${IMAGE_ROOTFS}$entry :: || bbwarn "mcopy cannot copy ${IMAGE_ROOTFS}$entry into boot.img"
Brad Bishopf27bdd52018-05-07 15:42:31 +0200175 done
176 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500177
Brad Bishopf27bdd52018-05-07 15:42:31 +0200178 # Add stamp file
179 echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info
Brad Bishop26bdd442019-08-16 17:08:17 -0400180 mcopy -v -i ${WORKDIR}/boot.img ${WORKDIR}/image-version-info :: || bbfatal "mcopy cannot copy ${WORKDIR}/image-version-info into boot.img"
Patrick Williams8b8bc412016-08-17 15:02:23 -0500181
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800182 # Deploy vfat partition
183 if [ "${SDIMG_VFAT_DEPLOY}" = "1" ]; then
Brad Bishopf27bdd52018-05-07 15:42:31 +0200184 cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT}
185 ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT}
186 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500187
Brad Bishopf27bdd52018-05-07 15:42:31 +0200188 # Burn Partitions
189 dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
190 # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat
191 if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz"
192 then
193 xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
194 else
195 dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
196 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500197}
198
199ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
200
201rpi_generate_sysctl_config() {
Brad Bishopf27bdd52018-05-07 15:42:31 +0200202 # systemd sysctl config
203 test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \
204 echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf
Patrick Williams8b8bc412016-08-17 15:02:23 -0500205
Brad Bishopf27bdd52018-05-07 15:42:31 +0200206 # sysv sysctl config
207 IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf"
208 test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \
209 sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF}
210 echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF}
Patrick Williams8b8bc412016-08-17 15:02:23 -0500211}