David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 1 | #!/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 | |
| 25 | EXIT_SUCCESS=0 |
| 26 | EXIT_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). |
| 31 | if [ -z "$FLASHROM" ] ; then |
| 32 | FLASHROM="../flashrom" |
| 33 | fi |
| 34 | echo "testing flashrom binary: ${FLASHROM}" |
| 35 | |
| 36 | OLDDIR=$(pwd) |
| 37 | if [ -z "$TMPDIR" ]; then |
| 38 | TMPDIR=$(mktemp -d) # test data location |
| 39 | fi |
| 40 | |
| 41 | ZERO_4K="00_4k.bin" |
| 42 | FF_4K="ff_4k.bin" |
| 43 | FF_4K_TEXT="ff_4k.txt" |
| 44 | |
| 45 | TESTFILE="test.bin" |
| 46 | |
| 47 | which uuencode > /dev/null |
| 48 | if [ "$?" != "0" ] ; then |
| 49 | echo "uuencode is required to use this script" |
| 50 | exit $EXIT_FAILURE |
| 51 | fi |
| 52 | |
| 53 | which flashrom > /dev/null |
| 54 | if [ "$?" != "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 |
| 59 | fi |
| 60 | |
| 61 | # Keep a copy of flashrom with the test data |
| 62 | cp "$FLASHROM" "${TMPDIR}/" |
| 63 | cd "$TMPDIR" |
| 64 | echo "Running test in ${TMPDIR}" |
| 65 | |
| 66 | # Make 4k worth of 0xff bytes |
| 67 | echo "begin 640 $FF_4K" > "$FF_4K_TEXT" |
| 68 | for i in `seq 0 90` ; do |
| 69 | echo "M____________________________________________________________" >> "$FF_4K_TEXT" |
| 70 | done |
| 71 | echo "!_P``" >> "$FF_4K_TEXT" |
| 72 | echo "\`" >> "$FF_4K_TEXT" |
| 73 | echo "end" >> "$FF_4K_TEXT" |
| 74 | uudecode -o "$FF_4K" "$FF_4K_TEXT" |
| 75 | rm -f "$FF_4K_TEXT" |
| 76 | |
| 77 | # Make 4k worth of 0x00 bytes |
| 78 | dd if=/dev/zero of="$ZERO_4K" bs=1 count=4096 2> /dev/null |
| 79 | echo "ffh pattern written in ${FF_4K}" |
| 80 | echo "00h pattern written in ${ZERO_4K}" |
| 81 | |
| 82 | echo "Reading BIOS image" |
| 83 | BIOS="bios.bin" |
David Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 84 | flashrom ${FLASHROM_PARAM} -r "$BIOS" > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 85 | echo "Original image saved as ${BIOS}" |
| 86 | |
| 87 | # $1: exit code |
| 88 | do_exit() { |
| 89 | echo "restoring original bios image using system's flashrom" |
David Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 90 | flashrom ${FLASHROM_PARAM} -w "$BIOS" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 91 | echo "test files remain in ${TMPDIR}" |
| 92 | cd "$OLDDIR" |
| 93 | exit "$1" |
| 94 | } |
| 95 | |
| 96 | # |
| 97 | # Actual tests are performed below. |
| 98 | # |
| 99 | NUM_REGIONS=16 |
| 100 | |
| 101 | # Make a layout - 4K regions on 4K boundaries. This will test basic |
| 102 | # functionality of erasing and writing specific blocks. |
| 103 | echo " |
| 104 | 0x000000:0x000fff 00_0 |
| 105 | 0x001000:0x001fff ff_0 |
| 106 | |
| 107 | 0x002000:0x002fff 00_1 |
| 108 | 0x003000:0x003fff ff_1 |
| 109 | |
| 110 | 0x004000:0x004fff 00_2 |
| 111 | 0x005000:0x005fff ff_2 |
| 112 | |
| 113 | 0x006000:0x006fff 00_3 |
| 114 | 0x007000:0x007fff ff_3 |
| 115 | |
| 116 | 0x008000:0x008fff 00_4 |
| 117 | 0x009000:0x009fff ff_4 |
| 118 | |
| 119 | 0x00a000:0x00afff 00_5 |
| 120 | 0x00b000:0x00bfff ff_5 |
| 121 | |
| 122 | 0x00c000:0x00cfff 00_6 |
| 123 | 0x00d000:0x00dfff ff_6 |
| 124 | |
| 125 | 0x00e000:0x00efff 00_7 |
| 126 | 0x00f000:0x00ffff ff_7 |
| 127 | |
| 128 | 0x010000:0x010fff 00_8 |
| 129 | 0x011000:0x011fff ff_8 |
| 130 | |
| 131 | 0x012000:0x012fff 00_9 |
| 132 | 0x013000:0x013fff ff_9 |
| 133 | |
| 134 | 0x014000:0x014fff 00_10 |
| 135 | 0x015000:0x015fff ff_10 |
| 136 | |
| 137 | 0x016000:0x016fff 00_11 |
| 138 | 0x017000:0x017fff ff_11 |
| 139 | |
| 140 | 0x018000:0x018fff 00_12 |
| 141 | 0x019000:0x019fff ff_12 |
| 142 | |
| 143 | 0x01a000:0x01afff 00_13 |
| 144 | 0x01b000:0x01bfff ff_13 |
| 145 | |
| 146 | 0x01c000:0x01cfff 00_14 |
| 147 | 0x01d000:0x01dfff ff_14 |
| 148 | |
| 149 | 0x01e000:0x01efff 00_15 |
| 150 | 0x01f000:0x01ffff ff_15 |
| 151 | " > layout_4k_aligned.txt |
| 152 | |
| 153 | cp "$BIOS" "$TESTFILE" |
| 154 | i=0 |
| 155 | while [ $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 Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 160 | ./flashrom ${FLASHROM_PARAM} -l layout_4k_aligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 161 | 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 Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 169 | flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 170 | 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)) |
| 179 | done |
| 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 | # |
| 191 | echo " |
| 192 | 0x000800:0x0017ff 00_0 |
| 193 | 0x001800:0x0027ff ff_0 |
| 194 | |
| 195 | 0x002800:0x0037ff 00_1 |
| 196 | 0x003800:0x0047ff ff_1 |
| 197 | |
| 198 | 0x004800:0x0057ff 00_2 |
| 199 | 0x005800:0x0067ff ff_2 |
| 200 | |
| 201 | 0x006800:0x0077ff 00_3 |
| 202 | 0x007800:0x0087ff ff_3 |
| 203 | |
| 204 | 0x008800:0x0097ff 00_4 |
| 205 | 0x009800:0x00a7ff ff_4 |
| 206 | |
| 207 | 0x00a800:0x00b7ff 00_5 |
| 208 | 0x00b800:0x00c7ff ff_5 |
| 209 | |
| 210 | 0x00c800:0x00d7ff 00_6 |
| 211 | 0x00d800:0x00e7ff ff_6 |
| 212 | |
| 213 | 0x00e800:0x00f7ff 00_7 |
| 214 | 0x00f800:0x0107ff ff_7 |
| 215 | |
| 216 | 0x010800:0x0117ff 00_8 |
| 217 | 0x011800:0x0127ff ff_8 |
| 218 | |
| 219 | 0x012800:0x0137ff 00_9 |
| 220 | 0x013800:0x0147ff ff_9 |
| 221 | |
| 222 | 0x014800:0x0157ff 00_10 |
| 223 | 0x015800:0x0167ff ff_10 |
| 224 | |
| 225 | 0x016800:0x0177ff 00_11 |
| 226 | 0x017800:0x0187ff ff_11 |
| 227 | |
| 228 | 0x018800:0x0197ff 00_12 |
| 229 | 0x019800:0x01a7ff ff_12 |
| 230 | |
| 231 | 0x01a800:0x01b7ff 00_13 |
| 232 | 0x01b800:0x01c7ff ff_13 |
| 233 | |
| 234 | 0x01c800:0x01d7ff 00_14 |
| 235 | 0x01d800:0x01e7ff ff_14 |
| 236 | |
| 237 | 0x01e800:0x01f7ff 00_15 |
| 238 | 0x01f800:0x01ffff ff_15 |
| 239 | " > layout_unaligned.txt |
| 240 | |
| 241 | # reset the test file and ROM to the original state |
David Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 242 | flashrom ${FLASHROM_PARAM} -w "$BIOS" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 243 | cp "$BIOS" "$TESTFILE" |
| 244 | |
| 245 | i=0 |
| 246 | while [ $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 Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 259 | ./flashrom ${FLASHROM_PARAM} -l layout_unaligned.txt -i 00_${i} -i ff_${i} -w "$TESTFILE" > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 260 | 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 Hendricks | 04c8379 | 2010-10-29 21:54:16 +0000 | [diff] [blame^] | 268 | flashrom ${FLASHROM_PARAM} -r difftest.bin > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 269 | 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)) |
| 278 | done |
| 279 | |
| 280 | echo "Result: PASS" |
| 281 | do_exit "$EXIT_SUCCESS" |