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 type
= vtoy_menu_pwd_file
;
880 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
883 type
= vtoy_menu_pwd_parent
;
884 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "parent");
887 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
888 if (iso
&& pwd
&& iso
[0] == '/')
890 node
= grub_zalloc(sizeof(menu_password
));
894 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
896 if (ventoy_plugin_parse_pwdstr((char *)pwd
, &(node
->password
)))
920 static int ventoy_plugin_pwd_check(VTOY_JSON
*json
, const char *isodisk
)
924 const char *iso
= NULL
;
925 const char *pwd
= NULL
;
926 VTOY_JSON
*pNode
= NULL
;
927 VTOY_JSON
*pCNode
= NULL
;
929 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
931 grub_printf("Not object %d\n", json
->enDataType
);
935 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
937 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
939 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
941 grub_printf("bootpwd:<%s>\n", pNode
->unData
.pcStrVal
);
945 grub_printf("Invalid bootpwd.\n");
948 else if ((type
= ventoy_plugin_get_pwd_type(pNode
->pcName
)) >= 0)
950 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
952 grub_printf("%s:<%s>\n", pNode
->pcName
, pNode
->unData
.pcStrVal
);
956 grub_printf("Invalid pwd <%s>\n", pNode
->unData
.pcStrVal
);
959 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
962 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
964 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
966 grub_printf("Not object %d\n", pCNode
->enDataType
);
970 if ((iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file")) != NULL
)
972 pos
= grub_strchr(iso
, '*');
973 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
975 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
977 if (0 == ventoy_plugin_parse_pwdstr((char *)pwd
, NULL
))
979 grub_printf("file:<%s> [%s]\n", iso
, (pos
? "*" : "OK"));
980 grub_printf("pwd:<%s>\n\n", pwd
);
984 grub_printf("Invalid password for <%s>\n", iso
);
989 grub_printf("<%s%s> not found\n", isodisk
, iso
);
992 else if ((iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "parent")) != NULL
)
994 if (ventoy_is_dir_exist("%s%s", isodisk
, iso
))
996 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
997 if (0 == ventoy_plugin_parse_pwdstr((char *)pwd
, NULL
))
999 grub_printf("dir:<%s> [%s]\n", iso
, (pos
? "*" : "OK"));
1000 grub_printf("pwd:<%s>\n\n", pwd
);
1004 grub_printf("Invalid password for <%s>\n", iso
);
1009 grub_printf("<%s%s> not found\n", isodisk
, iso
);
1014 grub_printf("No file item found in json.\n");
1023 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
1028 const char *iso
= NULL
;
1029 VTOY_JSON
*pNode
= NULL
;
1031 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1033 grub_printf("Not array type %d\n", json
->enDataType
);
1037 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1039 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
1041 grub_printf("NOT object type\n");
1044 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1047 pos
= grub_strchr(iso
, '*');
1048 if (pos
|| 0 == ventoy_plugin_check_path(isodisk
, iso
))
1050 grub_printf("image: %s [%s]\n", iso
, (pos
? "*" : "OK"));
1051 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
1053 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1055 if (autosel
>= 0 && autosel
<= pathnum
)
1057 grub_printf("autosel: %d [OK]\n", autosel
);
1061 grub_printf("autosel: %d [FAIL]\n", autosel
);
1067 grub_printf("image: %s [FAIL]\n", iso
);
1072 grub_printf("image not found\n");
1079 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
1083 const char *iso
= NULL
;
1084 VTOY_JSON
*pNode
= NULL
;
1085 persistence_config
*node
= NULL
;
1086 persistence_config
*next
= NULL
;
1087 file_fullpath
*backendpath
= NULL
;
1091 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1093 debug("Not array %d\n", json
->enDataType
);
1097 if (g_persistence_head
)
1099 for (node
= g_persistence_head
; node
; node
= next
)
1102 grub_check_free(node
->backendpath
);
1106 g_persistence_head
= NULL
;
1109 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1111 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1112 if (iso
&& iso
[0] == '/')
1114 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
1116 node
= grub_zalloc(sizeof(persistence_config
));
1119 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
1120 node
->backendpath
= backendpath
;
1121 node
->backendnum
= pathnum
;
1124 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1126 if (autosel
>= 0 && autosel
<= pathnum
)
1128 node
->autosel
= autosel
;
1132 if (g_persistence_head
)
1134 node
->next
= g_persistence_head
;
1137 g_persistence_head
= node
;
1146 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
1149 const char *path
= NULL
;
1150 const char *alias
= NULL
;
1151 VTOY_JSON
*pNode
= NULL
;
1155 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1157 grub_printf("Not array %d\n", json
->enDataType
);
1161 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1163 type
= vtoy_alias_image_file
;
1164 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1167 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1168 type
= vtoy_alias_directory
;
1171 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1172 if (path
&& path
[0] == '/' && alias
)
1174 if (vtoy_alias_image_file
== type
)
1176 if (grub_strchr(path
, '*'))
1178 grub_printf("image: <%s> [ * ]\n", path
);
1180 else if (ventoy_is_file_exist("%s%s", isodisk
, path
))
1182 grub_printf("image: <%s> [ OK ]\n", path
);
1186 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
1191 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
1193 grub_printf("dir: <%s> [ OK ]\n", path
);
1197 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
1201 grub_printf("alias: <%s>\n\n", alias
);
1208 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
1211 const char *path
= NULL
;
1212 const char *alias
= NULL
;
1213 VTOY_JSON
*pNode
= NULL
;
1214 menu_alias
*node
= NULL
;
1215 menu_alias
*next
= NULL
;
1219 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1221 debug("Not array %d\n", json
->enDataType
);
1225 if (g_menu_alias_head
)
1227 for (node
= g_menu_alias_head
; node
; node
= next
)
1233 g_menu_alias_head
= NULL
;
1236 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1238 type
= vtoy_alias_image_file
;
1239 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1242 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1243 type
= vtoy_alias_directory
;
1246 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1247 if (path
&& path
[0] == '/' && alias
)
1249 node
= grub_zalloc(sizeof(menu_alias
));
1253 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1254 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1256 if (g_menu_alias_head
)
1258 node
->next
= g_menu_alias_head
;
1261 g_menu_alias_head
= node
;
1270 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
1272 const char *path
= NULL
;
1273 const char *archive
= NULL
;
1274 VTOY_JSON
*pNode
= NULL
;
1278 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1280 grub_printf("Not array %d\n", json
->enDataType
);
1284 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1286 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1289 grub_printf("image not found\n");
1293 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1296 grub_printf("archive not found\n");
1300 if (grub_strchr(path
, '*'))
1302 grub_printf("image: <%s> [*]\n", path
);
1306 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
1309 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
1315 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
1317 const char *path
= NULL
;
1318 const char *archive
= NULL
;
1319 VTOY_JSON
*pNode
= NULL
;
1320 injection_config
*node
= NULL
;
1321 injection_config
*next
= NULL
;
1325 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1327 debug("Not array %d\n", json
->enDataType
);
1331 if (g_injection_head
)
1333 for (node
= g_injection_head
; node
; node
= next
)
1339 g_injection_head
= NULL
;
1342 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1344 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1345 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1346 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
1348 node
= grub_zalloc(sizeof(injection_config
));
1351 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1352 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
1354 if (g_injection_head
)
1356 node
->next
= g_injection_head
;
1359 g_injection_head
= node
;
1367 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
1371 const char *key
= NULL
;
1372 const char *class = NULL
;
1373 VTOY_JSON
*pNode
= NULL
;
1374 menu_class
*tail
= NULL
;
1375 menu_class
*node
= NULL
;
1376 menu_class
*next
= NULL
;
1380 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1382 debug("Not array %d\n", json
->enDataType
);
1386 if (g_menu_class_head
)
1388 for (node
= g_menu_class_head
; node
; node
= next
)
1394 g_menu_class_head
= NULL
;
1397 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1400 type
= vtoy_class_image_file
;
1401 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1404 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1411 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1412 type
= vtoy_class_directory
;
1416 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1419 node
= grub_zalloc(sizeof(menu_class
));
1423 node
->parent
= parent
;
1424 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
1425 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
1427 if (g_menu_class_head
)
1433 g_menu_class_head
= node
;
1443 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
1445 const char *name
= NULL
;
1446 const char *key
= NULL
;
1447 const char *class = NULL
;
1448 VTOY_JSON
*pNode
= NULL
;
1452 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1454 grub_printf("Not array %d\n", json
->enDataType
);
1458 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1461 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1465 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "parent");
1469 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1473 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1476 grub_printf("%s: <%s>\n", name
, key
);
1477 grub_printf("class: <%s>\n\n", class);
1484 static int ventoy_plugin_custom_boot_entry(VTOY_JSON
*json
, const char *isodisk
)
1488 const char *key
= NULL
;
1489 const char *cfg
= NULL
;
1490 VTOY_JSON
*pNode
= NULL
;
1491 custom_boot
*tail
= NULL
;
1492 custom_boot
*node
= NULL
;
1493 custom_boot
*next
= NULL
;
1497 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1499 debug("Not array %d\n", json
->enDataType
);
1503 if (g_custom_boot_head
)
1505 for (node
= g_custom_boot_head
; node
; node
= next
)
1511 g_custom_boot_head
= NULL
;
1514 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1516 type
= vtoy_custom_boot_image_file
;
1517 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "file");
1520 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1521 type
= vtoy_custom_boot_directory
;
1524 cfg
= vtoy_json_get_string_ex(pNode
->pstChild
, "vcfg");
1527 node
= grub_zalloc(sizeof(custom_boot
));
1531 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", key
);
1532 len
= (int)grub_snprintf(node
->cfg
, sizeof(node
->cfg
), "%s", cfg
);
1534 if (len
>= 5 && grub_strncmp(node
->cfg
+ len
- 5, ".vcfg", 5) == 0)
1536 if (g_custom_boot_head
)
1542 g_custom_boot_head
= node
;
1557 static int ventoy_plugin_custom_boot_check(VTOY_JSON
*json
, const char *isodisk
)
1561 const char *key
= NULL
;
1562 const char *cfg
= NULL
;
1563 VTOY_JSON
*pNode
= NULL
;
1567 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1569 grub_printf("Not array %d\n", json
->enDataType
);
1573 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1575 type
= vtoy_custom_boot_image_file
;
1576 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "file");
1579 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1580 type
= vtoy_custom_boot_directory
;
1583 cfg
= vtoy_json_get_string_ex(pNode
->pstChild
, "vcfg");
1584 len
= (int)grub_strlen(cfg
);
1587 if (len
< 5 || grub_strncmp(cfg
+ len
- 5, ".vcfg", 5))
1589 grub_printf("<%s> does not have \".vcfg\" suffix\n\n", cfg
);
1593 grub_printf("%s: <%s>\n", (type
== vtoy_custom_boot_directory
) ? "dir" : "file", key
);
1594 grub_printf("vcfg: <%s>\n\n", cfg
);
1602 static int ventoy_plugin_conf_replace_entry(VTOY_JSON
*json
, const char *isodisk
)
1604 const char *isof
= NULL
;
1605 const char *orgf
= NULL
;
1606 const char *newf
= NULL
;
1607 VTOY_JSON
*pNode
= NULL
;
1608 conf_replace
*tail
= NULL
;
1609 conf_replace
*node
= NULL
;
1610 conf_replace
*next
= NULL
;
1614 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1616 debug("Not array %d\n", json
->enDataType
);
1620 if (g_conf_replace_head
)
1622 for (node
= g_conf_replace_head
; node
; node
= next
)
1628 g_conf_replace_head
= NULL
;
1631 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1633 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1634 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1635 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1636 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1638 node
= grub_zalloc(sizeof(conf_replace
));
1641 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", isof
);
1642 grub_snprintf(node
->orgconf
, sizeof(node
->orgconf
), "%s", orgf
);
1643 grub_snprintf(node
->newconf
, sizeof(node
->newconf
), "%s", newf
);
1645 if (g_conf_replace_head
)
1651 g_conf_replace_head
= node
;
1661 static int ventoy_plugin_conf_replace_check(VTOY_JSON
*json
, const char *isodisk
)
1663 const char *isof
= NULL
;
1664 const char *orgf
= NULL
;
1665 const char *newf
= NULL
;
1666 VTOY_JSON
*pNode
= NULL
;
1667 grub_file_t file
= NULL
;
1672 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1674 grub_printf("Not array %d\n", json
->enDataType
);
1678 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1680 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1681 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1682 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1683 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1685 if (ventoy_check_file_exist("%s%s", isodisk
, isof
))
1687 grub_printf("iso:<%s> [OK]\n", isof
);
1689 grub_snprintf(cmd
, sizeof(cmd
), "loopback vtisocheck \"%s%s\"", isodisk
, isof
);
1690 grub_script_execute_sourcecode(cmd
);
1692 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "(vtisocheck)/%s", orgf
);
1695 if (grub_strcmp(file
->fs
->name
, "iso9660") == 0)
1697 grub_printf("org:<%s> [OK]\n", orgf
);
1701 grub_printf("org:<%s> [Exist But NOT ISO9660]\n", orgf
);
1703 grub_file_close(file
);
1707 grub_printf("org:<%s> [NOT Exist]\n", orgf
);
1710 grub_script_execute_sourcecode("loopback -d vtisocheck");
1712 else if (grub_strchr(isof
, '*'))
1714 grub_printf("iso:<%s> [*]\n", isof
);
1715 grub_printf("org:<%s>\n", orgf
);
1719 grub_printf("iso:<%s> [NOT Exist]\n", isof
);
1720 grub_printf("org:<%s>\n", orgf
);
1723 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isodisk
, newf
);
1726 if (file
->size
> vtoy_max_replace_file_size
)
1728 grub_printf("new:<%s> [Too Big %lu] \n", newf
, (ulong
)file
->size
);
1732 grub_printf("new:<%s> [OK]\n", newf
);
1734 grub_file_close(file
);
1738 grub_printf("new:<%s> [NOT Exist]\n", newf
);
1747 static int ventoy_plugin_auto_memdisk_entry(VTOY_JSON
*json
, const char *isodisk
)
1749 VTOY_JSON
*pNode
= NULL
;
1750 auto_memdisk
*node
= NULL
;
1751 auto_memdisk
*next
= NULL
;
1755 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1757 debug("Not array %d\n", json
->enDataType
);
1761 if (g_auto_memdisk_head
)
1763 for (node
= g_auto_memdisk_head
; node
; node
= next
)
1769 g_auto_memdisk_head
= NULL
;
1772 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1774 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1776 node
= grub_zalloc(sizeof(auto_memdisk
));
1779 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1781 if (g_auto_memdisk_head
)
1783 node
->next
= g_auto_memdisk_head
;
1786 g_auto_memdisk_head
= node
;
1794 static int ventoy_plugin_auto_memdisk_check(VTOY_JSON
*json
, const char *isodisk
)
1796 VTOY_JSON
*pNode
= NULL
;
1798 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1800 grub_printf("Not array %d\n", json
->enDataType
);
1804 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1806 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1808 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1810 if (grub_strchr(pNode
->unData
.pcStrVal
, '*'))
1812 grub_printf(" [*]\n");
1814 else if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1816 grub_printf(" [OK]\n");
1820 grub_printf(" [NOT EXIST]\n");
1828 static int ventoy_plugin_image_list_entry(VTOY_JSON
*json
, const char *isodisk
)
1830 VTOY_JSON
*pNode
= NULL
;
1831 image_list
*node
= NULL
;
1832 image_list
*next
= NULL
;
1833 image_list
*tail
= NULL
;
1837 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1839 debug("Not array %d\n", json
->enDataType
);
1843 if (g_image_list_head
)
1845 for (node
= g_image_list_head
; node
; node
= next
)
1851 g_image_list_head
= NULL
;
1854 if (grub_strncmp(json
->pcName
, "image_blacklist", 15) == 0)
1856 g_plugin_image_list
= VENTOY_IMG_BLACK_LIST
;
1860 g_plugin_image_list
= VENTOY_IMG_WHITE_LIST
;
1863 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1865 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1867 node
= grub_zalloc(sizeof(image_list
));
1870 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1872 if (g_image_list_head
)
1878 g_image_list_head
= node
;
1888 static int ventoy_plugin_image_list_check(VTOY_JSON
*json
, const char *isodisk
)
1890 VTOY_JSON
*pNode
= NULL
;
1892 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1894 grub_printf("Not array %d\n", json
->enDataType
);
1898 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1900 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1902 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1904 if (grub_strchr(pNode
->unData
.pcStrVal
, '*'))
1906 grub_printf(" [*]\n");
1908 else if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1910 grub_printf(" [OK]\n");
1914 grub_printf(" [NOT EXIST]\n");
1922 static plugin_entry g_plugin_entries
[] =
1924 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
1925 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
1926 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
1927 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
1928 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
1929 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
1930 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
},
1931 { "auto_memdisk", ventoy_plugin_auto_memdisk_entry
, ventoy_plugin_auto_memdisk_check
},
1932 { "image_list", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1933 { "image_blacklist", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1934 { "conf_replace", ventoy_plugin_conf_replace_entry
, ventoy_plugin_conf_replace_check
},
1935 { "dud", ventoy_plugin_dud_entry
, ventoy_plugin_dud_check
},
1936 { "password", ventoy_plugin_pwd_entry
, ventoy_plugin_pwd_check
},
1937 { "custom_boot", ventoy_plugin_custom_boot_entry
, ventoy_plugin_custom_boot_check
},
1940 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
1944 VTOY_JSON
*cur
= json
;
1946 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
1950 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1952 grub_snprintf(key
, sizeof(key
), "%s_%s", g_plugin_entries
[i
].key
, g_arch_mode_suffix
);
1953 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0 || grub_strcmp(key
, cur
->pcName
) == 0)
1955 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
1956 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
1967 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1972 VTOY_JSON
*json
= NULL
;
1977 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
1980 return GRUB_ERR_NONE
;
1983 debug("json configuration file size %d\n", (int)file
->size
);
1985 buf
= grub_malloc(file
->size
+ 1);
1988 grub_file_close(file
);
1992 buf
[file
->size
] = 0;
1993 grub_file_read(file
, buf
, file
->size
);
1994 grub_file_close(file
);
1996 json
= vtoy_json_create();
2004 ret
= vtoy_json_parse(json
, buf
);
2007 grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
2008 grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR");
2010 debug("Failed to parse json string %d\n", ret
);
2015 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
2017 vtoy_json_destroy(json
);
2021 if (g_boot_pwd
.type
)
2023 grub_printf("\n\n======= %s ======\n\n", grub_env_get("VTOY_TEXT_MENU_VER"));
2024 if (ventoy_check_password(&g_boot_pwd
, 3))
2026 grub_printf("\n!!! Password check failed, will exit after 5 seconds. !!!\n");
2033 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
2036 void ventoy_plugin_dump_injection(void)
2038 injection_config
*node
= NULL
;
2040 for (node
= g_injection_head
; node
; node
= node
->next
)
2042 grub_printf("\nIMAGE:<%s>\n", node
->isopath
);
2043 grub_printf("ARCHIVE:<%s>\n", node
->archive
);
2050 void ventoy_plugin_dump_auto_install(void)
2053 install_template
*node
= NULL
;
2055 for (node
= g_install_template_head
; node
; node
= node
->next
)
2057 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
2058 for (i
= 0; i
< node
->templatenum
; i
++)
2060 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
2067 void ventoy_plugin_dump_persistence(void)
2071 persistence_config
*node
= NULL
;
2072 ventoy_img_chunk_list chunk_list
;
2074 for (node
= g_persistence_head
; node
; node
= node
->next
)
2076 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
2078 for (i
= 0; i
< node
->backendnum
; i
++)
2080 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
2081 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
2084 grub_printf(" [ SUCCESS ]\n");
2085 grub_free(chunk_list
.chunk
);
2089 grub_printf(" [ FAILED ]\n");
2097 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
2100 install_template
*node
= NULL
;
2102 if (!g_install_template_head
)
2107 len
= (int)grub_strlen(isopath
);
2108 for (node
= g_install_template_head
; node
; node
= node
->next
)
2110 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2119 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
2121 install_template
*node
= NULL
;
2123 node
= ventoy_plugin_find_install_template(isopath
);
2124 if ((!node
) || (!node
->templatepath
))
2129 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
2134 return node
->templatepath
[node
->cursel
].path
;
2137 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
2140 persistence_config
*node
= NULL
;
2142 if (!g_persistence_head
)
2147 len
= (int)grub_strlen(isopath
);
2148 for (node
= g_persistence_head
; node
; node
= node
->next
)
2150 if ((len
== node
->pathlen
) && (ventoy_strcmp(node
->isopath
, isopath
) == 0))
2159 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
2162 grub_uint64_t start
= 0;
2163 grub_file_t file
= NULL
;
2164 persistence_config
*node
= NULL
;
2166 node
= ventoy_plugin_find_persistent(isopath
);
2167 if ((!node
) || (!node
->backendpath
))
2174 index
= node
->cursel
;
2177 if (index
< 0 || index
>= node
->backendnum
)
2182 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
2185 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
2189 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
2190 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
2191 if (NULL
== chunk_list
->chunk
)
2196 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
2197 chunk_list
->cur_chunk
= 0;
2199 start
= file
->device
->disk
->partition
->start
;
2200 ventoy_get_block_list(file
, chunk_list
, start
);
2202 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
2204 grub_free(chunk_list
->chunk
);
2205 chunk_list
->chunk
= NULL
;
2213 grub_file_close(file
);
2218 const char * ventoy_plugin_get_injection(const char *isopath
)
2221 injection_config
*node
= NULL
;
2223 if (!g_injection_head
)
2228 len
= (int)grub_strlen(isopath
);
2229 for (node
= g_injection_head
; node
; node
= node
->next
)
2231 if (node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2233 return node
->archive
;
2240 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
2243 menu_alias
*node
= NULL
;
2245 if (!g_menu_alias_head
)
2250 len
= (int)grub_strlen(isopath
);
2251 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
2253 if (node
->type
== type
&& node
->pathlen
&&
2254 node
->pathlen
== len
&& ventoy_strcmp(node
->isopath
, isopath
) == 0)
2263 const char * ventoy_plugin_get_menu_class(int type
, const char *name
, const char *path
)
2266 menu_class
*node
= NULL
;
2268 if (!g_menu_class_head
)
2273 if (vtoy_class_image_file
== type
)
2275 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2277 if (node
->type
!= type
)
2284 len
= (int)grub_strlen(path
);
2285 if ((node
->patlen
< len
) && (path
[node
->patlen
] == '/') &&
2286 (grub_strncmp(path
, node
->pattern
, node
->patlen
) == 0))
2293 len
= (int)grub_strlen(name
);
2294 if ((node
->patlen
< len
) && grub_strstr(name
, node
->pattern
))
2303 len
= (int)grub_strlen(name
);
2304 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2306 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
2316 int ventoy_plugin_add_custom_boot(const char *vcfgpath
)
2319 custom_boot
*node
= NULL
;
2321 node
= grub_zalloc(sizeof(custom_boot
));
2324 node
->type
= vtoy_custom_boot_image_file
;
2325 node
->pathlen
= grub_snprintf(node
->path
, sizeof(node
->path
), "%s", vcfgpath
);
2326 grub_snprintf(node
->cfg
, sizeof(node
->cfg
), "%s", vcfgpath
);
2329 len
= node
->pathlen
- 5;
2330 node
->path
[len
] = 0;
2331 node
->pathlen
= len
;
2333 if (g_custom_boot_head
)
2335 node
->next
= g_custom_boot_head
;
2337 g_custom_boot_head
= node
;
2343 const char * ventoy_plugin_get_custom_boot(const char *isopath
)
2347 custom_boot
*node
= NULL
;
2349 if (!g_custom_boot_head
)
2354 len
= (int)grub_strlen(isopath
);
2356 for (node
= g_custom_boot_head
; node
; node
= node
->next
)
2358 if (node
->type
== vtoy_custom_boot_image_file
)
2360 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->path
, len
) == 0)
2367 if (node
->pathlen
< len
&& isopath
[node
->pathlen
] == '/' &&
2368 grub_strncmp(isopath
, node
->path
, node
->pathlen
) == 0)
2370 for (i
= node
->pathlen
+ 1; i
< len
; i
++)
2372 if (isopath
[i
] == '/')
2389 grub_err_t
ventoy_cmd_dump_custom_boot(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2391 custom_boot
*node
= NULL
;
2397 for (node
= g_custom_boot_head
; node
; node
= node
->next
)
2399 grub_printf("[%s] <%s>:<%s>\n", (node
->type
== vtoy_custom_boot_directory
) ? "dir" : "file",
2400 node
->path
, node
->cfg
);
2406 int ventoy_plugin_check_memdisk(const char *isopath
)
2409 auto_memdisk
*node
= NULL
;
2411 if (!g_auto_memdisk_head
)
2416 len
= (int)grub_strlen(isopath
);
2417 for (node
= g_auto_memdisk_head
; node
; node
= node
->next
)
2419 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, isopath
, len
) == 0)
2428 int ventoy_plugin_get_image_list_index(int type
, const char *name
)
2432 image_list
*node
= NULL
;
2434 if (!g_image_list_head
)
2439 len
= (int)grub_strlen(name
);
2441 for (node
= g_image_list_head
; node
; node
= node
->next
, index
++)
2443 if (vtoy_class_directory
== type
)
2445 if (len
< node
->pathlen
&& ventoy_strncmp(node
->isopath
, name
, len
) == 0)
2452 if (len
== node
->pathlen
&& ventoy_strncmp(node
->isopath
, name
, len
) == 0)
2462 conf_replace
* ventoy_plugin_find_conf_replace(const char *iso
)
2467 if (!g_conf_replace_head
)
2472 len
= (int)grub_strlen(iso
);
2474 for (node
= g_conf_replace_head
; node
; node
= node
->next
)
2476 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, iso
, len
) == 0)
2485 dud
* ventoy_plugin_find_dud(const char *iso
)
2495 len
= (int)grub_strlen(iso
);
2496 for (node
= g_dud_head
; node
; node
= node
->next
)
2498 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, iso
, len
) == 0)
2507 int ventoy_plugin_load_dud(dud
*node
, const char *isopart
)
2513 for (i
= 0; i
< node
->dudnum
; i
++)
2515 if (node
->files
[i
].size
> 0)
2517 debug("file %d has been loaded\n", i
);
2521 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isopart
, node
->dudpath
[i
].path
);
2524 buf
= grub_malloc(file
->size
);
2527 grub_file_read(file
, buf
, file
->size
);
2528 node
->files
[i
].size
= (int)file
->size
;
2529 node
->files
[i
].buf
= buf
;
2531 grub_file_close(file
);
2538 static const vtoy_password
* ventoy_plugin_get_password(const char *isopath
)
2542 const char *pos
= NULL
;
2543 menu_password
*node
= NULL
;
2552 len
= (int)grub_strlen(isopath
);
2553 for (node
= g_pwd_head
; node
; node
= node
->next
)
2555 if (node
->type
== vtoy_menu_pwd_file
)
2557 if (node
->pathlen
== len
&& ventoy_strncmp(node
->isopath
, isopath
, len
) == 0)
2559 return &(node
->password
);
2564 for (node
= g_pwd_head
; node
; node
= node
->next
)
2566 if (node
->type
== vtoy_menu_pwd_parent
)
2568 if (node
->pathlen
< len
&& (isopath
[node
->pathlen
] == '/') &&
2569 ventoy_strncmp(node
->isopath
, isopath
, node
->pathlen
) == 0)
2571 return &(node
->password
);
2579 if (*isopath
== '.')
2588 for (i
= 0; i
< (int)ARRAY_SIZE(g_menu_prefix
); i
++)
2590 if (g_file_type_pwd
[i
].type
&& 0 == grub_strcasecmp(pos
+ 1, g_menu_prefix
[i
]))
2592 return g_file_type_pwd
+ i
;
2600 grub_err_t
ventoy_cmd_check_password(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2603 const vtoy_password
*pwd
= NULL
;
2608 pwd
= ventoy_plugin_get_password(args
[0]);
2611 if (0 == ventoy_check_password(pwd
, 1))
2629 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2636 VTOY_JSON
*node
= NULL
;
2637 VTOY_JSON
*json
= NULL
;
2646 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
2649 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
2650 grub_printf("Attention: directory name and filename are both case-sensitive.\n");
2654 buf
= grub_malloc(file
->size
+ 1);
2657 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
2661 buf
[file
->size
] = 0;
2662 grub_file_read(file
, buf
, file
->size
);
2664 json
= vtoy_json_create();
2667 grub_printf("Failed to create json\n");
2671 ret
= vtoy_json_parse(json
, buf
);
2674 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
2678 grub_snprintf(key
, sizeof(key
), "%s_%s", args
[1], g_arch_mode_suffix
);
2679 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
2681 if (grub_strcmp(node
->pcName
, args
[1]) == 0 || grub_strcmp(node
->pcName
, key
) == 0)
2689 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
2693 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
2695 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
2697 if (g_plugin_entries
[i
].checkfunc
)
2699 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
2706 check_free(file
, grub_file_close
);
2707 check_free(json
, vtoy_json_destroy
);
2708 grub_check_free(buf
);