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.

207 lines
5.8 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 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 [ ! "$have_input_image" -gt 0 ] ; then
  118. image_available=$(ls -1 | grep x230_coreboot_seabios || true)
  119. if [ -z "${image_available}" ] ; then
  120. echo "No image file found. Please add -i <file>"
  121. echo ""
  122. usage
  123. exit 1
  124. fi
  125. prompt="file not specified. Please select a file to flash. Please read the README for details about the differences:"
  126. options=( $(find -maxdepth 1 -name "x230_coreboot_seabios*rom" -print0 | xargs -0) )
  127. PS3="$prompt "
  128. select INPUT_IMAGE_PATH in "${options[@]}" "Quit" ; do
  129. if (( REPLY == 1 + ${#options[@]} )) ; then
  130. exit
  131. elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
  132. break
  133. else
  134. echo "Invalid option. Try another one."
  135. fi
  136. done
  137. fi
  138. OUTPUT_PATH=output
  139. INPUT_IMAGE_NAME=$(basename "${INPUT_IMAGE_PATH}")
  140. OUTPUT_IMAGE_NAME=${INPUT_IMAGE_NAME%%.*}_prepared_12mb.rom
  141. OUTPUT_IMAGE_PATH=${OUTPUT_PATH}/${OUTPUT_IMAGE_NAME}
  142. echo -e "input: ${INPUT_IMAGE_NAME}"
  143. echo -e "output: ${OUTPUT_IMAGE_PATH}"
  144. input_filesize=$(wc -c <"$INPUT_IMAGE_PATH")
  145. reference_filesize=4194304
  146. if [ ! "$input_filesize" -eq "$reference_filesize" ] ; then
  147. echo "Error: input file must be 4MB of size"
  148. exit 1
  149. fi
  150. rm -rf ${OUTPUT_PATH}
  151. mkdir ${OUTPUT_PATH}
  152. dd if=/dev/zero of="${OUTPUT_IMAGE_PATH}" bs=4M count=2 status=none
  153. dd if="${INPUT_IMAGE_PATH}" oflag=append conv=notrunc of="${OUTPUT_IMAGE_PATH}" bs=4M status=none
  154. LAYOUT_FILENAME="x230-layout.txt"
  155. echo "0x00000000:0x007fffff ifdmegbe" > ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  156. echo "0x00800000:0x00bfffff bios" >> ${OUTPUT_PATH}/${LAYOUT_FILENAME}
  157. echo -e "${YELLOW}WARNING${NC}: Make sure not to power off your computer or interrupt this process in any way!"
  158. echo -e " Interrupting this process may result in irreparable damage to your computer!"
  159. check_battery
  160. while true; do
  161. read -r -p "Flash the BIOS now? y/N: " yn
  162. case $yn in
  163. [Yy]* ) cd output && flashrom -p internal --layout ${LAYOUT_FILENAME} --image bios -w "${OUTPUT_IMAGE_NAME}"; break;;
  164. [Nn]* ) exit;;
  165. * ) exit;;
  166. esac
  167. done
  168. while true; do
  169. read -r -p "Reboot now? (please do!) Y/n: " yn
  170. case $yn in
  171. [Yy]* ) reboot ;;
  172. [Nn]* ) exit;;
  173. * ) reboot;;
  174. esac
  175. done