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.

162 lines
3.9 KiB

  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-3.0+
  3. # Copyright (C) 2018, Martin Kepplinger <martink@posteo.de>
  4. set -e
  5. cd "$(dirname "$0")"
  6. source "util/functions.sh"
  7. have_input_image=0
  8. extended_mode=0
  9. usage()
  10. {
  11. echo "Skulls for the X230"
  12. echo " Run this script on the X230 directly."
  13. echo ""
  14. echo " This flashes Heads to your BIOS, see http://osresearch.net"
  15. echo " Heads is a different project. No image is included."
  16. echo " Read https://github.com/osresearch/heads for how to build it"
  17. echo " Make sure you booted Linux with iomem=relaxed"
  18. echo ""
  19. echo "Usage: $0 -i <heads_image>.rom"
  20. echo ""
  21. echo " (EXPERIMENTAL) Extended Bios:"
  22. echo " -x use extended mode"
  23. echo ""
  24. echo "Usage example:"
  25. echo "Flash an extended Heads image: $0 -x -i <heads_image>.rom"
  26. }
  27. args=$(getopt -o xi:h -- "$@")
  28. if [ $? -ne 0 ] ; then
  29. usage
  30. exit 1
  31. fi
  32. eval set -- "$args"
  33. while [ $# -gt 0 ]
  34. do
  35. case "$1" in
  36. -i)
  37. INPUT_IMAGE_PATH=$2
  38. have_input_image=1
  39. shift
  40. ;;
  41. -x)
  42. extended_mode=1
  43. ;;
  44. -h)
  45. usage
  46. exit 1
  47. ;;
  48. --)
  49. shift
  50. break
  51. ;;
  52. *)
  53. echo "Invalid option: $1"
  54. exit 1
  55. ;;
  56. esac
  57. shift
  58. done
  59. force_x230_and_root
  60. if [ ! "$have_input_image" -gt 0 ] ; then
  61. image_available=$(ls -1 | grep rom || true)
  62. if [ -z "${image_available}" ] ; then
  63. echo "No image file found. Please add -i <file>"
  64. echo ""
  65. usage
  66. exit 1
  67. fi
  68. prompt="file not specified. Please select a file to flash:"
  69. options=( $(find -maxdepth 1 -name "*rom" -print0 | xargs -0) )
  70. PS3="$prompt "
  71. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  72. if (( REPLY == 1 + ${#options[@]} )) ; then
  73. exit
  74. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  75. break
  76. else
  77. echo "Invalid option. Try another one."
  78. fi
  79. done
  80. fi
  81. OUTPUT_PATH=output
  82. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  83. OUTPUT_IMAGE_NAME=${INPUT_IMAGE_NAME%%.*}_prepared.rom
  84. echo -e "input: ${INPUT_IMAGE_NAME}"
  85. echo -e "output: ${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}"
  86. input_filesize=$(wc -c <"$INPUT_IMAGE_PATH")
  87. reference_filesize=12582912
  88. if [ ! "$input_filesize" -eq "$reference_filesize" ] ; then
  89. echo "Error: input file must be 12MB of size"
  90. exit 1
  91. fi
  92. rm -rf ${OUTPUT_PATH}
  93. mkdir ${OUTPUT_PATH}
  94. cp "${INPUT_IMAGE_PATH}" "${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}"
  95. LAYOUT_FILENAME="x230-layout-heads.txt"
  96. if [ ! "$extended_mode" -gt 0 ] ; then
  97. echo "0x00000000:0x00000fff ifd" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  98. echo "0x00001000:0x00002fff gbe" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  99. echo "0x00003000:0x004fffff me" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  100. echo "0x00500000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  101. else
  102. echo -e "${YELLOW}EXPERIMENTAL: extended flash image${NC}"
  103. echo "0x00000000:0x00000fff ifd" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  104. echo "0x00001000:0x00002fff gbe" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  105. echo "0x00003000:0x0001afff me" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  106. echo "0x0001b000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  107. fi
  108. echo -e "${YELLOW}WARNING${NC}: Make sure not to power off your computer or interrupt this process in any way!"
  109. echo -e " Interrupting this process may result in irreparable damage to your computer!"
  110. check_battery
  111. while true; do
  112. read -r -p "Flash the BIOS now? y/N: " yn
  113. case $yn in
  114. [Yy]* )
  115. if [ ! "$extended_mode" -gt 0 ] ; then
  116. cd ${OUTPUT_PATH} && ${FLASHROM} --force --noverify-all -p internal --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"
  117. else
  118. cd ${OUTPUT_PATH} && ${FLASHROM} -p internal --layout ${LAYOUT_FILENAME} --image ifd -w "${OUTPUT_IMAGE_NAME}"
  119. ${FLASHROM} -p internal --layout ${LAYOUT_FILENAME} --image me -w "${OUTPUT_IMAGE_NAME}"
  120. ${FLASHROM} -p internal --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"
  121. fi
  122. break
  123. ;;
  124. [Nn]* )
  125. exit
  126. ;;
  127. * )
  128. exit
  129. ;;
  130. esac
  131. done
  132. rm -rf ${OUTPUT_PATH}
  133. while true; do
  134. read -r -p "Power off now? (please do!) Y/n: " yn
  135. case $yn in
  136. [Yy]* ) poweroff ;;
  137. [Nn]* ) exit;;
  138. * ) poweroff ;;
  139. esac
  140. done