grub-helper 5.7 KB

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