]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/grub/grub.cfg
1.0.09 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 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"; 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 locate_initrd {
54 vt_linux_locate_initrd
55
56 if [ -n "${vtdebug_flag}" ]; then
57 vt_linux_dump_initrd
58 sleep 5
59 fi
60 }
61
62 function find_wim_file {
63 unset ventoy_wim_file
64
65 for file in "sources/boot.wim" "sources/BOOT.WIM" "Sources/Win10PEx64.WIM" "boot/BOOT.WIM" "winpe_x64.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 function distro_specify_initrd_file_phase2 {
109 if [ -f (loop)/boot/initrd.img ]; then
110 vt_linux_specify_initrd_file /boot/initrd.img
111 elif [ -f (loop)/Setup/initrd.gz ]; then
112 vt_linux_specify_initrd_file /Setup/initrd.gz
113 elif [ -f (loop)/isolinux/initramfs ]; then
114 vt_linux_specify_initrd_file /isolinux/initramfs
115 elif [ -f (loop)/boot/iniramfs.igz ]; then
116 vt_linux_specify_initrd_file /boot/iniramfs.igz
117
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 $2 $1
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" "/grub/grub.cfg"; 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 vt_check_mode 2; 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 vt_check_mode 1; 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 ${chosen_path}
258 else
259 uefi_linux_menu_func $1 ${chosen_path}
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 function legacy_windows_menu_func {
278 vt_windows_reset
279
280 if [ "$ventoy_compatible" = "NO" ]; then
281 find_wim_file (loop)
282 if [ -n "$ventoy_wim_file" ]; then
283 vt_windows_locate_wim $ventoy_wim_file
284 elif [ -n "${vtdebug_flag}" ]; then
285 echo No wim file found
286 fi
287 fi
288
289 vt_windows_chain_data ${1}${chosen_path}
290
291 if [ -n "${vtdebug_flag}" ]; then
292 sleep 5
293 fi
294
295 if [ -n "$vtoy_chain_mem_addr" ]; then
296 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
297 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
298 boot
299 else
300 echo "chain empty failed"
301 sleep 5
302 fi
303 }
304
305 function legacy_linux_menu_func {
306 if [ "$ventoy_compatible" = "NO" ]; then
307 vt_load_cpio $vtoy_path/ventoy.cpio $2 $1
308
309 vt_linux_clear_initrd
310
311 if [ -d (loop)/pmagic ]; then
312 vt_linux_specify_initrd_file /pmagic/initrd.img
313 else
314 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
315 if [ -d (loop)/$dir ]; then
316 vt_linux_parse_initrd_isolinux (loop)/$dir/
317 fi
318 done
319 fi
320
321 # special process for special distros
322 #archlinux
323 if [ -d (loop)/arch/boot/syslinux ]; then
324 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
325 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
326
327 #manjaro
328 elif [ -d (loop)/manjaro ]; then
329 if [ -e (loop)/boot/grub/kernels.cfg ]; then
330 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
331 fi
332 elif [ -e (loop)/boot/grub/grub.cfg ]; then
333 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
334 fi
335
336 distro_specify_initrd_file
337
338 vt_linux_initrd_count vtcount
339 if [ $vtcount -eq 0 ]; then
340 distro_specify_initrd_file_phase2
341 fi
342
343 locate_initrd
344 fi
345
346 vt_linux_chain_data ${1}${chosen_path}
347
348 if [ -n "${vtdebug_flag}" ]; then
349 sleep 5
350 fi
351
352 if [ -n "$vtoy_chain_mem_addr" ]; then
353 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
354 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
355 boot
356 else
357 echo "chain empty failed"
358 sleep 5
359 fi
360 }
361
362 function legacy_iso_menu_func {
363
364 if [ -d (loop)/ ]; then
365 loopback -d loop
366 fi
367
368 vt_chosen_img_path chosen_path
369
370 if vt_is_udf ${1}${chosen_path}; then
371 set ventoy_fs_probe=udf
372 else
373 set ventoy_fs_probe=iso9660
374 fi
375
376 loopback loop ${1}${chosen_path}
377
378 get_os_type (loop)
379
380 if [ -n "$vtcompat" ]; then
381 set ventoy_compatible=YES
382 unset vtcompat
383 elif vt_check_mode 1; then
384 set ventoy_compatible=YES
385 else
386 vt_check_compatible (loop)
387 fi
388
389 vt_img_sector ${1}${chosen_path}
390
391 if [ "$vtoy_os" = "Windows" ]; then
392 if [ "$ventoy_fs_probe" = "iso9660" ]; then
393 set ventoy_compatible=YES
394 elif [ -f (loop)/HBCD_PE.ini ]; then
395 set ventoy_compatible=YES
396 fi
397
398 legacy_windows_menu_func $1 ${chosen_path}
399 else
400 legacy_linux_menu_func $1 ${chosen_path}
401 fi
402 }
403
404 function legacy_iso_memdisk {
405 vt_chosen_img_path chosen_path
406
407 linux16 $vtoy_path/memdisk iso raw
408 echo "Loading ISO file to memory ..."
409 initrd16 ${1}${chosen_path}
410 boot
411 }
412
413 function common_menuentry {
414 if [ "$grub_platform" = "pc" ]; then
415 if vt_check_mode 0; then
416 legacy_iso_memdisk $iso_path
417 else
418 legacy_iso_menu_func $iso_path
419 fi
420 else
421 if vt_check_mode 0; then
422 uefi_iso_memdisk $iso_path
423 else
424 uefi_iso_menu_func $iso_path
425 fi
426 fi
427 }
428
429
430 #############################################################
431 #############################################################
432 #############################################################
433 ####### Main Process ###########
434 #############################################################
435 #############################################################
436 #############################################################
437
438 set VENTOY_VERSION="1.0.09"
439
440
441 # Default menu display mode, you can change it as you want.
442 # 0: List mode
443 # 1: TreeView mode
444 set VTOY_DEFAULT_MENU_MODE=0
445
446 #disable timeout
447 unset timeout
448
449 set VTOY_MEM_DISK_STR="MEMDISK"
450 set VTOY_ISO_RAW_STR="ISO RAW"
451 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
452
453 set VTOY_F2_CMD="ventoy_power"
454
455 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
456 set VTOY_F3_CMD="vt_dynamic_menu 1 1"
457 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:TreeView"
458 else
459 set VTOY_F3_CMD="vt_dynamic_menu 1 0"
460 set VTOY_HOTKEY_TIP="F1:Memdisk F2:Power F3:ListView"
461 fi
462
463 if [ "$grub_platform" = "pc" ]; then
464 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION BIOS www.ventoy.net"
465 else
466 set VTOY_TEXT_MENU_VER="Ventoy $VENTOY_VERSION UEFI www.ventoy.net"
467 fi
468
469 vt_device $root vtoy_dev
470
471 if [ "$vtoy_dev" = "tftp" ]; then
472 set vtoy_path=($root)
473 for vtid in 0 1 2 3; do
474 if [ -d (hd$vtid,2)/ventoy ]; then
475 set iso_path=(hd$vtid,1)
476 break
477 fi
478 done
479 else
480 set vtoy_path=($root)/ventoy
481 set iso_path=($vtoy_dev,1)
482 fi
483
484 loadfont ascii
485
486 if [ -f $iso_path/ventoy/ventoy.json ]; then
487 vt_load_plugin $iso_path
488 fi
489
490 if [ -n "$vtoy_gfxmode" ]; then
491 set gfxmode=$vtoy_gfxmode
492 else
493 set gfxmode=1920x1080,1366x768,1024x768
494 fi
495
496 if [ -n "$vtoy_theme" ]; then
497 set theme=$vtoy_theme
498 else
499 set theme=$prefix/themes/ventoy/theme.txt
500 fi
501
502 terminal_output gfxterm
503
504 #colect all image files (iso files)
505 set ventoy_img_count=0
506 vt_list_img $iso_path ventoy_img_count
507
508 #Main menu
509 if [ $ventoy_img_count -gt 0 ]; then
510 if [ $VTOY_DEFAULT_MENU_MODE -eq 0 ]; then
511 vt_dynamic_menu 0 0
512 else
513 vt_dynamic_menu 0 1
514 fi
515 else
516 menuentry "No ISO files found (Press enter to reboot ...)" {
517 echo -e "\n Rebooting ... "
518 reboot
519 }
520 fi