blob: bf71e9452cd31e5f86a5e795e8e19305b1326dc7 [file] [log] [blame]
George Hungc1989592021-06-02 10:53:27 +08001#!/bin/bash
George Hungf089ba12020-05-20 17:22:25 +08002# 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
20SPI_SW_SELECT=169
21
22# Kernel control string for bind/unbind
George Hungc1989592021-06-02 10:53:27 +080023KERNEL_FIU_ID="c0000000.spi"
George Hungf089ba12020-05-20 17:22:25 +080024
25# Kernel sysfs path for bind/unbind
26KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU"
27
George Hungc1989592021-06-02 10:53:27 +080028# 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
31for fname in $(find ${KERNEL_SYSFS_FIU} -type l)
32do
33 if [ "${fname##*\.}" == "fiu" ]
34 then
35 KERNEL_FIU_ID="c0000000.fiu"
36 break
37 fi
38done
39
George Hungf089ba12020-05-20 17:22:25 +080040IMAGE_FILE="/tmp/image-bios"
41
42# Taken from /run/initramfs/update
43# Given label name, return mtd node. e.g. `findmtd bmc` returns 'mtd0'
44findmtd() {
45 m=$(grep -xl "$1" /sys/class/mtd/*/name)
46 m=${m%/name}
47 m=${m##*/}
48 echo $m
49}
50
51cleanup() {
52 if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
53 echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
54 fi
55 echo low > /sys/class/gpio/gpio${SPI_SW_SELECT}/direction # Switch mux to host
56 rm -f ${IMAGE_FILE}
57}
58trap cleanup EXIT SIGHUP SIGINT SIGTERM
59
60main() {
61 if [ ! -f ${IMAGE_FILE} ]; then
62 echo "Invalid bios image file!"
63 exit 1
64 fi
65
66 echo "Starting bios update..."
67 if [ ! -d "/sys/class/gpio/gpio${SPI_SW_SELECT}" ]; then
68 echo "${SPI_SW_SELECT}" > /sys/class/gpio/export
69 fi
70
71 echo high > /sys/class/gpio/gpio${SPI_SW_SELECT}/direction # Switch mux to BMC
72
73 if [ -d "${KERNEL_SYSFS_FIU}/${KERNEL_FIU_ID}" ]; then
74 echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/unbind
75 fi
76 echo "${KERNEL_FIU_ID}" > "${KERNEL_SYSFS_FIU}"/bind
77
GeorgeHuang6e124702021-11-24 16:37:21 +080078 # BIOS flash is labelled 'bios-primary'
79 bios_mtd=$(findmtd bios-primary)
80 if [ -z "${bios_mtd}" ]; then
George Hungf089ba12020-05-20 17:22:25 +080081 echo "Cannot find bios flash mtd partition!"
82 exit 1
83 fi
84
GeorgeHuang6e124702021-11-24 16:37:21 +080085 flashcp -v $IMAGE_FILE /dev/"${bios_mtd}"
George Hungf089ba12020-05-20 17:22:25 +080086 if [ $? -eq 0 ]; then
87 echo "bios update successfully..."
88 else
89 echo "bios update failed..."
90 exit 1
91 fi
92}
93# Exit without running main() if sourced
94return 0 2>/dev/null
95
96main "$@"