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 vtoy_password g_boot_pwd
;
46 static vtoy_password g_file_type_pwd
[img_type_max
];
47 static install_template
*g_install_template_head
= NULL
;
48 static dud
*g_dud_head
= NULL
;
49 static menu_password
*g_pwd_head
= NULL
;
50 static persistence_config
*g_persistence_head
= NULL
;
51 static menu_tip
*g_menu_tip_head
= NULL
;
52 static menu_alias
*g_menu_alias_head
= NULL
;
53 static menu_class
*g_menu_class_head
= NULL
;
54 static custom_boot
*g_custom_boot_head
= NULL
;
55 static injection_config
*g_injection_head
= NULL
;
56 static auto_memdisk
*g_auto_memdisk_head
= NULL
;
57 static image_list
*g_image_list_head
= NULL
;
58 static conf_replace
*g_conf_replace_head
= NULL
;
60 static int g_theme_num
= 0;
61 static theme_list
*g_theme_head
= NULL
;
62 static int g_theme_random
= vtoy_theme_random_boot_second
;
63 static char g_theme_single_file
[256];
65 static int ventoy_plugin_is_parent(const char *pat
, int patlen
, const char *isopath
)
69 if (isopath
[patlen
] == '/' && ventoy_strncmp(pat
, isopath
, patlen
) == 0 &&
70 grub_strchr(isopath
+ patlen
+ 1, '/') == NULL
)
77 if (pat
[0] == '/' && grub_strchr(isopath
+ 1, '/') == NULL
)
86 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
89 VTOY_JSON
*pNode
= NULL
;
90 VTOY_JSON
*pChild
= NULL
;
94 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
96 grub_printf("Not array type %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
)
107 if (grub_strcmp(pChild
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
109 grub_printf("%s: %s [%s]\n", pChild
->pcName
, pChild
->unData
.pcStrVal
,
110 ventoy_check_file_exist("%s%s", isodisk
, pChild
->unData
.pcStrVal
) ? "OK" : "NOT EXIST");
114 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
119 grub_printf("%s is NOT string type\n", pChild
->pcName
);
125 grub_printf("%s is not an object\n", pNode
->pcName
);
133 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
135 VTOY_JSON
*pNode
= NULL
;
136 VTOY_JSON
*pChild
= NULL
;
140 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
142 debug("Not array %d\n", json
->enDataType
);
146 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
148 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
150 pChild
= pNode
->pstChild
;
151 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
153 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
161 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
167 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
170 grub_printf("file: %s\n", value
);
173 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
177 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
182 grub_printf("Theme file %s does NOT exist\n", value
);
188 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "file");
191 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
193 value
= node
->unData
.pcStrVal
;
194 grub_printf("file: %s\n", value
);
197 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
201 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
206 grub_printf("Theme file %s does NOT exist\n", value
);
211 value
= vtoy_json_get_string_ex(json
->pstChild
, "random");
214 grub_printf("random: %s\n", value
);
219 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
222 grub_printf("gfxmode: %s\n", value
);
225 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
228 grub_printf("display_mode: %s\n", value
);
231 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
234 grub_printf("serial_param %s\n", value
);
237 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
240 grub_printf("ventoy_left: %s\n", value
);
243 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
246 grub_printf("ventoy_top: %s\n", value
);
249 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
252 grub_printf("ventoy_color: %s\n", value
);
255 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
258 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
260 if (node
->enDataType
== JSON_TYPE_STRING
)
262 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
264 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
268 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
275 grub_printf("fonts NOT found\n");
281 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
285 VTOY_JSON
*node
= NULL
;
286 theme_list
*tail
= NULL
;
287 theme_list
*themenode
= NULL
;
289 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
294 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
298 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
301 if (ventoy_check_file_exist(filepath
) == 0)
303 debug("Theme file %s does not exist\n", filepath
);
307 debug("vtoy_theme %s\n", filepath
);
308 ventoy_env_export("vtoy_theme", filepath
);
309 grub_snprintf(g_theme_single_file
, sizeof(g_theme_single_file
), "%s", filepath
);
313 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "file");
316 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
318 value
= node
->unData
.pcStrVal
;
321 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
325 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
328 if (ventoy_check_file_exist(filepath
) == 0)
333 themenode
= grub_zalloc(sizeof(theme_list
));
336 grub_snprintf(themenode
->theme
.path
, sizeof(themenode
->theme
.path
), "%s", filepath
);
339 tail
->next
= themenode
;
343 g_theme_head
= themenode
;
350 ventoy_env_export("vtoy_theme", "random");
351 value
= vtoy_json_get_string_ex(json
->pstChild
, "random");
354 if (grub_strcmp(value
, "boot_second") == 0)
356 g_theme_random
= vtoy_theme_random_boot_second
;
358 else if (grub_strcmp(value
, "boot_day") == 0)
360 g_theme_random
= vtoy_theme_random_boot_day
;
362 else if (grub_strcmp(value
, "boot_month") == 0)
364 g_theme_random
= vtoy_theme_random_boot_month
;
370 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
373 debug("vtoy_gfxmode %s\n", value
);
374 ventoy_env_export("vtoy_gfxmode", value
);
377 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
380 debug("display_mode %s\n", value
);
381 ventoy_env_export("vtoy_display_mode", value
);
384 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
387 debug("serial_param %s\n", value
);
388 ventoy_env_export("vtoy_serial_param", value
);
391 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
394 ventoy_env_export("VTLE_LFT", value
);
397 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
400 ventoy_env_export("VTLE_TOP", value
);
403 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
406 ventoy_env_export("VTLE_CLR", value
);
409 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
412 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
414 if (node
->enDataType
== JSON_TYPE_STRING
&&
415 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
417 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
418 grub_font_load(filepath
);
426 static int ventoy_plugin_check_path(const char *path
, const char *file
)
430 grub_printf("%s is NOT begin with '/' \n", file
);
434 if (grub_strchr(file
, '\\'))
436 grub_printf("%s contains invalid '\\' \n", file
);
440 if (grub_strstr(file
, "//"))
442 grub_printf("%s contains invalid double slash\n", file
);
446 if (grub_strstr(file
, "../"))
448 grub_printf("%s contains invalid '../' \n", file
);
452 if (!ventoy_is_file_exist("%s%s", path
, file
))
454 grub_printf("%s%s does NOT exist\n", path
, file
);
461 static int ventoy_plugin_check_fullpath
472 VTOY_JSON
*node
= json
;
473 VTOY_JSON
*child
= NULL
;
477 if (0 == grub_strcmp(key
, node
->pcName
))
481 node
= node
->pstNext
;
489 if (JSON_TYPE_STRING
== node
->enDataType
)
492 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
493 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
495 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
497 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
499 if (JSON_TYPE_STRING
!= child
->enDataType
)
501 grub_printf("Non string json type\n");
505 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
506 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
517 static int ventoy_plugin_parse_fullpath
522 file_fullpath
**fullpath
,
528 VTOY_JSON
*node
= json
;
529 VTOY_JSON
*child
= NULL
;
530 file_fullpath
*path
= NULL
;
534 if (0 == grub_strcmp(key
, node
->pcName
))
538 node
= node
->pstNext
;
546 if (JSON_TYPE_STRING
== node
->enDataType
)
548 debug("%s is string type data\n", node
->pcName
);
550 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
552 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
556 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
559 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
565 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
567 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
569 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
571 debug("Invalid data type:%d\n", child
->enDataType
);
576 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
578 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
583 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
585 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
587 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
601 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
607 const char *iso
= NULL
;
608 VTOY_JSON
*pNode
= NULL
;
610 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
612 grub_printf("Not array type %d\n", json
->enDataType
);
616 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
618 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
620 grub_printf("NOT object type\n");
623 if ((iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image")) != NULL
)
625 pos
= grub_strchr(iso
, '*');
626 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
628 grub_printf("image: %s [%s]\n", iso
, (pos
? "*" : "OK"));
629 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
631 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
633 if (autosel
>= 0 && autosel
<= pathnum
)
635 grub_printf("autosel: %d [OK]\n", autosel
);
639 grub_printf("autosel: %d [FAIL]\n", autosel
);
643 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "timeout", &timeout
))
647 grub_printf("timeout: %d [OK]\n", timeout
);
651 grub_printf("timeout: %d [FAIL]\n", timeout
);
657 grub_printf("image: %s [FAIL]\n", iso
);
660 else if ((iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent")) != NULL
)
662 if (ventoy_is_dir_exist("%s%s", isodisk
, iso
))
664 grub_printf("parent: %s [OK]\n", iso
);
665 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
667 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
669 if (autosel
>= 0 && autosel
<= pathnum
)
671 grub_printf("autosel: %d [OK]\n", autosel
);
675 grub_printf("autosel: %d [FAIL]\n", autosel
);
679 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "timeout", &timeout
))
683 grub_printf("timeout: %d [OK]\n", timeout
);
687 grub_printf("timeout: %d [FAIL]\n", timeout
);
693 grub_printf("parent: %s [FAIL]\n", iso
);
698 grub_printf("image not found\n");
705 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
711 const char *iso
= NULL
;
712 VTOY_JSON
*pNode
= NULL
;
713 install_template
*node
= NULL
;
714 install_template
*next
= NULL
;
715 file_fullpath
*templatepath
= NULL
;
717 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
719 debug("Not array %d\n", json
->enDataType
);
723 if (g_install_template_head
)
725 for (node
= g_install_template_head
; node
; node
= next
)
728 grub_check_free(node
->templatepath
);
732 g_install_template_head
= NULL
;
735 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
737 type
= auto_install_type_file
;
738 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
741 type
= auto_install_type_parent
;
742 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
745 if (iso
&& iso
[0] == '/')
747 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
749 node
= grub_zalloc(sizeof(install_template
));
753 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
754 node
->templatepath
= templatepath
;
755 node
->templatenum
= pathnum
;
759 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
761 if (autosel
>= 0 && autosel
<= pathnum
)
763 node
->autosel
= autosel
;
767 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "timeout", &timeout
))
771 node
->timeout
= timeout
;
775 if (g_install_template_head
)
777 node
->next
= g_install_template_head
;
780 g_install_template_head
= node
;
789 static int ventoy_plugin_dud_check(VTOY_JSON
*json
, const char *isodisk
)
793 const char *iso
= NULL
;
794 VTOY_JSON
*pNode
= NULL
;
796 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
798 grub_printf("Not array type %d\n", json
->enDataType
);
802 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
804 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
806 grub_printf("NOT object type\n");
809 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
812 pos
= grub_strchr(iso
, '*');
813 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
815 grub_printf("image: %s [%s]\n", iso
, (pos
? "*" : "OK"));
816 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "dud", &pathnum
);
820 grub_printf("image: %s [FAIL]\n", iso
);
825 grub_printf("image not found\n");
832 static int ventoy_plugin_dud_entry(VTOY_JSON
*json
, const char *isodisk
)
835 const char *iso
= NULL
;
836 VTOY_JSON
*pNode
= NULL
;
839 file_fullpath
*dudpath
= NULL
;
841 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
843 debug("Not array %d\n", json
->enDataType
);
849 for (node
= g_dud_head
; node
; node
= next
)
852 grub_check_free(node
->dudpath
);
859 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
861 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
862 if (iso
&& iso
[0] == '/')
864 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "dud", &dudpath
, &pathnum
))
866 node
= grub_zalloc(sizeof(dud
));
869 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
870 node
->dudpath
= dudpath
;
871 node
->dudnum
= pathnum
;
872 node
->files
= grub_zalloc(sizeof(dudfile
) * pathnum
);
878 node
->next
= g_dud_head
;
895 static int ventoy_plugin_parse_pwdstr(char *pwdstr
, vtoy_password
*pwd
)
902 vtoy_password tmpPwd
;
904 len
= (int)grub_strlen(pwdstr
);
907 if (NULL
== pwd
) grub_printf("Password too long %d\n", len
);
911 grub_memset(&tmpPwd
, 0, sizeof(tmpPwd
));
913 if (grub_strncmp(pwdstr
, "txt#", 4) == 0)
915 tmpPwd
.type
= VTOY_PASSWORD_TXT
;
916 grub_snprintf(tmpPwd
.text
, sizeof(tmpPwd
.text
), "%s", pwdstr
+ 4);
918 else if (grub_strncmp(pwdstr
, "md5#", 4) == 0)
922 for (i
= 0; i
< 16; i
++)
924 bytes
[0] = pwdstr
[4 + i
* 2];
925 bytes
[1] = pwdstr
[4 + i
* 2 + 1];
928 if (grub_isxdigit(bytes
[0]) && grub_isxdigit(bytes
[1]))
930 tmpPwd
.md5
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
934 if (NULL
== pwd
) grub_printf("Invalid md5 hex format %s %d\n", pwdstr
, i
);
938 tmpPwd
.type
= VTOY_PASSWORD_MD5
;
940 else if ((len
- 4) > 32)
942 pos
= grub_strchr(pwdstr
+ 4, '#');
945 if (NULL
== pwd
) grub_printf("Invalid md5 password format %s\n", pwdstr
);
949 if (len
- 1 - (int)(long)(pos
- pwdstr
) != 32)
951 if (NULL
== pwd
) grub_printf("Invalid md5 salt password format %s\n", pwdstr
);
957 grub_snprintf(tmpPwd
.salt
, sizeof(tmpPwd
.salt
), "%s", pwdstr
+ 4);
961 for (i
= 0; i
< 16; i
++)
963 bytes
[0] = pos
[i
* 2];
964 bytes
[1] = pos
[i
* 2 + 1];
967 if (grub_isxdigit(bytes
[0]) && grub_isxdigit(bytes
[1]))
969 tmpPwd
.md5
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
973 if (NULL
== pwd
) grub_printf("Invalid md5 hex format %s %d\n", pwdstr
, i
);
978 tmpPwd
.type
= VTOY_PASSWORD_SALT_MD5
;
982 if (NULL
== pwd
) grub_printf("Invalid md5 password format %s\n", pwdstr
);
988 if (NULL
== pwd
) grub_printf("Invalid password format %s\n", pwdstr
);
994 grub_memcpy(pwd
, &tmpPwd
, sizeof(tmpPwd
));
1000 static int ventoy_plugin_get_pwd_type(const char *pwd
)
1005 for (i
= 0; pwd
&& i
< (int)ARRAY_SIZE(g_menu_prefix
); i
++)
1007 grub_snprintf(pwdtype
, sizeof(pwdtype
), "%spwd", g_menu_prefix
[i
]);
1008 if (grub_strcmp(pwdtype
, pwd
) == 0)
1010 return img_type_start
+ i
;
1017 static int ventoy_plugin_pwd_entry(VTOY_JSON
*json
, const char *isodisk
)
1020 const char *iso
= NULL
;
1021 const char *pwd
= NULL
;
1022 VTOY_JSON
*pNode
= NULL
;
1023 VTOY_JSON
*pCNode
= NULL
;
1024 menu_password
*node
= NULL
;
1025 menu_password
*tail
= NULL
;
1026 menu_password
*next
= NULL
;
1030 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
1032 debug("Not object %d\n", json
->enDataType
);
1038 for (node
= g_pwd_head
; node
; node
= next
)
1047 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1049 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
1051 ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, &g_boot_pwd
);
1053 else if ((type
= ventoy_plugin_get_pwd_type(pNode
->pcName
)) >= 0)
1055 ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, g_file_type_pwd
+ type
);
1057 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
1059 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
1061 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
1066 type
= vtoy_menu_pwd_file
;
1067 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
1070 type
= vtoy_menu_pwd_parent
;
1071 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "parent");
1074 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
1075 if (iso
&& pwd
&& iso
[0] == '/')
1077 node
= grub_zalloc(sizeof(menu_password
));
1081 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
1083 if (ventoy_plugin_parse_pwdstr((char *)pwd
, &(node
->password
)))
1107 static int ventoy_plugin_pwd_check(VTOY_JSON
*json
, const char *isodisk
)
1111 const char *iso
= NULL
;
1112 const char *pwd
= NULL
;
1113 VTOY_JSON
*pNode
= NULL
;
1114 VTOY_JSON
*pCNode
= NULL
;
1116 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
1118 grub_printf("Not object %d\n", json
->enDataType
);
1122 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1124 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
1126 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
1128 grub_printf("bootpwd:<%s>\n", pNode
->unData
.pcStrVal
);
1132 grub_printf("Invalid bootpwd.\n");
1135 else if ((type
= ventoy_plugin_get_pwd_type(pNode
->pcName
)) >= 0)
1137 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
1139 grub_printf("%s:<%s>\n", pNode
->pcName
, pNode
->unData
.pcStrVal
);
1143 grub_printf("Invalid pwd <%s>\n", pNode
->unData
.pcStrVal
);
1146 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
1149 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
1151 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
1153 grub_printf("Not object %d\n", pCNode
->enDataType
);
1157 if ((iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file")) != NULL
)
1159 pos
= grub_strchr(iso
, '*');
1160 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
1162 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
1164 if (0 == ventoy_plugin_parse_pwdstr((char *)pwd
, NULL
))
1166 grub_printf("file:<%s> [%s]\n", iso
, (pos
? "*" : "OK"));
1167 grub_printf("pwd:<%s>\n\n", pwd
);
1171 grub_printf("Invalid password for <%s>\n", iso
);
1176 grub_printf("<%s%s> not found\n", isodisk
, iso
);
1179 else if ((iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "parent")) != NULL
)
1181 if (ventoy_is_dir_exist("%s%s", isodisk
, iso
))
1183 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
1184 if (0 == ventoy_plugin_parse_pwdstr((char *)pwd
, NULL
))
1186 grub_printf("dir:<%s> [%s]\n", iso
, (pos
? "*" : "OK"));
1187 grub_printf("pwd:<%s>\n\n", pwd
);
1191 grub_printf("Invalid password for <%s>\n", iso
);
1196 grub_printf("<%s%s> not found\n", isodisk
, iso
);
1201 grub_printf("No file item found in json.\n");
1210 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
1216 const char *iso
= NULL
;
1217 VTOY_JSON
*pNode
= NULL
;
1219 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1221 grub_printf("Not array type %d\n", json
->enDataType
);
1225 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1227 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
1229 grub_printf("NOT object type\n");
1232 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1235 pos
= grub_strchr(iso
, '*');
1236 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
1238 grub_printf("image: %s [%s]\n", iso
, (pos
? "*" : "OK"));
1239 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
1241 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1243 if (autosel
>= 0 && autosel
<= pathnum
)
1245 grub_printf("autosel: %d [OK]\n", autosel
);
1249 grub_printf("autosel: %d [FAIL]\n", autosel
);
1253 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "timeout", &timeout
))
1257 grub_printf("timeout: %d [OK]\n", timeout
);
1261 grub_printf("timeout: %d [FAIL]\n", timeout
);
1267 grub_printf("image: %s [FAIL]\n", iso
);
1272 grub_printf("image not found\n");
1279 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
1284 const char *iso
= NULL
;
1285 VTOY_JSON
*pNode
= NULL
;
1286 persistence_config
*node
= NULL
;
1287 persistence_config
*next
= NULL
;
1288 file_fullpath
*backendpath
= NULL
;
1292 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1294 debug("Not array %d\n", json
->enDataType
);
1298 if (g_persistence_head
)
1300 for (node
= g_persistence_head
; node
; node
= next
)
1303 grub_check_free(node
->backendpath
);
1307 g_persistence_head
= NULL
;
1310 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1312 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1313 if (iso
&& iso
[0] == '/')
1315 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
1317 node
= grub_zalloc(sizeof(persistence_config
));
1320 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
1321 node
->backendpath
= backendpath
;
1322 node
->backendnum
= pathnum
;
1326 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1328 if (autosel
>= 0 && autosel
<= pathnum
)
1330 node
->autosel
= autosel
;
1334 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "timeout", &timeout
))
1338 node
->timeout
= timeout
;
1342 if (g_persistence_head
)
1344 node
->next
= g_persistence_head
;
1347 g_persistence_head
= node
;
1356 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
1359 const char *path
= NULL
;
1360 const char *alias
= NULL
;
1361 VTOY_JSON
*pNode
= NULL
;
1365 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1367 grub_printf("Not array %d\n", json
->enDataType
);
1371 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1373 type
= vtoy_alias_image_file
;
1374 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1377 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1378 type
= vtoy_alias_directory
;
1381 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1382 if (path
&& path
[0] == '/' && alias
)
1384 if (vtoy_alias_image_file
== type
)
1386 if (grub_strchr(path
, '*'))
1388 grub_printf("image: <%s> [ * ]\n", path
);
1390 else if (ventoy_is_file_exist("%s%s", isodisk
, path
))
1392 grub_printf("image: <%s> [ OK ]\n", path
);
1396 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
1401 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
1403 grub_printf("dir: <%s> [ OK ]\n", path
);
1407 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
1411 grub_printf("alias: <%s>\n\n", alias
);
1418 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
1421 const char *path
= NULL
;
1422 const char *alias
= NULL
;
1423 VTOY_JSON
*pNode
= NULL
;
1424 menu_alias
*node
= NULL
;
1425 menu_alias
*next
= NULL
;
1429 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1431 debug("Not array %d\n", json
->enDataType
);
1435 if (g_menu_alias_head
)
1437 for (node
= g_menu_alias_head
; node
; node
= next
)
1443 g_menu_alias_head
= NULL
;
1446 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1448 type
= vtoy_alias_image_file
;
1449 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1452 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1453 type
= vtoy_alias_directory
;
1456 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1457 if (path
&& path
[0] == '/' && alias
)
1459 node
= grub_zalloc(sizeof(menu_alias
));
1463 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1464 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1466 if (g_menu_alias_head
)
1468 node
->next
= g_menu_alias_head
;
1471 g_menu_alias_head
= node
;
1479 static int ventoy_plugin_menutip_check(VTOY_JSON
*json
, const char *isodisk
)
1481 const char *path
= NULL
;
1482 const char *tip
= NULL
;
1483 VTOY_JSON
*pNode
= NULL
;
1487 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
1489 grub_printf("Not object %d\n", json
->enDataType
);
1493 tip
= vtoy_json_get_string_ex(json
->pstChild
, "left");
1496 grub_printf("left: <%s>\n", tip
);
1499 tip
= vtoy_json_get_string_ex(json
->pstChild
, "top");
1502 grub_printf("top: <%s>\n", tip
);
1505 tip
= vtoy_json_get_string_ex(json
->pstChild
, "color");
1508 grub_printf("color: <%s>\n", tip
);
1511 pNode
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
1512 for (pNode
= pNode
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1514 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1515 if (path
&& path
[0] == '/')
1517 if (grub_strchr(path
, '*'))
1519 grub_printf("image: <%s> [ * ]\n", path
);
1521 else if (ventoy_check_file_exist("%s%s", isodisk
, path
))
1523 grub_printf("image: <%s> [ OK ]\n", path
);
1527 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
1530 tip
= vtoy_json_get_string_ex(pNode
->pstChild
, "tip");
1533 grub_printf("tip: <%s>\n", tip
);
1537 tip
= vtoy_json_get_string_ex(pNode
->pstChild
, "tip1");
1539 grub_printf("tip1: <%s>\n", tip
);
1541 grub_printf("tip1: <NULL>\n");
1543 tip
= vtoy_json_get_string_ex(pNode
->pstChild
, "tip2");
1545 grub_printf("tip2: <%s>\n", tip
);
1547 grub_printf("tip2: <NULL>\n");
1552 grub_printf("image: <%s> [ INVALID ]\n", path
);
1559 static int ventoy_plugin_menutip_entry(VTOY_JSON
*json
, const char *isodisk
)
1561 const char *path
= NULL
;
1562 const char *tip
= NULL
;
1563 VTOY_JSON
*pNode
= NULL
;
1564 menu_tip
*node
= NULL
;
1565 menu_tip
*next
= NULL
;
1569 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
1571 debug("Not object %d\n", json
->enDataType
);
1575 pNode
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "tips");
1578 debug("Not tips found\n");
1582 if (g_menu_tip_head
)
1584 for (node
= g_menu_tip_head
; node
; node
= next
)
1590 g_menu_tip_head
= NULL
;
1593 tip
= vtoy_json_get_string_ex(json
->pstChild
, "left");
1596 grub_env_set("VTOY_TIP_LEFT", tip
);
1599 tip
= vtoy_json_get_string_ex(json
->pstChild
, "top");
1602 grub_env_set("VTOY_TIP_TOP", tip
);
1605 tip
= vtoy_json_get_string_ex(json
->pstChild
, "color");
1608 grub_env_set("VTOY_TIP_COLOR", tip
);
1611 for (pNode
= pNode
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1613 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1614 if (path
&& path
[0] == '/')
1616 node
= grub_zalloc(sizeof(menu_tip
));
1619 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1621 tip
= vtoy_json_get_string_ex(pNode
->pstChild
, "tip");
1624 grub_snprintf(node
->tip1
, 1000, "%s", tip
);
1628 tip
= vtoy_json_get_string_ex(pNode
->pstChild
, "tip1");
1630 grub_snprintf(node
->tip1
, 1000, "%s", tip
);
1632 tip
= vtoy_json_get_string_ex(pNode
->pstChild
, "tip2");
1634 grub_snprintf(node
->tip2
, 1000, "%s", tip
);
1637 if (g_menu_tip_head
)
1639 node
->next
= g_menu_tip_head
;
1642 g_menu_tip_head
= node
;
1650 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
1653 const char *path
= NULL
;
1654 const char *archive
= NULL
;
1655 VTOY_JSON
*pNode
= NULL
;
1659 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1661 grub_printf("Not array %d\n", json
->enDataType
);
1665 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1667 type
= injection_type_file
;
1668 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1671 type
= injection_type_parent
;
1672 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1675 grub_printf("image/parent not found\n");
1680 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1683 grub_printf("archive not found\n");
1687 if (type
== injection_type_file
)
1689 if (grub_strchr(path
, '*'))
1691 grub_printf("image: <%s> [*]\n", path
);
1695 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
1700 grub_printf("parent: <%s> [%s]\n", path
,
1701 ventoy_is_dir_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
1704 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
1710 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
1713 const char *path
= NULL
;
1714 const char *archive
= NULL
;
1715 VTOY_JSON
*pNode
= NULL
;
1716 injection_config
*node
= NULL
;
1717 injection_config
*next
= NULL
;
1721 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1723 debug("Not array %d\n", json
->enDataType
);
1727 if (g_injection_head
)
1729 for (node
= g_injection_head
; node
; node
= next
)
1735 g_injection_head
= NULL
;
1738 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1740 type
= injection_type_file
;
1741 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1744 type
= injection_type_parent
;
1745 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1748 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1749 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
1751 node
= grub_zalloc(sizeof(injection_config
));
1755 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1756 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
1758 if (g_injection_head
)
1760 node
->next
= g_injection_head
;
1763 g_injection_head
= node
;
1771 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
1775 const char *key
= NULL
;
1776 const char *class = NULL
;
1777 VTOY_JSON
*pNode
= NULL
;
1778 menu_class
*tail
= NULL
;
1779 menu_class
*node
= NULL
;
1780 menu_class
*next
= NULL
;
1784 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1786 debug("Not array %d\n", json
->enDataType
);
1790 if (g_menu_class_head
)
1792 for (node
= g_menu_class_head
; node
; node
= next
)
1798 g_menu_class_head
= NULL
;
1801 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1804 type
= vtoy_class_image_file
;
1805 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1808 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1815 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1816 type
= vtoy_class_directory
;
1820 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1823 node
= grub_zalloc(sizeof(menu_class
));
1827 node
->parent
= parent
;
1828 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
1829 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
1831 if (g_menu_class_head
)
1837 g_menu_class_head
= node
;
1847 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
1849 const char *name
= NULL
;
1850 const char *key
= NULL
;
1851 const char *class = NULL
;
1852 VTOY_JSON
*pNode
= NULL
;
1856 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1858 grub_printf("Not array %d\n", json
->enDataType
);
1862 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1865 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1869 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1873 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1877 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1880 grub_printf("%s: <%s>\n", name
, key
);
1881 grub_printf("class: <%s>\n\n", class);
1888 static int ventoy_plugin_custom_boot_entry(VTOY_JSON
*json
, const char *isodisk
)
1892 const char *key
= NULL
;
1893 const char *cfg
= NULL
;
1894 VTOY_JSON
*pNode
= NULL
;
1895 custom_boot
*tail
= NULL
;
1896 custom_boot
*node
= NULL
;
1897 custom_boot
*next
= NULL
;
1901 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1903 debug("Not array %d\n", json
->enDataType
);
1907 if (g_custom_boot_head
)
1909 for (node
= g_custom_boot_head
; node
; node
= next
)
1915 g_custom_boot_head
= NULL
;
1918 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1920 type
= vtoy_custom_boot_image_file
;
1921 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "file");
1924 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1925 type
= vtoy_custom_boot_directory
;
1928 cfg
= vtoy_json_get_string_ex(pNode
->pstChild
, "vcfg");
1931 node
= grub_zalloc(sizeof(custom_boot
));
1935 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", key
);
1936 len
= (int)grub_snprintf(node
->cfg
, sizeof(node
->cfg
), "%s", cfg
);
1938 if (len
>= 5 && grub_strncmp(node
->cfg
+ len
- 5, ".vcfg", 5) == 0)
1940 if (g_custom_boot_head
)
1946 g_custom_boot_head
= node
;
1961 static int ventoy_plugin_custom_boot_check(VTOY_JSON
*json
, const char *isodisk
)
1965 const char *key
= NULL
;
1966 const char *cfg
= NULL
;
1967 VTOY_JSON
*pNode
= NULL
;
1971 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1973 grub_printf("Not array %d\n", json
->enDataType
);
1977 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1979 type
= vtoy_custom_boot_image_file
;
1980 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "file");
1983 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1984 type
= vtoy_custom_boot_directory
;
1987 cfg
= vtoy_json_get_string_ex(pNode
->pstChild
, "vcfg");
1988 len
= (int)grub_strlen(cfg
);
1991 if (len
< 5 || grub_strncmp(cfg
+ len
- 5, ".vcfg", 5))
1993 grub_printf("<%s> does not have \".vcfg\" suffix\n\n", cfg
);
1997 grub_printf("%s: <%s>\n", (type
== vtoy_custom_boot_directory
) ? "dir" : "file", key
);
1998 grub_printf("vcfg: <%s>\n\n", cfg
);
2006 static int ventoy_plugin_conf_replace_entry(VTOY_JSON
*json
, const char *isodisk
)
2009 const char *isof
= NULL
;
2010 const char *orgf
= NULL
;
2011 const char *newf
= NULL
;
2012 VTOY_JSON
*pNode
= NULL
;
2013 conf_replace
*tail
= NULL
;
2014 conf_replace
*node
= NULL
;
2015 conf_replace
*next
= NULL
;
2019 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
2021 debug("Not array %d\n", json
->enDataType
);
2025 if (g_conf_replace_head
)
2027 for (node
= g_conf_replace_head
; node
; node
= next
)
2033 g_conf_replace_head
= NULL
;
2036 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
2038 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
2039 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
2040 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
2041 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
2043 node
= grub_zalloc(sizeof(conf_replace
));
2046 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "img", &img
))
2051 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", isof
);
2052 grub_snprintf(node
->orgconf
, sizeof(node
->orgconf
), "%s", orgf
);
2053 grub_snprintf(node
->newconf
, sizeof(node
->newconf
), "%s", newf
);
2055 if (g_conf_replace_head
)
2061 g_conf_replace_head
= node
;
2071 static int ventoy_plugin_conf_replace_check(VTOY_JSON
*json
, const char *isodisk
)
2074 const char *isof
= NULL
;
2075 const char *orgf
= NULL
;
2076 const char *newf
= NULL
;
2077 VTOY_JSON
*pNode
= NULL
;
2078 grub_file_t file
= NULL
;
2083 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
2085 grub_printf("Not array %d\n", json
->enDataType
);
2089 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
2091 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
2092 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
2093 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
2094 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
2096 if (ventoy_check_file_exist("%s%s", isodisk
, isof
))
2098 grub_printf("iso:<%s> [OK]\n", isof
);
2100 grub_snprintf(cmd
, sizeof(cmd
), "loopback vtisocheck \"%s%s\"", isodisk
, isof
);
2101 grub_script_execute_sourcecode(cmd
);
2103 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "(vtisocheck)/%s", orgf
);
2106 if (grub_strcmp(file
->fs
->name
, "iso9660") == 0)
2108 grub_printf("org:<%s> [OK]\n", orgf
);
2112 grub_printf("org:<%s> [Exist But NOT ISO9660]\n", orgf
);
2114 grub_file_close(file
);
2118 grub_printf("org:<%s> [NOT Exist]\n", orgf
);
2121 grub_script_execute_sourcecode("loopback -d vtisocheck");
2123 else if (grub_strchr(isof
, '*'))
2125 grub_printf("iso:<%s> [*]\n", isof
);
2126 grub_printf("org:<%s>\n", orgf
);
2130 grub_printf("iso:<%s> [NOT Exist]\n", isof
);
2131 grub_printf("org:<%s>\n", orgf
);
2134 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isodisk
, newf
);
2137 if (file
->size
> vtoy_max_replace_file_size
)
2139 grub_printf("new:<%s> [Too Big %lu] \n", newf
, (ulong
)file
->size
);
2143 grub_printf("new1:<%s> [OK]\n", newf
);
2145 grub_file_close(file
);
2149 grub_printf("new:<%s> [NOT Exist]\n", newf
);
2152 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "img", &img
))
2154 grub_printf("img:<%d>\n", img
);
2164 static int ventoy_plugin_auto_memdisk_entry(VTOY_JSON
*json
, const char *isodisk
)
2166 VTOY_JSON
*pNode
= NULL
;
2167 auto_memdisk
*node
= NULL
;
2168 auto_memdisk
*next
= NULL
;
2172 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
2174 debug("Not array %d\n", json
->enDataType
);
2178 if (g_auto_memdisk_head
)
2180 for (node
= g_auto_memdisk_head
; node
; node
= next
)
2186 g_auto_memdisk_head
= NULL
;
2189 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
2191 if (pNode
->enDataType
== JSON_TYPE_STRING
)
2193 node
= grub_zalloc(sizeof(auto_memdisk
));
2196 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
2198 if (g_auto_memdisk_head
)
2200 node
->next
= g_auto_memdisk_head
;
2203 g_auto_memdisk_head
= node
;
2211 static int ventoy_plugin_auto_memdisk_check(VTOY_JSON
*json
, const char *isodisk
)
2213 VTOY_JSON
*pNode
= NULL
;
2215 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
2217 grub_printf("Not array %d\n", json
->enDataType
);
2221 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
2223 if (pNode
->enDataType
== JSON_TYPE_STRING
)
2225 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
2227 if (grub_strchr(pNode
->unData
.pcStrVal
, '*'))
2229 grub_printf(" [*]\n");
2231 else if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
2233 grub_printf(" [OK]\n");
2237 grub_printf(" [NOT EXIST]\n");
2245 static int ventoy_plugin_image_list_entry(VTOY_JSON
*json
, const char *isodisk
)
2247 VTOY_JSON
*pNode
= NULL
;
2248 image_list
*node
= NULL
;
2249 image_list
*next
= NULL
;
2250 image_list
*tail
= NULL
;
2254 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
2256 debug("Not array %d\n", json
->enDataType
);
2260 if (g_image_list_head
)
2262 for (node
= g_image_list_head
; node
; node
= next
)
2268 g_image_list_head
= NULL
;
2271 if (grub_strncmp(json
->pcName
, "image_blacklist", 15) == 0)
2273 g_plugin_image_list
= VENTOY_IMG_BLACK_LIST
;
2277 g_plugin_image_list
= VENTOY_IMG_WHITE_LIST
;
2280 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
2282 if (pNode
->enDataType
== JSON_TYPE_STRING
)
2284 node
= grub_zalloc(sizeof(image_list
));
2287 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
2289 if (g_image_list_head
)
2295 g_image_list_head
= node
;
2305 static int ventoy_plugin_image_list_check(VTOY_JSON
*json
, const char *isodisk
)
2307 VTOY_JSON
*pNode
= NULL
;
2309 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
2311 grub_printf("Not array %d\n", json
->enDataType
);
2315 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
2317 if (pNode
->enDataType
== JSON_TYPE_STRING
)
2319 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
2321 if (grub_strchr(pNode
->unData
.pcStrVal
, '*'))
2323 grub_printf(" [*]\n");
2325 else if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
2327 grub_printf(" [OK]\n");
2331 grub_printf(" [NOT EXIST]\n");
2339 static plugin_entry g_plugin_entries
[] =
2341 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
, 0 },
2342 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
, 0 },
2343 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
, 0 },
2344 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
, 0 },
2345 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
, 0 },
2346 { "menu_tip", ventoy_plugin_menutip_entry
, ventoy_plugin_menutip_check
, 0 },
2347 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
, 0 },
2348 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
, 0 },
2349 { "auto_memdisk", ventoy_plugin_auto_memdisk_entry
, ventoy_plugin_auto_memdisk_check
, 0 },
2350 { "image_list", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
, 0 },
2351 { "image_blacklist", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
, 0 },
2352 { "conf_replace", ventoy_plugin_conf_replace_entry
, ventoy_plugin_conf_replace_check
, 0 },
2353 { "dud", ventoy_plugin_dud_entry
, ventoy_plugin_dud_check
, 0 },
2354 { "password", ventoy_plugin_pwd_entry
, ventoy_plugin_pwd_check
, 0 },
2355 { "custom_boot", ventoy_plugin_custom_boot_entry
, ventoy_plugin_custom_boot_check
, 0 },
2358 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
2362 VTOY_JSON
*cur
= NULL
;
2364 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
2366 for (cur
= json
; cur
; cur
= cur
->pstNext
)
2368 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
2370 grub_snprintf(key
, sizeof(key
), "%s_%s", g_plugin_entries
[i
].key
, g_arch_mode_suffix
);
2371 if (g_plugin_entries
[i
].flag
== 0 && grub_strcmp(key
, cur
->pcName
) == 0)
2373 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
2374 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
2375 g_plugin_entries
[i
].flag
= 1;
2382 for (cur
= json
; cur
; cur
= cur
->pstNext
)
2384 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
2386 if (g_plugin_entries
[i
].flag
== 0 && grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
2388 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
2389 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
2390 g_plugin_entries
[i
].flag
= 1;
2399 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2404 VTOY_JSON
*json
= NULL
;
2409 grub_env_set("VTOY_TIP_LEFT", "10%");
2410 grub_env_set("VTOY_TIP_TOP", "80%+5");
2411 grub_env_set("VTOY_TIP_COLOR", "blue");
2412 grub_env_set("VTOY_TIP_ALIGN", "left");
2414 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
2417 return GRUB_ERR_NONE
;
2420 debug("json configuration file size %d\n", (int)file
->size
);
2422 buf
= grub_malloc(file
->size
+ 1);
2425 grub_file_close(file
);
2429 buf
[file
->size
] = 0;
2430 grub_file_read(file
, buf
, file
->size
);
2431 grub_file_close(file
);
2433 json
= vtoy_json_create();
2441 ret
= vtoy_json_parse(json
, buf
);
2444 grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
2445 grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR");
2447 debug("Failed to parse json string %d\n", ret
);
2452 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
2454 vtoy_json_destroy(json
);
2458 if (g_boot_pwd
.type
)
2460 grub_printf("\n\n======= %s ======\n\n", grub_env_get("VTOY_TEXT_MENU_VER"));
2461 if (ventoy_check_password(&g_boot_pwd
, 3))
2463 grub_printf("\n!!! Password check failed, will exit after 5 seconds. !!!\n");
2470 if (g_menu_tip_head
)
2472 grub_env_set("VTOY_MENU_TIP_ENABLE", "1");
2476 grub_env_unset("VTOY_MENU_TIP_ENABLE");
2479 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2482 void ventoy_plugin_dump_injection(void)
2484 injection_config
*node
= NULL
;
2486 for (node
= g_injection_head
; node
; node
= node
->next
)
2488 grub_printf("\n%s:<%s>\n", (node
->type
== injection_type_file
) ? "IMAGE" : "PARENT", node
->isopath
);
2489 grub_printf("ARCHIVE:<%s>\n", node
->archive
);
2496 void ventoy_plugin_dump_auto_install(void)
2499 install_template
*node
= NULL
;
2501 for (node
= g_install_template_head
; node
; node
= node
->next
)
2503 grub_printf("\n%s:<%s> <%d>\n",
2504 (node
->type
== auto_install_type_file
) ? "IMAGE" : "PARENT",
2505 node
->isopath
, node
->templatenum
);
2506 for (i
= 0; i
< node
->templatenum
; i
++)
2508 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
2515 void ventoy_plugin_dump_persistence(void)
2519 persistence_config
*node
= NULL
;
2520 ventoy_img_chunk_list chunk_list
;
2522 for (node
= g_persistence_head
; node
; node
= node
->next
)
2524 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
2526 for (i
= 0; i
< node
->backendnum
; i
++)
2528 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
2529 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
2532 grub_printf(" [ SUCCESS ]\n");
2533 grub_free(chunk_list
.chunk
);
2537 grub_printf(" [ FAILED ]\n");
2545 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
2548 install_template
*node
= NULL
;
2550 if (!g_install_template_head
)
2555 len
= (int)grub_strlen(isopath
);
2556 for (node
= g_install_template_head
; node
; node
= node
->next
)
2558 if (node
->type
== auto_install_type_file
)
2560 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2567 for (node
= g_install_template_head
; node
; node
= node
->next
)
2569 if (node
->type
== auto_install_type_parent
)
2571 if (node
->pathlen
< len
&& ventoy_plugin_is_parent(node
->isopath
, node
->pathlen
, isopath
))
2581 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
2583 install_template
*node
= NULL
;
2585 node
= ventoy_plugin_find_install_template(isopath
);
2586 if ((!node
) || (!node
->templatepath
))
2591 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
2596 return node
->templatepath
[node
->cursel
].path
;
2599 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
2602 persistence_config
*node
= NULL
;
2604 if (!g_persistence_head
)
2609 len
= (int)grub_strlen(isopath
);
2610 for (node
= g_persistence_head
; node
; node
= node
->next
)
2612 if ((len
== node
->pathlen
) && (ventoy_strcmp(node
->isopath
, isopath
) == 0))
2621 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
2624 grub_uint64_t start
= 0;
2625 grub_file_t file
= NULL
;
2626 persistence_config
*node
= NULL
;
2628 node
= ventoy_plugin_find_persistent(isopath
);
2629 if ((!node
) || (!node
->backendpath
))
2636 index
= node
->cursel
;
2639 if (index
< 0 || index
>= node
->backendnum
)
2644 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
2647 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
2651 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
2652 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
2653 if (NULL
== chunk_list
->chunk
)
2658 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
2659 chunk_list
->cur_chunk
= 0;
2661 start
= file
->device
->disk
->partition
->start
;
2662 ventoy_get_block_list(file
, chunk_list
, start
);
2664 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
2666 grub_free(chunk_list
->chunk
);
2667 chunk_list
->chunk
= NULL
;
2675 grub_file_close(file
);
2680 const char * ventoy_plugin_get_injection(const char *isopath
)
2683 injection_config
*node
= NULL
;
2685 if (!g_injection_head
)
2690 len
= (int)grub_strlen(isopath
);
2691 for (node
= g_injection_head
; node
; node
= node
->next
)
2693 if (node
->type
== injection_type_file
)
2695 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2697 return node
->archive
;
2702 for (node
= g_injection_head
; node
; node
= node
->next
)
2704 if (node
->type
== injection_type_parent
)
2706 if (node
->pathlen
< len
&& ventoy_plugin_is_parent(node
->isopath
, node
->pathlen
, isopath
))
2708 return node
->archive
;
2716 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
2719 menu_alias
*node
= NULL
;
2721 if (!g_menu_alias_head
)
2726 len
= (int)grub_strlen(isopath
);
2727 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
2729 if (node
->type
== type
&& node
->pathlen
&&
2730 node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2739 const menu_tip
* ventoy_plugin_get_menu_tip(const char *isopath
)
2742 menu_tip
*node
= NULL
;
2744 if (!g_menu_tip_head
)
2749 len
= (int)grub_strlen(isopath
);
2750 for (node
= g_menu_tip_head
; node
; node
= node
->next
)
2752 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2761 const char * ventoy_plugin_get_menu_class(int type
, const char *name
, const char *path
)
2765 menu_class
*node
= NULL
;
2767 if (!g_menu_class_head
)
2772 namelen
= (int)grub_strlen(name
);
2773 pathlen
= (int)grub_strlen(path
);
2775 if (vtoy_class_image_file
== type
)
2777 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2779 if (node
->type
!= type
)
2784 if (node
->parent
== 0)
2786 if ((node
->patlen
< namelen
) && grub_strstr(name
, node
->pattern
))
2793 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2795 if (node
->type
!= type
)
2802 if ((node
->patlen
< pathlen
) && ventoy_plugin_is_parent(node
->pattern
, node
->patlen
, path
))
2811 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2813 if (node
->type
== type
&& node
->patlen
== namelen
&& grub_strncmp(name
, node
->pattern
, namelen
) == 0)
2823 int ventoy_plugin_add_custom_boot(const char *vcfgpath
)
2826 custom_boot
*node
= NULL
;
2828 node
= grub_zalloc(sizeof(custom_boot
));
2831 node
->type
= vtoy_custom_boot_image_file
;
2832 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", vcfgpath
);
2833 grub_snprintf(node
->cfg
, sizeof(node
->cfg
), "%s", vcfgpath
);
2836 len
= node
->pathlen
- 5;
2837 node
->path
[len
] = 0;
2838 node
->pathlen
= len
;
2840 if (g_custom_boot_head
)
2842 node
->next
= g_custom_boot_head
;
2844 g_custom_boot_head
= node
;
2850 const char * ventoy_plugin_get_custom_boot(const char *isopath
)
2854 custom_boot
*node
= NULL
;
2856 if (!g_custom_boot_head
)
2861 len
= (int)grub_strlen(isopath
);
2863 for (node
= g_custom_boot_head
; node
; node
= node
->next
)
2865 if (node
->type
== vtoy_custom_boot_image_file
)
2867 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->path
, len
) == 0)
2874 if (node
->pathlen
< len
&& isopath
[node
->pathlen
] == '/' &&
2875 grub_strncmp(isopath
, node
->path
, node
->pathlen
) == 0)
2877 for (i
= node
->pathlen
+ 1; i
< len
; i
++)
2879 if (isopath
[i
] == '/')
2896 grub_err_t
ventoy_cmd_dump_custom_boot(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2898 custom_boot
*node
= NULL
;
2904 for (node
= g_custom_boot_head
; node
; node
= node
->next
)
2906 grub_printf("[%s] <%s>:<%s>\n", (node
->type
== vtoy_custom_boot_directory
) ? "dir" : "file",
2907 node
->path
, node
->cfg
);
2913 int ventoy_plugin_check_memdisk(const char *isopath
)
2916 auto_memdisk
*node
= NULL
;
2918 if (!g_auto_memdisk_head
)
2923 len
= (int)grub_strlen(isopath
);
2924 for (node
= g_auto_memdisk_head
; node
; node
= node
->next
)
2926 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, isopath
, len
) == 0)
2935 int ventoy_plugin_get_image_list_index(int type
, const char *name
)
2939 image_list
*node
= NULL
;
2941 if (!g_image_list_head
)
2946 len
= (int)grub_strlen(name
);
2948 for (node
= g_image_list_head
; node
; node
= node
->next
, index
++)
2950 if (vtoy_class_directory
== type
)
2952 if (len
< node
->pathlen
&& ventoy_strncmp(node
->isopath
, name
, len
) == 0)
2959 if (len
== node
->pathlen
&& ventoy_strncmp(node
->isopath
, name
, len
) == 0)
2969 conf_replace
* ventoy_plugin_find_conf_replace(const char *iso
)
2974 if (!g_conf_replace_head
)
2979 len
= (int)grub_strlen(iso
);
2981 for (node
= g_conf_replace_head
; node
; node
= node
->next
)
2983 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, iso
, len
) == 0)
2992 dud
* ventoy_plugin_find_dud(const char *iso
)
3002 len
= (int)grub_strlen(iso
);
3003 for (node
= g_dud_head
; node
; node
= node
->next
)
3005 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, iso
, len
) == 0)
3014 int ventoy_plugin_load_dud(dud
*node
, const char *isopart
)
3020 for (i
= 0; i
< node
->dudnum
; i
++)
3022 if (node
->files
[i
].size
> 0)
3024 debug("file %d has been loaded\n", i
);
3028 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isopart
, node
->dudpath
[i
].path
);
3031 buf
= grub_malloc(file
->size
);
3034 grub_file_read(file
, buf
, file
->size
);
3035 node
->files
[i
].size
= (int)file
->size
;
3036 node
->files
[i
].buf
= buf
;
3038 grub_file_close(file
);
3045 static const vtoy_password
* ventoy_plugin_get_password(const char *isopath
)
3049 const char *pos
= NULL
;
3050 menu_password
*node
= NULL
;
3059 len
= (int)grub_strlen(isopath
);
3060 for (node
= g_pwd_head
; node
; node
= node
->next
)
3062 if (node
->type
== vtoy_menu_pwd_file
)
3064 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, isopath
, len
) == 0)
3066 return &(node
->password
);
3071 for (node
= g_pwd_head
; node
; node
= node
->next
)
3073 if (node
->type
== vtoy_menu_pwd_parent
)
3075 if (node
->pathlen
< len
&& ventoy_plugin_is_parent(node
->isopath
, node
->pathlen
, isopath
))
3077 return &(node
->password
);
3085 if (*isopath
== '.')
3094 for (i
= 0; i
< (int)ARRAY_SIZE(g_menu_prefix
); i
++)
3096 if (g_file_type_pwd
[i
].type
&& 0 == grub_strcasecmp(pos
+ 1, g_menu_prefix
[i
]))
3098 return g_file_type_pwd
+ i
;
3106 grub_err_t
ventoy_cmd_check_password(grub_extcmd_context_t ctxt
, int argc
, char **args
)
3109 const vtoy_password
*pwd
= NULL
;
3114 pwd
= ventoy_plugin_get_password(args
[0]);
3117 if (0 == ventoy_check_password(pwd
, 1))
3135 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
3142 VTOY_JSON
*node
= NULL
;
3143 VTOY_JSON
*json
= NULL
;
3152 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
3155 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
3156 grub_printf("Attention: directory name and filename are both case-sensitive.\n");
3160 buf
= grub_malloc(file
->size
+ 1);
3163 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
3167 buf
[file
->size
] = 0;
3168 grub_file_read(file
, buf
, file
->size
);
3170 json
= vtoy_json_create();
3173 grub_printf("Failed to create json\n");
3177 ret
= vtoy_json_parse(json
, buf
);
3180 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
3184 grub_snprintf(key
, sizeof(key
), "%s_%s", args
[1], g_arch_mode_suffix
);
3185 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
3187 if (grub_strcmp(node
->pcName
, key
) == 0)
3195 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
3197 if (grub_strcmp(node
->pcName
, args
[1]) == 0)
3205 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
3210 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
3212 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
3214 if (g_plugin_entries
[i
].checkfunc
)
3216 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
3223 check_free(file
, grub_file_close
);
3224 check_free(json
, vtoy_json_destroy
);
3225 grub_check_free(buf
);
3230 grub_err_t
ventoy_cmd_set_theme(grub_extcmd_context_t ctxt
, int argc
, char **args
)
3232 grub_uint32_t i
= 0;
3233 grub_uint32_t mod
= 0;
3234 theme_list
*node
= g_theme_head
;
3235 struct grub_datetime datetime
;
3241 if (g_theme_single_file
[0])
3243 debug("single theme %s\n", g_theme_single_file
);
3244 grub_env_set("theme", g_theme_single_file
);
3248 debug("g_theme_num = %d\n", g_theme_num
);
3250 if (g_theme_num
== 0)
3255 grub_memset(&datetime
, 0, sizeof(datetime
));
3256 grub_get_datetime(&datetime
);
3258 if (g_theme_random
== vtoy_theme_random_boot_second
)
3260 grub_divmod32((grub_uint32_t
)datetime
.second
, (grub_uint32_t
)g_theme_num
, &mod
);
3262 else if (g_theme_random
== vtoy_theme_random_boot_day
)
3264 grub_divmod32((grub_uint32_t
)datetime
.day
, (grub_uint32_t
)g_theme_num
, &mod
);
3266 else if (g_theme_random
== vtoy_theme_random_boot_month
)
3268 grub_divmod32((grub_uint32_t
)datetime
.month
, (grub_uint32_t
)g_theme_num
, &mod
);
3271 debug("%04d/%02d/%02d %02d:%02d:%02d radom:%d mod:%d\n",
3272 datetime
.year
, datetime
.month
, datetime
.day
,
3273 datetime
.hour
, datetime
.minute
, datetime
.second
,
3274 g_theme_random
, mod
);
3276 for (i
= 0; i
< mod
&& node
; i
++)
3281 debug("random theme %s\n", node
->theme
.path
);
3282 grub_env_set("theme", node
->theme
.path
);
3285 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);