1#!/bin/bash
2
3# This script is used to flash the UEFI/EDKII
4
5# Syntax: ampere_flash_bios.sh $image_file $device_sellect
6# Where:
7#       $image_file : the image binary file
8#       $device_sellect : 1 - Host Main SPI Nor
9#                         2 - Host Second SPI Nor
10
11# Author : Chanh Nguyen (chnguyen@amperecomputing.com)
12
13# Note:
14#      BMC_GPIOW6_SPI0_PROGRAM_SEL (GPIO 182): 1 => BMC owns SPI bus for upgrading
15#                                              0 => HOST owns SPI bus for upgrading
16
17#      BMC_GPIOW7_SPI0_BACKUP_SEL (GPIO 183) :  1 => to switch SPI_CS0_L to primary SPI Nor device
18#                                               0 => to switch SPI_CS0_L to second SPI Nor device
19
20# shellcheck disable=SC2046
21# shellcheck disable=SC2086
22
23do_flash () {
24	# always unbind then bind the ASpeed SMC driver again to prevent
25	# the changing of the device erasesize by nvparm
26	HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
27	if [ -n "$HOST_MTD" ];
28	then
29		echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/unbind
30		sleep 2
31	fi
32	echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/bind
33
34	# Check the PNOR partition available
35	HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
36	if [ -z "$HOST_MTD" ];
37	then
38		echo "Fail to probe the Host SPI-NOR device"
39		exit 1
40	fi
41
42	echo "--- Flashing firmware image $IMAGE to @/dev/$HOST_MTD"
43	flashcp -v "$IMAGE" /dev/"$HOST_MTD"
44}
45
46
47if [ $# -eq 0 ]; then
48	echo "Usage: $(basename "$0") <UEFI/EDKII image file> <DEV_SEL> [SPECIAL_BOOT]"
49	echo "Where:"
50	echo "    DEV_SEL 1 is Primary SPI (by default), 2 is Second SPI"
51	echo "    SPECIAL_BOOT: Optional, input '1' to flash "Secure Provisioning" image and enter Special Boot mode. Default: 0"
52	exit 0
53fi
54
55IMAGE="$1"
56if [ ! -f "$IMAGE" ]; then
57	echo "The image file $IMAGE does not exist"
58	exit 1
59fi
60
61if [ -z "$2" ]; then
62       DEV_SEL="1"    # by default, select primary device
63else
64       DEV_SEL="$2"
65fi
66
67SPECIAL_BOOT=0
68if [[ "$3" == "1" ]]; then
69	SPECIAL_BOOT=1
70fi
71
72echo "SPECIAL_BOOT mode: $SPECIAL_BOOT"
73# Turn off the Host if it is currently ON
74chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
75echo "--- Current Chassis State: $chassisstate"
76if [ "$chassisstate" == 'On' ];
77then
78	echo "--- Turning the Chassis off"
79	obmcutil chassisoff
80	sleep 10
81	# Check if HOST was OFF
82	chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
83	if [ "$chassisstate_off" == 'On' ];
84	then
85		echo "--- Error : Failed turning the Chassis off"
86		exit 1
87	fi
88fi
89
90# Switch the host SPI bus to BMC"
91echo "--- Switch the host SPI bus to BMC."
92if ! gpioset $(gpiofind spi0-program-sel)=1; then
93	echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state"
94	exit 1
95fi
96
97# Switch the host SPI bus (between primary and secondary)
98# 183 is BMC_GPIOW7_SPI0_BACKUP_SEL
99if [[ $DEV_SEL == 1 ]]; then
100	echo "Run update Primary Host SPI-NOR"
101	gpioset $(gpiofind spi0-backup-sel)=1       # Primary SPI
102elif [[ $DEV_SEL == 2 ]]; then
103	echo "Run update Secondary Host SPI-NOR"
104	gpioset $(gpiofind spi0-backup-sel)=0       # Second SPI
105else
106	echo "Please choose primary SPI (1) or second SPI (2)"
107	exit 0
108fi
109
110# Restrict to flash Secondary Host SPI-NOR in case of SPECIAL_BOOT
111if [ $SPECIAL_BOOT == 1 ] && [ "$DEV_SEL" == 2 ]; then
112	echo "Flashing 2nd Host SPI NOR image with SECProv image is not allowed"
113	exit
114fi
115
116# Flash the firmware
117do_flash
118
119# Assert SPECIAL_BOOT GPIO PIN
120if [[ $SPECIAL_BOOT == 1 ]]; then
121	gpioset $(gpiofind host0-special-boot)=1
122	# Set HOST BOOTCOUNT to 0 to prevent Host reboot
123	busctl set-property xyz.openbmc_project.State.Host0 \
124		/xyz/openbmc_project/state/host0 \
125		xyz.openbmc_project.Control.Boot.RebootAttempts RetryAttempts u 0
126fi
127
128# Switch the SPI bus to the primary SPI device
129echo "Switch to the Primary Host SPI-NOR"
130gpioset $(gpiofind spi0-backup-sel)=1       # Primary SPI
131
132# Switch the host SPI bus to HOST."
133echo "--- Switch the host SPI bus to HOST."
134if ! gpioset $(gpiofind spi0-program-sel)=0; then
135	echo "ERROR: Switch the host SPI bus to HOST. Please check GPIO state"
136	exit 1
137fi
138
139if [ "$chassisstate" == 'On' ] || [ $SPECIAL_BOOT == 1 ];
140then
141	sleep 5
142	echo "Turn on the Host"
143	obmcutil poweron
144fi
145
146# Detection SECProv of failure or success
147if [[ $SPECIAL_BOOT == 1 ]]; then
148	# 30s time out in wait for FW_BOOT_OK
149	state=0
150	cnt=60
151	while [ $cnt -gt 0 ];
152	do
153		# Monitor FW_BOOT_OK gpio
154		state=$(gpioget $(gpiofind s0-fw-boot-ok))
155		if [[ "$state" == "1" ]]; then
156			break
157		fi
158		sleep 0.5
159		cnt=$((cnt - 1))
160	done
161
162	echo "--- Turning the Chassis off"
163	obmcutil chassisoff
164
165	# Deassert SPECIAL_BOOT GPIO PIN
166	gpioset $(gpiofind host0-special-boot)=0
167
168	sleep 10
169	# Recover HOST BOOTCOUNT to default
170	busctl set-property xyz.openbmc_project.State.Host0 \
171		/xyz/openbmc_project/state/host0 \
172		xyz.openbmc_project.Control.Boot.RebootAttempts RetryAttempts u 3
173fi
174