cb-helper 6.7 KB

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