blob: 4b99f0be4ade9662eb40d03216230640c365084a [file] [log] [blame]
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -08001#!/bin/bash
2
3set -e
4
5POWER_CMD="/usr/sbin/power-util mb"
6IMAGE_FILE=$1/bios.bin
7GPIO=389
8
9IPMB_OBJ="xyz.openbmc_project.Ipmi.Channel.Ipmb"
10IPMB_PATH="/xyz/openbmc_project/Ipmi/Channel/Ipmb"
11IPMB_INTF="org.openbmc.Ipmb"
12IPMB_CALL="sendRequest yyyyay"
13ME_CMD_RECOVER="1 0x2e 0 0xdf 4 0x57 0x01 0x00 0x01"
14ME_CMD_RESET="1 6 0 0x2 0"
15SPI_DEV="1e630000.spi"
16SPI_PATH="/sys/bus/platform/drivers/aspeed-smc"
17
18set_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 Williams552e93e2021-10-27 10:15:20 -050023 echo $GPIO > "export"
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080024 cd gpio$GPIO
25 else
26 cd /sys/class/gpio/gpio$GPIO
27 fi
Patrick Williams552e93e2021-10-27 10:15:20 -050028 direc=$(cat direction)
29 if [ "$direc" == "in" ]; then
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080030 echo "out" > direction
31 fi
Patrick Williams552e93e2021-10-27 10:15:20 -050032 data=$(cat value)
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080033 if [ "$data" == "0" ]; then
34 echo 1 > value
35 fi
36 return 0
37}
38
39set_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 Williams552e93e2021-10-27 10:15:20 -050044 echo $GPIO > "export"
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080045 cd gpio$GPIO
46 else
47 cd /sys/class/gpio/gpio$GPIO
48 fi
Patrick Williams552e93e2021-10-27 10:15:20 -050049 direc=$(cat direction)
50 if [ "$direc" == "in" ]; then
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080051 echo "out" > direction
52 fi
Patrick Williams552e93e2021-10-27 10:15:20 -050053 data=$(cat value)
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080054 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
62echo "Bios upgrade started at $(date)"
63
64#Power off host server.
65echo "Power off host server"
66$POWER_CMD off
67sleep 15
Patrick Williams552e93e2021-10-27 10:15:20 -050068if [ "$($POWER_CMD status)" != "off" ];
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080069then
70 echo "Host server didn't power off"
71 echo "Bios upgrade failed"
Patrick Williams552e93e2021-10-27 10:15:20 -050072 exit 1
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080073fi
74echo "Host server powered off"
75
76#Set ME to recovery mode
77echo "Set ME to recovery mode"
Patrick Williams552e93e2021-10-27 10:15:20 -050078# shellcheck disable=SC2086
79busctl call "$IPMB_OBJ" "$IPMB_PATH" "$IPMB_INTF" $IPMB_CALL $ME_CMD_RECOVER
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080080sleep 5
81
82#Flip GPIO to access SPI flash used by host.
83echo "Set GPIO $GPIO to access SPI flash from BMC used by host"
84set_gpio_to_bmc
85
86#Bind spi driver to access flash
87echo "bind aspeed-smc spi driver"
88echo -n $SPI_DEV > $SPI_PATH/bind
89sleep 1
90
91#Flashcp image to device.
92if [ -e "$IMAGE_FILE" ];
93then
94 echo "Bios image is $IMAGE_FILE"
95 for d in mtd6 mtd7 ; do
96 if [ -e "/dev/$d" ]; then
Patrick Williams552e93e2021-10-27 10:15:20 -050097 mtd=$(cat /sys/class/mtd/$d/name)
98 if [ "$mtd" == "pnor" ]; then
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -080099 echo "Flashing bios image to $d..."
Patrick Williams552e93e2021-10-27 10:15:20 -0500100 if flashcp -v "$IMAGE_FILE" /dev/$d; then
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -0800101 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
111else
112 echo "Bios image $IMAGE_FILE doesn't exist"
113fi
114
115#Unbind spi driver
116sleep 1
117echo "Unbind aspeed-smc spi driver"
118echo -n $SPI_DEV > $SPI_PATH/unbind
119sleep 10
120
121#Flip GPIO back for host to access SPI flash
122echo "Set GPIO $GPIO back for host to access SPI flash"
123set_gpio_to_pch
124sleep 5
125
126#Reset ME to boot from new bios
127echo "Reset ME to boot from new bios"
Patrick Williams552e93e2021-10-27 10:15:20 -0500128# shellcheck disable=SC2086
129busctl call "$IPMB_OBJ" "$IPMB_PATH" "$IPMB_INTF" $IPMB_CALL $ME_CMD_RESET
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -0800130sleep 10
131
132#Power on server
133echo "Power on server"
134$POWER_CMD on
135sleep 5
136
137# Retry to power on once again if server didn't powered on
Patrick Williams552e93e2021-10-27 10:15:20 -0500138if [ "$($POWER_CMD status)" != "on" ];
Vijay Khemka2d9fe0f2020-02-18 14:05:24 -0800139then
140 sleep 5
141 echo "Powering on server again"
142 $POWER_CMD on
143fi