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.

224 lines
6.5 KiB

  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-3.0+
  3. # Copyright (C) 2019, Martin Kepplinger <martink@posteo.de>
  4. set -e
  5. cd "$(dirname "$0")"
  6. source "util/functions.sh"
  7. have_input_image=0
  8. request_update=0
  9. verbose=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] [-U] [-h]"
  19. echo "Options:"
  20. echo " -i path to the image to flash"
  21. echo " -U update: check for a new Skulls package online"
  22. echo " -v verbose output. prints more information"
  23. echo " -h print this help text"
  24. }
  25. args=$(getopt -o i:hUv -- "$@")
  26. if [ $? -ne 0 ] ; then
  27. usage
  28. exit 1
  29. fi
  30. eval set -- "$args"
  31. while [ $# -gt 0 ]
  32. do
  33. case "$1" in
  34. -i)
  35. INPUT_IMAGE_PATH=$2
  36. have_input_image=1
  37. shift
  38. ;;
  39. -h)
  40. usage
  41. exit 1
  42. ;;
  43. -U)
  44. request_update=1
  45. ;;
  46. -v)
  47. verbose=1
  48. ;;
  49. --)
  50. shift
  51. break
  52. ;;
  53. *)
  54. echo "Invalid option: $1"
  55. exit 1
  56. ;;
  57. esac
  58. shift
  59. done
  60. if [ "$request_update" -gt 0 ] ; then
  61. warn_not_root
  62. command -v curl >/dev/null 2>&1 || { echo -e >&2 "${RED}Please install curl.${NC}"; exit 1; }
  63. CURRENT_VERSION=$(head -2 NEWS | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
  64. UPSTREAM_FILE=$(curl -s https://api.github.com/repos/merge/skulls/releases/latest | grep browser_download_url | cut -d'"' -f4 | cut -d'/' -f9 | head -n 1)
  65. UPSTREAM_VERSION=$(curl -s https://api.github.com/repos/merge/skulls/releases/latest | grep browser_download_url | cut -d'"' -f4 | cut -d'/' -f9 | head -n 1 | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
  66. UPSTREAM_X230=$(echo ${UPSTREAM_FILE} | grep x230)
  67. if [[ -z "$UPSTREAM_X230" ]] ; then
  68. echo "The latest release didn't include the X230"
  69. exit 0
  70. fi
  71. if [[ $verbose -gt 0 ]] ; then
  72. echo "This is v$CURRENT_VERSION and latest is v$UPSTREAM_VERSION"
  73. fi
  74. if [[ "$CURRENT_VERSION" = "$UPSTREAM_VERSION" ]] ; then
  75. echo -e "${GREEN}You are using the latest version of Skulls for the X230${NC}"
  76. exit 0
  77. elif [[ "$CURRENT_VERSION" < "$UPSTREAM_VERSION" ]] ; then
  78. echo -e "${RED}You have ${CURRENT_VERSION} but there is version ${UPSTREAM_VERSION} available for the X230. Please update.${NC}"
  79. read -r -p "Download it to the parent directory now? [y/N] " response
  80. case "$response" in
  81. [yY][eE][sS]|[yY])
  82. UPSTREAM_URL=$(curl -s https://api.github.com/repos/merge/skulls/releases/latest | grep browser_download_url | cut -d'"' -f4 | head -n 1)
  83. UPSTREAM_URL_SHA256=$(curl -s https://api.github.com/repos/merge/skulls/releases/latest | grep browser_download_url | cut -d'"' -f4 | head -n 3 | tail -n 1)
  84. cd ..
  85. curl -LO ${UPSTREAM_URL}
  86. curl -LO ${UPSTREAM_URL_SHA256}
  87. sha256sum -c ${UPSTREAM_FILE}.sha256
  88. mkdir skulls-x230-${UPSTREAM_VERSION}
  89. tar -xf ${UPSTREAM_FILE} -C skulls-x230-${UPSTREAM_VERSION}/
  90. echo "Version ${UPSTREAM_VERSION} extracted to ../skulls-x230-${UPSTREAM_VERSION}/"
  91. echo "Please continue in the new directory."
  92. ;;
  93. *)
  94. exit 0
  95. ;;
  96. esac
  97. else
  98. echo "You seem to use a development version. Please use release package skulls-x230 ${UPSTREAM_VERSION} for flashing."
  99. fi
  100. exit 0
  101. fi
  102. force_x230_and_root
  103. BIOS_VENDOR=$(${DMIDECODE} -t bios | grep Vendor | cut -d':' -f2)
  104. if [[ $BIOS_VENDOR != *"coreboot"* ]] ; then
  105. BIOS_VERSION=$(${DMIDECODE} -s bios-version | grep -o '[1-2].[0-7][0-9]')
  106. bios_major=$(echo "$BIOS_VERSION" | cut -d. -f1)
  107. bios_minor=$(echo "$BIOS_VERSION" | cut -d. -f2)
  108. if [ "${bios_minor}" -ge "61" ] ; then
  109. echo "Ready to use external_install_bottom.sh and external_install_top.sh"
  110. echo "Please run both scripts from a different computer with a"
  111. echo "hardware SPI flasher."
  112. else
  113. echo -e "The installed original BIOS is very old."
  114. echo -e "${RED}Please upgrade${NC} from lenovo.com before installing coreboot."
  115. fi
  116. exit 0
  117. fi
  118. if [[ "$verbose" -gt 0 ]] ; then
  119. if [ -d "/sys/class/power_supply/BAT0" ] ; then
  120. bat_last_full=$(cat /sys/class/power_supply/BAT0/charge_full)
  121. bat_design_cap=$(cat /sys/class/power_supply/BAT0/charge_full_design)
  122. bat_health=$(echo "scale=2 ; $bat_last_full/$bat_design_cap" | bc | sed 's/^\./0./')
  123. echo "INFO: Battery hardware health is $bat_health%"
  124. fi
  125. fi
  126. if [ ! "$have_input_image" -gt 0 ] ; then
  127. image_available=$(ls -1 | grep x230_coreboot_seabios || true)
  128. if [ -z "${image_available}" ] ; then
  129. echo "No image file found. Please add -i <file>"
  130. echo ""
  131. usage
  132. exit 1
  133. fi
  134. prompt="file not specified. Please select a file to flash. Please read the README for details about the differences:"
  135. options=( $(find -maxdepth 1 -name "x230_coreboot_seabios*rom" -print0 | xargs -0) )
  136. PS3="$prompt "
  137. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  138. if (( REPLY == 1 + ${#options[@]} )) ; then
  139. exit
  140. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  141. break
  142. else
  143. echo "Invalid option. Try another one."
  144. fi
  145. done
  146. fi
  147. OUTPUT_PATH=output
  148. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  149. OUTPUT_IMAGE_NAME=${INPUT_IMAGE_NAME%%.*}_prepared_12mb.rom
  150. OUTPUT_IMAGE_PATH=${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}
  151. echo -e "input: ${INPUT_IMAGE_NAME}"
  152. echo -e "output: ${OUTPUT_IMAGE_PATH}"
  153. input_filesize=$(wc -c <"$INPUT_IMAGE_PATH")
  154. reference_filesize=4194304
  155. if [ ! "$input_filesize" -eq "$reference_filesize" ] ; then
  156. echo "Error: input file must be 4MB of size"
  157. exit 1
  158. fi
  159. rm -rf ${OUTPUT_PATH}
  160. mkdir ${OUTPUT_PATH}
  161. dd if=/dev/zero of="${OUTPUT_IMAGE_PATH}" bs=4M count=2 status=none
  162. dd if="${INPUT_IMAGE_PATH}" oflag=append conv=notrunc of="${OUTPUT_IMAGE_PATH}" bs=4M status=none
  163. LAYOUT_FILENAME="x230-layout.txt"
  164. echo "0x00000000:0x00000fff ifd" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  165. echo "0x00001000:0x00002fff gbe" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  166. echo "0x00003000:0x004fffff me" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  167. echo "0x00500000:0x007fffff unused" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  168. echo "0x00800000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  169. echo -e "${YELLOW}WARNING${NC}: Make sure not to power off your computer or interrupt this process in any way!"
  170. echo -e " Interrupting this process may result in irreparable damage to your computer!"
  171. check_battery
  172. while true; do
  173. read -r -p "Flash the BIOS now? y/N: " yn
  174. case $yn in
  175. [Yy]* ) cd ${OUTPUT_PATH} && ${FLASHROM} --force --noverify-all -p internal -U --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"; break;;
  176. [Nn]* ) exit;;
  177. * ) exit;;
  178. esac
  179. done
  180. rm -rf ${OUTPUT_PATH}
  181. while true; do
  182. read -r -p "Power off now? (please do!) Y/n: " yn
  183. case $yn in
  184. [Yy]* ) poweroff ;;
  185. [Nn]* ) exit;;
  186. * ) poweroff ;;
  187. esac
  188. done