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.

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