grub.cfg 6.5 KB

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