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.

201 lines
4.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. have_input_image=0
  9. have_chipname=0
  10. have_backupname=0
  11. have_flasher=0
  12. usage()
  13. {
  14. echo "Skulls for the X230"
  15. echo " Run this script on an external computer with a flasher"
  16. echo " connected to the X230's top chip (closer to the display"
  17. echo " and farther from you)"
  18. echo ""
  19. echo "Usage: $0 -i <image.rom> [-c <chipname>] [-k <backup_filename>]"
  20. echo ""
  21. echo " -f <hardware_flasher>"
  22. echo " supported flashers: rpi, ch341a"
  23. echo ""
  24. echo " -i <path to image to flash>"
  25. echo " -c <chipname> to use for flashrom"
  26. echo " -k <path to backup to save>"
  27. }
  28. args=$(getopt -o f:i:c:k:h -- "$@")
  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. -h)
  58. usage
  59. exit 1
  60. ;;
  61. --)
  62. shift
  63. break
  64. ;;
  65. *)
  66. echo "Invalid option: $1"
  67. exit 1
  68. ;;
  69. esac
  70. shift
  71. done
  72. command -v flashrom >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install flashrom and run as root${NC}."; exit 1; }
  73. if [ ! "$have_input_image" -gt 0 ] ; then
  74. image_available=$(ls -1 | grep x230_coreboot_seabios || true)
  75. if [ -z "${image_available}" ] ; then
  76. echo "No image file found. Please add -i <file>"
  77. echo ""
  78. usage
  79. exit 1
  80. fi
  81. prompt="file not specified. Please select a file to flash:"
  82. options=( $(find -maxdepth 1 -name "x230_coreboot_seabios*rom" -print0 | xargs -0) )
  83. PS3="$prompt "
  84. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  85. if (( REPLY == 1 + ${#options[@]} )) ; then
  86. exit
  87. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  88. echo "You picked $INPUT_IMAGE_PATH which is file $REPLY"
  89. break
  90. else
  91. echo "Invalid option. Try another one."
  92. fi
  93. done
  94. fi
  95. if [ ! "$have_flasher" -gt 0 ] ; then
  96. echo "Please select the hardware you use:"
  97. PS3='Please select the hardware flasher: '
  98. options=("Raspberry Pi" "CH341A" "Quit")
  99. select opt in "${options[@]}"
  100. do
  101. case $opt in
  102. "Raspberry Pi")
  103. FLASHER="rpi"
  104. break
  105. ;;
  106. "CH341A")
  107. FLASHER="ch341a"
  108. break
  109. ;;
  110. "Quit")
  111. exit 0
  112. ;;
  113. *) echo invalid option;;
  114. esac
  115. done
  116. fi
  117. programmer=""
  118. if [ "${FLASHER}" = "rpi" ] ; then
  119. echo "Ok. Run this on a Rasperry Pi."
  120. programmer="linux_spi:dev=/dev/spidev0.0,spispeed=128"
  121. elif [ "${FLASHER}" = "ch341a" ] ; then
  122. echo "Ok. Connect a CH341A programmer"
  123. programmer="ch341a_spi"
  124. else
  125. echo "invalid flashrom programmer"
  126. usage
  127. exit 1
  128. fi
  129. TEMP_DIR=`mktemp -d`
  130. if [ ! "$have_chipname" -gt 0 ] ; then
  131. echo "trying to detect the chip..."
  132. flashrom -p ${programmer} &> ${TEMP_DIR}/chips || true
  133. flashrom_error=""
  134. flashrom_error=$(cat ${TEMP_DIR}/chips | grep -i error || true)
  135. if [ ! -z "${flashrom_error}" ] ; then
  136. cat ${TEMP_DIR}/chips
  137. rm -rf ${TEMP_DIR}
  138. exit 1
  139. fi
  140. CHIPNAME=""
  141. chip_found=0
  142. CHIPNAME=$(cat ${TEMP_DIR}/chips | grep Found | grep "MX25L3206E" | grep -o '".*"' || true)
  143. if [ ! -z "${CHIPNAME}" ] ; then
  144. chip_found=1
  145. fi
  146. if [ ! "$chip_found" -gt 0 ] ; then
  147. echo "chip not detected."
  148. flashrom -p ${programmer}
  149. rm -rf ${TEMP_DIR}
  150. echo "Please find it manually in the list above and rerun with the -c parameter."
  151. exit 1
  152. else
  153. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  154. fi
  155. fi
  156. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  157. INPUT_IMAGE_SIZE=$(wc -c <"$INPUT_IMAGE_PATH")
  158. reference_filesize=4194304
  159. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  160. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  161. exit 1
  162. fi
  163. echo "verifying SPI connection by reading 2 times. please wait."
  164. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  165. echo "Could not create temp dir"
  166. exit 1
  167. fi
  168. flashrom -p ${programmer} -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  169. flashrom -p ${programmer} -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  170. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  171. if [ "$have_backupname" -gt 0 ] ; then
  172. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  173. echo "current image saved as ${BACKUPNAME}"
  174. fi
  175. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  176. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  177. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  178. exit 1
  179. fi
  180. rm -rf ${TEMP_DIR}
  181. echo -e "${GREEN}connection ok${NC}. flashing ${INPUT_IMAGE_NAME}"
  182. flashrom -p ${programmer} -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}
  183. echo -e "${GREEN}DONE${NC}"