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.

90 lines
1.7 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 flashrom without arguments"
  61. usage
  62. exit 1
  63. fi
  64. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  65. echo "verifying SPI connection by reading"
  66. TEMP_DIR=`mktemp -d`
  67. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  68. echo "Could not create temp dir"
  69. exit 1
  70. fi
  71. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  72. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  73. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  74. if [ "$have_backupname" -gt 0 ] ; then
  75. mv ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  76. echo "current image saved as ${BACKUPIMAGE}"
  77. fi
  78. rm -rf ${TEMP_DIR}
  79. echo -e "connection ok. flashing ${GREEN}${INPUT_IMAGE_NAME}${NC}"
  80. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}