blob: 57cefea080e4443ba9e0d1978955cda56559eb28 [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"
38
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"
50SDIMG_ROOTFS = "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.${SDIMG_ROOTFS_TYPE}"
51
52IMAGE_DEPENDS_rpi-sdimg = " \
53 parted-native \
54 mtools-native \
55 dosfstools-native \
56 virtual/kernel:do_deploy \
57 ${IMAGE_BOOTLOADER} \
58 ${@bb.utils.contains('KERNEL_IMAGETYPE', 'uImage', 'u-boot', '',d)} \
59 "
60
61# SD card image name
62SDIMG = "${DEPLOY_DIR_IMAGE}/${IMAGE_NAME}.rootfs.rpi-sdimg"
63
64# Compression method to apply to SDIMG after it has been created. Supported
65# compression formats are "gzip", "bzip2" or "xz". The original .rpi-sdimg file
66# is kept and a new compressed file is created if one of these compression
67# formats is chosen. If SDIMG_COMPRESSION is set to any other value it is
68# silently ignored.
69#SDIMG_COMPRESSION ?= ""
70
71# Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS.
72FATPAYLOAD ?= ""
73
74IMAGEDATESTAMP = "${@time.strftime('%Y.%m.%d',time.gmtime())}"
75IMAGE_CMD_rpi-sdimg[vardepsexclude] += "IMAGEDATESTAMP"
76IMAGE_CMD_rpi-sdimg[vardepsexclude] += "DATETIME"
77
78IMAGE_CMD_rpi-sdimg () {
79
80 # 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)
84
85 echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
86
87 # Check if we are building with device tree support
88 DTS="${@get_dts(d, None)}"
89
90 # Initialize sdcard image file
91 dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
92
93 # 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
101
102 # 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
105 mkfs.vfat -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
106 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/bcm2835-bootfiles/* ::/
107 if test -n "${DTS}"; then
108 # Device Tree Overlays are assumed to be suffixed by '-overlay.dtb' string and will be put in a dedicated folder
109 DT_OVERLAYS="${@split_overlays(d, 0)}"
110 DT_ROOT="${@split_overlays(d, 1)}"
111
112 # Copy board device trees to root folder
113 for DTB in ${DT_ROOT}; do
114 DTB_BASE_NAME=`basename ${DTB} .dtb`
115
116 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${DTB_BASE_NAME}.dtb ::${DTB_BASE_NAME}.dtb
117 done
118
119 # Copy device tree overlays to dedicated folder
120 mmd -i ${WORKDIR}/boot.img overlays
121 for DTB in ${DT_OVERLAYS}; do
122 DTB_BASE_NAME=`basename ${DTB} .dtb`
123
124 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}-${DTB_BASE_NAME}.dtb ::overlays/${DTB_BASE_NAME}.dtb
125 done
126 fi
127 case "${KERNEL_IMAGETYPE}" in
128 "uImage")
129 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/u-boot.bin ::${SDIMG_KERNELIMAGE}
130 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::uImage
131 ;;
132 *)
133 mcopy -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/${KERNEL_IMAGETYPE}${KERNEL_INITRAMFS}-${MACHINE}.bin ::${SDIMG_KERNELIMAGE}
134 ;;
135 esac
136
137 if [ -n ${FATPAYLOAD} ] ; then
138 echo "Copying payload into VFAT"
139 for entry in ${FATPAYLOAD} ; do
140 # add the || true to stop aborting on vfat issues like not supporting .~lock files
141 mcopy -i ${WORKDIR}/boot.img -s -v ${IMAGE_ROOTFS}$entry :: || true
142 done
143 fi
144
145 # Add stamp file
146 echo "${IMAGE_NAME}-${IMAGEDATESTAMP}" > ${WORKDIR}/image-version-info
147 mcopy -i ${WORKDIR}/boot.img -v ${WORKDIR}//image-version-info ::
148
149 # Burn Partitions
150 dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
151 # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat
152 if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz"
153 then
154 xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
155 else
156 dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024) && sync && sync
157 fi
158
159 # Optionally apply compression
160 case "${SDIMG_COMPRESSION}" in
161 "gzip")
162 gzip -k9 "${SDIMG}"
163 ;;
164 "bzip2")
165 bzip2 -k9 "${SDIMG}"
166 ;;
167 "xz")
168 xz -k "${SDIMG}"
169 ;;
170 esac
171}
172
173ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
174
175rpi_generate_sysctl_config() {
176 # systemd sysctl config
177 test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \
178 echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf
179
180 # sysv sysctl config
181 IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf"
182 test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \
183 sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF}
184 echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF}
185}