summaryrefslogtreecommitdiff
path: root/util/board_status/board_status.sh
blob: 8073ae444478a702be74e0e5b094b193659517c0 (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env sh
#
#

EXIT_SUCCESS=0
EXIT_FAILURE=1

# Stuff from command-line switches
COREBOOT_IMAGE="build/coreboot.rom"
REMOTE_HOST=""
REMOTE_PORT_OPTION=""
CLOBBER_OUTPUT=0
UPLOAD_RESULTS=0
SERIAL_PORT_SPEED=115200

# Used to specify whether a command should always be run locally or
# if command should be run remoteley when a remote host is specified.
LOCAL=0
REMOTE=1
FATAL=0
NONFATAL=1

# Used if cbmem is not in default $PATH, e.g. not installed or when using `sudo`
CBMEM_PATH=""

# Used if nvramtool is not in default $PATH, e.g. not installed or when using `sudo`
NVRAMTOOL_PATH=""

# test a command
#
# $1: 0 ($LOCAL) to run command locally,
#     1 ($REMOTE) to run remotely if remote host defined
# $2: command to test
# $3: 0 ($FATAL) Exit with an error if the command fails
#     1 ($NONFATAL) Don't exit on command test failure
test_cmd()
{
	local rc

	if [ -e "$2" ]; then
		return
	fi

	if [ "$1" -eq "$REMOTE" ] && [ -n "$REMOTE_HOST" ]; then
		ssh $REMOTE_PORT_OPTION root@${REMOTE_HOST} command -v "$2" > /dev/null
		rc=$?
	else
		command -v "$2" >/dev/null
		rc=$?
	fi

	if [ $rc -eq 0 ]; then
		return 0
	fi

	if [ "$3" = "1" ]; then
		return 1
	fi

	echo "$2 not found"
	exit $EXIT_FAILURE
}

_cmd()
{
	if [ -e "$2" ]; then
		return $EXIT_FAILURE
	fi

	if [ -n "$3" ]; then
		pipe_location="${3}"
	else
		pipe_location="/dev/null"
	fi

	if [ "$1" -eq "$REMOTE" ] && [ -n "$REMOTE_HOST" ]; then
		ssh $REMOTE_PORT_OPTION "root@${REMOTE_HOST}" "$2" > "$pipe_location" 2>&1
	else
		$2 > "$pipe_location" 2>&1
	fi

	return $?
}

# run a command
#
# $1: 0 ($LOCAL) to run command locally,
#     1 ($REMOTE) to run remotely if remote host defined
# $2: command
# $3: filename to direct output of command into
cmd()
{
	_cmd $1 "$2" "$3"

	if [ $? -eq 0 ]; then
		return
	fi

	echo "Failed to run \"$2\", aborting"
	rm -f "$3"	# don't leave an empty file
	exit $EXIT_FAILURE
}

# run a command where failure is considered to be non-fatal
#
# $1: 0 ($LOCAL) to run command locally,
#     1 ($REMOTE) to run remotely if remote host defined
# $2: command
# $3: filename to direct output of command into
cmd_nonfatal()
{
	_cmd $1 "$2" "$3"

	if [ $? -eq 0 ]; then
		return
	fi

	echo "Failed to run \"$2\", ignoring"
	rm -f "$3"	# don't leave an empty file
}

# read from a serial port device
#
# $1: serial device to read from
# $2: serial port speed
# $3: filename to direct output of command into
get_serial_bootlog () {

	local TTY=$1
	local SPEED=$2
	local FILENAME=$3

	if [ ! -c "$TTY" ]; then
		echo "$TTY is not a valid serial device"
		exit $EXIT_FAILURE
	fi

	# make the text more noticible
	test_cmd $LOCAL "tput" $NONFATAL
	tput_not_available=$?
	if [ $tput_not_available -eq 0 ]; then
		tput bold
		tput setaf 10 # set bright green
	fi

	echo
	echo "Waiting to receive boot log from $TTY"
	echo "Press [Enter] when the boot is complete."
	echo

	if [ $tput_not_available -eq 0 ]; then
		tput sgr0
	fi

	# set up the serial port
	stty -F $TTY $SPEED cs8 -cstopb -parenb clocal

	# read from the serial port - user must press enter when complete
	test_cmd $LOCAL "tee"
	while read LINE; do
		echo "$LINE" | tee -a "$FILENAME"
	done < "$SERIAL_DEVICE" &
	PID=$!

	read foo
	kill "$PID" 2>/dev/null

	echo "Finished reading boot log."
}

show_help() {
	echo "Usage:
	${0} <option>

Options
    -c, --cbmem
        Path to cbmem on device under test (DUT).
    -n, --nvramtool
        Path to nvramtool on device under test (DUT).
    -C, --clobber
        Clobber temporary output when finished. Useful for debugging.
    -h, --help
        Show this message.
    -i, --image  <image>
        Path to coreboot image (Default is $COREBOOT_IMAGE).
    -r, --remote-host  <host>
        Obtain machine information from remote host (using ssh).
    -s, --serial-device  </dev/xxx>
        Obtain boot log via serial device.
    -S, --serial-speed  <speed>
        Set the port speed for the serial device (Default is $SERIAL_PORT_SPEED).
    -u, --upload-results
        Upload results to coreboot.org.

Long options:
    --ssh-port <port>
        Use a specific SSH port.
"
}

getopt -T
if [ $? -ne 4 ]; then
	echo "GNU-compatible getopt(1) required."
	exit $EXIT_FAILURE
fi

LONGOPTS="cbmem:,clobber,help,image:,remote-host:,upload-results"
LONGOPTS="${LONGOPTS},serial-device:,serial-speed:"
LONGOPTS="${LONGOPTS},ssh-port:"

ARGS=$(getopt -o c:n:Chi:r:s:S:u -l "$LONGOPTS" -n "$0" -- "$@");
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$ARGS"
while true ; do
	case "$1" in
		# generic options
		-c|--cbmem)
			shift
			CBMEM_PATH="$1"
			;;
		-n|--nvramtool)
			shift
			NVRAMTOOL_PATH="$1"
			;;
		-C|--clobber)
			CLOBBER_OUTPUT=1
			;;
		-h|--help)
			show_help
			exit $EXIT_SUCCESS
			;;
		-i|--image)
			shift
			COREBOOT_IMAGE="$1"
			;;
		-r|--remote-host)
			shift
			REMOTE_HOST="$1"
			;;
		-u|--upload-results)
			UPLOAD_RESULTS=1
			;;

		# serial port options
		-s|--serial-device)
			shift
			SERIAL_DEVICE="$1"
			;;
		-S|--serial-speed)
			shift
			SERIAL_PORT_SPEED="$1"
			;;

		# ssh options
		--ssh-port)
			shift
			REMOTE_PORT_OPTION="-p $1"
			;;

		# error handling
		--)
			shift
			if [ -n "$*" ]; then
				echo "Non-option parameters detected: '$*'"
				exit $EXIT_FAILURE
			fi
			break
			;;
		*)
			echo "error processing options at '$1'"
			exit $EXIT_FAILURE
	esac
	shift
