inject 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env bash
  2. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  3. # SPDX-License-Identifier: GPL-3.0-only
  4. Error_out(){
  5. if [ ! -z ${@+x} ]; then
  6. printf "ERROR: ${@}\n"
  7. fi
  8. cat <<- EOF
  9. USAGE: ./blobutil inject -r [/path/to/rom] -b [boardname] -m [macaddress]
  10. Example: ./blobutil inject -r x230_12mb.rom -b x230_12mb
  11. Adding a macadress to the gbe is optional.
  12. If the [-m] parameter is left blank, the gbe will not be touched.
  13. Type './blobutil inject listboards' to get a list of valid boards
  14. EOF
  15. exit 1
  16. }
  17. Modify_gbe(){
  18. printf "changing mac address in gbe to ${new_mac}\n"
  19. _gbe_location=${CONFIG_GBE_BIN_PATH#../../}
  20. if [ ! -d nvmutils/ ]; then
  21. git clone https://notabug.org/osboot/nvmutils
  22. if [ ! -d nvmutils/ ]; then
  23. printf "E: could not download nvmutils"
  24. exit 1
  25. fi
  26. (
  27. cd nvmutils/
  28. git reset --hard ba9f5ada6a05d7ef8af45e30b700cd627a055867
  29. )
  30. fi
  31. if [ ! -f nvmutils/nvmmac ]; then
  32. ( cd nvmutils/ && make )
  33. fi
  34. _gbe_tmp=$(mktemp -t gbeXXXX.bin)
  35. cp ${_gbe_location} ${_gbe_tmp}
  36. ./nvmutils/nvmmac ${_gbe_tmp} ${new_mac} || Error_out 'failed to modify mac address\nmake sure the mac address in the correct format'
  37. ./coreboot/default/util/ifdtool/ifdtool -i GbE:${_gbe_tmp} ${rom} -O ${rom} || exit 1
  38. rm ${_gbe_tmp}
  39. }
  40. listboards() {
  41. for boarddir in resources/coreboot/*; do
  42. if [ ! -d "${boarddir}" ]; then continue; fi
  43. board="${boarddir##resources/coreboot/}"
  44. board="${board%/}"
  45. printf '%s\n' "${board##*/}"
  46. done
  47. }
  48. # This function tries to determine the board from the filename of the rom.
  49. # It will only succeed if the filename is not changed from the build/download
  50. Detect_board(){
  51. filename=$(basename ${rom})
  52. case ${filename} in
  53. grub_*)
  54. board=$(cut -d '_' -f2-3 <<<${filename})
  55. ;;
  56. seabios_grubfirst_*|seabios_withgrub_*)
  57. board=$(cut -d '_' -f3-4 <<<${filename})
  58. ;;
  59. *)
  60. return 1
  61. esac
  62. if [ -d "resources/coreboot/${board}/" ]; then
  63. printf '%s\n' "${board}"
  64. else
  65. return 1
  66. fi
  67. }
  68. Patch(){
  69. set -- "resources/coreboot/${board}/config/*"
  70. . ${1} 2>/dev/null
  71. . "resources/coreboot/${board}/board.cfg"
  72. if [ "$CONFIG_HAVE_MRC" = "y" ]; then
  73. printf 'adding mrc\n'
  74. ./coreboot/default/util/cbfstool/cbfstool ${rom} add -f mrc/haswell/mrc.bin -n mrc.bin -t mrc || exit 1
  75. fi
  76. if [ "${CONFIG_HAVE_ME_BIN}" = "y" ]; then
  77. _me_location=${CONFIG_ME_BIN_PATH#../../}
  78. printf 'adding intel management engine\n'
  79. ./coreboot/default/util/ifdtool/ifdtool -i me:${_me_location} ${rom} -O ${rom} || exit 1
  80. fi
  81. if [ "${modifygbe}" = "true" ]; then
  82. Modify_gbe
  83. fi
  84. }
  85. if [ "${1}" = "listboards" ]; then
  86. listboards
  87. exit 0
  88. fi
  89. # Implementing parameter parsing now so more options can be added later
  90. while getopts r:b:m: option
  91. do
  92. case "${option}"
  93. in
  94. r)rom=${OPTARG};;
  95. b)board=${OPTARG};;
  96. m)
  97. modifygbe=true
  98. new_mac=${OPTARG}
  99. ;;
  100. esac
  101. done
  102. if [ -z ${rom+x} ]; then
  103. Error_out 'no rom specified'
  104. elif [ ! -f "${rom}" ]; then
  105. Error_out "${rom} is not a valid path"
  106. elif [ -z ${board+x} ]; then
  107. board=$(Detect_board) || \
  108. Error_out 'no board specified'
  109. fi
  110. if [ ! -d "resources/coreboot/${board}/" ]; then
  111. printf "board ${board} not found\n"
  112. Error_out
  113. fi
  114. if [ ! -d coreboot/default ]; then
  115. printf "downloading coreboot\n"
  116. ./download coreboot default
  117. fi
  118. if [ ! -f "coreboot/default/util/ifdtool/ifdtool" ]; then
  119. printf "building ifdtool from coreboot\n"
  120. ( cd coreboot/default/util/ifdtool && make )
  121. fi
  122. if [ ! -f "coreboot/default/util/cbfstool/cbfstool" ]; then
  123. printf "building cbfstool from coreboot\n"
  124. ( cd coreboot/default/util/cbfstool && make )
  125. fi
  126. ./blobutil download ${board} && Patch