Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | POWER_CMD="/usr/sbin/power-util mb" |
| 6 | IMAGE_FILE=$1/bios.bin |
| 7 | GPIO=389 |
| 8 | |
| 9 | IPMB_OBJ="xyz.openbmc_project.Ipmi.Channel.Ipmb" |
| 10 | IPMB_PATH="/xyz/openbmc_project/Ipmi/Channel/Ipmb" |
| 11 | IPMB_INTF="org.openbmc.Ipmb" |
| 12 | IPMB_CALL="sendRequest yyyyay" |
| 13 | ME_CMD_RECOVER="1 0x2e 0 0xdf 4 0x57 0x01 0x00 0x01" |
| 14 | ME_CMD_RESET="1 6 0 0x2 0" |
| 15 | SPI_DEV="1e630000.spi" |
| 16 | SPI_PATH="/sys/bus/platform/drivers/aspeed-smc" |
| 17 | |
| 18 | set_gpio_to_bmc() |
| 19 | { |
| 20 | echo "switch bios GPIO to bmc" |
| 21 | if [ ! -d /sys/class/gpio/gpio$GPIO ]; then |
| 22 | cd /sys/class/gpio |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 23 | echo $GPIO > "export" |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 24 | cd gpio$GPIO |
| 25 | else |
| 26 | cd /sys/class/gpio/gpio$GPIO |
| 27 | fi |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 28 | direc=$(cat direction) |
| 29 | if [ "$direc" == "in" ]; then |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 30 | echo "out" > direction |
| 31 | fi |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 32 | data=$(cat value) |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 33 | if [ "$data" == "0" ]; then |
| 34 | echo 1 > value |
| 35 | fi |
| 36 | return 0 |
| 37 | } |
| 38 | |
| 39 | set_gpio_to_pch() |
| 40 | { |
| 41 | echo "switch bios GPIO to pch" |
| 42 | if [ ! -d /sys/class/gpio/gpio$GPIO ]; then |
| 43 | cd /sys/class/gpio |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 44 | echo $GPIO > "export" |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 45 | cd gpio$GPIO |
| 46 | else |
| 47 | cd /sys/class/gpio/gpio$GPIO |
| 48 | fi |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 49 | direc=$(cat direction) |
| 50 | if [ "$direc" == "in" ]; then |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 51 | echo "out" > direction |
| 52 | fi |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 53 | data=$(cat value) |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 54 | if [ "$data" == "1" ]; then |
| 55 | echo 0 > value |
| 56 | fi |
| 57 | echo "in" > direction |
| 58 | echo $GPIO > /sys/class/gpio/unexport |
| 59 | return 0 |
| 60 | } |
| 61 | |
| 62 | echo "Bios upgrade started at $(date)" |
| 63 | |
| 64 | #Power off host server. |
| 65 | echo "Power off host server" |
| 66 | $POWER_CMD off |
| 67 | sleep 15 |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 68 | if [ "$($POWER_CMD status)" != "off" ]; |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 69 | then |
| 70 | echo "Host server didn't power off" |
| 71 | echo "Bios upgrade failed" |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 72 | exit 1 |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 73 | fi |
| 74 | echo "Host server powered off" |
| 75 | |
| 76 | #Set ME to recovery mode |
| 77 | echo "Set ME to recovery mode" |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 78 | # shellcheck disable=SC2086 |
| 79 | busctl call "$IPMB_OBJ" "$IPMB_PATH" "$IPMB_INTF" $IPMB_CALL $ME_CMD_RECOVER |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 80 | sleep 5 |
| 81 | |
| 82 | #Flip GPIO to access SPI flash used by host. |
| 83 | echo "Set GPIO $GPIO to access SPI flash from BMC used by host" |
| 84 | set_gpio_to_bmc |
| 85 | |
| 86 | #Bind spi driver to access flash |
| 87 | echo "bind aspeed-smc spi driver" |
| 88 | echo -n $SPI_DEV > $SPI_PATH/bind |
| 89 | sleep 1 |
| 90 | |
| 91 | #Flashcp image to device. |
| 92 | if [ -e "$IMAGE_FILE" ]; |
| 93 | then |
| 94 | echo "Bios image is $IMAGE_FILE" |
| 95 | for d in mtd6 mtd7 ; do |
| 96 | if [ -e "/dev/$d" ]; then |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 97 | mtd=$(cat /sys/class/mtd/$d/name) |
| 98 | if [ "$mtd" == "pnor" ]; then |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 99 | echo "Flashing bios image to $d..." |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 100 | if flashcp -v "$IMAGE_FILE" /dev/$d; then |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 101 | echo "bios updated successfully..." |
| 102 | else |
| 103 | echo "bios update failed..." |
| 104 | fi |
| 105 | break |
| 106 | fi |
| 107 | echo "$d is not a pnor device" |
| 108 | fi |
| 109 | echo "$d not available" |
| 110 | done |
| 111 | else |
| 112 | echo "Bios image $IMAGE_FILE doesn't exist" |
| 113 | fi |
| 114 | |
| 115 | #Unbind spi driver |
| 116 | sleep 1 |
| 117 | echo "Unbind aspeed-smc spi driver" |
| 118 | echo -n $SPI_DEV > $SPI_PATH/unbind |
| 119 | sleep 10 |
| 120 | |
| 121 | #Flip GPIO back for host to access SPI flash |
| 122 | echo "Set GPIO $GPIO back for host to access SPI flash" |
| 123 | set_gpio_to_pch |
| 124 | sleep 5 |
| 125 | |
| 126 | #Reset ME to boot from new bios |
| 127 | echo "Reset ME to boot from new bios" |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 128 | # shellcheck disable=SC2086 |
| 129 | busctl call "$IPMB_OBJ" "$IPMB_PATH" "$IPMB_INTF" $IPMB_CALL $ME_CMD_RESET |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 130 | sleep 10 |
| 131 | |
| 132 | #Power on server |
| 133 | echo "Power on server" |
| 134 | $POWER_CMD on |
| 135 | sleep 5 |
| 136 | |
| 137 | # Retry to power on once again if server didn't powered on |
Patrick Williams | 552e93e | 2021-10-27 10:15:20 -0500 | [diff] [blame] | 138 | if [ "$($POWER_CMD status)" != "on" ]; |
Vijay Khemka | 2d9fe0f | 2020-02-18 14:05:24 -0800 | [diff] [blame] | 139 | then |
| 140 | sleep 5 |
| 141 | echo "Powering on server again" |
| 142 | $POWER_CMD on |
| 143 | fi |