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
;
48 static auto_memdisk
*g_auto_memdisk_head
= NULL
;
49 static image_list
*g_image_list_head
= NULL
;
51 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
54 VTOY_JSON
*pNode
= NULL
;
55 VTOY_JSON
*pChild
= NULL
;
59 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
61 grub_printf("Not array type %d\n", json
->enDataType
);
65 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
67 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
69 pChild
= pNode
->pstChild
;
70 if (pChild
->enDataType
== JSON_TYPE_STRING
)
72 if (grub_strcmp(pChild
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
74 grub_printf("%s: %s [%s]\n", pChild
->pcName
, pChild
->unData
.pcStrVal
,
75 ventoy_check_file_exist("%s%s", isodisk
, pChild
->unData
.pcStrVal
) ? "OK" : "NOT EXIST");
79 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
84 grub_printf("%s is NOT string type\n", pChild
->pcName
);
90 grub_printf("%s is not an object\n", pNode
->pcName
);
98 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
100 VTOY_JSON
*pNode
= NULL
;
101 VTOY_JSON
*pChild
= NULL
;
105 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
107 debug("Not array %d\n", json
->enDataType
);
111 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
113 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
115 pChild
= pNode
->pstChild
;
116 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
118 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
126 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
132 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
135 grub_printf("file: %s\n", value
);
138 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
142 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
147 grub_printf("Theme file %s does NOT exist\n", value
);
152 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
155 grub_printf("gfxmode: %s\n", value
);
158 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
161 grub_printf("display_mode: %s\n", value
);
164 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
167 grub_printf("serial_param %s\n", value
);
170 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
173 grub_printf("ventoy_left: %s\n", value
);
176 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
179 grub_printf("ventoy_top: %s\n", value
);
182 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
185 grub_printf("ventoy_color: %s\n", value
);
188 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
191 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
193 if (node
->enDataType
== JSON_TYPE_STRING
)
195 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
197 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
201 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
208 grub_printf("fonts NOT found\n");
214 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
220 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
225 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
229 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
232 if (ventoy_is_file_exist(filepath
) == 0)
234 debug("Theme file %s does not exist\n", filepath
);
238 debug("vtoy_theme %s\n", filepath
);
239 grub_env_set("vtoy_theme", filepath
);
242 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
245 debug("vtoy_gfxmode %s\n", value
);
246 grub_env_set("vtoy_gfxmode", value
);
249 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
252 debug("display_mode %s\n", value
);
253 grub_env_set("vtoy_display_mode", value
);
256 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
259 debug("serial_param %s\n", value
);
260 grub_env_set("vtoy_serial_param", value
);
263 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
266 grub_env_set("VTLE_LFT", value
);
269 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
272 grub_env_set("VTLE_TOP", value
);
275 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
278 grub_env_set("VTLE_CLR", value
);
281 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
284 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
286 if (node
->enDataType
== JSON_TYPE_STRING
&&
287 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
289 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
290 grub_font_load(filepath
);
298 static int ventoy_plugin_check_path(const char *path
, const char *file
)
302 grub_printf("%s is NOT begin with '/' \n", file
);
306 if (grub_strchr(file
, '\\'))
308 grub_printf("%s contains invalid '\\' \n", file
);
312 if (grub_strstr(file
, "//"))
314 grub_printf("%s contains invalid double slash\n", file
);
318 if (grub_strstr(file
, "../"))
320 grub_printf("%s contains invalid '../' \n", file
);
324 if (!ventoy_is_file_exist("%s%s", path
, file
))
326 grub_printf("%s%s does NOT exist\n", path
, file
);
333 static int ventoy_plugin_check_fullpath
344 VTOY_JSON
*node
= json
;
345 VTOY_JSON
*child
= NULL
;
349 if (0 == grub_strcmp(key
, node
->pcName
))
353 node
= node
->pstNext
;
361 if (JSON_TYPE_STRING
== node
->enDataType
)
364 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
365 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
367 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
369 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
371 if (JSON_TYPE_STRING
!= child
->enDataType
)
373 grub_printf("Non string json type\n");
377 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
378 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
389 static int ventoy_plugin_parse_fullpath
394 file_fullpath
**fullpath
,
400 VTOY_JSON
*node
= json
;
401 VTOY_JSON
*child
= NULL
;
402 file_fullpath
*path
= NULL
;
406 if (0 == grub_strcmp(key
, node
->pcName
))
410 node
= node
->pstNext
;
418 if (JSON_TYPE_STRING
== node
->enDataType
)
420 debug("%s is string type data\n", node
->pcName
);
422 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
424 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
428 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
431 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
437 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
439 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
441 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
443 debug("Invalid data type:%d\n", child
->enDataType
);
448 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
450 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
455 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
457 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
459 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
473 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
477 const char *iso
= NULL
;
478 VTOY_JSON
*pNode
= NULL
;
480 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
482 grub_printf("Not array type %d\n", json
->enDataType
);
486 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
488 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
490 grub_printf("NOT object type\n");
493 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
496 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
498 grub_printf("image: %s [OK]\n", iso
);
499 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
501 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
503 if (autosel
>= 0 && autosel
<= pathnum
)
505 grub_printf("autosel: %d [OK]\n", autosel
);
509 grub_printf("autosel: %d [FAIL]\n", autosel
);
515 grub_printf("image: %s [FAIL]\n", iso
);
520 grub_printf("image not found\n");
527 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
531 const char *iso
= NULL
;
532 VTOY_JSON
*pNode
= NULL
;
533 install_template
*node
= NULL
;
534 install_template
*next
= NULL
;
535 file_fullpath
*templatepath
= NULL
;
537 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
539 debug("Not array %d\n", json
->enDataType
);
543 if (g_install_template_head
)
545 for (node
= g_install_template_head
; node
; node
= next
)
548 grub_check_free(node
->templatepath
);
552 g_install_template_head
= NULL
;
555 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
557 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
558 if (iso
&& iso
[0] == '/')
560 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
562 node
= grub_zalloc(sizeof(install_template
));
565 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
566 node
->templatepath
= templatepath
;
567 node
->templatenum
= pathnum
;
570 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
572 if (autosel
>= 0 && autosel
<= pathnum
)
574 node
->autosel
= autosel
;
578 if (g_install_template_head
)
580 node
->next
= g_install_template_head
;
583 g_install_template_head
= node
;
592 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
596 const char *iso
= NULL
;
597 VTOY_JSON
*pNode
= NULL
;
599 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
601 grub_printf("Not array type %d\n", json
->enDataType
);
605 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
607 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
609 grub_printf("NOT object type\n");
612 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
615 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
617 grub_printf("image: %s [OK]\n", iso
);
618 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
620 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
622 if (autosel
>= 0 && autosel
<= pathnum
)
624 grub_printf("autosel: %d [OK]\n", autosel
);
628 grub_printf("autosel: %d [FAIL]\n", autosel
);
634 grub_printf("image: %s [FAIL]\n", iso
);
639 grub_printf("image not found\n");
646 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
650 const char *iso
= NULL
;
651 VTOY_JSON
*pNode
= NULL
;
652 persistence_config
*node
= NULL
;
653 persistence_config
*next
= NULL
;
654 file_fullpath
*backendpath
= NULL
;
658 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
660 debug("Not array %d\n", json
->enDataType
);
664 if (g_persistence_head
)
666 for (node
= g_persistence_head
; node
; node
= next
)
669 grub_check_free(node
->backendpath
);
673 g_persistence_head
= NULL
;
676 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
678 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
679 if (iso
&& iso
[0] == '/')
681 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
683 node
= grub_zalloc(sizeof(persistence_config
));
686 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
687 node
->backendpath
= backendpath
;
688 node
->backendnum
= pathnum
;
691 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
693 if (autosel
>= 0 && autosel
<= pathnum
)
695 node
->autosel
= autosel
;
699 if (g_persistence_head
)
701 node
->next
= g_persistence_head
;
704 g_persistence_head
= node
;
713 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
716 const char *path
= NULL
;
717 const char *alias
= NULL
;
718 VTOY_JSON
*pNode
= NULL
;
722 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
724 grub_printf("Not array %d\n", json
->enDataType
);
728 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
730 type
= vtoy_alias_image_file
;
731 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
734 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
735 type
= vtoy_alias_directory
;
738 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
739 if (path
&& path
[0] == '/' && alias
)
741 if (vtoy_alias_image_file
== type
)
743 if (ventoy_is_file_exist("%s%s", isodisk
, path
))
745 grub_printf("image: <%s> [ OK ]\n", path
);
749 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
754 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
756 grub_printf("dir: <%s> [ OK ]\n", path
);
760 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
764 grub_printf("alias: <%s>\n\n", alias
);
771 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
774 const char *path
= NULL
;
775 const char *alias
= NULL
;
776 VTOY_JSON
*pNode
= NULL
;
777 menu_alias
*node
= NULL
;
778 menu_alias
*next
= NULL
;
782 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
784 debug("Not array %d\n", json
->enDataType
);
788 if (g_menu_alias_head
)
790 for (node
= g_menu_alias_head
; node
; node
= next
)
796 g_menu_alias_head
= NULL
;
799 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
801 type
= vtoy_alias_image_file
;
802 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
805 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
806 type
= vtoy_alias_directory
;
809 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
810 if (path
&& path
[0] == '/' && alias
)
812 node
= grub_zalloc(sizeof(menu_alias
));
816 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
817 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
819 if (g_menu_alias_head
)
821 node
->next
= g_menu_alias_head
;
824 g_menu_alias_head
= node
;
833 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
835 const char *path
= NULL
;
836 const char *archive
= NULL
;
837 VTOY_JSON
*pNode
= NULL
;
841 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
843 grub_printf("Not array %d\n", json
->enDataType
);
847 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
849 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
852 grub_printf("image not found\n");
856 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
859 grub_printf("archive not found\n");
863 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
864 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
870 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
872 const char *path
= NULL
;
873 const char *archive
= NULL
;
874 VTOY_JSON
*pNode
= NULL
;
875 injection_config
*node
= NULL
;
876 injection_config
*next
= NULL
;
880 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
882 debug("Not array %d\n", json
->enDataType
);
886 if (g_injection_head
)
888 for (node
= g_injection_head
; node
; node
= next
)
894 g_injection_head
= NULL
;
897 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
899 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
900 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
901 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
903 node
= grub_zalloc(sizeof(injection_config
));
906 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
907 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
909 if (g_injection_head
)
911 node
->next
= g_injection_head
;
914 g_injection_head
= node
;
922 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
925 const char *key
= NULL
;
926 const char *class = NULL
;
927 VTOY_JSON
*pNode
= NULL
;
928 menu_class
*tail
= NULL
;
929 menu_class
*node
= NULL
;
930 menu_class
*next
= NULL
;
934 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
936 debug("Not array %d\n", json
->enDataType
);
940 if (g_menu_class_head
)
942 for (node
= g_menu_class_head
; node
; node
= next
)
948 g_menu_class_head
= NULL
;
951 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
953 type
= vtoy_class_image_file
;
954 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
957 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
958 type
= vtoy_class_directory
;
961 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
964 node
= grub_zalloc(sizeof(menu_class
));
968 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
969 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
971 if (g_menu_class_head
)
977 g_menu_class_head
= node
;
987 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
990 const char *key
= NULL
;
991 const char *class = NULL
;
992 VTOY_JSON
*pNode
= NULL
;
996 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
998 grub_printf("Not array %d\n", json
->enDataType
);
1002 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1004 type
= vtoy_class_image_file
;
1005 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1008 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1009 type
= vtoy_class_directory
;
1012 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1015 grub_printf("%s: <%s>\n", (type
== vtoy_class_directory
) ? "dir" : "key", key
);
1016 grub_printf("class: <%s>\n\n", class);
1023 static int ventoy_plugin_auto_memdisk_entry(VTOY_JSON
*json
, const char *isodisk
)
1025 VTOY_JSON
*pNode
= NULL
;
1026 auto_memdisk
*node
= NULL
;
1027 auto_memdisk
*next
= NULL
;
1031 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1033 debug("Not array %d\n", json
->enDataType
);
1037 if (g_auto_memdisk_head
)
1039 for (node
= g_auto_memdisk_head
; node
; node
= next
)
1045 g_auto_memdisk_head
= NULL
;
1048 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1050 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1052 node
= grub_zalloc(sizeof(auto_memdisk
));
1055 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1057 if (g_auto_memdisk_head
)
1059 node
->next
= g_auto_memdisk_head
;
1062 g_auto_memdisk_head
= node
;
1070 static int ventoy_plugin_auto_memdisk_check(VTOY_JSON
*json
, const char *isodisk
)
1072 VTOY_JSON
*pNode
= NULL
;
1074 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1076 grub_printf("Not array %d\n", json
->enDataType
);
1080 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1082 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1084 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1086 if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1088 grub_printf(" [OK]\n");
1092 grub_printf(" [NOT EXIST]\n");
1100 static int ventoy_plugin_image_list_entry(VTOY_JSON
*json
, const char *isodisk
)
1102 VTOY_JSON
*pNode
= NULL
;
1103 image_list
*node
= NULL
;
1104 image_list
*next
= NULL
;
1108 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1110 debug("Not array %d\n", json
->enDataType
);
1114 if (g_image_list_head
)
1116 for (node
= g_image_list_head
; node
; node
= next
)
1122 g_image_list_head
= NULL
;
1125 g_plugin_image_list
= 1;
1127 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1129 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1131 node
= grub_zalloc(sizeof(image_list
));
1134 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1136 if (g_image_list_head
)
1138 node
->next
= g_image_list_head
;
1141 g_image_list_head
= node
;
1149 static int ventoy_plugin_image_list_check(VTOY_JSON
*json
, const char *isodisk
)
1151 VTOY_JSON
*pNode
= NULL
;
1153 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1155 grub_printf("Not array %d\n", json
->enDataType
);
1159 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1161 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1163 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1165 if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1167 grub_printf(" [OK]\n");
1171 grub_printf(" [NOT EXIST]\n");
1179 static plugin_entry g_plugin_entries
[] =
1181 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
1182 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
1183 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
1184 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
1185 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
1186 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
1187 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
},
1188 { "auto_memdisk", ventoy_plugin_auto_memdisk_entry
, ventoy_plugin_auto_memdisk_check
},
1189 { "image_list", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1192 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
1195 VTOY_JSON
*cur
= json
;
1197 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
1201 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1203 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
1205 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
1206 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
1217 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1222 VTOY_JSON
*json
= NULL
;
1227 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
1230 return GRUB_ERR_NONE
;
1233 debug("json configuration file size %d\n", (int)file
->size
);
1235 buf
= grub_malloc(file
->size
+ 1);
1238 grub_file_close(file
);
1242 buf
[file
->size
] = 0;
1243 grub_file_read(file
, buf
, file
->size
);
1244 grub_file_close(file
);
1246 json
= vtoy_json_create();
1254 ret
= vtoy_json_parse(json
, buf
);
1257 debug("Failed to parse json string %d\n", ret
);
1262 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
1264 vtoy_json_destroy(json
);
1268 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
1271 void ventoy_plugin_dump_injection(void)
1273 injection_config
*node
= NULL
;
1275 for (node
= g_injection_head
; node
; node
= node
->next
)
1277 grub_printf("\nIMAGE:<%s>\n", node
->isopath
);
1278 grub_printf("ARCHIVE:<%s>\n", node
->archive
);
1285 void ventoy_plugin_dump_auto_install(void)
1288 install_template
*node
= NULL
;
1290 for (node
= g_install_template_head
; node
; node
= node
->next
)
1292 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
1293 for (i
= 0; i
< node
->templatenum
; i
++)
1295 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
1302 void ventoy_plugin_dump_persistence(void)
1306 persistence_config
*node
= NULL
;
1307 ventoy_img_chunk_list chunk_list
;
1309 for (node
= g_persistence_head
; node
; node
= node
->next
)
1311 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
1313 for (i
= 0; i
< node
->backendnum
; i
++)
1315 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
1316 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
1319 grub_printf(" [ SUCCESS ]\n");
1320 grub_free(chunk_list
.chunk
);
1324 grub_printf(" [ FAILED ]\n");
1332 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
1335 install_template
*node
= NULL
;
1337 if (!g_install_template_head
)
1342 len
= (int)grub_strlen(isopath
);
1343 for (node
= g_install_template_head
; node
; node
= node
->next
)
1345 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1354 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
1356 install_template
*node
= NULL
;
1358 node
= ventoy_plugin_find_install_template(isopath
);
1359 if ((!node
) || (!node
->templatepath
))
1364 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
1369 return node
->templatepath
[node
->cursel
].path
;
1372 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
1375 persistence_config
*node
= NULL
;
1377 if (!g_persistence_head
)
1382 len
= (int)grub_strlen(isopath
);
1383 for (node
= g_persistence_head
; node
; node
= node
->next
)
1385 if ((len
== node
->pathlen
) && (grub_strcmp(node
->isopath
, isopath
) == 0))
1394 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
1397 grub_uint64_t start
= 0;
1398 grub_file_t file
= NULL
;
1399 persistence_config
*node
= NULL
;
1401 node
= ventoy_plugin_find_persistent(isopath
);
1402 if ((!node
) || (!node
->backendpath
))
1409 index
= node
->cursel
;
1412 if (index
< 0 || index
>= node
->backendnum
)
1417 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
1420 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
1424 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
1425 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
1426 if (NULL
== chunk_list
->chunk
)
1431 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
1432 chunk_list
->cur_chunk
= 0;
1434 start
= file
->device
->disk
->partition
->start
;
1435 ventoy_get_block_list(file
, chunk_list
, start
);
1437 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
1439 grub_free(chunk_list
->chunk
);
1440 chunk_list
->chunk
= NULL
;
1448 grub_file_close(file
);
1453 const char * ventoy_plugin_get_injection(const char *isopath
)
1456 injection_config
*node
= NULL
;
1458 if (!g_injection_head
)
1463 len
= (int)grub_strlen(isopath
);
1464 for (node
= g_injection_head
; node
; node
= node
->next
)
1466 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1468 return node
->archive
;
1475 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
1478 menu_alias
*node
= NULL
;
1480 if (!g_menu_alias_head
)
1485 len
= (int)grub_strlen(isopath
);
1486 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
1488 if (node
->type
== type
&& node
->pathlen
&&
1489 node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1498 const char * ventoy_plugin_get_menu_class(int type
, const char *name
)
1501 menu_class
*node
= NULL
;
1503 if (!g_menu_class_head
)
1508 len
= (int)grub_strlen(name
);
1510 if (vtoy_class_image_file
== type
)
1512 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1514 if (node
->type
== type
&& node
->patlen
<= len
&& grub_strstr(name
, node
->pattern
))
1522 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1524 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
1534 int ventoy_plugin_check_memdisk(const char *isopath
)
1537 auto_memdisk
*node
= NULL
;
1539 if (!g_auto_memdisk_head
)
1544 len
= (int)grub_strlen(isopath
);
1545 for (node
= g_auto_memdisk_head
; node
; node
= node
->next
)
1547 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->isopath
, len
) == 0)
1556 int ventoy_plugin_check_image_list(const char *isopath
)
1559 image_list
*node
= NULL
;
1561 if (!g_image_list_head
)
1566 len
= (int)grub_strlen(isopath
);
1567 for (node
= g_image_list_head
; node
; node
= node
->next
)
1569 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->isopath
, len
) == 0)
1578 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1584 VTOY_JSON
*node
= NULL
;
1585 VTOY_JSON
*json
= NULL
;
1594 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
1597 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
1601 buf
= grub_malloc(file
->size
+ 1);
1604 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
1608 buf
[file
->size
] = 0;
1609 grub_file_read(file
, buf
, file
->size
);
1611 json
= vtoy_json_create();
1614 grub_printf("Failed to create json\n");
1618 ret
= vtoy_json_parse(json
, buf
);
1621 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
1625 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
1627 if (grub_strcmp(node
->pcName
, args
[1]) == 0)
1635 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
1639 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1641 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
1643 if (g_plugin_entries
[i
].checkfunc
)
1645 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
1652 check_free(file
, grub_file_close
);
1653 check_free(json
, vtoy_json_destroy
);
1654 grub_check_free(buf
);