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.

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