]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_browser.c
c083f19268cad82162a783c38564f63c1b1bcb9a
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / ventoy / ventoy_browser.c
1 /******************************************************************************
2 * ventoy_browser.c
3 *
4 * Copyright (c) 2022, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #include <grub/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/disk.h>
26 #include <grub/device.h>
27 #include <grub/term.h>
28 #include <grub/partition.h>
29 #include <grub/file.h>
30 #include <grub/normal.h>
31 #include <grub/extcmd.h>
32 #include <grub/datetime.h>
33 #include <grub/i18n.h>
34 #include <grub/net.h>
35 #include <grub/time.h>
36 #include <grub/ventoy.h>
37 #include "ventoy_def.h"
38
39 GRUB_MOD_LICENSE ("GPLv3+");
40
41 #define BROWSER_MENU_BUF 65536
42
43 static const char *g_vtoy_dev = NULL;
44 static grub_fs_t g_menu_fs = NULL;
45 static char *g_menu_device = NULL;
46 static grub_device_t g_menu_dev = NULL;
47 static char g_menu_path_buf[1024];
48 static int g_menu_path_len = 0;
49 static browser_node *g_browser_list = NULL;
50
51 static int ventoy_browser_strcmp(char *str1, char *str2)
52 {
53 char *s1, *s2;
54 int c1 = 0;
55 int c2 = 0;
56
57 for (s1 = str1, s2 = str2; *s1 && *s2; s1++, s2++)
58 {
59 c1 = *s1;
60 c2 = *s2;
61
62 if (0 == g_sort_case_sensitive)
63 {
64 if (grub_islower(c1))
65 {
66 c1 = c1 - 'a' + 'A';
67 }
68
69 if (grub_islower(c2))
70 {
71 c2 = c2 - 'a' + 'A';
72 }
73 }
74
75 if (c1 != c2)
76 {
77 break;
78 }
79 }
80
81 return (c1 - c2);
82 }
83
84 static int ventoy_browser_mbuf_alloc(browser_mbuf *mbuf)
85 {
86 grub_memset(mbuf, 0, sizeof(browser_mbuf));
87 mbuf->buf = grub_malloc(BROWSER_MENU_BUF);
88 if (!mbuf->buf)
89 {
90 return 0;
91 }
92
93 mbuf->pos = 0;
94 mbuf->max = BROWSER_MENU_BUF;
95 return 1;
96 }
97
98 static inline void ventoy_browser_mbuf_free(browser_mbuf *mbuf)
99 {
100 if (mbuf)
101 grub_check_free(mbuf->buf)
102 }
103
104 static inline int ventoy_browser_mbuf_extend(browser_mbuf *mbuf)
105 {
106 if (mbuf->max - mbuf->pos <= VTOY_SIZE_1KB)
107 {
108 mbuf->max += BROWSER_MENU_BUF;
109 mbuf->buf = grub_realloc(mbuf->buf, mbuf->max);
110 }
111
112 return 0;
113 }
114
115 static browser_node * ventoy_browser_find_top_node(int dir)
116 {
117 browser_node *node = NULL;
118 browser_node *sel = NULL;
119
120 for (node = g_browser_list; node; node = node->next)
121 {
122 if (node->dir == dir)
123 {
124 if (sel)
125 {
126 if (ventoy_browser_strcmp(sel->filename, node->filename) > 0)
127 {
128 sel = node;
129 }
130 }
131 else
132 {
133 sel = node;
134 }
135 }
136 }
137
138 return sel;
139 }
140
141 static int ventoy_browser_iterate_partition(struct grub_disk *disk, const grub_partition_t partition, void *data)
142 {
143 char partname[64];
144 char title[256];
145 grub_device_t dev;
146 grub_fs_t fs;
147 char *Label = NULL;
148 browser_mbuf *mbuf = (browser_mbuf *)data;
149
150 (void)data;
151
152 if (partition->number < 2 && g_vtoy_dev && grub_strcmp(disk->name, g_vtoy_dev) == 0)
153 {
154 return 0;
155 }
156
157 grub_snprintf(partname, sizeof(partname) - 1, "%s,%d", disk->name, partition->number + 1);
158
159 dev = grub_device_open(partname);
160 if (!dev)
161 {
162 return 0;
163 }
164
165 fs = grub_fs_probe(dev);
166 if (!fs)
167 {
168 grub_device_close(dev);
169 return 0;
170 }
171
172 fs->fs_label(dev, &Label);
173
174 grub_snprintf(title, sizeof(title), "%-10s (%s,%s%d) [%s] %s %s",
175 "DISK", disk->name, partition->msdostype == 0xee ? "gpt" : "msdos",
176 partition->number + 1, (Label ? Label : ""), fs->name,
177 grub_get_human_size(partition->len << disk->log_sector_size, GRUB_HUMAN_SIZE_SHORT));
178
179 if (ventoy_get_fs_type(fs->name) >= ventoy_fs_max)
180 {
181 browser_ssprintf(mbuf, "menuentry \"%s\" --class=vtoydisk {\n"
182 " echo \"unsupported file system type!\" \n"
183 " ventoy_pause\n"
184 "}\n",
185 title);
186 }
187 else
188 {
189 browser_ssprintf(mbuf, "menuentry \"%s\" --class=vtoydisk {\n"
190 " vt_browser_dir %s,%d 0x%lx /\n"
191 "}\n",
192 title, disk->name, partition->number + 1, (ulong)fs);
193 }
194
195 ventoy_browser_mbuf_extend(mbuf);
196
197 return 0;
198 }
199
200 static int ventoy_browser_iterate_disk(const char *name, void *data)
201 {
202 grub_disk_t disk;
203
204 if (name[0] != 'h')
205 {
206 return 0;
207 }
208
209 disk = grub_disk_open(name);
210 if (disk)
211 {
212 grub_partition_iterate(disk, ventoy_browser_iterate_partition, data);
213 grub_disk_close(disk);
214 }
215
216 return 0;
217 }
218
219 static int ventoy_browser_valid_dirname(const char *name, int len)
220 {
221 if ((len == 1 && name[0] == '.') ||
222 (len == 2 && name[0] == '.' && name[1] == '.'))
223 {
224 return 0;
225 }
226
227 if (!ventoy_img_name_valid(name, len))
228 {
229 return 0;
230 }
231
232 if (name[0] == '$')
233 {
234 if (0 == grub_strncmp(name, "$RECYCLE.BIN", 12) ||
235 0 == grub_strncasecmp(name, "$Extend", 7))
236 {
237 return 0;
238 }
239 }
240
241 if (len == 25 && grub_strncmp(name, "System Volume Information", 25) == 0)
242 {
243 return 0;
244 }
245
246 return 1;
247 }
248
249 static int ventoy_browser_valid_filename(const char *filename, int len, int *type)
250 {
251 if (len < 4)
252 {
253 return 0;
254 }
255
256 if (FILE_FLT(ISO) && 0 == grub_strcasecmp(filename + len - 4, ".iso"))
257 {
258 *type = img_type_iso;
259 }
260 else if (FILE_FLT(WIM) && g_wimboot_enable && (0 == grub_strcasecmp(filename + len - 4, ".wim")))
261 {
262 *type = img_type_wim;
263 }
264 else if (FILE_FLT(VHD) && g_vhdboot_enable && (0 == grub_strcasecmp(filename + len - 4, ".vhd") ||
265 (len >= 5 && 0 == grub_strcasecmp(filename + len - 5, ".vhdx"))))
266 {
267 *type = img_type_vhd;
268 }
269 #ifdef GRUB_MACHINE_EFI
270 else if (FILE_FLT(EFI) && 0 == grub_strcasecmp(filename + len - 4, ".efi"))
271 {
272 *type = img_type_efi;
273 }
274 #endif
275 else if (FILE_FLT(IMG) && 0 == grub_strcasecmp(filename + len - 4, ".img"))
276 {
277 if (len == 18 && grub_strncmp(filename, "ventoy_", 7) == 0)
278 {
279 if (grub_strncmp(filename + 7, "wimboot", 7) == 0 ||
280 grub_strncmp(filename + 7, "vhdboot", 7) == 0)
281 {
282 return 0;
283 }
284 }
285 *type = img_type_img;
286 }
287 else if (FILE_FLT(VTOY) && len >= 5 && 0 == grub_strcasecmp(filename + len - 5, ".vtoy"))
288 {
289 *type = img_type_vtoy;
290 }
291 else
292 {
293 return 0;
294 }
295
296 if (g_filt_dot_underscore_file && filename[0] == '.' && filename[1] == '_')
297 {
298 return 0;
299 }
300
301 return 1;
302 }
303
304 static int ventoy_browser_iterate_dir(const char *filename, const struct grub_dirhook_info *info, void *data)
305 {
306 int type;
307 int len;
308 browser_node *node;
309
310 (void)data;
311
312 len = grub_strlen(filename);
313
314 if (info->dir)
315 {
316 if (!ventoy_browser_valid_dirname(filename, len))
317 {
318 return 0;
319 }
320
321 node = grub_zalloc(sizeof(browser_node));
322 if (!node)
323 {
324 return 0;
325 }
326
327 node->dir = 1;
328 grub_strncpy(node->filename, filename, sizeof(node->filename));
329 grub_snprintf(node->menuentry, sizeof(node->menuentry),
330 "menuentry \"%-10s [%s]\" --class=vtoydir {\n"
331 " vt_browser_dir %s 0x%lx \"%s/%s\"\n"
332 "}\n",
333 "DIR", filename, g_menu_device, (ulong)g_menu_fs, g_menu_path_buf, filename);
334 }
335 else
336 {
337 grub_uint64_t fsize = info->size;
338
339 if (!ventoy_browser_valid_filename(filename, len, &type))
340 {
341 return 0;
342 }
343
344 node = grub_zalloc(sizeof(browser_node));
345 if (!node)
346 {
347 return 0;
348 }
349
350 if (fsize == 0)
351 {
352 struct grub_file file;
353
354 grub_memset(&file, 0, sizeof(file));
355 file.device = g_menu_dev;
356 grub_snprintf(node->menuentry, sizeof(node->menuentry), "%s/%s", g_menu_path_buf, filename);
357 if (g_menu_fs->fs_open(&file, node->menuentry) == GRUB_ERR_NONE)
358 {
359 fsize = file.size;
360 g_menu_fs->fs_close(&file);
361 }
362 }
363
364 node->dir = 0;
365 grub_strncpy(node->filename, filename, sizeof(node->filename));
366 grub_snprintf(node->menuentry, sizeof(node->menuentry),
367 "menuentry \"%-10s %s\" --class=%s {\n"
368 " vt_set_fake_vlnk \"(%s)%s/%s\" %s %llu\n"
369 " %s_common_menuentry\n"
370 " vt_reset_fake_vlnk\n"
371 "}\n",
372 grub_get_human_size(fsize, GRUB_HUMAN_SIZE_SHORT), filename, g_menu_class[type],
373 g_menu_device, g_menu_path_buf, filename, g_menu_prefix[type], (ulonglong)fsize,
374 g_menu_prefix[type]);
375 }
376
377 node->prev = NULL;
378 node->next = g_browser_list;
379 if (g_browser_list)
380 {
381 g_browser_list->prev = node;
382 }
383 g_browser_list = node;
384
385 return 0;
386 }
387
388 grub_err_t ventoy_cmd_browser_dir(grub_extcmd_context_t ctxt, int argc, char **args)
389 {
390 int i;
391 grub_fs_t fs;
392 grub_device_t dev;
393 char cfgfile[64];
394 browser_node *node;
395 browser_mbuf mbuf;
396
397 (void)ctxt;
398 (void)argc;
399
400 if (!ventoy_browser_mbuf_alloc(&mbuf))
401 {
402 return 1;
403 }
404
405 fs = (grub_fs_t)grub_strtoul(args[1], NULL, 16);
406 if (!fs)
407 {
408 debug("Invalid fs %s\n", args[1]);
409 return 1;
410 }
411
412 dev = grub_device_open(args[0]);
413 if (!dev)
414 {
415 debug("Failed to open device %s\n", args[0]);
416 return 1;
417 }
418
419 g_menu_fs = fs;
420 g_menu_device = args[0];
421 g_menu_dev = dev;
422 g_browser_list = NULL;
423
424 if (args[2][0] == '/' && args[2][1] == 0)
425 {
426 g_menu_path_len = 0;
427 g_menu_path_buf[0] = 0;
428 fs->fs_dir(dev, "/", ventoy_browser_iterate_dir, NULL);
429 }
430 else
431 {
432 g_menu_path_len = grub_snprintf(g_menu_path_buf, sizeof(g_menu_path_buf), "%s", args[2]);
433 fs->fs_dir(dev, g_menu_path_buf, ventoy_browser_iterate_dir, NULL);
434 }
435 grub_device_close(dev);
436
437 browser_ssprintf(&mbuf, "menuentry \"%-10s [../]\" --class=\"vtoyret\" VTOY_RET {\n "
438 " echo 'return ...' \n}\n", "<--");
439
440 for (i = 1; i >= 0; i--)
441 {
442 while (1)
443 {
444 node = ventoy_browser_find_top_node(i);
445 if (node)
446 {
447 browser_ssprintf(&mbuf, "%s", node->menuentry);
448 ventoy_browser_mbuf_extend(&mbuf);
449
450 if (node->prev)
451 {
452 node->prev->next = node->next;
453 }
454 if (node->next)
455 {
456 node->next->prev = node->prev;
457 }
458
459 if (node == g_browser_list)
460 {
461 g_browser_list = node->next;
462 }
463 grub_free(node);
464 }
465 else
466 {
467 break;
468 }
469 }
470 }
471 g_browser_list = NULL;
472
473 grub_snprintf(cfgfile, sizeof(cfgfile), "configfile mem:0x%lx:size:%d", (ulong)mbuf.buf, mbuf.pos);
474 grub_script_execute_sourcecode(cfgfile);
475
476 ventoy_browser_mbuf_free(&mbuf);
477 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
478 }
479
480 grub_err_t ventoy_cmd_browser_disk(grub_extcmd_context_t ctxt, int argc, char **args)
481 {
482 char cfgfile[64];
483 browser_mbuf mbuf;
484
485 (void)ctxt;
486 (void)argc;
487 (void)args;
488
489 if (!ventoy_browser_mbuf_alloc(&mbuf))
490 {
491 return 1;
492 }
493
494 g_vtoy_dev = grub_env_get("vtoydev");
495
496 browser_ssprintf(&mbuf, "menuentry \"%-10s [Return]\" --class=\"vtoyret\" VTOY_RET {\n "
497 " echo 'return ...' \n}\n", "<--");
498
499 grub_disk_dev_iterate(ventoy_browser_iterate_disk, &mbuf);
500
501 grub_snprintf(cfgfile, sizeof(cfgfile), "configfile mem:0x%lx:size:%d", (ulong)mbuf.buf, mbuf.pos);
502 grub_script_execute_sourcecode(cfgfile);
503
504 ventoy_browser_mbuf_free(&mbuf);
505 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
506 }
507