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.

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