wp-toggle.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 enable/disable bits (that is, SPI chip is not hardware
  20. # write-protected). Also, assume output is in the form:
  21. # WP: write protect is enabled
  22. # WP: write protect is disabled
  23. #
  24. # FIXME: Carl-Daniel pointed out that this script does little to validate that
  25. # the settings on the chip are actually correct. It merely checks that Flashrom
  26. # prints what we want it to print.
  27. . "$(pwd)/common.sh"
  28. logfile="${0}.log"
  29. wp_disabled=0
  30. wp_enabled=1
  31. magic_string="write protect is"
  32. wp_toggle_fail()
  33. {
  34. echo "$1" >> ${logfile}
  35. echo "$0: failed" >> ${logfile}
  36. exit ${EXIT_FAILURE}
  37. }
  38. wp_status()
  39. {
  40. if [ "$(printf %s\\n "$1" | sed 's/.*enabled.*/enabled/')" = "enabled" ]; then
  41. return ${wp_enabled}
  42. elif [ "$(printf %s\\n "$1" | sed 's/.*disabled.*/disabled/')" = "disabled" ]; then
  43. return ${wp_disabled}
  44. else
  45. wp_toggle_fail "unknown write protect status: \"$1\""
  46. fi
  47. }
  48. # Back-up old settings
  49. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null | grep "$magic_string")
  50. wp_status "$tmp"
  51. old_status=$?
  52. echo "old write protect status: ${old_status}" >> ${logfile}
  53. # invert the old setting
  54. if [ ${old_status} -eq ${wp_enabled} ]; then
  55. do_test_flashrom --wp-disable
  56. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null | grep "$magic_string")
  57. wp_status "$tmp"
  58. if [ $? -eq ${wp_enabled} ]; then
  59. wp_toggle_fail "failed to disable write protection"
  60. fi
  61. elif [ ${old_status} -eq ${wp_disabled} ]; then
  62. do_test_flashrom --wp-enable
  63. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null | grep "$magic_string")
  64. wp_status "$tmp"
  65. if [ $? -eq ${wp_disabled} ]; then
  66. wp_toggle_fail "failed to enable write protection"
  67. fi
  68. fi
  69. # restore old setting
  70. if [ ${old_status} -eq ${wp_enabled} ]; then
  71. do_test_flashrom --wp-enable
  72. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null | grep "$magic_string")
  73. wp_status "$tmp"
  74. if [ $? -ne ${wp_enabled} ]; then
  75. wp_toggle_fail "failed to enable write protection"
  76. fi
  77. elif [ ${old_status} -eq ${wp_disabled} ]; then
  78. do_test_flashrom --wp-disable
  79. tmp=$(./flashrom ${FLASHROM_PARAM} --wp-status 2>/dev/null | grep "$magic_string")
  80. wp_status "$tmp"
  81. if [ $? -ne ${wp_disabled} ]; then
  82. wp_toggle_fail "failed to disable write protection"
  83. fi
  84. fi
  85. echo "$0: passed" >> ${logfile}
  86. return ${EXIT_SUCCESS}