grub.cfg 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. set prefix=(memdisk)/boot/grub
  2. insmod at_keyboard
  3. insmod usb_keyboard
  4. insmod nativedisk
  5. insmod ehci
  6. insmod ohci
  7. insmod uhci
  8. insmod usb
  9. insmod usbms
  10. insmod usbserial_pl2303
  11. insmod usbserial_ftdi
  12. insmod usbserial_usbdebug
  13. insmod regexp
  14. # Serial and keyboard configuration, very important.
  15. serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1
  16. terminal_input --append serial
  17. terminal_output --append serial
  18. terminal_input --append at_keyboard
  19. terminal_output --append cbmemc
  20. gfxpayload=keep
  21. terminal_output --append gfxterm
  22. # Default to first option, automatically boot after 1 second
  23. set default="0"
  24. set timeout=1
  25. # This is useful when using 'cat' on long files on GRUB terminal
  26. set pager=1
  27. keymap usqwerty
  28. function try_user_config {
  29. set root="${1}"
  30. for dir in boot grub grub2 boot/grub boot/grub2; do
  31. for name in '' osboot_ autoboot_ libreboot_ coreboot_; do
  32. if [ -f /"${dir}"/"${name}"grub.cfg ]; then
  33. unset superusers
  34. configfile /"${dir}"/"${name}"grub.cfg
  35. fi
  36. done
  37. done
  38. }
  39. function search_grub {
  40. echo -n "Attempting to load grub.cfg from: "
  41. unset ddev
  42. if [ (${1}?) != "(${1}?)" ]; then
  43. ddev=(${1}*) # Both raw and partitioned devices
  44. fi
  45. for i in ${ddev}; do
  46. echo -n "${i} "
  47. try_user_config "${i}"
  48. done
  49. echo # Insert newline
  50. }
  51. function try_isolinux_config {
  52. set root="${1}"
  53. for dir in '' /boot; do
  54. if [ -f "${dir}"/isolinux/isolinux.cfg ]; then
  55. syslinux_configfile -i "${dir}"/isolinux/isolinux.cfg
  56. elif [ -f "${dir}"/syslinux/syslinux.cfg ]; then
  57. syslinux_configfile -s "${dir}"/syslinux/syslinux.cfg
  58. fi
  59. done
  60. }
  61. function search_isolinux {
  62. unset ddev
  63. if [ (${1}?) != "(${1}?)" ]; then
  64. ddev=(${1}*) # Both raw and partitioned devices.
  65. echo -n "Attempting to parse isolinux menu from: "
  66. fi
  67. for i in ${ddev}; do
  68. echo -n "${i} "
  69. try_isolinux_config "${i}"
  70. done
  71. echo # Insert newline
  72. }
  73. menuentry 'Load Operating System (incl. fully encrypted disks) [o]' --hotkey='o' {
  74. # GRUB2 handles (almost) every possible disk setup, but only the location of
  75. # /boot is actually important since GRUB2 only loads the user's config.
  76. # LVM, RAID, filesystems and encryption on both raw devices and partitions in
  77. # all various combinations need to be supported. Since full disk encryption is
  78. # possible with GRUB2 as payload and probably even used by most users, this
  79. # configuration tries to load the operating system in the following way:
  80. # 1. Look for user configuration on unencrypted devices first to avoid
  81. # unnecessary decryption routines in the following order:
  82. # 1) raw devices and MBR/GPT partitions
  83. search_grub usb
  84. search_grub ahci
  85. search_grub ata
  86. # 2) LVM and RAID which might be used accross multiple devices
  87. unset lvmvol
  88. for vol in bootvol rootvol; do
  89. if [ (lvm\/?atrix-${vol}) != "(lvm/?atrix-${vol})" ]; then # Sketchy check, hardcoded string to be dropped in future
  90. lvmvol="${lvmvol} (lvm/matrix-${vol})"
  91. fi
  92. done
  93. unset raidvol
  94. if [ (md/?) != "(md/?)" ] ; then
  95. raidvol=(md/?)
  96. fi
  97. for vol in ${lvmvol} ${raidvol} ; do
  98. try_user_config "${vol}"
  99. done
  100. # 2. In case no configuration could be found, try decrypting devices. Look
  101. # on raw crypto devices as well as inside LVM volumes this time.
  102. # The user will be prompted for a passphrase if a LUKS header was found.
  103. # Encrypted disks and partitions
  104. #TODO: This needs to be adjusted on each device to exclude ODD
  105. #TODO: Usually ATA is for odd if both exist!
  106. #TODO: Shouldn't it stop at first successful cryptomount?
  107. #TODO: Unset variables before use!
  108. #TODO: Pick better variable name scheme than ${ddev}, or find way to make it local
  109. unset ahcidev
  110. unset atadev
  111. unset usbdev
  112. if [ (ahci?) != "(ahci?)" ]; then
  113. ahcidev=(ahci*)
  114. fi
  115. if [ (ata?) != "(ata?)" ]; then
  116. atadev=(ata*)
  117. fi
  118. if [ (usb?) != "(usb?)" ]; then
  119. usbdev=(usb*)
  120. fi
  121. set pager=0
  122. echo -n "Attempting to cryptomount: "
  123. for dev in ${usbdev} ${ahcidev} ${atadev} ${lvmvol}; do # what about raid?
  124. echo -n "${dev} "
  125. cryptomount "${dev}"
  126. done
  127. set pager=1
  128. echo # Insert newline
  129. # Rescan lvm volumes, should probably use test at this point
  130. unset lvmvol
  131. for vol in bootvol rootvol; do
  132. if [ (lvm\/?atrix-${vol}) != "(lvm/?atrix-${vol})" ]; then # Sketchy check, hardcoded string to be dropped in future
  133. lvmvol="${lvmvol} (lvm/matrix-${vol})"
  134. fi
  135. done
  136. # 3) encrypted devices/partitions
  137. search_grub crypto
  138. # 4) LVM inside LUKS containers
  139. for vol in ${lvmvol}; do
  140. try_user_config "${vol}"
  141. done
  142. # TODO: generalize last resorts
  143. # Use first connected device? not just sata port 1
  144. # Last resort, if all else fails
  145. set root=ahci0,1
  146. for p in / /boot/; do
  147. if [ -f "${p}vmlinuz" ]; then
  148. linux ${p}vmlinuz root=/dev/sda1 rw
  149. if [ -f "${p}initrd.img" ]; then
  150. initrd ${p}initrd.img
  151. fi
  152. fi
  153. done
  154. # Last resort (for setups that use IDE instead of SATA)
  155. set root=ata0,1
  156. for p in / /boot/; do
  157. if [ -f "${p}vmlinuz" ]; then
  158. linux ${p}vmlinuz root=/dev/sda1 rw
  159. if [ -f "${p}initrd.img" ]; then
  160. initrd ${p}initrd.img
  161. fi
  162. fi
  163. done
  164. true # Prevent pager requiring to accept each line instead of whole screen
  165. }
  166. menuentry 'Search ISOLINUX menu (AHCI) [a]' --hotkey='a' {
  167. search_isolinux ahci
  168. }
  169. menuentry 'Search ISOLINUX menu (USB) [u]' --hotkey='u' {
  170. search_isolinux usb
  171. }
  172. menuentry 'Search ISOLINUX menu (CD/DVD) [d]' --hotkey='d' {
  173. insmod ata
  174. unset ahcidev
  175. unset atadev
  176. if [ (ata?) != "(ata?)" ]; then
  177. atadev=(ata?) # Only full drives not partitions
  178. fi
  179. if [ (ahci?) != "(ahci?)" ]; then
  180. ahcidev=(ahci1) # TODO: hardcoded!!!
  181. fi
  182. echo -n "Attempting to parse isolinux menu from: "
  183. for dev in ${atadev} ${ahcidev}; do
  184. echo -n "${dev} "
  185. try_isolinux_config "${dev}"
  186. done
  187. echo # Insert newline
  188. }
  189. menuentry 'Load test configuration (grubtest.cfg) inside of CBFS [t]' --hotkey='t' {
  190. set root='(cbfsdisk)'
  191. if [ -f /grubtest.cfg ]; then
  192. configfile /grubtest.cfg
  193. fi
  194. }
  195. menuentry 'Search for GRUB2 configuration on external media [s]' --hotkey='s' {
  196. search_grub usb
  197. }
  198. if [ -f (cbfsdisk)/seabios.elf ]; then
  199. menuentry 'Load SeaBIOS (payload) [b]' --hotkey='b' {
  200. set root='cbfsdisk'
  201. chainloader /seabios.elf
  202. }
  203. fi
  204. if [ -f (cbfsdisk)/img/grub2 ]; then
  205. menuentry 'Return to SeaBIOS [b]' --hotkey='b' {
  206. set root='cbfsdisk'
  207. chainloader /fallback/payload
  208. }
  209. fi
  210. menuentry 'Poweroff [p]' --hotkey='p' {
  211. halt
  212. }
  213. menuentry 'Reboot [r]' --hotkey='r' {
  214. reboot
  215. }
  216. if [ -f (cbfsdisk)/tianocore.elf ]; then
  217. menuentry 'Load Tianocore UEFI payload' {
  218. set root='cbfsdisk'
  219. chainloader /tianocore.elf
  220. }
  221. fi
  222. if [ -f (cbfsdisk)/img/memtest ]; then
  223. menuentry 'Load MemTest86+ [m]' --hotkey='m' {
  224. set root='cbfsdisk'
  225. chainloader /img/memtest
  226. }
  227. fi