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.

102 lines
2.2 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. usage()
  12. {
  13. echo "Usage: $0 -i <image.rom> -c <chipname> [-k <backup_filename>]"
  14. }
  15. args=$(getopt -o i:c:k:h -- "$@")
  16. if [ $? -ne 0 ] ; then
  17. usage
  18. exit 1
  19. fi
  20. eval set -- "$args"
  21. while [ $# -gt 0 ]
  22. do
  23. case "$1" in
  24. -i)
  25. INPUT_IMAGE_PATH=$2
  26. have_input_image=1
  27. shift
  28. ;;
  29. -c)
  30. CHIPNAME=$2
  31. have_chipname=1
  32. shift
  33. ;;
  34. -k)
  35. BACKUPNAME=$2
  36. have_backupname=1
  37. shift
  38. ;;
  39. -h)
  40. usage
  41. exit 1
  42. ;;
  43. --)
  44. shift
  45. break
  46. ;;
  47. *)
  48. echo "Invalid option: $1"
  49. exit 1
  50. ;;
  51. esac
  52. shift
  53. done
  54. if [ ! "$have_input_image" -gt 0 ] ; then
  55. echo "no input image provided"
  56. usage
  57. exit 1
  58. fi
  59. if [ ! "$have_chipname" -gt 0 ] ; then
  60. echo "no chipname provided. to find it out, run:"
  61. echo "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128"
  62. exit 1
  63. fi
  64. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  65. INPUT_IMAGE_SIZE=$(wc -c <"$INPUT_IMAGE_PATH")
  66. reference_filesize=4194304
  67. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  68. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  69. exit 1
  70. fi
  71. echo "verifying SPI connection by reading 2 times. please wait."
  72. TEMP_DIR=`mktemp -d`
  73. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  74. echo "Could not create temp dir"
  75. exit 1
  76. fi
  77. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  78. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  79. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  80. if [ "$have_backupname" -gt 0 ] ; then
  81. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  82. echo "current image saved as ${BACKUPNAME}"
  83. fi
  84. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  85. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  86. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  87. exit 1
  88. fi
  89. rm -rf ${TEMP_DIR}
  90. echo "connection ok. flashing ${INPUT_IMAGE_NAME}"
  91. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}
  92. echo -e "${GREEN}DONE${NC}"