summaryrefslogtreecommitdiff
path: root/alt-rootfs-installer/socs.d/K1x-riscv64.sh
blob: 3ec7c358262bb311c228f5e18be6611b151c83d8 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash
# vim:sw=8:noet

FSBL_UID=496846A9-9E0A-4ABA-9736-A41A9FC6B5D8
ENV_UID=5F720A03-FF87-4B93-A46F-F5788594EA3B
OPENSBI_UID=0FCB0F7C-4A6C-4C81-8C69-4F0A67F0D5E7
UBOOT_UID=3E5B8F92-0BFF-4E20-8E88-7EEC40A7CC42

MEDIATABLE=gpt
ROOTPART_NUM=1
START_SECTOR=4 # MiB
CONSOLE=1

soc_write_bootloader() {
	unmount_partition

	if [ -n "$ROOTFS" ]; then
		print_message "Creating partitions for FSBL, ENV, OpenSBI and U-Boot..."

		sgdisk -a 1 \
			--new=0:256:+256K --change-name=0:fsbl \
				--typecode=0:"$FSBL_UID" \
			--new=0:768:+64K --change-name=0:env \
				--typecode=0:"$ENV_UID" \
			--new=0:2048:+1M --change-name=0:opensbi \
				--typecode=0:"$OPENSBI_UID" \
			--new=0:4096:+2M --change-name=0:uboot \
				--typecode=0:"$UBOOT_UID" \
			"$MEDIA" ||
			fatal_error "failed to create bootloader partitions"

		sgdisk --sort "$MEDIA" ||
			fatal_error "failed to sort partitions by sector"
		print_done
		sync_partitions
	fi

	local FSBLPART ENVPART UBOOTPART OPENSBIPART
	PART_NUMS=$(ls "$MEDIA"* | sed "s:$MEDIA::" | sed 's/^p//')
	for PART_NUM in $PART_NUMS; do
		local CODE
		CODE=$(sgdisk -i "$PART_NUM" "$MEDIA" |
			grep 'Partition GUID code:' | cut -d ' ' -f 4)
		case "$CODE" in
			"$FSBL_UID") FSBLPART="${MEDIA}${partsuffix-}${PART_NUM}";;
			"$ENV_UID") ENVPART="${MEDIA}${partsuffix-}${PART_NUM}";;
			"$OPENSBI_UID") OPENSBIPART="${MEDIA}${partsuffix-}${PART_NUM}";;
			"$UBOOT_UID") UBOOTPART="${MEDIA}${partsuffix-}${PART_NUM}";;
		esac
	done

	# ensure we have all necessary partitions
	[ -n "$FSBLPART" ] && [ -n "$ENVPART" ] && [ -n "$OPENSBIPART" ] && [ -n "$UBOOTPART" ] ||
		fatal_error "bootloader partitions not found: expected FSBL, ENV, OpenSBI, U-Boot"

	# find actual rootfs after bootloader zone
	local RAW_START=$((START_SECTOR * 2048))
	local PART_NUMS PART_NUM PS
	PART_NUMS=$(ls "$MEDIA"* | sed "s:$MEDIA::" | sed 's/^p//')
	for PART_NUM in $PART_NUMS; do
		PS=$(sgdisk -i "$PART_NUM" "$MEDIA" 2>/dev/null |
			grep 'First sector:' | tr -s ' ' | cut -d ' ' -f 3)
		if [ -n "$PS" ] && [ "$PS" -ge "$RAW_START" ]; then
			ROOTPART_NUM=$PART_NUM
			break
		fi
	done

	ROOTPART="${MEDIA}${partsuffix-}${ROOTPART_NUM}"

	mount_partition

	TARGET=spacemit-k1x set_path_to_uboot
	print_message "Searching for U-Boot files for ${TARGET-}..."
	[ -d "$UBOOT" ] || fatal_error "U-Boot files for ${TARGET-} were not found"
	print_done

	print_message "Searching for OpenSBI files for ${TARGET-}..."
	OPENSBI="${TMPROOT-}/usr/share/opensbi-spacemit/k1x"
	[ -d "$OPENSBI" ] || fatal_error "OpenSBI files for ${TARGET-} were not found"
	print_done

	write_partition_blob "bootinfo" "$UBOOT/bootinfo_sd.bin" "$MEDIA" "1M"

	write_partition_blob "FSBL" "$UBOOT/FSBL.bin" "$FSBLPART"

	write_partition_blob "U-Boot environment" "$UBOOT/u-boot-env-default.bin" "$ENVPART"

	write_partition_blob "U-Boot" "$UBOOT/u-boot.itb" "$UBOOTPART"

	write_partition_blob "OpenSBI" "$OPENSBI/generic/firmware/fw_dynamic.itb" "$OPENSBIPART"
}