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.

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