blob: e7c34c63fba01eb14a9eb700aefe52580596f9a5 [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
29IMAGE_BOOTLOADER ?= "bcm2835-bootfiles"
30
31# Set initramfs extension
32KERNEL_INITRAMFS ?= ""
33
34# Kernel image name
35SDIMG_KERNELIMAGE_raspberrypi ?= "kernel.img"
36SDIMG_KERNELIMAGE_raspberrypi2 ?= "kernel7.img"
Brad Bishop3a5fbf52017-12-04 02:17:07 -050037SDIMG_KERNELIMAGE_raspberrypi3-64 ?= "kernel8.img"
Patrick Williams8b8bc412016-08-17 15:02:23 -050038
39# Boot partition volume id
40BOOTDD_VOLUME_ID ?= "${MACHINE}"
41
42# Boot partition size [in KiB] (will be rounded up to IMAGE_ROOTFS_ALIGNMENT)
43BOOT_SPACE ?= "40960"
44
45# Set alignment to 4MB [in KiB]
46IMAGE_ROOTFS_ALIGNMENT = "4096"
47
48# Use an uncompressed ext3 by default as rootfs
49SDIMG_ROOTFS_TYPE ?= "ext3"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${SDIMG_ROOTFS_TYPE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050051
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052do_image_rpi_sdimg[depends] = " \
Brad Bishopf27bdd52018-05-07 15:42:31 +020053 parted-native:do_populate_sysroot \
54 mtools-native:do_populate_sysroot \
55 dosfstools-native:do_populate_sysroot \
56 virtual/kernel:do_deploy \
57 ${IMAGE_BOOTLOADER}:do_deploy \
58 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot:do_deploy', '',d)} \
59 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'rpi-u-boot-scr:do_deploy', '',d)} \
60"
Patrick Williams8b8bc412016-08-17 15:02:23 -050061
Brad Bishop316dfdd2018-06-25 12:45:53 -040062do_image_rpi_sdimg[recrdeps] = "do_build"
63
Patrick Williams8b8bc412016-08-17 15:02:23 -050064# SD card image name
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -060065SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.rpi-sdimg"
Patrick Williams8b8bc412016-08-17 15:02:23 -050066
67# Compression method to apply to SDIMG after it has been created. Supported
68# compression formats are "gzip", "bzip2" or "xz". The original .rpi-sdimg file
69# is kept and a new compressed file is created if one of these compression
70# formats is chosen. If SDIMG_COMPRESSION is set to any other value it is
71# silently ignored.
72#SDIMG_COMPRESSION ?= ""
73
74# Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS.
75FATPAYLOAD ?= ""
76
Brad Bishop6e60e8b2018-02-01 10:27:11 -050077# SD card vfat partition image name
78SDIMG_VFAT = "${IMAGE_NAME}.vfat"
79SDIMG_LINK_VFAT = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.vfat"
80
Brad Bishopd7bf8c12018-02-25 22:55:05 -050081def split_overlays(d, out, ver=None):
82 dts = d.getVar("KERNEL_DEVICETREE")
Brad Bishopf27bdd52018-05-07 15:42:31 +020083 # 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 -050084 if out:
85 overlays = oe.utils.str_filter_out('\S+\-overlay\.dtb$', dts, d)
86 overlays = oe.utils.str_filter_out('\S+\.dtbo$', overlays, d)
87 else:
88 overlays = oe.utils.str_filter('\S+\-overlay\.dtb$', dts, d) + \
89 " " + oe.utils.str_filter('\S+\.dtbo$', dts, d)
90
91 return overlays
92
Patrick Williams8b8bc412016-08-17 15:02:23 -050093IMAGE_CMD_rpi-sdimg () {
94
Brad Bishopf27bdd52018-05-07 15:42:31 +020095 # Align partitions
96 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
97 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
98 SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE)
Patrick Williams8b8bc412016-08-17 15:02:23 -050099
Brad Bishopf27bdd52018-05-07 15:42:31 +0200100 echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
Patrick Williams8b8bc412016-08-17 15:02:23 -0500101
Brad Bishopf27bdd52018-05-07 15:42:31 +0200102 # Check if we are building with device tree support
103 DTS="${KERNEL_DEVICETREE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -0500104
Brad Bishopf27bdd52018-05-07 15:42:31 +0200105 # Initialize sdcard image file
106 dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
Patrick Williams8b8bc412016-08-17 15:02:23 -0500107
Brad Bishopf27bdd52018-05-07 15:42:31 +0200108 # Create partition table
109 parted -s ${SDIMG} mklabel msdos
110 # Create boot partition and mark it as bootable
111 parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT})
112 parted -s ${SDIMG} set 1 boot on
113 # Create rootfs partition to the end of disk
114 parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s
115 parted ${SDIMG} print
Patrick Williams8b8bc412016-08-17 15:02:23 -0500116
Brad Bishopf27bdd52018-05-07 15:42:31 +0200117 # Create a vfat image with boot files
118 BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }')
119 rm -f ${WORKDIR}/boot.img
120 mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
121 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ::/
122 if test -n "${DTS}"; then
123 # Copy board device trees to root folder
124 for dtbf in ${@split_overlays(d, True)}; do
125 dtb=`basename $dtbf`
126 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/$dtb ::$dtb
127 done
Patrick Williams8b8bc412016-08-17 15:02:23 -0500128
Brad Bishopf27bdd52018-05-07 15:42:31 +0200129 # Copy device tree overlays to dedicated folder
130 mmd -i ${WORKDIR}/boot.img overlays
131 for dtbf in ${@split_overlays(d, False)}; do
132 dtb=`basename $dtbf`
133 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/$dtb ::overlays/$dtb
134 done
135 fi
136 if [ "${RPI_USE_U_BOOT}" = "1" ]; then
137 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE}
138 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${KERNEL_IMAGETYPE}
139 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/boot.scr ::boot.scr
140 else
141 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${SDIMG_KERNELIMAGE}
142 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500143
Brad Bishopf27bdd52018-05-07 15:42:31 +0200144 if [ -n ${FATPAYLOAD} ] ; then
145 echo "Copying payload into VFAT"
146 for entry in ${FATPAYLOAD} ; do
147 # add the || true to stop aborting on vfat issues like not supporting .~lock files
148 mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true
149 done
150 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500151
Brad Bishopf27bdd52018-05-07 15:42:31 +0200152 # Add stamp file
153 echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info
154 mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}/image-version-info ::
Patrick Williams8b8bc412016-08-17 15:02:23 -0500155
Brad Bishopf27bdd52018-05-07 15:42:31 +0200156 # Deploy vfat partition (for u-boot case only)
157 if [ "${RPI_USE_U_BOOT}" = "1" ]; then
158 cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT}
159 ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT}
160 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500161
Brad Bishopf27bdd52018-05-07 15:42:31 +0200162 # Burn Partitions
163 dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
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)
168 else
169 dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
170 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500171
Brad Bishopf27bdd52018-05-07 15:42:31 +0200172 # 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
Patrick Williams8b8bc412016-08-17 15:02:23 -0500184}
185
186ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
187
188rpi_generate_sysctl_config() {
Brad Bishopf27bdd52018-05-07 15:42:31 +0200189 # 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
Patrick Williams8b8bc412016-08-17 15:02:23 -0500192
Brad Bishopf27bdd52018-05-07 15:42:31 +0200193 # 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}
Patrick Williams8b8bc412016-08-17 15:02:23 -0500198}