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/crypto.h>
36 #include <grub/time.h>
37 #include <grub/font.h>
38 #include <grub/ventoy.h>
39 #include "ventoy_def.h"
41 GRUB_MOD_LICENSE ("GPLv3+");
43 char g_arch_mode_suffix
[64];
44 static char g_iso_disk_name
[128];
45 static grub_uint8_t g_boot_pwd
= 0;
46 static grub_uint8_t g_boot_sha256
[32];
47 static install_template
*g_install_template_head
= NULL
;
48 static dud
*g_dud_head
= NULL
;
49 static vtoy_password
*g_pwd_head
= NULL
;
50 static persistence_config
*g_persistence_head
= NULL
;
51 static menu_alias
*g_menu_alias_head
= NULL
;
52 static menu_class
*g_menu_class_head
= NULL
;
53 static injection_config
*g_injection_head
= NULL
;
54 static auto_memdisk
*g_auto_memdisk_head
= NULL
;
55 static image_list
*g_image_list_head
= NULL
;
56 static conf_replace
*g_conf_replace_head
= NULL
;
58 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
61 VTOY_JSON
*pNode
= NULL
;
62 VTOY_JSON
*pChild
= NULL
;
66 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
68 grub_printf("Not array type %d\n", json
->enDataType
);
72 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
74 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
76 pChild
= pNode
->pstChild
;
77 if (pChild
->enDataType
== JSON_TYPE_STRING
)
79 if (grub_strcmp(pChild
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
81 grub_printf("%s: %s [%s]\n", pChild
->pcName
, pChild
->unData
.pcStrVal
,
82 ventoy_check_file_exist("%s%s", isodisk
, pChild
->unData
.pcStrVal
) ? "OK" : "NOT EXIST");
86 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
91 grub_printf("%s is NOT string type\n", pChild
->pcName
);
97 grub_printf("%s is not an object\n", pNode
->pcName
);
105 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
107 VTOY_JSON
*pNode
= NULL
;
108 VTOY_JSON
*pChild
= NULL
;
112 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
114 debug("Not array %d\n", json
->enDataType
);
118 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
120 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
122 pChild
= pNode
->pstChild
;
123 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
125 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
133 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
139 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
142 grub_printf("file: %s\n", value
);
145 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
149 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
154 grub_printf("Theme file %s does NOT exist\n", value
);
159 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
162 grub_printf("gfxmode: %s\n", value
);
165 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
168 grub_printf("display_mode: %s\n", value
);
171 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
174 grub_printf("serial_param %s\n", value
);
177 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
180 grub_printf("ventoy_left: %s\n", value
);
183 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
186 grub_printf("ventoy_top: %s\n", value
);
189 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
192 grub_printf("ventoy_color: %s\n", value
);
195 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
198 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
200 if (node
->enDataType
== JSON_TYPE_STRING
)
202 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
204 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
208 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
215 grub_printf("fonts NOT found\n");
221 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
227 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
232 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
236 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
239 if (ventoy_is_file_exist(filepath
) == 0)
241 debug("Theme file %s does not exist\n", filepath
);
245 debug("vtoy_theme %s\n", filepath
);
246 grub_env_set("vtoy_theme", filepath
);
249 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
252 debug("vtoy_gfxmode %s\n", value
);
253 grub_env_set("vtoy_gfxmode", value
);
256 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
259 debug("display_mode %s\n", value
);
260 grub_env_set("vtoy_display_mode", value
);
263 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
266 debug("serial_param %s\n", value
);
267 grub_env_set("vtoy_serial_param", value
);
270 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
273 grub_env_set("VTLE_LFT", value
);
276 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
279 grub_env_set("VTLE_TOP", value
);
282 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
285 grub_env_set("VTLE_CLR", value
);
288 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
291 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
293 if (node
->enDataType
== JSON_TYPE_STRING
&&
294 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
296 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
297 grub_font_load(filepath
);
305 static int ventoy_plugin_check_path(const char *path
, const char *file
)
309 grub_printf("%s is NOT begin with '/' \n", file
);
313 if (grub_strchr(file
, '\\'))
315 grub_printf("%s contains invalid '\\' \n", file
);
319 if (grub_strstr(file
, "//"))
321 grub_printf("%s contains invalid double slash\n", file
);
325 if (grub_strstr(file
, "../"))
327 grub_printf("%s contains invalid '../' \n", file
);
331 if (!ventoy_is_file_exist("%s%s", path
, file
))
333 grub_printf("%s%s does NOT exist\n", path
, file
);
340 static int ventoy_plugin_check_fullpath
351 VTOY_JSON
*node
= json
;
352 VTOY_JSON
*child
= NULL
;
356 if (0 == grub_strcmp(key
, node
->pcName
))
360 node
= node
->pstNext
;
368 if (JSON_TYPE_STRING
== node
->enDataType
)
371 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
372 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
374 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
376 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
378 if (JSON_TYPE_STRING
!= child
->enDataType
)
380 grub_printf("Non string json type\n");
384 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
385 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
396 static int ventoy_plugin_parse_fullpath
401 file_fullpath
**fullpath
,
407 VTOY_JSON
*node
= json
;
408 VTOY_JSON
*child
= NULL
;
409 file_fullpath
*path
= NULL
;
413 if (0 == grub_strcmp(key
, node
->pcName
))
417 node
= node
->pstNext
;
425 if (JSON_TYPE_STRING
== node
->enDataType
)
427 debug("%s is string type data\n", node
->pcName
);
429 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
431 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
435 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
438 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
444 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
446 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
448 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
450 debug("Invalid data type:%d\n", child
->enDataType
);
455 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
457 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
462 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
464 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
466 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
480 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
484 const char *iso
= NULL
;
485 VTOY_JSON
*pNode
= NULL
;
487 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
489 grub_printf("Not array type %d\n", json
->enDataType
);
493 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
495 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
497 grub_printf("NOT object type\n");
500 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
503 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
505 grub_printf("image: %s [OK]\n", iso
);
506 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
508 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
510 if (autosel
>= 0 && autosel
<= pathnum
)
512 grub_printf("autosel: %d [OK]\n", autosel
);
516 grub_printf("autosel: %d [FAIL]\n", autosel
);
522 grub_printf("image: %s [FAIL]\n", iso
);
527 grub_printf("image not found\n");
534 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
538 const char *iso
= NULL
;
539 VTOY_JSON
*pNode
= NULL
;
540 install_template
*node
= NULL
;
541 install_template
*next
= NULL
;
542 file_fullpath
*templatepath
= NULL
;
544 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
546 debug("Not array %d\n", json
->enDataType
);
550 if (g_install_template_head
)
552 for (node
= g_install_template_head
; node
; node
= next
)
555 grub_check_free(node
->templatepath
);
559 g_install_template_head
= NULL
;
562 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
564 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
565 if (iso
&& iso
[0] == '/')
567 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
569 node
= grub_zalloc(sizeof(install_template
));
572 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
573 node
->templatepath
= templatepath
;
574 node
->templatenum
= pathnum
;
577 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
579 if (autosel
>= 0 && autosel
<= pathnum
)
581 node
->autosel
= autosel
;
585 if (g_install_template_head
)
587 node
->next
= g_install_template_head
;
590 g_install_template_head
= node
;
599 static int ventoy_plugin_dud_check(VTOY_JSON
*json
, const char *isodisk
)
602 const char *iso
= NULL
;
603 VTOY_JSON
*pNode
= NULL
;
605 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
607 grub_printf("Not array type %d\n", json
->enDataType
);
611 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
613 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
615 grub_printf("NOT object type\n");
618 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
621 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
623 grub_printf("image: %s [OK]\n", iso
);
624 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "dud", &pathnum
);
628 grub_printf("image: %s [FAIL]\n", iso
);
633 grub_printf("image not found\n");
640 static int ventoy_plugin_dud_entry(VTOY_JSON
*json
, const char *isodisk
)
643 const char *iso
= NULL
;
644 VTOY_JSON
*pNode
= NULL
;
647 file_fullpath
*dudpath
= NULL
;
649 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
651 debug("Not array %d\n", json
->enDataType
);
657 for (node
= g_dud_head
; node
; node
= next
)
660 grub_check_free(node
->dudpath
);
667 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
669 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
670 if (iso
&& iso
[0] == '/')
672 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "dud", &dudpath
, &pathnum
))
674 node
= grub_zalloc(sizeof(dud
));
677 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
678 node
->dudpath
= dudpath
;
679 node
->dudnum
= pathnum
;
680 node
->files
= grub_zalloc(sizeof(dudfile
) * pathnum
);
686 node
->next
= g_dud_head
;
703 static int ventoy_plugin_pwd_entry(VTOY_JSON
*json
, const char *isodisk
)
707 const char *iso
= NULL
;
708 const char *pwd
= NULL
;
709 VTOY_JSON
*pNode
= NULL
;
710 VTOY_JSON
*pCNode
= NULL
;
711 vtoy_password
*node
= NULL
;
712 vtoy_password
*tail
= NULL
;
713 vtoy_password
*next
= NULL
;
718 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
720 debug("Not object %d\n", json
->enDataType
);
726 for (node
= g_pwd_head
; node
; node
= next
)
735 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
737 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
739 len
= (int)grub_strlen(pNode
->unData
.pcStrVal
);
743 for (i
= 0; i
< 32; i
++)
745 bytes
[0] = pNode
->unData
.pcStrVal
[i
* 2];
746 bytes
[1] = pNode
->unData
.pcStrVal
[i
* 2 + 1];
748 g_boot_sha256
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
752 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
754 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
756 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
761 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
762 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
763 if (iso
&& pwd
&& iso
[0] == '/')
765 node
= grub_zalloc(sizeof(vtoy_password
));
768 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
769 len
= (int)grub_strlen(pwd
);
776 for (i
= 0; i
< 32; i
++)
778 bytes
[0] = pwd
[i
* 2];
779 bytes
[1] = pwd
[i
* 2 + 1];
781 node
->sha256
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
802 static int ventoy_plugin_pwd_check(VTOY_JSON
*json
, const char *isodisk
)
805 const char *iso
= NULL
;
806 const char *pwd
= NULL
;
807 VTOY_JSON
*pNode
= NULL
;
808 VTOY_JSON
*pCNode
= NULL
;
810 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
812 grub_printf("Not object %d\n", json
->enDataType
);
816 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
818 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
820 len
= (int)grub_strlen(pNode
->unData
.pcStrVal
);
823 grub_printf("Invalid bootpwd len :%d\n", len
);
827 grub_printf("bootpwd:<%s>\n", pNode
->unData
.pcStrVal
);
830 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
832 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
834 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
836 grub_printf("Not object %d\n", pCNode
->enDataType
);
840 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
843 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
845 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
847 len
= (int)grub_strlen(pwd
);
850 grub_printf("Invalid sha256 len <%d>\n", len
);
854 grub_printf("file:<%s> [OK]\n", iso
);
855 grub_printf("pwd:<%s>\n\n", pwd
);
861 grub_printf("No file found\n");
870 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
874 const char *iso
= NULL
;
875 VTOY_JSON
*pNode
= NULL
;
877 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
879 grub_printf("Not array type %d\n", json
->enDataType
);
883 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
885 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
887 grub_printf("NOT object type\n");
890 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
893 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
895 grub_printf("image: %s [OK]\n", iso
);
896 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
898 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
900 if (autosel
>= 0 && autosel
<= pathnum
)
902 grub_printf("autosel: %d [OK]\n", autosel
);
906 grub_printf("autosel: %d [FAIL]\n", autosel
);
912 grub_printf("image: %s [FAIL]\n", iso
);
917 grub_printf("image not found\n");
924 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
928 const char *iso
= NULL
;
929 VTOY_JSON
*pNode
= NULL
;
930 persistence_config
*node
= NULL
;
931 persistence_config
*next
= NULL
;
932 file_fullpath
*backendpath
= NULL
;
936 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
938 debug("Not array %d\n", json
->enDataType
);
942 if (g_persistence_head
)
944 for (node
= g_persistence_head
; node
; node
= next
)
947 grub_check_free(node
->backendpath
);
951 g_persistence_head
= NULL
;
954 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
956 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
957 if (iso
&& iso
[0] == '/')
959 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
961 node
= grub_zalloc(sizeof(persistence_config
));
964 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
965 node
->backendpath
= backendpath
;
966 node
->backendnum
= pathnum
;
969 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
971 if (autosel
>= 0 && autosel
<= pathnum
)
973 node
->autosel
= autosel
;
977 if (g_persistence_head
)
979 node
->next
= g_persistence_head
;
982 g_persistence_head
= node
;
991 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
994 const char *path
= NULL
;
995 const char *alias
= NULL
;
996 VTOY_JSON
*pNode
= NULL
;
1000 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1002 grub_printf("Not array %d\n", json
->enDataType
);
1006 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1008 type
= vtoy_alias_image_file
;
1009 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1012 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1013 type
= vtoy_alias_directory
;
1016 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1017 if (path
&& path
[0] == '/' && alias
)
1019 if (vtoy_alias_image_file
== type
)
1021 if (ventoy_is_file_exist("%s%s", isodisk
, path
))
1023 grub_printf("image: <%s> [ OK ]\n", path
);
1027 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
1032 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
1034 grub_printf("dir: <%s> [ OK ]\n", path
);
1038 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
1042 grub_printf("alias: <%s>\n\n", alias
);
1049 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
1052 const char *path
= NULL
;
1053 const char *alias
= NULL
;
1054 VTOY_JSON
*pNode
= NULL
;
1055 menu_alias
*node
= NULL
;
1056 menu_alias
*next
= NULL
;
1060 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1062 debug("Not array %d\n", json
->enDataType
);
1066 if (g_menu_alias_head
)
1068 for (node
= g_menu_alias_head
; node
; node
= next
)
1074 g_menu_alias_head
= NULL
;
1077 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1079 type
= vtoy_alias_image_file
;
1080 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1083 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1084 type
= vtoy_alias_directory
;
1087 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1088 if (path
&& path
[0] == '/' && alias
)
1090 node
= grub_zalloc(sizeof(menu_alias
));
1094 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1095 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1097 if (g_menu_alias_head
)
1099 node
->next
= g_menu_alias_head
;
1102 g_menu_alias_head
= node
;
1111 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
1113 const char *path
= NULL
;
1114 const char *archive
= NULL
;
1115 VTOY_JSON
*pNode
= NULL
;
1119 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1121 grub_printf("Not array %d\n", json
->enDataType
);
1125 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1127 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1130 grub_printf("image not found\n");
1134 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1137 grub_printf("archive not found\n");
1141 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
1142 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
1148 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
1150 const char *path
= NULL
;
1151 const char *archive
= NULL
;
1152 VTOY_JSON
*pNode
= NULL
;
1153 injection_config
*node
= NULL
;
1154 injection_config
*next
= NULL
;
1158 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1160 debug("Not array %d\n", json
->enDataType
);
1164 if (g_injection_head
)
1166 for (node
= g_injection_head
; node
; node
= next
)
1172 g_injection_head
= NULL
;
1175 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1177 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1178 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1179 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
1181 node
= grub_zalloc(sizeof(injection_config
));
1184 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1185 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
1187 if (g_injection_head
)
1189 node
->next
= g_injection_head
;
1192 g_injection_head
= node
;
1200 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
1203 const char *key
= NULL
;
1204 const char *class = NULL
;
1205 VTOY_JSON
*pNode
= NULL
;
1206 menu_class
*tail
= NULL
;
1207 menu_class
*node
= NULL
;
1208 menu_class
*next
= NULL
;
1212 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1214 debug("Not array %d\n", json
->enDataType
);
1218 if (g_menu_class_head
)
1220 for (node
= g_menu_class_head
; node
; node
= next
)
1226 g_menu_class_head
= NULL
;
1229 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1231 type
= vtoy_class_image_file
;
1232 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1235 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1236 type
= vtoy_class_directory
;
1239 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1242 node
= grub_zalloc(sizeof(menu_class
));
1246 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
1247 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
1249 if (g_menu_class_head
)
1255 g_menu_class_head
= node
;
1265 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
1268 const char *key
= NULL
;
1269 const char *class = NULL
;
1270 VTOY_JSON
*pNode
= NULL
;
1274 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1276 grub_printf("Not array %d\n", json
->enDataType
);
1280 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1282 type
= vtoy_class_image_file
;
1283 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1286 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1287 type
= vtoy_class_directory
;
1290 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1293 grub_printf("%s: <%s>\n", (type
== vtoy_class_directory
) ? "dir" : "key", key
);
1294 grub_printf("class: <%s>\n\n", class);
1301 static int ventoy_plugin_conf_replace_entry(VTOY_JSON
*json
, const char *isodisk
)
1303 const char *isof
= NULL
;
1304 const char *orgf
= NULL
;
1305 const char *newf
= NULL
;
1306 VTOY_JSON
*pNode
= NULL
;
1307 conf_replace
*tail
= NULL
;
1308 conf_replace
*node
= NULL
;
1309 conf_replace
*next
= NULL
;
1313 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1315 debug("Not array %d\n", json
->enDataType
);
1319 if (g_conf_replace_head
)
1321 for (node
= g_conf_replace_head
; node
; node
= next
)
1327 g_conf_replace_head
= NULL
;
1330 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1332 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1333 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1334 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1335 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1337 node
= grub_zalloc(sizeof(conf_replace
));
1340 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", isof
);
1341 grub_snprintf(node
->orgconf
, sizeof(node
->orgconf
), "%s", orgf
);
1342 grub_snprintf(node
->newconf
, sizeof(node
->newconf
), "%s", newf
);
1344 if (g_conf_replace_head
)
1350 g_conf_replace_head
= node
;
1360 static int ventoy_plugin_conf_replace_check(VTOY_JSON
*json
, const char *isodisk
)
1362 const char *isof
= NULL
;
1363 const char *orgf
= NULL
;
1364 const char *newf
= NULL
;
1365 VTOY_JSON
*pNode
= NULL
;
1366 grub_file_t file
= NULL
;
1371 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1373 grub_printf("Not array %d\n", json
->enDataType
);
1377 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1379 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1380 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1381 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1382 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1384 if (ventoy_check_file_exist("%s%s", isodisk
, isof
))
1386 grub_printf("iso:<%s> [OK]\n", isof
);
1388 grub_snprintf(cmd
, sizeof(cmd
), "loopback vtisocheck %s%s", isodisk
, isof
);
1389 grub_script_execute_sourcecode(cmd
);
1391 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "(vtisocheck)/%s", orgf
);
1394 if (grub_strcmp(file
->fs
->name
, "iso9660") == 0)
1396 grub_printf("org:<%s> [OK]\n", orgf
);
1400 grub_printf("org:<%s> [Exist But NOT ISO9660]\n", orgf
);
1402 grub_file_close(file
);
1406 grub_printf("org:<%s> [NOT Exist]\n", orgf
);
1409 grub_script_execute_sourcecode("loopback -d vtisocheck");
1413 grub_printf("iso:<%s> [NOT Exist]\n", isof
);
1414 grub_printf("org:<%s>\n", orgf
);
1417 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isodisk
, newf
);
1420 if (file
->size
> vtoy_max_replace_file_size
)
1422 grub_printf("new:<%s> [Too Big %lu] \n", newf
, (ulong
)file
->size
);
1426 grub_printf("new:<%s> [OK]\n", newf
);
1428 grub_file_close(file
);
1432 grub_printf("new:<%s> [NOT Exist]\n", newf
);
1441 static int ventoy_plugin_auto_memdisk_entry(VTOY_JSON
*json
, const char *isodisk
)
1443 VTOY_JSON
*pNode
= NULL
;
1444 auto_memdisk
*node
= NULL
;
1445 auto_memdisk
*next
= NULL
;
1449 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1451 debug("Not array %d\n", json
->enDataType
);
1455 if (g_auto_memdisk_head
)
1457 for (node
= g_auto_memdisk_head
; node
; node
= next
)
1463 g_auto_memdisk_head
= NULL
;
1466 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1468 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1470 node
= grub_zalloc(sizeof(auto_memdisk
));
1473 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1475 if (g_auto_memdisk_head
)
1477 node
->next
= g_auto_memdisk_head
;
1480 g_auto_memdisk_head
= node
;
1488 static int ventoy_plugin_auto_memdisk_check(VTOY_JSON
*json
, const char *isodisk
)
1490 VTOY_JSON
*pNode
= NULL
;
1492 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1494 grub_printf("Not array %d\n", json
->enDataType
);
1498 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1500 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1502 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1504 if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1506 grub_printf(" [OK]\n");
1510 grub_printf(" [NOT EXIST]\n");
1518 static int ventoy_plugin_image_list_entry(VTOY_JSON
*json
, const char *isodisk
)
1520 VTOY_JSON
*pNode
= NULL
;
1521 image_list
*node
= NULL
;
1522 image_list
*next
= NULL
;
1523 image_list
*tail
= NULL
;
1527 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1529 debug("Not array %d\n", json
->enDataType
);
1533 if (g_image_list_head
)
1535 for (node
= g_image_list_head
; node
; node
= next
)
1541 g_image_list_head
= NULL
;
1544 g_plugin_image_list
= 1;
1546 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1548 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1550 node
= grub_zalloc(sizeof(image_list
));
1553 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1555 if (g_image_list_head
)
1561 g_image_list_head
= node
;
1571 static int ventoy_plugin_image_list_check(VTOY_JSON
*json
, const char *isodisk
)
1573 VTOY_JSON
*pNode
= NULL
;
1575 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1577 grub_printf("Not array %d\n", json
->enDataType
);
1581 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1583 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1585 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1587 if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1589 grub_printf(" [OK]\n");
1593 grub_printf(" [NOT EXIST]\n");
1601 static plugin_entry g_plugin_entries
[] =
1603 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
1604 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
1605 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
1606 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
1607 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
1608 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
1609 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
},
1610 { "auto_memdisk", ventoy_plugin_auto_memdisk_entry
, ventoy_plugin_auto_memdisk_check
},
1611 { "image_list", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1612 { "conf_replace", ventoy_plugin_conf_replace_entry
, ventoy_plugin_conf_replace_check
},
1613 { "dud", ventoy_plugin_dud_entry
, ventoy_plugin_dud_check
},
1614 { "password", ventoy_plugin_pwd_entry
, ventoy_plugin_pwd_check
},
1617 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
1621 VTOY_JSON
*cur
= json
;
1623 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
1627 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1629 grub_snprintf(key
, sizeof(key
), "%s_%s", g_plugin_entries
[i
].key
, g_arch_mode_suffix
);
1630 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0 || grub_strcmp(key
, cur
->pcName
) == 0)
1632 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
1633 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
1644 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1649 VTOY_JSON
*json
= NULL
;
1654 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
1657 return GRUB_ERR_NONE
;
1660 debug("json configuration file size %d\n", (int)file
->size
);
1662 buf
= grub_malloc(file
->size
+ 1);
1665 grub_file_close(file
);
1669 buf
[file
->size
] = 0;
1670 grub_file_read(file
, buf
, file
->size
);
1671 grub_file_close(file
);
1673 json
= vtoy_json_create();
1681 ret
= vtoy_json_parse(json
, buf
);
1684 grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
1685 grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR");
1687 debug("Failed to parse json string %d\n", ret
);
1692 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
1694 vtoy_json_destroy(json
);
1700 grub_printf("\n\n\n\n");
1701 if (ventoy_check_password(g_boot_sha256
, 3))
1703 grub_printf("\n!!! Password check failed, will exit after 5 seconds. !!!\n");
1710 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
1713 void ventoy_plugin_dump_injection(void)
1715 injection_config
*node
= NULL
;
1717 for (node
= g_injection_head
; node
; node
= node
->next
)
1719 grub_printf("\nIMAGE:<%s>\n", node
->isopath
);
1720 grub_printf("ARCHIVE:<%s>\n", node
->archive
);
1727 void ventoy_plugin_dump_auto_install(void)
1730 install_template
*node
= NULL
;
1732 for (node
= g_install_template_head
; node
; node
= node
->next
)
1734 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
1735 for (i
= 0; i
< node
->templatenum
; i
++)
1737 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
1744 void ventoy_plugin_dump_persistence(void)
1748 persistence_config
*node
= NULL
;
1749 ventoy_img_chunk_list chunk_list
;
1751 for (node
= g_persistence_head
; node
; node
= node
->next
)
1753 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
1755 for (i
= 0; i
< node
->backendnum
; i
++)
1757 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
1758 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
1761 grub_printf(" [ SUCCESS ]\n");
1762 grub_free(chunk_list
.chunk
);
1766 grub_printf(" [ FAILED ]\n");
1774 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
1777 install_template
*node
= NULL
;
1779 if (!g_install_template_head
)
1784 len
= (int)grub_strlen(isopath
);
1785 for (node
= g_install_template_head
; node
; node
= node
->next
)
1787 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1796 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
1798 install_template
*node
= NULL
;
1800 node
= ventoy_plugin_find_install_template(isopath
);
1801 if ((!node
) || (!node
->templatepath
))
1806 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
1811 return node
->templatepath
[node
->cursel
].path
;
1814 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
1817 persistence_config
*node
= NULL
;
1819 if (!g_persistence_head
)
1824 len
= (int)grub_strlen(isopath
);
1825 for (node
= g_persistence_head
; node
; node
= node
->next
)
1827 if ((len
== node
->pathlen
) && (grub_strcmp(node
->isopath
, isopath
) == 0))
1836 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
1839 grub_uint64_t start
= 0;
1840 grub_file_t file
= NULL
;
1841 persistence_config
*node
= NULL
;
1843 node
= ventoy_plugin_find_persistent(isopath
);
1844 if ((!node
) || (!node
->backendpath
))
1851 index
= node
->cursel
;
1854 if (index
< 0 || index
>= node
->backendnum
)
1859 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
1862 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
1866 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
1867 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
1868 if (NULL
== chunk_list
->chunk
)
1873 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
1874 chunk_list
->cur_chunk
= 0;
1876 start
= file
->device
->disk
->partition
->start
;
1877 ventoy_get_block_list(file
, chunk_list
, start
);
1879 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
1881 grub_free(chunk_list
->chunk
);
1882 chunk_list
->chunk
= NULL
;
1890 grub_file_close(file
);
1895 const char * ventoy_plugin_get_injection(const char *isopath
)
1898 injection_config
*node
= NULL
;
1900 if (!g_injection_head
)
1905 len
= (int)grub_strlen(isopath
);
1906 for (node
= g_injection_head
; node
; node
= node
->next
)
1908 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1910 return node
->archive
;
1917 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
1920 menu_alias
*node
= NULL
;
1922 if (!g_menu_alias_head
)
1927 len
= (int)grub_strlen(isopath
);
1928 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
1930 if (node
->type
== type
&& node
->pathlen
&&
1931 node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1940 const char * ventoy_plugin_get_menu_class(int type
, const char *name
)
1943 menu_class
*node
= NULL
;
1945 if (!g_menu_class_head
)
1950 len
= (int)grub_strlen(name
);
1952 if (vtoy_class_image_file
== type
)
1954 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1956 if (node
->type
== type
&& node
->patlen
<= len
&& grub_strstr(name
, node
->pattern
))
1964 for (node
= g_menu_class_head
; node
; node
= node
->next
)
1966 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
1976 int ventoy_plugin_check_memdisk(const char *isopath
)
1979 auto_memdisk
*node
= NULL
;
1981 if (!g_auto_memdisk_head
)
1986 len
= (int)grub_strlen(isopath
);
1987 for (node
= g_auto_memdisk_head
; node
; node
= node
->next
)
1989 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->isopath
, len
) == 0)
1998 int ventoy_plugin_get_image_list_index(int type
, const char *name
)
2002 image_list
*node
= NULL
;
2004 if (!g_image_list_head
)
2009 len
= (int)grub_strlen(name
);
2011 for (node
= g_image_list_head
; node
; node
= node
->next
, index
++)
2013 if (vtoy_class_directory
== type
)
2015 if (len
< node
->pathlen
&& grub_strncmp(name
, node
->isopath
, len
) == 0)
2022 if (len
== node
->pathlen
&& grub_strncmp(name
, node
->isopath
, len
) == 0)
2032 conf_replace
* ventoy_plugin_find_conf_replace(const char *iso
)
2037 if (!g_conf_replace_head
)
2042 len
= (int)grub_strlen(iso
);
2044 for (node
= g_conf_replace_head
; node
; node
= node
->next
)
2046 if (node
->pathlen
== len
&& grub_strncmp(iso
, node
->isopath
, len
) == 0)
2055 dud
* ventoy_plugin_find_dud(const char *iso
)
2065 len
= (int)grub_strlen(iso
);
2066 for (node
= g_dud_head
; node
; node
= node
->next
)
2068 if (node
->pathlen
== len
&& grub_strncmp(iso
, node
->isopath
, len
) == 0)
2077 int ventoy_plugin_load_dud(dud
*node
, const char *isopart
)
2083 for (i
= 0; i
< node
->dudnum
; i
++)
2085 if (node
->files
[i
].size
> 0)
2087 debug("file %d has been loaded\n", i
);
2091 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isopart
, node
->dudpath
[i
].path
);
2094 buf
= grub_malloc(file
->size
);
2097 grub_file_read(file
, buf
, file
->size
);
2098 node
->files
[i
].size
= (int)file
->size
;
2099 node
->files
[i
].buf
= buf
;
2101 grub_file_close(file
);
2108 static const grub_uint8_t
* ventoy_plugin_get_password(const char *isopath
)
2111 vtoy_password
*node
= NULL
;
2113 if ((!g_pwd_head
) || (!isopath
))
2118 len
= (int)grub_strlen(isopath
);
2119 for (node
= g_pwd_head
; node
; node
= node
->next
)
2121 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->isopath
, len
) == 0)
2123 return node
->sha256
;
2130 int ventoy_check_password(const grub_uint8_t
*pwdsha256
, int retry
)
2133 grub_uint8_t sha256
[32];
2137 grub_memset(input
, 0, sizeof(input
));
2139 grub_printf("Enter password: ");
2141 grub_password_get(input
, sizeof(input
));
2143 grub_crypto_hash(GRUB_MD_SHA256
, sha256
, input
, grub_strlen(input
));
2145 if (grub_memcmp(pwdsha256
, sha256
, 32) == 0)
2151 grub_printf("Invalid password!\n\n");
2159 grub_err_t
ventoy_cmd_check_password(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2162 const grub_uint8_t
*sha256
= NULL
;
2167 sha256
= ventoy_plugin_get_password(args
[0]);
2170 if (0 == ventoy_check_password(sha256
, 1))
2188 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2195 VTOY_JSON
*node
= NULL
;
2196 VTOY_JSON
*json
= NULL
;
2205 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
2208 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
2209 grub_printf("Attention: directory name and filename are both case-sensitive.\n");
2213 buf
= grub_malloc(file
->size
+ 1);
2216 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
2220 buf
[file
->size
] = 0;
2221 grub_file_read(file
, buf
, file
->size
);
2223 json
= vtoy_json_create();
2226 grub_printf("Failed to create json\n");
2230 ret
= vtoy_json_parse(json
, buf
);
2233 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
2237 grub_snprintf(key
, sizeof(key
), "%s_%s", args
[1], g_arch_mode_suffix
);
2238 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
2240 if (grub_strcmp(node
->pcName
, args
[1]) == 0 || grub_strcmp(node
->pcName
, key
) == 0)
2248 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
2252 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
2254 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
2256 if (g_plugin_entries
[i
].checkfunc
)
2258 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
2265 check_free(file
, grub_file_close
);
2266 check_free(json
, vtoy_json_destroy
);
2267 grub_check_free(buf
);