grub-helper 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. elif ! [[ -d "$keymap_out_path" ]]; then
  93. printf '\n%s\n' "Error: File $keymap_out_path is not a directory" 1>&2
  94. return 1
  95. fi
  96. "$grub_mklayout" --output="$grub_kbd_layout" --input="$raw_layout_path"
  97. }
  98. grub_build_bootable_image() {
  99. local arch="$(grub_arch "$target" "$@")"
  100. local format="$(grub_format "$target" "$@")"
  101. local prefix="$(grub_prefix "$target" "$@")"
  102. local config_path="$(grub_config_path "$target" "$@")"
  103. local grub_mkimage="$sources_path/grub-mkimage"
  104. local grub_module_dir="$sources_path/grub-core"
  105. local grubimg="$build_path/grub.img"
  106. local grub_bootimg="$grub_module_dir/boot.img"
  107. local grub_bootable_img="$build_path/grub2"
  108. "$grub_mkimage" \
  109. --config="$config_path" \
  110. --directory="$grub_module_dir" \
  111. --output="$grubimg" \
  112. --format="$format" \
  113. --prefix="$prefix" \
  114. cbfs configfile
  115. cat "$grub_bootimg" "$grubimg" > "$grub_bootable_img"
  116. rm -f "$grubimg"
  117. }
  118. grub_build_floppy_image() {
  119. local grubimg="$build_path/grub2"
  120. local tempfile="$build_path/temp.file"
  121. if ! grub_build_bootable_image "$@"; then
  122. printf '\n%s\n\n' "Error: Failed to build a GRUB image" 1>&2
  123. return 1
  124. fi
  125. local size="$(grub_size "$target" "$@")"
  126. # Pre-allocate a floppy-sized image
  127. # SeaBIOS requires floppy images to have a "correct" size
  128. if ! [[ -e "$tempfile" ]]; then
  129. dd if=/dev/zero of="$tempfile" bs=1024 count="${size:-160}"
  130. else
  131. printf '\n%s\n\n' "Error: File $tempfile already exists!" 1>&2
  132. return 1
  133. fi
  134. local -i grubimg_size="$(stat -c %s "$grubimg")"
  135. local -i floppy_size="$((${size:-160} * 1024))"
  136. # Graft the GRUB image onto the blank floppy image
  137. if ((grubimg_size <= floppy_size)); then
  138. dd if="$grubimg" of="$tempfile" bs=1 conv=notrunc
  139. rm -f "$grubimg"
  140. mv "$tempfile" "$grubimg"
  141. else
  142. printf '\n%s' "Error: Image ${grubimg##*/} is too large; " 1>&2
  143. printf '%s\n\n' "it must be less than ${size}KiB in size" 1>&2
  144. return 1
  145. fi
  146. }
  147. grub_build_standalone_image() {
  148. local arch="$(grub_arch "$target" "$@")"
  149. local format="$(grub_format "$target" "$@")"
  150. local prefix="$(grub_prefix "$target" "$@")"
  151. local config_path="$(grub_config_path "$target" "$@")"
  152. local grubimg="$build_path/grub2"
  153. local grub_mkimage="$sources_path/grub-mkimage"
  154. local grub_mkstandalone="$sources_path/grub-mkstandalone"
  155. local grub_module_dir="$sources_path/grub-core"
  156. "$grub_mkstandalone" \
  157. --grub-mkimage="$grub_mkimage" \
  158. --fonts='' \
  159. --themes='' \
  160. --locales='' \
  161. --install-modules='cbfs configfile' \
  162. --directory="$grub_module_dir" \
  163. --format="$format" \
  164. --output="$grubimg" \
  165. /boot/grub/grub.cfg="$config_path"
  166. }