| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | |
| 5 | # On some systems, `parted` and `debugfs` are located in /sbin. |
| 6 | export PATH="$PATH:/sbin" |
| 7 | |
| 8 | exit_if_uninstalledscip-shell shell coreboot 25.09+dfsg-1 exit_if_uninstalled().() { |
| 9 | local cmd_name="$1" |
| 10 | local deb_pkg_name="$2" |
| 11 | |
| 12 | if type "$cmd_name" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 13 | return |
| 14 | fi |
| 15 | |
| 16 | printfscip-shell system . . printf. '`%s` was not found. ' "$cmd_name" >&2 |
| 17 | printfscip-shell system . . printf. 'On Debian-based systems, it can be installed\n' >&2 |
| 18 | printfscip-shell system . . printf. 'by running `apt install %s`.\n' "$deb_pkg_name" >&2 |
| 19 | |
| 20 | exit 1 |
| 21 | } |
| 22 | |
| 23 | exit_if_dependencies_are_missingscip-shell shell coreboot 25.09+dfsg-1 exit_if_dependencies_are_missing().() { |
| 24 | exit_if_uninstalledscip-shell shell coreboot 25.09+dfsg-1 exit_if_uninstalled(). "uudecode" "sharutils" |
| 25 | exit_if_uninstalledscip-shell shell coreboot 25.09+dfsg-1 exit_if_uninstalled(). "debugfs" "e2fsprogs" |
| 26 | exit_if_uninstalledscip-shell shell coreboot 25.09+dfsg-1 exit_if_uninstalled(). "parted" "parted" |
| 27 | exit_if_uninstalledscip-shell shell coreboot 25.09+dfsg-1 exit_if_uninstalled(). "curl" "curl" |
| 28 | exit_if_uninstalledscip-shell shell coreboot 25.09+dfsg-1 exit_if_uninstalled(). "unzip" "unzip" |
| 29 | } |
| 30 | |
| 31 | get_inventoryscip-shell shell coreboot 25.09+dfsg-1 get_inventory().() { |
| 32 | _conf=$1 |
| 33 | _url=https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf |
| 34 | |
| 35 | echoscip-shell system . . echo. "Downloading recovery image inventory..." |
| 36 | |
| 37 | curlscip-shell system . . curl. -s "$_url" >$_conf |
| 38 | } |
| 39 | |
| 40 | download_imagescip-shell shell coreboot 25.09+dfsg-1 download_image().() { |
| 41 | _url=$1 |
| 42 | _file=$2 |
| 43 | |
| 44 | echoscip-shell system . . echo. "Downloading recovery image" |
| 45 | curlscip-shell system . . curl. "$_url" >"$_file.zip" |
| 46 | echoscip-shell system . . echo. "Decompressing recovery image" |
| 47 | unzip -q "$_file.zip" |
| 48 | rmscip-shell system . . rm. "$_file.zip" |
| 49 | } |
| 50 | |
| 51 | extract_partitionscip-shell shell coreboot 25.09+dfsg-1 extract_partition().() { |
| 52 | NAME=$1 |
| 53 | FILE=$2 |
| 54 | ROOTFS=$3 |
| 55 | _bs=1024 |
| 56 | |
| 57 | echoscip-shell system . . echo. "Extracting ROOT-A partition" |
| 58 | ROOTP=$(printfscip-shell system . . printf. "unit\nB\nprint\nquit\n" | |
| 59 | parted $FILE 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. $NAME) |
| 60 | |
| 61 | if [scip-shell system . . `[`. "$ROOTP" == "" ]; then |
| 62 | # Automatic extraction failed, likely due to parted detecting |
| 63 | # overlapping partitions. Fall back to using fdisk and assume |
| 64 | # ROOT-A is partition #3 |
| 65 | echoscip-shell system . . echo. "(Extracting via parted failed; falling back to fdisk)" |
| 66 | _ssize=$(printfscip-shell system . . printf. "p q" | fdisk $FILE | grepscip-shell system . . grep. "Sector size" | |
| 67 | cutscip-shell system . . cut. -f2 -d: | cutscip-shell system . . cut. -f2 -d ' ') |
| 68 | _start=$(printfscip-shell system . . printf. "p q" | fdisk $FILE | grepscip-shell system . . grep. "bin3" | trscip-shell system . . tr. -s ' ' | |
| 69 | cutscip-shell system . . cut. -f2 -d ' ') |
| 70 | _nsec=$(printfscip-shell system . . printf. "p q" | fdisk $FILE | grepscip-shell system . . grep. "bin3" | trscip-shell system . . tr. -s ' ' | |
| 71 | cutscip-shell system . . cut. -f4 -d ' ') |
| 72 | START=$(($_ssize * $_start)) |
| 73 | SIZE=$(($_ssize * $_nsec)) |
| 74 | else |
| 75 | START=$(($(echoscip-shell system . . echo. $ROOTP | cutscip-shell system . . cut. -f2 -d\ | trscip-shell system . . tr. -d "B"))) |
| 76 | SIZE=$(($(echoscip-shell system . . echo. $ROOTP | cutscip-shell system . . cut. -f4 -d\ | trscip-shell system . . tr. -d "B"))) |
| 77 | fi |
| 78 | |
| 79 | ddscip-shell system . . dd. if=$FILE of=$ROOTFS bs=$_bs skip=$(($START / $_bs)) \ |
| 80 | count=$(($SIZE / $_bs)) >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 81 | } |
| 82 | |
| 83 | extract_shellballscip-shell shell coreboot 25.09+dfsg-1 extract_shellball().() { |
| 84 | ROOTFS=$1 |
| 85 | SHELLBALL=$2 |
| 86 | |
| 87 | echoscip-shell system . . echo. "Extracting chromeos-firmwareupdate" |
| 88 | printfscip-shell system . . printf. "cd /usr/sbin\ndump chromeos-firmwareupdate $SHELLBALL\nquit" | |
| 89 | debugfs $ROOTFS >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 90 | } |
| 91 | |
| 92 | extract_corebootscip-shell shell coreboot 25.09+dfsg-1 extract_coreboot().() { |
| 93 | _shellball=$1 |
| 94 | _unpacked=$(mktempscip-shell system . . mktemp. -d) |
| 95 | |
| 96 | echoscip-shell system . . echo. "Extracting coreboot image" |
| 97 | if ! shscip-shell system . . sh. $_shellball --unpack $_unpacked >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 98 | shscip-shell system . . sh. $_shellball --sb_extract $_unpacked >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 99 | fi |
| 100 | |
| 101 | if [scip-shell system . . `[`. -d $_unpacked/models/ ]; then |
| 102 | _version=$(catscip-shell system . . cat. $_unpacked/VERSION | grepscip-shell system . . grep. -m 1 -e Model.*$_board -A5 | |
| 103 | grepscip-shell system . . grep. "BIOS (RW) version:" | cutscip-shell system . . cut. -f2 -d: | trscip-shell system . . tr. -d \ ) |
| 104 | if [scip-shell system . . `[`. "$_version" == "" ]; then |
| 105 | _version=$(catscip-shell system . . cat. $_unpacked/VERSION | grepscip-shell system . . grep. -m 1 -e Model.*$_board -A5 | |
| 106 | grepscip-shell system . . grep. "BIOS version:" | cutscip-shell system . . cut. -f2 -d: | trscip-shell system . . tr. -d \ ) |
| 107 | fi |
| 108 | if [scip-shell system . . `[`. -f $_unpacked/models/$_board/setvars.sh ]; then |
| 109 | _bios_image=$(grepscip-shell system . . grep. "IMAGE_MAIN" $_unpacked/models/$_board/setvars.sh | |
| 110 | cutscip-shell system . . cut. -f2 -d'"') |
| 111 | else |
| 112 | # special case for REEF, others? |
| 113 | _version=$(grepscip-shell system . . grep. -m1 "host" "$_unpacked/manifest.json" | cutscip-shell system . . cut. -f12 -d'"') |
| 114 | _bios_image=$(grepscip-shell system . . grep. -m1 "image" "$_unpacked/manifest.json" | cutscip-shell system . . cut. -f4 -d'"') |
| 115 | fi |
| 116 | elif [scip-shell system . . `[`. -f "$_unpacked/manifest.json" ]; then |
| 117 | _version=$(grepscip-shell system . . grep. -m1 -A4 "$BOARD\":" "$_unpacked/manifest.json" | grepscip-shell system . . grep. -m1 "rw" | |
| 118 | sedscip-shell system . . sed. 's/.*\(rw.*\)/\1/' | sedscip-shell system . . sed. 's/.*\("Google.*\)/\1/' | cutscip-shell system . . cut. -f2 -d'"') |
| 119 | _bios_image=$(grepscip-shell system . . grep. -m1 -A10 "$BOARD\":" "$_unpacked/manifest.json" | |
| 120 | grepscip-shell system . . grep. -m1 "image" | sedscip-shell system . . sed. 's/.*"image": //' | cutscip-shell system . . cut. -f2 -d'"') |
| 121 | else |
| 122 | _version=$(catscip-shell system . . cat. $_unpacked/VERSION | grepscip-shell system . . grep. BIOS\ version: | |
| 123 | cutscip-shell system . . cut. -f2 -d: | trscip-shell system . . tr. -d \ ) |
| 124 | _bios_image=bios.bin |
| 125 | fi |
| 126 | if cpscip-shell system . . cp. $_unpacked/$_bios_image coreboot-$_version.bin; then |
| 127 | echoscip-shell system . . echo. "Extracted coreboot-$_version.bin" |
| 128 | fi |
| 129 | rmscip-shell system . . rm. -rf "$_unpacked" |
| 130 | rmscip-shell system . . rm. $_shellball |
| 131 | } |
| 132 | |
| 133 | do_one_boardscip-shell shell coreboot 25.09+dfsg-1 do_one_board().() { |
| 134 | _board=$1 |
| 135 | _url=$2 |
| 136 | _file=$3 |
| 137 | |
| 138 | download_imagescip-shell shell coreboot 25.09+dfsg-1 download_image(). $_url $_file |
| 139 | |
| 140 | extract_partitionscip-shell shell coreboot 25.09+dfsg-1 extract_partition(). ROOT-A $_file root-a.ext2 |
| 141 | extract_shellballscip-shell shell coreboot 25.09+dfsg-1 extract_shellball(). root-a.ext2 chromeos-firmwareupdate-$_board |
| 142 | rmscip-shell system . . rm. $_file root-a.ext2 |
| 143 | |
| 144 | extract_corebootscip-shell shell coreboot 25.09+dfsg-1 extract_coreboot(). chromeos-firmwareupdate-$_board |
| 145 | } |
| 146 | |
| 147 | # |
| 148 | # Main |
| 149 | # |
| 150 | |
| 151 | BOARD=${1,,} |
| 152 | |
| 153 | exit_if_dependencies_are_missingscip-shell shell coreboot 25.09+dfsg-1 exit_if_dependencies_are_missing(). |
| 154 | |
| 155 | if [scip-shell system . . `[`. "$BOARD" == "all" ]; then |
| 156 | CONF=$(mktempscip-shell system . . mktemp.) |
| 157 | get_inventoryscip-shell shell coreboot 25.09+dfsg-1 get_inventory(). $CONF |
| 158 | |
| 159 | grepscip-shell system . . grep. ^name= $CONF | while read _line; do |
| 160 | name=$(echoscip-shell system . . echo. $_line | cutscip-shell system . . cut. -f2 -d=) |
| 161 | echoscip-shell system . . echo. Processing board $name |
| 162 | eval $(grepscip-shell system . . grep. -v hwid= $CONF | grepscip-shell system . . grep. -A11 "$_line" | |
| 163 | grepscip-shell system . . grep. '\(url=\|file=\)') |
| 164 | BOARD=$(echoscip-shell system . . echo. $url | cutscip-shell system . . cut. -f3 -d_) |
| 165 | do_one_boardscip-shell shell coreboot 25.09+dfsg-1 do_one_board(). $BOARD $url $file |
| 166 | done |
| 167 | |
| 168 | rmscip-shell system . . rm. "$CONF" |
| 169 | elif [scip-shell system . . `[`. "$BOARD" != "" ]; then |
| 170 | CONF=$(mktempscip-shell system . . mktemp.) |
| 171 | get_inventoryscip-shell shell coreboot 25.09+dfsg-1 get_inventory(). $CONF |
| 172 | |
| 173 | echoscip-shell system . . echo. Processing board $BOARD |
| 174 | eval $(grepscip-shell system . . grep. -i $BOARD -A8 $CONF | grepscip-shell system . . grep. '\(url=\|file=\)') |
| 175 | do_one_boardscip-shell shell coreboot 25.09+dfsg-1 do_one_board(). $BOARD $url $file |
| 176 | |
| 177 | rmscip-shell system . . rm. "$CONF" |
| 178 | else |
| 179 | echoscip-shell system . . echo. "Usage: $0 <boardname>" |
| 180 | echoscip-shell system . . echo. " $0 all" |
| 181 | echoscip-shell system . . echo. |
| 182 | exit 1 |
| 183 | fi |