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