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.

153 lines
3.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. YELLOW='\033[0;33m'
  7. NC='\033[0m'
  8. set -e
  9. have_input_image=0
  10. usage()
  11. {
  12. echo "Skulls for the X230"
  13. echo " Run this script on the X230 directly."
  14. echo ""
  15. echo " This flashes Heads to your BIOS, see http://osresearch.net"
  16. echo " Heads is a different project. No image is included."
  17. echo " Read https://github.com/osresearch/heads for how to build it"
  18. echo " Make sure you booted Linux with iomem=relaxed"
  19. echo ""
  20. echo "Usage: $0 -i <heads_image>.rom"
  21. }
  22. check_battery() {
  23. local capacity=$(cat /sys/class/power_supply/BAT*/capacity 2>/dev/null || echo -ne "0")
  24. local online=$(cat /sys/class/power_supply/AC/online 2>/dev/null || cat /sys/class/power_supply/ADP*/online 2>/dev/null || echo -ne "0")
  25. local failed=0
  26. if [ "${online}" == "0" ] ; then
  27. failed=1
  28. fi
  29. if [ "${capacity}" -lt 25 ]; then
  30. failed=1
  31. fi
  32. if [ $failed == "1" ]; then
  33. echo -e "${YELLOW}WARNING:${NC} To prevent shutdowns, we recommend to only run this script when"
  34. echo " your laptop is plugged in to the power supply AND"
  35. echo " the battery is present and sufficiently charged (over 25%)."
  36. while true; do
  37. read -r -p "Continue anyways? (please do NOT!) y/N: " yn
  38. case $yn in
  39. [Yy]* ) break;;
  40. [Nn]* ) exit;;
  41. * ) exit;;
  42. esac
  43. done
  44. fi
  45. }
  46. args=$(getopt -o i:h -- "$@")
  47. if [ $? -ne 0 ] ; then
  48. usage
  49. exit 1
  50. fi
  51. eval set -- "$args"
  52. while [ $# -gt 0 ]
  53. do
  54. case "$1" in
  55. -i)
  56. INPUT_IMAGE_PATH=$2
  57. have_input_image=1
  58. shift
  59. ;;
  60. -h)
  61. usage
  62. exit 1
  63. ;;
  64. --)
  65. shift
  66. break
  67. ;;
  68. *)
  69. echo "Invalid option: $1"
  70. exit 1
  71. ;;
  72. esac
  73. shift
  74. done
  75. if [ ! "$have_input_image" -gt 0 ] ; then
  76. image_available=$(ls -1 | grep rom || true)
  77. if [ -z "${image_available}" ] ; then
  78. echo "No image file found. Please add -i <file>"
  79. echo ""
  80. usage
  81. exit 1
  82. fi
  83. prompt="file not specified. Please select a file to flash:"
  84. options=( $(find -maxdepth 1 -name "*rom" -print0 | xargs -0) )
  85. PS3="$prompt "
  86. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  87. if (( REPLY == 1 + ${#options[@]} )) ; then
  88. exit
  89. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  90. break
  91. else
  92. echo "Invalid option. Try another one."
  93. fi
  94. done
  95. fi
  96. OUTPUT_PATH=output
  97. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  98. OUTPUT_IMAGE_NAME=${INPUT_IMAGE_NAME%%.*}_prepared.rom
  99. OUTPUT_IMAGE_PATH=${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}
  100. echo -e "input: ${INPUT_IMAGE_NAME}"
  101. echo -e "output: ${OUTPUT_IMAGE_PATH}"
  102. input_filesize=$(wc -c <"$INPUT_IMAGE_PATH")
  103. reference_filesize=12582912
  104. if [ ! "$input_filesize" -eq "$reference_filesize" ] ; then
  105. echo "Error: input file must be 12MB of size"
  106. exit 1
  107. fi
  108. rm -rf ${OUTPUT_PATH}
  109. mkdir ${OUTPUT_PATH}
  110. cp "${INPUT_IMAGE_PATH}" "${OUTPUT_IMAGE_PATH}"
  111. LAYOUT_FILENAME="x230-layout-heads.txt"
  112. echo "0x00000000:0x00000fff ifd" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  113. echo "0x00001000:0x00002fff gbe" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  114. echo "0x00003000:0x004fffff me" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  115. echo "0x00500000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  116. echo -e "${YELLOW}WARNING${NC}: Make sure not to power off your computer or interrupt this process in any way!"
  117. echo -e " Interrupting this process may result in irreparable damage to your computer!"
  118. check_battery
  119. while true; do
  120. read -r -p "Flash the BIOS now? y/N: " yn
  121. case $yn in
  122. [Yy]* ) cd output && flashrom -p internal --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"; break;;
  123. [Nn]* ) exit;;
  124. * ) exit;;
  125. esac
  126. done
  127. while true; do
  128. read -r -p "Reboot now? (please do!) Y/n: " yn
  129. case $yn in
  130. [Yy]* ) reboot ;;
  131. [Nn]* ) exit;;
  132. * ) reboot;;
  133. esac
  134. done