grub.cfg 6.3 KB

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