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.

219 lines
6.2 KiB

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