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