blob: ec9e06d6ae612406447b3004e9d7a4f747ebdc48 [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() {
William A. Kennington III14c56fc2022-07-20 14:58:27 -070021 [ -n "${bootfile_url-}" ] || return 0
William A. Kennington III32e440d2021-12-03 18:19:07 -080022
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`.
William A. Kennington IIId99d91e2022-05-25 11:42:30 -070036 local tflags=()
37 if [[ "$bootfile_url" =~ [\&?]format=TAR(_GZIP)?(&|$) ]]; then
38 local t="${BASH_REMATCH[1]}"
39 [ "$t" = '_GZIP' ] && tflags+=('-z')
40 else
William A. Kennington III32e440d2021-12-03 18:19:07 -080041 echo "Unknown upgrade unpack method: $bootfile_url" >&2
42 return 1
43 fi
44
William A. Kennington III568f2e32022-04-29 10:24:53 -070045 # Determine the path of the image file for the correct machine
46 # Our netboot can serve us images for multiple models
47 local machine
48 machine="$(source /etc/os-release && echo "$OPENBMC_TARGET_MACHINE")" || return
49
William A. Kennington III32e440d2021-12-03 18:19:07 -080050 # Ensure some sane output file limit
51 # Currently no BMC image is larger than 64M
William A. Kennington III1fbee912022-04-28 14:26:44 -070052 # We want to allow 2 images and a small amount of metadata (2*64+2)M
53 local max_mb=$((2*64 + 2))
54 ulimit -f $((max_mb * 1024 * 1024 / 512)) || return
William A. Kennington III5a8202c2022-05-24 18:52:03 -070055 timeout=$((SECONDS + 300))
William A. Kennington III3cfbd152022-05-24 18:53:52 -070056 stime=5
57 while true; do
58 local st=()
William A. Kennington III5a8202c2022-05-24 18:52:03 -070059 curl -LSsk --max-time $((timeout - SECONDS)) "$bootfile_url" |
William A. Kennington IIIc5623472022-06-10 11:54:36 -070060 tar "${tflags[@]}" --wildcards -xC "$tmpdir" "*/firmware-gbmc/$machine" \
William A. Kennington III3cfbd152022-05-24 18:53:52 -070061 && st=("${PIPESTATUS[@]}") || st=("${PIPESTATUS[@]}")
62 # Curl failures should continue
63 if (( st[0] == 0 )); then
64 # Tar failures when curl succeeds are hard errors to start over.
65 if (( st[1] != 0 )); then
66 echo 'Unpacking failed' >&2
67 return 1
68 fi
69 # Success should continue without retry
70 break
71 fi
72 if (( SECONDS + stime >= timeout )); then
73 echo 'Timed out fetching image' >&2
74 return 1
75 fi
William A. Kennington III790c9722022-04-04 14:51:52 -070076 (shopt -s nullglob dotglob; rm -rf -- "${tmpdir:?}"/*)
William A. Kennington III3cfbd152022-05-24 18:53:52 -070077 sleep $stime
William A. Kennington III499f7242022-03-23 11:23:06 -070078 done
William A. Kennington III32e440d2021-12-03 18:19:07 -080079
80 local sig
William A. Kennington IIIab8ea8d2022-04-04 15:36:25 -070081 sig="$(find "$tmpdir" -name 'image-*.sig' | head -n 1)" || return
William A. Kennington III32e440d2021-12-03 18:19:07 -080082 local img="${sig%.sig}"
83 mv "$sig" "$GBMC_UPGRADE_SIG" || return
84 mv "$img" "$GBMC_UPGRADE_IMG" || return
85
86 # Regular packages have a VERSION file with the image
87 local imgdir="${sig%/*}"
88 if [ -f "$imgdir/VERSION" ]; then
89 cat "$imgdir/VERSION" || return
William A. Kennington III85c714a2022-04-04 15:32:32 -070090 return 0
William A. Kennington III32e440d2021-12-03 18:19:07 -080091 fi
92
93 # Staging packages have a directory named after the version
94 local vdir="${imgdir##*/}"
95 if [[ "$vdir" =~ ([0-9]+[.]){3}[0-9]+ ]]; then
96 echo "$vdir"
William A. Kennington III85c714a2022-04-04 15:32:32 -070097 return 0
William A. Kennington III32e440d2021-12-03 18:19:07 -080098 fi
William A. Kennington III85c714a2022-04-04 15:32:32 -070099
100 return 1
William A. Kennington III32e440d2021-12-03 18:19:07 -0800101)
102
103GBMC_BR_DHCP_HOOKS+=(gbmc_upgrade_hook)
104
105gbmc_upgrade=1