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