flash 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/bash
  2. # flash script: uses flashrom to flash a libreboot ROM image
  3. #
  4. # Copyright (C) 2014, 2015 Francis Rowe <info@gluglug.org.uk>
  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 3 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, see <http://www.gnu.org/licenses/>.
  18. #
  19. # So that I one day find it again
  20. # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
  21. ## Don't add here. errors are expected.
  22. [ "x${DEBUG+set}" = 'xset' ] && set -v
  23. # set -u -e
  24. if [[ ${EUID} -ne 0 ]]; then
  25. printf "This script must be run as root\n"
  26. exit 1
  27. fi
  28. arch="unknown"
  29. if [ $(uname -i) = "i686" ] || [ $(uname -m) = "i686" ]; then
  30. arch="i686"
  31. elif [ $(uname -i) = "x86_64" ] || [ $(uname -m) = "x86_64" ]; then
  32. arch="x86_64"
  33. else
  34. printf "This script must be run on an i686 or x86_64 host. x86_64 is recommended.\n"
  35. exit 1
  36. fi
  37. usage="usage: ./flash mode path/to/yourrom.rom"
  38. availablemodes="update, forceupdate, i945lenovo_firstflash, i945lenovo_secondflash, i945apple_firstflash"
  39. mode="unknown"
  40. rompath="unknown"
  41. # User specified no or too few/many parameters
  42. if [ $# -lt 2 ]; then
  43. printf "%s\n" "${usage}"
  44. printf "You need to specify exactly one mode, and one file\n"
  45. printf "%s\n" "${availablemodes}"
  46. exit 1
  47. fi
  48. mode="${1}"
  49. rompath="${2}"
  50. # User specified an invalid mode of operation
  51. if [ "${mode}" != "update" ] && [ "${mode}" != "forceupdate" ] && [ "${mode}" != "i945lenovo_firstflash" ] && [ "${mode}" != "i945lenovo_secondflash" ] && [ "${mode}" != "i945apple_firstflash" ]; then
  52. printf "%s\n" "${usage}"
  53. printf "Invalid mode. Modes available: %s\n" "${availablemodes}"
  54. exit 1
  55. else
  56. printf "Mode selected: %s\n" "${mode}"
  57. fi
  58. # The specified file does not exist
  59. if [ ! -f "${rompath}" ]; then
  60. printf "File not found!\n"
  61. exit 1
  62. fi
  63. flashrom="unknown"
  64. if [ -f "build" ]; then
  65. # git or libreboot_src
  66. flashrom="./flashrom/flashrom"
  67. else
  68. # libreboot_util
  69. flashrom="./flashrom/${arch}/flashrom"
  70. fi
  71. if [ ! -f "${flashrom}" ]; then
  72. printf "flashrom binary not present\n"
  73. exit 1
  74. fi
  75. # i945 lenovobios
  76. bucts="unknown"
  77. flashrom_lenovobios_sst="unknown"
  78. flashrom_lenovobios_macronix="unknown"
  79. if [ "${mode}" = "i945lenovo_firstflash" ] || [ "${mode}" = "i945lenovo_secondflash" ]; then
  80. if [ -f "build" ]; then
  81. # git or libreboot_src
  82. bucts="./bucts/bucts"
  83. flashrom_lenovobios_sst="./flashrom/flashrom_lenovobios_sst"
  84. flashrom_lenovobios_macronix="./flashrom/flashrom_lenovobios_macronix"
  85. else
  86. # libreboot_util
  87. bucts="./bucts/${arch}/bucts"
  88. flashrom_lenovobios_sst="./flashrom/${arch}/flashrom_lenovobios_sst"
  89. flashrom_lenovobios_macronix="./flashrom/${arch}/flashrom_lenovobios_macronix"
  90. fi
  91. # anti-bricking precaution
  92. if [ ! -f "${bucts}" ]; then
  93. printf "bucts binary not present. ABORTING so as to protect against bricking the machine.\n"
  94. exit 1
  95. fi
  96. # fail if flashrom is not present
  97. if [ ! -f "${flashrom_lenovobios_sst}" ] || [ ! -f "${flashrom_lenovobios_macronix}" ]; then
  98. printf "Flashrom binaries not present.\n"
  99. exit 1
  100. fi
  101. fi
  102. if [ "${mode}" = "update" ]; then
  103. ${flashrom} -p internal -w "${rompath}"
  104. elif [ "${mode}" = "forceupdate" ]; then
  105. ${flashrom} -p internal:boardmismatch=force -w "${rompath}"
  106. elif [ "${mode}" = "i945apple_firstflash" ]; then
  107. ${flashrom} -p internal:laptop=force_I_want_a_brick -w "${rompath}"
  108. elif [ "${mode}" = "i945lenovo_firstflash" ]; then
  109. ${bucts} 1 # needed to prevent bricks.
  110. # One will fail (this is harmless), and the other will succeed.
  111. ${flashrom_lenovobios_sst} -p internal -w "${rompath}"
  112. ${flashrom_lenovobios_macronix} -p internal -w "${rompath}"
  113. elif [ "${mode}" = "i945lenovo_secondflash" ]; then
  114. ${flashrom} -p internal -w "${rompath}"
  115. ${bucts} 0
  116. fi