test_v2.sh 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2016 Google Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. EXIT_SUCCESS=0
  19. EXIT_FAILURE=1
  20. RC=$EXIT_SUCCESS
  21. FATAL=0
  22. NONFATAL=1
  23. #
  24. # Stuff obtained from command-line
  25. #
  26. # Generic options
  27. BACKUP_IMAGE=""
  28. OLD_FLASHROM=""
  29. NEW_FLASHROM=""
  30. NO_CLEAN=0
  31. SKIP_CONSISTENCY_CHECK=0
  32. UPLOAD_RESULTS=0
  33. # LOCAL_FLASHROM is required if both a secondary programmer *and* a remote host
  34. # are used since OLD_FLASHROM and NEW_FLASHROM will be called on the remote
  35. # host and we need a local copy of flashrom to control the secondary programmer.
  36. # By default this will be set to the result of `which flashrom`.
  37. LOCAL_FLASHROM=""
  38. # Primary/Secondary programmer names
  39. PRIMARY_PROG=""
  40. SECONDARY_PROG=""
  41. # Primary/Secondary programmer options
  42. PRIMARY_OPTS=""
  43. SECONDARY_OPTS=""
  44. # Calls preflash_hook() and postflash_hook() before and after doing a command.
  45. CUSTOM_HOOKS_FILENAME=""
  46. # if doing wp test, we require the commands that the programmer uses to enable/disable wp
  47. WP_HOOKS_FILENAME="";
  48. # logfile to store the script's output
  49. SCRIPT_LOGFILE="flashrom-test_script_output.txt"
  50. # Information stored to restore dut WP state at the end of a writeprotect test.
  51. HW_WP_STATE=0
  52. SW_WP_STATE=0
  53. WP_RANGE_START=0
  54. WP_RANGE_LEN=0
  55. # Test type
  56. TEST_TYPE_UNKNOWN=0
  57. TEST_TYPE_SINGLE=1
  58. TEST_TYPE_ENDURANCE=2
  59. TEST_TYPE_WRITEPROTECT=3
  60. TEST_TYPE=$TEST_TYPE_UNKNOWN
  61. # Region modes
  62. REGION_MODE_UNKNOWN=0
  63. REGION_MODE_CLOBBER=1
  64. REGION_MODE_DESCRIPTOR=2
  65. REGION_MODE_FLASHMAP=3
  66. REGION_MODE_LAYOUT=4
  67. REGION_MODE=$REGION_MODE_UNKNOWN
  68. DESCRIPTOR_REGION="BIOS"
  69. FLASHMAP_REGION="RW_SECTION_A"
  70. LAYOUT_FILE=""
  71. LAYOUT_REGION="RW"
  72. SMALL_REGION=0
  73. # Remote testing options
  74. SSH_PORT=""
  75. REMOTE_HOST=""
  76. REMOTE_PORT_OPTION=""
  77. REMOTE_ONLY=0
  78. REMOTE_PROGRAMMER_PARAMS=""
  79. SSH_CMD="ssh $REMOTE_PORT_OPTION root@${REMOTE_HOST} command -v"
  80. LOCAL=0
  81. REMOTE=1
  82. DO_REMOTE=0 # boolean to use for cmd() and tests
  83. WP_DISABLE=0
  84. WP_ENABLE=1
  85. # In case we need to run flashrom locally and we're not already root.
  86. SUDO_CMD=""
  87. if [ "$(id -u)" -ne "0" ]; then
  88. SUDO_CMD="sudo"
  89. fi
  90. # 1KB
  91. K=1024
  92. show_help() {
  93. printf "Usage:
  94. ${0} <options>
  95. General options:
  96. -b, --backup-image <path>
  97. Backup image to write unconditionally at end of testing.
  98. -h, --help
  99. Show this message.
  100. -l, --layout-file <path>
  101. Layout file (required if mode is \"layout\", resides locally).
  102. -m, --mode <arg>
  103. Region access mode (clobber, descriptor, flashmap, layout).
  104. -n, --new <path>
  105. Path to new version of flashrom.
  106. -o, --old <path>
  107. Path to old (stable) version of flashrom.
  108. -p, --primary-programmer <parameters>
  109. Primary programmer options.
  110. -r, --remote-host <host>
  111. Remote host to test primary programmer on.
  112. -s, --secondary-programmer <parameters>
  113. Secondary programmer options.
  114. -t, --type <arg>
  115. Test type (single, endurance, writeprotect).
  116. -u, --upload-results
  117. Upload results to flashrom.org.
  118. Long options:
  119. --custom-hooks <filename>
  120. Supply a script with custom hooks to run before and after commands.
  121. --descriptor-region <name>
  122. Specify region to use in descriptor mode (default: $DESCRIPTOR_REGION)
  123. --flashmap-region <name>
  124. Specify region to use in flashmap mode (default: $FLASHMAP_REGION)
  125. --layout-region <name>
  126. Specify region to use in layout mode (default: $LAYOUT_REGION)
  127. --local-flashrom <path>
  128. Path to local version of flashrom when using both a secondary programmer
  129. and remote host (default: $($SUDO_CMD which flashrom))
  130. --no-clean
  131. Do not remove temporary files.
  132. --skip-consistency-check
  133. Skip the consistency check (two consecutive reads) at beginning.
  134. --small-region
  135. Omit tests that require large amounts of space (>16KB).
  136. --wp-hooks <filename>
  137. Supply a script with commands that the connected external programmer
  138. uses to enable and disable write protect for the DUT. This is required
  139. when running a write-protect test.
  140. Remote connectivity options:
  141. --ssh-port <port>
  142. Use a specific SSH port.
  143. See documentation for usage examples (TODO: Migrate https://goo.gl/3jNoL7
  144. to flashrom wiki).\n
  145. "
  146. }
  147. getopt -T
  148. if [ $? -ne 4 ]; then
  149. printf "GNU-compatible getopt(1) required.\n"
  150. exit $EXIT_FAILURE
  151. fi
  152. LONGOPTS="backup-image:,help,,new:,old:,remote-host:,upload-results:"
  153. LONGOPTS="${LONGOPTS},primary-programmer:,secondary-programmer:,local-flashrom:"
  154. LONGOPTS="${LONGOPTS},custom-hooks:,mode:,skip-consistency-check,small-region"
  155. LONGOPTS="${LONGOPTS},wp-hooks:,type:"
  156. LONGOPTS="${LONGOPTS},layout-file:,descriptor-region:,flashmap-region:,layout-region:"
  157. LONGOPTS="${LONGOPTS},no-clean"
  158. LONGOPTS="${LONGOPTS},ssh-port:"
  159. ARGS=$(getopt -o b:hl:m:n:o:p:r:s:t:u -l "$LONGOPTS" -n "$0" -- "$@");
  160. if [ $? != 0 ] ; then printf "Terminating...\n" >&2 ; exit 1 ; fi
  161. eval set -- "$ARGS"
  162. while true ; do
  163. case "$1" in
  164. # Generic options
  165. -b|--backup-image)
  166. shift
  167. BACKUP_IMAGE="$1"
  168. ;;
  169. -h|--help)
  170. show_help
  171. exit $EXIT_SUCCESS
  172. ;;
  173. -l|--layout-file)
  174. shift
  175. LAYOUT_FILE="$1"
  176. ;;
  177. -m|--mode)
  178. shift
  179. if [ "$1" = "clobber" ]; then
  180. REGION_MODE=$REGION_MODE_CLOBBER
  181. elif [ "$1" = "descriptor" ]; then
  182. REGION_MODE=$REGION_MODE_DESCRIPTOR
  183. elif [ "$1" = "flashmap" ]; then
  184. REGION_MODE=$REGION_MODE_FLASHMAP
  185. elif [ "$1" = "layout" ]; then
  186. REGION_MODE=$REGION_MODE_LAYOUT
  187. else
  188. printf "Unknown mode: $1\n"
  189. exit $EXIT_FAILURE
  190. fi
  191. ;;
  192. -n|--new)
  193. shift
  194. NEW_FLASHROM="$1"
  195. ;;
  196. -o|--old)
  197. shift
  198. OLD_FLASHROM="$1"
  199. ;;
  200. -p|--primary_programmer)
  201. shift
  202. PRIMARY_OPTS="-p $1"
  203. ;;
  204. -s|--secondary_programmer)
  205. shift
  206. SECONDARY_OPTS="-p $1"
  207. ;;
  208. -t|--type)
  209. shift
  210. if [ "$1" = "single" ]; then
  211. TEST_TYPE=$TEST_TYPE_SINGLE
  212. elif [ "$1" = "endurance" ]; then
  213. TEST_TYPE=$TEST_TYPE_ENDURANCE
  214. elif [ "$1" = "writeprotect" ]; then
  215. TEST_TYPE=$TEST_TYPE_WRITEPROTECT
  216. else
  217. printf "Unknown type: $1\n"
  218. exit $EXIT_FAILURE
  219. fi
  220. ;;
  221. -r|--remote-host)
  222. DO_REMOTE=1
  223. shift
  224. REMOTE_HOST="$1"
  225. ;;
  226. -u|--upload-results)
  227. UPLOAD_RESULTS=1
  228. ;;
  229. # Longopts only
  230. --custom-hooks)
  231. shift
  232. CUSTOM_HOOKS_FILENAME="$1"
  233. ;;
  234. --descriptor-region)
  235. shift
  236. DESCRIPTOR_REGION="$1"
  237. ;;
  238. --flashmap-region)
  239. shift
  240. FLASHMAP_REGION="$1"
  241. ;;
  242. --layout-region)
  243. shift
  244. LAYOUT_REGION="$1"
  245. ;;
  246. --local-flashrom)
  247. shift
  248. LOCAL_FLASHROM="$1"
  249. ;;
  250. --no-clean)
  251. NO_CLEAN=1
  252. ;;
  253. --skip-consistency-check)
  254. SKIP_CONSISTENCY_CHECK=1
  255. ;;
  256. --small-region)
  257. SMALL_REGION=1
  258. ;;
  259. --wp-hooks)
  260. shift
  261. WP_HOOKS_FILENAME="$1"
  262. ;;
  263. # Remote testing options
  264. --ssh-port)
  265. shift
  266. REMOTE_PORT_OPTION="-p $1"
  267. ;;
  268. # error handling
  269. --)
  270. shift
  271. if [ -n "$*" ]; then
  272. printf "Non-option parameters detected: '$*'\n"
  273. exit $EXIT_FAILURE
  274. fi
  275. break
  276. ;;
  277. *)
  278. printf "error processing options at '$1'\n"
  279. exit $EXIT_FAILURE
  280. esac
  281. shift
  282. done
  283. # TODO: Implement this.
  284. if [ $UPLOAD_RESULTS -eq 1 ]; then
  285. printf "TODO: Implement ability to upload results.\n"
  286. exit $EXIT_FAILURE
  287. fi
  288. #
  289. # Source helper scripts
  290. #
  291. export REMOTE_HOST REMOTE_PORT_OPTION
  292. export LOCAL REMOTE FATAL NONFATAL EXIT_SUCCESS EXIT_FAILURE
  293. export CUSTOM_HOOKS_FILENAME SUDO_CMD
  294. . "$(pwd)/tests/tests_v2/cmd.sh"
  295. # We will set up a logs directory within the tmpdirs to store
  296. # all output logs.
  297. LOGS="logs"
  298. # Setup temporary working directories:
  299. # LOCAL_TMPDIR: Working directory on local host.
  300. # REMOTE_TMPDIR: Working directory on remote host.
  301. # TMPDIR: The temporary directy in which we do most of the work. This is
  302. # convenient for commands that depend on $DO_REMOTE.
  303. LOCAL_TMPDIR=$(mktemp -d --tmpdir flashrom_test.XXXXXXXX)
  304. if [ $? -ne 0 ] ; then
  305. printf "Could not create temporary directory\n"
  306. exit $EXIT_FAILURE
  307. fi
  308. mkdir "${LOCAL_TMPDIR}/${LOGS}"
  309. if [ $DO_REMOTE -eq 1 ]; then
  310. REMOTE_TMPDIR=$(ssh root@${REMOTE_HOST} mktemp -d --tmpdir flashrom_test.XXXXXXXX)
  311. if [ $? -ne 0 ] ; then
  312. printf "Could not create temporary directory\n"
  313. exit $EXIT_FAILURE
  314. fi
  315. scmd $REMOTE "mkdir ${REMOTE_TMPDIR}/${LOGS}"
  316. fi
  317. if [ $DO_REMOTE -eq 0 ]; then
  318. TMPDIR="$LOCAL_TMPDIR"
  319. else
  320. TMPDIR="$REMOTE_TMPDIR"
  321. fi
  322. #
  323. # Test command-line validity.
  324. #
  325. if [ $TEST_TYPE -eq $TEST_TYPE_UNKNOWN ]; then
  326. printf "Must specify a test type (-t/--type).\n"
  327. exit $EXIT_FAILURE
  328. elif [ $TEST_TYPE -eq $TEST_TYPE_SINGLE ]; then
  329. if [ $REGION_MODE -eq $REGION_MODE_UNKNOWN ]; then
  330. printf "Must specify a region access mode (-m/--mode).\n"
  331. exit $EXIT_FAILURE
  332. elif [ $REGION_MODE -eq $REGION_MODE_LAYOUT ]; then
  333. if [ -z "$LAYOUT_FILE" ]; then
  334. printf "Must specify a layout file when using layout mode.\n"
  335. exit $EXIT_FAILURE
  336. fi
  337. scmd $DO_REMOTE "stat $LAYOUT_FILE"
  338. if [ $? -ne 0 ]; then
  339. if [ $DO_REMOTE -eq 1 ]; then
  340. tmp=" on remote host $REMOTE_HOST."
  341. else
  342. tmp=" on local host."
  343. fi
  344. printf "Layout file $LAYOUT_FILE not found${tmp}\n"
  345. exit $EXIT_FAILURE
  346. fi
  347. if [ $DO_REMOTE -eq 1 ]; then
  348. scp root@"${REMOTE_HOST}:$LAYOUT_FILE" "${LOCAL_TMPDIR}/" 2>&1 >/dev/null
  349. fi
  350. fi
  351. elif [ $TEST_TYPE -eq $TEST_TYPE_WRITEPROTECT ]; then
  352. if [ -z "$WP_HOOKS_FILENAME" ] || [ ! -e "$WP_HOOKS_FILENAME" ]; then
  353. printf "Must specify a wp hooks file when doing a write-protect test.\n"
  354. exit $EXIT_FAILURE
  355. fi
  356. . "$WP_HOOKS_FILENAME"
  357. fi
  358. if [ $DO_REMOTE -eq 1 ]; then
  359. # Test connection to remote host
  360. test_cmd $DO_REMOTE "ls /" $NONFATAL
  361. if [ $? -ne 0 ]; then
  362. printf "Could not connect to remote host ${REMOTE_HOST}\n"
  363. exit $EXIT_FAILURE
  364. fi
  365. fi
  366. # Remote host and secondary programmer are in use, so either the user must
  367. # specify a local version of flashrom to control the secondary programmer
  368. # or it must be found in the default path.
  369. if [ $DO_REMOTE -eq 1 ] && [ -n "$SECONDARY_OPTS" ]; then
  370. if [ -z "$LOCAL_FLASHROM" ]; then
  371. LOCAL_FLASHROM="$($SUDO_CMD which flashrom)"
  372. fi
  373. if [ ! -e "$LOCAL_FLASHROM" ]; then
  374. printf "$LOCAL_FLASHROM does not exist\n"
  375. exit $EXIT_FAILURE
  376. fi
  377. fi
  378. #
  379. # Dependencies
  380. #
  381. # cmp is used to compare files
  382. test_cmd $DO_REMOTE "cmp" $FATAL
  383. if [ ! -e "/dev/urandom" ]; then
  384. printf "This script uses /dev/urandom\n"
  385. exit $EXIT_FAILURE
  386. fi
  387. if [ ! -e "/dev/zero" ]; then
  388. printf "This script uses /dev/zero\n"
  389. exit $EXIT_FAILURE
  390. fi
  391. #
  392. # Setup.
  393. #
  394. grep -rH 'projectname = .*flashrom' .git/config >/dev/null 2>&1
  395. if [ $? -ne 0 ]; then
  396. printf "Script must be run from root of flashrom directory\n"
  397. exit $EXIT_FAILURE
  398. fi
  399. if [ -z "$OLD_FLASHROM" ]; then
  400. if [ $DO_REMOTE -eq 1 ]; then
  401. OLD_FLASHROM="$(ssh root@${REMOTE_HOST} which flashrom)"
  402. else
  403. OLD_FLASHROM="$($SUDO_CMD which flashrom)"
  404. fi
  405. fi
  406. test_cmd $DO_REMOTE "$OLD_FLASHROM --help" $NONFATAL
  407. if [ $? -ne 0 ]; then
  408. printf "Old flashrom binary is not usable.\n"
  409. exit $EXIT_FAILURE
  410. fi
  411. # Check if both flashrom binaries support logging
  412. scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS --flash-name -o ${TMPDIR}/flash_name.txt"
  413. if [ $? -ne 0 ]; then
  414. printf "Old flashrom binary does not support logging.\n"
  415. exit $EXIT_FAILURE
  416. fi
  417. scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS --flash-name -o ${TMPDIR}/flash_name.txt"
  418. if [ $? -ne 0 ]; then
  419. printf "New flashrom binary does not support logging.\n"
  420. exit $EXIT_FAILURE
  421. fi
  422. scmd $DO_REMOTE "rm -f ${TMPDIR}/flash_name.txt"
  423. # print $1 and store it in the script log file
  424. print_and_log()
  425. {
  426. printf "$1" | tee -a "${LOCAL_TMPDIR}/${LOGS}/${SCRIPT_LOGFILE}"
  427. }
  428. # Copy files from local tmpdir to remote host tmpdir
  429. copy_to_remote()
  430. {
  431. for F in $@; do
  432. scp "${LOCAL_TMPDIR}/${F}" root@"${REMOTE_HOST}:${REMOTE_TMPDIR}" 2>&1 >/dev/null
  433. done
  434. }
  435. # Copy files from remote host tmpdir to local tmpdir
  436. copy_from_remote()
  437. {
  438. for F in $@; do
  439. scp root@"${REMOTE_HOST}:${REMOTE_TMPDIR}/${F}" "${LOCAL_TMPDIR}/" 2>&1 >/dev/null
  440. done
  441. }
  442. # A wrapper for scmd calls to flashrom when we want to log the output
  443. # $1 and $2 are the arguments to be passed into scmd
  444. # $3 is the context of the flashrom call (to be used in the logfile)
  445. flashrom_log_scmd()
  446. {
  447. local logfile="flashrom-${3}.txt"
  448. tmpdir=$LOCAL_TMPDIR
  449. if [ $1 -eq $REMOTE ]; then
  450. tmpdir=$REMOTE_TMPDIR
  451. fi
  452. scmd $1 "$2 -o ${tmpdir}/${LOGS}/${logfile}"
  453. # if the call was successful, we don't want to save the log (only save failure logs)
  454. if [ $? -eq $EXIT_SUCCESS ]; then
  455. scmd $1 "rm -f ${tmpdir}/${LOGS}/${logfile}"
  456. else
  457. # if the log was stored remotely, we want to copy it over to local tmpdir
  458. if [ $1 -eq $REMOTE ]; then
  459. scp root@"${REMOTE_HOST}:${REMOTE_TMPDIR}/${LOGS}/${logfile}" "${LOCAL_TMPDIR}/${LOGS}" 2>&1 >/dev/null
  460. fi
  461. fi
  462. }
  463. # Read current image as backup in case one hasn't already been specified.
  464. if [ -z "$BACKUP_IMAGE" ]; then
  465. backup_file="backup.bin"
  466. if [ $DO_REMOTE -eq 1 ]; then
  467. BACKUP_IMAGE="${REMOTE_TMPDIR}/${backup_file}"
  468. else
  469. BACKUP_IMAGE="${LOCAL_TMPDIR}/${backup_file}"
  470. fi
  471. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -r $BACKUP_IMAGE" "read_backup"
  472. if [ $? -ne 0 ]; then
  473. print_and_log "Failed to read backup image, aborting.\n"
  474. exit $EXIT_FAILURE
  475. fi
  476. if [ $DO_REMOTE -eq 1 ]; then
  477. copy_from_remote "$backup_file"
  478. fi
  479. else
  480. if [ $DO_REMOTE -eq 1 ]; then
  481. scmd $DO_REMOTE "cp $BACKUP_IMAGE $REMOTE_TMPDIR"
  482. copy_from_remote "$(basename $BACKUP_IMAGE)"
  483. fi
  484. fi
  485. # The copy of flashrom to test. If unset, we'll assume the user wants to test
  486. # a newly built flashrom binary in the current directory.
  487. if [ -z "$NEW_FLASHROM" ] ; then
  488. if [ -x "flashrom" ]; then
  489. NEW_FLASHROM="flashrom"
  490. else
  491. print_and_log "Must supply new flashrom version to test\n"
  492. exit $EXIT_FAILURE
  493. fi
  494. fi
  495. print_and_log "Stable flashrom binary: ${OLD_FLASHROM}\n"
  496. print_and_log "New flashrom binary to test: ${NEW_FLASHROM}\n"
  497. print_and_log "Local temporary files will be stored in ${LOCAL_TMPDIR}\n"
  498. if [ $DO_REMOTE -eq 1 ]; then
  499. print_and_log "Remote temporary files will be stored in ${REMOTE_HOST}:${REMOTE_TMPDIR}\n"
  500. print_and_log "Backup image: ${REMOTE_HOST}:${BACKUP_IMAGE}\n"
  501. print_and_log "Backup image also stored at: ${LOCAL_TMPDIR}/$(basename ${BACKUP_IMAGE})\n"
  502. else
  503. print_and_log "Backup image: ${BACKUP_IMAGE}\n"
  504. fi
  505. #
  506. # Now the fun begins.
  507. #
  508. cmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS --get-size" "${LOCAL_TMPDIR}/chip_size.txt"
  509. tmp=$(cat ${LOCAL_TMPDIR}/chip_size.txt)
  510. cmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS --get-size" "${LOCAL_TMPDIR}/chip_size.txt"
  511. CHIP_SIZE=$(cat ${LOCAL_TMPDIR}/chip_size.txt)
  512. CHIP_SIZE_KB=$(($CHIP_SIZE / $K))
  513. CHIP_SIZE_HALF=$(($CHIP_SIZE / 2))
  514. if [ $CHIP_SIZE -ne $tmp ]; then
  515. print_and_log "New flashrom and old flashrom disagree on chip size. Aborting.\n"
  516. exit $EXIT_FAILURE
  517. else
  518. print_and_log "Chip size: $CHIP_SIZE_KB KiB\n"
  519. fi
  520. # Upload results
  521. #do_upload()
  522. #{
  523. # # TODO: implement this
  524. #}
  525. # Remove temporary files
  526. do_cleanup()
  527. {
  528. if [ $NO_CLEAN -eq 1 ]; then
  529. print_and_log "Skipping cleanup.\n"
  530. return $EXIT_SUCCESS
  531. fi
  532. rm -rf "$LOCAL_TMPDIR"
  533. if [ -n "$REMOTE_HOST" ]; then
  534. ssh root@${REMOTE_HOST} rm -rf "$REMOTE_TMPDIR"
  535. fi
  536. return $EXIT_SUCCESS
  537. }
  538. # $1: Message to display to user.
  539. test_fail()
  540. {
  541. print_and_log "$1\n"
  542. do_cleanup
  543. exit $EXIT_FAILURE
  544. }
  545. write_backup_image()
  546. {
  547. print_and_log "Writing backup image.\n"
  548. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -w $BACKUP_IMAGE" "write_backup"
  549. }
  550. save_wp_state()
  551. {
  552. print_and_log "Saving initial write protect state\n"
  553. if [ $DO_REMOTE -eq $REMOTE ]; then
  554. cmd $DO_REMOTE "crossystem wpsw_cur" "${LOCAL_TMPDIR}/hw_wp_state.txt"
  555. HW_WP_STATE=$(cat ${LOCAL_TMPDIR}/hw_wp_state.txt )
  556. fi
  557. cmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS --wp-status | grep -E -o 'enabled|disabled'" "${LOCAL_TMPDIR}/sw_wp_state.txt"
  558. sw_wp_str=$( cat ${LOCAL_TMPDIR}/sw_wp_state.txt )
  559. if [ "$sw_wp_str" = "enabled" ]; then
  560. SW_WP_STATE=1
  561. elif [ "$sw_wp_str" = "disabled" ]; then
  562. SW_WP_STATE=0
  563. fi
  564. cmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS --wp-status | grep -o -E '0x[0-9]{8}'" "${LOCAL_TMPDIR}/wp_range.txt"
  565. for count in 1 2; do
  566. read -r line
  567. dec_val=$(printf "%d\n" $line )
  568. if [ $count -eq 1 ]; then
  569. WP_RANGE_START=$dec_val
  570. elif [ $count -eq 2 ]; then
  571. WP_RANGE_LEN=$dec_val
  572. fi
  573. done < ${LOCAL_TMPDIR}/wp_range.txt
  574. }
  575. restore_wp_state()
  576. {
  577. print_and_log "Restoring initial write protect state\n"
  578. local restore_opts="--wp-range $WP_RANGE_START $WP_RANGE_LEN"
  579. if [ $SW_WP_STATE -eq 1 ]; then
  580. restore_opts="--wp-enable $restore_opts"
  581. elif [ $SW_WP_STATE -eq 0 ]; then
  582. restore_opts="--wp-disable $restore_opts"
  583. fi
  584. # TODO: move these dut-control commands into functions from an external
  585. # script, similar to the custom hooks
  586. wp_enable_hook
  587. wp_off_hook
  588. scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS $restore_opts"
  589. # if we are running on a remote machine, we stored the hw wp state and can restore now
  590. if [ $DO_REMOTE -eq $REMOTE ] && [ $HW_WP_STATE -eq 1 ]; then
  591. wp_on_hook
  592. fi
  593. wp_disable_hook
  594. }
  595. # $1 is 0/1 for disable/enable
  596. # $2 and $3 are range start and range length
  597. set_wp_state()
  598. {
  599. local wp_opts="--wp-range $2 $3"
  600. if [ $1 -eq $WP_DISABLE ]; then
  601. wp_opts="--wp-disable $wp_opts"
  602. elif [ $1 -eq $WP_ENABLE ]; then
  603. wp_opts="--wp-enable $wp_opts"
  604. else
  605. #invalid argument
  606. return $EXIT_FAILURE
  607. fi
  608. # TODO: move these dut-control commands into functions from an external
  609. # script, similar to the custom hooks
  610. wp_enable_hook
  611. wp_off_hook
  612. scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS $wp_opts"
  613. wp_on_hook
  614. wp_disable_hook
  615. }
  616. # this saves us some repeated code in the writeprotect test
  617. # $1 is the message to be passed into test_fail()
  618. wp_test_fail()
  619. {
  620. set_wp_state $WP_DISABLE 0 0 # completely disable wp so we can write the backup
  621. write_backup_image
  622. restore_wp_state # restore the fw and sw writeprotect states to the initial saved states.
  623. test_fail "$1"
  624. }
  625. # Read a region twice and compare results
  626. # $1: address of region (in bytes)
  627. # $2: length of region (in bytes)
  628. double_read_test()
  629. {
  630. local cmp1="${TMPDIR}/cmp1.bin"
  631. local cmp2="${TMPDIR}/cmp2.bin"
  632. local layout="double_read_test_layout.txt"
  633. local len=$(($2 / $K))
  634. print_and_log "Doing double read test, size: $len KiB\n"
  635. # FIXME: Figure out how to do printf remotely...
  636. printf "%06x:%06x region\n" $1 $(($1 + $2 - 1)) > "${LOCAL_TMPDIR}/${layout}"
  637. if [ $DO_REMOTE -eq 1 ]; then copy_to_remote "$layout" ; fi
  638. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -r -l ${TMPDIR}/${layout} --ignore-fmap -i region:${cmp1}" "double_read_1"
  639. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -r -l ${TMPDIR}/${layout} --ignore-fmap -i region:${cmp2}" "double_read_2"
  640. scmd $DO_REMOTE "cmp $cmp1 $cmp2"
  641. if [ $? -ne 0 ]; then
  642. test_fail "Double-read test failed, aborting."
  643. fi
  644. }
  645. PARTIAL_WRITE_TEST_REGION_SIZE=0
  646. PARTIAL_WRITE_TEST_ALIGN_SIZE_KB=0
  647. # helper function to reduce repetitiveness of partial_write_test
  648. partial_write_test_helper()
  649. {
  650. local test_name="$1"
  651. local offset_kb=$2
  652. local size_kb=$3
  653. local hex="$4"
  654. local oct=""
  655. oct="\\$(printf "%03o" $hex)"
  656. cp "${LOCAL_TMPDIR}/random_4k_test.bin" "${LOCAL_TMPDIR}/${test_name}.bin"
  657. dd if=/dev/zero bs=1k count=${size_kb} 2>/dev/null | tr "\000" "$oct" > "${LOCAL_TMPDIR}/${hex}_${size_kb}k.bin"
  658. while [ $(($(($offset_kb + $size_kb)) * $K)) -lt $PARTIAL_WRITE_TEST_REGION_SIZE ]; do
  659. dd if="${LOCAL_TMPDIR}/${hex}_${size_kb}k.bin" of="${LOCAL_TMPDIR}/${test_name}.bin" bs=1k count=${size_kb} seek=${offset_kb} conv=notrunc 2>/dev/null
  660. offset_kb=$(($offset_kb + $PARTIAL_WRITE_TEST_ALIGN_SIZE_KB))
  661. done
  662. }
  663. # Regional partial write test. Given a region name, this will write patterns
  664. # of bytes designed to test corner cases.
  665. #
  666. # We assume that eraseable block size can be either 4KB or 64KB and
  667. # must test for both. For simplicity, we'll assume the region size is
  668. # at least 256KB.
  669. #
  670. # $1: Region name
  671. partial_write_test()
  672. {
  673. local opts="--fast-verify"
  674. local region_name="$1"
  675. local filename=""
  676. local test_num=0
  677. local prev_test_num=0
  678. if [ $REGION_MODE -ne $REGION_MODE_FLASHMAP ]; then
  679. opts="$opts --ignore-fmap"
  680. fi
  681. if [ $TEST_TYPE -eq $TEST_TYPE_SINGLE ]; then
  682. if [ $REGION_MODE -eq $REGION_MODE_LAYOUT ]; then
  683. opts="$opts -l $LAYOUT_FILE"
  684. elif [ $REGION_MODE -eq $REGION_MODE_CLOBBER ]; then
  685. printf "000000:%06x RW\n" $(($CHIP_SIZE - 1)) > "${LOCAL_TMPDIR}/clobber_mode_layout.txt"
  686. if [ $DO_REMOTE -eq 1 ]; then
  687. copy_to_remote "clobber_mode_layout.txt"
  688. fi
  689. opts="$opts -l ${TMPDIR}/clobber_mode_layout.txt"
  690. fi
  691. fi
  692. if [ $SMALL_REGION -eq 1 ]; then
  693. PARTIAL_WRITE_TEST_ALIGN_SIZE_KB=16
  694. else
  695. PARTIAL_WRITE_TEST_ALIGN_SIZE_KB=256
  696. fi
  697. # FIXME: Add sanity checks.
  698. print_and_log "Doing region-based partial write test on region \"$region_name\"\n"
  699. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS $opts -r -i ${region_name}:${TMPDIR}/${region_name}.bin" "read_region_${region_name}"
  700. if [ $DO_REMOTE -eq 1 ]; then
  701. copy_from_remote "${region_name}.bin"
  702. fi
  703. PARTIAL_WRITE_TEST_REGION_SIZE=$(stat --format=%s ${LOCAL_TMPDIR}/${region_name}.bin)
  704. if [ $PARTIAL_WRITE_TEST_REGION_SIZE -lt $(($PARTIAL_WRITE_TEST_ALIGN_SIZE_KB * $K)) ]; then
  705. print_and_log "Region $region_name is too small\n"
  706. return $EXIT_FAILURE
  707. fi
  708. if [ $(($PARTIAL_WRITE_TEST_REGION_SIZE % $(($PARTIAL_WRITE_TEST_ALIGN_SIZE_KB)))) -ne 0 ]; then
  709. print_and_log "Region $region_name is not aligned to $PARTIAL_WRITE_TEST_ALIGN_SIZE_KB\n"
  710. return $EXIT_FAILURE
  711. fi
  712. # Test procedure:
  713. # Clobber region with random content first. Then do writes using the
  714. # following sequences for each 128KB:
  715. # 0-2K : 0x00 (\000) Partial 4KB sector, lower half
  716. # 2K-6K : 0x11 (\021) Crossover 4KB sector boundary
  717. # 6K-8K : 0x22 (\042) Partial 4KB sector, upper half
  718. # 8K-16K : 0x33 (\063) Full 4KB sectors
  719. #
  720. # Repeat the above sequence for 64KB-aligned sizes
  721. # 0-32K : 0x44 (\104) Partial 64KB block, lower half
  722. # 32K-96K : 0x55 (\125) Crossover 64KB block boundary
  723. # 96K-128K : 0x66 (\146) Partial 64KB block, upper half
  724. # 128K-256K : 0x77 (\167) Full 64KB blocks
  725. test_num=0
  726. dd if=/dev/urandom of="${LOCAL_TMPDIR}/random_4k_test.bin" bs=4k count=$(($PARTIAL_WRITE_TEST_REGION_SIZE / $((4 * $K)))) 2>/dev/null
  727. # 0-2K : 0x00 (\000) Partial 4KB sector, lower half
  728. partial_write_test_helper "4k_test_${test_num}" 0 2 "0x00"
  729. prev_test_num=$test_num
  730. test_num=$(($test_num + 1))
  731. # 2K-6K : 0x11 (\021) Crossover 4KB sector boundary
  732. partial_write_test_helper "4k_test_${test_num}" 2 4 "0x11"
  733. test_num=$(($test_num + 1))
  734. # 6K-8K : 0x22 (\042) Partial 4KB sector, upper half
  735. partial_write_test_helper "4k_test_${test_num}" 6 2 "0x22"
  736. test_num=$(($test_num + 1))
  737. # 8K-16K : 0x33 (\063) Full 4KB sectors
  738. partial_write_test_helper "4k_test_${test_num}" 8 8 "0x33"
  739. for F in ${LOCAL_TMPDIR}/random_4k_test.bin ${LOCAL_TMPDIR}/4k_test_*.bin ; do
  740. filename=$(basename $F)
  741. if [ $DO_REMOTE -eq 1 ]; then
  742. copy_to_remote $filename
  743. fi
  744. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS $opts -w -i ${region_name}:${TMPDIR}/${filename}" "write_${filename}"
  745. if [ $? -ne 0 ]; then
  746. test_fail "Failed to write $filename to $region_name"
  747. fi
  748. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS $opts -v -i ${region_name}:${TMPDIR}/${filename}" "verify_${filename}"
  749. if [ $? -ne 0 ]; then
  750. test_fail "Failed to verify write of $filename to $region_name"
  751. fi
  752. if [ -n "$SECONDARY_OPTS" ]; then
  753. flashrom_log_scmd $LOCAL "$OLD_FLASHROM $SECONDARY_OPTS $opts -v -i ${region_name}:${LOCAL_TMPDIR}/${filename}" "verify_secondary_${filename}"
  754. if [ $? -ne 0 ]; then
  755. test_fail "Failed to verify write of $filename to $region_name using secondary programmer"
  756. fi
  757. fi
  758. print_and_log "\tWrote $filename to $region_name region successfully.\n"
  759. done
  760. if [ $SMALL_REGION -eq 1 ]; then
  761. return $EXIT_SUCCESS
  762. fi
  763. #
  764. # Second half: Tests for 64KB chunks
  765. #
  766. test_num=0
  767. dd if=/dev/urandom of="${LOCAL_TMPDIR}/random_64k_test.bin" bs=128k count=$(($PARTIAL_WRITE_TEST_REGION_SIZE / $((128*$K)))) 2>/dev/null
  768. # 0-32K : 0x44 (\104) Partial 64KB block, lower half
  769. partial_write_test_helper "64k_test_${test_num}" 0 32 "0x44"
  770. prev_test_num=$test_num
  771. test_num=$(($test_num + 1))
  772. # 32K-96K : 0x55 (\125) Crossover 64KB block boundary
  773. partial_write_test_helper "64k_test_${test_num}" 32 64 "0x55"
  774. test_num=$(($test_num + 1))
  775. # 96K-128K : 0x66 (\146) Partial 64KB block, upper half
  776. partial_write_test_helper "64k_test_${test_num}" 96 32 "0x66"
  777. test_num=$(($test_num + 1))
  778. # 128K-256K : 0x77 (\167) Full 64KB blocks
  779. partial_write_test_helper "64k_test_${test_num}" 128 128 "0x77"
  780. for F in ${LOCAL_TMPDIR}/random_64k_test.bin ${LOCAL_TMPDIR}/64k_test_*.bin ; do
  781. filename=$(basename $F)
  782. if [ $DO_REMOTE -eq 1 ]; then
  783. copy_to_remote $filename
  784. fi
  785. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS $opts -w -i ${region_name}:${TMPDIR}/${filename}" "write_${filename}"
  786. if [ $? -ne 0 ]; then
  787. test_fail "Failed to write $filename to $region_name"
  788. fi
  789. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS $opts -v -i ${region_name}:${TMPDIR}/${filename}" "verify_${filename}"
  790. if [ $? -ne 0 ]; then
  791. test_fail "Failed to verify write of $filename to $region_name"
  792. fi
  793. if [ -n "$SECONDARY_OPTS" ]; then
  794. flashrom_log_scmd $LOCAL "$OLD_FLASHROM $SECONDARY_OPTS $opts -v -i ${region_name}:${LOCAL_TMPDIR}/${filename}" "verify_secondary_${filename}"
  795. if [ $? -ne 0 ]; then
  796. test_fail "Failed to verify write of $filename to $region_name using secondary programmer"
  797. fi
  798. fi
  799. print_and_log "\tWrote $filename to $region_name region successfully.\n"
  800. done
  801. return $EXIT_SUCCESS
  802. }
  803. # Before anything else, check to see if Flashrom can succesfully probe
  804. # for and find the flash chips. If not, we will abort.
  805. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS" "verify_probe"
  806. if [ $? -ne 0 ]; then
  807. test_fail "Failed to find flash chips while probing, aborting."
  808. fi
  809. # Read ROM twice to test for consistency.
  810. if [ $SKIP_CONSISTENCY_CHECK -eq 0 ]; then
  811. double_read_test 0 $CHIP_SIZE
  812. fi
  813. if [ $TEST_TYPE -eq $TEST_TYPE_SINGLE ]; then
  814. if [ $REGION_MODE -eq $REGION_MODE_CLOBBER ]; then
  815. random_file="${TMPDIR}/random_${CHIP_SIZE_KB}K.bin"
  816. cmp_file="${TMPDIR}/cmp.bin"
  817. scmd $DO_REMOTE "dd if=/dev/urandom of=${random_file} bs=1k count=${CHIP_SIZE_KB}"
  818. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -w $random_file" "clobber_write"
  819. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -r $cmp_file" "clobber_verify"
  820. scmd $DO_REMOTE "cmp $random_file $cmp_file"
  821. if [ $? -ne 0 ]; then
  822. write_backup_image
  823. test_fail "Failed to clobber entire ROM."
  824. fi
  825. scmd $DO_REMOTE "rm -f $cmp_file $random_file"
  826. partial_write_test "RW"
  827. if [ $? -ne 0 ]; then
  828. print_and_log "Layout mode test failed\n"
  829. RC=$EXIT_FAILURE
  830. fi
  831. elif [ $REGION_MODE -eq $REGION_MODE_DESCRIPTOR ]; then
  832. # FIXME: This depends on descriptor regions being translated into internal
  833. # layout representation automatically so we can target them using -i.
  834. print_and_log "TODO: Descriptor mode\n"
  835. exit $EXIT_FAILURE
  836. elif [ $REGION_MODE -eq $REGION_MODE_FLASHMAP ]; then
  837. partial_write_test "$FLASHMAP_REGION" 0
  838. if [ $? -ne 0 ]; then
  839. print_and_log "Flashmap mode test failed\n"
  840. RC=$EXIT_FAILURE
  841. fi
  842. elif [ $REGION_MODE -eq $REGION_MODE_LAYOUT ]; then
  843. rw_layout=""
  844. addr=""
  845. end=""
  846. size=""
  847. size_kb=""
  848. # Look for a region name with any amount of leading whitespace
  849. # and no trailing whitespace or characters.
  850. rw_layout=$(grep "\s${LAYOUT_REGION}$" "${LOCAL_TMPDIR}/$(basename $LAYOUT_FILE)" | head -n 1)
  851. if [ -z "$rw_layout" ]; then
  852. print_and_log "No region matching \"${LAYOUT_REGION}\" found layout file \"${LAYOUT_FILE}\"\n"
  853. test_fail ""
  854. fi
  855. addr="0x$(echo "$rw_layout" | cut -d ' ' -f -1 | awk -F ':' '{ print $1 }')"
  856. end="0x$(echo "$rw_layout" | cut -d ' ' -f -1 | awk -F ':' '{ print $2 }')"
  857. size="$(($end - $addr + 1))"
  858. size_kb="$(($size / $K))"
  859. print_and_log "\"$LAYOUT_REGION\" region address: ${addr}, size: $size_kb KiB\n"
  860. partial_write_test "$LAYOUT_REGION"
  861. if [ $? -ne 0 ]; then
  862. print_and_log "Layout mode test failed\n"
  863. RC=$EXIT_FAILURE
  864. fi
  865. fi
  866. elif [ $TEST_TYPE -eq $TEST_TYPE_ENDURANCE ]; then
  867. iteration=1
  868. terminate=0
  869. random_file="${TMPDIR}/random_${CHIP_SIZE_KB}K.bin"
  870. cmp_file="${TMPDIR}/cmp.bin"
  871. # TODO: We can measure how long the tests take on average, throughput, etc.
  872. # i.e. { time $NEW_FLASHROM $PRIMARY_OPTS -w $random_file ; } 2>&1 | grep user | cut -f2
  873. # For this test we want to run clobber mode until failure
  874. while [ $terminate -eq 0 ]
  875. do
  876. print_and_log "Running iteration #${iteration}\n"
  877. scmd $DO_REMOTE "dd if=/dev/urandom of=${random_file} bs=1k count=${CHIP_SIZE_KB}"
  878. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -w $random_file" "endurance_write_${iteration}"
  879. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -r $cmp_file" "endurance_verify_${iteration}"
  880. scmd $DO_REMOTE "cmp $random_file $cmp_file"
  881. if [ $? -ne 0 ]; then
  882. terminate=1
  883. fi
  884. scmd $DO_REMOTE "rm -f $cmp_file $random_file"
  885. iteration=$(($iteration + 1))
  886. done
  887. # TODO: Determine what to return for the endurance test exit status
  888. # i.e. what constitutes a test pass and what constitutes a test fail?
  889. print_and_log "Failed on iteration $iteration\n"
  890. # TODO - Print performance metrics?
  891. elif [ $TEST_TYPE -eq $TEST_TYPE_WRITEPROTECT ]; then
  892. random_file_protect="${TMPDIR}/random_${CHIP_SIZE_KB}K_protect.bin"
  893. random_file_clobber1="${TMPDIR}/random_${CHIP_SIZE_KB}K_clobber1.bin"
  894. random_file_clobber2="${TMPDIR}/random_${CHIP_SIZE_KB}K_clobber2.bin"
  895. cmp_file="${TMPDIR}/cmp.bin"
  896. # generate random files
  897. scmd $DO_REMOTE "dd if=/dev/urandom of=$random_file_protect bs=1k count=${CHIP_SIZE_KB}"
  898. scmd $DO_REMOTE "dd if=/dev/urandom of=$random_file_clobber1 bs=1k count=${CHIP_SIZE_KB}"
  899. scmd $DO_REMOTE "dd if=/dev/urandom of=$random_file_clobber2 bs=1k count=${CHIP_SIZE_KB}"
  900. save_wp_state
  901. set_wp_state $WP_DISABLE 0 0
  902. # clobber ROM with an initial file
  903. print_and_log "Clobbering ROM with random initial content\n"
  904. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -w $random_file_protect" "writeprotect_initial_write"
  905. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -r $cmp_file" "writeprotect_initial_verify"
  906. scmd $DO_REMOTE "cmp $random_file_protect $cmp_file"
  907. if [ $? -ne 0 ]; then
  908. wp_test_fail "Failed to clobber ROM initially."
  909. fi
  910. scmd $DO_REMOTE "rm -f $cmp_file"
  911. # Protect lower half of ROM, then try clobbering.
  912. print_and_log "Starting lower half test\n"
  913. set_wp_state $WP_ENABLE 0 $CHIP_SIZE_HALF
  914. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -n -w $random_file_clobber1" "writeprotect_enable_lower_write"
  915. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -r $cmp_file" "writeprotect_enable_lower_verify"
  916. scmd $DO_REMOTE "cmp --bytes=$CHIP_SIZE_HALF $random_file_protect $cmp_file"
  917. if [ $? -ne 0 ]; then
  918. wp_test_fail "Failed to protect lower half of ROM - exiting writeprotect test."
  919. fi
  920. scmd $DO_REMOTE "cmp --ignore-initial=$CHIP_SIZE_HALF --bytes=$CHIP_SIZE_HALF $random_file_clobber1 $cmp_file"
  921. if [ $? -ne 0 ]; then
  922. wp_test_fail "Failed to clobber upper half of ROM - exiting writeprotect test."
  923. fi
  924. scmd $DO_REMOTE "rm -f $cmp_file"
  925. # Protect upper half of ROM, then try clobbering.
  926. print_and_log "Starting upper half test\n"
  927. set_wp_state $WP_ENABLE $CHIP_SIZE_HALF $CHIP_SIZE_HALF
  928. flashrom_log_scmd $DO_REMOTE "$NEW_FLASHROM $PRIMARY_OPTS -n -w $random_file_clobber2" "writeprotect_enable_upper_write"
  929. flashrom_log_scmd $DO_REMOTE "$OLD_FLASHROM $PRIMARY_OPTS -r $cmp_file" "writeprotect_enable_upper_verify"
  930. scmd $DO_REMOTE "cmp --bytes=$CHIP_SIZE_HALF $random_file_clobber2 $cmp_file"
  931. if [ $? -ne 0 ]; then
  932. wp_test_fail "Failed to clobber lower half of ROM - exiting writeprotect test."
  933. fi
  934. scmd $DO_REMOTE "cmp --ignore-initial=$CHIP_SIZE_HALF --bytes=$CHIP_SIZE_HALF $random_file_clobber1 $cmp_file"
  935. if [ $? -ne 0 ]; then
  936. wp_test_fail "Failed to protect upper half of ROM - exiting writeprotect test."
  937. fi
  938. scmd $DO_REMOTE "rm -f $cmp_file"
  939. set_wp_state $WP_DISABLE 0 0 # completely disable wp so we can write the backup
  940. fi
  941. # restore and cleanup
  942. write_backup_image
  943. if [ $TEST_TYPE -eq $TEST_TYPE_WRITEPROTECT ]; then
  944. restore_wp_state # restore the fw and sw writeprotect states to the initial saved states.
  945. fi
  946. if [ $RC -eq 0 ]; then
  947. print_and_log "Test status: PASS\n"
  948. else
  949. print_and_log "Test status: FAIL\n"
  950. fi
  951. do_cleanup
  952. exit $RC