Joseph Pillow | d3fccfb | 2016-04-21 16:15:10 -0700 | [diff] [blame] | 1 | #!/bin/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 | |
| 17 | # This script needs |
| 18 | # * sharutils |
| 19 | |
| 20 | #DEBUG=1 |
| 21 | |
| 22 | debug() |
| 23 | { |
| 24 | test "$DEBUG" == "1" && echo "$*" |
| 25 | } |
| 26 | |
| 27 | get_inventory() |
| 28 | { |
| 29 | _conf=$1 |
| 30 | _url=https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf |
| 31 | |
| 32 | debug "Downloading recovery image inventory..." |
| 33 | |
| 34 | curl -s "$_url" > $_conf |
| 35 | } |
| 36 | |
| 37 | download_image() |
| 38 | { |
| 39 | _url=$1 |
| 40 | _file=$2 |
| 41 | |
| 42 | debug "Downloading recovery image" |
| 43 | curl -s "$_url" > "$_file.zip" |
| 44 | debug "Decompressing recovery image" |
| 45 | unzip -q "$_file.zip" |
| 46 | rm "$_file.zip" |
| 47 | } |
| 48 | |
| 49 | extract_partition() |
| 50 | { |
| 51 | NAME=$1 |
| 52 | FILE=$2 |
| 53 | ROOTFS=$3 |
| 54 | _bs=1024 |
| 55 | |
| 56 | debug "Extracting ROOT-A partition" |
Joseph Pillow | d3fccfb | 2016-04-21 16:15:10 -0700 | [diff] [blame] | 57 | ROOTP=$( printf "unit\nB\nprint\nquit\n" | \ |
| 58 | parted $FILE 2>/dev/null | grep $NAME ) |
Joe Pillow | 9a4881a | 2016-02-19 15:18:14 -0800 | [diff] [blame] | 59 | |
| 60 | START=$(( $( echo $ROOTP | cut -f2 -d\ | tr -d "B" ) )) |
| 61 | SIZE=$(( $( echo $ROOTP | cut -f4 -d\ | tr -d "B" ) )) |
| 62 | |
| 63 | dd if=$FILE of=$ROOTFS bs=$_bs skip=$(( $START / $_bs )) \ |
| 64 | count=$(( $SIZE / $_bs )) > /dev/null 2>&1 |
| 65 | } |
| 66 | |
| 67 | extract_shellball() |
| 68 | { |
| 69 | ROOTFS=$1 |
| 70 | SHELLBALL=$2 |
| 71 | |
| 72 | debug "Extracting chromeos-firmwareupdate" |
| 73 | printf "cd /usr/sbin\ndump chromeos-firmwareupdate $SHELLBALL\nquit" | \ |
| 74 | debugfs $ROOTFS > /dev/null 2>&1 |
| 75 | } |
| 76 | |
| 77 | extract_coreboot() |
| 78 | { |
| 79 | _shellball=$1 |
| 80 | _unpacked=$( mktemp -d ) |
| 81 | |
| 82 | debug "Extracting coreboot image" |
| 83 | sh $_shellball --sb_extract $_unpacked > /dev/null |
| 84 | |
| 85 | _version=$( cat $_unpacked/VERSION | grep BIOS\ version: | \ |
| 86 | cut -f2 -d: | tr -d \ ) |
| 87 | |
| 88 | cp $_unpacked/bios.bin coreboot-$_version.bin |
| 89 | rm -r "$_unpacked" |
| 90 | } |
| 91 | |
| 92 | do_one_board() |
| 93 | { |
| 94 | _board=$1 |
| 95 | _url=$2 |
| 96 | _file=$3 |
| 97 | |
| 98 | download_image $_url $_file |
| 99 | |
| 100 | extract_partition ROOT-A $_file root-a.ext2 |
| 101 | extract_shellball root-a.ext2 chromeos-firmwareupdate-$_board |
| 102 | rm $_file root-a.ext2 |
| 103 | |
| 104 | extract_coreboot chromeos-firmwareupdate-$_board |
| 105 | } |
| 106 | |
| 107 | # |
| 108 | # Main |
| 109 | # |
| 110 | |
| 111 | BOARD=$1 |
| 112 | |
| 113 | if [ "$BOARD" == "all" ]; then |
| 114 | CONF=$( mktemp ) |
| 115 | get_inventory $CONF |
| 116 | |
| 117 | grep ^name= $CONF| while read _line; do |
| 118 | name=$( echo $_line | cut -f2 -d= ) |
| 119 | echo Processing board $name |
| 120 | eval $( grep -v hwid= $CONF | grep -A11 "$_line" | \ |
| 121 | grep '\(url=\|file=\)' ) |
| 122 | BOARD=$( echo $url | cut -f3 -d_ ) |
| 123 | do_one_board $BOARD $url $file |
| 124 | done |
| 125 | |
| 126 | rm "$CONF" |
| 127 | elif [ "$BOARD" != "" ]; then |
| 128 | CONF=$( mktemp ) |
| 129 | get_inventory $CONF |
| 130 | |
| 131 | echo Processing board $BOARD |
| 132 | eval $( grep $BOARD $CONF | grep '\(url=\|file=\)' ) |
| 133 | do_one_board $BOARD $url $file |
| 134 | |
| 135 | rm "$CONF" |
| 136 | else |
| 137 | echo "Usage: $0 <boardname>" |
| 138 | echo " $0 all" |
| 139 | echo |
| 140 | exit 1 |
| 141 | fi |