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_alias
*g_menu_alias_head
= NULL
;
52 static menu_class
*g_menu_class_head
= NULL
;
53 static custom_boot
*g_custom_boot_head
= NULL
;
54 static injection_config
*g_injection_head
= NULL
;
55 static auto_memdisk
*g_auto_memdisk_head
= NULL
;
56 static image_list
*g_image_list_head
= NULL
;
57 static conf_replace
*g_conf_replace_head
= NULL
;
59 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
62 VTOY_JSON
*pNode
= NULL
;
63 VTOY_JSON
*pChild
= NULL
;
67 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
69 grub_printf("Not array type %d\n", json
->enDataType
);
73 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
75 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
77 pChild
= pNode
->pstChild
;
78 if (pChild
->enDataType
== JSON_TYPE_STRING
)
80 if (grub_strcmp(pChild
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
82 grub_printf("%s: %s [%s]\n", pChild
->pcName
, pChild
->unData
.pcStrVal
,
83 ventoy_check_file_exist("%s%s", isodisk
, pChild
->unData
.pcStrVal
) ? "OK" : "NOT EXIST");
87 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
92 grub_printf("%s is NOT string type\n", pChild
->pcName
);
98 grub_printf("%s is not an object\n", pNode
->pcName
);
106 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
108 VTOY_JSON
*pNode
= NULL
;
109 VTOY_JSON
*pChild
= NULL
;
113 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
115 debug("Not array %d\n", json
->enDataType
);
119 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
121 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
123 pChild
= pNode
->pstChild
;
124 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
126 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
134 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
140 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
143 grub_printf("file: %s\n", value
);
146 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
150 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
155 grub_printf("Theme file %s does NOT exist\n", value
);
160 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
163 grub_printf("gfxmode: %s\n", value
);
166 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
169 grub_printf("display_mode: %s\n", value
);
172 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
175 grub_printf("serial_param %s\n", value
);
178 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
181 grub_printf("ventoy_left: %s\n", value
);
184 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
187 grub_printf("ventoy_top: %s\n", value
);
190 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
193 grub_printf("ventoy_color: %s\n", value
);
196 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
199 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
201 if (node
->enDataType
== JSON_TYPE_STRING
)
203 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
205 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
209 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
216 grub_printf("fonts NOT found\n");
222 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
228 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
233 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
237 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
240 if (ventoy_is_file_exist(filepath
) == 0)
242 debug("Theme file %s does not exist\n", filepath
);
246 debug("vtoy_theme %s\n", filepath
);
247 grub_env_set("vtoy_theme", filepath
);
250 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
253 debug("vtoy_gfxmode %s\n", value
);
254 grub_env_set("vtoy_gfxmode", value
);
257 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
260 debug("display_mode %s\n", value
);
261 grub_env_set("vtoy_display_mode", value
);
264 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
267 debug("serial_param %s\n", value
);
268 grub_env_set("vtoy_serial_param", value
);
271 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
274 grub_env_set("VTLE_LFT", value
);
277 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
280 grub_env_set("VTLE_TOP", value
);
283 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
286 grub_env_set("VTLE_CLR", value
);
289 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
292 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
294 if (node
->enDataType
== JSON_TYPE_STRING
&&
295 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
297 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
298 grub_font_load(filepath
);
306 static int ventoy_plugin_check_path(const char *path
, const char *file
)
310 grub_printf("%s is NOT begin with '/' \n", file
);
314 if (grub_strchr(file
, '\\'))
316 grub_printf("%s contains invalid '\\' \n", file
);
320 if (grub_strstr(file
, "//"))
322 grub_printf("%s contains invalid double slash\n", file
);
326 if (grub_strstr(file
, "../"))
328 grub_printf("%s contains invalid '../' \n", file
);
332 if (!ventoy_is_file_exist("%s%s", path
, file
))
334 grub_printf("%s%s does NOT exist\n", path
, file
);
341 static int ventoy_plugin_check_fullpath
352 VTOY_JSON
*node
= json
;
353 VTOY_JSON
*child
= NULL
;
357 if (0 == grub_strcmp(key
, node
->pcName
))
361 node
= node
->pstNext
;
369 if (JSON_TYPE_STRING
== node
->enDataType
)
372 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
373 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
375 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
377 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
379 if (JSON_TYPE_STRING
!= child
->enDataType
)
381 grub_printf("Non string json type\n");
385 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
386 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
397 static int ventoy_plugin_parse_fullpath
402 file_fullpath
**fullpath
,
408 VTOY_JSON
*node
= json
;
409 VTOY_JSON
*child
= NULL
;
410 file_fullpath
*path
= NULL
;
414 if (0 == grub_strcmp(key
, node
->pcName
))
418 node
= node
->pstNext
;
426 if (JSON_TYPE_STRING
== node
->enDataType
)
428 debug("%s is string type data\n", node
->pcName
);
430 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
432 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
436 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
439 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
445 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
447 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
449 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
451 debug("Invalid data type:%d\n", child
->enDataType
);
456 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
458 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
463 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
465 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
467 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
481 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
486 const char *iso
= NULL
;
487 VTOY_JSON
*pNode
= NULL
;
489 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
491 grub_printf("Not array type %d\n", json
->enDataType
);
495 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
497 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
499 grub_printf("NOT object type\n");
502 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
505 pos
= grub_strchr(iso
, '*');
506 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
508 grub_printf("image: %s [%s]\n", iso
, (pos
? "*" : "OK"));
509 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
511 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
513 if (autosel
>= 0 && autosel
<= pathnum
)
515 grub_printf("autosel: %d [OK]\n", autosel
);
519 grub_printf("autosel: %d [FAIL]\n", autosel
);
525 grub_printf("image: %s [FAIL]\n", iso
);
530 grub_printf("image not found\n");
537 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
541 const char *iso
= NULL
;
542 VTOY_JSON
*pNode
= NULL
;
543 install_template
*node
= NULL
;
544 install_template
*next
= NULL
;
545 file_fullpath
*templatepath
= NULL
;
547 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
549 debug("Not array %d\n", json
->enDataType
);
553 if (g_install_template_head
)
555 for (node
= g_install_template_head
; node
; node
= next
)
558 grub_check_free(node
->templatepath
);
562 g_install_template_head
= NULL
;
565 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
567 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
568 if (iso
&& iso
[0] == '/')
570 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
572 node
= grub_zalloc(sizeof(install_template
));
575 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
576 node
->templatepath
= templatepath
;
577 node
->templatenum
= pathnum
;
580 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
582 if (autosel
>= 0 && autosel
<= pathnum
)
584 node
->autosel
= autosel
;
588 if (g_install_template_head
)
590 node
->next
= g_install_template_head
;
593 g_install_template_head
= node
;
602 static int ventoy_plugin_dud_check(VTOY_JSON
*json
, const char *isodisk
)
606 const char *iso
= NULL
;
607 VTOY_JSON
*pNode
= NULL
;
609 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
611 grub_printf("Not array type %d\n", json
->enDataType
);
615 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
617 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
619 grub_printf("NOT object type\n");
622 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
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
, "dud", &pathnum
);
633 grub_printf("image: %s [FAIL]\n", iso
);
638 grub_printf("image not found\n");
645 static int ventoy_plugin_dud_entry(VTOY_JSON
*json
, const char *isodisk
)
648 const char *iso
= NULL
;
649 VTOY_JSON
*pNode
= NULL
;
652 file_fullpath
*dudpath
= NULL
;
654 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
656 debug("Not array %d\n", json
->enDataType
);
662 for (node
= g_dud_head
; node
; node
= next
)
665 grub_check_free(node
->dudpath
);
672 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
674 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
675 if (iso
&& iso
[0] == '/')
677 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "dud", &dudpath
, &pathnum
))
679 node
= grub_zalloc(sizeof(dud
));
682 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
683 node
->dudpath
= dudpath
;
684 node
->dudnum
= pathnum
;
685 node
->files
= grub_zalloc(sizeof(dudfile
) * pathnum
);
691 node
->next
= g_dud_head
;
708 static int ventoy_plugin_parse_pwdstr(char *pwdstr
, vtoy_password
*pwd
)
715 vtoy_password tmpPwd
;
717 len
= (int)grub_strlen(pwdstr
);
720 if (NULL
== pwd
) grub_printf("Password too long %d\n", len
);
724 grub_memset(&tmpPwd
, 0, sizeof(tmpPwd
));
726 if (grub_strncmp(pwdstr
, "txt#", 4) == 0)
728 tmpPwd
.type
= VTOY_PASSWORD_TXT
;
729 grub_snprintf(tmpPwd
.text
, sizeof(tmpPwd
.text
), "%s", pwdstr
+ 4);
731 else if (grub_strncmp(pwdstr
, "md5#", 4) == 0)
735 for (i
= 0; i
< 16; i
++)
737 bytes
[0] = pwdstr
[4 + i
* 2];
738 bytes
[1] = pwdstr
[4 + i
* 2 + 1];
741 if (grub_isxdigit(bytes
[0]) && grub_isxdigit(bytes
[1]))
743 tmpPwd
.md5
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
747 if (NULL
== pwd
) grub_printf("Invalid md5 hex format %s %d\n", pwdstr
, i
);
751 tmpPwd
.type
= VTOY_PASSWORD_MD5
;
753 else if ((len
- 4) > 32)
755 pos
= grub_strchr(pwdstr
+ 4, '#');
758 if (NULL
== pwd
) grub_printf("Invalid md5 password format %s\n", pwdstr
);
762 if (len
- 1 - (int)(long)(pos
- pwdstr
) != 32)
764 if (NULL
== pwd
) grub_printf("Invalid md5 salt password format %s\n", pwdstr
);
770 grub_snprintf(tmpPwd
.salt
, sizeof(tmpPwd
.salt
), "%s", pwdstr
+ 4);
774 for (i
= 0; i
< 16; i
++)
776 bytes
[0] = pos
[i
* 2];
777 bytes
[1] = pos
[i
* 2 + 1];
780 if (grub_isxdigit(bytes
[0]) && grub_isxdigit(bytes
[1]))
782 tmpPwd
.md5
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
786 if (NULL
== pwd
) grub_printf("Invalid md5 hex format %s %d\n", pwdstr
, i
);
791 tmpPwd
.type
= VTOY_PASSWORD_SALT_MD5
;
795 if (NULL
== pwd
) grub_printf("Invalid md5 password format %s\n", pwdstr
);
801 if (NULL
== pwd
) grub_printf("Invalid password format %s\n", pwdstr
);
807 grub_memcpy(pwd
, &tmpPwd
, sizeof(tmpPwd
));
813 static int ventoy_plugin_get_pwd_type(const char *pwd
)
818 for (i
= 0; pwd
&& i
< (int)ARRAY_SIZE(g_menu_prefix
); i
++)
820 grub_snprintf(pwdtype
, sizeof(pwdtype
), "%spwd", g_menu_prefix
[i
]);
821 if (grub_strcmp(pwdtype
, pwd
) == 0)
823 return img_type_start
+ i
;
830 static int ventoy_plugin_pwd_entry(VTOY_JSON
*json
, const char *isodisk
)
833 const char *iso
= NULL
;
834 const char *pwd
= NULL
;
835 VTOY_JSON
*pNode
= NULL
;
836 VTOY_JSON
*pCNode
= NULL
;
837 menu_password
*node
= NULL
;
838 menu_password
*tail
= NULL
;
839 menu_password
*next
= NULL
;
843 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
845 debug("Not object %d\n", json
->enDataType
);
851 for (node
= g_pwd_head
; node
; node
= next
)
860 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
862 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
864 ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, &g_boot_pwd
);
866 else if ((type
= ventoy_plugin_get_pwd_type(pNode
->pcName
)) >= 0)
868 ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, g_file_type_pwd
+ type
);
870 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
872 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
874 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
879 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
880 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
881 if (iso
&& pwd
&& iso
[0] == '/')
883 node
= grub_zalloc(sizeof(menu_password
));
886 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
888 if (ventoy_plugin_parse_pwdstr((char *)pwd
, &(node
->password
)))
912 static int ventoy_plugin_pwd_check(VTOY_JSON
*json
, const char *isodisk
)
916 const char *iso
= NULL
;
917 const char *pwd
= NULL
;
918 VTOY_JSON
*pNode
= NULL
;
919 VTOY_JSON
*pCNode
= NULL
;
921 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
923 grub_printf("Not object %d\n", json
->enDataType
);
927 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
929 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
931 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
933 grub_printf("bootpwd:<%s>\n", pNode
->unData
.pcStrVal
);
937 grub_printf("Invalid bootpwd.\n");
940 else if ((type
= ventoy_plugin_get_pwd_type(pNode
->pcName
)) >= 0)
942 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
944 grub_printf("%s:<%s>\n", pNode
->pcName
, pNode
->unData
.pcStrVal
);
948 grub_printf("Invalid pwd <%s>\n", pNode
->unData
.pcStrVal
);
951 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
954 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
956 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
958 grub_printf("Not object %d\n", pCNode
->enDataType
);
962 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
965 pos
= grub_strchr(iso
, '*');
966 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
968 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
970 if (0 == ventoy_plugin_parse_pwdstr((char *)pwd
, NULL
))
972 grub_printf("file:<%s> [%s]\n", iso
, (pos
? "*" : "OK"));
973 grub_printf("pwd:<%s>\n\n", pwd
);
977 grub_printf("Invalid password for <%s>\n", iso
);
982 grub_printf("<%s%s> not found\n", isodisk
, iso
);
987 grub_printf("No file item found in json.\n");
996 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
1001 const char *iso
= NULL
;
1002 VTOY_JSON
*pNode
= NULL
;
1004 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1006 grub_printf("Not array type %d\n", json
->enDataType
);
1010 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1012 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
1014 grub_printf("NOT object type\n");
1017 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1020 pos
= grub_strchr(iso
, '*');
1021 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
1023 grub_printf("image: %s [%s]\n", iso
, (pos
? "*" : "OK"));
1024 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
1026 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1028 if (autosel
>= 0 && autosel
<= pathnum
)
1030 grub_printf("autosel: %d [OK]\n", autosel
);
1034 grub_printf("autosel: %d [FAIL]\n", autosel
);
1040 grub_printf("image: %s [FAIL]\n", iso
);
1045 grub_printf("image not found\n");
1052 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
1056 const char *iso
= NULL
;
1057 VTOY_JSON
*pNode
= NULL
;
1058 persistence_config
*node
= NULL
;
1059 persistence_config
*next
= NULL
;
1060 file_fullpath
*backendpath
= NULL
;
1064 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1066 debug("Not array %d\n", json
->enDataType
);
1070 if (g_persistence_head
)
1072 for (node
= g_persistence_head
; node
; node
= next
)
1075 grub_check_free(node
->backendpath
);
1079 g_persistence_head
= NULL
;
1082 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1084 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1085 if (iso
&& iso
[0] == '/')
1087 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
1089 node
= grub_zalloc(sizeof(persistence_config
));
1092 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
1093 node
->backendpath
= backendpath
;
1094 node
->backendnum
= pathnum
;
1097 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1099 if (autosel
>= 0 && autosel
<= pathnum
)
1101 node
->autosel
= autosel
;
1105 if (g_persistence_head
)
1107 node
->next
= g_persistence_head
;
1110 g_persistence_head
= node
;
1119 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
1122 const char *path
= NULL
;
1123 const char *alias
= NULL
;
1124 VTOY_JSON
*pNode
= NULL
;
1128 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1130 grub_printf("Not array %d\n", json
->enDataType
);
1134 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1136 type
= vtoy_alias_image_file
;
1137 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1140 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1141 type
= vtoy_alias_directory
;
1144 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1145 if (path
&& path
[0] == '/' && alias
)
1147 if (vtoy_alias_image_file
== type
)
1149 if (grub_strchr(path
, '*'))
1151 grub_printf("image: <%s> [ * ]\n", path
);
1153 else if (ventoy_is_file_exist("%s%s", isodisk
, path
))
1155 grub_printf("image: <%s> [ OK ]\n", path
);
1159 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
1164 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
1166 grub_printf("dir: <%s> [ OK ]\n", path
);
1170 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
1174 grub_printf("alias: <%s>\n\n", alias
);
1181 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
1184 const char *path
= NULL
;
1185 const char *alias
= NULL
;
1186 VTOY_JSON
*pNode
= NULL
;
1187 menu_alias
*node
= NULL
;
1188 menu_alias
*next
= NULL
;
1192 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1194 debug("Not array %d\n", json
->enDataType
);
1198 if (g_menu_alias_head
)
1200 for (node
= g_menu_alias_head
; node
; node
= next
)
1206 g_menu_alias_head
= NULL
;
1209 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1211 type
= vtoy_alias_image_file
;
1212 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1215 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1216 type
= vtoy_alias_directory
;
1219 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1220 if (path
&& path
[0] == '/' && alias
)
1222 node
= grub_zalloc(sizeof(menu_alias
));
1226 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1227 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1229 if (g_menu_alias_head
)
1231 node
->next
= g_menu_alias_head
;
1234 g_menu_alias_head
= node
;
1243 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
1245 const char *path
= NULL
;
1246 const char *archive
= NULL
;
1247 VTOY_JSON
*pNode
= NULL
;
1251 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1253 grub_printf("Not array %d\n", json
->enDataType
);
1257 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1259 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1262 grub_printf("image not found\n");
1266 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1269 grub_printf("archive not found\n");
1273 if (grub_strchr(path
, '*'))
1275 grub_printf("image: <%s> [*]\n", path
);
1279 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
1282 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
1288 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
1290 const char *path
= NULL
;
1291 const char *archive
= NULL
;
1292 VTOY_JSON
*pNode
= NULL
;
1293 injection_config
*node
= NULL
;
1294 injection_config
*next
= NULL
;
1298 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1300 debug("Not array %d\n", json
->enDataType
);
1304 if (g_injection_head
)
1306 for (node
= g_injection_head
; node
; node
= next
)
1312 g_injection_head
= NULL
;
1315 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1317 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1318 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1319 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
1321 node
= grub_zalloc(sizeof(injection_config
));
1324 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1325 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
1327 if (g_injection_head
)
1329 node
->next
= g_injection_head
;
1332 g_injection_head
= node
;
1340 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
1344 const char *key
= NULL
;
1345 const char *class = NULL
;
1346 VTOY_JSON
*pNode
= NULL
;
1347 menu_class
*tail
= NULL
;
1348 menu_class
*node
= NULL
;
1349 menu_class
*next
= NULL
;
1353 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1355 debug("Not array %d\n", json
->enDataType
);
1359 if (g_menu_class_head
)
1361 for (node
= g_menu_class_head
; node
; node
= next
)
1367 g_menu_class_head
= NULL
;
1370 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1373 type
= vtoy_class_image_file
;
1374 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1377 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1384 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1385 type
= vtoy_class_directory
;
1389 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1392 node
= grub_zalloc(sizeof(menu_class
));
1396 node
->parent
= parent
;
1397 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
1398 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
1400 if (g_menu_class_head
)
1406 g_menu_class_head
= node
;
1416 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
1418 const char *name
= NULL
;
1419 const char *key
= NULL
;
1420 const char *class = NULL
;
1421 VTOY_JSON
*pNode
= NULL
;
1425 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1427 grub_printf("Not array %d\n", json
->enDataType
);
1431 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1434 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1438 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1442 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1446 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1449 grub_printf("%s: <%s>\n", name
, key
);
1450 grub_printf("class: <%s>\n\n", class);
1457 static int ventoy_plugin_custom_boot_entry(VTOY_JSON
*json
, const char *isodisk
)
1461 const char *key
= NULL
;
1462 const char *cfg
= NULL
;
1463 VTOY_JSON
*pNode
= NULL
;
1464 custom_boot
*tail
= NULL
;
1465 custom_boot
*node
= NULL
;
1466 custom_boot
*next
= NULL
;
1470 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1472 debug("Not array %d\n", json
->enDataType
);
1476 if (g_custom_boot_head
)
1478 for (node
= g_custom_boot_head
; node
; node
= next
)
1484 g_custom_boot_head
= NULL
;
1487 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1489 type
= vtoy_custom_boot_image_file
;
1490 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "file");
1493 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1494 type
= vtoy_custom_boot_directory
;
1497 cfg
= vtoy_json_get_string_ex(pNode
->pstChild
, "vcfg");
1500 node
= grub_zalloc(sizeof(custom_boot
));
1504 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", key
);
1505 len
= (int)grub_snprintf(node
->cfg
, sizeof(node
->cfg
), "%s", cfg
);
1507 if (len
>= 5 && grub_strncmp(node
->cfg
+ len
- 5, ".vcfg", 5) == 0)
1509 if (g_custom_boot_head
)
1515 g_custom_boot_head
= node
;
1530 static int ventoy_plugin_custom_boot_check(VTOY_JSON
*json
, const char *isodisk
)
1534 const char *key
= NULL
;
1535 const char *cfg
= NULL
;
1536 VTOY_JSON
*pNode
= NULL
;
1540 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1542 grub_printf("Not array %d\n", json
->enDataType
);
1546 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1548 type
= vtoy_custom_boot_image_file
;
1549 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "file");
1552 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1553 type
= vtoy_custom_boot_directory
;
1556 cfg
= vtoy_json_get_string_ex(pNode
->pstChild
, "vcfg");
1557 len
= (int)grub_strlen(cfg
);
1560 if (len
< 5 || grub_strncmp(cfg
+ len
- 5, ".vcfg", 5))
1562 grub_printf("<%s> does not have \".vcfg\" suffix\n\n", cfg
);
1566 grub_printf("%s: <%s>\n", (type
== vtoy_custom_boot_directory
) ? "dir" : "file", key
);
1567 grub_printf("vcfg: <%s>\n\n", cfg
);
1575 static int ventoy_plugin_conf_replace_entry(VTOY_JSON
*json
, const char *isodisk
)
1577 const char *isof
= NULL
;
1578 const char *orgf
= NULL
;
1579 const char *newf
= NULL
;
1580 VTOY_JSON
*pNode
= NULL
;
1581 conf_replace
*tail
= NULL
;
1582 conf_replace
*node
= NULL
;
1583 conf_replace
*next
= NULL
;
1587 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1589 debug("Not array %d\n", json
->enDataType
);
1593 if (g_conf_replace_head
)
1595 for (node
= g_conf_replace_head
; node
; node
= next
)
1601 g_conf_replace_head
= NULL
;
1604 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1606 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1607 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1608 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1609 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1611 node
= grub_zalloc(sizeof(conf_replace
));
1614 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", isof
);
1615 grub_snprintf(node
->orgconf
, sizeof(node
->orgconf
), "%s", orgf
);
1616 grub_snprintf(node
->newconf
, sizeof(node
->newconf
), "%s", newf
);
1618 if (g_conf_replace_head
)
1624 g_conf_replace_head
= node
;
1634 static int ventoy_plugin_conf_replace_check(VTOY_JSON
*json
, const char *isodisk
)
1636 const char *isof
= NULL
;
1637 const char *orgf
= NULL
;
1638 const char *newf
= NULL
;
1639 VTOY_JSON
*pNode
= NULL
;
1640 grub_file_t file
= NULL
;
1645 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1647 grub_printf("Not array %d\n", json
->enDataType
);
1651 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1653 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1654 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1655 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1656 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1658 if (ventoy_check_file_exist("%s%s", isodisk
, isof
))
1660 grub_printf("iso:<%s> [OK]\n", isof
);
1662 grub_snprintf(cmd
, sizeof(cmd
), "loopback vtisocheck \"%s%s\"", isodisk
, isof
);
1663 grub_script_execute_sourcecode(cmd
);
1665 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "(vtisocheck)/%s", orgf
);
1668 if (grub_strcmp(file
->fs
->name
, "iso9660") == 0)
1670 grub_printf("org:<%s> [OK]\n", orgf
);
1674 grub_printf("org:<%s> [Exist But NOT ISO9660]\n", orgf
);
1676 grub_file_close(file
);
1680 grub_printf("org:<%s> [NOT Exist]\n", orgf
);
1683 grub_script_execute_sourcecode("loopback -d vtisocheck");
1685 else if (grub_strchr(isof
, '*'))
1687 grub_printf("iso:<%s> [*]\n", isof
);
1688 grub_printf("org:<%s>\n", orgf
);
1692 grub_printf("iso:<%s> [NOT Exist]\n", isof
);
1693 grub_printf("org:<%s>\n", orgf
);
1696 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isodisk
, newf
);
1699 if (file
->size
> vtoy_max_replace_file_size
)
1701 grub_printf("new:<%s> [Too Big %lu] \n", newf
, (ulong
)file
->size
);
1705 grub_printf("new:<%s> [OK]\n", newf
);
1707 grub_file_close(file
);
1711 grub_printf("new:<%s> [NOT Exist]\n", newf
);
1720 static int ventoy_plugin_auto_memdisk_entry(VTOY_JSON
*json
, const char *isodisk
)
1722 VTOY_JSON
*pNode
= NULL
;
1723 auto_memdisk
*node
= NULL
;
1724 auto_memdisk
*next
= NULL
;
1728 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1730 debug("Not array %d\n", json
->enDataType
);
1734 if (g_auto_memdisk_head
)
1736 for (node
= g_auto_memdisk_head
; node
; node
= next
)
1742 g_auto_memdisk_head
= NULL
;
1745 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1747 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1749 node
= grub_zalloc(sizeof(auto_memdisk
));
1752 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1754 if (g_auto_memdisk_head
)
1756 node
->next
= g_auto_memdisk_head
;
1759 g_auto_memdisk_head
= node
;
1767 static int ventoy_plugin_auto_memdisk_check(VTOY_JSON
*json
, const char *isodisk
)
1769 VTOY_JSON
*pNode
= NULL
;
1771 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1773 grub_printf("Not array %d\n", json
->enDataType
);
1777 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1779 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1781 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1783 if (grub_strchr(pNode
->unData
.pcStrVal
, '*'))
1785 grub_printf(" [*]\n");
1787 else if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1789 grub_printf(" [OK]\n");
1793 grub_printf(" [NOT EXIST]\n");
1801 static int ventoy_plugin_image_list_entry(VTOY_JSON
*json
, const char *isodisk
)
1803 VTOY_JSON
*pNode
= NULL
;
1804 image_list
*node
= NULL
;
1805 image_list
*next
= NULL
;
1806 image_list
*tail
= NULL
;
1810 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1812 debug("Not array %d\n", json
->enDataType
);
1816 if (g_image_list_head
)
1818 for (node
= g_image_list_head
; node
; node
= next
)
1824 g_image_list_head
= NULL
;
1827 if (grub_strncmp(json
->pcName
, "image_blacklist", 15) == 0)
1829 g_plugin_image_list
= VENTOY_IMG_BLACK_LIST
;
1833 g_plugin_image_list
= VENTOY_IMG_WHITE_LIST
;
1836 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1838 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1840 node
= grub_zalloc(sizeof(image_list
));
1843 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1845 if (g_image_list_head
)
1851 g_image_list_head
= node
;
1861 static int ventoy_plugin_image_list_check(VTOY_JSON
*json
, const char *isodisk
)
1863 VTOY_JSON
*pNode
= NULL
;
1865 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1867 grub_printf("Not array %d\n", json
->enDataType
);
1871 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1873 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1875 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1877 if (grub_strchr(pNode
->unData
.pcStrVal
, '*'))
1879 grub_printf(" [*]\n");
1881 else if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1883 grub_printf(" [OK]\n");
1887 grub_printf(" [NOT EXIST]\n");
1895 static plugin_entry g_plugin_entries
[] =
1897 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
1898 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
1899 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
1900 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
1901 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
1902 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
1903 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
},
1904 { "auto_memdisk", ventoy_plugin_auto_memdisk_entry
, ventoy_plugin_auto_memdisk_check
},
1905 { "image_list", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1906 { "image_blacklist", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1907 { "conf_replace", ventoy_plugin_conf_replace_entry
, ventoy_plugin_conf_replace_check
},
1908 { "dud", ventoy_plugin_dud_entry
, ventoy_plugin_dud_check
},
1909 { "password", ventoy_plugin_pwd_entry
, ventoy_plugin_pwd_check
},
1910 { "custom_boot", ventoy_plugin_custom_boot_entry
, ventoy_plugin_custom_boot_check
},
1913 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
1917 VTOY_JSON
*cur
= json
;
1919 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
1923 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1925 grub_snprintf(key
, sizeof(key
), "%s_%s", g_plugin_entries
[i
].key
, g_arch_mode_suffix
);
1926 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0 || grub_strcmp(key
, cur
->pcName
) == 0)
1928 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
1929 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
1940 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1945 VTOY_JSON
*json
= NULL
;
1950 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
1953 return GRUB_ERR_NONE
;
1956 debug("json configuration file size %d\n", (int)file
->size
);
1958 buf
= grub_malloc(file
->size
+ 1);
1961 grub_file_close(file
);
1965 buf
[file
->size
] = 0;
1966 grub_file_read(file
, buf
, file
->size
);
1967 grub_file_close(file
);
1969 json
= vtoy_json_create();
1977 ret
= vtoy_json_parse(json
, buf
);
1980 grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
1981 grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR");
1983 debug("Failed to parse json string %d\n", ret
);
1988 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
1990 vtoy_json_destroy(json
);
1994 if (g_boot_pwd
.type
)
1996 grub_printf("\n\n======= %s ======\n\n", grub_env_get("VTOY_TEXT_MENU_VER"));
1997 if (ventoy_check_password(&g_boot_pwd
, 3))
1999 grub_printf("\n!!! Password check failed, will exit after 5 seconds. !!!\n");
2006 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2009 void ventoy_plugin_dump_injection(void)
2011 injection_config
*node
= NULL
;
2013 for (node
= g_injection_head
; node
; node
= node
->next
)
2015 grub_printf("\nIMAGE:<%s>\n", node
->isopath
);
2016 grub_printf("ARCHIVE:<%s>\n", node
->archive
);
2023 void ventoy_plugin_dump_auto_install(void)
2026 install_template
*node
= NULL
;
2028 for (node
= g_install_template_head
; node
; node
= node
->next
)
2030 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
2031 for (i
= 0; i
< node
->templatenum
; i
++)
2033 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
2040 void ventoy_plugin_dump_persistence(void)
2044 persistence_config
*node
= NULL
;
2045 ventoy_img_chunk_list chunk_list
;
2047 for (node
= g_persistence_head
; node
; node
= node
->next
)
2049 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
2051 for (i
= 0; i
< node
->backendnum
; i
++)
2053 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
2054 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
2057 grub_printf(" [ SUCCESS ]\n");
2058 grub_free(chunk_list
.chunk
);
2062 grub_printf(" [ FAILED ]\n");
2070 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
2073 install_template
*node
= NULL
;
2075 if (!g_install_template_head
)
2080 len
= (int)grub_strlen(isopath
);
2081 for (node
= g_install_template_head
; node
; node
= node
->next
)
2083 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2092 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
2094 install_template
*node
= NULL
;
2096 node
= ventoy_plugin_find_install_template(isopath
);
2097 if ((!node
) || (!node
->templatepath
))
2102 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
2107 return node
->templatepath
[node
->cursel
].path
;
2110 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
2113 persistence_config
*node
= NULL
;
2115 if (!g_persistence_head
)
2120 len
= (int)grub_strlen(isopath
);
2121 for (node
= g_persistence_head
; node
; node
= node
->next
)
2123 if ((len
== node
->pathlen
) && (ventoy_strcmp(node
->isopath
, isopath
) == 0))
2132 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
2135 grub_uint64_t start
= 0;
2136 grub_file_t file
= NULL
;
2137 persistence_config
*node
= NULL
;
2139 node
= ventoy_plugin_find_persistent(isopath
);
2140 if ((!node
) || (!node
->backendpath
))
2147 index
= node
->cursel
;
2150 if (index
< 0 || index
>= node
->backendnum
)
2155 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
2158 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
2162 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
2163 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
2164 if (NULL
== chunk_list
->chunk
)
2169 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
2170 chunk_list
->cur_chunk
= 0;
2172 start
= file
->device
->disk
->partition
->start
;
2173 ventoy_get_block_list(file
, chunk_list
, start
);
2175 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
2177 grub_free(chunk_list
->chunk
);
2178 chunk_list
->chunk
= NULL
;
2186 grub_file_close(file
);
2191 const char * ventoy_plugin_get_injection(const char *isopath
)
2194 injection_config
*node
= NULL
;
2196 if (!g_injection_head
)
2201 len
= (int)grub_strlen(isopath
);
2202 for (node
= g_injection_head
; node
; node
= node
->next
)
2204 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2206 return node
->archive
;
2213 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
2216 menu_alias
*node
= NULL
;
2218 if (!g_menu_alias_head
)
2223 len
= (int)grub_strlen(isopath
);
2224 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
2226 if (node
->type
== type
&& node
->pathlen
&&
2227 node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2236 const char * ventoy_plugin_get_menu_class(int type
, const char *name
, const char *path
)
2239 menu_class
*node
= NULL
;
2241 if (!g_menu_class_head
)
2246 if (vtoy_class_image_file
== type
)
2248 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2250 if (node
->type
!= type
)
2257 len
= (int)grub_strlen(path
);
2258 if ((node
->patlen
< len
) && (path
[node
->patlen
] == '/') &&
2259 (grub_strncmp(path
, node
->pattern
, node
->patlen
) == 0))
2266 len
= (int)grub_strlen(name
);
2267 if ((node
->patlen
< len
) && grub_strstr(name
, node
->pattern
))
2276 len
= (int)grub_strlen(name
);
2277 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2279 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
2289 int ventoy_plugin_add_custom_boot(const char *vcfgpath
)
2292 custom_boot
*node
= NULL
;
2294 node
= grub_zalloc(sizeof(custom_boot
));
2297 node
->type
= vtoy_custom_boot_image_file
;
2298 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", vcfgpath
);
2299 grub_snprintf(node
->cfg
, sizeof(node
->cfg
), "%s", vcfgpath
);
2302 len
= node
->pathlen
- 5;
2303 node
->path
[len
] = 0;
2304 node
->pathlen
= len
;
2306 if (g_custom_boot_head
)
2308 node
->next
= g_custom_boot_head
;
2310 g_custom_boot_head
= node
;
2316 const char * ventoy_plugin_get_custom_boot(const char *isopath
)
2320 custom_boot
*node
= NULL
;
2322 if (!g_custom_boot_head
)
2327 len
= (int)grub_strlen(isopath
);
2329 for (node
= g_custom_boot_head
; node
; node
= node
->next
)
2331 if (node
->type
== vtoy_custom_boot_image_file
)
2333 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->path
, len
) == 0)
2340 if (node
->pathlen
< len
&& isopath
[node
->pathlen
] == '/' &&
2341 grub_strncmp(isopath
, node
->path
, node
->pathlen
) == 0)
2343 for (i
= node
->pathlen
+ 1; i
< len
; i
++)
2345 if (isopath
[i
] == '/')
2362 grub_err_t
ventoy_cmd_dump_custom_boot(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2364 custom_boot
*node
= NULL
;
2370 for (node
= g_custom_boot_head
; node
; node
= node
->next
)
2372 grub_printf("[%s] <%s>:<%s>\n", (node
->type
== vtoy_custom_boot_directory
) ? "dir" : "file",
2373 node
->path
, node
->cfg
);
2379 int ventoy_plugin_check_memdisk(const char *isopath
)
2382 auto_memdisk
*node
= NULL
;
2384 if (!g_auto_memdisk_head
)
2389 len
= (int)grub_strlen(isopath
);
2390 for (node
= g_auto_memdisk_head
; node
; node
= node
->next
)
2392 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, isopath
, len
) == 0)
2401 int ventoy_plugin_get_image_list_index(int type
, const char *name
)
2405 image_list
*node
= NULL
;
2407 if (!g_image_list_head
)
2412 len
= (int)grub_strlen(name
);
2414 for (node
= g_image_list_head
; node
; node
= node
->next
, index
++)
2416 if (vtoy_class_directory
== type
)
2418 if (len
< node
->pathlen
&& ventoy_strncmp(node
->isopath
, name
, len
) == 0)
2425 if (len
== node
->pathlen
&& ventoy_strncmp(node
->isopath
, name
, len
) == 0)
2435 conf_replace
* ventoy_plugin_find_conf_replace(const char *iso
)
2440 if (!g_conf_replace_head
)
2445 len
= (int)grub_strlen(iso
);
2447 for (node
= g_conf_replace_head
; node
; node
= node
->next
)
2449 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, iso
, len
) == 0)
2458 dud
* ventoy_plugin_find_dud(const char *iso
)
2468 len
= (int)grub_strlen(iso
);
2469 for (node
= g_dud_head
; node
; node
= node
->next
)
2471 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, iso
, len
) == 0)
2480 int ventoy_plugin_load_dud(dud
*node
, const char *isopart
)
2486 for (i
= 0; i
< node
->dudnum
; i
++)
2488 if (node
->files
[i
].size
> 0)
2490 debug("file %d has been loaded\n", i
);
2494 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isopart
, node
->dudpath
[i
].path
);
2497 buf
= grub_malloc(file
->size
);
2500 grub_file_read(file
, buf
, file
->size
);
2501 node
->files
[i
].size
= (int)file
->size
;
2502 node
->files
[i
].buf
= buf
;
2504 grub_file_close(file
);
2511 static const vtoy_password
* ventoy_plugin_get_password(const char *isopath
)
2515 const char *pos
= NULL
;
2516 menu_password
*node
= NULL
;
2525 len
= (int)grub_strlen(isopath
);
2526 for (node
= g_pwd_head
; node
; node
= node
->next
)
2528 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, isopath
, len
) == 0)
2530 return &(node
->password
);
2537 if (*isopath
== '.')
2546 for (i
= 0; i
< (int)ARRAY_SIZE(g_menu_prefix
); i
++)
2548 if (g_file_type_pwd
[i
].type
&& 0 == grub_strcasecmp(pos
+ 1, g_menu_prefix
[i
]))
2550 return g_file_type_pwd
+ i
;
2558 grub_err_t
ventoy_cmd_check_password(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2561 const vtoy_password
*pwd
= NULL
;
2566 pwd
= ventoy_plugin_get_password(args
[0]);
2569 if (0 == ventoy_check_password(pwd
, 1))
2587 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2594 VTOY_JSON
*node
= NULL
;
2595 VTOY_JSON
*json
= NULL
;
2604 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
2607 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
2608 grub_printf("Attention: directory name and filename are both case-sensitive.\n");
2612 buf
= grub_malloc(file
->size
+ 1);
2615 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
2619 buf
[file
->size
] = 0;
2620 grub_file_read(file
, buf
, file
->size
);
2622 json
= vtoy_json_create();
2625 grub_printf("Failed to create json\n");
2629 ret
= vtoy_json_parse(json
, buf
);
2632 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
2636 grub_snprintf(key
, sizeof(key
), "%s_%s", args
[1], g_arch_mode_suffix
);
2637 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
2639 if (grub_strcmp(node
->pcName
, args
[1]) == 0 || grub_strcmp(node
->pcName
, key
) == 0)
2647 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
2651 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
2653 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
2655 if (g_plugin_entries
[i
].checkfunc
)
2657 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
2664 check_free(file
, grub_file_close
);
2665 check_free(json
, vtoy_json_destroy
);
2666 grub_check_free(buf
);