build.sh 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #!/usr/bin/env bash
  2. # use TERM to exit on error
  3. trap "exit 1" TERM
  4. export TOP_PID=$$
  5. # coreboot repo
  6. COREBOOT_REPO_PURISM="https://source.puri.sm/coreboot/coreboot.git"
  7. COREBOOT_REPO_TAG="4.14-Purism-1"
  8. # dependencies
  9. DEPS_BASE=(dmidecode wget sha256sum gzip)
  10. DEPS_FLASH=(git build-essential libpci-dev libudev-dev zlib1g-dev)
  11. DEPS_FLASH_FEDORA=(git gcc g++ make xz bzip2 pciutils-devel)
  12. DEPS_COREBOOT=(gnat m4 bison flex libncurses5-dev python python2)
  13. DEPS_COREBOOT_FEDORA=(gcc-gnat bison flex ncurses-devel zlib-devel python python2 patch)
  14. DEPS_GRUB2=(autoconf automake autopoint libfreetype-dev fonts-unifont)
  15. DEPS_GRUB2_FEDORA=(autoconf automake gettext-devel freetype-devel unifont-fonts)
  16. #local dirs
  17. LOCAL_TOOLS_PATH="./tools"
  18. LOCAL_TEMP_DIR="$(mktemp -d)"
  19. # locally compiled coreboot utils
  20. CBFSTOOL_CB="./coreboot/util/cbfstool/cbfstool"
  21. IFDTOOL_CB="./coreboot/util/ifdtool/ifdtool"
  22. BACKUP_DIR="./backups"
  23. # functions, functions, functions
  24. check_root() {
  25. if [[ "$EUID" != 0 ]]; then
  26. die "This script must be run as root; re-run prefixed with sudo"
  27. fi
  28. }
  29. print_info () {
  30. echo "################################"
  31. echo "Librem 14 coreboot+Grub2 builder "
  32. echo "################################"
  33. }
  34. initial_setup () {
  35. # get model, firmware version, serial # via dmidecode
  36. vendor=$(dmidecode -s bios-vendor)
  37. model=$(dmidecode -s system-product-name | grep -i Librem)
  38. CURRENT_FW_VER=$(dmidecode -s bios-version)
  39. CURRENT_FW_DATE=$(dmidecode -s bios-release-date)
  40. DMIDECODE_SERIAL_NUMBER=$(dmidecode -t 1 | grep "Serial Number" | cut -d' ' -f 3-)
  41. # set CURRENT_FW_TYPE type
  42. if [ "$vendor" = "coreboot" ]; then
  43. case ${CURRENT_FW_VER^^} in
  44. *"PUREBOOT"* )
  45. CURRENT_FW_TYPE="HEADS";;
  46. *"HEADS"* )
  47. CURRENT_FW_TYPE="HEADS";;
  48. *"GRUB2"* )
  49. CURRENT_FW_TYPE="GRUB2";;
  50. * )
  51. CURRENT_FW_TYPE="STANDARD";;
  52. esac
  53. else
  54. CURRENT_FW_TYPE="UNKNOWN"
  55. fi
  56. FW_UPDATE=""
  57. case $CURRENT_FW_TYPE in
  58. "HEADS" )
  59. FW_TYPE_STRING="PureBoot (coreboot+Heads)"
  60. CURR_FW_NUM=`echo ${CURRENT_FW_VER} | sed -e "s/^PureBoot-beta-//" -e "s/^PureBoot-Release-//"`
  61. UPD_FW_NUM=`echo ${COREBOOT_HEADS_VERSION} | sed -e "s/^PureBoot-beta-//" -e "s/^PureBoot-Release-//"`
  62. [[ "$UPD_FW_NUM" > "$CURR_FW_NUM" ]] \
  63. && FW_UPDATE=`echo ${COREBOOT_HEADS_VERSION} | tr '-' ' '`
  64. CURRENT_FW_VER=`echo ${CURRENT_FW_VER#PureBoot-} | tr '-' ' '`
  65. CURRENT_FW_DATE=""
  66. ;;
  67. "STANDARD" )
  68. FW_TYPE_STRING="Standard (coreboot+SeaBIOS)"
  69. [[ "$COREBOOT_SEABIOS_VERSION" > "$CURRENT_FW_VER" ]] \
  70. && FW_UPDATE="$COREBOOT_SEABIOS_VERSION";
  71. CURRENT_FW_DATE="(${CURRENT_FW_DATE})"
  72. ;;
  73. "GRUB2" )
  74. FW_TYPE_STRING="Grub2 (coreboot+Grub2)"
  75. [[ "$COREBOOT_GRUB2_VERSION" > "$CURRENT_FW_VER" ]] \
  76. && FW_UPDATE="$COREBOOT_GRUB2_VERSION";
  77. CURRENT_FW_DATE="(${CURRENT_FW_DATE})"
  78. ;;
  79. * )
  80. FW_TYPE_STRING="Unknown" ;;
  81. esac
  82. # set LIBREM_MODEL, MODEL_INDEX
  83. # strip leading 'librem' + space/underscore
  84. model=`echo ${model,,} | sed -e "s/^librem//" -e "s/^ //" -e "s/^_//"`
  85. case ${model} in
  86. "14")
  87. LIBREM_MODEL="14";
  88. PLATFORM="CML";
  89. MODEL_INDEX="9";;
  90. *)
  91. LIBREM_MODEL="unknown";
  92. PLATFORM="UNK";
  93. MODEL_INDEX="0";;
  94. esac
  95. }
  96. cleanup() {
  97. # ensure all files owned by calling/non-root user
  98. chown -R -f $(logname). .
  99. }
  100. die () {
  101. local msg=$1
  102. if [ ! -z "$msg" ]; then
  103. echo ""
  104. echo -e "$msg"
  105. echo ""
  106. fi
  107. kill -s TERM $TOP_PID
  108. cleanup
  109. exit 1
  110. }
  111. get_flashrom () {
  112. # check if flashrom exists on system
  113. echo ""
  114. echo "Checking for usable version of flashrom"
  115. FLASHROM_CMD=$(which flashrom)
  116. if [[ ! -z "$FLASHROM_CMD" && "$PLATFORM" != "CML" ]]; then
  117. #check version
  118. version=$($FLASHROM_CMD -R | awk -F" " '{print $2;exit}' | grep "1.")
  119. if [[ "$version" > "v1.1" ]]; then
  120. # verify build supports logging (some PureOS/Debian versions broken)
  121. if $FLASHROM_CMD -L -o /tmp/flashrom.log >/dev/null 2>&1 ; then
  122. #use system flashrom
  123. echo "Found built-in flashrom version $version"
  124. return
  125. fi
  126. fi
  127. fi
  128. # no system flashrom, not version 1.2+, or PLATFORM == CML:
  129. # build from upstream flashrom source
  130. # check deps first
  131. check_dependencies "flashrom"
  132. if [ ! -d ${LOCAL_TOOLS_PATH}/flashrom ]; then
  133. (
  134. mkdir -p ${LOCAL_TOOLS_PATH}
  135. cd ${LOCAL_TOOLS_PATH}
  136. echo -n "Cloning flashrom git repo..."
  137. git clone --single-branch --branch master https://review.coreboot.org/flashrom.git >/dev/null
  138. [ $? -ne 0 ] && die "Error cloning flashrom repo from git"
  139. echo "done"
  140. )
  141. fi
  142. (
  143. echo -n "Building flashrom from source..."
  144. cd ${LOCAL_TOOLS_PATH}/flashrom
  145. git fetch origin >/dev/null 2>&1
  146. if ! git show c64486b >/dev/null 2>&1; then
  147. cd ..
  148. rm -rf ./flashrom
  149. git clone --single-branch --branch master https://review.coreboot.org/flashrom.git >/dev/null
  150. [ $? -ne 0 ] && die "Error cloning flashrom repo from git"
  151. cd flashrom
  152. fi
  153. git reset --hard c64486b >/dev/null 2>&1
  154. [ $? -ne 0 ] && die "Error checking out flashrom via git"
  155. make CONFIG_NOTHING=yes CONFIG_DUMMY=yes CONFIG_INTERNAL=yes WARNERROR=no
  156. [ $? -ne 0 ] && die "Error building flashrom (missing build dependency libpci-dev?)"
  157. echo "done"
  158. )
  159. FLASHROM_CMD="${LOCAL_TOOLS_PATH}/flashrom/flashrom"
  160. }
  161. get_serial () {
  162. if [ "$PLATFORM" = "BDW" ]; then
  163. #no serial to set, clear file if exists
  164. rm -f ${LOCAL_TEMP_DIR}/serial_number.txt >/dev/null
  165. return;
  166. fi
  167. #prompt user for serial # selection/entry
  168. echo ""
  169. echo "Set the device serial number:"
  170. echo ""
  171. if [ "$DMIDECODE_SERIAL_NUMBER" != "" ]; then
  172. DMIDECODE_SERIAL_OPT=1
  173. MANUAL_SERIAL_OPT=2
  174. NO_SERIAL_OPT=3
  175. echo "${DMIDECODE_SERIAL_OPT} - Extracted from your local system (${DMIDECODE_SERIAL_NUMBER})"
  176. else
  177. DMIDECODE_SERIAL_OPT=0
  178. MANUAL_SERIAL_OPT=1
  179. NO_SERIAL_OPT=2
  180. fi
  181. echo "${MANUAL_SERIAL_OPT} - Enter serial number manually"
  182. echo "${NO_SERIAL_OPT} - Do not set a serial number"
  183. echo ""
  184. serial=0
  185. [ ${MODEL_INDEX} = 0 ] && default=2 || default=1
  186. while [ "$serial" == "0" ]; do
  187. read -r -p "Enter your choice (default: $default): " serial
  188. [ "$serial" = "" ] && serial=$default
  189. case $serial in
  190. ${DMIDECODE_SERIAL_OPT} )
  191. if [ "$DMIDECODE_SERIAL_OPT" = 1 ]; then
  192. echo -n "$DMIDECODE_SERIAL_NUMBER" > ${LOCAL_TEMP_DIR}/serial_number.txt
  193. else
  194. echo "Invalid option"
  195. serial=0
  196. fi
  197. ;;
  198. ${MANUAL_SERIAL_OPT} )
  199. read -r -p "Enter the machine's serial number : " serial_number
  200. echo -n "$serial_number" > ${LOCAL_TEMP_DIR}/serial_number.txt
  201. ;;
  202. ${NO_SERIAL_OPT} )
  203. echo -n "" > ${LOCAL_TEMP_DIR}/serial_number.txt
  204. ;;
  205. * )
  206. echo "Invalid option"
  207. serial=0
  208. ;;
  209. esac
  210. done
  211. echo ""
  212. }
  213. flashrom_progress() {
  214. local current=0
  215. local total_bytes=0
  216. local percent=0
  217. local IN=''
  218. local spin='-\|/'
  219. local spin_idx=0
  220. local progressbar=''
  221. local progressbar2=''
  222. local status='init'
  223. local prev_word=''
  224. local prev_prev_word=''
  225. progressbar2=$(for ((i=0; i < 49; i++)) do echo -ne ' ' ; done)
  226. echo "Initializing internal Flash Programmer"
  227. while true ; do
  228. prev_prev_word=$prev_word
  229. prev_word=$IN
  230. read -r -d' ' IN || break
  231. if [ "$total_bytes" != "0" ]; then
  232. current=$(echo "$IN" | grep -E -o '0x[0-9a-f]+-0x[0-9a-f]+:.*' | grep -E -o "0x[0-9a-f]+" | tail -n 1)
  233. if [ "${current}" != "" ]; then
  234. percent=$((100 * (current + 1) / total_bytes))
  235. progressbar=$(for ((i=0; i < $((percent / 2)); i++)) do echo -ne '#' ; done)
  236. progressbar2=$(for ((i=0; i < $((49 - percent / 2)); i++)) do echo -ne ' ' ; done)
  237. fi
  238. else
  239. if [ "$prev_prev_word" == "Reading" ] && [ "$IN" == "bytes" ]; then
  240. total_bytes=$prev_word
  241. echo "Total flash size : $total_bytes bytes"
  242. fi
  243. fi
  244. if [ "$percent" -eq 100 ]; then
  245. spin_idx=4
  246. else
  247. spin_idx=$(( (spin_idx+1) %4 ))
  248. fi
  249. if [ "$status" == "init" ]; then
  250. if [ "$IN" == "contents..." ]; then
  251. status="reading"
  252. echo "Reading old flash contents. Please wait..."
  253. fi
  254. fi
  255. if [ "$status" == "reading" ]; then
  256. if echo "${IN}" | grep "done." > /dev/null ; then
  257. status="writing"
  258. fi
  259. fi
  260. if [ "$status" == "writing" ]; then
  261. echo -ne "Flashing: [${progressbar}${spin:$spin_idx:1}${progressbar2}] (${percent}%)\\r"
  262. if echo "$IN" | grep "Verifying" > /dev/null ; then
  263. status="verifying"
  264. echo ""
  265. echo "Verifying flash contents. Please wait..."
  266. fi
  267. if echo "$IN" | grep "identical" > /dev/null ; then
  268. status="done"
  269. echo ""
  270. echo "The flash contents are identical to the image being flashed."
  271. fi
  272. fi
  273. if [ "$status" == "verifying" ]; then
  274. if echo "${IN}" | grep "VERIFIED." > /dev/null ; then
  275. status="done"
  276. echo "The flash contents were verified and the image was flashed correctly."
  277. fi
  278. fi
  279. done
  280. echo ""
  281. if [ "$status" == "done" ]; then
  282. return 0
  283. else
  284. echo 'Error flashing coreboot -- see timestampped flashrom log in current directory for more info'
  285. echo ""
  286. return 1
  287. fi
  288. }
  289. set_serial_number () {
  290. # pass in full path of file in which to inject serial
  291. [[ -z "$1" || ! -f "$1" ]] && die "Error: a valid firmware filename is required"
  292. # check if we have a serial # to add to cbfs
  293. if [ -f ${LOCAL_TEMP_DIR}/serial_number.txt ]; then
  294. # get cbfstool if needed
  295. get_cbfstool
  296. # inject serial into coreboot image
  297. echo "Injecting serial number into firmware image"
  298. $CBFSTOOL_BIN "$1" remove -n serial_number -r COREBOOT >/dev/null 2>&1
  299. $CBFSTOOL_BIN "$1" add -n serial_number -t raw -f resources/serial_number.txt -r COREBOOT >/dev/null 2>&1
  300. [ $? -ne 0 ] && die "Error adding serial number to file ${1}"
  301. fi
  302. }
  303. backup_firmware () {
  304. # assume FLASHROM_CMD exists/is set already, LOCAL_TEMP_DIR set up
  305. mkdir -p ${BACKUP_DIR}
  306. DATE_FIX=$(date '+%Y%m%d-%H%M%S')
  307. $FLASHROM_CMD -p internal:ich_spi_mode=hwseq -r ${BACKUP_DIR}/backup-${DATE_FIX}.bin -o ${BACKUP_DIR}/backup-$(date '+%Y%m%d-%H%M%S').log >/dev/null 2>&1
  308. if [ $? -ne 0 ]; then
  309. die "Error reading current firmware; see flashrom log for more info."
  310. fi
  311. }
  312. flash_firmware_internal () {
  313. # assume FLASHROM_CMD exists/is set already
  314. # pass in full path of file to be flashed
  315. [[ -z "$1" || ! -f "$1" ]] && die "Error: a valid filename to be flashed is required"
  316. $FLASHROM_CMD -p internal:ich_spi_mode=hwseq -w "$1" -V -o "./flashrom-$(date '+%Y%m%d-%H%M%S').log" 2>&1 | flashrom_progress
  317. return $?
  318. }
  319. update_serial_number () {
  320. # ensure using SeaBIOS
  321. if [[ "${CURRENT_FW_TYPE}" != "STANDARD" ]]; then
  322. echo "This feature is only valid for use with the standard/SeaBIOS firmware"
  323. echo ""
  324. return
  325. fi
  326. # show serial menu / create serial.txt
  327. get_serial
  328. # check for / get flashrom
  329. get_flashrom
  330. # read current firmware
  331. echo ""
  332. if [ ! -f ${LOCAL_TEMP_DIR}/${CURRENT_FW_BIN} ]; then
  333. echo "Reading current firmware..."
  334. read_current_firmware
  335. else
  336. echo "Current firmware already read from flash"
  337. fi
  338. # inject into current firmware
  339. set_serial_number "${LOCAL_TEMP_DIR}/${CURRENT_FW_BIN}"
  340. # prompt to update
  341. echo ""
  342. flash=0
  343. while [ "$flash" != "y" ] && [ "$flash" != "n" ]; do
  344. read -r -p "Do you want to update the serial number now (Y/n) ? " flash
  345. [[ "$flash" == "N" ]] && flash="n"
  346. [[ "$flash" = "" || "$flash" == "Y" ]] && flash="y"
  347. done
  348. if [ "$flash" == "y" ]; then
  349. echo ""
  350. echo "coreboot flashing in progress. Do NOT interrupt this process."
  351. echo ""
  352. flash_firmware_internal "${LOCAL_TEMP_DIR}/${CURRENT_FW_BIN}"
  353. if [ $? -eq 0 ]; then
  354. echo "Serial number successfully updated; change will take effect on next boot"
  355. echo ""
  356. fi
  357. fi
  358. }
  359. update_crossgcc_toolchain() {
  360. # assume called from coreboot root dir
  361. local CURRENT_TOOLCHAIN_VERSION=0
  362. local GCC_FILE='util/crossgcc/xgcc/bin/i386-elf-gcc'
  363. local TARGET_TOOLCHAIN_VERSION="$(git log -n 1 --pretty=%h util/crossgcc)"
  364. if [ -f "${GCC_FILE}" ]; then
  365. CURRENT_TOOLCHAIN_VERSION=$(${GCC_FILE} --version | grep -m 1 'coreboot toolchain' \
  366. | cut -f2 -d'v' | cut -f2 -d'_' | cut -f1 -d')')
  367. fi
  368. if [ "${CURRENT_TOOLCHAIN_VERSION}" != "${TARGET_TOOLCHAIN_VERSION}" ]; then
  369. echo "coreboot toolchain version changed from ${CURRENT_TOOLCHAIN_VERSION} to ${TARGET_TOOLCHAIN_VERSION}"
  370. echo "Cleaning crossgcc compiler before rebuilding it"
  371. make crossgcc-clean
  372. retries=0
  373. until [ "$retries" -ge 5 ]
  374. do
  375. make crossgcc-i386 CPUS=$(nproc) && break
  376. retries=$((retries+1))
  377. done
  378. [ $? -ne 0 ] && die "Error building coreboot toolchain" || true
  379. fi
  380. }
  381. build_coreboot() {
  382. # extract / set device serial # to be injected later
  383. get_serial
  384. echo ""
  385. echo "Checking out/updating coreboot repo..."
  386. echo ""
  387. # check dir, clone if needed
  388. if [ ! -d coreboot ]; then
  389. echo ""
  390. git clone --branch ${COREBOOT_REPO_TAG} ${COREBOOT_REPO_PURISM} coreboot
  391. [ $? -ne 0 ] && die "Error cloning coreboot git repo"
  392. (
  393. cd coreboot
  394. # It can fail if you don't have a git global user.name/user.email setup
  395. make gitconfig 2>/dev/null || true
  396. # init/update submodules
  397. git submodule update --init --force --checkout >/dev/null 2>&1
  398. [ $? -ne 0 ] && die "Error checking out/updating coreboot submodules"
  399. )
  400. fi
  401. # check out correct branch
  402. (
  403. cd coreboot
  404. git fetch 2>/dev/null
  405. [ $? -ne 0 ] && die "Error fetching coreboot git repo"
  406. git fetch --tags 2>/dev/null
  407. [ $? -ne 0 ] && die "Error fetching coreboot git repo"
  408. git checkout --detach ${COREBOOT_REPO_TAG} 2>/dev/null
  409. [ $? -ne 0 ] && die "Error checking out coreboot git repo"
  410. # ensure submodules sane
  411. if [[ "`git diff 3rdparty`" != "" ]]; then
  412. git submodule update --force --checkout >/dev/null 2>&1
  413. [[ "`git diff 3rdparty`" != "" ]] && \
  414. die "submodules have been modified; build would not be reproducible"
  415. fi
  416. #build cbfstool and ifdtool
  417. (
  418. cd util/cbfstool
  419. make
  420. [ $? -ne 0 ] && die "Error building cbfstool"
  421. )
  422. (
  423. cd util/ifdtool
  424. make
  425. [ $? -ne 0 ] && die "Error building ifdtool"
  426. )
  427. )
  428. # build coreboot
  429. (
  430. # set pwd
  431. cd coreboot
  432. # let user know this will take a few
  433. echo -e "\n\n"
  434. echo "Ready to build coreboot - this will take some time depending on your connection"
  435. echo "speed and CPU/RAM, esp if the toolchain needs to be built."
  436. echo ""
  437. # check/build toolchain
  438. update_crossgcc_toolchain || die
  439. # get git version
  440. GIT_VERSION=$(git describe --tags --dirty)
  441. # do a clean build
  442. make distclean
  443. # copy config
  444. cp ../resources/config .config
  445. # copt grub config
  446. cp ../resources/grub.cfg grub.cfg
  447. echo "CONFIG_LOCALVERSION=\"${GIT_VERSION}\"" >> .config
  448. make olddefconfig >/dev/null
  449. # build coreboot and payload(s)
  450. make
  451. [ $? -ne 0 ] && die "Error building coreboot"
  452. # copy to root dir
  453. #cp build/coreboot.rom ../${COREBOOT_IMAGE}
  454. )
  455. # calculate hash of BIOS region before injecting bootorder/serial
  456. ${IFDTOOL_CB} -x ${COREBOOT_IMAGE}
  457. # set serial
  458. set_serial_number ${COREBOOT_IMAGE}
  459. # print CBFS contents
  460. ${CBFSTOOL_CB} ${COREBOOT_IMAGE} print
  461. echo ""
  462. echo ""
  463. echo "Finished building coreboot for Librem ${LIBREM_MODEL^}"
  464. echo ""
  465. # prompt to flash
  466. echo ""
  467. flash=0
  468. while [ "$flash" != "y" ] && [ "$flash" != "n" ]; do
  469. read -r -p "Do you want to flash the coreboot update now (y/N) ? " flash
  470. if [ "$flash" = "" ] || [ "$flash" == "N" ]; then
  471. flash="n"
  472. fi
  473. if [ "$flash" == "Y" ]; then
  474. flash="y"
  475. fi
  476. done
  477. if [ "$flash" == "y" ]; then
  478. # check for / get flashrom
  479. get_flashrom
  480. echo ""
  481. echo "coreboot flashing in progress. Do NOT interrupt this process."
  482. echo ""
  483. flash_firmware_internal ${COREBOOT_IMAGE}
  484. if [ $? -eq 0 ]; then
  485. echo ""
  486. echo "You must reboot for the coreboot update to take effect."
  487. echo ""
  488. read -r -p "Reboot now? (y/N) ? " rb
  489. if [ "$rb" = "Y" ] || [ "$rb" == "y" ]; then
  490. cleanup
  491. reboot
  492. fi
  493. fi
  494. else
  495. echo ""
  496. fi
  497. }
  498. check_dependencies() {
  499. local dep_type=$1
  500. local DEPS
  501. local use_dnf=false
  502. [[ `which dnf 2>/dev/null` ]] && use_dnf=true
  503. case $dep_type in
  504. "base")
  505. DEPS=${DEPS_BASE[@]} ;;
  506. "flashrom")
  507. if [ $use_dnf = true ]; then
  508. DEPS=(${DEPS_BASE[@]} ${DEPS_FLASH_FEDORA[@]}) ;
  509. else
  510. DEPS=(${DEPS_BASE[@]} ${DEPS_FLASH[@]}) ;
  511. fi
  512. ;;
  513. "coreboot")
  514. if [ $use_dnf = true ]; then
  515. DEPS=(${DEPS_BASE[@]} ${DEPS_FLASH_FEDORA[@]} ${DEPS_GRUB2_FEDORA[@]}) ;
  516. else
  517. DEPS=(${DEPS_BASE[@]} ${DEPS_FLASH[@]} ${DEPS_COREBOOT[@]} ${DEPS_GRUB2[@]}) ;
  518. fi
  519. ;;
  520. *)
  521. ;;
  522. esac
  523. local pkg needed=()
  524. for pkg in "${DEPS[@]}"; do
  525. if [[ ! `which ${pkg} 2>/dev/null` \
  526. && ! `dnf list installed 2>/dev/null | grep "^${pkg}"` \
  527. && ! `apt list --installed 2>/dev/null | grep "^${pkg}"` \
  528. && ! -f "/usr/share/doc/${pkg}/copyright" ]]; then
  529. needed+=("${pkg}")
  530. fi
  531. done
  532. if [[ "${#needed[@]}" -gt 0 ]]; then
  533. echo ""
  534. echo "One or more required dependencies are missing: ${needed[@]}"
  535. echo "The script will now attempt to install them for you"
  536. echo ""
  537. if [ $use_dnf = true ]; then
  538. if ! sudo dnf -y install "${needed[@]}" ; then
  539. die "Some required dependencies could not be installed automatically"
  540. fi
  541. elif which apt >/dev/null ; then
  542. sudo apt-get update >/dev/null 2>&1
  543. if ! sudo apt-get -y install "${needed[@]}" ; then
  544. die "Some required dependencies could not be installed automatically"
  545. fi
  546. else
  547. echo "The script was unable to install the required dependencies automatically"
  548. echo ""
  549. die "Please ensure all required dependencies are installed and re-run this script"
  550. fi
  551. fi
  552. }
  553. # Start of main script
  554. # let's do stuff
  555. check_root
  556. check_dependencies "base"
  557. check_dependencies "flashrom"
  558. check_dependencies "coreboot"
  559. initial_setup
  560. print_info
  561. get_flashrom
  562. build_coreboot