]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/grub/grub.cfg
Update README.md
[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 else
150 unset LoadIsoEfiDriver
151 fi
152
153 vt_chosen_img_path chosen_path
154
155 if vt_is_udf ${1}${chosen_path}; then
156 set ventoy_fs_probe=udf
157 else
158 set ventoy_fs_probe=iso9660
159 fi
160
161 loopback loop ${1}${chosen_path}
162 get_os_type (loop)
163
164 vt_check_compatible (loop)
165
166 vt_img_sector ${1}${chosen_path}
167
168 if [ "$vtoy_os" = "Windows" ]; then
169 if [ "$ventoy_fs_probe" = "iso9660" ]; then
170 set ventoy_compatible=YES
171 fi
172
173 uefi_windows_menu_func $1
174 else
175 uefi_linux_menu_func $1
176 fi
177
178 terminal_output gfxterm
179 }
180
181 function legacy_windows_menu_func {
182 vt_windows_reset
183
184 if [ "$ventoy_compatible" = "NO" ]; then
185 find_wim_file (loop)
186 if [ -n "$ventoy_wim_file" ]; then
187 vt_windows_locate_wim $ventoy_wim_file
188 elif [ -n "${vtdebug_flag}" ]; then
189 echo No wim file found
190 fi
191 fi
192
193 vt_windows_chain_data ${1}${chosen_path}
194
195 if [ -n "${vtdebug_flag}" ]; then
196 sleep 5
197 fi
198
199 if [ -n "$vtoy_chain_mem_addr" ]; then
200 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft
201 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
202 boot
203 else
204 echo "chain empty failed"
205 sleep 5
206 fi
207 }
208
209 function legacy_linux_menu_func {
210 if [ "$ventoy_compatible" = "NO" ]; then
211
212 vt_load_cpio $vtoy_path/ventoy.cpio
213
214 vt_linux_clear_initrd
215
216 for dir in "isolinux" "boot/isolinux" "boot/x86_64/loader" "syslinux" "boot/syslinux"; do
217 if [ -d (loop)/$dir ]; then
218 vt_linux_parse_initrd_isolinux (loop)/$dir/
219 fi
220 done
221
222 vt_linux_initrd_count initrd_count
223
224 # special process for special distros
225 if vt_cmp $initrd_count eq 0; then
226 #archlinux
227 if [ -d (loop)/arch/boot/syslinux ]; then
228 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/
229 vt_linux_parse_initrd_isolinux (loop)/arch/boot/syslinux/ /arch/boot/syslinux/
230
231 #manjaro
232 elif [ -d (loop)/manjaro ]; then
233 if [ -e (loop)/boot/grub/kernels.cfg ]; then
234 vt_linux_parse_initrd_grub file (loop)/boot/grub/kernels.cfg
235 fi
236 elif [ -e (loop)/boot/grub/grub.cfg ]; then
237 vt_linux_parse_initrd_grub file (loop)/boot/grub/grub.cfg
238 fi
239 fi
240
241 vt_linux_initrd_count initrd_count
242 if vt_cmp $initrd_count eq 0; then
243 distro_specify_initrd_file
244 fi
245
246 locate_initrd
247 fi
248
249 vt_linux_chain_data ${1}${chosen_path}
250
251 if [ -n "${vtdebug_flag}" ]; then
252 sleep 5
253 fi
254
255 if [ -n "$vtoy_chain_mem_addr" ]; then
256 linux16 $vtoy_path/ipxe.krn ${vtdebug_flag}
257 initrd16 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
258 boot
259 else
260 echo "chain empty failed"
261 sleep 5
262 fi
263 }
264
265 function legacy_iso_menu_func {
266
267 if [ -d (loop)/ ]; then
268 loopback -d loop
269 fi
270
271 vt_chosen_img_path chosen_path
272
273 if vt_is_udf ${1}${chosen_path}; then
274 set ventoy_fs_probe=udf
275 else
276 set ventoy_fs_probe=iso9660
277 fi
278
279 loopback loop ${1}${chosen_path}
280
281 get_os_type (loop)
282
283 vt_check_compatible (loop)
284
285 vt_img_sector ${1}${chosen_path}
286
287 if [ "$vtoy_os" = "Windows" ]; then
288 if [ "$ventoy_fs_probe" = "iso9660" ]; then
289 set ventoy_compatible=YES
290 fi
291 legacy_windows_menu_func $1
292 else
293 legacy_linux_menu_func $1
294 fi
295 }
296
297
298
299
300
301 #############################################################
302 #############################################################
303 #############################################################
304 ####### Main Process ###########
305 #############################################################
306 #############################################################
307 #############################################################
308
309 set VENTOY_VERSION="1.0.02"
310
311 #disable timeout
312 unset timeout
313
314 vt_device $root vtoy_dev
315
316 if [ "$vtoy_dev" = "tftp" ]; then
317 set vtoy_path=($root)
318 for vtid in 0 1 2 3; do
319 if [ -d (hd$vtid,2)/grub ]; then
320 set iso_path=(hd$vtid,1)
321 break
322 fi
323 done
324 else
325 set vtoy_path=($root)/ventoy
326 set iso_path=($vtoy_dev,1)
327 fi
328
329 loadfont ascii
330
331 if [ -f $iso_path/ventoy/ventoy.json ]; then
332 vt_load_plugin $iso_path
333 fi
334
335 terminal_output gfxterm
336
337 if [ -n "$vtoy_theme" ]; then
338 set theme=$vtoy_theme
339 else
340 set theme=$prefix/themes/ventoy/theme.txt
341 fi
342
343 if [ -n "$vtoy_gfxmode" ]; then
344 set gfxmode=$vtoy_gfxmode
345 else
346 set gfxmode=1024x768
347 fi
348
349 #colect all image files (iso files)
350 set ventoy_img_count=0
351 vt_list_img $iso_path ventoy_img_count
352
353 #Dynamic menu for every iso file
354 if vt_cmp $ventoy_img_count ne 0; then
355 set imgid=0
356 while vt_cmp $imgid lt $ventoy_img_count; do
357 vt_img_name $imgid img_name
358 menuentry "$img_name" {
359 if [ "$grub_platform" = "pc" ]; then
360 legacy_iso_menu_func $iso_path
361 else
362 uefi_iso_menu_func $iso_path
363 fi
364 }
365
366 vt_incr imgid 1
367 done
368 else
369 menuentry "No ISO files found (Press enter to reboot ...)" {
370 echo -e "\n Rebooting ... "
371 reboot
372 }
373 fi
374