partial_writes_x86_bios.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # TODO: We need to make sure to force re-writes when desired since flashrom
  26. # may skip regions which do not need to be re-written.
  27. #
  28. . "$(pwd)/common.sh"
  29. logfile="${0}.log"
  30. zero_4k="00_4k.bin"
  31. ff_4k="ff_4k.bin"
  32. ff_4k_text="ff_4k.txt"
  33. testfile="test.bin"
  34. partial_writes_fail()
  35. {
  36. echo "$1" >> ${logfile}
  37. echo "$0: failed" >> ${logfile}
  38. exit ${EXIT_FAILURE}
  39. }
  40. # FIXME: this is a chromium-os -ism. most distros don't strip out "diff"...
  41. which diff > /dev/null || partial_writes_fail "diff is required to use this script"
  42. which uuencode > /dev/null || partial_writes_fail "uuencode is required to use this script"
  43. # Make 4k worth of 0xff bytes
  44. echo "begin 640 $ff_4k" > "$ff_4k_text"
  45. i=0
  46. while [ $i -le 90 ] ; do
  47. echo "M____________________________________________________________" >> "$ff_4k_text"
  48. i=$((${i} + 1))
  49. done
  50. echo "!_P``" >> "$ff_4k_text"
  51. echo "\`" >> "$ff_4k_text"
  52. echo "end" >> "$ff_4k_text"
  53. uudecode -o "$ff_4k" "$ff_4k_text"
  54. rm -f "$ff_4k_text"
  55. # Make 4k worth of 0x00 bytes
  56. dd if=/dev/zero of="$zero_4k" bs=1 count=4096 2> /dev/null
  57. echo "ffh pattern written in ${ff_4k}" >> ${logfile}
  58. echo "00h pattern written in ${zero_4k}" >> ${logfile}
  59. #
  60. # Actual tests are performed below.
  61. #
  62. num_regions=16
  63. # Make a layout - 4K regions on 4K boundaries. This will test basic
  64. # functionality of erasing and writing specific blocks.
  65. offset=0
  66. for i in `seq 0 $((${num_regions} - 1))` ; do
  67. offset_00=$((${i} * 8192))
  68. offset_ff=$((${i} * 8192 + 4096))
  69. echo "\
  70. `printf 0x%x $((${start} + ${offset_00}))`:`printf 0x%x $((${start} + ${offset_00} + 0xfff))` 00_${i}
  71. `printf 0x%x $((${start} + ${offset_ff}))`:`printf 0x%x $((${start} + ${offset_ff} + 0xfff))` ff_${i}
  72. " >> layout_bios_4k_aligned.txt
  73. done
  74. cp "${BACKUP}" "$testfile"
  75. i=0
  76. while [ $i -lt $num_regions ] ; do
  77. tmpstr="aligned region ${i} test: "
  78. offset=$((${i} * 8192))
  79. dd if=${zero_4k} of=${testfile} bs=1 conv=notrunc seek=${offset} 2> /dev/null
  80. dd if=${ff_4k} of=${testfile} bs=1 conv=notrunc seek=$((${offset} + 4096)) 2> /dev/null
  81. do_test_flashrom -l layout_bios_4k_aligned.txt -i 00_${i} -i ff_${i} -w "$testfile"
  82. if [ $? -ne 0 ] ; then
  83. partial_writes_fail "${tmpstr}failed to flash region"
  84. fi
  85. # download the entire ROM image and use diff to compare to ensure
  86. # flashrom logic does not violate user-specified regions
  87. system_flashrom -r difftest.bin
  88. diff -q difftest.bin "$testfile"
  89. if [ $? -ne 0 ] ; then
  90. partial_writes_fail "${tmpstr}failed diff test"
  91. fi
  92. rm -f difftest.bin
  93. i=$((${i} + 1))
  94. echo "${tmpstr}passed" | tee -a ${logfile}
  95. done
  96. # Make a layout - 4K regions on 4.5K boundaries. This will help find problems
  97. # with logic that only operates on part of a block. For example, if a user
  98. # wishes to re-write a fraction of a block, then:
  99. # 1. The whole block must be erased.
  100. # 2. The old content must be restored at unspecified offsets.
  101. # 3. The new content must be written at specified offsets.
  102. #
  103. # Note: The last chunk of 0xff bytes is only 2K as to avoid overrunning a 128KB
  104. # test image.
  105. #
  106. for i in `seq 0 $((${num_regions} - 1))` ; do
  107. offset_00=$((${i} * 8192 + 2048))
  108. offset_ff=$((${i} * 8192 + 4096 + 2048))
  109. echo "\
  110. `printf 0x%06x $((${start} + ${offset_00}))`:`printf 0x%06x $((${start} + ${offset_00} + 0xfff))` 00_${i}
  111. `printf 0x%06x $((${start} + ${offset_ff}))`:`printf 0x%06x $((${start} + ${offset_ff} + 0xfff))` ff_${i}
  112. " >> layout_bios_unaligned.txt
  113. done
  114. # reset the test file and ROM to the original state
  115. system_flashrom -w "${BACKUP}"
  116. cp "$BACKUP" "$testfile"
  117. i=0
  118. while [ $i -lt $num_regions ] ; do
  119. tmpstr="unaligned region ${i} test: "
  120. offset=$(($((${i} * 8192)) + 2048))
  121. # Protect against too long write
  122. writelen=4096
  123. if [ $((${offset} + 4096 + 4096)) -ge 131072 ]; then
  124. writelen=$((131072 - $((${offset} + 4096))))
  125. if [ ${writelen} -lt 0 ]; then
  126. writelen=0
  127. fi
  128. fi
  129. dd if=${zero_4k} of=${testfile} bs=1 conv=notrunc seek=${offset} 2> /dev/null
  130. dd if=${ff_4k} of=${testfile} bs=1 conv=notrunc seek=$((${offset} + 4096)) count=writelen 2> /dev/null
  131. do_test_flashrom -l layout_bios_unaligned.txt -i 00_${i} -i ff_${i} -w "$testfile"
  132. if [ $? -ne 0 ] ; then
  133. partial_writes_fail "${tmpstr} failed to flash region"
  134. fi
  135. # download the entire ROM image and use diff to compare to ensure
  136. # flashrom logic does not violate user-specified regions
  137. system_flashrom -r difftest.bin
  138. diff -q difftest.bin "$testfile"
  139. if [ $? -ne 0 ] ; then
  140. partial_writes_fail "${tmpstr} failed diff test"
  141. fi
  142. rm -f difftest.bin
  143. i=$((${i} + 1))
  144. echo "${tmpstr}passed" | tee -a ${logfile}
  145. done
  146. return "$EXIT_SUCCESS"