]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_browser.c
01c7248e7d4c56bb6eb139b212a39521b3e5c317
[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 == 1 && 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 if (g_tree_view_menu_style == 0)
175 {
176 grub_snprintf(title, sizeof(title), "%-10s (%s,%s%d) [%s] %s %s",
177 "DISK", disk->name, partition->msdostype == 0xee ? "gpt" : "msdos",
178 partition->number + 1, (Label ? Label : ""), fs->name,
179 grub_get_human_size(partition->len << disk->log_sector_size, GRUB_HUMAN_SIZE_SHORT));
180 }
181 else
182 {
183 grub_snprintf(title, sizeof(title), "(%s,%s%d) [%s] %s %s",
184 disk->name, partition->msdostype == 0xee ? "gpt" : "msdos",
185 partition->number + 1, (Label ? Label : ""), fs->name,
186 grub_get_human_size(partition->len << disk->log_sector_size, GRUB_HUMAN_SIZE_SHORT));
187 }
188
189 if (ventoy_get_fs_type(fs->name) >= ventoy_fs_max)
190 {
191 browser_ssprintf(mbuf, "menuentry \"%s\" --class=vtoydisk {\n"
192 " echo \"unsupported file system type!\" \n"
193 " ventoy_pause\n"
194 "}\n",
195 title);
196 }
197 else
198 {
199 browser_ssprintf(mbuf, "menuentry \"%s\" --class=vtoydisk {\n"
200 " vt_browser_dir %s,%d 0x%lx /\n"
201 "}\n",
202 title, disk->name, partition->number + 1, (ulong)fs);
203 }
204
205 ventoy_browser_mbuf_extend(mbuf);
206
207 return 0;
208 }
209
210 static int ventoy_browser_iterate_disk(const char *name, void *data)
211 {
212 grub_disk_t disk;
213
214 if (name[0] != 'h')
215 {
216 return 0;
217 }
218
219 disk = grub_disk_open(name);
220 if (disk)
221 {
222 grub_partition_iterate(disk, ventoy_browser_iterate_partition, data);
223 grub_disk_close(disk);
224 }
225
226 return 0;
227 }
228
229 static int ventoy_browser_valid_dirname(const char *name, int len)
230 {
231 if ((len == 1 && name[0] == '.') ||
232 (len == 2 && name[0] == '.' && name[1] == '.'))
233 {
234 return 0;
235 }
236
237 if (!ventoy_img_name_valid(name, len))
238 {
239 return 0;
240 }
241
242 if (name[0] == '$')
243 {
244 if (0 == grub_strncmp(name, "$RECYCLE.BIN", 12) ||
245 0 == grub_strncasecmp(name, "$Extend", 7))
246 {
247 return 0;
248 }
249 }
250
251 if (len == 25 && grub_strncmp(name, "System Volume Information", 25) == 0)
252 {
253 return 0;
254 }
255
256 return 1;
257 }
258
259 static int ventoy_browser_valid_filename(const char *filename, int len, int *type)
260 {
261 if (len < 4)
262 {
263 return 0;
264 }
265
266 if (FILE_FLT(ISO) && 0 == grub_strcasecmp(filename + len - 4, ".iso"))
267 {
268 *type = img_type_iso;
269 }
270 else if (FILE_FLT(WIM) && g_wimboot_enable && (0 == grub_strcasecmp(filename + len - 4, ".wim")))
271 {
272 *type = img_type_wim;
273 }
274 else if (FILE_FLT(VHD) && g_vhdboot_enable && (0 == grub_strcasecmp(filename + len - 4, ".vhd") ||
275 (len >= 5 && 0 == grub_strcasecmp(filename + len - 5, ".vhdx"))))
276 {
277 *type = img_type_vhd;
278 }
279 #ifdef GRUB_MACHINE_EFI
280 else if (FILE_FLT(EFI) && 0 == grub_strcasecmp(filename + len - 4, ".efi"))
281 {
282 *type = img_type_efi;
283 }
284 #endif
285 else if (FILE_FLT(IMG) && 0 == grub_strcasecmp(filename + len - 4, ".img"))
286 {
287 if (len == 18 && grub_strncmp(filename, "ventoy_", 7) == 0)
288 {
289 if (grub_strncmp(filename + 7, "wimboot", 7) == 0 ||
290 grub_strncmp(filename + 7, "vhdboot", 7) == 0)
291 {
292 return 0;
293 }
294 }
295 *type = img_type_img;
296 }
297 else if (FILE_FLT(VTOY) && len >= 5 && 0 == grub_strcasecmp(filename + len - 5, ".vtoy"))
298 {
299 *type = img_type_vtoy;
300 }
301 else
302 {
303 return 0;
304 }
305
306 if (g_filt_dot_underscore_file && filename[0] == '.' && filename[1] == '_')
307 {
308 return 0;
309 }
310
311 return 1;
312 }
313
314 static int ventoy_browser_iterate_dir(const char *filename, const struct grub_dirhook_info *info, void *data)
315 {
316 int type;
317 int len;
318 browser_node *node;
319
320 (void)data;
321
322 len = grub_strlen(filename);
323
324 if (info->dir)
325 {
326 if (!ventoy_browser_valid_dirname(filename, len))
327 {
328 return 0;
329 }
330
331 node = grub_zalloc(sizeof(browser_node));
332 if (!node)
333 {
334 return 0;
335 }
336
337 node->dir = 1;
338 grub_strncpy(node->filename, filename, sizeof(node->filename));
339
340 if (g_tree_view_menu_style == 0)
341 {
342 grub_snprintf(node->menuentry, sizeof(node->menuentry),
343 "menuentry \"%-10s [%s]\" --class=vtoydir {\n"
344 " vt_browser_dir %s 0x%lx \"%s/%s\"\n"
345 "}\n",
346 "DIR", filename, g_menu_device, (ulong)g_menu_fs, g_menu_path_buf, filename);
347 }
348 else
349 {
350 grub_snprintf(node->menuentry, sizeof(node->menuentry),
351 "menuentry \"[%s]\" --class=vtoydir {\n"
352 " vt_browser_dir %s 0x%lx \"%s/%s\"\n"
353 "}\n",
354 filename, g_menu_device, (ulong)g_menu_fs, g_menu_path_buf, filename);
355 }
356 }
357 else
358 {
359 grub_uint64_t fsize = info->size;
360
361 if (!ventoy_browser_valid_filename(filename, len, &type))
362 {
363 return 0;
364 }
365
366 node = grub_zalloc(sizeof(browser_node));
367 if (!node)
368 {
369 return 0;
370 }
371
372 if (fsize == 0)
373 {
374 struct grub_file file;
375
376 grub_memset(&file, 0, sizeof(file));
377 file.device = g_menu_dev;
378 grub_snprintf(node->menuentry, sizeof(node->menuentry), "%s/%s", g_menu_path_buf, filename);
379 if (g_menu_fs->fs_open(&file, node->menuentry) == GRUB_ERR_NONE)
380 {
381 fsize = file.size;
382 g_menu_fs->fs_close(&file);
383 }
384 }
385
386 node->dir = 0;
387 grub_strncpy(node->filename, filename, sizeof(node->filename));
388
389 if (g_tree_view_menu_style == 0)
390 {
391 grub_snprintf(node->menuentry, sizeof(node->menuentry),
392 "menuentry \"%-10s %s\" --class=%s {\n"
393 " vt_set_fake_vlnk \"(%s)%s/%s\" %s %llu\n"
394 " %s_common_menuentry\n"
395 " vt_reset_fake_vlnk\n"
396 "}\n",
397 grub_get_human_size(fsize, GRUB_HUMAN_SIZE_SHORT), filename, g_menu_class[type],
398 g_menu_device, g_menu_path_buf, filename, g_menu_prefix[type], (ulonglong)fsize,
399 g_menu_prefix[type]);
400 }
401 else
402 {
403 grub_snprintf(node->menuentry, sizeof(node->menuentry),
404 "menuentry \"%s\" --class=%s {\n"
405 " vt_set_fake_vlnk \"(%s)%s/%s\" %s %llu\n"
406 " %s_common_menuentry\n"
407 " vt_reset_fake_vlnk\n"
408 "}\n",
409 filename, g_menu_class[type],
410 g_menu_device, g_menu_path_buf, filename, g_menu_prefix[type], (ulonglong)fsize,
411 g_menu_prefix[type]);
412 }
413 }
414
415 node->prev = NULL;
416 node->next = g_browser_list;
417 if (g_browser_list)
418 {
419 g_browser_list->prev = node;
420 }
421 g_browser_list = node;
422
423 return 0;
424 }
425
426 static grub_err_t ventoy_browser_iso_part(void)
427 {
428 char cfgfile[64];
429 char *buffer = NULL;
430 int pos = 0;
431 int buflen = 0;
432 int cfglen = 0;
433
434 cfglen = g_tree_script_pos - g_tree_script_pre;
435 buflen = cfglen + 512;
436 buffer = grub_malloc(buflen);
437 if (!buffer)
438 {
439 return 1;
440 }
441
442 if (g_tree_view_menu_style == 0)
443 {
444 pos = grub_snprintf(buffer, buflen, "menuentry \"%-10s [../]\" --class=\"vtoyret\" VTOY_RET {\n "
445 " echo 'return ...' \n}\n", "<--");
446 }
447 else
448 {
449 pos = grub_snprintf(buffer, buflen, "menuentry \"[../]\" --class=\"vtoyret\" VTOY_RET {\n "
450 " echo 'return ...' \n}\n");
451 }
452
453 grub_memcpy(buffer + pos, g_tree_script_buf + g_tree_script_pre, cfglen);
454 pos += cfglen;
455
456 grub_snprintf(cfgfile, sizeof(cfgfile), "configfile mem:0x%lx:size:%d", (ulong)buffer, pos);
457 grub_script_execute_sourcecode(cfgfile);
458
459 grub_free(buffer);
460 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
461 }
462
463 grub_err_t ventoy_cmd_browser_dir(grub_extcmd_context_t ctxt, int argc, char **args)
464 {
465 int i;
466 grub_fs_t fs;
467 grub_device_t dev;
468 char cfgfile[64];
469 browser_node *node;
470 browser_mbuf mbuf;
471
472 (void)ctxt;
473 (void)argc;
474
475 if (args[2][0] == '/' && args[2][1] == 0)
476 {
477 grub_snprintf(cfgfile, sizeof(cfgfile), "(%s)", args[0]);
478 if (grub_strcmp(cfgfile, g_iso_path) == 0)
479 {
480 return ventoy_browser_iso_part();
481 }
482 }
483
484 if (!ventoy_browser_mbuf_alloc(&mbuf))
485 {
486 return 1;
487 }
488
489 fs = (grub_fs_t)grub_strtoul(args[1], NULL, 16);
490 if (!fs)
491 {
492 debug("Invalid fs %s\n", args[1]);
493 return 1;
494 }
495
496 dev = grub_device_open(args[0]);
497 if (!dev)
498 {
499 debug("Failed to open device %s\n", args[0]);
500 return 1;
501 }
502
503 g_menu_fs = fs;
504 g_menu_device = args[0];
505 g_menu_dev = dev;
506 g_browser_list = NULL;
507
508 if (args[2][0] == '/' && args[2][1] == 0)
509 {
510 g_menu_path_len = 0;
511 g_menu_path_buf[0] = 0;
512 fs->fs_dir(dev, "/", ventoy_browser_iterate_dir, NULL);
513 }
514 else
515 {
516 g_menu_path_len = grub_snprintf(g_menu_path_buf, sizeof(g_menu_path_buf), "%s", args[2]);
517 fs->fs_dir(dev, g_menu_path_buf, ventoy_browser_iterate_dir, NULL);
518 }
519 grub_device_close(dev);
520
521 if (g_tree_view_menu_style == 0)
522 {
523 browser_ssprintf(&mbuf, "menuentry \"%-10s [../]\" --class=\"vtoyret\" VTOY_RET {\n "
524 " echo 'return ...' \n}\n", "<--");
525 }
526 else
527 {
528 browser_ssprintf(&mbuf, "menuentry \"[../]\" --class=\"vtoyret\" VTOY_RET {\n "
529 " echo 'return ...' \n}\n");
530 }
531
532 for (i = 1; i >= 0; i--)
533 {
534 while (1)
535 {
536 node = ventoy_browser_find_top_node(i);
537 if (node)
538 {
539 browser_ssprintf(&mbuf, "%s", node->menuentry);
540 ventoy_browser_mbuf_extend(&mbuf);
541
542 if (node->prev)
543 {
544 node->prev->next = node->next;
545 }
546 if (node->next)
547 {
548 node->next->prev = node->prev;
549 }
550
551 if (node == g_browser_list)
552 {
553 g_browser_list = node->next;
554 }
555 grub_free(node);
556 }
557 else
558 {
559 break;
560 }
561 }
562 }
563 g_browser_list = NULL;
564
565 grub_snprintf(cfgfile, sizeof(cfgfile), "configfile mem:0x%lx:size:%d", (ulong)mbuf.buf, mbuf.pos);
566 grub_script_execute_sourcecode(cfgfile);
567
568 ventoy_browser_mbuf_free(&mbuf);
569 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
570 }
571
572 grub_err_t ventoy_cmd_browser_disk(grub_extcmd_context_t ctxt, int argc, char **args)
573 {
574 char cfgfile[64];
575 browser_mbuf mbuf;
576
577 (void)ctxt;
578 (void)argc;
579 (void)args;
580
581 if (!ventoy_browser_mbuf_alloc(&mbuf))
582 {
583 return 1;
584 }
585
586 g_vtoy_dev = grub_env_get("vtoydev");
587
588 if (g_tree_view_menu_style == 0)
589 {
590 browser_ssprintf(&mbuf, "menuentry \"%-10s [Return]\" --class=\"vtoyret\" VTOY_RET {\n "
591 " echo 'return ...' \n}\n", "<--");
592 }
593 else
594 {
595 browser_ssprintf(&mbuf, "menuentry \"[Return]\" --class=\"vtoyret\" VTOY_RET {\n "
596 " echo 'return ...' \n}\n");
597 }
598
599 grub_disk_dev_iterate(ventoy_browser_iterate_disk, &mbuf);
600
601 grub_snprintf(cfgfile, sizeof(cfgfile), "configfile mem:0x%lx:size:%d", (ulong)mbuf.buf, mbuf.pos);
602 grub_script_execute_sourcecode(cfgfile);
603
604 ventoy_browser_mbuf_free(&mbuf);
605 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
606 }
607