blob: 7cf37c91a89ef84ada6ee4ddbdd1963cdd811c43 [file] [log] [blame]
David Hendricks444cefc2010-10-29 20:17:41 +00001#!/bin/sh
2#
3# Copyright (C) 2010 Google Inc.
4# Written by David Hendricks for Google Inc.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
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# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19#
20# This script attempts to test Flashrom partial write capability by writing
21# patterns of 0xff and 0x00 bytes to the lowest 128KB of flash. 128KB is chosen
22# since 64KB is usually the largest possible block size, so we will try to
23# cover at least two blocks with this test.
24
25EXIT_SUCCESS=0
26EXIT_FAILURE=1
27
28# The copy of flashrom to test. If unset, we'll assume the user wants to test
29# a newly built flashrom binary in the parent directory (this script should
30# reside in flashrom/util).
31if [ -z "$FLASHROM" ] ; then
32 FLASHROM="../flashrom"
33fi
34echo "testing flashrom binary: ${FLASHROM}"
35
36OLDDIR=$(pwd)
37if [ -z "$TMPDIR" ]; then
38 TMPDIR=$(mktemp -d) # test data location
39fi
40
41ZERO_4K="00_4k.bin"
42FF_4K="ff_4k.bin"
43FF_4K_TEXT="ff_4k.txt"
44
45TESTFILE="test.bin"
46
47which uuencode > /dev/null
48if [ "$?" != "0" ] ; then
49 echo "uuencode is required to use this script"
50 exit $EXIT_FAILURE
51fi
52
53which flashrom > /dev/null
54if [ "$?" != "0" ] ; then
55 echo "Please install a stable version of flashrom in your path."
56 echo "This will be used to compare the test flashrom binary and "
57 echo "restore your firmware image at the end of the test."
58 exit $EXIT_FAILURE
59fi
60
61# Keep a copy of flashrom with the test data
62cp "$FLASHROM" "${TMPDIR}/"
63cd "$TMPDIR"
64echo "Running test in ${TMPDIR}"
65
66# Make 4k worth of 0xff bytes
67echo "begin 640 $FF_4K" > "$FF_4K_TEXT"
68for i in `seq 0 90` ; do
69 echo "M____________________________________________________________" >> "$FF_4K_TEXT"
70done
71echo "!_P``" >> "$FF_4K_TEXT"
72echo "\`" >> "$FF_4K_TEXT"
73echo "end" >> "$FF_4K_TEXT"
74uudecode -o "$FF_4K" "$FF_4K_TEXT"
75rm -f "$FF_4K_TEXT"
76
77# Make 4k worth of 0x00 bytes
78dd if=/dev/zero of="$ZERO_4K" bs=1 count=4096 2> /dev/null
79echo "ffh pattern written in ${FF_4K}"
80echo "00h pattern written in ${ZERO_4K}"
81
82echo "Reading BIOS image"
83BIOS="bios.bin"
David Hendricks04c83792010-10-29 21:54:16 +000084flashrom ${FLASHROM_PARAM} -r "$BIOS" > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +000085echo "Original image saved as ${BIOS}"
86
87# $1: exit code
88do_exit() {
89 echo "restoring original bios image using system's flashrom"
David Hendricks04c83792010-10-29 21:54:16 +000090 flashrom ${FLASHROM_PARAM} -w "$BIOS"
David Hendricks444cefc2010-10-29 20:17:41 +000091 echo "test files remain in ${TMPDIR}"
92 cd "$OLDDIR"
93 exit "$1"
94}
95
96#
97# Actual tests are performed below.
98#
99NUM_REGIONS=16
100
101# Make a layout - 4K regions on 4K boundaries. This will test basic
102# functionality of erasing and writing specific blocks.
103echo "
1040x000000:0x000fff 00_0
1050x001000:0x001fff ff_0
106
1070x002000:0x002fff 00_1
1080x003000:0x003fff ff_1
109
1100x004000:0x004fff 00_2
1110x005000:0x005fff ff_2
112
1130x006000:0x006fff 00_3
1140x007000:0x007fff ff_3
115
1160x008000:0x008fff 00_4
1170x009000:0x009fff ff_4
118
1190x00a000:0x00afff 00_5
1200x00b000:0x00bfff ff_5
121
1220x00c000:0x00cfff 00_6
1230x00d000:0x00dfff ff_6
124
1250x00e000:0x00efff 00_7
1260x00f000:0x00ffff ff_7
127
1280x010000:0x010fff 00_8
1290x011000:0x011fff ff_8
130
1310x012000:0x012fff 00_9
1320x013000:0x013fff ff_9
133
1340x014000:0x014fff 00_10
1350x015000:0x015fff ff_10
136
1370x016000:0x016fff 00_11
1380x017000:0x017fff ff_11
139
1400x018000:0x018fff 00_12
1410x019000:0x019fff ff_12
142
1430x01a000:0x01afff 00_13
1440x01b000:0x01bfff ff_13
145
1460x01c000:0x01cfff 00_14
1470x01d000:0x01dfff ff_14
148
1490x01e000:0x01efff 00_15
1500x01f000:0x01ffff ff_15
151" > layout_4k_aligned.txt
152
153cp "$BIOS" "$TESTFILE"
154i=0
155while [ $i -lt $NUM_REGIONS ] ; do
156 offset=$((${i} * 8192))
157 dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null
158 dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) 2> /dev/null
159
David Hendricks04c83792010-10-29 21:54:16 +0000160 ./flashrom ${FLASHROM_PARAM} -l layout_4k_aligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000161 if [ "$?" != "0" ] ; then
162 echo "partial flash failed on iteration ${i}"
163 echo "Result: FAIL"
164 do_exit "$EXIT_FAILURE"
165 fi
166
167 # download the entire ROM image and use diff to compare to ensure
168 # flashrom logic does not violate user-specified regions
David Hendricks04c83792010-10-29 21:54:16 +0000169 flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000170 diff -q difftest.bin "$TESTFILE"
171 if [ "$?" != "0" ] ; then
172 echo "diff test failed on iteration ${i}"
173 echo "Result: FAIL"
174 do_exit "$EXIT_FAILURE"
175 fi
176 rm -f difftest.bin
177
178 i=$((${i} + 1))
179done
180
181# Make a layout - 4K regions on 4.5K boundaries. This will help find problems
182# with logic that only operates on part of a block. For example, if a user
183# wishes to re-write a fraction of a block, then:
184# 1. The whole block must be erased.
185# 2. The old content must be restored at unspecified offsets.
186# 3. The new content must be written at specified offsets.
187#
188# Note: The last chunk of 0xff bytes is only 2K as to avoid overrunning a 128KB
189# test image.
190#
191echo "
1920x000800:0x0017ff 00_0
1930x001800:0x0027ff ff_0
194
1950x002800:0x0037ff 00_1
1960x003800:0x0047ff ff_1
197
1980x004800:0x0057ff 00_2
1990x005800:0x0067ff ff_2
200
2010x006800:0x0077ff 00_3
2020x007800:0x0087ff ff_3
203
2040x008800:0x0097ff 00_4
2050x009800:0x00a7ff ff_4
206
2070x00a800:0x00b7ff 00_5
2080x00b800:0x00c7ff ff_5
209
2100x00c800:0x00d7ff 00_6
2110x00d800:0x00e7ff ff_6
212
2130x00e800:0x00f7ff 00_7
2140x00f800:0x0107ff ff_7
215
2160x010800:0x0117ff 00_8
2170x011800:0x0127ff ff_8
218
2190x012800:0x0137ff 00_9
2200x013800:0x0147ff ff_9
221
2220x014800:0x0157ff 00_10
2230x015800:0x0167ff ff_10
224
2250x016800:0x0177ff 00_11
2260x017800:0x0187ff ff_11
227
2280x018800:0x0197ff 00_12
2290x019800:0x01a7ff ff_12
230
2310x01a800:0x01b7ff 00_13
2320x01b800:0x01c7ff ff_13
233
2340x01c800:0x01d7ff 00_14
2350x01d800:0x01e7ff ff_14
236
2370x01e800:0x01f7ff 00_15
2380x01f800:0x01ffff ff_15
239" > layout_unaligned.txt
240
241# reset the test file and ROM to the original state
David Hendricks04c83792010-10-29 21:54:16 +0000242flashrom ${FLASHROM_PARAM} -w "$BIOS"
David Hendricks444cefc2010-10-29 20:17:41 +0000243cp "$BIOS" "$TESTFILE"
244
245i=0
246while [ $i -lt $NUM_REGIONS ] ; do
247 offset=$(($((${i} * 8192)) + 2048))
248 # Protect against too long write
249 writelen=4096
250 if [ $((${offset} + 4096 + 4096)) -ge 131072 ]; then
251 writelen=$((131072 - $((${offset} + 4096))))
252 if [ ${writelen} -lt 0 ]; then
253 writelen=0
254 fi
255 fi
256 dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null
257 dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) count=writelen 2> /dev/null
258
David Hendricks04c83792010-10-29 21:54:16 +0000259 ./flashrom ${FLASHROM_PARAM} -l layout_unaligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000260 if [ "$?" != "0" ] ; then
261 echo "partial flash failed on iteration ${i}"
262 echo "Result: FAIL"
263 do_exit "$EXIT_FAILURE"
264 fi
265
266 # download the entire ROM image and use diff to compare to ensure
267 # flashrom logic does not violate user-specified regions
David Hendricks04c83792010-10-29 21:54:16 +0000268 flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000269 diff -q difftest.bin "$TESTFILE"
270 if [ "$?" != "0" ] ; then
271 echo "diff test failed on iteration ${i}"
272 echo "Result: FAIL"
273 do_exit "$EXIT_FAILURE"
274 fi
275 rm -f difftest.bin
276
277 i=$((${i} + 1))
278done
279
280echo "Result: PASS"
281do_exit "$EXIT_SUCCESS"