summaryrefslogtreecommitdiff
path: root/alt-rootfs-installer/socs.d/StarFive-riscv64.sh
blob: faae8d01bad64b1fa32e74e58bddf7251913fdd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/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
}