blob: 56a854e038ae232361f5bbbbee2c3b9e59787ad5 [file] [log] [blame]
Patrick Williams8b8bc412016-08-17 15:02:23 -05001inherit image_types
2inherit 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
27IMAGE_TYPEDEP_rpi-sdimg = "${SDIMG_ROOTFS_TYPE}"
28
29# Set kernel and boot loader
30IMAGE_BOOTLOADER ?= "bcm2835-bootfiles"
31
32# Set initramfs extension
33KERNEL_INITRAMFS ?= ""
34
35# Kernel image name
36SDIMG_KERNELIMAGE_raspberrypi ?= "kernel.img"
37SDIMG_KERNELIMAGE_raspberrypi2 ?= "kernel7.img"
Brad Bishop3a5fbf52017-12-04 02:17:07 -050038SDIMG_KERNELIMAGE_raspberrypi3-64 ?= "kernel8.img"
Patrick Williams8b8bc412016-08-17 15:02:23 -050039
40# Boot partition volume id
41BOOTDD_VOLUME_ID ?= "${MACHINE}"
42
43# Boot partition size [in KiB] (will be rounded up to IMAGE_ROOTFS_ALIGNMENT)
44BOOT_SPACE ?= "40960"
45
46# Set alignment to 4MB [in KiB]
47IMAGE_ROOTFS_ALIGNMENT = "4096"
48
49# Use an uncompressed ext3 by default as rootfs
50SDIMG_ROOTFS_TYPE ?= "ext3"
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -060051SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.${SDIMG_ROOTFS_TYPE}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050052
53IMAGE_DEPENDS_rpi-sdimg = " \
54 parted-native \
55 mtools-native \
56 dosfstools-native \
57 virtual/kernel:do_deploy \
58 ${IMAGE_BOOTLOADER} \
59 ${@bb.utils.contains('KERNEL_IMAGETYPE', 'uImage', 'u-boot', '',d)} \
60 "
61
62# SD card image name
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -060063SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.rpi-sdimg"
Patrick Williams8b8bc412016-08-17 15:02:23 -050064
65# Compression method to apply to SDIMG after it has been created. Supported
66# compression formats are "gzip", "bzip2" or "xz". The original .rpi-sdimg file
67# is kept and a new compressed file is created if one of these compression
68# formats is chosen. If SDIMG_COMPRESSION is set to any other value it is
69# silently ignored.
70#SDIMG_COMPRESSION ?= ""
71
72# 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
76SDIMG_VFAT = "${IMAGE_NAME}.vfat"
77SDIMG_LINK_VFAT = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.vfat"
78
Patrick Williams8b8bc412016-08-17 15:02:23 -050079IMAGE_CMD_rpi-sdimg () {
80
81 # Align partitions
82 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
83 BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
84 SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE)
85
86 echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
87
88 # Check if we are building with device tree support
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -060089 DTS="${@get_dts(d)}"
Patrick Williams8b8bc412016-08-17 15:02:23 -050090
91 # Initialize sdcard image file
92 dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
93
94 # Create partition table
95 parted -s ${SDIMG} mklabel msdos
96 # Create boot partition and mark it as bootable
97 parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT})
98 parted -s ${SDIMG} set 1 boot on
99 # Create rootfs partition to the end of disk
100 parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s
101 parted ${SDIMG} print
102
103 # Create a vfat image with boot files
104 BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }')
105 rm -f ${WORKDIR}/boot.img
106 mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
107 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ::/
108 if test -n "${DTS}"; then
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600109 # 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 -0500110 DT_OVERLAYS="${@split_overlays(d, 0)}"
111 DT_ROOT="${@split_overlays(d, 1)}"
112
113 # Copy board device trees to root folder
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500114 for DTB in $DT_ROOT; do
Patrick Williams8b8bc412016-08-17 15:02:23 -0500115 DTB_BASE_NAME=`basename ${DTB} .dtb`
116
117 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${DTB_BASE_NAME}.dtb ::${DTB_BASE_NAME}.dtb
118 done
119
120 # Copy device tree overlays to dedicated folder
121 mmd -i ${WORKDIR}/boot.img overlays
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 for DTB in $DT_OVERLAYS; do
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600123 DTB_EXT=${DTB##*.}
124 DTB_BASE_NAME=`basename ${DTB} ."${DTB_EXT}"`
Patrick Williams8b8bc412016-08-17 15:02:23 -0500125
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600126 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 -0500127 done
128 fi
129 case "${KERNEL_IMAGETYPE}" in
130 "uImage")
131 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE}
132 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::uImage
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600133 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/boot.scr ::boot.scr
Patrick Williams8b8bc412016-08-17 15:02:23 -0500134 ;;
135 *)
136 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${SDIMG_KERNELIMAGE}
137 ;;
138 esac
139
140 if [ -n ${FATPAYLOAD} ] ; then
141 echo "Copying payload into VFAT"
142 for entry in ${FATPAYLOAD} ; do
143 # add the || true to stop aborting on vfat issues like not supporting .~lock files
144 mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true
145 done
146 fi
147
148 # Add stamp file
Patrick Williamsb2e6a9b2017-02-23 20:31:26 -0600149 echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info
150 mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}/image-version-info ::
Patrick Williams8b8bc412016-08-17 15:02:23 -0500151
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500152 # Deploy vfat partition (for u-boot case only)
153 case "${KERNEL_IMAGETYPE}" in
154 "uImage")
155 cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT}
156 ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT}
157 ;;
158 *)
159 ;;
160 esac
161
Patrick Williams8b8bc412016-08-17 15:02:23 -0500162 # Burn Partitions
163 dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
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) && sync && sync
168 else
169 dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
170 fi
171
172 # 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
184}
185
186ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
187
188rpi_generate_sysctl_config() {
189 # 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
192
193 # 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}
198}