#!/bin/bash # vim:sw=8:noet SPL_UID=2E54B353-1271-4842-806F-E436D6AF6985 UBOOT_UID=BC13C2FF-59E6-4262-A352-B275FD6F7172 FSBL=$SPL_UID BBL=$UBOOT_UID MEDIATABLE=gpt ROOTPART_NUM=1 START_SECTOR=16 # MiB CONSOLE=1 soc_write_bootloader() { unmount_partition [ -n "$ROOTFS" ] || clear_riscv64_bootloader_partition print_message "Creating partitions for U-Boot SPL and OpenSBI + U-Boot..." sgdisk \ --set-alignment=2 \ --new=0:4096:8191 \ --change-name=0:spl \ --typecode=0:"$SPL_UID" \ --new=0:8192:16383 \ --change-name=0:uboot \ --typecode=0:"$UBOOT_UID" \ "$MEDIA" || fatal_error "failed to create U-Boot SPL and OpenSBI + U-Boot partitions" print_done print_message "Checking partition numbers..." SPLPART_NUM="" UBOOTPART_NUM="" local PART_NUMS=$(ls "$MEDIA"* |sed "s:$MEDIA::" |sed 's/^p//') local PART_NUM for PART_NUM in $PART_NUMS; do FIND_PART_CODE=$(sgdisk -i "$PART_NUM" "$MEDIA" | grep 'Partition GUID code:' | cut -d ' ' -f 4) case "$FIND_PART_CODE" in "$SPL_UID") SPLPART_NUM="$PART_NUM";; "$UBOOT_UID") UBOOTPART_NUM="$PART_NUM";; esac done if [[ "$UBOOTPART_NUM" != 2 && "$SPLPART_NUM" == 2 ]]; then sgdisk --transpose=$SPLPART_NUM:$UBOOTPART_NUM "$MEDIA" || fatal_error "failed to swap U-Boot SPL and OpenSBI + U-Boot partitions" SPLPART_NUM=$UBOOTPART_NUM UBOOTPART_NUM=2 elif [[ "$UBOOTPART_NUM" != 2 && "$SPLPART_NUM" != 2 ]]; then print_message "WARNING: Swap $UBOOTPART_NUM with 2-nd partition or else it won't boot!" print_message " # sgdisk --transpose=2:$UBOOTPART_NUM \"$MEDIA\"" fi print_done sync_partitions mount_partition print_message "Searching for U-Boot files..." # All starfive boards use same binary TARGET=starfive_visionfive2 set_path_to_uboot SPL_BLOB="$(readlink -e "$UBOOT/u-boot-spl.bin.normal.out")" [ -f "${SPL_BLOB-}" ] || fatal_error "${SPL_BLOB-} was not found" UBOOT_BLOB="$(readlink -e "$UBOOT/u-boot.itb")" [ -f "${UBOOT_BLOB-}" ] || fatal_error "${UBOOT_BLOB-}} was not found" print_done print_message "Writing U-Boot SPL for ${TARGET-}..." SPLPART="${MEDIA}${partsuffix-}${SPLPART_NUM}" [ -b "${SPLPART-}" ] || fatal_error "${SPLPART-} is not a block device or does not exist" dd if="$SPL_BLOB" of="$SPLPART" bs=4M status=none || fatal_error "failed to write U-Boot SPL" sync print_done print_message "Writing OpenSBI + U-Boot for ${TARGET-}..." UBOOTPART="${MEDIA}${partsuffix-}${UBOOTPART_NUM}" [ -b "${UBOOTPART-}" ] || fatal_error "${UBOOTPART-} is not a block device or does not exist" dd if="$UBOOT_BLOB" of="$UBOOTPART" bs=4M status=none || fatal_error "failed to write OpenSBI+U-Boot" sync print_done }