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