blob: 7852dc4acca8d3e299de117be5e2989c4b8aa20f [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
Thang Q. Nguyen0401a582022-07-05 13:25:08 +000022 # Check the ASpeed SMC driver bound before
23 HOST_SPI=/sys/bus/platform/drivers/spi-aspeed-smc/1e630000.spi
24 if [ -d "$HOST_SPI" ]; then
25 echo "Unbind the ASpeed SMC driver"
26 echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/unbind
27 sleep 2
28 fi
29
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000030 # If the PNOR partition is not available, then bind again driver
31 echo "--- Bind the ASpeed SMC driver"
Thang Q. Nguyen9b2186a2022-06-27 05:24:25 +000032 echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/bind
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000033 sleep 2
34
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000035 HOST_MTD=$(< /proc/mtd grep "pnor-uefi" | sed -n 's/^\(.*\):.*/\1/p')
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000036 if [ -z "$HOST_MTD" ];
37 then
38 echo "Fail to probe Host SPI-NOR device"
39 exit 1
40 fi
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000041 fi
Thang Q. Nguyen509991d2021-07-20 15:48:50 +070042
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000043 echo "--- Flashing firmware to @/dev/$HOST_MTD"
44 flashcp -v "$IMAGE" /dev/"$HOST_MTD"
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000045}
46
47
48if [ $# -eq 0 ]; then
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000049 echo "Usage: $(basename "$0") <BIOS image file>"
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000050 exit 0
51fi
52
53IMAGE="$1"
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000054if [ ! -f "$IMAGE" ]; then
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000055 echo "The image file $IMAGE does not exist"
56 exit 1
57fi
58
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000059if [ -z "$2" ]; then
60 DEV_SEL="1" # by default, select primary device
61else
62 DEV_SEL="$2"
63fi
64
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000065# Turn off the Host if it is currently ON
66chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}')
67echo "--- Current Chassis State: $chassisstate"
68if [ "$chassisstate" == 'On' ];
69then
70 echo "--- Turning the Chassis off"
71 obmcutil chassisoff
72 sleep 10
73 # Check if HOST was OFF
74 chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}')
75 if [ "$chassisstate_off" == 'On' ];
76 then
77 echo "--- Error : Failed turning the Chassis off"
78 exit 1
79 fi
80fi
81
82# Switch the host SPI bus to BMC"
83echo "--- Switch the host SPI bus to BMC."
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +000084if ! gpioset 0 226=0; then
Thang Q. Nguyen25254b72021-01-07 07:16:50 +000085 echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state"
86 exit 1
87fi
88
Thang Q. Nguyene96820d2021-12-17 02:24:24 +000089# Switch the host SPI bus (between primary and secondary)
90# 227 is BMC_SPI0_BACKUP_SEL
91if [[ $DEV_SEL == 1 ]]; then
92 echo "Run update primary Host SPI-NOR"
93 gpioset 0 227=0 # Primary SPI
94elif [[ $DEV_SEL == 2 ]]; then
95 echo "Run update secondary Host SPI-NOR"
96 gpioset 0 227=1 # Second SPI
97else
98 echo "Please choose primary SPI (1) or second SPI (2)"
99 exit 0
100fi
101
Thang Q. Nguyen25254b72021-01-07 07:16:50 +0000102# Flash the firmware
Thang Q. Nguyene96820d2021-12-17 02:24:24 +0000103do_flash
Thang Q. Nguyen25254b72021-01-07 07:16:50 +0000104
105# Switch the host SPI bus to HOST."
106echo "--- Switch the host SPI bus to HOST."
Thang Q. Nguyendde1fed2021-11-04 08:30:27 +0000107if ! gpioset 0 226=1; then
Thang Q. Nguyen25254b72021-01-07 07:16:50 +0000108 echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state"
109 exit 1
110fi
111
112if [ "$chassisstate" == 'On' ];
113then
114 sleep 5
115 echo "Turn on the Host"
Thang Q. Nguyen509991d2021-07-20 15:48:50 +0700116 obmcutil poweron
Thang Q. Nguyen25254b72021-01-07 07:16:50 +0000117fi