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.

219 lines
5.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. have_input_image=0
  9. have_chipname=0
  10. have_backupname=0
  11. have_flasher=0
  12. rpi_frequency=0
  13. usage()
  14. {
  15. echo "Skulls for the X230"
  16. echo " Run this script on an external computer with a flasher"
  17. echo " connected to the X230's top chip (closer to the display"
  18. echo " and farther from you)"
  19. echo ""
  20. echo "Usage: $0 [-i <image.rom>] [-c <chipname>] [-k <backup_filename>] [-f <flasher>] [-b <spispeed>]"
  21. echo ""
  22. echo " -f <hardware_flasher> supported flashers: rpi, ch341a"
  23. echo " -i <image> path to image to flash"
  24. echo " -c <chipname> to use for flashrom"
  25. echo " -k <backup> save the current image as"
  26. echo " -b <spi frequency> frequency of the RPi SPI bus in Hz. default: 128"
  27. }
  28. args=$(getopt -o f:i:c:k:hb: -- "$@")
  29. if [ $? -ne 0 ] ; then
  30. usage
  31. exit 1
  32. fi
  33. eval set -- "$args"
  34. while [ $# -gt 0 ]
  35. do
  36. case "$1" in
  37. -f)
  38. FLASHER=$2
  39. have_flasher=1
  40. shift
  41. ;;
  42. -i)
  43. INPUT_IMAGE_PATH=$2
  44. have_input_image=1
  45. shift
  46. ;;
  47. -c)
  48. CHIPNAME=$2
  49. have_chipname=1
  50. shift
  51. ;;
  52. -k)
  53. BACKUPNAME=$2
  54. have_backupname=1
  55. shift
  56. ;;
  57. -b)
  58. rpi_frequency=$2
  59. shift
  60. ;;
  61. -h)
  62. usage
  63. exit 1
  64. ;;
  65. --)
  66. shift
  67. break
  68. ;;
  69. *)
  70. echo "Invalid option: $1"
  71. exit 1
  72. ;;
  73. esac
  74. shift
  75. done
  76. command -v flashrom >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install flashrom and run as root${NC}."; exit 1; }
  77. command -v mktemp >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install mktemp (coreutils)${NC}."; exit 1; }
  78. if [ ! "$have_input_image" -gt 0 ] ; then
  79. image_available=$(ls -1 | grep x230_coreboot_seabios || true)
  80. if [ -z "${image_available}" ] ; then
  81. echo "No image file found. Please add -i <file>"
  82. echo ""
  83. usage
  84. exit 1
  85. fi
  86. prompt="Please select a file to flash or start with the -i option to use a different one:"
  87. options=( $(find -maxdepth 1 -name "*rom" -print0 | xargs -0) )
  88. PS3="$prompt "
  89. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  90. if (( REPLY == 1 + ${#options[@]} )) ; then
  91. exit
  92. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  93. break
  94. else
  95. echo "Invalid option. Try another one."
  96. fi
  97. done
  98. fi
  99. if [ ! "$have_flasher" -gt 0 ] ; then
  100. echo "Please select the hardware you use:"
  101. PS3='Please select the hardware flasher: '
  102. options=("Raspberry Pi" "CH341A" "Quit")
  103. select opt in "${options[@]}"
  104. do
  105. case $opt in
  106. "Raspberry Pi")
  107. FLASHER="rpi"
  108. break
  109. ;;
  110. "CH341A")
  111. FLASHER="ch341a"
  112. break
  113. ;;
  114. "Quit")
  115. exit 0
  116. ;;
  117. *) echo invalid option;;
  118. esac
  119. done
  120. fi
  121. if [ ! "${rpi_frequency}" -gt 0 ] ; then
  122. rpi_frequency=512
  123. fi
  124. programmer=""
  125. if [ "${FLASHER}" = "rpi" ] ; then
  126. programmer="linux_spi:dev=/dev/spidev0.0,spispeed=${rpi_frequency}"
  127. elif [ "${FLASHER}" = "ch341a" ] ; then
  128. programmer="ch341a_spi"
  129. else
  130. echo "invalid flashrom programmer"
  131. usage
  132. exit 1
  133. fi
  134. TEMP_DIR=$(mktemp -d)
  135. if [ ! -d "$TEMP_DIR" ]; then
  136. echo "${RED}Error:${NC} Could not create temp dir"
  137. exit 1
  138. fi
  139. if [ ! "$have_chipname" -gt 0 ] ; then
  140. echo "trying to detect the chip..."
  141. flashrom -p ${programmer} &> "${TEMP_DIR}"/chips || true
  142. flashrom_error=""
  143. flashrom_error=$(cat "${TEMP_DIR}"/chips | grep -i error || true)
  144. if [ ! -z "${flashrom_error}" ] ; then
  145. cat "${TEMP_DIR}"/chips
  146. rm -rf "${TEMP_DIR}"
  147. exit 1
  148. fi
  149. CHIPNAME=""
  150. chip_found=0
  151. if [ ! "$chip_found" -gt 0 ] ; then
  152. CHIPNAME=$(cat "${TEMP_DIR}"/chips | grep Found | grep MX25L3206E | 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 EN25QH32 | 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 "Please find it manually in the list above and rerun with the -c parameter."
  168. exit 1
  169. else
  170. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  171. fi
  172. fi
  173. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  174. INPUT_IMAGE_SIZE=$(wc -c < "$INPUT_IMAGE_PATH")
  175. reference_filesize=4194304
  176. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  177. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  178. exit 1
  179. fi
  180. echo "verifying SPI connection by reading 2 times. please wait."
  181. flashrom -p ${programmer} -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  182. flashrom -p ${programmer} -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  183. cmp --silent "${TEMP_DIR}"/test1.rom "${TEMP_DIR}"/test2.rom
  184. if [ "$have_backupname" -gt 0 ] ; then
  185. cp "${TEMP_DIR}"/test1.rom "${BACKUPNAME}"
  186. sha256sum "${TEMP_DIR}"/test1.rom > "${BACKUPNAME}".sha256
  187. echo "current image saved as ${BACKUPNAME}"
  188. fi
  189. TEMP_SIZE=$(wc -c < "$TEMP_DIR/test1.rom")
  190. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  191. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  192. exit 1
  193. fi
  194. rm -rf "${TEMP_DIR}"
  195. echo -e "${GREEN}connection ok${NC}. flashing ${INPUT_IMAGE_NAME}"
  196. flashrom -p ${programmer} -c "${CHIPNAME}" -w "${INPUT_IMAGE_PATH}"
  197. echo -e "${GREEN}DONE${NC}"