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.

175 lines
3.9 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"
  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. if [ ! "$have_input_image" -gt 0 ] ; then
  73. echo "Image file to flash is needed. Please add -i <file>"
  74. echo ""
  75. usage
  76. exit 1
  77. fi
  78. if [ ! "$have_flasher" -gt 0 ] ; then
  79. echo "Please select the hardware you use:"
  80. PS3='Please select the hardware flasher: '
  81. options=("Raspberry Pi" "CH341A" "Quit")
  82. select opt in "${options[@]}"
  83. do
  84. case $opt in
  85. "Raspberry Pi")
  86. FLASHER="rpi"
  87. break
  88. ;;
  89. "CH341A")
  90. FLASHER="ch341a"
  91. break
  92. ;;
  93. "Quit")
  94. exit 0
  95. ;;
  96. *) echo invalid option;;
  97. esac
  98. done
  99. fi
  100. if [ "${FLASHER}" = "rpi" ] ; then
  101. echo "Ok. Run this on a Rasperry Pi."
  102. elif [ "${FLASHER}" = "ch341a" ] ; then
  103. echo "The CH341A is not yet supported"
  104. exit 0
  105. else
  106. exit 1
  107. fi
  108. TEMP_DIR=`mktemp -d`
  109. if [ ! "$have_chipname" -gt 0 ] ; then
  110. echo "trying to detect the chip..."
  111. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 &> ${TEMP_DIR}/chips || true
  112. flashrom_error=""
  113. flashrom_error=$(cat ${TEMP_DIR}/chips | grep -i error || true)
  114. if [ ! -z "${flashrom_error}" ] ; then
  115. cat ${TEMP_DIR}/chips
  116. rm -rf ${TEMP_DIR}
  117. exit 1
  118. fi
  119. CHIPNAME=""
  120. chip_found=0
  121. CHIPNAME=$(cat ${TEMP_DIR}/chips | grep Found | grep "MX25L3206E" | grep -o '".*"' || true)
  122. if [ ! -z "${CHIPNAME}" ] ; then
  123. chip_found=1
  124. fi
  125. if [ ! "$chip_found" -gt 0 ] ; then
  126. echo "chip not detected."
  127. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128
  128. rm -rf ${TEMP_DIR}
  129. echo "Please find it manually in the list above and rerun with the -c parameter."
  130. exit 1
  131. else
  132. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  133. fi
  134. fi
  135. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  136. INPUT_IMAGE_SIZE=$(wc -c <"$INPUT_IMAGE_PATH")
  137. reference_filesize=4194304
  138. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  139. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  140. exit 1
  141. fi
  142. echo "verifying SPI connection by reading 2 times. please wait."
  143. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  144. echo "Could not create temp dir"
  145. exit 1
  146. fi
  147. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  148. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  149. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  150. if [ "$have_backupname" -gt 0 ] ; then
  151. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  152. echo "current image saved as ${BACKUPNAME}"
  153. fi
  154. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  155. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  156. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  157. exit 1
  158. fi
  159. rm -rf ${TEMP_DIR}
  160. echo -e "${GREEN}connection ok${NC}. flashing ${INPUT_IMAGE_NAME}"
  161. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}
  162. echo -e "${GREEN}DONE${NC}"