blob: dc33fac8cef883e52a943d6f52dea64bd54cb127 [file] [log] [blame]
Yegor Timoshenkoc2e49412018-10-07 01:58:27 +00001#!/usr/bin/env bash
Joe Pillow9a4881a2016-02-19 15:18:14 -08002#
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 Corrickdf90c622018-12-31 20:34:36 +130017# On some systems, `parted` and `debugfs` are located in /sbin.
18export PATH="$PATH:/sbin"
19
Tristan Corrickd80607d2018-12-31 20:54:42 +130020exit_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
35exit_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 Pillow9a4881a2016-02-19 15:18:14 -080042get_inventory()
43{
44 _conf=$1
45 _url=https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf
46
Tristan Corrickf50bd6d2018-12-31 20:58:43 +130047 echo "Downloading recovery image inventory..."
Joe Pillow9a4881a2016-02-19 15:18:14 -080048
49 curl -s "$_url" > $_conf
50}
51
52download_image()
53{
54 _url=$1
55 _file=$2
56
Tristan Corrickf50bd6d2018-12-31 20:58:43 +130057 echo "Downloading recovery image"
Tristan Corrick30348c22018-12-31 20:41:11 +130058 curl "$_url" > "$_file.zip"
Tristan Corrickf50bd6d2018-12-31 20:58:43 +130059 echo "Decompressing recovery image"
Joe Pillow9a4881a2016-02-19 15:18:14 -080060 unzip -q "$_file.zip"
61 rm "$_file.zip"
62}
63
64extract_partition()
65{
66 NAME=$1
67 FILE=$2
68 ROOTFS=$3
69 _bs=1024
70
Tristan Corrickf50bd6d2018-12-31 20:58:43 +130071 echo "Extracting ROOT-A partition"
Joseph Pillowd3fccfb2016-04-21 16:15:10 -070072 ROOTP=$( printf "unit\nB\nprint\nquit\n" | \
73 parted $FILE 2>/dev/null | grep $NAME )
Joe Pillow9a4881a2016-02-19 15:18:14 -080074
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 Hubere1b902c2019-10-18 20:12:50 +020079 count=$(( $SIZE / $_bs )) > /dev/null
Joe Pillow9a4881a2016-02-19 15:18:14 -080080}
81
82extract_shellball()
83{
84 ROOTFS=$1
85 SHELLBALL=$2
86
Tristan Corrickf50bd6d2018-12-31 20:58:43 +130087 echo "Extracting chromeos-firmwareupdate"
Joe Pillow9a4881a2016-02-19 15:18:14 -080088 printf "cd /usr/sbin\ndump chromeos-firmwareupdate $SHELLBALL\nquit" | \
89 debugfs $ROOTFS > /dev/null 2>&1
90}
91
92extract_coreboot()
93{
94 _shellball=$1
95 _unpacked=$( mktemp -d )
96
Tristan Corrickf50bd6d2018-12-31 20:58:43 +130097 echo "Extracting coreboot image"
Joe Pillow9a4881a2016-02-19 15:18:14 -080098 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
107do_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
126BOARD=$1
127
Tristan Corrickd80607d2018-12-31 20:54:42 +1300128exit_if_dependencies_are_missing
129
Joe Pillow9a4881a2016-02-19 15:18:14 -0800130if [ "$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"
144elif [ "$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"
153else
154 echo "Usage: $0 <boardname>"
155 echo " $0 all"
156 echo
157 exit 1
158fi