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. source "util/functions.sh"
  10. IFDTOOL=./util/ifdtool/ifdtool
  11. ME_CLEANER_PATH=./util/me_cleaner/me_cleaner.py
  12. have_chipname=0
  13. have_backupname=0
  14. me_clean=0
  15. lock=0
  16. have_flasher=0
  17. rpi_frequency=0
  18. usage()
  19. {
  20. echo "Skulls for the X230"
  21. echo " Run this script on an external computer with a flasher"
  22. echo " connected to the X230's bottom chip (farther away from"
  23. echo " the display, closer to you)."
  24. echo ""
  25. echo "Usage: $0 [-m] [-k <backup_filename>] [-l] [-f <flasher>] [-b <spispeed>] [-c <chip>]"
  26. echo ""
  27. echo " -f <hardware_flasher> supported flashers: rpi, ch341a"
  28. echo " -c <chipname> flashrom chip name to use"
  29. echo " -m apply me_cleaner -S -d"
  30. echo " -l lock the flash instead of unlocking it"
  31. echo " -k <backup> save the current image as"
  32. echo " -b <spi frequency> frequency of the RPi SPI bus in Hz. default: 128"
  33. }
  34. args=$(getopt -o f:mlc:k:hb: -- "$@")
  35. if [ $? -ne 0 ] ; then
  36. usage
  37. exit 1
  38. fi
  39. eval set -- "$args"
  40. while [ $# -gt 0 ]
  41. do
  42. case "$1" in
  43. -f)
  44. FLASHER=$2
  45. have_flasher=1
  46. shift
  47. ;;
  48. -m)
  49. me_clean=1
  50. ;;
  51. -l)
  52. lock=1
  53. ;;
  54. -c)
  55. CHIPNAME=$2
  56. have_chipname=1
  57. shift
  58. ;;
  59. -k)
  60. BACKUPNAME=$2
  61. have_backupname=1
  62. shift
  63. ;;
  64. -b)
  65. rpi_frequency=$2
  66. shift
  67. ;;
  68. -h)
  69. usage
  70. exit 1
  71. ;;
  72. --)
  73. shift
  74. break
  75. ;;
  76. *)
  77. echo "Invalid option: $1"
  78. usage
  79. exit 1
  80. ;;
  81. esac
  82. shift
  83. done
  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 X230"
  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}"