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