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.

193 lines
4.4 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. autodetect_chip=0
  15. usage()
  16. {
  17. echo "Usage: $0 [-c <chipname>] [-a] [-m] [-k <backup_filename>] [-l]"
  18. echo ""
  19. echo "-c <chipname> flashrom chip description to use"
  20. echo "-a try to autodetect the chipname"
  21. echo "-m apply me_cleaner -S"
  22. echo "-l lock the flash instead of unlocking it"
  23. echo "-k <file> save the read image as <file>"
  24. }
  25. args=$(getopt -o mlc:ak:h -- "$@")
  26. if [ $? -ne 0 ] ; then
  27. usage
  28. exit 1
  29. fi
  30. eval set -- "$args"
  31. while [ $# -gt 0 ]
  32. do
  33. case "$1" in
  34. -m)
  35. me_clean=1
  36. ;;
  37. -l)
  38. lock=1
  39. ;;
  40. -a)
  41. autodetect_chip=1
  42. ;;
  43. -c)
  44. CHIPNAME=$2
  45. have_chipname=1
  46. shift
  47. ;;
  48. -k)
  49. BACKUPNAME=$2
  50. have_backupname=1
  51. shift
  52. ;;
  53. -h)
  54. usage
  55. exit 1
  56. ;;
  57. --)
  58. shift
  59. break
  60. ;;
  61. *)
  62. echo "Invalid option: $1"
  63. exit 1
  64. ;;
  65. esac
  66. shift
  67. done
  68. TEMP_DIR=`mktemp -d`
  69. if [ ! "$have_chipname" -gt 0 ] ; then
  70. if [ ! "$autodetect_chip" -gt 0 ] ; then
  71. echo -e "${RED}no chipname provided${NC}. To get it, we run:"
  72. echo "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128"
  73. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128
  74. usage
  75. rm -rf ${TEMP_DIR}
  76. exit 1
  77. else
  78. echo "trying to detect the chip..."
  79. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 &> ${TEMP_DIR}/chips || true
  80. flashrom_error=""
  81. flashrom_error=$(cat ${TEMP_DIR}/chips | grep -i error)
  82. if [ ! -z "${flashrom_error}" ] ; then
  83. cat ${TEMP_DIR}/chips
  84. rm -rf ${TEMP_DIR}
  85. exit 1
  86. fi
  87. CHIPNAME=""
  88. chip_found=0
  89. CHIPNAME=$(cat ${TEMP_DIR}/chips | grep Found | grep "MX25L6406E/MX25L6408E" | grep -o '".*"')
  90. if [ ! -z "${CHIPNAME}" ] ; then
  91. chip_found=1
  92. fi
  93. CHIPNAME=$(cat ${TEMP_DIR}/chips | grep Found | grep "EN25QH64" | grep -o '".*"')
  94. if [ ! -z "${CHIPNAME}" ] ; then
  95. chip_found=1
  96. fi
  97. if [ ! "$chip_found" -gt 0 ] ; then
  98. echo -e "${RED}Error:${NC} chip not detected. Please find it manually:"
  99. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128
  100. rm -rf ${TEMP_DIR}
  101. exit 1
  102. else
  103. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  104. fi
  105. fi
  106. fi
  107. make -C util/ifdtool
  108. if [ ! -e ${IFDTOOL_PATH} ] ; then
  109. echo "ifdtool not found at ${IFDTOOL_PATH}"
  110. exit 1
  111. fi
  112. if [ ! "$me_clean" -gt 0 ] ; then
  113. echo -e "Intel ME will ${RED}not${NC} be cleaned. Use -m if it should be."
  114. else
  115. echo -e "Intel ME will be ${GREEN}cleaned${NC}."
  116. fi
  117. if [ ! "$lock" -gt 0 ] ; then
  118. echo -e "The flash ROM will be ${GREEN}unlocked${NC}."
  119. else
  120. echo -e "The flash ROM will be ${RED}locked${NC}."
  121. fi
  122. if [ "$me_clean" -gt 0 ] ; then
  123. if [ ! -e ${ME_CLEANER_PATH} ] ; then
  124. echo "me_cleaner not found at ${ME_CLEANER_PATH}"
  125. rm -rf ${TEMP_DIR}
  126. exit 1
  127. fi
  128. fi
  129. echo "Start reading 2 times. Please be patient..."
  130. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  131. echo -e "${RED}Error:${NC} Could not create temp dir"
  132. rm -rf ${TEMP_DIR}
  133. exit 1
  134. fi
  135. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  136. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  137. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  138. if [ "$have_backupname" -gt 0 ] ; then
  139. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  140. echo "current image saved as ${BACKUPNAME}"
  141. fi
  142. reference_size=8388608
  143. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  144. if [ ! "$reference_size" -eq "$TEMP_SIZE" ] ; then
  145. echo -e "${RED}Error:${NC} didn't read 8M. You might be at the wrong chip."
  146. rm -rf ${TEMP_DIR}
  147. exit 1
  148. fi
  149. echo -e "${GREEN}connection ok${NC}"
  150. echo "start unlocking ..."
  151. if [ "$me_clean" -gt 0 ] ; then
  152. ${ME_CLEANER_PATH} -S -O ${TEMP_DIR}/work.rom ${TEMP_DIR}/test1.rom
  153. else
  154. cp ${TEMP_DIR}/test1.rom ${TEMP_DIR}/work.rom
  155. fi
  156. if [ ! "$lock" -gt 0 ] ; then
  157. ${IFDTOOL_PATH} -u ${TEMP_DIR}/work.rom
  158. else
  159. ${IFDTOOL_PATH} -l ${TEMP_DIR}/work.rom
  160. fi
  161. if [ ! -e ${TEMP_DIR}/work.rom.new ] ; then
  162. echo -e "${RED}Error:${NC} ifdtool failed. ${TEMP_DIR}/work.rom.new not found."
  163. rm -rf ${TEMP_DIR}
  164. exit 1
  165. fi
  166. if [ "$me_clean" -gt 0 ] ; then
  167. echo -e "${GREEN}ifdtool and me_cleaner ok${NC}"
  168. else
  169. echo -e "${GREEN}ifdtool ok${NC}"
  170. fi
  171. make clean -C util/ifdtool
  172. echo "start writing..."
  173. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${TEMP_DIR}/work.rom.new
  174. rm -rf ${TEMP_DIR}
  175. echo -e "${GREEN}DONE${NC}"