blob: a7b9ac82fbacb6e52efe7ddccc3b80098dceabdb [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# <--------->
Andrew Geissler3e34fba2021-03-05 15:21:55 -060016# 4MiB 48MiB SDIMG_ROOTFS
Patrick Williams8b8bc412016-08-17 15:02:23 -050017# <-----------------------> <----------> <---------------------->
18# ------------------------ ------------ ------------------------
19# | IMAGE_ROOTFS_ALIGNMENT | BOOT_SPACE | ROOTFS_SIZE |
20# ------------------------ ------------ ------------------------
21# ^ ^ ^ ^
22# | | | |
Andrew Geissler3e34fba2021-03-05 15:21:55 -060023# 0 4MiB 4MiB + 48MiB 4MiB + 48Mib + SDIMG_ROOTFS
Patrick Williams8b8bc412016-08-17 15:02:23 -050024
25# This image depends on the rootfs image
Patrick Williams213cb262021-08-07 19:21:33 -050026IMAGE_TYPEDEP:rpi-sdimg = "${SDIMG_ROOTFS_TYPE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050027
Patrick Williams8b8bc412016-08-17 15:02:23 -050028# Kernel image name
Patrick Williams213cb262021-08-07 19:21:33 -050029SDIMG_KERNELIMAGE:raspberrypi ?= "kernel.img"
30SDIMG_KERNELIMAGE:raspberrypi2 ?= "kernel7.img"
31SDIMG_KERNELIMAGE:raspberrypi3-64 ?= "kernel8.img"
Patrick Williams8b8bc412016-08-17 15:02:23 -050032
33# Boot partition volume id
Andrew Geissler3e34fba2021-03-05 15:21:55 -060034# Shorten raspberrypi to just rpi to keep it under 11 characters
35# now enforced by mkfs.vfat from dosfstools-4.2
36BOOTDD_VOLUME_ID ?= "${@d.getVar('MACHINE').replace('raspberrypi', 'rpi')}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050037
38# Boot partition size [in KiB] (will be rounded up to IMAGE_ROOTFS_ALIGNMENT)
Andrew Geissler3e34fba2021-03-05 15:21:55 -060039BOOT_SPACE ?= "49152"
Patrick Williams8b8bc412016-08-17 15:02:23 -050040
41# Set alignment to 4MB [in KiB]
42IMAGE_ROOTFS_ALIGNMENT = "4096"
43
44# Use an uncompressed ext3 by default as rootfs
45SDIMG_ROOTFS_TYPE ?= "ext3"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${SDIMG_ROOTFS_TYPE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050047
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080048# For the names of kernel artifacts
49inherit kernel-artifact-names
50
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -050051RPI_SDIMG_EXTRA_DEPENDS ?= ""
52
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053do_image_rpi_sdimg[depends] = " \
Brad Bishopf27bdd52018-05-07 15:42:31 +020054 parted-native:do_populate_sysroot \
55 mtools-native:do_populate_sysroot \
56 dosfstools-native:do_populate_sysroot \
57 virtual/kernel:do_deploy \
Andrew Geissler26e4bea2020-11-30 19:54:03 -060058 rpi-bootfiles:do_deploy \
Brad Bishop26bdd442019-08-16 17:08:17 -040059 ${@bb.utils.contains('MACHINE_FEATURES', 'armstub', 'armstubs:do_deploy', '' ,d)} \
Brad Bishopf27bdd52018-05-07 15:42:31 +020060 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot:do_deploy', '',d)} \
Andrew Geissler062316f2020-05-15 14:19:14 -050061 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot-default-script:do_deploy', '',d)} \
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -050062 ${RPI_SDIMG_EXTRA_DEPENDS} \
Brad Bishopf27bdd52018-05-07 15:42:31 +020063"
Patrick Williams8b8bc412016-08-17 15:02:23 -050064
Brad Bishop316dfdd2018-06-25 12:45:53 -040065do_image_rpi_sdimg[recrdeps] = "do_build"
66
Patrick Williams8b8bc412016-08-17 15:02:23 -050067# SD card image name
Brad Bishop31121382019-12-06 18:34:07 -050068SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}${IMAGE_NAME_SUFFIX}.rpi-sdimg"
Patrick Williams8b8bc412016-08-17 15:02:23 -050069
Patrick Williams8b8bc412016-08-17 15:02:23 -050070# Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS.
71FATPAYLOAD ?= ""
72
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073# SD card vfat partition image name
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080074SDIMG_VFAT_DEPLOY ?= "${RPI_USE_U_BOOT}"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050075SDIMG_VFAT = "${IMAGE_NAME}.vfat"
76SDIMG_LINK_VFAT = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.vfat"
77
Patrick Williams213cb262021-08-07 19:21:33 -050078IMAGE_CMD:rpi-sdimg () {
Patrick Williams8b8bc412016-08-17 15:02:23 -050079
Brad Bishopf27bdd52018-05-07 15:42:31 +020080 # Align partitions
81 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
82 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
83 SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE)
Patrick Williams8b8bc412016-08-17 15:02:23 -050084
Brad Bishopf27bdd52018-05-07 15:42:31 +020085 echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
Patrick Williams8b8bc412016-08-17 15:02:23 -050086
Brad Bishopf27bdd52018-05-07 15:42:31 +020087 # Check if we are building with device tree support
Andrew Geissler9aee5002022-03-30 16:27:02 +000088 DTS="${@make_dtb_boot_files(d)}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050089
Brad Bishopf27bdd52018-05-07 15:42:31 +020090 # Initialize sdcard image file
91 dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
Patrick Williams8b8bc412016-08-17 15:02:23 -050092
Brad Bishopf27bdd52018-05-07 15:42:31 +020093 # Create partition table
94 parted -s ${SDIMG} mklabel msdos
95 # Create boot partition and mark it as bootable
96 parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT})
97 parted -s ${SDIMG} set 1 boot on
98 # Create rootfs partition to the end of disk
99 parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s
100 parted ${SDIMG} print
Patrick Williams8b8bc412016-08-17 15:02:23 -0500101
Brad Bishopf27bdd52018-05-07 15:42:31 +0200102 # Create a vfat image with boot files
103 BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }')
104 rm -f ${WORKDIR}/boot.img
Andrew Geissler1c349142019-02-19 19:49:56 -0600105 mkfs.vfat -F32 -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -0500106 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 -0400107 if [ "${@bb.utils.contains("MACHINE_FEATURES", "armstub", "1", "0", d)}" = "1" ]; then
108 mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/armstubs/${ARMSTUB} ::/ || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/armstubs/${ARMSTUB} into boot.img"
109 fi
Brad Bishopf27bdd52018-05-07 15:42:31 +0200110 if test -n "${DTS}"; then
Andrew Geissler9aee5002022-03-30 16:27:02 +0000111 # Copy board device trees (including overlays)
112 # There is an assumption here - no DTB in other directories than root
113 # and root/overlays. mmd/mcopy are not very flexible tools.
Brad Bishopf27bdd52018-05-07 15:42:31 +0200114 mmd -i ${WORKDIR}/boot.img overlays
Andrew Geissler9aee5002022-03-30 16:27:02 +0000115 for entry in ${DTS} ; do
116 # Split entry at optional ';'
117 if [ $(echo "$entry" | grep -c \;) = "0" ] ; then
118 DEPLOY_FILE="$entry"
119 DEST_FILENAME="$entry"
120 else
121 DEPLOY_FILE="$(echo "$entry" | cut -f1 -d\;)"
122 DEST_FILENAME="$(echo "$entry" | cut -f2- -d\;)"
123 fi
124 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"
Brad Bishopf27bdd52018-05-07 15:42:31 +0200125 done
126 fi
127 if [ "${RPI_USE_U_BOOT}" = "1" ]; then
Brad Bishop26bdd442019-08-16 17:08:17 -0400128 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"
129 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 -0800130 if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
Brad Bishop26bdd442019-08-16 17:08:17 -0400131 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 -0800132 else
Brad Bishop26bdd442019-08-16 17:08:17 -0400133 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 -0800134 fi
Brad Bishopf27bdd52018-05-07 15:42:31 +0200135 else
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800136 if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
Brad Bishop26bdd442019-08-16 17:08:17 -0400137 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 -0800138 else
Brad Bishop26bdd442019-08-16 17:08:17 -0400139 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 -0800140 fi
Brad Bishopf27bdd52018-05-07 15:42:31 +0200141 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500142
Andrew Geissler9d7e0aa2020-08-21 15:56:44 -0500143 # Add files (eg. hypervisor binaries) from the deploy dir
144 if [ -n "${DEPLOYPAYLOAD}" ] ; then
145 echo "Copying deploy file payload into VFAT"
146 for entry in ${DEPLOYPAYLOAD} ; do
147 # Split entry at optional ':' to enable file renaming for the destination
148 if [ $(echo "$entry" | grep -c :) = "0" ] ; then
149 DEPLOY_FILE="$entry"
150 DEST_FILENAME="$entry"
151 else
152 DEPLOY_FILE="$(echo "$entry" | cut -f1 -d:)"
153 DEST_FILENAME="$(echo "$entry" | cut -f2- -d:)"
154 fi
155 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"
156 done
157 fi
158
Brad Bishop15ae2502019-06-18 21:44:24 -0400159 if [ -n "${FATPAYLOAD}" ] ; then
Brad Bishopf27bdd52018-05-07 15:42:31 +0200160 echo "Copying payload into VFAT"
161 for entry in ${FATPAYLOAD} ; do
Brad Bishop26bdd442019-08-16 17:08:17 -0400162 # use bbwarn instead of bbfatal to stop aborting on vfat issues like not supporting .~lock files
163 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 +0200164 done
165 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500166
Brad Bishopf27bdd52018-05-07 15:42:31 +0200167 # Add stamp file
168 echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info
Brad Bishop26bdd442019-08-16 17:08:17 -0400169 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 -0500170
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800171 # Deploy vfat partition
172 if [ "${SDIMG_VFAT_DEPLOY}" = "1" ]; then
Brad Bishopf27bdd52018-05-07 15:42:31 +0200173 cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT}
174 ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT}
175 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500176
Brad Bishopf27bdd52018-05-07 15:42:31 +0200177 # Burn Partitions
178 dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
179 # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat
180 if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz"
181 then
182 xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
183 else
184 dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
185 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500186}
187
188ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
189
190rpi_generate_sysctl_config() {
Brad Bishopf27bdd52018-05-07 15:42:31 +0200191 # systemd sysctl config
192 test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \
193 echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf
Patrick Williams8b8bc412016-08-17 15:02:23 -0500194
Brad Bishopf27bdd52018-05-07 15:42:31 +0200195 # sysv sysctl config
196 IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf"
197 test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \
198 sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF}
199 echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF}
Patrick Williams8b8bc412016-08-17 15:02:23 -0500200}