From 7320b42f0b8143d7c76ed3c0e6b1c0ff0dc078ad Mon Sep 17 00:00:00 2001 From: Mirza Krak Date: Fri, 29 Nov 2019 16:58:33 +0000 Subject: [PATCH] Implement MENDER_COPY_BOOT_GAP feature This is necessary for the U-boot -> EFI -> GRUB integration to be complete as many boards embedd the U-Boot binary at the start of the SD/eMMC a.k.a boot-gap. When MENDER_COPY_BOOT_GAP is enabled, it will be copied from the input disk image to the output ".sdimg" MEN-2784 Changelog: Title Signed-off-by: Mirza Krak --- configs/beaglebone_black_base_config | 3 +++ configs/mender_convert_config | 7 +++++++ configs/qemux86-64_config | 3 +++ configs/raspberrypi_config | 3 +++ configs/rockpro64_config | 3 +++ mender-convert-extract | 7 +++++-- mender-convert-package | 16 ++++++++++++++++ 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/configs/beaglebone_black_base_config b/configs/beaglebone_black_base_config index 84fb04b..5878958 100644 --- a/configs/beaglebone_black_base_config +++ b/configs/beaglebone_black_base_config @@ -6,6 +6,9 @@ # There are reported issues with GRUB bootloader integration, fallback to U-boot. MENDER_GRUB_EFI_INTEGRATION=n +# We will write a modified bootloader +MENDER_COPY_BOOT_GAP=n + # 4MB alignment MENDER_PARTITION_ALIGNMENT="4194304" diff --git a/configs/mender_convert_config b/configs/mender_convert_config index 01344bc..b056b74 100644 --- a/configs/mender_convert_config +++ b/configs/mender_convert_config @@ -120,6 +120,13 @@ MENDER_KERNEL_DEVICETREE=kernel.dtb # https://github.com/intel/bmap-tools MENDER_USE_BMAP="n" +# Copy the boot gap, which is the area from sector 1 until first parition, +# this is done because some images might contain the bootloader embedded here +# and we need to copy the bootloader to the output image. +# +# In most cases you would like this enabled. +MENDER_COPY_BOOT_GAP="y" + source configs/mender_grub_config # Function to create Mender Artifact diff --git a/configs/qemux86-64_config b/configs/qemux86-64_config index aa46612..42e2b67 100644 --- a/configs/qemux86-64_config +++ b/configs/qemux86-64_config @@ -14,3 +14,6 @@ MENDER_STORAGE_DEVICE=/dev/sda MENDER_DEVICE_TYPE="qemux86_64" + +# Nothing to copy +MENDER_COPY_BOOT_GAP="n" diff --git a/configs/raspberrypi_config b/configs/raspberrypi_config index 42f3720..655fc34 100644 --- a/configs/raspberrypi_config +++ b/configs/raspberrypi_config @@ -1,6 +1,9 @@ # Raspberry Pi does not support GRUB bootloader integration, fallback to U-boot. MENDER_GRUB_EFI_INTEGRATION=n +# Nothing to copy +MENDER_COPY_BOOT_GAP=n + # 4MB alignment MENDER_PARTITION_ALIGNMENT="4194304" diff --git a/configs/rockpro64_config b/configs/rockpro64_config index 9e9e631..061a2ea 100644 --- a/configs/rockpro64_config +++ b/configs/rockpro64_config @@ -1,6 +1,9 @@ # ROCKPro64 do not support GRUB bootloader integration, fallback to U-boot. MENDER_GRUB_EFI_INTEGRATION=n +# We will write a modified bootloader +MENDER_COPY_BOOT_GAP=n + ROCKPRO64_BINARIES_URL="${MENDER_STORAGE_URL}/mender-convert/armbian/rockpro64/${ROCKPRO64_BINARIES}" function platform_modify() { diff --git a/mender-convert-extract b/mender-convert-extract index d43e233..9514310 100755 --- a/mender-convert-extract +++ b/mender-convert-extract @@ -102,5 +102,8 @@ fi # this is done because some images might contain the bootloader embedded here # and we might need to keep this area intact when we generate our custom # image. -disk_extract_part "${disk_image}" \ - 1 $(( $(disk_get_part_value ${disk_image} 1 START) - 1)) work/boot-gap.bin +if [ "${MENDER_COPY_BOOT_GAP}" == "y" ]; then + log_info "Extracting boot gap to work/boot-gap.bin" + disk_extract_part "${disk_image}" \ + 1 $(( $(disk_get_part_value ${disk_image} 1 START) - 1)) work/boot-gap.bin +fi diff --git a/mender-convert-package b/mender-convert-package index 8fea628..d707b83 100755 --- a/mender-convert-package +++ b/mender-convert-package @@ -102,6 +102,16 @@ else overhead_sectors=$(( ${alignment_sectors} * 6 )) fi +# Validate boot-gap.bin size, ensuring it will fit between MBR and boot_part_start +if [ "${MENDER_COPY_BOOT_GAP}" == "y" ]; then + # Add one block for MBR + boot_gap_sectors=$(( $(stat --printf="%b" work/boot-gap.bin) + 1)) + if [ ${boot_gap_sectors} -ge ${boot_part_start} ]; then + log_warn "The work/boot-gap.bin file will overwrite the boot partition" + log_fatal "Please increase MENDER_PARTITION_ALIGNMENT (2x will typically solve this))" + fi +fi + # Validate boot part size actual_boot_size=$(du --apparent-size -s --block-size=512 ${boot_part} | cut -f 1) if [ ${actual_boot_size} -gt ${boot_part_sectors} ]; then @@ -199,6 +209,12 @@ run_and_log_cmd "${PARTED} -s ${sdimg_path} -- unit s mkpart primary ext2 ${root run_and_log_cmd "${PARTED} -s ${sdimg_path} -- unit s mkpart primary ext2 ${data_start} ${data_end}" run_and_log_cmd "${PARTED} -s ${sdimg_path} print" +# Write boot-gap +if [ "${MENDER_COPY_BOOT_GAP}" == "y" ]; then + log_info "Writing boot gap of size: ${boot_part_sectors} (sectors)" + disk_write_at_offset "${output_dir}/boot-gap.bin" "${sdimg_path}" "1" +fi + # Burn Partitions disk_write_at_offset "${boot_part}" "${sdimg_path}" "${boot_part_start}" disk_write_at_offset "${output_dir}/rootfs.img" "${sdimg_path}" "${rootfsa_start}"