You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.1 KiB

  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-3.0+
  3. # Copyright (C) 2018, Martin Kepplinger <martink@posteo.de>
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. NC='\033[0m'
  7. set -e
  8. IFDTOOL_PATH=./util/ifdtool/ifdtool
  9. ME_CLEANER_PATH=./util/me_cleaner/me_cleaner.py
  10. have_chipname=0
  11. have_backupname=0
  12. me_clean=0
  13. lock=0
  14. usage()
  15. {
  16. echo "Usage: $0 -c <chipname> [-m] [-k <backup_filename>] [-l]"
  17. echo ""
  18. echo "-m apply me_cleaner -S"
  19. echo "-l lock the flash instead of unlocking it"
  20. }
  21. args=$(getopt -o mlc:k:h -- "$@")
  22. if [ $? -ne 0 ] ; then
  23. usage
  24. exit 1
  25. fi
  26. eval set -- "$args"
  27. while [ $# -gt 0 ]
  28. do
  29. case "$1" in
  30. -m)
  31. me_clean=1
  32. ;;
  33. -l)
  34. lock=1
  35. ;;
  36. -c)
  37. CHIPNAME=$2
  38. have_chipname=1
  39. shift
  40. ;;
  41. -k)
  42. BACKUPNAME=$2
  43. have_backupname=1
  44. shift
  45. ;;
  46. -h)
  47. usage
  48. exit 1
  49. ;;
  50. --)
  51. shift
  52. break
  53. ;;
  54. *)
  55. echo "Invalid option: $1"
  56. exit 1
  57. ;;
  58. esac
  59. shift
  60. done
  61. if [ ! "$have_chipname" -gt 0 ] ; then
  62. echo "no chipname provided. to find it out, run:"
  63. echo "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128"
  64. usage
  65. exit 1
  66. fi
  67. make -C util/ifdtool
  68. if [ ! -e ${IFDTOOL_PATH} ] ; then
  69. echo "ifdtool not found at ${IFDTOOL_PATH}"
  70. exit 1
  71. fi
  72. if [ ! "$me_clean" -gt 0 ] ; then
  73. echo "Intel ME will NOT be cleaned. Use -m if it should be."
  74. else
  75. echo "Intel ME will be cleaned."
  76. fi
  77. if [ ! "$lock" -gt 0 ] ; then
  78. echo -e "The flash ROM will be ${GREEN}unlocked${NC}."
  79. else
  80. echo "The flash ROM will be LOCKED!"
  81. fi
  82. if [ "$me_clean" -gt 0 ] ; then
  83. if [ ! -e ${ME_CLEANER_PATH} ] ; then
  84. echo "me_cleaner not found at ${ME_CLEANER_PATH}"
  85. exit 1
  86. fi
  87. fi
  88. echo "Start reading 2 times. Please be patient."
  89. TEMP_DIR=`mktemp -d`
  90. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  91. echo -e "${RED}Error:${NC} Could not create temp dir"
  92. exit 1
  93. fi
  94. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  95. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  96. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  97. if [ "$have_backupname" -gt 0 ] ; then
  98. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  99. echo "current image saved as ${BACKUPNAME}"
  100. fi
  101. reference_size=8388608
  102. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  103. if [ ! "$reference_size" -eq "$TEMP_SIZE" ] ; then
  104. echo -e "${RED}Error:${NC} didn't read 8M. You might be at the wrong chip."
  105. rm -rf ${TEMP_DIR}
  106. exit 1
  107. fi
  108. echo -e "${GREEN}connection ok${NC}"
  109. echo "start unlocking ..."
  110. if [ "$me_clean" -gt 0 ] ; then
  111. ${ME_CLEANER_PATH} -S -O ${TEMP_DIR}/work.rom ${TEMP_DIR}/test1.rom
  112. else
  113. cp ${TEMP_DIR}/test1.rom ${TEMP_DIR}/work.rom
  114. fi
  115. if [ ! "$lock" -gt 0 ] ; then
  116. ${IFDTOOL_PATH} -u ${TEMP_DIR}/work.rom
  117. else
  118. ${IFDTOOL_PATH} -l ${TEMP_DIR}/work.rom
  119. fi
  120. if [ ! -e ${TEMP_DIR}/work.rom.new ] ; then
  121. echo -e "${RED}Error:${NC} ifdtool failed. ${TEMP_DIR}/work.rom.new not found."
  122. rm -rf ${TEMP_DIR}
  123. exit 1
  124. fi
  125. if [ "$me_clean" -gt 0 ] ; then
  126. echo -e "${GREEN}ifdtool and me_cleaner ok${NC}"
  127. else
  128. echo -e "${GREEN}ifdtool ok${NC}"
  129. fi
  130. make clean -C util/ifdtool
  131. echo "start writing..."
  132. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${TEMP_DIR}/work.rom.new
  133. echo -e "${GREEN}DONE${NC}"