George Hung | c198959 | 2021-06-02 10:53:27 +0800 | [diff] [blame] | 1 | #!/bin/bash |
George Hung | f089ba1 | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 2 | # Copyright 2020 Google LLC |
| 3 | # Copyright 2020 Quanta Computer Inc. |
| 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 | # Global variables |
| 18 | |
| 19 | # GPIO to control the host SPI mux |
| 20 | SPI_SW_SELECT=169 |
| 21 | |
| 22 | # Kernel control string for bind/unbind |
George Hung | c198959 | 2021-06-02 10:53:27 +0800 | [diff] [blame] | 23 | KERNEL_FIU_ID="c0000000.spi" |
George Hung | f089ba1 | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 24 | |
| 25 | # Kernel sysfs path for bind/unbind |
| 26 | KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU" |
| 27 | |
George Hung | c198959 | 2021-06-02 10:53:27 +0800 | [diff] [blame] | 28 | # the node of FIU is spi for kernel 5.10, but |
| 29 | # for less than or equal kernel 5.4, the node |
| 30 | # is fiu |
Patrick Williams | 8c22623 | 2023-04-15 20:05:21 -0500 | [diff] [blame] | 31 | if ls "$KERNEL_SYSFS_FIU"/*.fiu 1> /dev/null 2>&1; then |
| 32 | KERNEL_FIU_ID="c0000000.fiu" |
| 33 | fi |
George Hung | c198959 | 2021-06-02 10:53:27 +0800 | [diff] [blame] | 34 | |
George Hung | f089ba1 | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 35 | IMAGE_FILE="/tmp/image-bios" |
| 36 | |
| 37 | # Taken from /run/initramfs/update |
| 38 | # Given label name, return mtd node. e.g. `findmtd bmc` returns 'mtd0' |
| 39 | findmtd() { |
| 40 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 41 | m=${m%/name} |
| 42 | m=${m##*/} |
Patrick Williams | 8c22623 | 2023-04-15 20:05:21 -0500 | [diff] [blame] | 43 | echo "$m" |
George Hung | f089ba1 | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | cleanup() { |
| 47 | if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then |
| 48 | echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind |
| 49 | fi |
| 50 | echo low > /sys/class/gpio/gpio${SPI_SW_SELECT}/direction # Switch mux to host |
| 51 | rm -f ${IMAGE_FILE} |
| 52 | } |
| 53 | trap cleanup EXIT SIGHUP SIGINT SIGTERM |
| 54 | |
| 55 | main() { |
| 56 | if [ ! -f ${IMAGE_FILE} ]; then |
| 57 | echo "Invalid bios image file!" |
| 58 | exit 1 |
| 59 | fi |
| 60 | |
| 61 | echo "Starting bios update..." |
| 62 | if [ ! -d "/sys/class/gpio/gpio${SPI_SW_SELECT}" ]; then |
| 63 | echo "${SPI_SW_SELECT}" > /sys/class/gpio/export |
| 64 | fi |
| 65 | |
| 66 | echo high > /sys/class/gpio/gpio${SPI_SW_SELECT}/direction # Switch mux to BMC |
| 67 | |
| 68 | if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then |
| 69 | echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind |
| 70 | fi |
| 71 | echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/bind |
| 72 | |
GeorgeHuang | 6e12470 | 2021-11-24 16:37:21 +0800 | [diff] [blame] | 73 | # BIOS flash is labelled 'bios-primary' |
| 74 | bios_mtd=$(findmtd bios-primary) |
| 75 | if [ -z "${bios_mtd}" ]; then |
George Hung | f089ba1 | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 76 | echo "Cannot find bios flash mtd partition!" |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
Patrick Williams | 8c22623 | 2023-04-15 20:05:21 -0500 | [diff] [blame] | 80 | if flashcp -v $IMAGE_FILE /dev/"${bios_mtd}" ; then |
George Hung | f089ba1 | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 81 | echo "bios update successfully..." |
| 82 | else |
| 83 | echo "bios update failed..." |
| 84 | exit 1 |
| 85 | fi |
| 86 | } |
| 87 | # Exit without running main() if sourced |
Patrick Williams | 8c22623 | 2023-04-15 20:05:21 -0500 | [diff] [blame] | 88 | if ! (return 0 2>/dev/null); then |
| 89 | main "$@" |
| 90 | fi |