blob: 69a692351fc5ba3747052b55007be6ccdad56536 [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)
Carl-Daniel Hailfingereb6337f2010-11-02 00:16:27 +000037
38# test data location
39TMPDIR=$(mktemp -d -t flashrom_test.XXXXXXXXXX)
40if [ "$?" != "0" ] ; then
41 echo "Could not create temporary directory"
42 exit $EXIT_FAILURE
David Hendricks444cefc2010-10-29 20:17:41 +000043fi
44
45ZERO_4K="00_4k.bin"
46FF_4K="ff_4k.bin"
47FF_4K_TEXT="ff_4k.txt"
48
49TESTFILE="test.bin"
50
51which uuencode > /dev/null
52if [ "$?" != "0" ] ; then
53 echo "uuencode is required to use this script"
54 exit $EXIT_FAILURE
55fi
56
57which flashrom > /dev/null
58if [ "$?" != "0" ] ; then
59 echo "Please install a stable version of flashrom in your path."
60 echo "This will be used to compare the test flashrom binary and "
61 echo "restore your firmware image at the end of the test."
62 exit $EXIT_FAILURE
63fi
64
65# Keep a copy of flashrom with the test data
66cp "$FLASHROM" "${TMPDIR}/"
67cd "$TMPDIR"
68echo "Running test in ${TMPDIR}"
69
70# Make 4k worth of 0xff bytes
71echo "begin 640 $FF_4K" > "$FF_4K_TEXT"
Carl-Daniel Hailfingereb6337f2010-11-02 00:16:27 +000072i=0
73while [ $i -le 90 ] ; do
David Hendricks444cefc2010-10-29 20:17:41 +000074 echo "M____________________________________________________________" >> "$FF_4K_TEXT"
Carl-Daniel Hailfingereb6337f2010-11-02 00:16:27 +000075 i=$((${i} + 1))
David Hendricks444cefc2010-10-29 20:17:41 +000076done
77echo "!_P``" >> "$FF_4K_TEXT"
78echo "\`" >> "$FF_4K_TEXT"
79echo "end" >> "$FF_4K_TEXT"
80uudecode -o "$FF_4K" "$FF_4K_TEXT"
81rm -f "$FF_4K_TEXT"
82
83# Make 4k worth of 0x00 bytes
84dd if=/dev/zero of="$ZERO_4K" bs=1 count=4096 2> /dev/null
85echo "ffh pattern written in ${FF_4K}"
86echo "00h pattern written in ${ZERO_4K}"
87
88echo "Reading BIOS image"
89BIOS="bios.bin"
David Hendricks04c83792010-10-29 21:54:16 +000090flashrom ${FLASHROM_PARAM} -r "$BIOS" > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +000091echo "Original image saved as ${BIOS}"
92
93# $1: exit code
94do_exit() {
David Hendricks738f8e22010-11-02 03:03:38 +000095 if [ ${1} -eq ${EXIT_FAILURE} ] ; then
96 echo "Result: FAILED"
97 else
98 echo "Result: PASSED"
99 fi
100
David Hendricks444cefc2010-10-29 20:17:41 +0000101 echo "restoring original bios image using system's flashrom"
David Hendricks04c83792010-10-29 21:54:16 +0000102 flashrom ${FLASHROM_PARAM} -w "$BIOS"
David Hendricks444cefc2010-10-29 20:17:41 +0000103 echo "test files remain in ${TMPDIR}"
104 cd "$OLDDIR"
105 exit "$1"
106}
107
108#
109# Actual tests are performed below.
110#
111NUM_REGIONS=16
112
113# Make a layout - 4K regions on 4K boundaries. This will test basic
114# functionality of erasing and writing specific blocks.
115echo "
1160x000000:0x000fff 00_0
1170x001000:0x001fff ff_0
118
1190x002000:0x002fff 00_1
1200x003000:0x003fff ff_1
121
1220x004000:0x004fff 00_2
1230x005000:0x005fff ff_2
124
1250x006000:0x006fff 00_3
1260x007000:0x007fff ff_3
127
1280x008000:0x008fff 00_4
1290x009000:0x009fff ff_4
130
1310x00a000:0x00afff 00_5
1320x00b000:0x00bfff ff_5
133
1340x00c000:0x00cfff 00_6
1350x00d000:0x00dfff ff_6
136
1370x00e000:0x00efff 00_7
1380x00f000:0x00ffff ff_7
139
1400x010000:0x010fff 00_8
1410x011000:0x011fff ff_8
142
1430x012000:0x012fff 00_9
1440x013000:0x013fff ff_9
145
1460x014000:0x014fff 00_10
1470x015000:0x015fff ff_10
148
1490x016000:0x016fff 00_11
1500x017000:0x017fff ff_11
151
1520x018000:0x018fff 00_12
1530x019000:0x019fff ff_12
154
1550x01a000:0x01afff 00_13
1560x01b000:0x01bfff ff_13
157
1580x01c000:0x01cfff 00_14
1590x01d000:0x01dfff ff_14
160
1610x01e000:0x01efff 00_15
1620x01f000:0x01ffff ff_15
163" > layout_4k_aligned.txt
164
165cp "$BIOS" "$TESTFILE"
166i=0
167while [ $i -lt $NUM_REGIONS ] ; do
David Hendricks738f8e22010-11-02 03:03:38 +0000168 echo -n "aligned region ${i} test: "
David Hendricks444cefc2010-10-29 20:17:41 +0000169 offset=$((${i} * 8192))
170 dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null
171 dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) 2> /dev/null
172
David Hendricks04c83792010-10-29 21:54:16 +0000173 ./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 +0000174 if [ "$?" != "0" ] ; then
David Hendricks738f8e22010-11-02 03:03:38 +0000175 echo "failed to flash region"
David Hendricks444cefc2010-10-29 20:17:41 +0000176 do_exit "$EXIT_FAILURE"
177 fi
178
179 # download the entire ROM image and use diff to compare to ensure
180 # flashrom logic does not violate user-specified regions
David Hendricks04c83792010-10-29 21:54:16 +0000181 flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000182 diff -q difftest.bin "$TESTFILE"
183 if [ "$?" != "0" ] ; then
David Hendricks738f8e22010-11-02 03:03:38 +0000184 echo "failed diff test"
David Hendricks444cefc2010-10-29 20:17:41 +0000185 do_exit "$EXIT_FAILURE"
186 fi
187 rm -f difftest.bin
188
189 i=$((${i} + 1))
David Hendricks738f8e22010-11-02 03:03:38 +0000190 echo "passed"
David Hendricks444cefc2010-10-29 20:17:41 +0000191done
192
193# Make a layout - 4K regions on 4.5K boundaries. This will help find problems
194# with logic that only operates on part of a block. For example, if a user
195# wishes to re-write a fraction of a block, then:
196# 1. The whole block must be erased.
197# 2. The old content must be restored at unspecified offsets.
198# 3. The new content must be written at specified offsets.
199#
200# Note: The last chunk of 0xff bytes is only 2K as to avoid overrunning a 128KB
201# test image.
202#
203echo "
2040x000800:0x0017ff 00_0
2050x001800:0x0027ff ff_0
206
2070x002800:0x0037ff 00_1
2080x003800:0x0047ff ff_1
209
2100x004800:0x0057ff 00_2
2110x005800:0x0067ff ff_2
212
2130x006800:0x0077ff 00_3
2140x007800:0x0087ff ff_3
215
2160x008800:0x0097ff 00_4
2170x009800:0x00a7ff ff_4
218
2190x00a800:0x00b7ff 00_5
2200x00b800:0x00c7ff ff_5
221
2220x00c800:0x00d7ff 00_6
2230x00d800:0x00e7ff ff_6
224
2250x00e800:0x00f7ff 00_7
2260x00f800:0x0107ff ff_7
227
2280x010800:0x0117ff 00_8
2290x011800:0x0127ff ff_8
230
2310x012800:0x0137ff 00_9
2320x013800:0x0147ff ff_9
233
2340x014800:0x0157ff 00_10
2350x015800:0x0167ff ff_10
236
2370x016800:0x0177ff 00_11
2380x017800:0x0187ff ff_11
239
2400x018800:0x0197ff 00_12
2410x019800:0x01a7ff ff_12
242
2430x01a800:0x01b7ff 00_13
2440x01b800:0x01c7ff ff_13
245
2460x01c800:0x01d7ff 00_14
2470x01d800:0x01e7ff ff_14
248
2490x01e800:0x01f7ff 00_15
2500x01f800:0x01ffff ff_15
251" > layout_unaligned.txt
252
253# reset the test file and ROM to the original state
David Hendricks738f8e22010-11-02 03:03:38 +0000254flashrom ${FLASHROM_PARAM} -w "$BIOS" > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000255cp "$BIOS" "$TESTFILE"
256
257i=0
258while [ $i -lt $NUM_REGIONS ] ; do
David Hendricks738f8e22010-11-02 03:03:38 +0000259 echo -n "unaligned region ${i} test: "
260
David Hendricks444cefc2010-10-29 20:17:41 +0000261 offset=$(($((${i} * 8192)) + 2048))
262 # Protect against too long write
263 writelen=4096
264 if [ $((${offset} + 4096 + 4096)) -ge 131072 ]; then
265 writelen=$((131072 - $((${offset} + 4096))))
266 if [ ${writelen} -lt 0 ]; then
267 writelen=0
268 fi
269 fi
270 dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null
271 dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) count=writelen 2> /dev/null
272
David Hendricks04c83792010-10-29 21:54:16 +0000273 ./flashrom ${FLASHROM_PARAM} -l layout_unaligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000274 if [ "$?" != "0" ] ; then
David Hendricks738f8e22010-11-02 03:03:38 +0000275 echo "failed to flash region"
David Hendricks444cefc2010-10-29 20:17:41 +0000276 do_exit "$EXIT_FAILURE"
277 fi
278
279 # download the entire ROM image and use diff to compare to ensure
280 # flashrom logic does not violate user-specified regions
David Hendricks04c83792010-10-29 21:54:16 +0000281 flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null
David Hendricks444cefc2010-10-29 20:17:41 +0000282 diff -q difftest.bin "$TESTFILE"
283 if [ "$?" != "0" ] ; then
David Hendricks738f8e22010-11-02 03:03:38 +0000284 echo "failed diff test"
David Hendricks444cefc2010-10-29 20:17:41 +0000285 do_exit "$EXIT_FAILURE"
286 fi
287 rm -f difftest.bin
288
289 i=$((${i} + 1))
David Hendricks738f8e22010-11-02 03:03:38 +0000290 echo "passed"
David Hendricks444cefc2010-10-29 20:17:41 +0000291done
292
David Hendricks444cefc2010-10-29 20:17:41 +0000293do_exit "$EXIT_SUCCESS"