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.

255 lines
5.9 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. have_flasher=0
  15. rpi_frequency=0
  16. usage()
  17. {
  18. echo "Skulls for the X230"
  19. echo " Run this script on an external computer with a flasher"
  20. echo " connected to the X230's bottom chip (farther away from"
  21. echo " the display, closer to you)."
  22. echo ""
  23. echo "Usage: $0 [-c <chipname>] [-a] [-m] [-k <backup_filename>] [-l]"
  24. echo ""
  25. echo " -f <hardware_flasher> supported flashers: rpi, ch341a"
  26. echo " -c <chipname> flashrom chip description to use"
  27. echo " -m apply me_cleaner -S -d"
  28. echo " -l lock the flash instead of unlocking it"
  29. echo " -k <backup> save the current image as"
  30. echo " -b <spi frequency> frequency of the RPi SPI bus in Hz. default: 128"
  31. }
  32. args=$(getopt -o f:mlc:k:hb: -- "$@")
  33. if [ $? -ne 0 ] ; then
  34. usage
  35. exit 1
  36. fi
  37. eval set -- "$args"
  38. while [ $# -gt 0 ]
  39. do
  40. case "$1" in
  41. -f)
  42. FLASHER=$2
  43. have_flasher=1
  44. shift
  45. ;;
  46. -m)
  47. me_clean=1
  48. ;;
  49. -l)
  50. lock=1
  51. ;;
  52. -c)
  53. CHIPNAME=$2
  54. have_chipname=1
  55. shift
  56. ;;
  57. -k)
  58. BACKUPNAME=$2
  59. have_backupname=1
  60. shift
  61. ;;
  62. -b)
  63. rpi_frequency=$2
  64. shift
  65. ;;
  66. -h)
  67. usage
  68. exit 1
  69. ;;
  70. --)
  71. shift
  72. break
  73. ;;
  74. *)
  75. echo "Invalid option: $1"
  76. usage
  77. exit 1
  78. ;;
  79. esac
  80. shift
  81. done
  82. command -v flashrom >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install flashrom and run as root${NC}."; exit 1; }
  83. command -v make >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install make and a C compiler${NC}."; exit 1; }
  84. command -v mktemp >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install mktemp (coreutils)${NC}."; exit 1; }
  85. if [ ! "$have_flasher" -gt 0 ] ; then
  86. echo "Skulls for the X230"
  87. echo ""
  88. echo "Please select the hardware you use:"
  89. PS3='Please select the hardware flasher: '
  90. options=("Raspberry Pi" "CH341A" "Exit")
  91. select opt in "${options[@]}"
  92. do
  93. case $opt in
  94. "Raspberry Pi")
  95. FLASHER="rpi"
  96. break
  97. ;;
  98. "CH341A")
  99. FLASHER="ch341a"
  100. break
  101. ;;
  102. "Exit")
  103. exit 0
  104. ;;
  105. *) echo invalid option;;
  106. esac
  107. done
  108. fi
  109. if [ ! "${rpi_frequency}" -gt 0 ] ; then
  110. rpi_frequency=512
  111. fi
  112. programmer=""
  113. if [ "${FLASHER}" = "rpi" ] ; then
  114. echo "Ok. Run this on a Rasperry Pi."
  115. programmer="linux_spi:dev=/dev/spidev0.0,spispeed=${rpi_frequency}"
  116. elif [ "${FLASHER}" = "ch341a" ] ; then
  117. echo "Ok. Connect a CH341A programmer"
  118. programmer="ch341a_spi"
  119. else
  120. echo "invalid flashrom programmer"
  121. usage
  122. exit 1
  123. fi
  124. TEMP_DIR=$(mktemp -d)
  125. if [ ! -d "$TEMP_DIR" ]; then
  126. echo -e "${RED}Error:${NC} Could not create temp dir"
  127. rm -rf "${TEMP_DIR}"
  128. exit 1
  129. fi
  130. if [ ! "$have_chipname" -gt 0 ] ; then
  131. echo "trying to detect the chip..."
  132. flashrom -p ${programmer} &> "${TEMP_DIR}"/chips || true
  133. flashrom_error=""
  134. flashrom_error=$(cat "${TEMP_DIR}"/chips | grep -i error || true)
  135. if [ ! -z "${flashrom_error}" ] ; then
  136. usage
  137. echo "-------------- flashrom error: ---------------"
  138. cat "${TEMP_DIR}"/chips
  139. rm -rf "${TEMP_DIR}"
  140. exit 1
  141. fi
  142. CHIPNAME=""
  143. chip_found=0
  144. if [ ! "$chip_found" -gt 0 ] ; then
  145. CHIPNAME=$(cat "${TEMP_DIR}"/chips | grep Found | grep "MX25L6406E/MX25L6408E" | grep -oP '"\K[^"\047]+(?=["\047])' || true)
  146. if [ ! -z "${CHIPNAME}" ] ; then
  147. chip_found=1
  148. fi
  149. fi
  150. if [ ! "$chip_found" -gt 0 ] ; then
  151. CHIPNAME=$(cat "${TEMP_DIR}"/chips | grep Found | grep "EN25QH64" | grep -o '".*"' | grep -oP '"\K[^"\047]+(?=["\047])' || true)
  152. if [ ! -z "${CHIPNAME}" ] ; then
  153. chip_found=1
  154. fi
  155. fi
  156. if [ ! "$chip_found" -gt 0 ] ; then
  157. echo "chip not detected."
  158. flashrom -p ${programmer} || true
  159. rm -rf "${TEMP_DIR}"
  160. echo "chip not detected. Please find it manually and rerun with the -c parameter."
  161. exit 1
  162. else
  163. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  164. fi
  165. fi
  166. make -C util/ifdtool
  167. if [ ! -e ${IFDTOOL_PATH} ] ; then
  168. echo "ifdtool not found at ${IFDTOOL_PATH}"
  169. exit 1
  170. fi
  171. if [ ! "$me_clean" -gt 0 ] ; then
  172. echo -e "Intel ME will ${RED}not${NC} be cleaned. Use -m if it should be."
  173. else
  174. echo -e "Intel ME will be ${GREEN}cleaned${NC}."
  175. fi
  176. if [ ! "$lock" -gt 0 ] ; then
  177. echo -e "The flash ROM will be ${GREEN}unlocked${NC}."
  178. else
  179. echo -e "The flash ROM will be ${RED}locked${NC}."
  180. fi
  181. if [ "$me_clean" -gt 0 ] ; then
  182. if [ ! -e ${ME_CLEANER_PATH} ] ; then
  183. echo "me_cleaner not found at ${ME_CLEANER_PATH}"
  184. rm -rf "${TEMP_DIR}"
  185. exit 1
  186. fi
  187. fi
  188. echo "Start reading 2 times. Please be patient..."
  189. flashrom -p ${programmer} -c ${CHIPNAME} -r "${TEMP_DIR}"/test1.rom
  190. flashrom -p ${programmer} -c ${CHIPNAME} -r "${TEMP_DIR}"/test2.rom
  191. cmp --silent "${TEMP_DIR}"/test1.rom "${TEMP_DIR}"/test2.rom
  192. if [ "$have_backupname" -gt 0 ] ; then
  193. cp "${TEMP_DIR}"/test1.rom "${BACKUPNAME}"
  194. echo "current image saved as ${BACKUPNAME}"
  195. fi
  196. reference_size=8388608
  197. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  198. if [ ! "$reference_size" -eq "$TEMP_SIZE" ] ; then
  199. echo -e "${RED}Error:${NC} didn't read 8M. You might be at the wrong chip."
  200. rm -rf "${TEMP_DIR}"
  201. exit 1
  202. fi
  203. echo -e "${GREEN}connection ok${NC}"
  204. echo "start unlocking ..."
  205. if [ "$me_clean" -gt 0 ] ; then
  206. ${ME_CLEANER_PATH} -d -S -O "${TEMP_DIR}"/work.rom "${TEMP_DIR}"/test1.rom
  207. else
  208. cp "${TEMP_DIR}"/test1.rom "${TEMP_DIR}"/work.rom
  209. fi
  210. if [ ! "$lock" -gt 0 ] ; then
  211. ${IFDTOOL_PATH} -u "${TEMP_DIR}"/work.rom
  212. else
  213. ${IFDTOOL_PATH} -l "${TEMP_DIR}"/work.rom
  214. fi
  215. if [ ! -e "${TEMP_DIR}"/work.rom.new ] ; then
  216. echo -e "${RED}Error:${NC} ifdtool failed. ${TEMP_DIR}/work.rom.new not found."
  217. rm -rf "${TEMP_DIR}"
  218. exit 1
  219. fi
  220. if [ "$me_clean" -gt 0 ] ; then
  221. echo -e "${GREEN}ifdtool and me_cleaner ok${NC}"
  222. else
  223. echo -e "${GREEN}ifdtool ok${NC}"
  224. fi
  225. make clean -C util/ifdtool
  226. echo "start writing..."
  227. flashrom -p ${programmer} -c "${CHIPNAME}" -w "${TEMP_DIR}"/work.rom.new
  228. rm -rf "${TEMP_DIR}"
  229. echo -e "${GREEN}DONE${NC}"