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.

208 lines
4.8 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>]"
  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 "x230_coreboot_seabios*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=128
  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. CHIPNAME=$(cat "${TEMP_DIR}"/chips | grep Found | grep MX25L3206E | grep -oP '"\K[^"\047]+(?=["\047])' || true)
  152. if [ ! -z "${CHIPNAME}" ] ; then
  153. chip_found=1
  154. fi
  155. if [ ! "$chip_found" -gt 0 ] ; then
  156. echo "chip not detected."
  157. flashrom -p ${programmer}
  158. rm -rf "${TEMP_DIR}"
  159. echo "Please find it manually in the list above and rerun with the -c parameter."
  160. exit 1
  161. else
  162. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  163. fi
  164. fi
  165. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  166. INPUT_IMAGE_SIZE=$(wc -c < "$INPUT_IMAGE_PATH")
  167. reference_filesize=4194304
  168. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  169. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  170. exit 1
  171. fi
  172. echo "verifying SPI connection by reading 2 times. please wait."
  173. flashrom -p ${programmer} -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  174. flashrom -p ${programmer} -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  175. cmp --silent "${TEMP_DIR}"/test1.rom "${TEMP_DIR}"/test2.rom
  176. if [ "$have_backupname" -gt 0 ] ; then
  177. cp "${TEMP_DIR}"/test1.rom "${BACKUPNAME}"
  178. echo "current image saved as ${BACKUPNAME}"
  179. fi
  180. TEMP_SIZE=$(wc -c < "$TEMP_DIR/test1.rom")
  181. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  182. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  183. exit 1
  184. fi
  185. rm -rf "${TEMP_DIR}"
  186. echo -e "${GREEN}connection ok${NC}. flashing ${INPUT_IMAGE_NAME}"
  187. flashrom -p ${programmer} -c "${CHIPNAME}" -w "${INPUT_IMAGE_PATH}"
  188. echo -e "${GREEN}DONE${NC}"