1inherit image_types
2
3#
4# Create an image that can be written onto a SD card using dd.
5#
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              48MiB           SDIMG_ROOTFS
17# <-----------------------> <----------> <---------------------->
18#  ------------------------ ------------ ------------------------
19# | IMAGE_ROOTFS_ALIGNMENT | BOOT_SPACE | ROOTFS_SIZE            |
20#  ------------------------ ------------ ------------------------
21# ^                        ^            ^                        ^
22# |                        |            |                        |
23# 0                      4MiB     4MiB + 48MiB       4MiB + 48Mib + SDIMG_ROOTFS
24
25# This image depends on the rootfs image
26IMAGE_TYPEDEP:rpi-sdimg = "${SDIMG_ROOTFS_TYPE}"
27
28# Boot partition volume id
29# Shorten raspberrypi to just rpi to keep it under 11 characters
30# now enforced by mkfs.vfat from dosfstools-4.2
31BOOTDD_VOLUME_ID ?= "${@d.getVar('MACHINE').replace('raspberrypi', 'rpi')}"
32
33# Boot partition size [in KiB] (will be rounded up to IMAGE_ROOTFS_ALIGNMENT)
34BOOT_SPACE ?= "49152"
35
36# Set alignment to 4MB [in KiB]
37IMAGE_ROOTFS_ALIGNMENT = "4096"
38
39# Use an uncompressed ext3 by default as rootfs
40SDIMG_ROOTFS_TYPE ?= "ext3"
41SDIMG_ROOTFS = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.${SDIMG_ROOTFS_TYPE}"
42
43# For the names of kernel artifacts
44inherit kernel-artifact-names
45
46RPI_SDIMG_EXTRA_DEPENDS ?= ""
47
48do_image_rpi_sdimg[depends] = " \
49    parted-native:do_populate_sysroot \
50    mtools-native:do_populate_sysroot \
51    dosfstools-native:do_populate_sysroot \
52    virtual/kernel:do_deploy \
53    rpi-bootfiles:do_deploy \
54    ${@bb.utils.contains('MACHINE_FEATURES', 'armstub', 'armstubs:do_deploy', '' ,d)} \
55    ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot:do_deploy', '',d)} \
56    ${@bb.utils.contains('RPI_USE_U_BOOT', '1', 'u-boot-default-script:do_deploy', '',d)} \
57    ${RPI_SDIMG_EXTRA_DEPENDS} \
58"
59
60do_image_rpi_sdimg[recrdeps] = "do_build"
61
62# SD card image name
63SDIMG = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rpi-sdimg"
64
65# Additional files and/or directories to be copied into the vfat partition from the IMAGE_ROOTFS.
66FATPAYLOAD ?= ""
67
68# SD card vfat partition image name
69SDIMG_VFAT_DEPLOY ?= "${RPI_USE_U_BOOT}"
70SDIMG_VFAT = "${IMAGE_NAME}.vfat"
71SDIMG_LINK_VFAT = "${IMGDEPLOYDIR}/${IMAGE_LINK_NAME}.vfat"
72
73IMAGE_CMD:rpi-sdimg () {
74
75    # Align partitions
76    BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE} + ${IMAGE_ROOTFS_ALIGNMENT} - 1)
77    BOOT_SPACE_ALIGNED=$(expr ${BOOT_SPACE_ALIGNED} - ${BOOT_SPACE_ALIGNED} % ${IMAGE_ROOTFS_ALIGNMENT})
78    SDIMG_SIZE=$(expr ${IMAGE_ROOTFS_ALIGNMENT} + ${BOOT_SPACE_ALIGNED} + $ROOTFS_SIZE)
79
80    echo "Creating filesystem with Boot partition ${BOOT_SPACE_ALIGNED} KiB and RootFS $ROOTFS_SIZE KiB"
81
82    # Check if we are building with device tree support
83    DTS="${@make_dtb_boot_files(d)}"
84
85    # Initialize sdcard image file
86    dd if=/dev/zero of=${SDIMG} bs=1024 count=0 seek=${SDIMG_SIZE}
87
88    # Create partition table
89    parted -s ${SDIMG} mklabel msdos
90    # Create boot partition and mark it as bootable
91    parted -s ${SDIMG} unit KiB mkpart primary fat32 ${IMAGE_ROOTFS_ALIGNMENT} $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT})
92    parted -s ${SDIMG} set 1 boot on
93    # Create rootfs partition to the end of disk
94    parted -s ${SDIMG} -- unit KiB mkpart primary ext2 $(expr ${BOOT_SPACE_ALIGNED} \+ ${IMAGE_ROOTFS_ALIGNMENT}) -1s
95    parted ${SDIMG} print
96
97    # Create a vfat image with boot files
98    BOOT_BLOCKS=$(LC_ALL=C parted -s ${SDIMG} unit b print | awk '/ 1 / { print substr($4, 1, length($4 -1)) / 512 /2 }')
99    rm -f ${WORKDIR}/boot.img
100    mkfs.vfat -F32 -n "${BOOTDD_VOLUME_ID}" -S 512 -C ${WORKDIR}/boot.img $BOOT_BLOCKS
101    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"
102    if [ "${@bb.utils.contains("MACHINE_FEATURES", "armstub", "1", "0", d)}" = "1" ]; then
103        mcopy -v -i ${WORKDIR}/boot.img -s ${DEPLOY_DIR_IMAGE}/armstubs/${ARMSTUB} ::/ || bbfatal "mcopy cannot copy ${DEPLOY_DIR_IMAGE}/armstubs/${ARMSTUB} into boot.img"
104    fi
105    if test -n "${DTS}"; then
106        # Copy board device trees (including overlays)
107        # There is an assumption here - no DTB in other directories than root
108        # and root/overlays. mmd/mcopy are not very flexible tools.
109        mmd -i ${WORKDIR}/boot.img overlays
110        for entry in ${DTS} ; do
111            # Split entry at optional ';'
112            if [ $(echo "$entry" | grep -c \;) = "0" ] ; then
113                DEPLOY_FILE="$entry"
114                DEST_FILENAME="$entry"
115            else
116                DEPLOY_FILE="$(echo "$entry" | cut -f1 -d\;)"
117                DEST_FILENAME="$(echo "$entry" | cut -f2- -d\;)"
118            fi
119            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"
120        done
121    fi
122    if [ "${RPI_USE_U_BOOT}" = "1" ]; then
123        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"
124        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"
125        if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
126            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"
127        else
128            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"
129        fi
130    else
131        if [ ! -z "${INITRAMFS_IMAGE}" -a "${INITRAMFS_IMAGE_BUNDLE}" = "1" ]; then
132            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"
133        else
134            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"
135        fi
136    fi
137
138    # Add files (eg. hypervisor binaries) from the deploy dir
139    if [ -n "${DEPLOYPAYLOAD}" ] ; then
140        echo "Copying deploy file payload into VFAT"
141        for entry in ${DEPLOYPAYLOAD} ; do
142            # Split entry at optional ':' to enable file renaming for the destination
143            if [ $(echo "$entry" | grep -c :) = "0" ] ; then
144                DEPLOY_FILE="$entry"
145                DEST_FILENAME="$entry"
146            else
147                DEPLOY_FILE="$(echo "$entry" | cut -f1 -d:)"
148                DEST_FILENAME="$(echo "$entry" | cut -f2- -d:)"
149            fi
150            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"
151        done
152    fi
153
154    if [ -n "${FATPAYLOAD}" ] ; then
155        echo "Copying payload into VFAT"
156        for entry in ${FATPAYLOAD} ; do
157            # use bbwarn instead of bbfatal to stop aborting on vfat issues like not supporting .~lock files
158            mcopy -v -i ${WORKDIR}/boot.img -s ${IMAGE_ROOTFS}$entry :: || bbwarn "mcopy cannot copy ${IMAGE_ROOTFS}$entry into boot.img"
159        done
160    fi
161
162    # Add stamp file
163    echo "${IMAGE_NAME}" > ${WORKDIR}/image-version-info
164    mcopy -v -i ${WORKDIR}/boot.img ${WORKDIR}/image-version-info :: || bbfatal "mcopy cannot copy ${WORKDIR}/image-version-info into boot.img"
165
166    # Deploy vfat partition
167    if [ "${SDIMG_VFAT_DEPLOY}" = "1" ]; then
168        cp ${WORKDIR}/boot.img ${IMGDEPLOYDIR}/${SDIMG_VFAT}
169        ln -sf ${SDIMG_VFAT} ${SDIMG_LINK_VFAT}
170    fi
171
172    # Burn Partitions
173    dd if=${WORKDIR}/boot.img of=${SDIMG} conv=notrunc seek=1 bs=$(expr ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
174    # If SDIMG_ROOTFS_TYPE is a .xz file use xzcat
175    if echo "${SDIMG_ROOTFS_TYPE}" | egrep -q "*\.xz"
176    then
177        xzcat ${SDIMG_ROOTFS} | dd of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
178    else
179        dd if=${SDIMG_ROOTFS} of=${SDIMG} conv=notrunc seek=1 bs=$(expr 1024 \* ${BOOT_SPACE_ALIGNED} + ${IMAGE_ROOTFS_ALIGNMENT} \* 1024)
180    fi
181}
182
183ROOTFS_POSTPROCESS_COMMAND += " rpi_generate_sysctl_config ; "
184
185rpi_generate_sysctl_config() {
186    # systemd sysctl config
187    test -d ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d && \
188        echo "vm.min_free_kbytes = 8192" > ${IMAGE_ROOTFS}${sysconfdir}/sysctl.d/rpi-vm.conf
189
190    # sysv sysctl config
191    IMAGE_SYSCTL_CONF="${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf"
192    test -e ${IMAGE_ROOTFS}${sysconfdir}/sysctl.conf && \
193        sed -e "/vm.min_free_kbytes/d" -i ${IMAGE_SYSCTL_CONF}
194    echo "" >> ${IMAGE_SYSCTL_CONF} && echo "vm.min_free_kbytes = 8192" >> ${IMAGE_SYSCTL_CONF}
195}
196