Chanh Nguyen | b50fedb | 2022-10-24 15:29:56 +0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # This script is used to flash the UEFI/EDKII |
| 4 | |
| 5 | # Syntax: ampere_flash_bios.sh $image_file $device_sellect |
| 6 | # Where: |
| 7 | # $image_file : the image binary file |
| 8 | # $device_sellect : 1 - Host Main SPI Nor |
| 9 | # 2 - Host Second SPI Nor |
| 10 | |
| 11 | # Author : Chanh Nguyen (chnguyen@amperecomputing.com) |
| 12 | |
| 13 | # Note: |
| 14 | # BMC_GPIOW6_SPI0_PROGRAM_SEL (GPIO 182): 1 => BMC owns SPI bus for upgrading |
| 15 | # 0 => HOST owns SPI bus for upgrading |
| 16 | |
| 17 | # BMC_GPIOW7_SPI0_BACKUP_SEL (GPIO 183) : 1 => to switch SPI_CS0_L to primary SPI Nor device |
| 18 | # 0 => to switch SPI_CS0_L to second SPI Nor device |
| 19 | |
| 20 | # shellcheck disable=SC2046 |
| 21 | |
| 22 | do_flash () { |
| 23 | # Check the HNOR partition available |
| 24 | HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p') |
| 25 | if [ -z "$HOST_MTD" ]; |
| 26 | then |
| 27 | # Check the ASpeed SMC driver binded before |
| 28 | HOST_SPI=/sys/bus/platform/drivers/spi-aspeed-smc/1e630000.spi |
| 29 | if [ -d "$HOST_SPI" ]; then |
| 30 | echo "Unbind the ASpeed SMC driver" |
| 31 | echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/unbind |
| 32 | sleep 2 |
| 33 | fi |
| 34 | |
| 35 | # If the HNOR partition is not available, then bind again driver |
| 36 | echo "--- Bind the ASpeed SMC driver" |
| 37 | echo 1e630000.spi > /sys/bus/platform/drivers/spi-aspeed-smc/bind |
| 38 | sleep 2 |
| 39 | |
| 40 | HOST_MTD=$(< /proc/mtd grep "pnor" | sed -n 's/^\(.*\):.*/\1/p') |
| 41 | if [ -z "$HOST_MTD" ]; |
| 42 | then |
| 43 | echo "Fail to probe Host SPI-NOR device" |
| 44 | exit 1 |
| 45 | fi |
| 46 | fi |
| 47 | |
| 48 | echo "--- Flashing firmware image $IMAGE to @/dev/$HOST_MTD" |
| 49 | flashcp -v "$IMAGE" /dev/"$HOST_MTD" |
| 50 | } |
| 51 | |
| 52 | |
| 53 | if [ $# -eq 0 ]; then |
Hieu Huynh | 7fc10f7 | 2023-07-29 15:39:51 +0000 | [diff] [blame] | 54 | echo "Usage: $(basename "$0") <UEFI/EDKII image file> <DEV_SEL> [SPECIAL_BOOT]" |
| 55 | echo "Where:" |
| 56 | echo " DEV_SEL 1 is Primary SPI (by default), 2 is Second SPI" |
| 57 | echo " SPECIAL_BOOT: Optional, input '1' to enter & flash SPECIAL_BOOT mode. Default: 0" |
Chanh Nguyen | b50fedb | 2022-10-24 15:29:56 +0700 | [diff] [blame] | 58 | exit 0 |
| 59 | fi |
| 60 | |
| 61 | IMAGE="$1" |
| 62 | if [ ! -f "$IMAGE" ]; then |
| 63 | echo "The image file $IMAGE does not exist" |
| 64 | exit 1 |
| 65 | fi |
| 66 | |
| 67 | if [ -z "$2" ]; then |
| 68 | DEV_SEL="1" # by default, select primary device |
| 69 | else |
| 70 | DEV_SEL="$2" |
| 71 | fi |
| 72 | |
Hieu Huynh | 7fc10f7 | 2023-07-29 15:39:51 +0000 | [diff] [blame] | 73 | SPECIAL_BOOT=0 |
| 74 | if [[ "$3" == "1" ]]; then |
| 75 | SPECIAL_BOOT=1 |
| 76 | fi |
| 77 | |
| 78 | echo "SPECIAL_BOOT mode: $SPECIAL_BOOT" |
Chanh Nguyen | b50fedb | 2022-10-24 15:29:56 +0700 | [diff] [blame] | 79 | # Turn off the Host if it is currently ON |
| 80 | chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}') |
| 81 | echo "--- Current Chassis State: $chassisstate" |
| 82 | if [ "$chassisstate" == 'On' ]; |
| 83 | then |
| 84 | echo "--- Turning the Chassis off" |
| 85 | obmcutil chassisoff |
| 86 | sleep 10 |
| 87 | # Check if HOST was OFF |
| 88 | chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}') |
| 89 | if [ "$chassisstate_off" == 'On' ]; |
| 90 | then |
| 91 | echo "--- Error : Failed turning the Chassis off" |
| 92 | exit 1 |
| 93 | fi |
| 94 | fi |
| 95 | |
| 96 | # Switch the host SPI bus to BMC" |
| 97 | echo "--- Switch the host SPI bus to BMC." |
| 98 | if ! gpioset $(gpiofind spi0-program-sel)=1; then |
| 99 | echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state" |
| 100 | exit 1 |
| 101 | fi |
| 102 | |
| 103 | # Switch the host SPI bus (between primary and secondary) |
| 104 | # 183 is BMC_GPIOW7_SPI0_BACKUP_SEL |
| 105 | if [[ $DEV_SEL == 1 ]]; then |
| 106 | echo "Run update Primary Host SPI-NOR" |
| 107 | gpioset $(gpiofind spi0-backup-sel)=1 # Primary SPI |
| 108 | elif [[ $DEV_SEL == 2 ]]; then |
| 109 | echo "Run update Second Host SPI-NOR" |
| 110 | gpioset $(gpiofind spi0-backup-sel)=0 # Second SPI |
| 111 | else |
| 112 | echo "Please choose primary SPI (1) or second SPI (2)" |
| 113 | exit 0 |
| 114 | fi |
| 115 | |
Hieu Huynh | 7fc10f7 | 2023-07-29 15:39:51 +0000 | [diff] [blame] | 116 | # Restrict to flash Second Host SPI-NOR in case of SPECIAL_BOOT |
| 117 | if [ $SPECIAL_BOOT == 1 ] && [ "$DEV_SEL" == 2 ]; then |
| 118 | echo "Not allow to flash the Second Host SPI-NOR with SPECIAL_BOOT image" |
| 119 | exit |
| 120 | fi |
| 121 | |
Chanh Nguyen | b50fedb | 2022-10-24 15:29:56 +0700 | [diff] [blame] | 122 | # Flash the firmware |
| 123 | do_flash |
| 124 | |
Hieu Huynh | 7fc10f7 | 2023-07-29 15:39:51 +0000 | [diff] [blame] | 125 | # Assert SPECIAL_BOOT GPIO PIN |
| 126 | if [[ $SPECIAL_BOOT == 1 ]]; then |
| 127 | gpioset $(gpiofind host0-special-boot)=1 |
| 128 | fi |
| 129 | |
Chanh Nguyen | b50fedb | 2022-10-24 15:29:56 +0700 | [diff] [blame] | 130 | # Switch the SPI bus to the primary spi device |
| 131 | echo "Switch to the Primary Host SPI-NOR" |
| 132 | gpioset $(gpiofind spi0-backup-sel)=1 # Primary SPI |
| 133 | |
| 134 | # Switch the host SPI bus to HOST." |
| 135 | echo "--- Switch the host SPI bus to HOST." |
| 136 | if ! gpioset $(gpiofind spi0-program-sel)=0; then |
| 137 | echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state" |
| 138 | exit 1 |
| 139 | fi |
| 140 | |
Hieu Huynh | 7fc10f7 | 2023-07-29 15:39:51 +0000 | [diff] [blame] | 141 | if [ "$chassisstate" == 'On' ] || [ $SPECIAL_BOOT == 1 ]; |
Chanh Nguyen | b50fedb | 2022-10-24 15:29:56 +0700 | [diff] [blame] | 142 | then |
| 143 | sleep 5 |
| 144 | echo "Turn on the Host" |
| 145 | obmcutil poweron |
| 146 | fi |
Hieu Huynh | 7fc10f7 | 2023-07-29 15:39:51 +0000 | [diff] [blame] | 147 | |
| 148 | # Deassert SPECIAL_BOOT GPIO PIN if it is being asserted |
| 149 | if [[ $SPECIAL_BOOT == 1 ]]; then |
| 150 | # Time out checking for Host ON is 60s |
| 151 | cnt=12 |
| 152 | while [ "$cnt" -gt 0 ]; |
| 153 | do |
| 154 | cnt=$((cnt - 1)) |
| 155 | if systemctl status obmc-host-already-on@0.target | grep "Active: active"; then |
| 156 | echo "Deassert SPECIAL_BOOT GPIO PIN if it is being asserted." |
| 157 | gpioset $(gpiofind host0-special-boot)=0 |
| 158 | exit 0 |
| 159 | fi |
| 160 | sleep 5 |
| 161 | done |
| 162 | fi |