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.

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