blob: b13e4b50a6b9304223cd5b1fbb054ce3276dfab5 [file] [log] [blame]
Thang Q. Nguyen25254b72021-01-07 07:16:50 +00001#!/bin/bash
2#
3# Copyright (c) 2021 Ampere Computing LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17do_flash () {
18 # Check the PNOR partition available
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000019 HOST_MTD=$(< /proc/mtd grep "pnor-uefi" | sed -n 's/^\(.*\):.*/\1/p')
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000020 if [ -z "$HOST_MTD" ];
21 then
22 # If the PNOR partition is not available, then bind again driver
23 echo "--- Bind the ASpeed SMC driver"
24 echo 1e630000.spi > /sys/bus/platform/drivers/aspeed-smc/bind
25 sleep 2
26
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000027 HOST_MTD=$(< /proc/mtd grep "pnor-uefi" | sed -n 's/^\(.*\):.*/\1/p')
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000028 if [ -z "$HOST_MTD" ];
29 then
30 echo "Fail to probe Host SPI-NOR device"
31 exit 1
32 fi
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000033 fi
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070034
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000035 echo "--- Flashing firmware to @/dev/$HOST_MTD"
36 flashcp -v "$IMAGE" /dev/"$HOST_MTD"
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000037}
38
39
40if [ $# -eq 0 ]; then
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000041 echo "Usage: $(basename "$0") <BIOS image file>"
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000042 exit 0
43fi
44
45IMAGE="$1"
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000046if [ ! -f "$IMAGE" ]; then
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000047 echo "The image file $IMAGE does not exist"
48 exit 1
49fi
50
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000051if [ -z "$2" ]; then
52 DEV_SEL="1" # by default, select primary device
53else
54 DEV_SEL="$2"
55fi
56
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000057# Turn off the Host if it is currently ON
58chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
59echo "--- Current Chassis State: $chassisstate"
60if [ "$chassisstate" == 'On' ];
61then
62 echo "--- Turning the Chassis off"
63 obmcutil chassisoff
64 sleep 10
65 # Check if HOST was OFF
66 chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
67 if [ "$chassisstate_off" == 'On' ];
68 then
69 echo "--- Error : Failed turning the Chassis off"
70 exit 1
71 fi
72fi
73
74# Switch the host SPI bus to BMC"
75echo "--- Switch the host SPI bus to BMC."
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000076if ! gpioset 0 226=0; then
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000077 echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state"
78 exit 1
79fi
80
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000081# Switch the host SPI bus (between primary and secondary)
82# 227 is BMC_SPI0_BACKUP_SEL
83if [[ $DEV_SEL == 1 ]]; then
84 echo "Run update primary Host SPI-NOR"
85 gpioset 0 227=0 # Primary SPI
86elif [[ $DEV_SEL == 2 ]]; then
87 echo "Run update secondary Host SPI-NOR"
88 gpioset 0 227=1 # Second SPI
89else
90 echo "Please choose primary SPI (1) or second SPI (2)"
91 exit 0
92fi
93
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000094# Flash the firmware
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000095do_flash
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000096
97# Switch the host SPI bus to HOST."
98echo "--- Switch the host SPI bus to HOST."
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000099if ! gpioset 0 226=1; then
Thang Q. Nguyen25254b72021-01-07 07:16:50 +0000100 echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state"
101 exit 1
102fi
103
104if [ "$chassisstate" == 'On' ];
105then
106 sleep 5
107 echo "Turn on the Host"
Thang Q. Nguyen509991d2021-07-20 15:48:50 +0700108 obmcutil poweron
Thang Q. Nguyen25254b72021-01-07 07:16:50 +0000109fi