grub-helper 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. FORMAT='format'
  19. MODMIN='modules-minimal'
  20. PLATFORM='platform'
  21. PREFIX='prefix'
  22. SIZE='size'
  23. grub_arch() {
  24. project_file_contents "${project}" "${CONFIGS}" "${ARCH}" "$@"
  25. }
  26. grub_format() {
  27. project_file_contents "${project}" "${CONFIGS}" "${FORMAT}" "$@"
  28. }
  29. grub_platform() {
  30. project_file_contents "${project}" "${CONFIGS}" "${PLATFORM}" "$@"
  31. }
  32. grub_prefix() {
  33. project_file_contents "${project}" "${CONFIGS}" "${PREFIX}" "$@"
  34. }
  35. grub_size() {
  36. project_file_contents "${project}" "${CONFIGS}" "${SIZE}" "$@"
  37. }
  38. grub_config_path() {
  39. project_file_path "${project}" "${CONFIGS}" "${CONFIG}" "$@"
  40. }
  41. grub_modmin_path() {
  42. project_file_path "${project}" "${CONFIGS}" "${MODMIN}" "$@"
  43. }
  44. grub_copy_modules() {
  45. local grub_module_dir="${sources_path}/grub-core"
  46. local keep_dir="${build_path}/$(grub_format "${target}" "$@")"
  47. mkdir -p "${keep_dir}"
  48. cp -a "${grub_module_dir}"/*.@(mod|lst) "${keep_dir}"
  49. }
  50. grub_build_utils() {
  51. (
  52. # If arch and/or platform files don't exist,
  53. # the configure script will pick a reasonable default
  54. local arch="$(grub_arch "${target}" "$@")"
  55. local platform="$(grub_platform "${target}" "$@")"
  56. cd "${sources_path}" || return
  57. if git_project_check "${repository}"; then
  58. ./autogen.sh
  59. fi
  60. ./configure --target="${arch}" --with-platform="${platform}"
  61. make -j"${TASKS}"
  62. )
  63. }
  64. grub_build_layout() {
  65. local raw_layout="${1##*/}"
  66. local raw_layout_path="$1"
  67. local grub_kbdcomp="${sources_path}/grub-kbdcomp"
  68. local grub_kbd_layout="${build_path}/${raw_layout}.gkb"
  69. "${grub_kbdcomp}" --output="${grub_kbd_layout}" "${raw_layout_path}"
  70. }
  71. grub_build_bootable_image() {
  72. local arch="$(grub_arch "${target}" "$@")"
  73. local format="$(grub_format "${target}" "$@")"
  74. local prefix="$(grub_prefix "${target}" "$@")"
  75. local config_path="$(grub_config_path "${target}" "$@")"
  76. local grub_mkimage="${sources_path}/grub-mkimage"
  77. local grub_module_dir="${sources_path}/grub-core"
  78. local grubimg="${build_path}/grub.img"
  79. local grub_bootimg="${grub_module_dir}/boot.img"
  80. local grub_bootable_img="${build_path}/grub2"
  81. "${grub_mkimage}" \
  82. --config="${config_path}" \
  83. --directory="${grub_module_dir}" \
  84. --output="${grubimg}" \
  85. --format="${format}" \
  86. --prefix="${prefix}" \
  87. cbfs configfile
  88. cat "${grub_bootimg}" "${grubimg}" > "${grub_bootable_img}"
  89. rm -f "${grubimg}"
  90. }
  91. grub_build_floppy_image() {
  92. local grubimg="${build_path}/grub2"
  93. local tempfile="${build_path}/temp.file"
  94. if ! ( grub_build_bootable_image "$@" ); then
  95. printf '\n%s\n\n' "Error: Failed to build a GRUB image" 1>&2
  96. return 1
  97. fi
  98. local size="$(grub_size "${target}" "$@")"
  99. # Pre-allocate a floppy-sized image
  100. # SeaBIOS requires floppy images to have a "correct" size
  101. if ! [[ -e "${tempfile}" ]]; then
  102. dd if=/dev/zero of="${tempfile}" bs=1024 count="${size:-160}"
  103. else
  104. printf '\n%s\n\n' "Error: File ${tempfile} already exists!" 1>&2
  105. return 1
  106. fi
  107. local -i grubimg_size="$(stat -c %s "${grubimg}")"
  108. local -i floppy_size="$((${size:-160} * 1024))"
  109. # Graft the GRUB image onto the blank floppy image
  110. if ((grubimg_size <= floppy_size)); then
  111. dd if="${grubimg}" of="${tempfile}" bs=1 conv=notrunc
  112. rm -f "${grubimg}"
  113. mv "${tempfile}" "${grubimg}"
  114. else
  115. printf '\n%s' "Error: Image ${grubimg##*/} is too large; " 1>&2
  116. printf '%s\n\n' "it must be less than ${size}KiB in size." 1>&2
  117. return 1
  118. fi
  119. }
  120. grub_build_standalone_image() {
  121. local arch="$(grub_arch "${target}" "$@")"
  122. local format="$(grub_format "${target}" "$@")"
  123. local prefix="$(grub_prefix "${target}" "$@")"
  124. local config_path="$(grub_config_path "${target}" "$@")"
  125. local grubimg="${build_path}/grub2"
  126. local grub_mkimage="${sources_path}/grub-mkimage"
  127. local grub_mkstandalone="${sources_path}/grub-mkstandalone"
  128. local grub_module_dir="${sources_path}/grub-core"
  129. "${grub_mkstandalone}" \
  130. --grub-mkimage="${grub_mkimage}" \
  131. --fonts='' \
  132. --themes='' \
  133. --locales='' \
  134. --install-modules='cbfs configfile' \
  135. --directory="${grub_module_dir}" \
  136. --format="${format}" \
  137. --output="${grubimg}" \
  138. /boot/grub/grub.cfg="${config_path}"
  139. }