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.

238 lines
7.0 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. REMOTE_SHA256=$(curl -sL https://raw.githubusercontent.com/merge/skulls/master/x230/x230_skulls.sh | sha256sum | cut -d' ' -f1)
  64. CURRENT_SHA256=$(sha256sum $0 | cut -d' ' -f1)
  65. if [[ $CURRENT_SHA256 != $REMOTE_SHA256 ]] ; then
  66. echo -e "${RED}Script not equal to latest version on Github${NC}"
  67. mv $0 $0.old
  68. curl -sLO https://raw.githubusercontent.com/merge/skulls/master/x230/x230_skulls.sh
  69. chmod +rwx $0
  70. echo "Downloaded the latest version from Github and backed up the old version to $0.old"
  71. echo "Run command again"
  72. exit
  73. fi;
  74. CURRENT_VERSION=$(head -2 NEWS | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
  75. UPSTREAM_FILE=$(curl -s https://api.github.com/repos/merge/skulls/releases | grep browser_download_url | grep skulls-x230- | cut -d'"' -f4 | cut -d'/' -f9 | head -n 1)
  76. UPSTREAM_VERSION=$(curl -s https://api.github.com/repos/merge/skulls/releases | grep browser_download_url | grep skulls-x230- | cut -d'"' -f4 | cut -d'/' -f9 | head -n 1 | egrep -o "([0-9]{1,}\.)+[0-9]{1,}")
  77. UPSTREAM_X230=$(echo ${UPSTREAM_FILE} | grep x230)
  78. if [[ -z "$UPSTREAM_X230" ]] ; then
  79. echo "The latest release didn't include the X230"
  80. exit 0
  81. fi
  82. if [[ $verbose -gt 0 ]] ; then
  83. echo "This is v$CURRENT_VERSION and latest is v$UPSTREAM_VERSION"
  84. fi
  85. if [[ "$CURRENT_VERSION" = "$UPSTREAM_VERSION" ]] ; then
  86. echo -e "${GREEN}You are using the latest version of Skulls for the X230${NC}"
  87. exit 0
  88. elif [[ "$CURRENT_VERSION" < "$UPSTREAM_VERSION" ]] ; then
  89. echo -e "${RED}You have ${CURRENT_VERSION} but there is version ${UPSTREAM_VERSION} available for the X230. Please update.${NC}"
  90. read -r -p "Download it to the parent directory now? [y/N] " response
  91. case "$response" in
  92. [yY][eE][sS]|[yY])
  93. UPSTREAM_URL=$(curl -s https://api.github.com/repos/merge/skulls/releases/latest | grep browser_download_url | cut -d'"' -f4 | head -n 1)
  94. 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)
  95. cd ..
  96. curl -LO ${UPSTREAM_URL}
  97. curl -LO ${UPSTREAM_URL_SHA256}
  98. sha256sum -c ${UPSTREAM_FILE}.sha256
  99. mkdir skulls-x230-${UPSTREAM_VERSION}
  100. tar -xf ${UPSTREAM_FILE} -C skulls-x230-${UPSTREAM_VERSION}/
  101. echo "Version ${UPSTREAM_VERSION} extracted to ../skulls-x230-${UPSTREAM_VERSION}/"
  102. echo "Please continue in the new directory."
  103. ;;
  104. *)
  105. exit 0
  106. ;;
  107. esac
  108. else
  109. echo "You seem to use a development version. Please use release package skulls-x230 ${UPSTREAM_VERSION} for flashing."
  110. fi
  111. exit 0
  112. fi
  113. force_x230_and_root
  114. BIOS_VENDOR=$(${DMIDECODE} -t bios | grep Vendor | cut -d':' -f2)
  115. if [[ $BIOS_VENDOR != *"coreboot"* ]] ; then
  116. BIOS_VERSION=$(${DMIDECODE} -s bios-version | grep -o '[1-2].[0-7][0-9]')
  117. bios_major=$(echo "$BIOS_VERSION" | cut -d. -f1)
  118. bios_minor=$(echo "$BIOS_VERSION" | cut -d. -f2)
  119. if [ "${bios_minor}" -ge "61" ] ; then
  120. echo "Ready to use external_install_bottom.sh and external_install_top.sh"
  121. echo "Please run both scripts from a different computer with a"
  122. echo "hardware SPI flasher."
  123. else
  124. echo -e "The installed original BIOS is very old."
  125. echo -e "${RED}Please upgrade${NC} from lenovo.com before installing coreboot."
  126. fi
  127. exit 0
  128. fi
  129. if [[ "$verbose" -gt 0 ]] ; then
  130. if [ -d "/sys/class/power_supply/BAT0" ] ; then
  131. bat_last_full=$(cat /sys/class/power_supply/BAT0/charge_full)
  132. bat_design_cap=$(cat /sys/class/power_supply/BAT0/charge_full_design)
  133. bat_health=$(echo "scale=2 ; $bat_last_full/$bat_design_cap" | bc | sed 's/^\./0./')
  134. echo "INFO: Battery hardware health is $bat_health%"
  135. fi
  136. fi
  137. if [ ! "$have_input_image" -gt 0 ] ; then
  138. image_available=$(ls -1 | grep x230_coreboot_seabios || true)
  139. if [ -z "${image_available}" ] ; then
  140. echo "No image file found. Please add -i <file>"
  141. echo ""
  142. usage
  143. exit 1
  144. fi
  145. prompt="file not specified. Please select a file to flash. Please read the README for details about the differences:"
  146. options=( $(find -maxdepth 1 -name "x230_coreboot_seabios*rom" -print0 | xargs -0) )
  147. PS3="$prompt "
  148. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  149. if (( REPLY == 1 + ${#options[@]} )) ; then
  150. exit
  151. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  152. break
  153. else
  154. echo "Invalid option. Try another one."
  155. fi
  156. done
  157. fi
  158. OUTPUT_PATH=output
  159. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  160. OUTPUT_IMAGE_NAME=${INPUT_IMAGE_NAME%%.*}_prepared_12mb.rom
  161. OUTPUT_IMAGE_PATH=${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}
  162. echo -e "input: ${INPUT_IMAGE_NAME}"
  163. echo -e "output: ${OUTPUT_IMAGE_PATH}"
  164. input_filesize=$(wc -c <"$INPUT_IMAGE_PATH")
  165. reference_filesize=4194304
  166. if [ ! "$input_filesize" -eq "$reference_filesize" ] ; then
  167. echo "Error: input file must be 4MB of size"
  168. exit 1
  169. fi
  170. rm -rf ${OUTPUT_PATH}
  171. mkdir ${OUTPUT_PATH}
  172. dd if=/dev/zero of="${OUTPUT_IMAGE_PATH}" bs=4M count=2 status=none
  173. dd if="${INPUT_IMAGE_PATH}" oflag=append conv=notrunc of="${OUTPUT_IMAGE_PATH}" bs=4M status=none
  174. LAYOUT_FILENAME="x230-layout.txt"
  175. echo "0x00000000:0x00000fff ifd" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  176. echo "0x00001000:0x00002fff gbe" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  177. echo "0x00003000:0x004fffff me" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  178. echo "0x00500000:0x007fffff unused" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  179. echo "0x00800000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  180. echo -e "${YELLOW}WARNING${NC}: Make sure not to power off your computer or interrupt this process in any way!"
  181. echo -e " Interrupting this process may result in irreparable damage to your computer!"
  182. check_battery
  183. while true; do
  184. read -r -p "Flash the BIOS now? y/N: " yn
  185. case $yn in
  186. [Yy]* ) cd ${OUTPUT_PATH} && ${FLASHROM} --force --noverify-all -p internal --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"; break;;
  187. [Nn]* ) exit;;
  188. * ) exit;;
  189. esac
  190. done
  191. rm -rf ${OUTPUT_PATH}
  192. while true; do
  193. read -r -p "Power off now? (please do!) Y/n: " yn
  194. case $yn in
  195. [Yy]* ) poweroff ;;
  196. [Nn]* ) exit;;
  197. * ) poweroff ;;
  198. esac
  199. done