blob: 32a595f5b15977696af7c3f5c2bad048c37cf9ed [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
Patrick Williams8c226232023-04-15 20:05:21 -050031if ls "$KERNEL_SYSFS_FIU"/*.fiu 1> /dev/null 2>&1; then
32 KERNEL_FIU_ID="c0000000.fiu"
33fi
George Hungc1989592021-06-02 10:53:27 +080034
George Hungf089ba12020-05-20 17:22:25 +080035IMAGE_FILE="/tmp/image-bios"
36
37# Taken from /run/initramfs/update
38# Given label name, return mtd node. e.g. `findmtd bmc` returns 'mtd0'
39findmtd() {
40 m=$(grep -xl "$1" /sys/class/mtd/*/name)
41 m=${m%/name}
42 m=${m##*/}
Patrick Williams8c226232023-04-15 20:05:21 -050043 echo "$m"
George Hungf089ba12020-05-20 17:22:25 +080044}
45
46cleanup() {
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}
53trap cleanup EXIT SIGHUP SIGINT SIGTERM
54
55main() {
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
GeorgeHuang6e124702021-11-24 16:37:21 +080073 # BIOS flash is labelled 'bios-primary'
74 bios_mtd=$(findmtd bios-primary)
75 if [ -z "${bios_mtd}" ]; then
George Hungf089ba12020-05-20 17:22:25 +080076 echo "Cannot find bios flash mtd partition!"
77 exit 1
78 fi
79
Patrick Williams8c226232023-04-15 20:05:21 -050080 if flashcp -v $IMAGE_FILE /dev/"${bios_mtd}" ; then
George Hungf089ba12020-05-20 17:22:25 +080081 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 Williams8c226232023-04-15 20:05:21 -050088if ! (return 0 2>/dev/null); then
89 main "$@"
90fi