]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/grub/grub.cfg
1.0.08beta2
[Ventoy.git] / INSTALL / grub / grub.cfg
1 #************************************************************************************
2 # Copyright (c) 2020, longpanda <admin@ventoy.net>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # 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 #
17 #************************************************************************************
18
19 function ventoy_boot_from_hdd {
20 if [ "$grub_platform" = "pc" ]; then
21 if [ "$iso_path" = "(hd0,1)" ]; then
22 if [ -b (hd1) ]; then
23 set root=(hd1)
24 drivemap -s hd0 hd1
25 chainloader +1
26 boot
27 else
28 echo "No local hdd found ..."
29 sleep 3
30 fi
31 else
32 set root=(hd0)
33 chainloader +1
34 boot
35 fi
36 else
37 exit
38 fi
39 }
40
41 function ventoy_reboot {
42 reboot
43 }
44
45
46 function get_os_type {
47 set vtoy_os=Linux
48 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com"; do
49 if [ -e $1/$file ]; then
50 set vtoy_os=Windows
51 break
52 fi
53 done
54
55 if [ -n "${vtdebug_flag}" ]; then
56 echo ISO is $vtoy_os
57 fi
58 }
59
60 function locate_initrd {
61 vt_linux_locate_initrd
62
63 if [ -n "${vtdebug_flag}" ]; then
64 vt_linux_dump_initrd
65 sleep 5
66 fi
67 }
68
69 function find_wim_file {
70 unset ventoy_wim_file
71
72 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" "winpe_x64.wim"; do
73 if [ -e $1/$file ]; then
74 set ventoy_wim_file=$1/$file
75 break
76 fi
77 done
78 }
79
80 function distro_specify_initrd_file {
81 if [ -e (loop)/boot/all.rdz ]; then
82 vt_linux_specify_initrd_file /boot/all.rdz
83 elif [ -e (loop)/boot/xen.gz ]; then
84 if [ -e (loop)/install.img ]; then
85 vt_linux_specify_initrd_file /install.img
86 fi
87 elif [ -d (loop)/casper ]; then
88 if [ -e (loop)/casper/initrd ]; then
89 vt_linux_specify_initrd_file /casper/initrd
90 fi
91 if [ -e (loop)/casper/initrd-oem ]; then
92 vt_linux_specify_initrd_file /casper/initrd-oem
93 fi
94 elif [ -e (loop)/boot/grub/initrd.xz ]; then
95 vt_linux_specify_initrd_file /boot/grub/initrd.xz
96 elif [ -e (loop)/initrd.gz ]; then
97 vt_linux_specify_initrd_file /initrd.gz
98 elif [ -e (loop)/slax/boot/initrfs.img ]; then
99 vt_linux_specify_initrd_file /slax/boot/initrfs.img
100 elif [ -e (loop)/pmagic/initrd.img ]; then
101 vt_linux_specify_initrd_file /pmagic/initrd.img
102 elif [ -e (loop)/boot/initrd.xz ]; then
103 vt_linux_specify_initrd_file /boot/initrd.xz
104 elif [ -f (loop)/boot/initrd ]; then
105 vt_linux_specify_initrd_file /boot/initrd
106 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
107 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
108 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
109 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
110
111
112 fi
113 }
114
115 function distro_specify_initrd_file_phase2 {
116 if [ -f (loop)/boot/initrd.img ]; then
117 vt_linux_specify_initrd_file /boot/initrd.img
118 fi
119 }
120
121 function uefi_windows_menu_func {
122 vt_windows_reset
123
124 if [ "$ventoy_compatible" = "NO" ]; then
125 find_wim_file (loop)
126 if [ -n "$ventoy_wim_file" ]; then
127 vt_windows_locate_wim $ventoy_wim_file
128 fi
129 fi
130
131 vt_windows_chain_data ${1}${chosen_path}
132
133 if [ -n "${vtdebug_flag}" ]; then
134 sleep 5
135 fi
136
137 if [ -n "$vtoy_chain_mem_addr" ]; then
138 terminal_output console
139 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
140 boot
141 else
142 echo "chain empty failed"
143 sleep 5
144 fi
145 }
146
147 function uefi_linux_menu_func {
148 if [ "$ventoy_compatible" = "NO" ]; then
149 vt_load_cpio ${vtoy_path}/ventoy.cpio
150
151 vt_linux_clear_initrd
152
153 if [ -d (loop)/pmagic ]; then
154 vt_linux_specify_initrd_file /pmagic/initrd.img
155 else
156 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf"; do
157 if [ -e (loop)/$file ]; then
158 vt_linux_parse_initrd_grub file (loop)/$file
159 fi
160 done
161 fi
162
163 # special process for special distros
164 if [ -d (loop)/loader/entries ]; then
165 set LoadIsoEfiDriver=on
166 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
167 elif [ -d (loop)/boot/grub ]; then
168 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
169 fi
170
171 if [ -e (loop)/syslinux/alt0/full.cz ]; then
172 set LoadIsoEfiDriver=on
173 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
174 fi
175
176 distro_specify_initrd_file
177
178 vt_linux_initrd_count vtcount
179 if [ $vtcount -eq 0 ]; then
180 distro_specify_initrd_file_phase2
181
182 if [ "$vt_efi_dir" = "NO" ]; then
183 if [ -f (loop)/efi.img ]; then
184 vt_add_replace_file 0 "initrd"
185 fi
186 fi
187 fi
188
189 locate_initrd
190 fi
191
192 vt_linux_chain_data ${1}${chosen_path}
193
194 if [ -n "$vtoy_chain_mem_addr" ]; then
195 terminal_output console
196 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} FirstTry=${FirstTryBootFile} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
197 boot
198 else
199 echo "chain empty failed"
200 sleep 5
201 fi
202 }
203
204
205 function uefi_iso_menu_func {
206
207 if [ -d (loop)/ ]; then
208 loopback -d loop
209 fi
210
211 if [ -n "$vtisouefi" ]; then
212 set LoadIsoEfiDriver=on
213 unset vtisouefi
214 elif [ -n "$VTOY_ISO_UEFI_DRV" ]; then
215 set LoadIsoEfiDriver=on
216 else
217 unset LoadIsoEfiDriver
218 fi
219
220 vt_chosen_img_path chosen_path
221
222 if vt_is_udf ${1}${chosen_path}; then
223 set ventoy_fs_probe=udf
224 else
225 set ventoy_fs_probe=iso9660
226 fi
227
228 loopback loop ${1}${chosen_path}
229 get_os_type (loop)
230
231 if [ -d (loop)/EFI ]; then
232 set vt_efi_dir=YES
233 elif [ -d (loop)/efi ]; then
234 set vt_efi_dir=YES
235 else
236 set vt_efi_dir=NO
237 fi
238
239 if [ -n "$vtcompat" ]; then
240 set ventoy_compatible=YES
241 unset vtcompat
242 elif [ -n "$VTOY_ISO_RAW" ]; then
243 set ventoy_compatible=YES
244 else
245 vt_check_compatible (loop)
246 fi
247
248 vt_img_sector ${1}${chosen_path}
249
250 if [ "$vtoy_os" = "Windows" ]; then
251 if [ "$ventoy_fs_probe" = "iso9660" ]; then
252 set ventoy_compatible=YES
253 elif [ -f (loop)/HBCD_PE.ini ]; then
254 set ventoy_compatible=YES
255 fi
256
257 uefi_windows_menu_func $1
258 else
259 uefi_linux_menu_func $1
260 fi
261
262 terminal_output gfxterm
263 }
264
265 function uefi_iso_memdisk {
266 vt_chosen_img_path chosen_path
267
268 echo 'Loading ISO file to memory ...'
269 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
270
271 terminal_output console
272 chainloader ${vtoy_path}/ventoy_x64.efi memdisk env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_iso_buf_addr}:size:${vtoy_iso_buf_size}
273 boot
274 }
275
276
277
278
279 function legacy_windows_menu_func {
280 vt_windows_reset
281
282 if [ "$ventoy_compatible" = "NO" ]; then
283 find_wim_file (loop)
284 if [ -n "$ventoy_wim_file" ]; then
285 vt_windows_locate_wim $ventoy_wim_file
286 elif [ -n "${vtdebug_flag}" ]; then
287 echo No wim file found
288 fi
289 fi
290
291 vt_windows_chain_data ${1}${chosen_path}
292
293 if [ -n "${vtdebug_flag}" ]; then
294 sleep 5
295 fi
296
297 if [ -n "$vtoy_chain_mem_addr" ]; then
298 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
299 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
300 boot
301 else
302 echo "chain empty failed"
303 sleep 5
304 fi
305 }
306
307 function legacy_linux_menu_func {
308 if [ "$ventoy_compatible" = "NO" ]; then
309
310 vt_load_cpio $vtoy_path/ventoy.cpio
311
312 vt_linux_clear_initrd
313
314 if [ -d (loop)/pmagic ]; then
315 vt_linux_specify_initrd_file /pmagic/initrd.img
316 else
317 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
318 if [ -d (loop)/$dir ]; then
319 vt_linux_parse_initrd_isolinux (loop)/$dir/
320 fi
321 done
322 fi
323
324 # special process for special distros
325 #archlinux
326 if [ -d (loop)/arch/boot/syslinux ]; then
327 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
328 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
329
330 #manjaro
331 elif [ -d (loop)/manjaro ]; then
332 if [ -e (loop)/boot/grub/kernels.cfg ]; then
333 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
334 fi
335 elif [ -e (loop)/boot/grub/grub.cfg ]; then
336 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
337 fi
338
339 distro_specify_initrd_file
340
341 vt_linux_initrd_count vtcount
342 if [ $vtcount -eq 0 ]; then
343 distro_specify_initrd_file_phase2
344 fi
345
346 locate_initrd
347 fi
348
349 vt_linux_chain_data ${1}${chosen_path}
350
351 if [ -n "${vtdebug_flag}" ]; then
352 sleep 5
353 fi
354
355 if [ -n "$vtoy_chain_mem_addr" ]; then
356 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
357 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
358 boot
359 else
360 echo "chain empty failed"
361 sleep 5
362 fi
363 }
364
365 function legacy_iso_menu_func {
366
367 if [ -d (loop)/ ]; then
368 loopback -d loop
369 fi
370
371 vt_chosen_img_path chosen_path
372
373 if vt_is_udf ${1}${chosen_path}; then
374 set ventoy_fs_probe=udf
375 else
376 set ventoy_fs_probe=iso9660
377 fi
378
379 loopback loop ${1}${chosen_path}
380
381 get_os_type (loop)
382
383 if [ -n "$vtcompat" ]; then
384 set ventoy_compatible=YES
385 unset vtcompat
386 elif [ -n "$VTOY_ISO_RAW" ]; then
387 set ventoy_compatible=YES
388 else
389 vt_check_compatible (loop)
390 fi
391
392 vt_img_sector ${1}${chosen_path}
393
394 if [ "$vtoy_os" = "Windows" ]; then
395 if [ "$ventoy_fs_probe" = "iso9660" ]; then
396 set ventoy_compatible=YES
397 elif [ -f (loop)/HBCD_PE.ini ]; then
398 set ventoy_compatible=YES
399 fi
400
401 legacy_windows_menu_func $1
402 else
403 legacy_linux_menu_func $1
404 fi
405 }
406
407 function legacy_iso_memdisk {
408 vt_chosen_img_path chosen_path
409
410 linux16 $vtoy_path/memdisk iso raw
411 echo "Loading ISO file to memory ..."
412 initrd16 ${1}${chosen_path}
413 boot
414 }
415
416
417 #############################################################
418 #############################################################
419 #############################################################
420 ####### Main Process ###########
421 #############################################################
422 #############################################################
423 #############################################################
424
425 set VENTOY_VERSION="1.0.08b2"
426
427 #disable timeout
428 unset timeout
429
430 set VTOY_MEM_DISK_STR="MEMDISK"
431 set VTOY_ISO_RAW_STR="ISO RAW"
432 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
433
434 set VTOY_F2_CMD="ventoy_boot_from_hdd"
435 set VTOY_F3_CMD="ventoy_reboot"
436
437 vt_device $root vtoy_dev
438
439 if [ "$vtoy_dev" = "tftp" ]; then
440 set vtoy_path=($root)
441 for vtid in 0 1 2 3; do
442 if [ -d (hd$vtid,2)/ventoy ]; then
443 set iso_path=(hd$vtid,1)
444 break
445 fi
446 done
447 else
448 set vtoy_path=($root)/ventoy
449 set iso_path=($vtoy_dev,1)
450 fi
451
452 loadfont ascii
453
454 if [ -f $iso_path/ventoy/ventoy.json ]; then
455 vt_load_plugin $iso_path
456 fi
457
458 if [ -n "$vtoy_gfxmode" ]; then
459 set gfxmode=$vtoy_gfxmode
460 else
461 set gfxmode=1920x1080,1366x768,1024x768
462 fi
463
464 if [ -n "$vtoy_theme" ]; then
465 set theme=$vtoy_theme
466 else
467 set theme=$prefix/themes/ventoy/theme.txt
468 fi
469
470 terminal_output gfxterm
471
472 #colect all image files (iso files)
473 set ventoy_img_count=0
474 vt_list_img $iso_path ventoy_img_count
475
476 #Dynamic menu for every iso file
477 if vt_cmp $ventoy_img_count ne 0; then
478 set imgid=0
479 while vt_cmp $imgid lt $ventoy_img_count; do
480 vt_img_name $imgid img_name
481 menuentry "$img_name" {
482 if [ "$grub_platform" = "pc" ]; then
483 if [ -n "$VTOY_MEM_DISK" ]; then
484 legacy_iso_memdisk $iso_path
485 else
486 legacy_iso_menu_func $iso_path
487 fi
488 else
489 if [ -n "$VTOY_MEM_DISK" ]; then
490 uefi_iso_memdisk $iso_path
491 else
492 uefi_iso_menu_func $iso_path
493 fi
494 fi
495 }
496
497 vt_incr imgid 1
498 done
499 else
500 menuentry "No ISO files found (Press enter to reboot ...)" {
501 echo -e "\n Rebooting ... "
502 reboot
503 }
504 fi
505