wp-range.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. # Assume we can set a write protect range for 0-64KB
  20. # This assumes the output from wp-status is:
  21. # "some text ... start=0x00000000, len=0x00010000"
  22. # where the start and length are key=value pairs
  23. # FIXME: Should we intentionally disable write protection for this? The
  24. # status register will not be changeable if hardware WP is in effect.
  25. # Then again, if hardware WP is on, then we won't be able to disable WP
  26. # either...
  27. #
  28. # FIXME #2: As per Carl-Daniel's comment, this test does not adequately
  29. # check that encoding is correct. It only checks if the output matches
  30. # the requested range.
  31. #
  32. . "$(pwd)/common.sh"
  33. logfile="${0}.log"
  34. # Try to write protect the uppermost block
  35. new_wp_range_start=$((($(./flashrom ${FLASHROM_PARAM} --get-size 2>/dev/null) - 0x010000)))
  36. new_wp_range_len=0x010000
  37. # Back-up old settings
  38. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null)
  39. old_start=${tmp##*start=}
  40. old_start=`printf "%s" "${old_start%%,*}"`
  41. old_len=${tmp##*len=}
  42. old_len=`printf "%s" "${old_len%%,*}"`
  43. echo "old start: ${old_start}, old length: ${old_len}" >> ${logfile}
  44. # If the old write protection settings are the same as the new ones, we need
  45. # to choose new values. If this is the case, we'll drop the lower bound of the
  46. # range by 1 block and extend the range 64K block.
  47. if [ $((${old_start} == ${new_wp_range_start})) -ne 1 ] && [ $((${old_len} == ${new_wp_range_len})) -ne 1 ] ; then
  48. new_wp_range_start=$((${new_wp_range_start} - 0x10000))
  49. new_wp_range_len=$((${new_wp_range_len} + 0x10000))
  50. fi
  51. # Try to set new range values
  52. echo "attempting to set write protect range: start=${new_wp_range_start} ${new_wp_range_len}" >> ${logfile}
  53. do_test_flashrom --wp-range ${new_wp_range_start} ${new_wp_range_len}
  54. if [ ${?} -ne ${EXIT_SUCCESS} ]; then
  55. echo -n "failed to set write protect range" >> ${logfile}
  56. return ${EXIT_FAILURE}
  57. fi
  58. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null)
  59. new_start=${tmp##*start=}
  60. new_start=`printf "%s" "${new_start%%,*}"`
  61. new_len=${tmp##*len=}
  62. new_len=`printf "%s" "${new_len%%,*}"`
  63. if [ $((${new_start} == ${new_wp_range_start})) -ne 1 ]; then return ${EXIT_FAILURE} ; fi
  64. if [ $((${new_len} == ${new_wp_range_len})) -ne 1 ]; then return ${EXIT_FAILURE} ; fi
  65. # restore the old settings
  66. do_test_flashrom --wp-range ${old_start} ${old_len}
  67. if [ ${?} -ne ${EXIT_SUCCESS} ]; then
  68. echo "failed to restore old settings" >> ${logfile}
  69. return ${EXIT_FAILURE}
  70. fi
  71. echo "$0: passed" >> ${logfile}
  72. return ${EXIT_SUCCESS}