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
;
48 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
51 VTOY_JSON
*pNode
= NULL
;
52 VTOY_JSON
*pChild
= NULL
;
56 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
58 grub_printf("Not array type %d\n", json
->enDataType
);
62 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
64 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
66 pChild
= pNode
->pstChild
;
67 if (pChild
->enDataType
== JSON_TYPE_STRING
)
69 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
73 grub_printf("%s is NOT string type\n", pChild
->pcName
);
79 grub_printf("%s is not an object\n", pNode
->pcName
);
87 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
89 VTOY_JSON
*pNode
= NULL
;
90 VTOY_JSON
*pChild
= NULL
;
94 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
96 debug("Not array %d\n", json
->enDataType
);
100 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
102 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
104 pChild
= pNode
->pstChild
;
105 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
107 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
115 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
121 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
124 grub_printf("file: %s\n", value
);
127 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
131 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
136 grub_printf("Theme file %s does NOT exist\n", value
);
141 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
144 grub_printf("gfxmode: %s\n", value
);
147 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
150 grub_printf("display_mode: %s\n", value
);
153 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
156 grub_printf("ventoy_left: %s\n", value
);
159 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
162 grub_printf("ventoy_top: %s\n", value
);
165 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
168 grub_printf("ventoy_color: %s\n", value
);
171 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
174 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
176 if (node
->enDataType
== JSON_TYPE_STRING
)
178 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
180 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
184 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
191 grub_printf("fonts NOT found\n");
197 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
203 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
208 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
212 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
215 if (ventoy_is_file_exist(filepath
) == 0)
217 debug("Theme file %s does not exist\n", filepath
);
221 debug("vtoy_theme %s\n", filepath
);
222 grub_env_set("vtoy_theme", filepath
);
225 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
228 debug("vtoy_gfxmode %s\n", value
);
229 grub_env_set("vtoy_gfxmode", value
);
232 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
235 debug("display_mode %s\n", value
);
236 grub_env_set("vtoy_display_mode", value
);
239 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
242 grub_env_set("VTLE_LFT", value
);
245 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
248 grub_env_set("VTLE_TOP", value
);
251 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
254 grub_env_set("VTLE_CLR", value
);
257 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
260 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
262 if (node
->enDataType
== JSON_TYPE_STRING
&&
263 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
265 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
266 grub_font_load(filepath
);
274 static int ventoy_plugin_check_path(const char *path
, const char *file
)
278 grub_printf("%s is NOT begin with '/' \n", file
);
282 if (grub_strchr(file
, '\\'))
284 grub_printf("%s contains invalid '\\' \n", file
);
288 if (grub_strstr(file
, "//"))
290 grub_printf("%s contains invalid double slash\n", file
);
294 if (grub_strstr(file
, "../"))
296 grub_printf("%s contains invalid '../' \n", file
);
300 if (!ventoy_is_file_exist("%s%s", path
, file
))
302 grub_printf("%s%s does NOT exist\n", path
, file
);
309 static int ventoy_plugin_check_fullpath
320 VTOY_JSON
*node
= json
;
321 VTOY_JSON
*child
= NULL
;
325 if (0 == grub_strcmp(key
, node
->pcName
))
329 node
= node
->pstNext
;
337 if (JSON_TYPE_STRING
== node
->enDataType
)
340 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
341 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
343 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
345 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
347 if (JSON_TYPE_STRING
!= child
->enDataType
)
349 grub_printf("Non string json type\n");
353 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
354 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
365 static int ventoy_plugin_parse_fullpath
370 file_fullpath
**fullpath
,
376 VTOY_JSON
*node
= json
;
377 VTOY_JSON
*child
= NULL
;
378 file_fullpath
*path
= NULL
;
382 if (0 == grub_strcmp(key
, node
->pcName
))
386 node
= node
->pstNext
;
394 if (JSON_TYPE_STRING
== node
->enDataType
)
396 debug("%s is string type data\n", node
->pcName
);
398 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
400 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
404 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
407 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
413 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
415 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
417 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
419 debug("Invalid data type:%d\n", child
->enDataType
);
424 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
426 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
431 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
433 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
435 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
449 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
453 const char *iso
= NULL
;
454 VTOY_JSON
*pNode
= NULL
;
456 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
458 grub_printf("Not array type %d\n", json
->enDataType
);
462 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
464 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
466 grub_printf("NOT object type\n");
469 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
472 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
474 grub_printf("image: %s [OK]\n", iso
);
475 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
477 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
479 if (autosel
>= 0 && autosel
<= pathnum
)
481 grub_printf("autosel: %d [OK]\n", autosel
);
485 grub_printf("autosel: %d [FAIL]\n", autosel
);
491 grub_printf("image: %s [FAIL]\n", iso
);
496 grub_printf("image not found\n");
503 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
507 const char *iso
= NULL
;
508 VTOY_JSON
*pNode
= NULL
;
509 install_template
*node
= NULL
;
510 install_template
*next
= NULL
;
511 file_fullpath
*templatepath
= NULL
;
513 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
515 debug("Not array %d\n", json
->enDataType
);
519 if (g_install_template_head
)
521 for (node
= g_install_template_head
; node
; node
= next
)
524 grub_check_free(node
->templatepath
);
528 g_install_template_head
= NULL
;
531 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
533 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
534 if (iso
&& iso
[0] == '/')
536 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
538 node
= grub_zalloc(sizeof(install_template
));
541 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
542 node
->templatepath
= templatepath
;
543 node
->templatenum
= pathnum
;
546 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
548 if (autosel
>= 0 && autosel
<= pathnum
)
550 node
->autosel
= autosel
;
554 if (g_install_template_head
)
556 node
->next
= g_install_template_head
;
559 g_install_template_head
= node
;
568 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
572 const char *iso
= NULL
;
573 VTOY_JSON
*pNode
= NULL
;
575 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
577 grub_printf("Not array type %d\n", json
->enDataType
);
581 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
583 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
585 grub_printf("NOT object type\n");
588 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
591 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
593 grub_printf("image: %s [OK]\n", iso
);
594 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
596 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
598 if (autosel
>= 0 && autosel
<= pathnum
)
600 grub_printf("autosel: %d [OK]\n", autosel
);
604 grub_printf("autosel: %d [FAIL]\n", autosel
);
610 grub_printf("image: %s [FAIL]\n", iso
);
615 grub_printf("image not found\n");
622 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
626 const char *iso
= NULL
;
627 VTOY_JSON
*pNode
= NULL
;
628 persistence_config
*node
= NULL
;
629 persistence_config
*next
= NULL
;
630 file_fullpath
*backendpath
= NULL
;
634 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
636 debug("Not array %d\n", json
->enDataType
);
640 if (g_persistence_head
)
642 for (node
= g_persistence_head
; node
; node
= next
)
645 grub_check_free(node
->backendpath
);
649 g_persistence_head
= NULL
;
652 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
654 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
655 if (iso
&& iso
[0] == '/')
657 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
659 node
= grub_zalloc(sizeof(persistence_config
));
662 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
663 node
->backendpath
= backendpath
;
664 node
->backendnum
= pathnum
;
667 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
669 if (autosel
>= 0 && autosel
<= pathnum
)
671 node
->autosel
= autosel
;
675 if (g_persistence_head
)
677 node
->next
= g_persistence_head
;
680 g_persistence_head
= node
;
689 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
692 const char *path
= NULL
;
693 const char *alias
= NULL
;
694 VTOY_JSON
*pNode
= NULL
;
698 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
700 grub_printf("Not array %d\n", json
->enDataType
);
704 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
706 type
= vtoy_alias_image_file
;
707 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
710 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
711 type
= vtoy_alias_directory
;
714 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
715 if (path
&& path
[0] == '/' && alias
)
717 if (vtoy_alias_image_file
== type
)
719 if (ventoy_is_file_exist("%s%s", isodisk
, path
))
721 grub_printf("image: <%s> [ OK ]\n", path
);
725 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
730 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
732 grub_printf("dir: <%s> [ OK ]\n", path
);
736 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
740 grub_printf("alias: <%s>\n\n", alias
);
747 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
750 const char *path
= NULL
;
751 const char *alias
= NULL
;
752 VTOY_JSON
*pNode
= NULL
;
753 menu_alias
*node
= NULL
;
754 menu_alias
*next
= NULL
;
758 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
760 debug("Not array %d\n", json
->enDataType
);
764 if (g_menu_alias_head
)
766 for (node
= g_menu_alias_head
; node
; node
= next
)
772 g_menu_alias_head
= NULL
;
775 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
777 type
= vtoy_alias_image_file
;
778 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
781 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
782 type
= vtoy_alias_directory
;
785 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
786 if (path
&& path
[0] == '/' && alias
)
788 node
= grub_zalloc(sizeof(menu_alias
));
792 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
793 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
795 if (g_menu_alias_head
)
797 node
->next
= g_menu_alias_head
;
800 g_menu_alias_head
= node
;
808 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
811 const char *key
= NULL
;
812 const char *class = NULL
;
813 VTOY_JSON
*pNode
= NULL
;
814 menu_class
*tail
= NULL
;
815 menu_class
*node
= NULL
;
816 menu_class
*next
= NULL
;
820 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
822 debug("Not array %d\n", json
->enDataType
);
826 if (g_menu_class_head
)
828 for (node
= g_menu_class_head
; node
; node
= next
)
834 g_menu_class_head
= NULL
;
837 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
839 type
= vtoy_class_image_file
;
840 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
843 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
844 type
= vtoy_class_directory
;
847 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
850 node
= grub_zalloc(sizeof(menu_class
));
854 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
855 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
857 if (g_menu_class_head
)
863 g_menu_class_head
= node
;
873 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
876 const char *key
= NULL
;
877 const char *class = NULL
;
878 VTOY_JSON
*pNode
= NULL
;
882 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
884 grub_printf("Not array %d\n", json
->enDataType
);
888 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
890 type
= vtoy_class_image_file
;
891 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
894 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
895 type
= vtoy_class_directory
;
898 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
901 grub_printf("%s: <%s>\n", (type
== vtoy_class_directory
) ? "dir" : "key", key
);
902 grub_printf("class: <%s>\n\n", class);
909 static plugin_entry g_plugin_entries
[] =
911 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
912 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
913 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
914 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
915 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
916 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
919 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
922 VTOY_JSON
*cur
= json
;
924 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
928 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
930 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
932 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
933 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
944 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
949 VTOY_JSON
*json
= NULL
;
954 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
957 return GRUB_ERR_NONE
;
960 debug("json configuration file size %d\n", (int)file
->size
);
962 buf
= grub_malloc(file
->size
+ 1);
965 grub_file_close(file
);
970 grub_file_read(file
, buf
, file
->size
);
971 grub_file_close(file
);
973 json
= vtoy_json_create();
981 ret
= vtoy_json_parse(json
, buf
);
984 debug("Failed to parse json string %d\n", ret
);
989 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
991 vtoy_json_destroy(json
);
995 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
998 void ventoy_plugin_dump_auto_install(void)
1001 install_template
*node
= NULL
;
1003 for (node
= g_install_template_head
; node
; node
= node
->next
)
1005 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
1006 for (i
= 0; i
< node
->templatenum
; i
++)
1008 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
1015 void ventoy_plugin_dump_persistence(void)
1019 persistence_config
*node
= NULL
;
1020 ventoy_img_chunk_list chunk_list
;
1022 for (node
= g_persistence_head
; node
; node
= node
->next
)
1024 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
1026 for (i
= 0; i
< node
->backendnum
; i
++)
1028 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
1029 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
1032 grub_printf(" [ SUCCESS ]\n");
1033 grub_free(chunk_list
.chunk
);
1037 grub_printf(" [ FAILED ]\n");
1045 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
1047 install_template
*node
= NULL
;
1048 int len
= (int)grub_strlen(isopath
);
1050 for (node
= g_install_template_head
; node
; node
= node
->next
)
1052 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1061 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
1063 install_template
*node
= NULL
;
1065 node
= ventoy_plugin_find_install_template(isopath
);
1066 if ((!node
) || (!node
->templatepath
))
1071 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
1076 return node
->templatepath
[node
->cursel
].path
;
1079 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
1081 persistence_config
*node
= NULL
;
1082 int len
= (int)grub_strlen(isopath
);
1084 for (node
= g_persistence_head
; node
; node
= node
->next
)
1086 if ((len
== node
->pathlen
) && (grub_strcmp(node
->isopath
, isopath
) == 0))
1095 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
1098 grub_uint64_t start
= 0;
1099 grub_file_t file
= NULL
;
1100 persistence_config
*node
= NULL
;
1102 node
= ventoy_plugin_find_persistent(isopath
);
1103 if ((!node
) || (!node
->backendpath
))
1110 index
= node
->cursel
;
1113 if (index
< 0 || index
>= node
->backendnum
)
1118 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
1121 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
1125 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
1126 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
1127 if (NULL
== chunk_list
->chunk
)
1132 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
1133 chunk_list
->cur_chunk
= 0;
1135 start
= file
->device
->disk
->partition
->start
;
1136 ventoy_get_block_list(file
, chunk_list
, start
);
1138 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
1140 grub_free(chunk_list
->chunk
);
1141 chunk_list
->chunk
= NULL
;
1149 grub_file_close(file
);
1154 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
1156 menu_alias
*node
= NULL
;
1157 int len
= (int)grub_strlen(isopath
);
1159 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
1161 if (node
->type
== type
&& node
->pathlen
&&
1162 node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1171 const char * ventoy_plugin_get_menu_class(int type
, const char *name
)
1173 menu_class
*node
= NULL
;
1174 int len
= (int)grub_strlen(name
);
1176 if (vtoy_class_image_file
== type
)
1178 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1180 if (node
->type
== type
&& node
->patlen
<= len
&& grub_strstr(name
, node
->pattern
))
1188 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1190 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
1200 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1206 VTOY_JSON
*node
= NULL
;
1207 VTOY_JSON
*json
= NULL
;
1216 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
1219 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
1223 buf
= grub_malloc(file
->size
+ 1);
1226 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
1230 buf
[file
->size
] = 0;
1231 grub_file_read(file
, buf
, file
->size
);
1233 json
= vtoy_json_create();
1236 grub_printf("Failed to create json\n");
1240 ret
= vtoy_json_parse(json
, buf
);
1243 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
1247 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
1249 if (grub_strcmp(node
->pcName
, args
[1]) == 0)
1257 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
1261 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1263 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
1265 if (g_plugin_entries
[i
].checkfunc
)
1267 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
1274 check_free(file
, grub_file_close
);
1275 check_free(json
, vtoy_json_destroy
);
1276 grub_check_free(buf
);