William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 1 | #!/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 | |
| 20 | gbmc_upgrade_hook() { |
William A. Kennington III | 14c56fc | 2022-07-20 14:58:27 -0700 | [diff] [blame] | 21 | [ -n "${bootfile_url-}" ] || return 0 |
William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 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 | |
| 31 | gbmc_upgrade_fetch() ( |
| 32 | echo "Fetching $bootfile_url" >&2 |
| 33 | |
William A. Kennington III | fea3baf | 2022-01-26 15:09:11 -0800 | [diff] [blame] | 34 | # 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 III | d99d91e | 2022-05-25 11:42:30 -0700 | [diff] [blame] | 36 | 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 III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 41 | echo "Unknown upgrade unpack method: $bootfile_url" >&2 |
| 42 | return 1 |
| 43 | fi |
| 44 | |
William A. Kennington III | 568f2e3 | 2022-04-29 10:24:53 -0700 | [diff] [blame] | 45 | # 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 III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 50 | # Ensure some sane output file limit |
| 51 | # Currently no BMC image is larger than 64M |
William A. Kennington III | 1fbee91 | 2022-04-28 14:26:44 -0700 | [diff] [blame] | 52 | # 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 III | 5a8202c | 2022-05-24 18:52:03 -0700 | [diff] [blame] | 55 | timeout=$((SECONDS + 300)) |
William A. Kennington III | 3cfbd15 | 2022-05-24 18:53:52 -0700 | [diff] [blame] | 56 | stime=5 |
| 57 | while true; do |
| 58 | local st=() |
William A. Kennington III | 5a8202c | 2022-05-24 18:52:03 -0700 | [diff] [blame] | 59 | curl -LSsk --max-time $((timeout - SECONDS)) "$bootfile_url" | |
William A. Kennington III | c562347 | 2022-06-10 11:54:36 -0700 | [diff] [blame] | 60 | tar "${tflags[@]}" --wildcards -xC "$tmpdir" "*/firmware-gbmc/$machine" \ |
William A. Kennington III | 3cfbd15 | 2022-05-24 18:53:52 -0700 | [diff] [blame] | 61 | && 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 III | 790c972 | 2022-04-04 14:51:52 -0700 | [diff] [blame] | 76 | (shopt -s nullglob dotglob; rm -rf -- "${tmpdir:?}"/*) |
William A. Kennington III | 3cfbd15 | 2022-05-24 18:53:52 -0700 | [diff] [blame] | 77 | sleep $stime |
William A. Kennington III | 499f724 | 2022-03-23 11:23:06 -0700 | [diff] [blame] | 78 | done |
William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 79 | |
| 80 | local sig |
William A. Kennington III | ab8ea8d | 2022-04-04 15:36:25 -0700 | [diff] [blame] | 81 | sig="$(find "$tmpdir" -name 'image-*.sig' | head -n 1)" || return |
William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 82 | 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 III | 85c714a | 2022-04-04 15:32:32 -0700 | [diff] [blame] | 90 | return 0 |
William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 91 | 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 III | 85c714a | 2022-04-04 15:32:32 -0700 | [diff] [blame] | 97 | return 0 |
William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 98 | fi |
William A. Kennington III | 85c714a | 2022-04-04 15:32:32 -0700 | [diff] [blame] | 99 | |
| 100 | return 1 |
William A. Kennington III | 32e440d | 2021-12-03 18:19:07 -0800 | [diff] [blame] | 101 | ) |
| 102 | |
| 103 | GBMC_BR_DHCP_HOOKS+=(gbmc_upgrade_hook) |
| 104 | |
| 105 | gbmc_upgrade=1 |