]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/grub/grub.cfg
1.0.12 release
[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_power {
20 configfile ($root)/grub/power.cfg
21 }
22
23 function get_os_type {
24 set vtoy_os=Linux
25 for file in "efi/microsoft" "sources/boot.wim" "boot/bcd" "bootmgr.efi" "boot/etfsboot.com" "BOOT/etfsboot.com"; do
26 if [ -e $1/$file ]; then
27 set vtoy_os=Windows
28 break
29 fi
30 done
31
32 if [ -n "${vtdebug_flag}" ]; then
33 echo ISO is $vtoy_os
34 fi
35 }
36
37 function vt_check_pe {
38 unset VT_PE_SUPPORT
39
40 if [ -f $1/HBCD_PE.ini ]; then
41 set ventoy_compatible=YES
42 set VT_PE_SUPPORT=YES
43 elif [ -f $1/easyu.flg ]; then
44 set VT_PE_SUPPORT=YES
45 elif [ -f $1/USM.ICO ]; then
46 set VT_PE_SUPPORT=YES
47 elif [ -d $1/USM_TOOL ]; then
48 set VT_PE_SUPPORT=YES
49 fi
50 }
51
52 function locate_initrd {
53 vt_linux_locate_initrd
54
55 if [ -n "${vtdebug_flag}" ]; then
56 vt_linux_dump_initrd
57 sleep 5
58 fi
59 }
60
61 function find_wim_file {
62 unset ventoy_wim_file
63
64 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" \
65 "winpe_x64.wim" "boot/10pex64.wim" "BOOT/USM1PE6L.WIM" "BOOT/USM1PE6F.WIM"; do
66 if [ -e $1/$file ]; then
67 set ventoy_wim_file=$1/$file
68 break
69 fi
70 done
71 }
72
73 function distro_specify_initrd_file {
74 if [ -e (loop)/boot/all.rdz ]; then
75 vt_linux_specify_initrd_file /boot/all.rdz
76 elif [ -e (loop)/boot/xen.gz ]; then
77 if [ -e (loop)/install.img ]; then
78 vt_linux_specify_initrd_file /install.img
79 fi
80 elif [ -d (loop)/casper ]; then
81 if [ -e (loop)/casper/initrd ]; then
82 vt_linux_specify_initrd_file /casper/initrd
83 fi
84 if [ -e (loop)/casper/initrd-oem ]; then
85 vt_linux_specify_initrd_file /casper/initrd-oem
86 fi
87 elif [ -e (loop)/boot/grub/initrd.xz ]; then
88 vt_linux_specify_initrd_file /boot/grub/initrd.xz
89 elif [ -e (loop)/initrd.gz ]; then
90 vt_linux_specify_initrd_file /initrd.gz
91 elif [ -e (loop)/slax/boot/initrfs.img ]; then
92 vt_linux_specify_initrd_file /slax/boot/initrfs.img
93 elif [ -e (loop)/pmagic/initrd.img ]; then
94 vt_linux_specify_initrd_file /pmagic/initrd.img
95 elif [ -e (loop)/boot/initrd.xz ]; then
96 vt_linux_specify_initrd_file /boot/initrd.xz
97 elif [ -f (loop)/boot/initrd ]; then
98 vt_linux_specify_initrd_file /boot/initrd
99 elif [ -f (loop)/boot/x86_64/loader/initrd ]; then
100 vt_linux_specify_initrd_file /boot/x86_64/loader/initrd
101 elif [ -f (loop)/boot/initramfs-x86_64.img ]; then
102 vt_linux_specify_initrd_file /boot/initramfs-x86_64.img
103
104
105 fi
106 }
107
108
109 function distro_specify_initrd_file_phase2 {
110 if [ -f (loop)/boot/initrd.img ]; then
111 vt_linux_specify_initrd_file /boot/initrd.img
112 elif [ -f (loop)/Setup/initrd.gz ]; then
113 vt_linux_specify_initrd_file /Setup/initrd.gz
114 elif [ -f (loop)/isolinux/initramfs ]; then
115 vt_linux_specify_initrd_file /isolinux/initramfs
116 elif [ -f (loop)/boot/iniramfs.igz ]; then
117 vt_linux_specify_initrd_file /boot/iniramfs.igz
118 elif [ -f (loop)/initrd-x86_64 ]; then
119 vt_linux_specify_initrd_file /initrd-x86_64
120 elif [ -f (loop)/live/initrd.img ]; then
121 vt_linux_specify_initrd_file /live/initrd.img
122 elif [ -f (loop)/initrd.img ]; then
123 vt_linux_specify_initrd_file /initrd.img
124
125 fi
126 }
127
128 function uefi_windows_menu_func {
129 vt_windows_reset
130
131 if [ "$ventoy_compatible" = "NO" ]; then
132 find_wim_file (loop)
133 if [ -n "$ventoy_wim_file" ]; then
134 vt_windows_locate_wim $ventoy_wim_file
135 fi
136 fi
137
138 vt_windows_chain_data ${1}${chosen_path}
139
140 if [ -n "${vtdebug_flag}" ]; then
141 sleep 5
142 fi
143
144 if [ -n "$vtoy_chain_mem_addr" ]; then
145 terminal_output console
146 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
147 boot
148 else
149 echo "chain empty failed"
150 sleep 5
151 fi
152 }
153
154 function uefi_linux_menu_func {
155 if [ "$ventoy_compatible" = "NO" ]; then
156 vt_load_cpio ${vtoy_path}/ventoy.cpio $2 $1
157
158 vt_linux_clear_initrd
159
160 if [ -d (loop)/pmagic ]; then
161 vt_linux_specify_initrd_file /pmagic/initrd.img
162 else
163 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf" "/grub/grub.cfg"; do
164 if [ -e (loop)/$file ]; then
165 vt_linux_parse_initrd_grub file (loop)/$file
166 fi
167 done
168 fi
169
170 # special process for special distros
171 if [ -d (loop)/loader/entries ]; then
172 set LoadIsoEfiDriver=on
173 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
174 elif [ -d (loop)/boot/grub ]; then
175 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
176 fi
177
178 if [ -e (loop)/syslinux/alt0/full.cz ]; then
179 set LoadIsoEfiDriver=on
180 set FirstTryBootFile='@EFI@BOOT@grubx64.efi'
181 fi
182
183 distro_specify_initrd_file
184
185 vt_linux_initrd_count vtcount
186 if [ $vtcount -eq 0 ]; then
187 distro_specify_initrd_file_phase2
188
189 if [ "$vt_efi_dir" = "NO" ]; then
190 if [ -f (loop)/efi.img ]; then
191 vt_add_replace_file 0 "initrd"
192 fi
193 fi
194 fi
195
196 locate_initrd
197 fi
198
199 vt_linux_chain_data ${1}${chosen_path}
200
201 if [ -n "$vtoy_chain_mem_addr" ]; then
202 terminal_output console
203 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}
204 boot
205 else
206 echo "chain empty failed"
207 sleep 5
208 fi
209 }
210
211
212 function uefi_iso_menu_func {
213
214 if [ -d (loop)/ ]; then
215 loopback -d loop
216 fi
217
218 if [ -n "$vtisouefi" ]; then
219 set LoadIsoEfiDriver=on
220 unset vtisouefi
221 elif vt_check_mode 2; then
222 set LoadIsoEfiDriver=on
223 else
224 unset LoadIsoEfiDriver
225 fi
226
227 vt_chosen_img_path chosen_path
228 vt_select_auto_install ${chosen_path}
229 vt_select_persistence ${chosen_path}
230
231 if vt_is_udf ${1}${chosen_path}; then
232 set ventoy_fs_probe=udf
233 else
234 set ventoy_fs_probe=iso9660
235 fi
236
237 loopback loop ${1}${chosen_path}
238 get_os_type (loop)
239
240 if [ -d (loop)/EFI ]; then
241 set vt_efi_dir=YES
242 elif [ -d (loop)/efi ]; then
243 set vt_efi_dir=YES
244 else
245 set vt_efi_dir=NO
246 fi
247
248 if [ -n "$vtcompat" ]; then
249 set ventoy_compatible=YES
250 unset vtcompat
251 elif vt_check_mode 1; then
252 set ventoy_compatible=YES
253 else
254 vt_check_compatible (loop)
255 fi
256
257 vt_img_sector ${1}${chosen_path}
258
259 if [ "$vtoy_os" = "Windows" ]; then
260 vt_check_pe (loop)
261 if [ "$VT_PE_SUPPORT" != "YES" ]; then
262 if [ "$ventoy_fs_probe" = "iso9660" ]; then
263 set ventoy_compatible=YES
264 fi
265 fi
266
267 uefi_windows_menu_func $1 ${chosen_path}
268 else
269 uefi_linux_menu_func $1 ${chosen_path}
270 fi
271
272 terminal_output gfxterm
273 }
274
275 function uefi_iso_memdisk {
276 vt_chosen_img_path chosen_path
277
278 echo 'Loading ISO file to memory ...'
279 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
280
281 terminal_output console
282 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}
283 boot
284 }
285
286
287 function legacy_windows_menu_func {
288 vt_windows_reset
289
290 if [ "$ventoy_compatible" = "NO" ]; then
291 find_wim_file (loop)
292 if [ -n "$ventoy_wim_file" ]; then
293 vt_windows_locate_wim $ventoy_wim_file
294 elif [ -n "${vtdebug_flag}" ]; then
295 echo No wim file found
296 fi
297 fi
298
299 vt_windows_chain_data ${1}${chosen_path}
300
301 if [ -n "${vtdebug_flag}" ]; then
302 sleep 5
303 fi
304
305 if [ -n "$vtoy_chain_mem_addr" ]; then
306 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
307 boot
308 else
309 echo "chain empty failed"
310 sleep 5
311 fi
312 }
313
314 function legacy_linux_menu_func {
315 if [ "$ventoy_compatible" = "NO" ]; then
316 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
317
318 vt_linux_clear_initrd
319
320 if [ -d (loop)/pmagic ]; then
321 vt_linux_specify_initrd_file /pmagic/initrd.img
322 else
323 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
324 if [ -d (loop)/$dir ]; then
325 vt_linux_parse_initrd_isolinux (loop)/$dir/
326 fi
327 done
328 fi
329
330 # special process for special distros
331 #archlinux
332 if [ -d (loop)/arch/boot/syslinux ]; then
333 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
334 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
335
336 #manjaro
337 elif [ -d (loop)/manjaro ]; then
338 if [ -e (loop)/boot/grub/kernels.cfg ]; then
339 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
340 fi
341 elif [ -e (loop)/boot/grub/grub.cfg ]; then
342 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
343 fi
344
345 distro_specify_initrd_file
346
347 vt_linux_initrd_count vtcount
348 if [ $vtcount -eq 0 ]; then
349 distro_specify_initrd_file_phase2
350 fi
351
352 locate_initrd
353 fi
354
355 vt_linux_chain_data ${1}${chosen_path}
356
357 if [ -n "${vtdebug_flag}" ]; then
358 sleep 5
359 fi
360
361 if [ -n "$vtoy_chain_mem_addr" ]; then
362 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
363 boot
364 else
365 echo "chain empty failed"
366 sleep 5
367 fi
368 }
369
370 function legacy_iso_menu_func {
371
372 if [ -d (loop)/ ]; then
373 loopback -d loop
374 fi
375
376 vt_chosen_img_path chosen_path
377 vt_select_auto_install ${chosen_path}
378 vt_select_persistence ${chosen_path}
379
380 if vt_is_udf ${1}${chosen_path}; then
381 set ventoy_fs_probe=udf
382 else
383 set ventoy_fs_probe=iso9660
384 fi
385
386 loopback loop ${1}${chosen_path}
387
388 get_os_type (loop)
389
390 if [ -n "$vtcompat" ]; then
391 set ventoy_compatible=YES
392 unset vtcompat
393 elif vt_check_mode 1; then
394 set ventoy_compatible=YES
395 else
396 vt_check_compatible (loop)
397 fi
398
399 vt_img_sector ${1}${chosen_path}
400
401 if [ "$vtoy_os" = "Windows" ]; then
402 vt_check_pe (loop)
403 if [ "$VT_PE_SUPPORT" != "YES" ]; then
404 if [ "$ventoy_fs_probe" = "iso9660" ]; then
405 set ventoy_compatible=YES
406 fi
407 fi
408
409 legacy_windows_menu_func $1 ${chosen_path}
410 else
411 legacy_linux_menu_func $1 ${chosen_path}
412 fi
413 }
414
415 function legacy_iso_memdisk {
416 vt_chosen_img_path chosen_path
417
418 linux16 $vtoy_path/memdisk iso raw
419 echo "Loading ISO file to memory ..."
420 initrd16 ${1}${chosen_path}
421 boot
422 }
423
424 function iso_common_menuentry {
425 if [ "$grub_platform" = "pc" ]; then
426 if vt_check_mode 0; then
427 legacy_iso_memdisk $iso_path
428 else
429 legacy_iso_menu_func $iso_path
430 fi
431 else
432 if vt_check_mode 0; then
433 uefi_iso_memdisk $iso_path
434 else
435 uefi_iso_menu_func $iso_path
436 fi
437 fi
438 }
439
440 function iso_unsupport_menuentry {
441 echo -e "\n The name of the iso file could NOT contain space or non-ascii characters. \n"
442 echo -e "\n Will return to main menu after 10 seconds ...\n"
443 sleep 10
444 }
445
446
447 function wim_common_menuentry {
448 vt_chosen_img_path chosen_path
449 vt_wim_chain_data ${iso_path}${chosen_path}
450 if [ -n "${vtdebug_flag}" ]; then
451 sleep 5
452 fi
453
454 if [ -n "$vtoy_chain_mem_addr" ]; then
455 if [ "$grub_platform" = "pc" ]; then
456 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
457 else
458 terminal_output console
459 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
460 fi
461 boot
462 else
463 echo "chain empty failed"
464 sleep 5
465 fi
466 }
467
468 function wim_unsupport_menuentry {
469 echo -e "\n The name of the wim file could NOT contain space or non-ascii characters. \n"
470 echo -e "\n Will return to main menu after 10 seconds ...\n"
471 sleep 10
472 }
473
474 #############################################################
475 #############################################################
476 #############################################################
477 ####### Main Process ###########
478 #############################################################
479 #############################################################
480 #############################################################
481
482 set VENTOY_VERSION="1.0.12"
483
484 # Default menu display mode, you can change it as you want.
485 # 0: List mode
486 # 1: TreeView mode
487 set VTOY_DEFAULT_MENU_MODE=0
488
489 #disable timeout
490 unset timeout
491
492 set VTOY_MEM_DISK_STR="[Memdisk]"
493 set VTOY_ISO_RAW_STR="ISO RAW"
494 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
495
496 set VTOY_F2_CMD="ventoy_power"
497
498 if [ "$grub_platform" = "pc" ]; then
499 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
500 else
501 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
502 fi
503
504 vt_device $root vtoy_dev
505
506 if [ "$vtoy_dev" = "tftp" ]; then
507 set vtoy_path=($root)
508 for vtid in 0 1 2 3; do
509 if [ -d (hd$vtid,2)/ventoy ]; then
510 set iso_path=(hd$vtid,1)
511 set vtoy_efi_part=(hd$vtid,2)
512 break
513 fi
514 done
515 else
516 set vtoy_path=($root)/ventoy
517 set iso_path=($vtoy_dev,1)
518 set vtoy_efi_part=($vtoy_dev,2)
519 fi
520
521 loadfont ascii
522
523 #Load Plugin
524 if [ -f $iso_path/ventoy/ventoy.json ]; then
525 vt_load_plugin $iso_path
526 fi
527
528 if [ -f $iso_path/ventoy/ventoy_wimboot.img ]; then
529 vt_load_wimboot $iso_path/ventoy/ventoy_wimboot.img
530 elif [ -f $vtoy_efi_part/ventoy/ventoy_wimboot.img ]; then
531 vt_load_wimboot $vtoy_efi_part/ventoy/ventoy_wimboot.img
532 fi
533
534
535 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
536 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
537 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView"
538 else
539 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
540 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView"
541 fi
542
543
544 if [ -n "$vtoy_gfxmode" ]; then
545 set gfxmode=$vtoy_gfxmode
546 else
547 set gfxmode=1920x1080,1366x768,1024x768
548 fi
549
550 if [ -n "$vtoy_theme" ]; then
551 set theme=$vtoy_theme
552 else
553 set theme=$prefix/themes/ventoy/theme.txt
554 fi
555
556 terminal_output gfxterm
557
558 #colect all image files (iso files)
559 set ventoy_img_count=0
560 vt_list_img $iso_path ventoy_img_count
561
562 #Main menu
563 if [ $ventoy_img_count -gt 0 ]; then
564 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
565 vt_dynamic_menu 0 0
566 else
567 vt_dynamic_menu 0 1
568 fi
569 else
570 if [ -n "$VTOY_NO_ISO_TIP" ]; then
571 NO_ISO_MENU="No ISO files found, $VTOY_NO_ISO_TIP"
572 elif [ -n "$VTOY_DEFAULT_SEARCH_ROOT" ]; then
573 NO_ISO_MENU="No ISO files found, please check VTOY_DEFAULT_SEARCH_ROOT"
574 else
575 NO_ISO_MENU="No ISO files found"
576 fi
577 menuentry "$NO_ISO_MENU (Press enter to reboot ...)" {
578 echo -e "\n Rebooting ... "
579 reboot
580 }
581 fi