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