blob: 9d55eeec5377a09387445661fc895379db32a57d [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
William A. Kennington IIIfea3baf2022-01-26 15:09:11 -080034 # We only support tarballs at the moment, our URLs will always denote
35 # this with a URI query param of `format=TAR`.
36 if ! [[ "$bootfile_url" =~ [\&?]format=TAR(&|$) ]]; then
William A. Kennington III32e440d2021-12-03 18:19:07 -080037 echo "Unknown upgrade unpack method: $bootfile_url" >&2
38 return 1
39 fi
40
William A. Kennington III568f2e32022-04-29 10:24:53 -070041 # Determine the path of the image file for the correct machine
42 # Our netboot can serve us images for multiple models
43 local machine
44 machine="$(source /etc/os-release && echo "$OPENBMC_TARGET_MACHINE")" || return
45
William A. Kennington III32e440d2021-12-03 18:19:07 -080046 # Ensure some sane output file limit
47 # Currently no BMC image is larger than 64M
William A. Kennington III1fbee912022-04-28 14:26:44 -070048 # We want to allow 2 images and a small amount of metadata (2*64+2)M
49 local max_mb=$((2*64 + 2))
50 ulimit -f $((max_mb * 1024 * 1024 / 512)) || return
William A. Kennington III5a8202c2022-05-24 18:52:03 -070051 timeout=$((SECONDS + 300))
William A. Kennington III499f7242022-03-23 11:23:06 -070052 while (( SECONDS < timeout )); do
53 local st=(0)
William A. Kennington III5a8202c2022-05-24 18:52:03 -070054 curl -LSsk --max-time $((timeout - SECONDS)) "$bootfile_url" |
55 tar -xC "$tmpdir" "firmware-gbmc/$machine" || st=("${PIPESTATUS[@]}")
William A. Kennington III499f7242022-03-23 11:23:06 -070056 (( st[0] != 0 )) || break
William A. Kennington III790c9722022-04-04 14:51:52 -070057 (shopt -s nullglob dotglob; rm -rf -- "${tmpdir:?}"/*)
William A. Kennington III499f7242022-03-23 11:23:06 -070058 sleep 5
59 done
William A. Kennington III32e440d2021-12-03 18:19:07 -080060
61 local sig
William A. Kennington IIIab8ea8d2022-04-04 15:36:25 -070062 sig="$(find "$tmpdir" -name 'image-*.sig' | head -n 1)" || return
William A. Kennington III32e440d2021-12-03 18:19:07 -080063 local img="${sig%.sig}"
64 mv "$sig" "$GBMC_UPGRADE_SIG" || return
65 mv "$img" "$GBMC_UPGRADE_IMG" || return
66
67 # Regular packages have a VERSION file with the image
68 local imgdir="${sig%/*}"
69 if [ -f "$imgdir/VERSION" ]; then
70 cat "$imgdir/VERSION" || return
William A. Kennington III85c714a2022-04-04 15:32:32 -070071 return 0
William A. Kennington III32e440d2021-12-03 18:19:07 -080072 fi
73
74 # Staging packages have a directory named after the version
75 local vdir="${imgdir##*/}"
76 if [[ "$vdir" =~ ([0-9]+[.]){3}[0-9]+ ]]; then
77 echo "$vdir"
William A. Kennington III85c714a2022-04-04 15:32:32 -070078 return 0
William A. Kennington III32e440d2021-12-03 18:19:07 -080079 fi
William A. Kennington III85c714a2022-04-04 15:32:32 -070080
81 return 1
William A. Kennington III32e440d2021-12-03 18:19:07 -080082)
83
84GBMC_BR_DHCP_HOOKS+=(gbmc_upgrade_hook)
85
86gbmc_upgrade=1