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>"
55	exit 0
56fi
57
58IMAGE="$1"
59if [ ! -f "$IMAGE" ]; then
60	echo "The image file $IMAGE does not exist"
61	exit 1
62fi
63
64if [ -z "$2" ]; then
65       DEV_SEL="1"    # by default, select primary device
66else
67       DEV_SEL="$2"
68fi
69
70# Turn off the Host if it is currently ON
71chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
72echo "--- Current Chassis State: $chassisstate"
73if [ "$chassisstate" == 'On' ];
74then
75	echo "--- Turning the Chassis off"
76	obmcutil chassisoff
77	sleep 10
78	# Check if HOST was OFF
79	chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
80	if [ "$chassisstate_off" == 'On' ];
81	then
82		echo "--- Error : Failed turning the Chassis off"
83		exit 1
84	fi
85fi
86
87# Switch the host SPI bus to BMC"
88echo "--- Switch the host SPI bus to BMC."
89if ! gpioset $(gpiofind spi0-program-sel)=1; then
90	echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state"
91	exit 1
92fi
93
94# Switch the host SPI bus (between primary and secondary)
95# 183 is BMC_GPIOW7_SPI0_BACKUP_SEL
96if [[ $DEV_SEL == 1 ]]; then
97	echo "Run update Primary Host SPI-NOR"
98	gpioset $(gpiofind spi0-backup-sel)=1       # Primary SPI
99elif [[ $DEV_SEL == 2 ]]; then
100	echo "Run update Second Host SPI-NOR"
101	gpioset $(gpiofind spi0-backup-sel)=0       # Second SPI
102else
103	echo "Please choose primary SPI (1) or second SPI (2)"
104	exit 0
105fi
106
107# Flash the firmware
108do_flash
109
110# Switch the SPI bus to the primary spi device
111echo "Switch to the Primary Host SPI-NOR"
112gpioset $(gpiofind spi0-backup-sel)=1       # Primary SPI
113
114# Switch the host SPI bus to HOST."
115echo "--- Switch the host SPI bus to HOST."
116if ! gpioset $(gpiofind spi0-program-sel)=0; then
117	echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state"
118	exit 1
119fi
120
121if [ "$chassisstate" == 'On' ];
122then
123	sleep 5
124	echo "Turn on the Host"
125	obmcutil poweron
126fi
127