Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 1 | #!/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 | |
| 17 | do_flash () { |
| 18 | # Check the PNOR partition available |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 19 | HOST_MTD=$(< /proc/mtd grep "pnor-uefi" | sed -n 's/^\(.*\):.*/\1/p') |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 20 | 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. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 27 | HOST_MTD=$(< /proc/mtd grep "pnor-uefi" | sed -n 's/^\(.*\):.*/\1/p') |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 28 | if [ -z "$HOST_MTD" ]; |
| 29 | then |
| 30 | echo "Fail to probe Host SPI-NOR device" |
| 31 | exit 1 |
| 32 | fi |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 33 | fi |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 34 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 35 | echo "--- Flashing firmware to @/dev/$HOST_MTD" |
| 36 | flashcp -v "$IMAGE" /dev/"$HOST_MTD" |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
| 40 | if [ $# -eq 0 ]; then |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 41 | echo "Usage: $(basename "$0") <BIOS image file>" |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 42 | exit 0 |
| 43 | fi |
| 44 | |
| 45 | IMAGE="$1" |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 46 | if [ ! -f "$IMAGE" ]; then |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 47 | echo "The image file $IMAGE does not exist" |
| 48 | exit 1 |
| 49 | fi |
| 50 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 51 | if [ -z "$2" ]; then |
| 52 | DEV_SEL="1" # by default, select primary device |
| 53 | else |
| 54 | DEV_SEL="$2" |
| 55 | fi |
| 56 | |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 57 | # Turn off the Host if it is currently ON |
| 58 | chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}') |
| 59 | echo "--- Current Chassis State: $chassisstate" |
| 60 | if [ "$chassisstate" == 'On' ]; |
| 61 | then |
| 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 |
| 72 | fi |
| 73 | |
| 74 | # Switch the host SPI bus to BMC" |
| 75 | echo "--- Switch the host SPI bus to BMC." |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 76 | if ! gpioset 0 226=0; then |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 77 | echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state" |
| 78 | exit 1 |
| 79 | fi |
| 80 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 81 | # Switch the host SPI bus (between primary and secondary) |
| 82 | # 227 is BMC_SPI0_BACKUP_SEL |
| 83 | if [[ $DEV_SEL == 1 ]]; then |
| 84 | echo "Run update primary Host SPI-NOR" |
| 85 | gpioset 0 227=0 # Primary SPI |
| 86 | elif [[ $DEV_SEL == 2 ]]; then |
| 87 | echo "Run update secondary Host SPI-NOR" |
| 88 | gpioset 0 227=1 # Second SPI |
| 89 | else |
| 90 | echo "Please choose primary SPI (1) or second SPI (2)" |
| 91 | exit 0 |
| 92 | fi |
| 93 | |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 94 | # Flash the firmware |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 95 | do_flash |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 96 | |
| 97 | # Switch the host SPI bus to HOST." |
| 98 | echo "--- Switch the host SPI bus to HOST." |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 99 | if ! gpioset 0 226=1; then |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 100 | echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state" |
| 101 | exit 1 |
| 102 | fi |
| 103 | |
| 104 | if [ "$chassisstate" == 'On' ]; |
| 105 | then |
| 106 | sleep 5 |
| 107 | echo "Turn on the Host" |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 108 | obmcutil poweron |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 109 | fi |