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
22do_flash () {
23	# Check the HNOR partition available
24	HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
25	if [ -z "$HOST_MTD" ];
26	then
27		# Check the ASpeed SMC driver binded before
28		HOST_SPI=/sys/bus/platform/drivers/spi-aspeed-smc/1e630000.spi
29		if [ -d "$HOST_SPI" ]; then
30			echo "Unbind the ASpeed SMC driver"
31			echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/unbind
32			sleep 2
33		fi
34
35		# If the HNOR partition is not available, then bind again driver
36		echo "--- Bind the ASpeed SMC driver"
37		echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/bind
38		sleep 2
39
40		HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p')
41		if [ -z "$HOST_MTD" ];
42		then
43			echo "Fail to probe Host SPI-NOR device"
44			exit 1
45		fi
46	fi
47
48	echo "--- Flashing firmware image $IMAGE to @/dev/$HOST_MTD"
49	flashcp -v "$IMAGE" /dev/"$HOST_MTD"
50}
51
52
53if [ $# -eq 0 ]; then
54	echo "Usage: $(basename "$0") <UEFI/EDKII image file> <DEV_SEL> [SPECIAL_BOOT]"
55	echo "Where:"
56	echo "    DEV_SEL 1 is Primary SPI (by default), 2 is Second SPI"
57	echo "    SPECIAL_BOOT: Optional, input '1' to enter & flash SPECIAL_BOOT mode. Default: 0"
58	exit 0
59fi
60
61IMAGE="$1"
62if [ ! -f "$IMAGE" ]; then
63	echo "The image file $IMAGE does not exist"
64	exit 1
65fi
66
67if [ -z "$2" ]; then
68       DEV_SEL="1"    # by default, select primary device
69else
70       DEV_SEL="$2"
71fi
72
73SPECIAL_BOOT=0
74if [[ "$3" == "1" ]]; then
75	SPECIAL_BOOT=1
76fi
77
78echo "SPECIAL_BOOT mode: $SPECIAL_BOOT"
79# Turn off the Host if it is currently ON
80chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
81echo "--- Current Chassis State: $chassisstate"
82if [ "$chassisstate" == 'On' ];
83then
84	echo "--- Turning the Chassis off"
85	obmcutil chassisoff
86	sleep 10
87	# Check if HOST was OFF
88	chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
89	if [ "$chassisstate_off" == 'On' ];
90	then
91		echo "--- Error : Failed turning the Chassis off"
92		exit 1
93	fi
94fi
95
96# Switch the host SPI bus to BMC"
97echo "--- Switch the host SPI bus to BMC."
98if ! gpioset $(gpiofind spi0-program-sel)=1; then
99	echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state"
100	exit 1
101fi
102
103# Switch the host SPI bus (between primary and secondary)
104# 183 is BMC_GPIOW7_SPI0_BACKUP_SEL
105if [[ $DEV_SEL == 1 ]]; then
106	echo "Run update Primary Host SPI-NOR"
107	gpioset $(gpiofind spi0-backup-sel)=1       # Primary SPI
108elif [[ $DEV_SEL == 2 ]]; then
109	echo "Run update Second Host SPI-NOR"
110	gpioset $(gpiofind spi0-backup-sel)=0       # Second SPI
111else
112	echo "Please choose primary SPI (1) or second SPI (2)"
113	exit 0
114fi
115
116# Restrict to flash Second Host SPI-NOR in case of SPECIAL_BOOT
117if [ $SPECIAL_BOOT == 1 ] && [ "$DEV_SEL" == 2 ]; then
118	echo "Not allow to flash the Second Host SPI-NOR with SPECIAL_BOOT image"
119	exit
120fi
121
122# Flash the firmware
123do_flash
124
125# Assert SPECIAL_BOOT GPIO PIN
126if [[ $SPECIAL_BOOT == 1 ]]; then
127	gpioset $(gpiofind host0-special-boot)=1
128fi
129
130# Switch the SPI bus to the primary spi device
131echo "Switch to the Primary Host SPI-NOR"
132gpioset $(gpiofind spi0-backup-sel)=1       # Primary SPI
133
134# Switch the host SPI bus to HOST."
135echo "--- Switch the host SPI bus to HOST."
136if ! gpioset $(gpiofind spi0-program-sel)=0; then
137	echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state"
138	exit 1
139fi
140
141if [ "$chassisstate" == 'On' ] || [ $SPECIAL_BOOT == 1 ];
142then
143	sleep 5
144	echo "Turn on the Host"
145	obmcutil poweron
146fi
147
148# Deassert SPECIAL_BOOT GPIO PIN if it is being asserted
149if [[ $SPECIAL_BOOT == 1 ]]; then
150	# Time out checking for Host ON is 60s
151	cnt=12
152	while [ "$cnt" -gt 0 ];
153	do
154		cnt=$((cnt - 1))
155		if systemctl status obmc-host-already-on@0.target | grep "Active: active"; then
156			echo "Deassert SPECIAL_BOOT GPIO PIN if it is being asserted."
157			gpioset $(gpiofind host0-special-boot)=0
158			exit 0
159		fi
160		sleep 5
161	done
162fi
163