blob: f03595c2c52fa4a9b3d27334390d9dcf76086002 [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] = " \
53 parted-native:do_populate_sysroot \
54 mtools-native:do_populate_sysroot \
55 dosfstools-native:do_populate_sysroot \
Patrick Williams8b8bc412016-08-17 15:02:23 -050056 virtual/kernel:do_deploy \
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057 ${IMAGE_BOOTLOADER}:do_deploy \
58 ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot:do_deploy', '',d)} \
Patrick Williams8b8bc412016-08-17 15:02:23 -050059 "
60
Brad Bishop316dfdd2018-06-25 12:45:53 -040061do_image_rpi_sdimg[recrdeps] = "do_build"
62
Patrick Williams8b8bc412016-08-17 15:02:23 -050063# SD card image name
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -060064SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.rpi-sdimg"
Patrick Williams8b8bc412016-08-17 15:02:23 -050065
66# Compression method to apply to SDIMG after it has been created. Supported
67# compression formats are "gzip", "bzip2" or "xz". The original .rpi-sdimg file
68# is kept and a new compressed file is created if one of these compression
69# formats is chosen. If SDIMG_COMPRESSION is set to any other value it is
70# silently ignored.
71#SDIMG_COMPRESSION ?= ""
72
73# Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS.
74FATPAYLOAD ?= ""
75
Brad Bishop6e60e8b2018-02-01 10:27:11 -050076# SD card vfat partition image name
77SDIMG_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")
82 if out:
83 overlays = oe.utils.str_filter_out('\S+\-overlay\.dtb$', dts, d)
84 overlays = oe.utils.str_filter_out('\S+\.dtbo$', overlays, d)
85 else:
86 overlays = oe.utils.str_filter('\S+\-overlay\.dtb$', dts, d) + \
87 " " + oe.utils.str_filter('\S+\.dtbo$', dts, d)
88
89 return overlays
90
Patrick Williams8b8bc412016-08-17 15:02:23 -050091IMAGE_CMD_rpi-sdimg () {
92
93 # Align partitions
94 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
95 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
96 SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE)
97
98 echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
99
100 # Check if we are building with device tree support
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101 DTS="${KERNEL_DEVICETREE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -0500102
103 # Initialize sdcard image file
104 dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
105
106 # Create partition table
107 parted -s ${SDIMG} mklabel msdos
108 # Create boot partition and mark it as bootable
109 parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT})
110 parted -s ${SDIMG} set 1 boot on
111 # Create rootfs partition to the end of disk
112 parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s
113 parted ${SDIMG} print
114
115 # Create a vfat image with boot files
116 BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }')
117 rm -f ${WORKDIR}/boot.img
118 mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
119 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ::/
120 if test -n "${DTS}"; then
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600121 # 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 Williams8b8bc412016-08-17 15:02:23 -0500122 DT_OVERLAYS="${@split_overlays(d, 0)}"
123 DT_ROOT="${@split_overlays(d, 1)}"
124
125 # Copy board device trees to root folder
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126 for DTB in $DT_ROOT; do
Patrick Williams8b8bc412016-08-17 15:02:23 -0500127 DTB_BASE_NAME=`basename ${DTB} .dtb`
128
129 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${DTB_BASE_NAME}.dtb ::${DTB_BASE_NAME}.dtb
130 done
131
132 # Copy device tree overlays to dedicated folder
133 mmd -i ${WORKDIR}/boot.img overlays
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500134 for DTB in $DT_OVERLAYS; do
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600135 DTB_EXT=${DTB##*.}
136 DTB_BASE_NAME=`basename ${DTB} ."${DTB_EXT}"`
Patrick Williams8b8bc412016-08-17 15:02:23 -0500137
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600138 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${DTB_BASE_NAME}.${DTB_EXT} ::overlays/${DTB_BASE_NAME}.${DTB_EXT}
Patrick Williams8b8bc412016-08-17 15:02:23 -0500139 done
140 fi
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 if [ "${RPI_USE_U_BOOT}" = "1" ]; then
Patrick Williams8b8bc412016-08-17 15:02:23 -0500142 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${KERNEL_IMAGETYPE}
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600144 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/boot.scr ::boot.scr
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 else
Patrick Williams8b8bc412016-08-17 15:02:23 -0500146 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${SDIMG_KERNELIMAGE}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500147 fi
Patrick Williams8b8bc412016-08-17 15:02:23 -0500148
149 if [ -n ${FATPAYLOAD} ] ; then
150 echo "Copying payload into VFAT"
151 for entry in ${FATPAYLOAD} ; do
152 # add the || true to stop aborting on vfat issues like not supporting .~lock files
153 mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true
154 done
155 fi
156
157 # Add stamp file
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600158 echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info
159 mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}/image-version-info ::
Patrick Williams8b8bc412016-08-17 15:02:23 -0500160
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500161 # Deploy vfat partition (for u-boot case only)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500162 if [ "${RPI_USE_U_BOOT}" = "1" ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT}
164 ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT}
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500165 fi
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500166
Patrick Williams8b8bc412016-08-17 15:02:23 -0500167 # Burn Partitions
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500168 dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
Patrick Williams8b8bc412016-08-17 15:02:23 -0500169 # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat
170 if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz"
171 then
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500172 xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
Patrick Williams8b8bc412016-08-17 15:02:23 -0500173 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500174 dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
Patrick Williams8b8bc412016-08-17 15:02:23 -0500175 fi
176
177 # Optionally apply compression
178 case "${SDIMG_COMPRESSION}" in
179 "gzip")
180 gzip -k9 "${SDIMG}"
181 ;;
182 "bzip2")
183 bzip2 -k9 "${SDIMG}"
184 ;;
185 "xz")
186 xz -k "${SDIMG}"
187 ;;
188 esac
189}
190
191ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
192
193rpi_generate_sysctl_config() {
194 # systemd sysctl config
195 test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \
196 echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf
197
198 # sysv sysctl config
199 IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf"
200 test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \
201 sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF}
202 echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF}
203}