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.

257 lines
5.9 KiB

5 years ago
  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_PATH=./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 T430"
  20. echo " Run this script on an external computer with a flasher"
  21. echo " connected to the T430's bottom chip (closer to the HDD/SSD SATA"
  22. echo " connector, further from the connector for the CD)."
  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 T430"
  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. echo "chip not detected."
  159. flashrom -p ${programmer} || true
  160. rm -rf "${TEMP_DIR}"
  161. echo "chip not detected. Please find it manually and rerun with the -c parameter."
  162. exit 1
  163. else
  164. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  165. fi
  166. fi
  167. make -C util/ifdtool
  168. if [ ! -e ${IFDTOOL_PATH} ] ; then
  169. echo "ifdtool not found at ${IFDTOOL_PATH}"
  170. exit 1
  171. fi
  172. if [ ! "$me_clean" -gt 0 ] ; then
  173. echo -e "Intel ME will ${RED}not${NC} be cleaned. Use -m if it should be."
  174. else
  175. echo -e "Intel ME will be ${GREEN}cleaned${NC}."
  176. fi
  177. if [ ! "$lock" -gt 0 ] ; then
  178. echo -e "The flash ROM will be ${GREEN}unlocked${NC}."
  179. else
  180. echo -e "The flash ROM will be ${RED}locked${NC}."
  181. fi
  182. if [ "$me_clean" -gt 0 ] ; then
  183. if [ ! -e ${ME_CLEANER_PATH} ] ; then
  184. echo "me_cleaner not found at ${ME_CLEANER_PATH}"
  185. rm -rf "${TEMP_DIR}"
  186. exit 1
  187. fi
  188. fi
  189. echo "Start reading 2 times. Please be patient..."
  190. flashrom -p ${programmer} -c ${CHIPNAME} -r "${TEMP_DIR}"/test1.rom
  191. flashrom -p ${programmer} -c ${CHIPNAME} -r "${TEMP_DIR}"/test2.rom
  192. cmp --silent "${TEMP_DIR}"/test1.rom "${TEMP_DIR}"/test2.rom
  193. if [ "$have_backupname" -gt 0 ] ; then
  194. cp "${TEMP_DIR}"/test1.rom "${BACKUPNAME}"
  195. echo "current image saved as ${BACKUPNAME}"
  196. fi
  197. reference_size=8388608
  198. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  199. if [ ! "$reference_size" -eq "$TEMP_SIZE" ] ; then
  200. echo -e "${RED}Error:${NC} didn't read 8M. You might be at the wrong chip."
  201. rm -rf "${TEMP_DIR}"
  202. exit 1
  203. fi
  204. echo -e "${GREEN}connection ok${NC}"
  205. echo "start unlocking ..."
  206. if [ "$me_clean" -gt 0 ] ; then
  207. ${ME_CLEANER_PATH} -d -S -O "${TEMP_DIR}"/work.rom "${TEMP_DIR}"/test1.rom
  208. else
  209. cp "${TEMP_DIR}"/test1.rom "${TEMP_DIR}"/work.rom
  210. fi
  211. if [ ! "$lock" -gt 0 ] ; then
  212. ${IFDTOOL_PATH} -u "${TEMP_DIR}"/work.rom
  213. else
  214. ${IFDTOOL_PATH} -l "${TEMP_DIR}"/work.rom
  215. fi
  216. if [ ! -e "${TEMP_DIR}"/work.rom.new ] ; then
  217. echo -e "${RED}Error:${NC} ifdtool failed. ${TEMP_DIR}/work.rom.new not found."
  218. rm -rf "${TEMP_DIR}"
  219. exit 1
  220. fi
  221. if [ "$me_clean" -gt 0 ] ; then
  222. echo -e "${GREEN}ifdtool and me_cleaner ok${NC}"
  223. else
  224. echo -e "${GREEN}ifdtool ok${NC}"
  225. fi
  226. make clean -C util/ifdtool
  227. echo "start writing..."
  228. flashrom -p ${programmer} -c "${CHIPNAME}" -w "${TEMP_DIR}"/work.rom.new
  229. rm -rf "${TEMP_DIR}"
  230. echo -e "${GREEN}DONE${NC}"