grub-helper 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/bin/env bash
  2. # Copyright (C) 2017 Andrew Robbins <contact@andrewrobbins.info>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ARCH='arch'
  17. CONFIG='config'
  18. FONTS='fonts'
  19. FONT_FILE='font-file'
  20. FONT_PROJECT='font-project'
  21. FORMAT='format'
  22. MODMIN='modules-minimal'
  23. PLATFORM='platform'
  24. PREFIX='prefix'
  25. SIZE='size'
  26. grub_arch() {
  27. project_file_contents "${project}" "${CONFIGS}" "${ARCH}" "$@"
  28. }
  29. grub_font_file() {
  30. project_file_contents "${project}" "${CONFIGS}" "${FONT_FILE}" "$@"
  31. }
  32. grub_font_project() {
  33. project_file_contents "${project}" "${CONFIGS}" "${FONT_PROJECT}" "$@"
  34. }
  35. grub_format() {
  36. project_file_contents "${project}" "${CONFIGS}" "${FORMAT}" "$@"
  37. }
  38. grub_platform() {
  39. project_file_contents "${project}" "${CONFIGS}" "${PLATFORM}" "$@"
  40. }
  41. grub_prefix() {
  42. project_file_contents "${project}" "${CONFIGS}" "${PREFIX}" "$@"
  43. }
  44. grub_size() {
  45. project_file_contents "${project}" "${CONFIGS}" "${SIZE}" "$@"
  46. }
  47. grub_config_path() {
  48. project_file_path "${project}" "${CONFIGS}" "${CONFIG}" "$@"
  49. }
  50. grub_modmin_path() {
  51. project_file_path "${project}" "${CONFIGS}" "${MODMIN}" "$@"
  52. }
  53. grub_copy_modules() {
  54. local grub_module_dir="${sources_path}/grub-core"
  55. local keep_dir="${build_path}/$(grub_format "${target}" "$@")"
  56. mkdir -p "${keep_dir}"
  57. cp -a "${grub_module_dir}"/*.@(mod|lst) "${keep_dir}"
  58. }
  59. grub_build_font() {
  60. # Font project-specific filenames and paths
  61. local font_file="$(grub_font_file "${FONTS}" "$@")"
  62. local font_project="$(grub_font_project "${FONTS}" "$@")"
  63. local font_build_dir="${root}/${BUILD}/${font_project}"
  64. local grub_mkfont="${sources_path}/grub-mkfont"
  65. # GRUB font directory for outputting the built PF2 file
  66. mkdir -p "${build_path}/${FONTS}"
  67. "${grub_mkfont}" --output="${build_path}/${FONTS}/${font_file%.*}.pf2" \
  68. "${font_build_dir}/${font_file}"
  69. }
  70. grub_build_utils() {
  71. (
  72. # If arch and/or platform files don't exist,
  73. # the configure script will pick a reasonable default
  74. local arch="$(grub_arch "${target}" "$@")"
  75. local platform="$(grub_platform "${target}" "$@")"
  76. cd "${sources_path}" || return
  77. if git_project_check "${repository}"; then
  78. ./autogen.sh
  79. fi
  80. ./configure --target="${arch}" --with-platform="${platform}"
  81. make -j"${TASKS}"
  82. )
  83. }
  84. grub_build_layout() {
  85. local raw_layout="${1##*/}"
  86. local raw_layout_path="$1"
  87. local keymap_out_path="${build_path}/keymaps"
  88. local grub_mklayout="${sources_path}/grub-mklayout"
  89. local grub_kbd_layout="${keymap_out_path}/${raw_layout}.gkb"
  90. if ! [[ -e "${keymap_out_path}" ]]; then
  91. mkdir -p "${keymap_out_path}"
  92. fi
  93. "${grub_mklayout}" --output="${grub_kbd_layout}" --input="${raw_layout_path}"
  94. }
  95. grub_build_bootable_image() {
  96. local arch="$(grub_arch "${target}" "$@")"
  97. local format="$(grub_format "${target}" "$@")"
  98. local prefix="$(grub_prefix "${target}" "$@")"
  99. local config_path="$(grub_config_path "${target}" "$@")"
  100. local grub_mkimage="${sources_path}/grub-mkimage"
  101. local grub_module_dir="${sources_path}/grub-core"
  102. local grubimg="${build_path}/grub.img"
  103. local grub_bootimg="${grub_module_dir}/boot.img"
  104. local grub_bootable_img="${build_path}/grub2"
  105. "${grub_mkimage}" \
  106. --config="${config_path}" \
  107. --directory="${grub_module_dir}" \
  108. --output="${grubimg}" \
  109. --format="${format}" \
  110. --prefix="${prefix}" \
  111. cbfs configfile
  112. cat "${grub_bootimg}" "${grubimg}" > "${grub_bootable_img}"
  113. rm -f "${grubimg}"
  114. }
  115. grub_build_floppy_image() {
  116. local grubimg="${build_path}/grub2"
  117. local tempfile="${build_path}/temp.file"
  118. if ! ( grub_build_bootable_image "$@" ); then
  119. printf '\n%s\n\n' "Error: Failed to build a GRUB image" 1>&2
  120. return 1
  121. fi
  122. local size="$(grub_size "${target}" "$@")"
  123. # Pre-allocate a floppy-sized image
  124. # SeaBIOS requires floppy images to have a "correct" size
  125. if ! [[ -e "${tempfile}" ]]; then
  126. dd if=/dev/zero of="${tempfile}" bs=1024 count="${size:-160}"
  127. else
  128. printf '\n%s\n\n' "Error: File ${tempfile} already exists!" 1>&2
  129. return 1
  130. fi
  131. local -i grubimg_size="$(stat -c %s "${grubimg}")"
  132. local -i floppy_size="$((${size:-160} * 1024))"
  133. # Graft the GRUB image onto the blank floppy image
  134. if ((grubimg_size <= floppy_size)); then
  135. dd if="${grubimg}" of="${tempfile}" bs=1 conv=notrunc
  136. rm -f "${grubimg}"
  137. mv "${tempfile}" "${grubimg}"
  138. else
  139. printf '\n%s' "Error: Image ${grubimg##*/} is too large; " 1>&2
  140. printf '%s\n\n' "it must be less than ${size}KiB in size." 1>&2
  141. return 1
  142. fi
  143. }
  144. grub_build_standalone_image() {
  145. local arch="$(grub_arch "${target}" "$@")"
  146. local format="$(grub_format "${target}" "$@")"
  147. local prefix="$(grub_prefix "${target}" "$@")"
  148. local config_path="$(grub_config_path "${target}" "$@")"
  149. local grubimg="${build_path}/grub2"
  150. local grub_mkimage="${sources_path}/grub-mkimage"
  151. local grub_mkstandalone="${sources_path}/grub-mkstandalone"
  152. local grub_module_dir="${sources_path}/grub-core"
  153. "${grub_mkstandalone}" \
  154. --grub-mkimage="${grub_mkimage}" \
  155. --fonts='' \
  156. --themes='' \
  157. --locales='' \
  158. --install-modules='cbfs configfile' \
  159. --directory="${grub_module_dir}" \
  160. --format="${format}" \
  161. --output="${grubimg}" \
  162. /boot/grub/grub.cfg="${config_path}"
  163. }