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 |
| 54 | echo "Usage: $(basename "$0") <UEFI/EDKII image file>" |
| 55 | exit 0 |
| 56 | fi |
| 57 | |
| 58 | IMAGE="$1" |
| 59 | if [ ! -f "$IMAGE" ]; then |
| 60 | echo "The image file $IMAGE does not exist" |
| 61 | exit 1 |
| 62 | fi |
| 63 | |
| 64 | if [ -z "$2" ]; then |
| 65 | DEV_SEL="1" # by default, select primary device |
| 66 | else |
| 67 | DEV_SEL="$2" |
| 68 | fi |
| 69 | |
| 70 | # Turn off the Host if it is currently ON |
| 71 | chassisstate=$(obmcutil chassisstate | awk -F. '{print $NF}') |
| 72 | echo "--- Current Chassis State: $chassisstate" |
| 73 | if [ "$chassisstate" == 'On' ]; |
| 74 | then |
| 75 | echo "--- Turning the Chassis off" |
| 76 | obmcutil chassisoff |
| 77 | sleep 10 |
| 78 | # Check if HOST was OFF |
| 79 | chassisstate_off=$(obmcutil chassisstate | awk -F. '{print $NF}') |
| 80 | if [ "$chassisstate_off" == 'On' ]; |
| 81 | then |
| 82 | echo "--- Error : Failed turning the Chassis off" |
| 83 | exit 1 |
| 84 | fi |
| 85 | fi |
| 86 | |
| 87 | # Switch the host SPI bus to BMC" |
| 88 | echo "--- Switch the host SPI bus to BMC." |
| 89 | if ! gpioset $(gpiofind spi0-program-sel)=1; then |
| 90 | echo "ERROR: Switch the host SPI bus to BMC. Please check gpio state" |
| 91 | exit 1 |
| 92 | fi |
| 93 | |
| 94 | # Switch the host SPI bus (between primary and secondary) |
| 95 | # 183 is BMC_GPIOW7_SPI0_BACKUP_SEL |
| 96 | if [[ $DEV_SEL == 1 ]]; then |
| 97 | echo "Run update Primary Host SPI-NOR" |
| 98 | gpioset $(gpiofind spi0-backup-sel)=1 # Primary SPI |
| 99 | elif [[ $DEV_SEL == 2 ]]; then |
| 100 | echo "Run update Second Host SPI-NOR" |
| 101 | gpioset $(gpiofind spi0-backup-sel)=0 # Second SPI |
| 102 | else |
| 103 | echo "Please choose primary SPI (1) or second SPI (2)" |
| 104 | exit 0 |
| 105 | fi |
| 106 | |
| 107 | # Flash the firmware |
| 108 | do_flash |
| 109 | |
| 110 | # Switch the SPI bus to the primary spi device |
| 111 | echo "Switch to the Primary Host SPI-NOR" |
| 112 | gpioset $(gpiofind spi0-backup-sel)=1 # Primary SPI |
| 113 | |
| 114 | # Switch the host SPI bus to HOST." |
| 115 | echo "--- Switch the host SPI bus to HOST." |
| 116 | if ! gpioset $(gpiofind spi0-program-sel)=0; then |
| 117 | echo "ERROR: Switch the host SPI bus to HOST. Please check gpio state" |
| 118 | exit 1 |
| 119 | fi |
| 120 | |
| 121 | if [ "$chassisstate" == 'On' ]; |
| 122 | then |
| 123 | sleep 5 |
| 124 | echo "Turn on the Host" |
| 125 | obmcutil poweron |
| 126 | fi |