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.

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