blob: 3a215fa50c0c6b6c5daf00cb0a0a23116d2b2a18 [file] [log] [blame]
George Hung85d9f2a2020-05-20 17:22:25 +08001#!/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
20SPI_SW_SELECT=169
21
22# Kernel control string for bind/unbind
23KERNEL_FIU_ID="c0000000.fiu"
24
25# Kernel sysfs path for bind/unbind
26KERNEL_SYSFS_FIU="/sys/bus/platform/drivers/NPCM-FIU"
27
28IMAGE_FILE="/tmp/image-bios"
29
30# Taken from /run/initramfs/update
31# Given label name, return mtd node. e.g. `findmtd bmc` returns 'mtd0'
32findmtd() {
33 m=$(grep -xl "$1" /sys/class/mtd/*/name)
34 m=${m%/name}
35 m=${m##*/}
36 echo $m
37}
38
39cleanup() {
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}
46trap cleanup EXIT SIGHUP SIGINT SIGTERM
47
48main() {
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
82return 0 2>/dev/null
83
84main "$@"