done

grep -rH 'coreboot.org' .git/config >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "Script must be run from root of coreboot directory"
	exit $EXIT_FAILURE
fi

if [ ! -e "$COREBOOT_IMAGE" ]; then
	echo "board_status needs $COREBOOT_IMAGE, but it does not exist."
	echo "Use \"-i IMAGE_FILE\" to select a different image, or \"--help\" for more options."
	exit $EXIT_FAILURE
fi

# Results will be placed in a temporary location until we're ready to upload.
# If the user does not wish to upload, results will remain in /tmp.
tmpdir=$(mktemp -d --tmpdir coreboot_board_status.XXXXXXXX)

# Obtain coreboot config by running cbfstool on the ROM image. cbfstool may
# already exist in build/ or util/cbfstool/, but if not then we'll build it
# now and clean it when we're done.
cbfstool_cmd="build/cbfstool"
do_clean_cbfstool=0
if [ ! -x $cbfstool_cmd ]; then
	cbfstool_cmd="util/cbfstool/cbfstool"
	if [ -e $cbfstool_cmd ]; then
		if test ! -x $cbfstool_cmd; then
			echo "Cannot execute $cbfstool_cmd."
			exit $EXIT_FAILURE
		fi
	else
		make -C util/cbfstool/
		do_clean_cbfstool=1
	fi
fi
test_cmd $LOCAL "$cbfstool_cmd"

tmpcfg=$(mktemp coreboot_config.XXXXXX)
echo "Extracting config.txt from $COREBOOT_IMAGE"
$cbfstool_cmd "$COREBOOT_IMAGE" extract -n config -f "${tmpdir}/config.txt" >/dev/null 2>&1
mv "${tmpdir}/config.txt" "${tmpdir}/config.short.txt"
cp "${tmpdir}/config.short.txt" "${tmpcfg}"
yes "" | make "DOTCONFIG=${tmpcfg}" oldconfig 2>/dev/null >/dev/null
mv "${tmpcfg}" "${tmpdir}/config.txt"
rm -f "${tmpcfg}.old"
$cbfstool_cmd "$COREBOOT_IMAGE" print > "${tmpdir}/cbfs.txt"
rom_contents=$($cbfstool_cmd "$COREBOOT_IMAGE" print 2>&1)
if [ -n "$(echo $rom_contents | grep payload_config)" ]; then
	echo "Extracting payload_config from $COREBOOT_IMAGE"
	$cbfstool_cmd "$COREBOOT_IMAGE" extract -n payload_config -f "${tmpdir}/payload_config.txt" >/dev/null 2>&1
