George Hung | 85d9f2a | 2020-05-20 17:22:25 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 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 |
| 23 | KERNEL_FIU_ID="c0000000.fiu" |
| 24 | |
| 25 | # Kernel sysfs path for bind/unbind |
| 26 | KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU" |
| 27 | |
| 28 | IMAGE_FILE="/tmp/image-bios" |
| 29 | |
| 30 | # Taken from /run/initramfs/update |
| 31 | # Given label name, return mtd node. e.g. `findmtd bmc` returns 'mtd0' |
| 32 | findmtd() { |
| 33 | m=$(grep -xl "$1" /sys/class/mtd/*/name) |
| 34 | m=${m%/name} |
| 35 | m=${m##*/} |
| 36 | echo $m |
| 37 | } |
| 38 | |
| 39 | cleanup() { |
| 40 | if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then |
| 41 | echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind |
| 42 | fi |
| 43 | echo low > /sys/class/gpio/gpio${SPI_SW_SELECT}/direction # Switch mux to host |
| 44 | rm -f ${IMAGE_FILE} |
| 45 | } |
| 46 | trap cleanup EXIT SIGHUP SIGINT SIGTERM |
| 47 | |
| 48 | main() { |
| 49 | if [ ! -f ${IMAGE_FILE} ]; then |
| 50 | echo "Invalid bios image file!" |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | echo "Starting bios update..." |
| 55 | if [ ! -d "/sys/class/gpio/gpio${SPI_SW_SELECT}" ]; then |
| 56 | echo "${SPI_SW_SELECT}" > /sys/class/gpio/export |
| 57 | fi |
| 58 | |
| 59 | echo high > /sys/class/gpio/gpio${SPI_SW_SELECT}/direction # Switch mux to BMC |
| 60 | |
| 61 | if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then |
| 62 | echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind |
| 63 | fi |
| 64 | echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/bind |
| 65 | |
| 66 | # BIOS flash is labelled 'pnor' |
| 67 | pnor_mtd=$(findmtd pnor) |
| 68 | if [ -z "${pnor_mtd}" ]; then |
| 69 | echo "Cannot find bios flash mtd partition!" |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | flashcp -v $IMAGE_FILE /dev/"${pnor_mtd}" |
| 74 | if [ $? -eq 0 ]; then |
| 75 | echo "bios update successfully..." |
| 76 | else |
| 77 | echo "bios update failed..." |
| 78 | exit 1 |
| 79 | fi |
| 80 | } |
| 81 | # Exit without running main() if sourced |
| 82 | return 0 2>/dev/null |
| 83 | |
| 84 | main "$@" |