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.

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