fi
if [ -n "$(echo $rom_contents | grep payload_version)" ]; then
	echo "Extracting payload_version from $COREBOOT_IMAGE"
	$cbfstool_cmd "$COREBOOT_IMAGE" extract -n payload_version -f "${tmpdir}/payload_version.txt" >/dev/null 2>&1
fi
md5sum -b "$COREBOOT_IMAGE" > "${tmpdir}/rom_checksum.txt"

if test $do_clean_cbfstool -eq 1; then
	make -C util/cbfstool clean
fi

# Obtain board and revision info to form the directory structure:
# <vendor>/<board>/<revision>/<timestamp>
mainboard_dir="$(grep CONFIG_MAINBOARD_DIR "${tmpdir}/config.txt" | awk -F '"' '{ print $2 }')"
vendor=$(echo "$mainboard_dir" | awk -F '/' '{ print $1 }')
mainboard=$(echo "$mainboard_dir" | awk -F '/' '{ print $2 }')

getrevision="util/board_status/getrevision.sh"
test_cmd $LOCAL $getrevision
tagged_version=$($getrevision -T)
timestamp=$($getrevision -t)

results="${vendor}/${mainboard}/${tagged_version}/${timestamp}"

if [ -n "$(echo $tagged_version | grep dirty)" ]; then
	echo "The repository is in a dirty state. Please see the output of"
	echo "'git status' below."
	git status
	exit $EXIT_FAILURE
fi

echo "Temporarily placing output in ${tmpdir}/${results}"
mkdir -p "${tmpdir}/${results}"

mv "${tmpdir}/config.txt" "${tmpdir}/${results}"
test -f "${tmpdir}/payload_config.txt" && mv "${tmpdir}/payload_config.txt" "${tmpdir}/${results}"
test -f "${tmpdir}/payload_version.txt" && mv "${tmpdir}/payload_version.txt" "${tmpdir}/${results}"
mv "${tmpdir}/config.short.txt" "${tmpdir}/${results}"
mv "${tmpdir}/cbfs.txt" "${tmpdir}/${results}"
mv "${tmpdir}/rom_checksum.txt" "${tmpdir}/${results}"

touch "${tmpdir}/${results}/revision.txt"
printf "Local revision: %s\n" "$($getrevision -l)" >> "${tmpdir}/${results}/revision.txt"
printf "Tagged revision: %s\n" "${tagged_version}" >> "${tmpdir}/${results}/revision.txt"
printf "Upstream revision: %s\n" "$($getrevision -u)" >> "${tmpdir}/${results}/revision.txt"
printf "Upstream URL: %s\n" "$($getrevision -U)" >> "${tmpdir}/${results}/revision.txt"
printf "Timestamp: %s\n" "$timestamp" >> "${tmpdir}/${results}/revision.txt"

if [ -n "$CBMEM_PATH" ]; then
	cbmem_cmd="$CBMEM_PATH"
else
	cbmem_cmd="cbmem"
fi

cmos_enabled=0
if grep -q "CONFIG_USE_OPTION_TABLE=y" "${tmpdir}/${results}/config.short.txt" > /dev/null; then
	cmos_enabled=1
fi

if [ -n "$NVRAMTOOL_PATH" ]; then
	nvramtool_cmd="$NVRAMTOOL_PATH"
else
	nvramtool_cmd="nvramtool"
fi

if [ -n "$SERIAL_DEVICE" ]; then
	get_serial_bootlog "$SERIAL_DEVICE" "$SERIAL_PORT_SPEED" "${tmpdir}/${results}/coreboot_console.txt"
elif [ -n "$REMOTE_HOST" ]; then
	echo "Verifying that CBMEM is available on remote device"
	test_cmd $REMOTE "$cbmem_cmd"
	echo "Getting coreboot boot log"
	cmd $REMOTE "$cbmem_cmd -1" "${tmpdir}/${results}/coreboot_console.txt"
	echo "Getting timestamp data"
	cmd_nonfatal $REMOTE "$cbmem_cmd -t" "${tmpdir}/${results}/coreboot_timestamps.txt"

	if [ "$cmos_enabled" -eq 1 ]; then
		echo "Verifying that nvramtool is available on remote device"
		test_cmd $REMOTE "$nvramtool_cmd"
		echo "Getting all CMOS values"
		cmd $REMOTE "$nvramtool_cmd -a" "${tmpdir}/${results}/cmos_values.txt"
	fi

	echo "Getting remote dmesg"
	cmd $REMOTE dmesg "${tmpdir}/${results}/kernel_log.txt"
