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.

104 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 get it out, we run:"
  61. echo "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128"
  62. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128
  63. usage
  64. exit 1
  65. fi
  66. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  67. INPUT_IMAGE_SIZE=$(wc -c <"$INPUT_IMAGE_PATH")
  68. reference_filesize=4194304
  69. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  70. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  71. exit 1
  72. fi
  73. echo "verifying SPI connection by reading 2 times. please wait."
  74. TEMP_DIR=`mktemp -d`
  75. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  76. echo "Could not create temp dir"
  77. exit 1
  78. fi
  79. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  80. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  81. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  82. if [ "$have_backupname" -gt 0 ] ; then
  83. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  84. echo "current image saved as ${BACKUPNAME}"
  85. fi
  86. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  87. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  88. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  89. exit 1
  90. fi
  91. rm -rf ${TEMP_DIR}
  92. echo -e "${GREEN}connection ok${NC}. flashing ${INPUT_IMAGE_NAME}"
  93. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}
  94. echo -e "${GREEN}DONE${NC}"