1#!/bin/bash
2#
3# Copyright (c) 2021 Ampere Computing LLC
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# This script updates the EDKII / SCP firmware.
18# Author : Chanh Nguyen (chnguyen@amperecomputing.com)
19# Date : Sep 7, 2021
20# Modified:
21
22usage () {
23	echo "Usage:"
24	echo "      $(basename "$0") <image path> "
25	echo "Where:"
26	echo "	<image path>: the path link to folder, which include image file and MANIFEST"
27	echo "Example:"
28	echo "     $(basename "$0") /tmp/images/ghdh1393"
29}
30
31
32IMG_PATH="$1"
33if [ ! -d "$IMG_PATH" ]; then
34	echo "The folder $IMG_PATH does not exist"
35	usage
36	exit 1
37fi
38
39MANIFEST_PATH="${IMG_PATH}/MANIFEST"
40if [ ! -f "$MANIFEST_PATH" ]; then
41	echo "The MANIFEST file $MANIFEST_PATH does not exist"
42	usage
43	exit 1
44fi
45
46EXTENDED_VERSION=$(awk '/ExtendedVersion/ {print}' "${MANIFEST_PATH}" | cut -d "=" -f 2)
47
48# If the ExtendedVersion is empty, set default to update UEFI/EDKII on primary device
49if [ -z "$EXTENDED_VERSION" ]
50then
51	EXTENDED_VERSION="primary"
52fi
53
54# Assign the command based on the ExtendedVersion
55case ${EXTENDED_VERSION} in
56	"primary")
57		IMAGE=$(find "${IMG_PATH}" -type f \( -name "*.img" -o -name "*.bin" -o -name "*.rom" \))
58		CMD="/usr/sbin/ampere_flash_bios.sh $IMAGE 1"
59		;;
60
61	"secondary")
62		IMAGE=$(find "${IMG_PATH}" -type f \( -name "*.img" -o -name "*.bin" -o -name "*.rom" \))
63		CMD="/usr/sbin/ampere_flash_bios.sh $IMAGE 2"
64		;;
65
66	"scp-primary")
67		IMAGE=$(find "${IMG_PATH}" -type f \( -name "*.img" -o -name "*.slim" -o -name "*.rom" \))
68		CMD="/usr/sbin/ampere_firmware_upgrade.sh smpmpro $IMAGE 1"
69		;;
70
71	"scp-secondary")
72		IMAGE=$(find "${IMG_PATH}" -type f \( -name "*.img" -o -name "*.slim" -o -name "*.rom" \))
73		CMD="/usr/sbin/ampere_firmware_upgrade.sh smpmpro $IMAGE 2"
74		;;
75
76	"fru")
77		IMAGE=$(find "${IMG_PATH}" -type f \( -name "*.bin" \))
78		CMD="/usr/sbin/ampere_firmware_upgrade.sh fru $IMAGE"
79		;;
80
81	*)
82		echo "Invalid ExtendedVersion: ${EXTENDED_VERSION}. Please check MANIFEST file!"
83		exit 1
84		;;
85esac
86
87
88if [ -z "$IMAGE" ]
89then
90	echo "ERROR: The image file: No such file or directory"
91	exit 1
92fi
93
94if ! eval "$CMD";
95then
96	echo "ERROR: The firmware update not successfull"
97	exit 1
98fi
99