1 /******************************************************************************
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
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.
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.
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/>.
20 #include <grub/types.h>
21 #include <grub/misc.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>
35 #include <grub/time.h>
36 #include <grub/font.h>
37 #include <grub/ventoy.h>
38 #include "ventoy_def.h"
40 GRUB_MOD_LICENSE ("GPLv3+");
42 static char g_iso_disk_name
[128];
43 static install_template
*g_install_template_head
= NULL
;
44 static persistence_config
*g_persistence_head
= NULL
;
45 static menu_alias
*g_menu_alias_head
= NULL
;
46 static menu_class
*g_menu_class_head
= NULL
;
47 static injection_config
*g_injection_head
= NULL
;
49 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
52 VTOY_JSON
*pNode
= NULL
;
53 VTOY_JSON
*pChild
= NULL
;
57 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
59 grub_printf("Not array type %d\n", json
->enDataType
);
63 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
65 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
67 pChild
= pNode
->pstChild
;
68 if (pChild
->enDataType
== JSON_TYPE_STRING
)
70 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
74 grub_printf("%s is NOT string type\n", pChild
->pcName
);
80 grub_printf("%s is not an object\n", pNode
->pcName
);
88 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
90 VTOY_JSON
*pNode
= NULL
;
91 VTOY_JSON
*pChild
= NULL
;
95 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
97 debug("Not array %d\n", json
->enDataType
);
101 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
103 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
105 pChild
= pNode
->pstChild
;
106 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
108 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
116 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
122 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
125 grub_printf("file: %s\n", value
);
128 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
132 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
137 grub_printf("Theme file %s does NOT exist\n", value
);
142 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
145 grub_printf("gfxmode: %s\n", value
);
148 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
151 grub_printf("display_mode: %s\n", value
);
154 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
157 grub_printf("ventoy_left: %s\n", value
);
160 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
163 grub_printf("ventoy_top: %s\n", value
);
166 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
169 grub_printf("ventoy_color: %s\n", value
);
172 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
175 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
177 if (node
->enDataType
== JSON_TYPE_STRING
)
179 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
181 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
185 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
192 grub_printf("fonts NOT found\n");
198 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
204 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
209 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
213 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
216 if (ventoy_is_file_exist(filepath
) == 0)
218 debug("Theme file %s does not exist\n", filepath
);
222 debug("vtoy_theme %s\n", filepath
);
223 grub_env_set("vtoy_theme", filepath
);
226 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
229 debug("vtoy_gfxmode %s\n", value
);
230 grub_env_set("vtoy_gfxmode", value
);
233 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
236 debug("display_mode %s\n", value
);
237 grub_env_set("vtoy_display_mode", value
);
240 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
243 grub_env_set("VTLE_LFT", value
);
246 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
249 grub_env_set("VTLE_TOP", value
);
252 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
255 grub_env_set("VTLE_CLR", value
);
258 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
261 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
263 if (node
->enDataType
== JSON_TYPE_STRING
&&
264 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
266 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
267 grub_font_load(filepath
);
275 static int ventoy_plugin_check_path(const char *path
, const char *file
)
279 grub_printf("%s is NOT begin with '/' \n", file
);
283 if (grub_strchr(file
, '\\'))
285 grub_printf("%s contains invalid '\\' \n", file
);
289 if (grub_strstr(file
, "//"))
291 grub_printf("%s contains invalid double slash\n", file
);
295 if (grub_strstr(file
, "../"))
297 grub_printf("%s contains invalid '../' \n", file
);
301 if (!ventoy_is_file_exist("%s%s", path
, file
))
303 grub_printf("%s%s does NOT exist\n", path
, file
);
310 static int ventoy_plugin_check_fullpath
321 VTOY_JSON
*node
= json
;
322 VTOY_JSON
*child
= NULL
;
326 if (0 == grub_strcmp(key
, node
->pcName
))
330 node
= node
->pstNext
;
338 if (JSON_TYPE_STRING
== node
->enDataType
)
341 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
342 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
344 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
346 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
348 if (JSON_TYPE_STRING
!= child
->enDataType
)
350 grub_printf("Non string json type\n");
354 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
355 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
366 static int ventoy_plugin_parse_fullpath
371 file_fullpath
**fullpath
,
377 VTOY_JSON
*node
= json
;
378 VTOY_JSON
*child
= NULL
;
379 file_fullpath
*path
= NULL
;
383 if (0 == grub_strcmp(key
, node
->pcName
))
387 node
= node
->pstNext
;
395 if (JSON_TYPE_STRING
== node
->enDataType
)
397 debug("%s is string type data\n", node
->pcName
);
399 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
401 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
405 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
408 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
414 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
416 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
418 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
420 debug("Invalid data type:%d\n", child
->enDataType
);
425 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
427 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
432 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
434 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
436 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
450 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
454 const char *iso
= NULL
;
455 VTOY_JSON
*pNode
= NULL
;
457 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
459 grub_printf("Not array type %d\n", json
->enDataType
);
463 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
465 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
467 grub_printf("NOT object type\n");
470 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
473 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
475 grub_printf("image: %s [OK]\n", iso
);
476 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
478 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
480 if (autosel
>= 0 && autosel
<= pathnum
)
482 grub_printf("autosel: %d [OK]\n", autosel
);
486 grub_printf("autosel: %d [FAIL]\n", autosel
);
492 grub_printf("image: %s [FAIL]\n", iso
);
497 grub_printf("image not found\n");
504 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
508 const char *iso
= NULL
;
509 VTOY_JSON
*pNode
= NULL
;
510 install_template
*node
= NULL
;
511 install_template
*next
= NULL
;
512 file_fullpath
*templatepath
= NULL
;
514 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
516 debug("Not array %d\n", json
->enDataType
);
520 if (g_install_template_head
)
522 for (node
= g_install_template_head
; node
; node
= next
)
525 grub_check_free(node
->templatepath
);
529 g_install_template_head
= NULL
;
532 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
534 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
535 if (iso
&& iso
[0] == '/')
537 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
539 node
= grub_zalloc(sizeof(install_template
));
542 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
543 node
->templatepath
= templatepath
;
544 node
->templatenum
= pathnum
;
547 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
549 if (autosel
>= 0 && autosel
<= pathnum
)
551 node
->autosel
= autosel
;
555 if (g_install_template_head
)
557 node
->next
= g_install_template_head
;
560 g_install_template_head
= node
;
569 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
573 const char *iso
= NULL
;
574 VTOY_JSON
*pNode
= NULL
;
576 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
578 grub_printf("Not array type %d\n", json
->enDataType
);
582 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
584 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
586 grub_printf("NOT object type\n");
589 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
592 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
594 grub_printf("image: %s [OK]\n", iso
);
595 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
597 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
599 if (autosel
>= 0 && autosel
<= pathnum
)
601 grub_printf("autosel: %d [OK]\n", autosel
);
605 grub_printf("autosel: %d [FAIL]\n", autosel
);
611 grub_printf("image: %s [FAIL]\n", iso
);
616 grub_printf("image not found\n");
623 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
627 const char *iso
= NULL
;
628 VTOY_JSON
*pNode
= NULL
;
629 persistence_config
*node
= NULL
;
630 persistence_config
*next
= NULL
;
631 file_fullpath
*backendpath
= NULL
;
635 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
637 debug("Not array %d\n", json
->enDataType
);
641 if (g_persistence_head
)
643 for (node
= g_persistence_head
; node
; node
= next
)
646 grub_check_free(node
->backendpath
);
650 g_persistence_head
= NULL
;
653 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
655 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
656 if (iso
&& iso
[0] == '/')
658 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
660 node
= grub_zalloc(sizeof(persistence_config
));
663 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
664 node
->backendpath
= backendpath
;
665 node
->backendnum
= pathnum
;
668 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
670 if (autosel
>= 0 && autosel
<= pathnum
)
672 node
->autosel
= autosel
;
676 if (g_persistence_head
)
678 node
->next
= g_persistence_head
;
681 g_persistence_head
= node
;
690 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
693 const char *path
= NULL
;
694 const char *alias
= NULL
;
695 VTOY_JSON
*pNode
= NULL
;
699 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
701 grub_printf("Not array %d\n", json
->enDataType
);
705 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
707 type
= vtoy_alias_image_file
;
708 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
711 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
712 type
= vtoy_alias_directory
;
715 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
716 if (path
&& path
[0] == '/' && alias
)
718 if (vtoy_alias_image_file
== type
)
720 if (ventoy_is_file_exist("%s%s", isodisk
, path
))
722 grub_printf("image: <%s> [ OK ]\n", path
);
726 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
731 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
733 grub_printf("dir: <%s> [ OK ]\n", path
);
737 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
741 grub_printf("alias: <%s>\n\n", alias
);
748 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
751 const char *path
= NULL
;
752 const char *alias
= NULL
;
753 VTOY_JSON
*pNode
= NULL
;
754 menu_alias
*node
= NULL
;
755 menu_alias
*next
= NULL
;
759 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
761 debug("Not array %d\n", json
->enDataType
);
765 if (g_menu_alias_head
)
767 for (node
= g_menu_alias_head
; node
; node
= next
)
773 g_menu_alias_head
= NULL
;
776 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
778 type
= vtoy_alias_image_file
;
779 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
782 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
783 type
= vtoy_alias_directory
;
786 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
787 if (path
&& path
[0] == '/' && alias
)
789 node
= grub_zalloc(sizeof(menu_alias
));
793 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
794 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
796 if (g_menu_alias_head
)
798 node
->next
= g_menu_alias_head
;
801 g_menu_alias_head
= node
;
810 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
812 const char *path
= NULL
;
813 const char *archive
= NULL
;
814 VTOY_JSON
*pNode
= NULL
;
818 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
820 grub_printf("Not array %d\n", json
->enDataType
);
824 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
826 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
829 grub_printf("image not found\n");
833 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
836 grub_printf("archive not found\n");
840 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
841 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
847 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
849 const char *path
= NULL
;
850 const char *archive
= NULL
;
851 VTOY_JSON
*pNode
= NULL
;
852 injection_config
*node
= NULL
;
853 injection_config
*next
= NULL
;
857 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
859 debug("Not array %d\n", json
->enDataType
);
863 if (g_injection_head
)
865 for (node
= g_injection_head
; node
; node
= next
)
871 g_injection_head
= NULL
;
874 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
876 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
877 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
878 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
880 node
= grub_zalloc(sizeof(injection_config
));
883 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
884 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
886 if (g_injection_head
)
888 node
->next
= g_injection_head
;
891 g_injection_head
= node
;
899 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
902 const char *key
= NULL
;
903 const char *class = NULL
;
904 VTOY_JSON
*pNode
= NULL
;
905 menu_class
*tail
= NULL
;
906 menu_class
*node
= NULL
;
907 menu_class
*next
= NULL
;
911 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
913 debug("Not array %d\n", json
->enDataType
);
917 if (g_menu_class_head
)
919 for (node
= g_menu_class_head
; node
; node
= next
)
925 g_menu_class_head
= NULL
;
928 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
930 type
= vtoy_class_image_file
;
931 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
934 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
935 type
= vtoy_class_directory
;
938 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
941 node
= grub_zalloc(sizeof(menu_class
));
945 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
946 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
948 if (g_menu_class_head
)
954 g_menu_class_head
= node
;
964 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
967 const char *key
= NULL
;
968 const char *class = NULL
;
969 VTOY_JSON
*pNode
= NULL
;
973 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
975 grub_printf("Not array %d\n", json
->enDataType
);
979 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
981 type
= vtoy_class_image_file
;
982 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
985 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
986 type
= vtoy_class_directory
;
989 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
992 grub_printf("%s: <%s>\n", (type
== vtoy_class_directory
) ? "dir" : "key", key
);
993 grub_printf("class: <%s>\n\n", class);
1000 static plugin_entry g_plugin_entries
[] =
1002 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
1003 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
1004 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
1005 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
1006 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
1007 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
1008 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
},
1011 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
1014 VTOY_JSON
*cur
= json
;
1016 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
1020 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1022 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
1024 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
1025 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
1036 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1041 VTOY_JSON
*json
= NULL
;
1046 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
1049 return GRUB_ERR_NONE
;
1052 debug("json configuration file size %d\n", (int)file
->size
);
1054 buf
= grub_malloc(file
->size
+ 1);
1057 grub_file_close(file
);
1061 buf
[file
->size
] = 0;
1062 grub_file_read(file
, buf
, file
->size
);
1063 grub_file_close(file
);
1065 json
= vtoy_json_create();
1073 ret
= vtoy_json_parse(json
, buf
);
1076 debug("Failed to parse json string %d\n", ret
);
1081 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
1083 vtoy_json_destroy(json
);
1087 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
1090 void ventoy_plugin_dump_auto_install(void)
1093 install_template
*node
= NULL
;
1095 for (node
= g_install_template_head
; node
; node
= node
->next
)
1097 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
1098 for (i
= 0; i
< node
->templatenum
; i
++)
1100 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
1107 void ventoy_plugin_dump_persistence(void)
1111 persistence_config
*node
= NULL
;
1112 ventoy_img_chunk_list chunk_list
;
1114 for (node
= g_persistence_head
; node
; node
= node
->next
)
1116 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
1118 for (i
= 0; i
< node
->backendnum
; i
++)
1120 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
1121 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
1124 grub_printf(" [ SUCCESS ]\n");
1125 grub_free(chunk_list
.chunk
);
1129 grub_printf(" [ FAILED ]\n");
1137 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
1139 install_template
*node
= NULL
;
1140 int len
= (int)grub_strlen(isopath
);
1142 for (node
= g_install_template_head
; node
; node
= node
->next
)
1144 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1153 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
1155 install_template
*node
= NULL
;
1157 node
= ventoy_plugin_find_install_template(isopath
);
1158 if ((!node
) || (!node
->templatepath
))
1163 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
1168 return node
->templatepath
[node
->cursel
].path
;
1171 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
1173 persistence_config
*node
= NULL
;
1174 int len
= (int)grub_strlen(isopath
);
1176 for (node
= g_persistence_head
; node
; node
= node
->next
)
1178 if ((len
== node
->pathlen
) && (grub_strcmp(node
->isopath
, isopath
) == 0))
1187 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
1190 grub_uint64_t start
= 0;
1191 grub_file_t file
= NULL
;
1192 persistence_config
*node
= NULL
;
1194 node
= ventoy_plugin_find_persistent(isopath
);
1195 if ((!node
) || (!node
->backendpath
))
1202 index
= node
->cursel
;
1205 if (index
< 0 || index
>= node
->backendnum
)
1210 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
1213 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
1217 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
1218 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
1219 if (NULL
== chunk_list
->chunk
)
1224 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
1225 chunk_list
->cur_chunk
= 0;
1227 start
= file
->device
->disk
->partition
->start
;
1228 ventoy_get_block_list(file
, chunk_list
, start
);
1230 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
1232 grub_free(chunk_list
->chunk
);
1233 chunk_list
->chunk
= NULL
;
1241 grub_file_close(file
);
1246 const char * ventoy_plugin_get_injection(const char *isopath
)
1248 injection_config
*node
= NULL
;
1249 int len
= (int)grub_strlen(isopath
);
1251 for (node
= g_injection_head
; node
; node
= node
->next
)
1253 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1255 return node
->archive
;
1262 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
1264 menu_alias
*node
= NULL
;
1265 int len
= (int)grub_strlen(isopath
);
1267 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
1269 if (node
->type
== type
&& node
->pathlen
&&
1270 node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1279 const char * ventoy_plugin_get_menu_class(int type
, const char *name
)
1281 menu_class
*node
= NULL
;
1282 int len
= (int)grub_strlen(name
);
1284 if (vtoy_class_image_file
== type
)
1286 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1288 if (node
->type
== type
&& node
->patlen
<= len
&& grub_strstr(name
, node
->pattern
))
1296 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1298 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
1308 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1314 VTOY_JSON
*node
= NULL
;
1315 VTOY_JSON
*json
= NULL
;
1324 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
1327 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
1331 buf
= grub_malloc(file
->size
+ 1);
1334 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
1338 buf
[file
->size
] = 0;
1339 grub_file_read(file
, buf
, file
->size
);
1341 json
= vtoy_json_create();
1344 grub_printf("Failed to create json\n");
1348 ret
= vtoy_json_parse(json
, buf
);
1351 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
1355 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
1357 if (grub_strcmp(node
->pcName
, args
[1]) == 0)
1365 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
1369 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1371 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
1373 if (g_plugin_entries
[i
].checkfunc
)
1375 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
1382 check_free(file
, grub_file_close
);
1383 check_free(json
, vtoy_json_destroy
);
1384 grub_check_free(buf
);