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 |
Thang Q. Nguyen | 0401a58 | 2022-07-05 13:25:08 +0000 | [diff] [blame^] | 22 | # 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. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 30 | # If the PNOR partition is not available, then bind again driver |
| 31 | echo "--- Bind the ASpeed SMC driver" |
Thang Q. Nguyen | 9b2186a | 2022-06-27 05:24:25 +0000 | [diff] [blame] | 32 | echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/bind |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 33 | sleep 2 |
| 34 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 35 | 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] | 36 | if [ -z "$HOST_MTD" ]; |
| 37 | then |
| 38 | echo "Fail to probe Host SPI-NOR device" |
| 39 | exit 1 |
| 40 | fi |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 41 | fi |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 42 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 43 | echo "--- Flashing firmware to @/dev/$HOST_MTD" |
| 44 | flashcp -v "$IMAGE" /dev/"$HOST_MTD" |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
| 48 | if [ $# -eq 0 ]; then |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 49 | echo "Usage: $(basename "$0") <BIOS image file>" |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 50 | exit 0 |
| 51 | fi |
| 52 | |
| 53 | IMAGE="$1" |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 54 | if [ ! -f "$IMAGE" ]; then |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 55 | echo "The image file $IMAGE does not exist" |
| 56 | exit 1 |
| 57 | fi |
| 58 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 59 | if [ -z "$2" ]; then |
| 60 | DEV_SEL="1" # by default, select primary device |
| 61 | else |
| 62 | DEV_SEL="$2" |
| 63 | fi |
| 64 | |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 65 | # Turn off the Host if it is currently ON |
| 66 | chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}') |
| 67 | echo "--- Current Chassis State: $chassisstate" |
| 68 | if [ "$chassisstate" == 'On' ]; |
| 69 | then |
| 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 |
| 80 | fi |
| 81 | |
| 82 | # Switch the host SPI bus to BMC" |
| 83 | echo "--- Switch the host SPI bus to BMC." |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 84 | if ! gpioset 0 226=0; then |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 85 | echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state" |
| 86 | exit 1 |
| 87 | fi |
| 88 | |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 89 | # Switch the host SPI bus (between primary and secondary) |
| 90 | # 227 is BMC_SPI0_BACKUP_SEL |
| 91 | if [[ $DEV_SEL == 1 ]]; then |
| 92 | echo "Run update primary Host SPI-NOR" |
| 93 | gpioset 0 227=0 # Primary SPI |
| 94 | elif [[ $DEV_SEL == 2 ]]; then |
| 95 | echo "Run update secondary Host SPI-NOR" |
| 96 | gpioset 0 227=1 # Second SPI |
| 97 | else |
| 98 | echo "Please choose primary SPI (1) or second SPI (2)" |
| 99 | exit 0 |
| 100 | fi |
| 101 | |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 102 | # Flash the firmware |
Thang Q. Nguyen | e96820d | 2021-12-17 02:24:24 +0000 | [diff] [blame] | 103 | do_flash |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 104 | |
| 105 | # Switch the host SPI bus to HOST." |
| 106 | echo "--- Switch the host SPI bus to HOST." |
Thang Q. Nguyen | dde1fed | 2021-11-04 08:30:27 +0000 | [diff] [blame] | 107 | if ! gpioset 0 226=1; then |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 108 | echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state" |
| 109 | exit 1 |
| 110 | fi |
| 111 | |
| 112 | if [ "$chassisstate" == 'On' ]; |
| 113 | then |
| 114 | sleep 5 |
| 115 | echo "Turn on the Host" |
Thang Q. Nguyen | 509991d | 2021-07-20 15:48:50 +0700 | [diff] [blame] | 116 | obmcutil poweron |
Thang Q. Nguyen | 25254b7 | 2021-01-07 07:16:50 +0000 | [diff] [blame] | 117 | fi |