blob: c94bf5bd0928f69c0e91100ee28dc0ff61479d98 [file] [log] [blame]
William A. Kennington III32e440d2021-12-03 18:19:07 -08001#!/bin/bash
2# Copyright 2021 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16[ -z "${gbmc_upgrade-}" ] || exit
17
18: "${GBMC_UPGRADE_SIG=/tmp/bmc.sig}"
19
20gbmc_upgrade_hook() {
21 [ -n "${bootfile_url-}" ] || return
22
23 local tmpdir
24 tmpdir="$(mktemp -d)" || return
25 gbmc_upgrade_internal || true
26 # SC doesn't know our variable is defined elsewhere
27 # shellcheck disable=SC2153
28 rm -rf -- "$tmpdir" "$GBMC_UPGRADE_SIG" "$GBMC_UPGRADE_IMG"
29}
30
31gbmc_upgrade_fetch() (
32 echo "Fetching $bootfile_url" >&2
33
34 # We only support tarballs at the moment
35 if [[ "$bootfile_url" != *.tar ]]; then
36 echo "Unknown upgrade unpack method: $bootfile_url" >&2
37 return 1
38 fi
39
40 # Ensure some sane output file limit
41 # Currently no BMC image is larger than 64M
42 ulimit -H -f $((96 * 1024 * 1024)) || return
43 wget "$bootfile_url" | tar -xC "$tmpdir" || true
44
45 local sig
46 sig="$(find "$tmpdir" -name 'image-*.sig')" || return
47 local img="${sig%.sig}"
48 mv "$sig" "$GBMC_UPGRADE_SIG" || return
49 mv "$img" "$GBMC_UPGRADE_IMG" || return
50
51 # Regular packages have a VERSION file with the image
52 local imgdir="${sig%/*}"
53 if [ -f "$imgdir/VERSION" ]; then
54 cat "$imgdir/VERSION" || return
55 fi
56
57 # Staging packages have a directory named after the version
58 local vdir="${imgdir##*/}"
59 if [[ "$vdir" =~ ([0-9]+[.]){3}[0-9]+ ]]; then
60 echo "$vdir"
61 fi
62)
63
64GBMC_BR_DHCP_HOOKS+=(gbmc_upgrade_hook)
65
66gbmc_upgrade=1