From 2d456ef6c7c30f2860ae450374eafae2b792be77 Mon Sep 17 00:00:00 2001 From: Michel Stempin Date: Sat, 17 Oct 2020 12:07:33 +0200 Subject: [PATCH] boot according to GPT bootable flags Signed-off-by: Michel Stempin --- FunKey/board/funkey/genimage.cfg | 1 + .../funkey/rootfs-overlay/etc/u-boot.env | 13 +- .../rootfs-overlay/usr/local/sbin/first_boot | 115 +++++++----------- .../rootfs-overlay/usr/local/sbin/toggle_boot | 8 ++ FunKey/configs/funkey_defconfig | 3 + 5 files changed, 64 insertions(+), 76 deletions(-) create mode 100755 FunKey/board/funkey/rootfs-overlay/usr/local/sbin/toggle_boot diff --git a/FunKey/board/funkey/genimage.cfg b/FunKey/board/funkey/genimage.cfg index a107816..5fb7632 100644 --- a/FunKey/board/funkey/genimage.cfg +++ b/FunKey/board/funkey/genimage.cfg @@ -45,6 +45,7 @@ image sdcard.img { partition rootfs { offset = 101M partition-type = 0x83 + bootable = "yes" image = "rootfs.ext4" size = 100M } diff --git a/FunKey/board/funkey/rootfs-overlay/etc/u-boot.env b/FunKey/board/funkey/rootfs-overlay/etc/u-boot.env index b26a333..82fb056 100644 --- a/FunKey/board/funkey/rootfs-overlay/etc/u-boot.env +++ b/FunKey/board/funkey/rootfs-overlay/etc/u-boot.env @@ -1,17 +1,14 @@ -alt_args_mmc=setenv bootargs console=ttyS0,115200 panic=5 rootwait fbcon=map:10 fbcon=font:VGA8x8 vt.global_cursor_default=0 root=/dev/mmcblk0p1 rootfstype=ext4 rootflags=commit=120,data=writeback,barrier=0,journal_async_commit rw quiet -alt_bootcmd=run alt_args_mmc; run alt_loadfdt; run alt_loadimage; bootz ${loadaddr} - ${fdtaddr} -alt_loadfdt=load mmc 0:1 ${fdtaddr} /boot/sun8i-v3s-funkey.dtb -alt_loadimage=load mmc 0:1 ${loadaddr} /boot/zImage -args_mmc=setenv bootargs console=ttyS0,115200 panic=5 rootwait fbcon=map:10 fbcon=font:VGA8x8 vt.global_cursor_default=0 root=/dev/mmcblk0p2 rootfstype=ext4 rootflags=commit=120,data=writeback,barrier=0,journal_async_commit rw quiet +args_mmc=setenv bootargs console=ttyS0,115200 panic=5 rootwait fbcon=map:10 fbcon=font:VGA8x8 vt.global_cursor_default=0 root=/dev/mmcblk0p${bootpart} rootfstype=ext4 rootflags=commit=120,data=writeback,barrier=0,journal_async_commit rw quiet baudrate=115200 -bootcmd=run args_mmc; run loadfdt; run loadimage; bootz ${loadaddr} - ${fdtaddr} +bootcmd=run getbootpart; run args_mmc; run loadfdt; run loadimage; bootz ${loadaddr} - ${fdtaddr} bootdelay=0 console=ttyS0,115200 fdt_high=0xffffffff fdtaddr=0x41800000 +getbootpart=part list mmc 0 -bootable bootpart loadaddr=0x41000000 -loadfdt=load mmc 0:2 ${fdtaddr} /boot/sun8i-v3s-funkey.dtb -loadimage=load mmc 0:2 ${loadaddr} /boot/zImage +loadfdt=load mmc 0:${bootpart} ${fdtaddr} /boot/sun8i-v3s-funkey.dtb +loadimage=load mmc 0:${bootpart} ${loadaddr} /boot/zImage stderr=serial@01c28000 stdin=serial@01c28000 stdout=serial@01c28000 diff --git a/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/first_boot b/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/first_boot index b7ea00e..eadac62 100755 --- a/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/first_boot +++ b/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/first_boot @@ -6,10 +6,16 @@ source /usr/local/lib/utils SELF=$(basename $0) + +# Find out the root partition number from the kernel command line root_part=$(cat /proc/cmdline | sed -n 's|^.*root=/dev/\([^ ]*\).*|\1|p') -part_num=${root_part#mmcblk0p} -if [ "${part_num}" -eq 1 ]; then +root_part_num=${root_part#mmcblk0p} +if [ "${root_part_num}" -eq 1 ]; then die 0 "recovery mode" +elif [ "${root_part_num}" = "{$root_part}" ]; then + die 1 "${root_part} is not an SD card partition, aborting" +elif [ "${root_part_num}" -ne 2 ]; then + die 2 "unknown partition layout, aborting" fi notif () { @@ -27,56 +33,47 @@ check_swap () { } check_root_id () { - [ $(id -u) -ne 0 ] && die 1 "this script must be run as root, aborting" + [ $(id -u) -ne 0 ] && die 3 "this script must be run as root, aborting" return 0 } resize_rootfs_partition () { - if [ "${part_num}" = "{$root_part}" ]; then - die 2 "${root_part} is not an SD card. Don't know how to expand it, aborting" - fi - if [ "${part_num}" -ne 2 ]; then - die 3 "your partition layout is not currently supported by this tool, aborting" - fi - local last_part_line=$(fdisk /dev/mmcblk0 -l | grep '^/' | tail -n 1) + + # Check that the last partition is the rootfs partition + local last_part_line=$(sgdisk -p /dev/mmcblk0 | tail -n 1) set ${last_part_line} - local last_part=${1#/dev/} - local part_start=${2} - if [ "${last_part}" != "${root_part}" ]; then - die 4 "${root_part} is not the last partition. Don't know how to expand, aborting" + local last_part=${1} + if [ "${last_part}" != "${root_part_num}" ]; then + die 4 "rootfs is not the last partition. Don't know how to expand, aborting" fi - # Return value will likely be error for fdisk as it fails to reload the - # partition table because the root fs is mounted - # NOTE: This script only works the genuine fdisk, NOT with the busybox one!!! - fdisk /dev/mmcblk0 >/dev/null 2>&1 </dev/null 2>&1 || die 5 "cannot remove the rootfs partition, aborting" + + # Re-create the rootfs partition with a 1GB size + sgdisk -n ${root_part_num}:0:+1G -c ${root_part_num}:rootfs /dev/mmcblk0 >/dev/null 2>&1 || die 6 "cannot resize the rootfs partition, aborting" + + # Mark the rootfs partition as bootable + sgdisk -A ${root_part_num}:set:2 /dev/mmcblk0 >/dev/null 2>&1 || die 7 "cannot make the rootfs partition bootable, aborting" + + # Copy the primary GPT to the end of the disk + sgdisk -e /dev/mmcblk0 >/dev/null 2>&1 || die 8 "cannot move the GPT to the end of the disk" sync return 0 } reload_partition_table () { - partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 6 "cannot reload the partition table, aborting" + partprobe /dev/mmcblk0 >/dev/null 2>&1 || die 9 "cannot reload the partition table, aborting" return 0 } resize_rootfs_filesystem () { - resize2fs /dev/${root_part} >/dev/null 2>&1 || die 7 "cannot resize the root filesystem, aborting" + resize2fs /dev/${root_part} >/dev/null 2>&1 || die 10 "cannot resize the root filesystem, aborting" return 0 } write_bootloader_env () { - fw_saveenv /etc/u-boot.env || die 8 "cannot write bootloader inevrionment, aborting" + fw_saveenv /etc/u-boot.env || die 11 "cannot write bootloader inevrionment, aborting" return 0 } @@ -85,20 +82,22 @@ create_swap_file () { set ${root_part_line} local space_left=${4} if [ ${space_left} -lt 131072 ]; then - die 9 "not enough free space for swap file found, aborting" + die 12 "not enough free space for swap file found, aborting" fi + + # Create an empty 128MB /swap file, change its permissions and format it as swap dd if=/dev/zero of=/swap bs=1M count=128 >/dev/null 2>&1 && chmod 0600 /swap >/dev/null 2>&1 && mkswap /swap >/dev/null 2>&1 if [ $? -ne 0 ]; then rm /swap - die 10 "cannot create swap file, aborting" + die 13 "cannot create swap file, aborting" fi return 0 } enable_swap_file () { - swapon -a >/dev/null 2>&1 || die 11 "cannot enable swap file, aborting" + swapon -a >/dev/null 2>&1 || die 14 "cannot enable swap file, aborting" return 0 } @@ -106,37 +105,17 @@ create_backing_store_partition () { mount | grep -q /dev/mmcblk0p3 if [ $? -ne 0 ]; then - # Find out the root partition number from kernel command line - if [ "$part_num" = "$root_part" ]; then - die 5 "$root_part is not an SD card. Don't know how to create the backing store partition" - fi - if [ "$part_num" -ne 2 ]; then - die 6 "your partition layout is not currently supported" + # Check that the last partition is the rootfs partition + local last_part_line=$(sgdisk -p /dev/mmcblk0 2>/dev/null | tail -n 1) + set ${last_part_line} + local last_part=${1} + if [ "$last_part" != "$root_part_num" ]; then + die 15 "rootfs is not the last partition. Don't know how to create the backing store partition" fi - # Check that the last partition is the root partition - local last_part_line=$(fdisk /dev/mmcblk0 -l 2>/dev/null | grep '^/' | tail -n 1) - set $last_part_line - local last_part=${1#/dev/} - if [ "$last_part" != "$root_part" ]; then - die 7 "$root_part is not the last partition. Don't know how to create the backing store partition" - fi - - # The return value will likely be error for fdisk as it fails - # to reload the partition table because the root fs is mounted - # NOTE: This script only works the genuine fdisk, NOT with the - # busybox one!!! - # Create a third FAT32 partition that filsl the disk - fdisk /dev/mmcblk0 >/dev/null 2>&1 </dev/null 2>&1 || die 16 "cannot create the backing store partition, aborting" sync fi return 0 @@ -145,12 +124,12 @@ EOF format_backing_store_partition () { # Format the backing store as FAT32 - mkfs.vfat /dev/mmcblk0p3 >/dev/null 2>&1 || die 9 "cannot format the backing store partition" + mkfs.vfat /dev/mmcblk0p3 >/dev/null 2>&1 || die 17 "cannot format the backing store partition" # Add file to force assembly tests - mount /mnt/ || die 9 "Cannot mount /mnt" - touch /mnt/.assembly_tests || die 9 "Cannot create assembly tests run file" - umount /mnt/ || die 9 "Cannot unmount /mnt" + mount /mnt/ || die 18 "Cannot mount /mnt" + #touch /mnt/.assembly_tests || die 19 "Cannot create assembly tests run file" + umount /mnt/ || die 20 "Cannot unmount /mnt" return 0 } diff --git a/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/toggle_boot b/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/toggle_boot new file mode 100755 index 0000000..9c1ed93 --- /dev/null +++ b/FunKey/board/funkey/rootfs-overlay/usr/local/sbin/toggle_boot @@ -0,0 +1,8 @@ +#!/bin/sh +sgdisk -A 1:toggle:2 -A 2:toggle:2 /dev/mmcblk0 >/dev/null 2>&1 +recovery=$(sgdisk -A 1:get:2 /dev/mmcblk0 | cut -d : -f3) +if [ ${recovery} -eq 1 ]; then + echo "Next boot will be in recovery mode" +else + echo "Next boot will be in normal mode" +fi diff --git a/FunKey/configs/funkey_defconfig b/FunKey/configs/funkey_defconfig index 59078e8..8f30de4 100644 --- a/FunKey/configs/funkey_defconfig +++ b/FunKey/configs/funkey_defconfig @@ -58,6 +58,9 @@ BR2_PACKAGE_SDL_IMAGE_JPEG=y BR2_PACKAGE_SDL_IMAGE_PNG=y BR2_PACKAGE_SDL_SOUND=y BR2_PACKAGE_SDL_SOUND_PLAYSOUND=y +BR2_PACKAGE_GPTFDISK=y +BR2_PACKAGE_GPTFDISK_GDISK=y +BR2_PACKAGE_GPTFDISK_SGDISK=y BR2_PACKAGE_PARTED=y BR2_PACKAGE_SYSSTAT=y BR2_PACKAGE_UBOOT_TOOLS=y