]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/grub/grub.cfg
1.0.05 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 fi
78 }
79
80 function uefi_windows_menu_func {
81 vt_windows_reset
82
83 if [ "$ventoy_compatible" = "NO" ]; then
84 find_wim_file (loop)
85 if [ -n "$ventoy_wim_file" ]; then
86 vt_windows_locate_wim $ventoy_wim_file
87 fi
88 fi
89
90 vt_windows_chain_data ${1}${chosen_path}
91
92 if [ -n "${vtdebug_flag}" ]; then
93 sleep 5
94 fi
95
96 if [ -n "$vtoy_chain_mem_addr" ]; then
97 terminal_output console
98 chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
99 boot
100 else
101 echo "chain empty failed"
102 sleep 5
103 fi
104 }
105
106 function uefi_linux_menu_func {
107 if [ "$ventoy_compatible" = "NO" ]; then
108 vt_load_cpio ${vtoy_path}/ventoy.cpio
109
110 vt_linux_clear_initrd
111
112 if [ -d (loop)/pmagic ]; then
113 vt_linux_specify_initrd_file /pmagic/initrd.img
114 else
115 for file in "boot/grub/grub.cfg" "EFI/BOOT/grub.cfg" "EFI/boot/grub.cfg" "efi/boot/grub.cfg" "EFI/BOOT/BOOTX64.conf"; do
116 if [ -e (loop)/$file ]; then
117 vt_linux_parse_initrd_grub file (loop)/$file
118 fi
119 done
120 fi
121
122 # special process for special distros
123 if [ -d (loop)/loader/entries ]; then
124 set LoadIsoEfiDriver=on
125 vt_linux_parse_initrd_grub dir (loop)/loader/entries/
126 elif [ -d (loop)/boot/grub ]; then
127 vt_linux_parse_initrd_grub dir (loop)/boot/grub/
128 fi
129
130 distro_specify_initrd_file
131
132 locate_initrd
133 fi
134
135 vt_linux_chain_data ${1}${chosen_path}
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
148 function uefi_iso_menu_func {
149
150 if [ -d (loop)/ ]; then
151 loopback -d loop
152 fi
153
154 if [ -n "$vtisouefi" ]; then
155 set LoadIsoEfiDriver=on
156 unset vtisouefi
157 elif [ -n "$VTOY_ISO_UEFI_DRV" ]; then
158 set LoadIsoEfiDriver=on
159 else
160 unset LoadIsoEfiDriver
161 fi
162
163 vt_chosen_img_path chosen_path
164
165 if vt_is_udf ${1}${chosen_path}; then
166 set ventoy_fs_probe=udf
167 else
168 set ventoy_fs_probe=iso9660
169 fi
170
171 loopback loop ${1}${chosen_path}
172 get_os_type (loop)
173
174 if [ -n "$vtcompat" ]; then
175 set ventoy_compatible=YES
176 unset vtcompat
177 elif [ -n "$VTOY_ISO_RAW" ]; then
178 set ventoy_compatible=YES
179 else
180 vt_check_compatible (loop)
181 fi
182
183 vt_img_sector ${1}${chosen_path}
184
185 if [ "$vtoy_os" = "Windows" ]; then
186 if [ "$ventoy_fs_probe" = "iso9660" ]; then
187 set ventoy_compatible=YES
188 fi
189
190 uefi_windows_menu_func $1
191 else
192 uefi_linux_menu_func $1
193 fi
194
195 terminal_output gfxterm
196 }
197
198 function uefi_iso_memdisk {
199 vt_chosen_img_path chosen_path
200
201 echo 'Loading ISO file to memory ...'
202 vt_load_iso_to_mem ${1}${chosen_path} vtoy_iso_buf
203
204 terminal_output console
205 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}
206 boot
207 }
208
209
210
211
212
213 function legacy_windows_menu_func {
214 vt_windows_reset
215
216 if [ "$ventoy_compatible" = "NO" ]; then
217 find_wim_file (loop)
218 if [ -n "$ventoy_wim_file" ]; then
219 vt_windows_locate_wim $ventoy_wim_file
220 elif [ -n "${vtdebug_flag}" ]; then
221 echo No wim file found
222 fi
223 fi
224
225 vt_windows_chain_data ${1}${chosen_path}
226
227 if [ -n "${vtdebug_flag}" ]; then
228 sleep 5
229 fi
230
231 if [ -n "$vtoy_chain_mem_addr" ]; then
232 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
233 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
234 boot
235 else
236 echo "chain empty failed"
237 sleep 5
238 fi
239 }
240
241 function legacy_linux_menu_func {
242 if [ "$ventoy_compatible" = "NO" ]; then
243
244 vt_load_cpio $vtoy_path/ventoy.cpio
245
246 vt_linux_clear_initrd
247
248 if [ -d (loop)/pmagic ]; then
249 vt_linux_specify_initrd_file /pmagic/initrd.img
250 else
251 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
252 if [ -d (loop)/$dir ]; then
253 vt_linux_parse_initrd_isolinux (loop)/$dir/
254 fi
255 done
256 fi
257
258 # special process for special distros
259 #archlinux
260 if [ -d (loop)/arch/boot/syslinux ]; then
261 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
262 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
263
264 #manjaro
265 elif [ -d (loop)/manjaro ]; then
266 if [ -e (loop)/boot/grub/kernels.cfg ]; then
267 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
268 fi
269 elif [ -e (loop)/boot/grub/grub.cfg ]; then
270 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
271 fi
272
273 distro_specify_initrd_file
274
275 locate_initrd
276 fi
277
278 vt_linux_chain_data ${1}${chosen_path}
279
280 if [ -n "${vtdebug_flag}" ]; then
281 sleep 5
282 fi
283
284 if [ -n "$vtoy_chain_mem_addr" ]; then
285 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
286 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
287 boot
288 else
289 echo "chain empty failed"
290 sleep 5
291 fi
292 }
293
294 function legacy_iso_menu_func {
295
296 if [ -d (loop)/ ]; then
297 loopback -d loop
298 fi
299
300 vt_chosen_img_path chosen_path
301
302 if vt_is_udf ${1}${chosen_path}; then
303 set ventoy_fs_probe=udf
304 else
305 set ventoy_fs_probe=iso9660
306 fi
307
308 loopback loop ${1}${chosen_path}
309
310 get_os_type (loop)
311
312 if [ -n "$vtcompat" ]; then
313 set ventoy_compatible=YES
314 unset vtcompat
315 elif [ -n "$VTOY_ISO_RAW" ]; then
316 set ventoy_compatible=YES
317 else
318 vt_check_compatible (loop)
319 fi
320
321 vt_img_sector ${1}${chosen_path}
322
323 if [ "$vtoy_os" = "Windows" ]; then
324 if [ "$ventoy_fs_probe" = "iso9660" ]; then
325 set ventoy_compatible=YES
326 fi
327 legacy_windows_menu_func $1
328 else
329 legacy_linux_menu_func $1
330 fi
331 }
332
333 function legacy_iso_memdisk {
334 vt_chosen_img_path chosen_path
335
336 linux16 $vtoy_path/memdisk iso raw
337 echo "Loading ISO file to memory ..."
338 initrd16 ${1}${chosen_path}
339 boot
340 }
341
342
343 #############################################################
344 #############################################################
345 #############################################################
346 ####### Main Process ###########
347 #############################################################
348 #############################################################
349 #############################################################
350
351 set VENTOY_VERSION="1.0.05"
352
353 #disable timeout
354 unset timeout
355
356 set VTOY_MEM_DISK_STR="MEMDISK"
357 set VTOY_ISO_RAW_STR="ISO RAW"
358 set VTOY_ISO_UEFI_DRV_STR="UEFI FS"
359
360 vt_device $root vtoy_dev
361
362 if [ "$vtoy_dev" = "tftp" ]; then
363 set vtoy_path=($root)
364 for vtid in 0 1 2 3; do
365 if [ -d (hd$vtid,2)/ventoy ]; then
366 set iso_path=(hd$vtid,1)
367 break
368 fi
369 done
370 else
371 set vtoy_path=($root)/ventoy
372 set iso_path=($vtoy_dev,1)
373 fi
374
375 loadfont ascii
376
377 if [ -f $iso_path/ventoy/ventoy.json ]; then
378 vt_load_plugin $iso_path
379 fi
380
381 terminal_output gfxterm
382
383 if [ -n "$vtoy_theme" ]; then
384 set theme=$vtoy_theme
385 else
386 set theme=$prefix/themes/ventoy/theme.txt
387 fi
388
389 if [ -n "$vtoy_gfxmode" ]; then
390 set gfxmode=$vtoy_gfxmode
391 else
392 set gfxmode=1024x768
393 fi
394
395 #colect all image files (iso files)
396 set ventoy_img_count=0
397 vt_list_img $iso_path ventoy_img_count
398
399 #Dynamic menu for every iso file
400 if vt_cmp $ventoy_img_count ne 0; then
401 set imgid=0
402 while vt_cmp $imgid lt $ventoy_img_count; do
403 vt_img_name $imgid img_name
404 menuentry "$img_name" {
405 if [ "$grub_platform" = "pc" ]; then
406 if [ -n "$VTOY_MEM_DISK" ]; then
407 legacy_iso_memdisk $iso_path
408 else
409 legacy_iso_menu_func $iso_path
410 fi
411 else
412 if [ -n "$VTOY_MEM_DISK" ]; then
413 uefi_iso_memdisk $iso_path
414 else
415 uefi_iso_menu_func $iso_path
416 fi
417 fi
418 }
419
420 vt_incr imgid 1
421 done
422 else
423 menuentry "No ISO files found (Press enter to reboot ...)" {
424 echo -e "\n Rebooting ... "
425 reboot
426 }
427 fi
428