else
	echo "Verifying that CBMEM is available"
	if [ $(id -u) -ne 0 ]; then
		command -v "$cbmem_cmd" >/dev/null
		if [ $? -ne 0 ]; then
			echo "Failed to run $cbmem_cmd. Check \$PATH or" \
			"use -c to specify path to cbmem binary."
			exit $EXIT_FAILURE
		else
			cbmem_cmd="sudo $cbmem_cmd"
		fi
	else
		test_cmd $LOCAL "$cbmem_cmd"
	fi

	echo "Getting coreboot boot log"
	cmd $LOCAL "$cbmem_cmd -1" "${tmpdir}/${results}/coreboot_console.txt"

	echo "Getting timestamp data"
	cmd_nonfatal $LOCAL "$cbmem_cmd -t" "${tmpdir}/${results}/coreboot_timestamps.txt"

	if [ "$cmos_enabled" -eq 1 ]; then
		echo "Verifying that nvramtool is available"
		if [ $(id -u) -ne 0 ]; then
			command -v "$nvramtool_cmd" >/dev/null
			if [ $? -ne 0 ]; then
				echo "Failed to run $nvramtool_cmd. Check \$PATH or" \
				"use -n to specify path to nvramtool binary."
				exit $EXIT_FAILURE
			else
				nvramtool_cmd="sudo $nvramtool_cmd"
			fi
		else
			test_cmd $LOCAL "$nvramtool_cmd"
		fi

		echo "Getting all CMOS values"
		cmd $LOCAL "$nvramtool_cmd -a" "${tmpdir}/${results}/cmos_values.txt"
	fi

	echo "Getting local dmesg"
	cmd $LOCAL "sudo dmesg" "${tmpdir}/${results}/kernel_log.txt"
fi

#
# Check files
#
if [ $(grep -- -dirty "${tmpdir}/${results}/coreboot_console.txt") ]; then
	echo "coreboot or the payload are built from a source tree in a" \
	"dirty state, making it hard to reproduce the result. Please" \
	"check in your source tree with 'git status'."
	exit $EXIT_FAILURE
fi

if [ $(grep -- unknown "${tmpdir}/${results}/coreboot_timestamps.txt") ]; then
	echo "Unknown timestamps found in 'coreboot_timestamps.txt'." \
	"Please rebuild the 'cbmem' utility and try again."
	exit $EXIT_FAILURE
fi

#
# Finish up.
#
coreboot_dir=$(pwd)
if [ $UPLOAD_RESULTS -eq 1 ]; then
	# extract username from ssh://<username>@review.coreboot.org/blah
	bsrepo=$(git config --get remote.origin.url | sed "s,\(.*\)/coreboot,\1/board-status,")

	cd "util/board_status/"
	if [ ! -e "board-status" ]; then
		# FIXME: the board-status directory might get big over time.
		# Is there a way we can push the results without fetching the
		# whole repo?
		git clone "$bsrepo"
		if [ $? -ne 0 ]; then
			echo "Error cloning board-status repo, aborting."
			exit $EXIT_FAILURE
		fi
	fi

	cd "board-status"

	echo "Checking for duplicate results"
	# get any updates to board-status
	git pull

	echo "${tagged_version}" | grep dirty >/dev/null 2>&1
	clean_version=$?
	existing_results=$(git ls-files "${mainboard_dir}/${tagged_version}")

	# reject duplicate results of non-dirty versions
	if [ "${clean_version}" -eq 1 ] && [ -n "${existing_results}" ] ; then
		echo "Result is a duplicate, aborting"
		exit $EXIT_FAILURE
	fi

	echo "Copying results to $(pwd)/${results}"

	# Note: Result directory should be unique due to the timestamp.
	cp -R "${tmpdir}/${vendor}" .

	echo "Uploading results"
	git add "${vendor}"
	git commit -a -m "${mainboard_dir}/${tagged_version}/${timestamp}"
	count=0
	until git push origin master || test $count -eq 3; do
	        git pull --rebase
		count=$((count + 1))
	done

	# Results have been uploaded so it's pointless to keep the
	# temporary files around.
	rm -rf "${tmpdir}"
	if test $count -eq 3; then
		echo "Error uploading to board-status repo, aborting."
		exit $EXIT_FAILURE
	fi
fi
cd "$coreboot_dir"

if [ $CLOBBER_OUTPUT -eq 1 ]; then
	rm -rf "${tmpdir}"
else
	if [ $UPLOAD_RESULTS -eq 1 ]; then
		echo
		echo "output files are in $(dirname $0)/board-status/${mainboard_dir}/${tagged_version}/${timestamp}"
	else
		echo
		echo "output files are in ${tmpdir}/${results}"
	fi
fi

exit $EXIT_SUCCESS