Yegor Timoshenko | c2e4941 | 2018-10-07 01:58:27 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 2 | # |
| 3 | # This file is part of the coreboot project. |
| 4 | # |
| 5 | # Copyright (C) 2016 Joe Pillow |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; version 2 of the License. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | |
Tristan Corrick | df90c62 | 2018-12-31 20:34:36 +1300 | [diff] [blame] | 17 | # On some systems, `parted` and `debugfs` are located in /sbin. |
| 18 | export PATH="$PATH:/sbin" |
| 19 | |
Tristan Corrick | d80607d | 2018-12-31 20:54:42 +1300 | [diff] [blame] | 20 | exit_if_uninstalled() { |
| 21 | local cmd_name="$1" |
| 22 | local deb_pkg_name="$2" |
| 23 | |
| 24 | if type "$cmd_name" >/dev/null 2>&1; then |
| 25 | return |
| 26 | fi |
| 27 | |
| 28 | printf '`%s` was not found. ' "$cmd_name" >&2 |
| 29 | printf 'On Debian-based systems, it can be installed\n' >&2 |
| 30 | printf 'by running `apt install %s`.\n' "$deb_pkg_name" >&2 |
| 31 | |
| 32 | exit 1 |
| 33 | } |
| 34 | |
| 35 | exit_if_dependencies_are_missing() { |
| 36 | exit_if_uninstalled "uudecode" "sharutils" |
| 37 | exit_if_uninstalled "debugfs" "e2fsprogs" |
| 38 | exit_if_uninstalled "parted" "parted" |
| 39 | exit_if_uninstalled "curl" "curl" |
| 40 | } |
| 41 | |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 42 | get_inventory() |
| 43 | { |
| 44 | _conf=$1 |
| 45 | _url=https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf |
| 46 | |
Tristan Corrick | f50bd6d | 2018-12-31 20:58:43 +1300 | [diff] [blame] | 47 | echo "Downloading recovery image inventory..." |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 48 | |
| 49 | curl -s "$_url" > $_conf |
| 50 | } |
| 51 | |
| 52 | download_image() |
| 53 | { |
| 54 | _url=$1 |
| 55 | _file=$2 |
| 56 | |
Tristan Corrick | f50bd6d | 2018-12-31 20:58:43 +1300 | [diff] [blame] | 57 | echo "Downloading recovery image" |
Tristan Corrick | 30348c2 | 2018-12-31 20:41:11 +1300 | [diff] [blame] | 58 | curl "$_url" > "$_file.zip" |
Tristan Corrick | f50bd6d | 2018-12-31 20:58:43 +1300 | [diff] [blame] | 59 | echo "Decompressing recovery image" |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 60 | unzip -q "$_file.zip" |
| 61 | rm "$_file.zip" |
| 62 | } |
| 63 | |
| 64 | extract_partition() |
| 65 | { |
| 66 | NAME=$1 |
| 67 | FILE=$2 |
| 68 | ROOTFS=$3 |
| 69 | _bs=1024 |
| 70 | |
Tristan Corrick | f50bd6d | 2018-12-31 20:58:43 +1300 | [diff] [blame] | 71 | echo "Extracting ROOT-A partition" |
Joseph Pillow | d3fccfb | 2016-04-21 16:15:10 -0700 | [diff] [blame] | 72 | ROOTP=$( printf "unit\nB\nprint\nquit\n" | \ |
| 73 | parted $FILE 2>/dev/null | grep $NAME ) |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 74 | |
| 75 | START=$(( $( echo $ROOTP | cut -f2 -d\ | tr -d "B" ) )) |
| 76 | SIZE=$(( $( echo $ROOTP | cut -f4 -d\ | tr -d "B" ) )) |
| 77 | |
| 78 | dd if=$FILE of=$ROOTFS bs=$_bs skip=$(( $START / $_bs )) \ |
Nico Huber | e1b902c | 2019-10-18 20:12:50 +0200 | [diff] [blame] | 79 | count=$(( $SIZE / $_bs )) > /dev/null |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | extract_shellball() |
| 83 | { |
| 84 | ROOTFS=$1 |
| 85 | SHELLBALL=$2 |
| 86 | |
Tristan Corrick | f50bd6d | 2018-12-31 20:58:43 +1300 | [diff] [blame] | 87 | echo "Extracting chromeos-firmwareupdate" |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 88 | printf "cd /usr/sbin\ndump chromeos-firmwareupdate $SHELLBALL\nquit" | \ |
| 89 | debugfs $ROOTFS > /dev/null 2>&1 |
| 90 | } |
| 91 | |
| 92 | extract_coreboot() |
| 93 | { |
| 94 | _shellball=$1 |
| 95 | _unpacked=$( mktemp -d ) |
| 96 | |
Tristan Corrick | f50bd6d | 2018-12-31 20:58:43 +1300 | [diff] [blame] | 97 | echo "Extracting coreboot image" |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 98 | sh $_shellball --sb_extract $_unpacked > /dev/null |
| 99 | |
| 100 | _version=$( cat $_unpacked/VERSION | grep BIOS\ version: | \ |
| 101 | cut -f2 -d: | tr -d \ ) |
| 102 | |
| 103 | cp $_unpacked/bios.bin coreboot-$_version.bin |
| 104 | rm -r "$_unpacked" |
| 105 | } |
| 106 | |
| 107 | do_one_board() |
| 108 | { |
| 109 | _board=$1 |
| 110 | _url=$2 |
| 111 | _file=$3 |
| 112 | |
| 113 | download_image $_url $_file |
| 114 | |
| 115 | extract_partition ROOT-A $_file root-a.ext2 |
| 116 | extract_shellball root-a.ext2 chromeos-firmwareupdate-$_board |
| 117 | rm $_file root-a.ext2 |
| 118 | |
| 119 | extract_coreboot chromeos-firmwareupdate-$_board |
| 120 | } |
| 121 | |
| 122 | # |
| 123 | # Main |
| 124 | # |
| 125 | |
| 126 | BOARD=$1 |
| 127 | |
Tristan Corrick | d80607d | 2018-12-31 20:54:42 +1300 | [diff] [blame] | 128 | exit_if_dependencies_are_missing |
| 129 | |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 130 | if [ "$BOARD" == "all" ]; then |
| 131 | CONF=$( mktemp ) |
| 132 | get_inventory $CONF |
| 133 | |
| 134 | grep ^name= $CONF| while read _line; do |
| 135 | name=$( echo $_line | cut -f2 -d= ) |
| 136 | echo Processing board $name |
| 137 | eval $( grep -v hwid= $CONF | grep -A11 "$_line" | \ |
| 138 | grep '\(url=\|file=\)' ) |
| 139 | BOARD=$( echo $url | cut -f3 -d_ ) |
| 140 | do_one_board $BOARD $url $file |
| 141 | done |
| 142 | |
| 143 | rm "$CONF" |
| 144 | elif [ "$BOARD" != "" ]; then |
| 145 | CONF=$( mktemp ) |
| 146 | get_inventory $CONF |
| 147 | |
| 148 | echo Processing board $BOARD |
| 149 | eval $( grep $BOARD $CONF | grep '\(url=\|file=\)' ) |
| 150 | do_one_board $BOARD $url $file |
| 151 | |
| 152 | rm "$CONF" |
| 153 | else |
| 154 | echo "Usage: $0 <boardname>" |
| 155 | echo " $0 all" |
| 156 | echo |
| 157 | exit 1 |
| 158 | fi |