Configuration and helper script for easy build, assembly and installation of Coreboot on the Thinkpad X220
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.

272 lines
7.0KB

  1. #!/bin/bash
  2. set -e
  3. base_directory="$(dirname $0)"
  4. # Download stuffs
  5. function download_code() {
  6. # Coreboot
  7. if [ ! -d ./coreboot ]; then
  8. printf "Downloading Coreboot\n"
  9. git clone --recursive http://review.coreboot.org/coreboot.git ./coreboot
  10. cd ./coreboot
  11. # Checkout this specific version
  12. git checkout tags/4.10
  13. cd $base_directory
  14. else
  15. printf "Coreboot repository is already present\n"
  16. fi
  17. # GRUB
  18. if [ ! -d ./grub ]; then
  19. printf "Downloading GRUB\n"
  20. git clone git://git.savannah.gnu.org/grub.git ./grub
  21. # Checkout this specific version
  22. cd ./grub
  23. git checkout "tags/2.02"
  24. cd ..
  25. else
  26. printf "GRUB repository is already present\n"
  27. fi
  28. # me_cleaner
  29. if [ ! -d ./me_cleaner ]; then
  30. printf "Downloading me_cleaner\n"
  31. git clone https://github.com/corna/me_cleaner.git ./me_cleaner
  32. else
  33. printf "me_cleaner repository is already present\n"
  34. fi
  35. }
  36. # Build Coreboot Utilities
  37. function build_utils() {
  38. if [ ! -d "coreboot/.git" ]; then
  39. printf "No Coreboot repository found in coreboot/\nDownload the code first\n"
  40. exit 1
  41. fi
  42. # Build ifdtool for stock BIOS splitting
  43. cd coreboot/util/ifdtool
  44. make -j${nproc}
  45. cd ../../../
  46. # Build cbfstool for managing Coreboot's filesystem
  47. cd coreboot/util/cbfstool
  48. make -j${nproc}
  49. cd ../../../
  50. }
  51. # Split the stock rom and organize the parts
  52. function split_bios() {
  53. if [ ! -f "binaries/bios.bin" ]; then
  54. printf "No stock bios (bios.bin) file found in binaries/\n"
  55. exit 1
  56. fi
  57. if [ ! -f "coreboot/util/ifdtool/ifdtool" ]; then
  58. printf "No ifdtool present, build the Coreboot utils first\n"
  59. exit 1
  60. fi
  61. cd binaries/
  62. ../coreboot/util/ifdtool/ifdtool -x bios.bin
  63. mv flashregion_0_flashdescriptor.bin descriptor.bin
  64. rm flashregion_1_bios.bin
  65. mv flashregion_2_intel_me.bin me.bin
  66. mv flashregion_3_gbe.bin gbe.bin
  67. cd ..
  68. }
  69. # Neuter Intel ME
  70. function neuter_me() {
  71. if [ ! -f "binaries/me.bin" ]; then
  72. printf "No Intel ME (me.bin) binary found in binaries/\n"
  73. exit 1
  74. fi
  75. cp binaries/me.bin binaries/me_neutered.bin
  76. python3 me_cleaner/me_cleaner.py binaries/me_neutered.bin
  77. }
  78. # Prepare Coreboot for compilation
  79. function pre_build_coreboot() {
  80. # Copy the config
  81. cp "config/coreboot.config" coreboot/.config
  82. # Goto the Coreboot directory
  83. cd coreboot
  84. # Build toolchain
  85. make crossgcc-i386 CPUS=${nproc} -b
  86. # Build IASL
  87. make -j${nproc} iasl
  88. # Get back
  89. cd ..
  90. }
  91. # Build Coreboot
  92. function build_coreboot() {
  93. # Goto the coreboot directory
  94. cd coreboot
  95. # Clean last build
  96. make clean
  97. rm -f ../out/coreboot.rom
  98. # Just make
  99. make -j${nproc} || make -j${nproc}
  100. # Exit if failed
  101. if [ $? -ne 0 ]; then
  102. printf "Failed to build Coreboot.\nExiting...\n"
  103. exit 1
  104. fi
  105. # Get back
  106. cd ..
  107. # Copy the resulting binary to a more accessible folder
  108. if [ ! -d out/ ]; then mkdir out/; fi
  109. mv coreboot/build/coreboot.rom out/coreboot.rom
  110. }
  111. # Build GRUB
  112. function build_grub() {
  113. # Check if the GRUB code is present
  114. if [ ! -d "grub/.git" ]; then
  115. printf "No GRUB repository found in grub/\nDownload the code first\n"
  116. exit 1
  117. fi
  118. # Change title just for goofs
  119. sed -i "s/_(\"GNU GRUB version %s\"), PACKAGE_VERSION/\"COREBOOT\"/g" grub/grub-core/normal/main.c
  120. # Copy the config
  121. cp "config/grub.config" "grub/.config"
  122. # Clean last build
  123. cd grub
  124. make clean
  125. cd ..
  126. # Build GRUB
  127. cd grub
  128. ./autogen.sh
  129. ./configure --with-platform=coreboot --disable-werror
  130. make -j${nproc}
  131. cd ..
  132. }
  133. # Assemble the GRUB payload
  134. function assemble_grup() {
  135. printf "Assembling the GRUB payload\n"
  136. # Load modules config
  137. source "config/grub_modules.conf"
  138. # Assemble GRUB
  139. grub/grub-mkstandalone \
  140. --grub-mkimage="grub/grub-mkimage" \
  141. -O i386-coreboot \
  142. -o "out/grub.elf" \
  143. -d "grub/grub-core/" \
  144. --fonts= --themes= --locales= \
  145. --modules="${grub_modules}" \
  146. --install-modules="${grub_install_modules}" \
  147. /boot/grub/grub.cfg="config/grub_memdisk.cfg" \
  148. /dejavusansmono.pf2="misc/dejavusansmono_24bold.pf2" \
  149. /boot/grub/layouts/usqwerty.gkb="misc/usqwerty.gkb"
  150. }
  151. # Configure SeaBIOS to chainload with GRUB
  152. function config_seabios() {
  153. printf "Configure SeaBIOS\n"
  154. if [ ! -f "out/coreboot.rom" ]; then
  155. printf "No Coreboot image found.\nBuild Coreboot first.\n"
  156. exit 1
  157. fi
  158. # Set GRUB as the default boot device
  159. printf "/rom@img/grub2\n" > "out/bootorder"
  160. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add -f "out/bootorder" -n bootorder -t raw
  161. rm -f "out/bootorder"
  162. # Hide SeaBIOS
  163. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add-int -i 0 -n etc/show-boot-menu
  164. # Don't load anything else
  165. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add-int -i 0 -n etc/pci-optionrom-exec
  166. # Print the contents of the CBFS volume
  167. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" print
  168. }
  169. # Install and config GRUB
  170. function install_grub() {
  171. printf "Install GRUB in the CBFS volume\n"
  172. if [ ! -f "out/coreboot.rom" ]; then
  173. printf "No Coreboot image found.\nBuild Coreboot first.\n"
  174. exit 1
  175. fi
  176. # Compress and add GRUB payload
  177. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add-payload -c lzma -f "out/grub.elf" -n img/grub2 && rm "out/grub.elf"
  178. # Add grub.cfg
  179. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" add -f "config/grub.cfg" -n grub.cfg -t raw
  180. # Print the contents of the CBFS volume
  181. coreboot/util/cbfstool/cbfstool "out/coreboot.rom" print
  182. }
  183. # Flashing activities
  184. function flash() {
  185. # Define which programmer to use
  186. case "$3" in
  187. # Internal
  188. "internal" )
  189. programmer="internal:laptop=force_I_want_a_brick"
  190. ;;
  191. # Raspberry Pi
  192. "rpi" )
  193. programmer="linux_spi:dev=/dev/spidev0.0"
  194. ;;
  195. # Arduino Boards with Xu2 USB chips
  196. "u2" )
  197. programmer="serprog:dev=/dev/ttyACM0:115200"
  198. ;;
  199. # Arduino Boards with FTDI USB chips
  200. "ftdi" )
  201. programmer="serprog:dev=/dev/ttyUSB0:2000000"
  202. ;;
  203. # Exit if no programmer is specified
  204. * )
  205. printf "You must specify the programmer\n"
  206. exit 1
  207. ;;
  208. esac
  209. # Write to the flash chip
  210. if [ "$2" == "write" ]; then
  211. # Exit if Coreboot hasn't been successfully compiled yet
  212. if [ ! -f "out/coreboot.rom" ]; then
  213. printf "Build Coreboot first\n"
  214. exit 1
  215. fi
  216. flashrom -p $programmer -w "out/coreboot.rom"
  217. # Do consecutive reads of the flash chip and compare them
  218. elif [ "$2" == "read" ]; then
  219. mkdir binaries/reads
  220. for i in {1..5}; do
  221. flashrom -p $programmer -r "binaries/reads/bios$i.bin"
  222. md5sum "binaries/reads/*.bin"
  223. done
  224. # Check if the flash chip is detected
  225. elif [ "$2" == "check" ]; then
  226. flashrom -p $programmer
  227. fi
  228. }
  229. function clean() {
  230. printf "Clean source and build directories"
  231. rm -rf ./coreboot/
  232. rm -rf ./grub/
  233. rm -rf ./me_cleaner/
  234. }
  235. function doit() {
  236. printf "Download, build and install"
  237. download_code
  238. build_utils
  239. split_bios
  240. neuter_me
  241. pre_build_coreboot
  242. build_coreboot
  243. build_grub
  244. assemble_grub
  245. config_seabios
  246. install_grub
  247. }
  248. # Run operation
  249. if [[ $1 != "" ]]; then
  250. eval "$@"
  251. # Exit if no operation is specified
  252. else
  253. printf "No operation specified\n"
  254. exit 1
  255. fi