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.

134 lines
2.8 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. IFDTOOL_PATH=./util/ifdtool/ifdtool
  9. ME_CLEANER_PATH=./util/me_cleaner/me_cleaner.py
  10. have_chipname=0
  11. have_backupname=0
  12. me_clean=0
  13. usage()
  14. {
  15. echo "Usage: $0 -c <chipname> [-m] [-k <backup_filename>]"
  16. echo ""
  17. echo "-m apply me_cleaner -S"
  18. }
  19. args=$(getopt -o mc:k:h -- "$@")
  20. if [ $? -ne 0 ] ; then
  21. usage
  22. exit 1
  23. fi
  24. eval set -- "$args"
  25. while [ $# -gt 0 ]
  26. do
  27. case "$1" in
  28. -m)
  29. me_clean=1
  30. ;;
  31. -c)
  32. CHIPNAME=$2
  33. have_chipname=1
  34. shift
  35. ;;
  36. -k)
  37. BACKUPNAME=$2
  38. have_backupname=1
  39. shift
  40. ;;
  41. -h)
  42. usage
  43. exit 1
  44. ;;
  45. --)
  46. shift
  47. break
  48. ;;
  49. *)
  50. echo "Invalid option: $1"
  51. exit 1
  52. ;;
  53. esac
  54. shift
  55. done
  56. if [ ! "$have_chipname" -gt 0 ] ; then
  57. echo "no chipname provided. to find it out, run:"
  58. echo "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128"
  59. usage
  60. exit 1
  61. fi
  62. make -C util/ifdtool
  63. if [ ! -e ${IFDTOOL_PATH} ] ; then
  64. echo "ifdtool not found at ${IFDTOOL_PATH}"
  65. exit 1
  66. fi
  67. if [ ! "$me_clean" -gt 0 ] ; then
  68. echo "Intel ME will NOT be cleaned. Use -m if it should be."
  69. else
  70. echo "Intel ME will be cleaned."
  71. fi
  72. if [ "$me_clean" -gt 0 ] ; then
  73. if [ ! -e ${ME_CLEANER_PATH} ] ; then
  74. echo "me_cleaner not found at ${ME_CLEANER_PATH}"
  75. exit 1
  76. fi
  77. fi
  78. echo "Start reading 2 times. Please be patient."
  79. TEMP_DIR=`mktemp -d`
  80. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  81. echo -e "${RED}Error:${NC} Could not create temp dir"
  82. exit 1
  83. fi
  84. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  85. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  86. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  87. if [ "$have_backupname" -gt 0 ] ; then
  88. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  89. echo "current image saved as ${BACKUPNAME}"
  90. fi
  91. reference_size=8388608
  92. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  93. if [ ! "$reference_size" -eq "$TEMP_SIZE" ] ; then
  94. echo -e "${RED}Error:${NC} didn't read 8M. You might be at the wrong chip."
  95. rm -rf ${TEMP_DIR}
  96. exit 1
  97. fi
  98. echo -e "${GREEN}connection ok${NC}"
  99. echo "start unlocking ..."
  100. if [ "$me_clean" -gt 0 ] ; then
  101. ${ME_CLEANER_PATH} -S -O ${TEMP_DIR}/work.rom ${TEMP_DIR}/test1.rom
  102. else
  103. cp ${TEMP_DIR}/test1.rom ${TEMP_DIR}/work.rom
  104. fi
  105. ${IFDTOOL_PATH} -u ${TEMP_DIR}/work.rom
  106. if [ ! -e ${TEMP_DIR}/work.rom.new ] ; then
  107. echo -e "${RED}Error:${NC} Unlocking failed. ${TEMP_DIR}/work.rom.new not found."
  108. rm -rf ${TEMP_DIR}
  109. exit 1
  110. fi
  111. if [ "$me_clean" -gt 0 ] ; then
  112. echo -e "${GREEN}unlock and me_cleaner ok${NC}"
  113. else
  114. echo -e "${GREEN}unlock ok${NC}"
  115. fi
  116. make clean -C util/ifdtool
  117. echo "start writing..."
  118. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${TEMP_DIR}/work.rom.new
  119. echo -e "${GREEN}DONE${NC}"