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 | # |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 16 | # This script attempts to test Flashprog partial write capability by writing |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 17 | # patterns of 0xff and 0x00 bytes to the lowest 128kB of flash. 128kB is chosen |
| 18 | # since 64kB is usually the largest possible block size, so we will try to |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 19 | # cover at least two blocks with this test. |
| 20 | |
| 21 | EXIT_SUCCESS=0 |
| 22 | EXIT_FAILURE=1 |
| 23 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 24 | # The copy of flashprog to test. If unset, we'll assume the user wants to test |
| 25 | # a newly built flashprog binary in the parent directory (this script should |
| 26 | # reside in flashprog/util). |
| 27 | if [ -z "$FLASHPROG" ] ; then |
| 28 | FLASHPROG="../flashprog" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 29 | fi |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 30 | echo "testing flashprog binary: ${FLASHPROG}" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 31 | |
| 32 | OLDDIR=$(pwd) |
Carl-Daniel Hailfinger | eb6337f | 2010-11-02 00:16:27 +0000 | [diff] [blame] | 33 | |
| 34 | # test data location |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 35 | TMPDIR=$(mktemp -d -t flashprog_test.XXXXXXXXXX) |
Carl-Daniel Hailfinger | eb6337f | 2010-11-02 00:16:27 +0000 | [diff] [blame] | 36 | if [ "$?" != "0" ] ; then |
| 37 | echo "Could not create temporary directory" |
| 38 | exit $EXIT_FAILURE |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 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 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 53 | which flashprog > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 54 | if [ "$?" != "0" ] ; then |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 55 | echo "Please install a stable version of flashprog in your path." |
| 56 | echo "This will be used to compare the test flashprog binary and " |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 57 | echo "restore your firmware image at the end of the test." |
| 58 | exit $EXIT_FAILURE |
| 59 | fi |
| 60 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 61 | # Keep a copy of flashprog with the test data |
| 62 | cp "$FLASHPROG" "${TMPDIR}/" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 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" |
Carl-Daniel Hailfinger | eb6337f | 2010-11-02 00:16:27 +0000 | [diff] [blame] | 68 | i=0 |
| 69 | while [ $i -le 90 ] ; do |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 70 | echo "M____________________________________________________________" >> "$FF_4K_TEXT" |
Carl-Daniel Hailfinger | eb6337f | 2010-11-02 00:16:27 +0000 | [diff] [blame] | 71 | i=$((${i} + 1)) |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 72 | done |
| 73 | echo "!_P``" >> "$FF_4K_TEXT" |
| 74 | echo "\`" >> "$FF_4K_TEXT" |
| 75 | echo "end" >> "$FF_4K_TEXT" |
| 76 | uudecode -o "$FF_4K" "$FF_4K_TEXT" |
| 77 | rm -f "$FF_4K_TEXT" |
| 78 | |
| 79 | # Make 4k worth of 0x00 bytes |
| 80 | dd if=/dev/zero of="$ZERO_4K" bs=1 count=4096 2> /dev/null |
| 81 | echo "ffh pattern written in ${FF_4K}" |
| 82 | echo "00h pattern written in ${ZERO_4K}" |
| 83 | |
| 84 | echo "Reading BIOS image" |
| 85 | BIOS="bios.bin" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 86 | flashprog ${FLASHPROG_PARAM} -r "$BIOS" > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 87 | echo "Original image saved as ${BIOS}" |
| 88 | |
| 89 | # $1: exit code |
| 90 | do_exit() { |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 91 | if [ ${1} -eq ${EXIT_FAILURE} ] ; then |
| 92 | echo "Result: FAILED" |
| 93 | else |
| 94 | echo "Result: PASSED" |
| 95 | fi |
| 96 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 97 | echo "restoring original bios image using system's flashprog" |
| 98 | flashprog ${FLASHPROG_PARAM} -w "$BIOS" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 99 | echo "test files remain in ${TMPDIR}" |
| 100 | cd "$OLDDIR" |
| 101 | exit "$1" |
| 102 | } |
| 103 | |
| 104 | # |
| 105 | # Actual tests are performed below. |
| 106 | # |
| 107 | NUM_REGIONS=16 |
| 108 | |
| 109 | # Make a layout - 4K regions on 4K boundaries. This will test basic |
| 110 | # functionality of erasing and writing specific blocks. |
| 111 | echo " |
| 112 | 0x000000:0x000fff 00_0 |
| 113 | 0x001000:0x001fff ff_0 |
| 114 | |
| 115 | 0x002000:0x002fff 00_1 |
| 116 | 0x003000:0x003fff ff_1 |
| 117 | |
| 118 | 0x004000:0x004fff 00_2 |
| 119 | 0x005000:0x005fff ff_2 |
| 120 | |
| 121 | 0x006000:0x006fff 00_3 |
| 122 | 0x007000:0x007fff ff_3 |
| 123 | |
| 124 | 0x008000:0x008fff 00_4 |
| 125 | 0x009000:0x009fff ff_4 |
| 126 | |
| 127 | 0x00a000:0x00afff 00_5 |
| 128 | 0x00b000:0x00bfff ff_5 |
| 129 | |
| 130 | 0x00c000:0x00cfff 00_6 |
| 131 | 0x00d000:0x00dfff ff_6 |
| 132 | |
| 133 | 0x00e000:0x00efff 00_7 |
| 134 | 0x00f000:0x00ffff ff_7 |
| 135 | |
| 136 | 0x010000:0x010fff 00_8 |
| 137 | 0x011000:0x011fff ff_8 |
| 138 | |
| 139 | 0x012000:0x012fff 00_9 |
| 140 | 0x013000:0x013fff ff_9 |
| 141 | |
| 142 | 0x014000:0x014fff 00_10 |
| 143 | 0x015000:0x015fff ff_10 |
| 144 | |
| 145 | 0x016000:0x016fff 00_11 |
| 146 | 0x017000:0x017fff ff_11 |
| 147 | |
| 148 | 0x018000:0x018fff 00_12 |
| 149 | 0x019000:0x019fff ff_12 |
| 150 | |
| 151 | 0x01a000:0x01afff 00_13 |
| 152 | 0x01b000:0x01bfff ff_13 |
| 153 | |
| 154 | 0x01c000:0x01cfff 00_14 |
| 155 | 0x01d000:0x01dfff ff_14 |
| 156 | |
| 157 | 0x01e000:0x01efff 00_15 |
| 158 | 0x01f000:0x01ffff ff_15 |
| 159 | " > layout_4k_aligned.txt |
| 160 | |
| 161 | cp "$BIOS" "$TESTFILE" |
| 162 | i=0 |
| 163 | while [ $i -lt $NUM_REGIONS ] ; do |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 164 | echo -n "aligned region ${i} test: " |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 165 | offset=$((${i} * 8192)) |
| 166 | dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null |
| 167 | dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) 2> /dev/null |
| 168 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 169 | ./flashprog ${FLASHPROG_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] | 170 | if [ "$?" != "0" ] ; then |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 171 | echo "failed to flash region" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 172 | do_exit "$EXIT_FAILURE" |
| 173 | fi |
| 174 | |
| 175 | # download the entire ROM image and use diff to compare to ensure |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 176 | # flashprog logic does not violate user-specified regions |
| 177 | flashprog ${FLASHPROG_PARAM} -r difftest.bin > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 178 | diff -q difftest.bin "$TESTFILE" |
| 179 | if [ "$?" != "0" ] ; then |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 180 | echo "failed diff test" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 181 | do_exit "$EXIT_FAILURE" |
| 182 | fi |
| 183 | rm -f difftest.bin |
| 184 | |
| 185 | i=$((${i} + 1)) |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 186 | echo "passed" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 187 | done |
| 188 | |
| 189 | # Make a layout - 4K regions on 4.5K boundaries. This will help find problems |
| 190 | # with logic that only operates on part of a block. For example, if a user |
| 191 | # wishes to re-write a fraction of a block, then: |
| 192 | # 1. The whole block must be erased. |
| 193 | # 2. The old content must be restored at unspecified offsets. |
| 194 | # 3. The new content must be written at specified offsets. |
| 195 | # |
Stefan Tauner | c0aaf95 | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 196 | # Note: The last chunk of 0xff bytes is only 2k as to avoid overrunning a 128kB |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 197 | # test image. |
| 198 | # |
| 199 | echo " |
| 200 | 0x000800:0x0017ff 00_0 |
| 201 | 0x001800:0x0027ff ff_0 |
| 202 | |
| 203 | 0x002800:0x0037ff 00_1 |
| 204 | 0x003800:0x0047ff ff_1 |
| 205 | |
| 206 | 0x004800:0x0057ff 00_2 |
| 207 | 0x005800:0x0067ff ff_2 |
| 208 | |
| 209 | 0x006800:0x0077ff 00_3 |
| 210 | 0x007800:0x0087ff ff_3 |
| 211 | |
| 212 | 0x008800:0x0097ff 00_4 |
| 213 | 0x009800:0x00a7ff ff_4 |
| 214 | |
| 215 | 0x00a800:0x00b7ff 00_5 |
| 216 | 0x00b800:0x00c7ff ff_5 |
| 217 | |
| 218 | 0x00c800:0x00d7ff 00_6 |
| 219 | 0x00d800:0x00e7ff ff_6 |
| 220 | |
| 221 | 0x00e800:0x00f7ff 00_7 |
| 222 | 0x00f800:0x0107ff ff_7 |
| 223 | |
| 224 | 0x010800:0x0117ff 00_8 |
| 225 | 0x011800:0x0127ff ff_8 |
| 226 | |
| 227 | 0x012800:0x0137ff 00_9 |
| 228 | 0x013800:0x0147ff ff_9 |
| 229 | |
| 230 | 0x014800:0x0157ff 00_10 |
| 231 | 0x015800:0x0167ff ff_10 |
| 232 | |
| 233 | 0x016800:0x0177ff 00_11 |
| 234 | 0x017800:0x0187ff ff_11 |
| 235 | |
| 236 | 0x018800:0x0197ff 00_12 |
| 237 | 0x019800:0x01a7ff ff_12 |
| 238 | |
| 239 | 0x01a800:0x01b7ff 00_13 |
| 240 | 0x01b800:0x01c7ff ff_13 |
| 241 | |
| 242 | 0x01c800:0x01d7ff 00_14 |
| 243 | 0x01d800:0x01e7ff ff_14 |
| 244 | |
| 245 | 0x01e800:0x01f7ff 00_15 |
| 246 | 0x01f800:0x01ffff ff_15 |
| 247 | " > layout_unaligned.txt |
| 248 | |
| 249 | # reset the test file and ROM to the original state |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 250 | flashprog ${FLASHPROG_PARAM} -w "$BIOS" > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 251 | cp "$BIOS" "$TESTFILE" |
| 252 | |
| 253 | i=0 |
| 254 | while [ $i -lt $NUM_REGIONS ] ; do |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 255 | echo -n "unaligned region ${i} test: " |
| 256 | |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 257 | offset=$(($((${i} * 8192)) + 2048)) |
| 258 | # Protect against too long write |
| 259 | writelen=4096 |
| 260 | if [ $((${offset} + 4096 + 4096)) -ge 131072 ]; then |
| 261 | writelen=$((131072 - $((${offset} + 4096)))) |
| 262 | if [ ${writelen} -lt 0 ]; then |
| 263 | writelen=0 |
| 264 | fi |
| 265 | fi |
| 266 | dd if=${ZERO_4K} of=${TESTFILE} bs=1 conv=notrunc seek=${offset} 2> /dev/null |
| 267 | dd if=${FF_4K} of=${TESTFILE} bs=1 conv=notrunc seek=$((${offset} + 4096)) count=writelen 2> /dev/null |
| 268 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 269 | ./flashprog ${FLASHPROG_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] | 270 | if [ "$?" != "0" ] ; then |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 271 | echo "failed to flash region" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 272 | do_exit "$EXIT_FAILURE" |
| 273 | fi |
| 274 | |
| 275 | # download the entire ROM image and use diff to compare to ensure |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame^] | 276 | # flashprog logic does not violate user-specified regions |
| 277 | flashprog ${FLASHPROG_PARAM} -r difftest.bin > /dev/null |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 278 | diff -q difftest.bin "$TESTFILE" |
| 279 | if [ "$?" != "0" ] ; then |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 280 | echo "failed diff test" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 281 | do_exit "$EXIT_FAILURE" |
| 282 | fi |
| 283 | rm -f difftest.bin |
| 284 | |
| 285 | i=$((${i} + 1)) |
David Hendricks | 738f8e2 | 2010-11-02 03:03:38 +0000 | [diff] [blame] | 286 | echo "passed" |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 287 | done |
| 288 | |
David Hendricks | 444cefc | 2010-10-29 20:17:41 +0000 | [diff] [blame] | 289 | do_exit "$EXIT_SUCCESS" |