forked from merge/skulls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skulls.sh
executable file
·252 lines (217 loc) · 6.73 KB
/
skulls.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2019, Martin Kepplinger <[email protected]>
set -e
cd "$(dirname "$0")"
source "util/functions.sh"
have_input_image=0
request_update=0
verbose=0
have_board=0
BOARD=""
usage()
{
echo "The Skulls coreboot distribution:"
echo " Run this script on the Laptop directly."
echo ""
echo " This flashes the BIOS with the given image."
echo " Make sure you booted Linux with iomem=relaxed"
echo ""
echo "Usage: $0 -b (x230|x230t|t430|t440p|t530) [-i <4mb_top_image>.rom] [-U] [-h]"
echo "Options:"
echo " -b board to flash. This must be \"x230\", \"x230t\", \"t430\", \"t440p\" or \"t530\""
echo " -i path to the image to flash"
echo " -U update: check for a new Skulls package online"
echo " -v verbose output. prints more information"
echo " -h print this help text"
}
args=$(getopt -o b:i:hUv -- "$@")
if [ $? -ne 0 ] ; then
usage
exit 1
fi
eval set -- "$args"
while [ $# -gt 0 ]
do
case "$1" in
-b)
BOARD=$2
have_board=1
shift
;;
-i)
INPUT_IMAGE_PATH=$2
have_input_image=1
shift
;;
-h)
usage
exit 1
;;
-U)
request_update=1
;;
-v)
verbose=1
;;
--)
shift
break
;;
*)
echo "Invalid option: $1"
exit 1
;;
esac
shift
done
# TODO make interactive and move to functions
if [ ! "$have_board" -gt 0 ] ; then
echo "No board specified. Please add -b <board>"
echo ""
usage
exit 1
fi
if [[ $BOARD == "x230" ]] ; then
if [[ $verbose -gt 0 ]] ; then
echo "Board: $BOARD"
fi
elif [[ $BOARD == "x230t" ]] ; then
if [[ $verbose -gt 0 ]] ; then
echo "Board: $BOARD"
fi
elif [[ $BOARD == "t430" ]] ; then
if [[ $verbose -gt 0 ]] ; then
echo "Board: $BOARD"
fi
elif [[ $BOARD == "t440p" ]] ; then
if [[ $verbose -gt 0 ]] ; then
echo "Board: $BOARD"
fi
elif [[ $BOARD == "t530" ]] ; then
if [[ $verbose -gt 0 ]] ; then
echo "Board: $BOARD"
fi
else
echo "Unsupported board: $BOARD"
echo ""
usage
exit 1
fi
if [ "$request_update" -gt 0 ] ; then
warn_not_root
command -v curl >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install curl.${NC}"; exit 1; }
CURRENT_VERSION=$(head -2 NEWS | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
UPSTREAM_FILE=$(curl -s https://api.github.com/repos/merge/skulls/releases | grep browser_download_url | cut -d'"' -f4 | cut -d'/' -f9 | head -n 1)
UPSTREAM_VERSION=$(curl -s https://api.github.com/repos/merge/skulls/releases | grep browser_download_url | cut -d'"' -f4 | cut -d'/' -f9 | head -n 1 | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
if [[ $verbose -gt 0 ]] ; then
echo "This is v$CURRENT_VERSION and latest is v$UPSTREAM_VERSION"
fi
if [[ "$CURRENT_VERSION" = "$UPSTREAM_VERSION" ]] ; then
echo -e "${GREEN}You are using the latest version of Skulls${NC}"
exit 0
elif [[ "$CURRENT_VERSION" < "$UPSTREAM_VERSION" ]] ; then
echo -e "${RED}You have ${CURRENT_VERSION} but there is version ${UPSTREAM_VERSION} available. Please update.${NC}"
read -r -p "Download it to the parent directory now? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
UPSTREAM_URL=$(curl -s https://api.github.com/repos/merge/skulls/releases | grep browser_download_url | cut -d'"' -f4 | head -n 1)
UPSTREAM_URL_SHA256=$(curl -s https://api.github.com/repos/merge/skulls/releases | grep browser_download_url | cut -d'"' -f4 | head -n 3 | tail -n 1)
cd ..
curl -LO ${UPSTREAM_URL}
curl -LO ${UPSTREAM_URL_SHA256}
sha256sum -c ${UPSTREAM_FILE}.sha256
mkdir skulls-${UPSTREAM_VERSION}
tar -xf ${UPSTREAM_FILE}
echo "Version ${UPSTREAM_VERSION} extracted to ../skulls-${UPSTREAM_VERSION}/"
echo "Please continue in the new directory."
;;
*)
exit 0
;;
esac
else
echo "You seem to use a development version. Please use release package skulls ${UPSTREAM_VERSION} for flashing."
fi
exit 0
fi
check_board_and_root
if [[ "$verbose" -gt 0 ]] ; then
if [ -d "/sys/class/power_supply/BAT0" ] ; then
bat_last_full=$(cat /sys/class/power_supply/BAT0/charge_full)
bat_design_cap=$(cat /sys/class/power_supply/BAT0/charge_full_design)
bat_health=$(echo "scale=2 ; $bat_last_full/$bat_design_cap" | bc | sed 's/^\./0./')
bat_health=$(echo "$bat_health*100" | bc)
echo "INFO: Battery hardware health is $bat_health%"
fi
fi
if [ ! "$have_input_image" -gt 0 ] ; then
image_available=$(ls -1 | grep ${BOARD}_coreboot_seabios || true)
if [ -z "${image_available}" ] ; then
echo "No image file found. Please add -i <file>"
echo ""
usage
exit 1
fi
prompt="file not specified. Please select a file to flash. Please read the README for details about the differences:"
options=( $(find -maxdepth 1 -name "${BOARD}_coreboot_seabios*rom" -print0 | xargs -0) )
PS3="$prompt "
select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
exit
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
break
else
echo "Invalid option. Try another one."
fi
done
fi
if [ -e ${INPUT_IMAGE_PATH}.sha256 ] ; then
cd $(dirname "${INPUT_IMAGE_PATH}")
sha256sum -c ${INPUT_IMAGE_PATH}.sha256
cd - > /dev/null
else
echo -e "${YELLOW}Warning:${NC} Cannot verify image hash"
fi
OUTPUT_PATH=output
INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
OUTPUT_IMAGE_NAME=${INPUT_IMAGE_NAME%%.*}_prepared_12mb.rom
OUTPUT_IMAGE_PATH=${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}
echo -e "input: ${INPUT_IMAGE_NAME}"
echo -e "output: ${OUTPUT_IMAGE_PATH}"
input_filesize=$(wc -c <"$INPUT_IMAGE_PATH")
reference_filesize=4194304
if [ ! "$input_filesize" -eq "$reference_filesize" ] ; then
echo -e "${RED}Error:${NC} input file must be 4MB of size"
exit 1
fi
rm -rf ${OUTPUT_PATH}
mkdir ${OUTPUT_PATH}
dd if=/dev/zero of="${OUTPUT_IMAGE_PATH}" bs=4M count=2 status=none
dd if="${INPUT_IMAGE_PATH}" oflag=append conv=notrunc of="${OUTPUT_IMAGE_PATH}" bs=4M status=none
LAYOUT_FILENAME="layout.txt"
echo "0x00000000:0x00000fff ifd" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
echo "0x00001000:0x00002fff gbe" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
echo "0x00003000:0x004fffff me" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
echo "0x00500000:0x007fffff unused" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
echo "0x00800000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
echo -e "${YELLOW}Warning:${NC} Make sure not to power off your computer or interrupt this process in any way!"
echo -e " Interrupting this process may result in irreparable damage to your computer!"
check_battery
while true; do
read -r -p "Flash the BIOS now? y/N: " yn
case $yn in
[Yy]* ) cd ${OUTPUT_PATH} && ${FLASHROM} --force --noverify-all -p internal --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"; break;;
[Nn]* ) exit;;
* ) exit;;
esac
done
rm -rf ${OUTPUT_PATH}
while true; do
read -r -p "Power off now? (please do!) Y/n: " yn
case $yn in
[Yy]* ) poweroff ;;
[Nn]* ) exit;;
* ) poweroff ;;
esac
done