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 install_template
*g_install_template_head
= NULL
;
47 static dud
*g_dud_head
= NULL
;
48 static menu_password
*g_pwd_head
= NULL
;
49 static persistence_config
*g_persistence_head
= NULL
;
50 static menu_alias
*g_menu_alias_head
= NULL
;
51 static menu_class
*g_menu_class_head
= NULL
;
52 static injection_config
*g_injection_head
= NULL
;
53 static auto_memdisk
*g_auto_memdisk_head
= NULL
;
54 static image_list
*g_image_list_head
= NULL
;
55 static conf_replace
*g_conf_replace_head
= NULL
;
57 static int ventoy_plugin_control_check(VTOY_JSON
*json
, const char *isodisk
)
60 VTOY_JSON
*pNode
= NULL
;
61 VTOY_JSON
*pChild
= NULL
;
65 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
67 grub_printf("Not array type %d\n", json
->enDataType
);
71 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
73 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
75 pChild
= pNode
->pstChild
;
76 if (pChild
->enDataType
== JSON_TYPE_STRING
)
78 if (grub_strcmp(pChild
->pcName
, "VTOY_DEFAULT_IMAGE") == 0)
80 grub_printf("%s: %s [%s]\n", pChild
->pcName
, pChild
->unData
.pcStrVal
,
81 ventoy_check_file_exist("%s%s", isodisk
, pChild
->unData
.pcStrVal
) ? "OK" : "NOT EXIST");
85 grub_printf("%s: %s\n", pChild
->pcName
, pChild
->unData
.pcStrVal
);
90 grub_printf("%s is NOT string type\n", pChild
->pcName
);
96 grub_printf("%s is not an object\n", pNode
->pcName
);
104 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
106 VTOY_JSON
*pNode
= NULL
;
107 VTOY_JSON
*pChild
= NULL
;
111 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
113 debug("Not array %d\n", json
->enDataType
);
117 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
119 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
121 pChild
= pNode
->pstChild
;
122 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
124 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
132 static int ventoy_plugin_theme_check(VTOY_JSON
*json
, const char *isodisk
)
138 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
141 grub_printf("file: %s\n", value
);
144 exist
= ventoy_is_file_exist("%s%s", isodisk
, value
);
148 exist
= ventoy_is_file_exist("%s/ventoy/%s", isodisk
, value
);
153 grub_printf("Theme file %s does NOT exist\n", value
);
158 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
161 grub_printf("gfxmode: %s\n", value
);
164 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
167 grub_printf("display_mode: %s\n", value
);
170 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
173 grub_printf("serial_param %s\n", value
);
176 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
179 grub_printf("ventoy_left: %s\n", value
);
182 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
185 grub_printf("ventoy_top: %s\n", value
);
188 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
191 grub_printf("ventoy_color: %s\n", value
);
194 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
197 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
199 if (node
->enDataType
== JSON_TYPE_STRING
)
201 if (ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
203 grub_printf("%s [OK]\n", node
->unData
.pcStrVal
);
207 grub_printf("%s [NOT EXIST]\n", node
->unData
.pcStrVal
);
214 grub_printf("fonts NOT found\n");
220 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
226 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
231 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
235 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
238 if (ventoy_is_file_exist(filepath
) == 0)
240 debug("Theme file %s does not exist\n", filepath
);
244 debug("vtoy_theme %s\n", filepath
);
245 grub_env_set("vtoy_theme", filepath
);
248 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
251 debug("vtoy_gfxmode %s\n", value
);
252 grub_env_set("vtoy_gfxmode", value
);
255 value
= vtoy_json_get_string_ex(json
->pstChild
, "display_mode");
258 debug("display_mode %s\n", value
);
259 grub_env_set("vtoy_display_mode", value
);
262 value
= vtoy_json_get_string_ex(json
->pstChild
, "serial_param");
265 debug("serial_param %s\n", value
);
266 grub_env_set("vtoy_serial_param", value
);
269 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
272 grub_env_set("VTLE_LFT", value
);
275 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
278 grub_env_set("VTLE_TOP", value
);
281 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
284 grub_env_set("VTLE_CLR", value
);
287 node
= vtoy_json_find_item(json
->pstChild
, JSON_TYPE_ARRAY
, "fonts");
290 for (node
= node
->pstChild
; node
; node
= node
->pstNext
)
292 if (node
->enDataType
== JSON_TYPE_STRING
&&
293 ventoy_check_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
))
295 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, node
->unData
.pcStrVal
);
296 grub_font_load(filepath
);
304 static int ventoy_plugin_check_path(const char *path
, const char *file
)
308 grub_printf("%s is NOT begin with '/' \n", file
);
312 if (grub_strchr(file
, '\\'))
314 grub_printf("%s contains invalid '\\' \n", file
);
318 if (grub_strstr(file
, "//"))
320 grub_printf("%s contains invalid double slash\n", file
);
324 if (grub_strstr(file
, "../"))
326 grub_printf("%s contains invalid '../' \n", file
);
330 if (!ventoy_is_file_exist("%s%s", path
, file
))
332 grub_printf("%s%s does NOT exist\n", path
, file
);
339 static int ventoy_plugin_check_fullpath
350 VTOY_JSON
*node
= json
;
351 VTOY_JSON
*child
= NULL
;
355 if (0 == grub_strcmp(key
, node
->pcName
))
359 node
= node
->pstNext
;
367 if (JSON_TYPE_STRING
== node
->enDataType
)
370 ret
= ventoy_plugin_check_path(isodisk
, node
->unData
.pcStrVal
);
371 grub_printf("%s: %s [%s]\n", key
, node
->unData
.pcStrVal
, ret
? "FAIL" : "OK");
373 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
375 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
377 if (JSON_TYPE_STRING
!= child
->enDataType
)
379 grub_printf("Non string json type\n");
383 rc
= ventoy_plugin_check_path(isodisk
, child
->unData
.pcStrVal
);
384 grub_printf("%s: %s [%s]\n", key
, child
->unData
.pcStrVal
, rc
? "FAIL" : "OK");
395 static int ventoy_plugin_parse_fullpath
400 file_fullpath
**fullpath
,
406 VTOY_JSON
*node
= json
;
407 VTOY_JSON
*child
= NULL
;
408 file_fullpath
*path
= NULL
;
412 if (0 == grub_strcmp(key
, node
->pcName
))
416 node
= node
->pstNext
;
424 if (JSON_TYPE_STRING
== node
->enDataType
)
426 debug("%s is string type data\n", node
->pcName
);
428 if ((node
->unData
.pcStrVal
[0] != '/') || (!ventoy_is_file_exist("%s%s", isodisk
, node
->unData
.pcStrVal
)))
430 debug("%s%s file not found\n", isodisk
, node
->unData
.pcStrVal
);
434 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
));
437 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", node
->unData
.pcStrVal
);
443 else if (JSON_TYPE_ARRAY
== node
->enDataType
)
445 for (child
= node
->pstChild
; child
; child
= child
->pstNext
)
447 if ((JSON_TYPE_STRING
!= child
->enDataType
) || (child
->unData
.pcStrVal
[0] != '/'))
449 debug("Invalid data type:%d\n", child
->enDataType
);
454 debug("%s is array type data, count=%d\n", node
->pcName
, count
);
456 path
= (file_fullpath
*)grub_zalloc(sizeof(file_fullpath
) * count
);
461 for (count
= 0, child
= node
->pstChild
; child
; child
= child
->pstNext
)
463 if (ventoy_is_file_exist("%s%s", isodisk
, child
->unData
.pcStrVal
))
465 grub_snprintf(path
->path
, sizeof(path
->path
), "%s", child
->unData
.pcStrVal
);
479 static int ventoy_plugin_auto_install_check(VTOY_JSON
*json
, const char *isodisk
)
483 const char *iso
= NULL
;
484 VTOY_JSON
*pNode
= NULL
;
486 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
488 grub_printf("Not array type %d\n", json
->enDataType
);
492 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
494 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
496 grub_printf("NOT object type\n");
499 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
502 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
504 grub_printf("image: %s [OK]\n", iso
);
505 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "template", &pathnum
);
507 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
509 if (autosel
>= 0 && autosel
<= pathnum
)
511 grub_printf("autosel: %d [OK]\n", autosel
);
515 grub_printf("autosel: %d [FAIL]\n", autosel
);
521 grub_printf("image: %s [FAIL]\n", iso
);
526 grub_printf("image not found\n");
533 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
537 const char *iso
= NULL
;
538 VTOY_JSON
*pNode
= NULL
;
539 install_template
*node
= NULL
;
540 install_template
*next
= NULL
;
541 file_fullpath
*templatepath
= NULL
;
543 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
545 debug("Not array %d\n", json
->enDataType
);
549 if (g_install_template_head
)
551 for (node
= g_install_template_head
; node
; node
= next
)
554 grub_check_free(node
->templatepath
);
558 g_install_template_head
= NULL
;
561 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
563 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
564 if (iso
&& iso
[0] == '/')
566 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "template", &templatepath
, &pathnum
))
568 node
= grub_zalloc(sizeof(install_template
));
571 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
572 node
->templatepath
= templatepath
;
573 node
->templatenum
= pathnum
;
576 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
578 if (autosel
>= 0 && autosel
<= pathnum
)
580 node
->autosel
= autosel
;
584 if (g_install_template_head
)
586 node
->next
= g_install_template_head
;
589 g_install_template_head
= node
;
598 static int ventoy_plugin_dud_check(VTOY_JSON
*json
, const char *isodisk
)
601 const char *iso
= NULL
;
602 VTOY_JSON
*pNode
= NULL
;
604 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
606 grub_printf("Not array type %d\n", json
->enDataType
);
610 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
612 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
614 grub_printf("NOT object type\n");
617 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
620 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
622 grub_printf("image: %s [OK]\n", iso
);
623 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "dud", &pathnum
);
627 grub_printf("image: %s [FAIL]\n", iso
);
632 grub_printf("image not found\n");
639 static int ventoy_plugin_dud_entry(VTOY_JSON
*json
, const char *isodisk
)
642 const char *iso
= NULL
;
643 VTOY_JSON
*pNode
= NULL
;
646 file_fullpath
*dudpath
= NULL
;
648 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
650 debug("Not array %d\n", json
->enDataType
);
656 for (node
= g_dud_head
; node
; node
= next
)
659 grub_check_free(node
->dudpath
);
666 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
668 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
669 if (iso
&& iso
[0] == '/')
671 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "dud", &dudpath
, &pathnum
))
673 node
= grub_zalloc(sizeof(dud
));
676 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
677 node
->dudpath
= dudpath
;
678 node
->dudnum
= pathnum
;
679 node
->files
= grub_zalloc(sizeof(dudfile
) * pathnum
);
685 node
->next
= g_dud_head
;
702 static int ventoy_plugin_parse_pwdstr(char *pwdstr
, vtoy_password
*pwd
)
709 vtoy_password tmpPwd
;
711 len
= (int)grub_strlen(pwdstr
);
714 if (NULL
== pwd
) grub_printf("Password too long %d\n", len
);
718 grub_memset(&tmpPwd
, 0, sizeof(tmpPwd
));
720 if (grub_strncmp(pwdstr
, "txt#", 4) == 0)
722 tmpPwd
.type
= VTOY_PASSWORD_TXT
;
723 grub_snprintf(tmpPwd
.text
, sizeof(tmpPwd
.text
), "%s", pwdstr
+ 4);
725 else if (grub_strncmp(pwdstr
, "md5#", 4) == 0)
729 for (i
= 0; i
< 16; i
++)
731 bytes
[0] = pwdstr
[4 + i
* 2];
732 bytes
[1] = pwdstr
[4 + i
* 2 + 1];
735 if (grub_isxdigit(bytes
[0]) && grub_isxdigit(bytes
[1]))
737 tmpPwd
.md5
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
741 if (NULL
== pwd
) grub_printf("Invalid md5 hex format %s %d\n", pwdstr
, i
);
745 tmpPwd
.type
= VTOY_PASSWORD_MD5
;
747 else if ((len
- 4) > 32)
749 pos
= grub_strchr(pwdstr
+ 4, '#');
752 if (NULL
== pwd
) grub_printf("Invalid md5 password format %s\n", pwdstr
);
756 if (len
- 1 - (int)(long)(pos
- pwdstr
) != 32)
758 if (NULL
== pwd
) grub_printf("Invalid md5 salt password format %s\n", pwdstr
);
764 grub_snprintf(tmpPwd
.salt
, sizeof(tmpPwd
.salt
), "%s", pwdstr
+ 4);
768 for (i
= 0; i
< 16; i
++)
770 bytes
[0] = pos
[i
* 2];
771 bytes
[1] = pos
[i
* 2 + 1];
774 if (grub_isxdigit(bytes
[0]) && grub_isxdigit(bytes
[1]))
776 tmpPwd
.md5
[i
] = (grub_uint8_t
)grub_strtoul(bytes
, NULL
, 16);
780 if (NULL
== pwd
) grub_printf("Invalid md5 hex format %s %d\n", pwdstr
, i
);
785 tmpPwd
.type
= VTOY_PASSWORD_SALT_MD5
;
789 if (NULL
== pwd
) grub_printf("Invalid md5 password format %s\n", pwdstr
);
795 if (NULL
== pwd
) grub_printf("Invalid password format %s\n", pwdstr
);
801 grub_memcpy(pwd
, &tmpPwd
, sizeof(tmpPwd
));
807 static int ventoy_plugin_pwd_entry(VTOY_JSON
*json
, const char *isodisk
)
809 const char *iso
= NULL
;
810 const char *pwd
= NULL
;
811 VTOY_JSON
*pNode
= NULL
;
812 VTOY_JSON
*pCNode
= NULL
;
813 menu_password
*node
= NULL
;
814 menu_password
*tail
= NULL
;
815 menu_password
*next
= NULL
;
819 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
821 debug("Not object %d\n", json
->enDataType
);
827 for (node
= g_pwd_head
; node
; node
= next
)
836 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
838 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
840 ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, &g_boot_pwd
);
842 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
844 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
846 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
851 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
852 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
853 if (iso
&& pwd
&& iso
[0] == '/')
855 node
= grub_zalloc(sizeof(menu_password
));
858 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
860 if (ventoy_plugin_parse_pwdstr((char *)pwd
, &(node
->password
)))
884 static int ventoy_plugin_pwd_check(VTOY_JSON
*json
, const char *isodisk
)
886 const char *iso
= NULL
;
887 const char *pwd
= NULL
;
888 VTOY_JSON
*pNode
= NULL
;
889 VTOY_JSON
*pCNode
= NULL
;
891 if (json
->enDataType
!= JSON_TYPE_OBJECT
)
893 grub_printf("Not object %d\n", json
->enDataType
);
897 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
899 if (pNode
->pcName
&& grub_strcmp("bootpwd", pNode
->pcName
) == 0)
901 if (0 == ventoy_plugin_parse_pwdstr(pNode
->unData
.pcStrVal
, NULL
))
903 grub_printf("bootpwd:<%s>\n", pNode
->unData
.pcStrVal
);
907 grub_printf("Invalid bootpwd.\n");
910 else if (pNode
->pcName
&& grub_strcmp("menupwd", pNode
->pcName
) == 0)
913 for (pCNode
= pNode
->pstChild
; pCNode
; pCNode
= pCNode
->pstNext
)
915 if (pCNode
->enDataType
!= JSON_TYPE_OBJECT
)
917 grub_printf("Not object %d\n", pCNode
->enDataType
);
921 iso
= vtoy_json_get_string_ex(pCNode
->pstChild
, "file");
924 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
926 pwd
= vtoy_json_get_string_ex(pCNode
->pstChild
, "pwd");
928 if (0 == ventoy_plugin_parse_pwdstr((char *)pwd
, NULL
))
930 grub_printf("file:<%s> [OK]\n", iso
);
931 grub_printf("pwd:<%s>\n\n", pwd
);
935 grub_printf("Invalid password for <%s>\n", iso
);
940 grub_printf("<%s%s> not found\n", isodisk
, iso
);
945 grub_printf("No file item found in json.\n");
954 static int ventoy_plugin_persistence_check(VTOY_JSON
*json
, const char *isodisk
)
958 const char *iso
= NULL
;
959 VTOY_JSON
*pNode
= NULL
;
961 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
963 grub_printf("Not array type %d\n", json
->enDataType
);
967 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
969 if (pNode
->enDataType
!= JSON_TYPE_OBJECT
)
971 grub_printf("NOT object type\n");
974 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
977 if (0 == ventoy_plugin_check_path(isodisk
, iso
))
979 grub_printf("image: %s [OK]\n", iso
);
980 ventoy_plugin_check_fullpath(pNode
->pstChild
, isodisk
, "backend", &pathnum
);
982 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
984 if (autosel
>= 0 && autosel
<= pathnum
)
986 grub_printf("autosel: %d [OK]\n", autosel
);
990 grub_printf("autosel: %d [FAIL]\n", autosel
);
996 grub_printf("image: %s [FAIL]\n", iso
);
1001 grub_printf("image not found\n");
1008 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
1012 const char *iso
= NULL
;
1013 VTOY_JSON
*pNode
= NULL
;
1014 persistence_config
*node
= NULL
;
1015 persistence_config
*next
= NULL
;
1016 file_fullpath
*backendpath
= NULL
;
1020 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1022 debug("Not array %d\n", json
->enDataType
);
1026 if (g_persistence_head
)
1028 for (node
= g_persistence_head
; node
; node
= next
)
1031 grub_check_free(node
->backendpath
);
1035 g_persistence_head
= NULL
;
1038 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1040 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1041 if (iso
&& iso
[0] == '/')
1043 if (0 == ventoy_plugin_parse_fullpath(pNode
->pstChild
, isodisk
, "backend", &backendpath
, &pathnum
))
1045 node
= grub_zalloc(sizeof(persistence_config
));
1048 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
1049 node
->backendpath
= backendpath
;
1050 node
->backendnum
= pathnum
;
1053 if (JSON_SUCCESS
== vtoy_json_get_int(pNode
->pstChild
, "autosel", &autosel
))
1055 if (autosel
>= 0 && autosel
<= pathnum
)
1057 node
->autosel
= autosel
;
1061 if (g_persistence_head
)
1063 node
->next
= g_persistence_head
;
1066 g_persistence_head
= node
;
1075 static int ventoy_plugin_menualias_check(VTOY_JSON
*json
, const char *isodisk
)
1078 const char *path
= NULL
;
1079 const char *alias
= NULL
;
1080 VTOY_JSON
*pNode
= NULL
;
1084 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1086 grub_printf("Not array %d\n", json
->enDataType
);
1090 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1092 type
= vtoy_alias_image_file
;
1093 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1096 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1097 type
= vtoy_alias_directory
;
1100 alias
= vtoy_json_get_string_ex(pNode
->pstChild
, "alias");
1101 if (path
&& path
[0] == '/' && alias
)
1103 if (vtoy_alias_image_file
== type
)
1105 if (ventoy_is_file_exist("%s%s", isodisk
, path
))
1107 grub_printf("image: <%s> [ OK ]\n", path
);
1111 grub_printf("image: <%s> [ NOT EXIST ]\n", path
);
1116 if (ventoy_is_dir_exist("%s%s", isodisk
, path
))
1118 grub_printf("dir: <%s> [ OK ]\n", path
);
1122 grub_printf("dir: <%s> [ NOT EXIST ]\n", path
);
1126 grub_printf("alias: <%s>\n\n", alias
);
1133 static int ventoy_plugin_menualias_entry(VTOY_JSON
*json
, const char *isodisk
)
1136 const char *path
= NULL
;
1137 const char *alias
= NULL
;
1138 VTOY_JSON
*pNode
= NULL
;
1139 menu_alias
*node
= NULL
;
1140 menu_alias
*next
= NULL
;
1144 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1146 debug("Not array %d\n", json
->enDataType
);
1150 if (g_menu_alias_head
)
1152 for (node
= g_menu_alias_head
; node
; node
= next
)
1158 g_menu_alias_head
= NULL
;
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 node
= grub_zalloc(sizeof(menu_alias
));
1178 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1179 grub_snprintf(node
->alias
, sizeof(node
->alias
), "%s", alias
);
1181 if (g_menu_alias_head
)
1183 node
->next
= g_menu_alias_head
;
1186 g_menu_alias_head
= node
;
1195 static int ventoy_plugin_injection_check(VTOY_JSON
*json
, const char *isodisk
)
1197 const char *path
= NULL
;
1198 const char *archive
= NULL
;
1199 VTOY_JSON
*pNode
= NULL
;
1203 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1205 grub_printf("Not array %d\n", json
->enDataType
);
1209 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1211 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1214 grub_printf("image not found\n");
1218 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1221 grub_printf("archive not found\n");
1225 grub_printf("image: <%s> [%s]\n", path
, ventoy_check_file_exist("%s%s", isodisk
, path
) ? "OK" : "NOT EXIST");
1226 grub_printf("archive: <%s> [%s]\n\n", archive
, ventoy_check_file_exist("%s%s", isodisk
, archive
) ? "OK" : "NOT EXIST");
1232 static int ventoy_plugin_injection_entry(VTOY_JSON
*json
, const char *isodisk
)
1234 const char *path
= NULL
;
1235 const char *archive
= NULL
;
1236 VTOY_JSON
*pNode
= NULL
;
1237 injection_config
*node
= NULL
;
1238 injection_config
*next
= NULL
;
1242 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1244 debug("Not array %d\n", json
->enDataType
);
1248 if (g_injection_head
)
1250 for (node
= g_injection_head
; node
; node
= next
)
1256 g_injection_head
= NULL
;
1259 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1261 path
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
1262 archive
= vtoy_json_get_string_ex(pNode
->pstChild
, "archive");
1263 if (path
&& path
[0] == '/' && archive
&& archive
[0] == '/')
1265 node
= grub_zalloc(sizeof(injection_config
));
1268 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", path
);
1269 grub_snprintf(node
->archive
, sizeof(node
->archive
), "%s", archive
);
1271 if (g_injection_head
)
1273 node
->next
= g_injection_head
;
1276 g_injection_head
= node
;
1284 static int ventoy_plugin_menuclass_entry(VTOY_JSON
*json
, const char *isodisk
)
1287 const char *key
= NULL
;
1288 const char *class = NULL
;
1289 VTOY_JSON
*pNode
= NULL
;
1290 menu_class
*tail
= NULL
;
1291 menu_class
*node
= NULL
;
1292 menu_class
*next
= NULL
;
1296 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1298 debug("Not array %d\n", json
->enDataType
);
1302 if (g_menu_class_head
)
1304 for (node
= g_menu_class_head
; node
; node
= next
)
1310 g_menu_class_head
= NULL
;
1313 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1315 type
= vtoy_class_image_file
;
1316 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1319 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1320 type
= vtoy_class_directory
;
1323 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1326 node
= grub_zalloc(sizeof(menu_class
));
1330 node
->patlen
= grub_snprintf(node
->pattern
, sizeof(node
->pattern
), "%s", key
);
1331 grub_snprintf(node
->class, sizeof(node
->class), "%s", class);
1333 if (g_menu_class_head
)
1339 g_menu_class_head
= node
;
1349 static int ventoy_plugin_menuclass_check(VTOY_JSON
*json
, const char *isodisk
)
1352 const char *key
= NULL
;
1353 const char *class = NULL
;
1354 VTOY_JSON
*pNode
= NULL
;
1358 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1360 grub_printf("Not array %d\n", json
->enDataType
);
1364 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1366 type
= vtoy_class_image_file
;
1367 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "key");
1370 key
= vtoy_json_get_string_ex(pNode
->pstChild
, "dir");
1371 type
= vtoy_class_directory
;
1374 class = vtoy_json_get_string_ex(pNode
->pstChild
, "class");
1377 grub_printf("%s: <%s>\n", (type
== vtoy_class_directory
) ? "dir" : "key", key
);
1378 grub_printf("class: <%s>\n\n", class);
1385 static int ventoy_plugin_conf_replace_entry(VTOY_JSON
*json
, const char *isodisk
)
1387 const char *isof
= NULL
;
1388 const char *orgf
= NULL
;
1389 const char *newf
= NULL
;
1390 VTOY_JSON
*pNode
= NULL
;
1391 conf_replace
*tail
= NULL
;
1392 conf_replace
*node
= NULL
;
1393 conf_replace
*next
= NULL
;
1397 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1399 debug("Not array %d\n", json
->enDataType
);
1403 if (g_conf_replace_head
)
1405 for (node
= g_conf_replace_head
; node
; node
= next
)
1411 g_conf_replace_head
= NULL
;
1414 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1416 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1417 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1418 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1419 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1421 node
= grub_zalloc(sizeof(conf_replace
));
1424 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", isof
);
1425 grub_snprintf(node
->orgconf
, sizeof(node
->orgconf
), "%s", orgf
);
1426 grub_snprintf(node
->newconf
, sizeof(node
->newconf
), "%s", newf
);
1428 if (g_conf_replace_head
)
1434 g_conf_replace_head
= node
;
1444 static int ventoy_plugin_conf_replace_check(VTOY_JSON
*json
, const char *isodisk
)
1446 const char *isof
= NULL
;
1447 const char *orgf
= NULL
;
1448 const char *newf
= NULL
;
1449 VTOY_JSON
*pNode
= NULL
;
1450 grub_file_t file
= NULL
;
1455 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1457 grub_printf("Not array %d\n", json
->enDataType
);
1461 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1463 isof
= vtoy_json_get_string_ex(pNode
->pstChild
, "iso");
1464 orgf
= vtoy_json_get_string_ex(pNode
->pstChild
, "org");
1465 newf
= vtoy_json_get_string_ex(pNode
->pstChild
, "new");
1466 if (isof
&& orgf
&& newf
&& isof
[0] == '/' && orgf
[0] == '/' && newf
[0] == '/')
1468 if (ventoy_check_file_exist("%s%s", isodisk
, isof
))
1470 grub_printf("iso:<%s> [OK]\n", isof
);
1472 grub_snprintf(cmd
, sizeof(cmd
), "loopback vtisocheck \"%s%s\"", isodisk
, isof
);
1473 grub_script_execute_sourcecode(cmd
);
1475 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "(vtisocheck)/%s", orgf
);
1478 if (grub_strcmp(file
->fs
->name
, "iso9660") == 0)
1480 grub_printf("org:<%s> [OK]\n", orgf
);
1484 grub_printf("org:<%s> [Exist But NOT ISO9660]\n", orgf
);
1486 grub_file_close(file
);
1490 grub_printf("org:<%s> [NOT Exist]\n", orgf
);
1493 grub_script_execute_sourcecode("loopback -d vtisocheck");
1497 grub_printf("iso:<%s> [NOT Exist]\n", isof
);
1498 grub_printf("org:<%s>\n", orgf
);
1501 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isodisk
, newf
);
1504 if (file
->size
> vtoy_max_replace_file_size
)
1506 grub_printf("new:<%s> [Too Big %lu] \n", newf
, (ulong
)file
->size
);
1510 grub_printf("new:<%s> [OK]\n", newf
);
1512 grub_file_close(file
);
1516 grub_printf("new:<%s> [NOT Exist]\n", newf
);
1525 static int ventoy_plugin_auto_memdisk_entry(VTOY_JSON
*json
, const char *isodisk
)
1527 VTOY_JSON
*pNode
= NULL
;
1528 auto_memdisk
*node
= NULL
;
1529 auto_memdisk
*next
= NULL
;
1533 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1535 debug("Not array %d\n", json
->enDataType
);
1539 if (g_auto_memdisk_head
)
1541 for (node
= g_auto_memdisk_head
; node
; node
= next
)
1547 g_auto_memdisk_head
= NULL
;
1550 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1552 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1554 node
= grub_zalloc(sizeof(auto_memdisk
));
1557 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1559 if (g_auto_memdisk_head
)
1561 node
->next
= g_auto_memdisk_head
;
1564 g_auto_memdisk_head
= node
;
1572 static int ventoy_plugin_auto_memdisk_check(VTOY_JSON
*json
, const char *isodisk
)
1574 VTOY_JSON
*pNode
= NULL
;
1576 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1578 grub_printf("Not array %d\n", json
->enDataType
);
1582 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1584 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1586 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1588 if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1590 grub_printf(" [OK]\n");
1594 grub_printf(" [NOT EXIST]\n");
1602 static int ventoy_plugin_image_list_entry(VTOY_JSON
*json
, const char *isodisk
)
1604 VTOY_JSON
*pNode
= NULL
;
1605 image_list
*node
= NULL
;
1606 image_list
*next
= NULL
;
1607 image_list
*tail
= NULL
;
1611 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1613 debug("Not array %d\n", json
->enDataType
);
1617 if (g_image_list_head
)
1619 for (node
= g_image_list_head
; node
; node
= next
)
1625 g_image_list_head
= NULL
;
1628 g_plugin_image_list
= 1;
1630 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1632 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1634 node
= grub_zalloc(sizeof(image_list
));
1637 node
->pathlen
= grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", pNode
->unData
.pcStrVal
);
1639 if (g_image_list_head
)
1645 g_image_list_head
= node
;
1655 static int ventoy_plugin_image_list_check(VTOY_JSON
*json
, const char *isodisk
)
1657 VTOY_JSON
*pNode
= NULL
;
1659 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
1661 grub_printf("Not array %d\n", json
->enDataType
);
1665 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
1667 if (pNode
->enDataType
== JSON_TYPE_STRING
)
1669 grub_printf("<%s> ", pNode
->unData
.pcStrVal
);
1671 if (ventoy_check_file_exist("%s%s", isodisk
, pNode
->unData
.pcStrVal
))
1673 grub_printf(" [OK]\n");
1677 grub_printf(" [NOT EXIST]\n");
1685 static plugin_entry g_plugin_entries
[] =
1687 { "control", ventoy_plugin_control_entry
, ventoy_plugin_control_check
},
1688 { "theme", ventoy_plugin_theme_entry
, ventoy_plugin_theme_check
},
1689 { "auto_install", ventoy_plugin_auto_install_entry
, ventoy_plugin_auto_install_check
},
1690 { "persistence", ventoy_plugin_persistence_entry
, ventoy_plugin_persistence_check
},
1691 { "menu_alias", ventoy_plugin_menualias_entry
, ventoy_plugin_menualias_check
},
1692 { "menu_class", ventoy_plugin_menuclass_entry
, ventoy_plugin_menuclass_check
},
1693 { "injection", ventoy_plugin_injection_entry
, ventoy_plugin_injection_check
},
1694 { "auto_memdisk", ventoy_plugin_auto_memdisk_entry
, ventoy_plugin_auto_memdisk_check
},
1695 { "image_list", ventoy_plugin_image_list_entry
, ventoy_plugin_image_list_check
},
1696 { "conf_replace", ventoy_plugin_conf_replace_entry
, ventoy_plugin_conf_replace_check
},
1697 { "dud", ventoy_plugin_dud_entry
, ventoy_plugin_dud_check
},
1698 { "password", ventoy_plugin_pwd_entry
, ventoy_plugin_pwd_check
},
1701 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
1705 VTOY_JSON
*cur
= json
;
1707 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
1711 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
1713 grub_snprintf(key
, sizeof(key
), "%s_%s", g_plugin_entries
[i
].key
, g_arch_mode_suffix
);
1714 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0 || grub_strcmp(key
, cur
->pcName
) == 0)
1716 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
1717 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
1728 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
1733 VTOY_JSON
*json
= NULL
;
1738 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
1741 return GRUB_ERR_NONE
;
1744 debug("json configuration file size %d\n", (int)file
->size
);
1746 buf
= grub_malloc(file
->size
+ 1);
1749 grub_file_close(file
);
1753 buf
[file
->size
] = 0;
1754 grub_file_read(file
, buf
, file
->size
);
1755 grub_file_close(file
);
1757 json
= vtoy_json_create();
1765 ret
= vtoy_json_parse(json
, buf
);
1768 grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1");
1769 grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR");
1771 debug("Failed to parse json string %d\n", ret
);
1776 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
1778 vtoy_json_destroy(json
);
1782 if (g_boot_pwd
.type
)
1784 grub_printf("\n\n======= %s ======\n\n", grub_env_get("VTOY_TEXT_MENU_VER"));
1785 if (ventoy_check_password(&g_boot_pwd
, 3))
1787 grub_printf("\n!!! Password check failed, will exit after 5 seconds. !!!\n");
1794 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
1797 void ventoy_plugin_dump_injection(void)
1799 injection_config
*node
= NULL
;
1801 for (node
= g_injection_head
; node
; node
= node
->next
)
1803 grub_printf("\nIMAGE:<%s>\n", node
->isopath
);
1804 grub_printf("ARCHIVE:<%s>\n", node
->archive
);
1811 void ventoy_plugin_dump_auto_install(void)
1814 install_template
*node
= NULL
;
1816 for (node
= g_install_template_head
; node
; node
= node
->next
)
1818 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->templatenum
);
1819 for (i
= 0; i
< node
->templatenum
; i
++)
1821 grub_printf("SCRIPT %d:<%s>\n", i
, node
->templatepath
[i
].path
);
1828 void ventoy_plugin_dump_persistence(void)
1832 persistence_config
*node
= NULL
;
1833 ventoy_img_chunk_list chunk_list
;
1835 for (node
= g_persistence_head
; node
; node
= node
->next
)
1837 grub_printf("\nIMAGE:<%s> <%d>\n", node
->isopath
, node
->backendnum
);
1839 for (i
= 0; i
< node
->backendnum
; i
++)
1841 grub_printf("PERSIST %d:<%s>", i
, node
->backendpath
[i
].path
);
1842 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, i
, &chunk_list
);
1845 grub_printf(" [ SUCCESS ]\n");
1846 grub_free(chunk_list
.chunk
);
1850 grub_printf(" [ FAILED ]\n");
1858 install_template
* ventoy_plugin_find_install_template(const char *isopath
)
1861 install_template
*node
= NULL
;
1863 if (!g_install_template_head
)
1868 len
= (int)grub_strlen(isopath
);
1869 for (node
= g_install_template_head
; node
; node
= node
->next
)
1871 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1880 char * ventoy_plugin_get_cur_install_template(const char *isopath
)
1882 install_template
*node
= NULL
;
1884 node
= ventoy_plugin_find_install_template(isopath
);
1885 if ((!node
) || (!node
->templatepath
))
1890 if (node
->cursel
< 0 || node
->cursel
>= node
->templatenum
)
1895 return node
->templatepath
[node
->cursel
].path
;
1898 persistence_config
* ventoy_plugin_find_persistent(const char *isopath
)
1901 persistence_config
*node
= NULL
;
1903 if (!g_persistence_head
)
1908 len
= (int)grub_strlen(isopath
);
1909 for (node
= g_persistence_head
; node
; node
= node
->next
)
1911 if ((len
== node
->pathlen
) && (grub_strcmp(node
->isopath
, isopath
) == 0))
1920 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, int index
, ventoy_img_chunk_list
*chunk_list
)
1923 grub_uint64_t start
= 0;
1924 grub_file_t file
= NULL
;
1925 persistence_config
*node
= NULL
;
1927 node
= ventoy_plugin_find_persistent(isopath
);
1928 if ((!node
) || (!node
->backendpath
))
1935 index
= node
->cursel
;
1938 if (index
< 0 || index
>= node
->backendnum
)
1943 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->backendpath
[index
].path
);
1946 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->backendpath
[index
].path
);
1950 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
1951 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
1952 if (NULL
== chunk_list
->chunk
)
1957 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
1958 chunk_list
->cur_chunk
= 0;
1960 start
= file
->device
->disk
->partition
->start
;
1961 ventoy_get_block_list(file
, chunk_list
, start
);
1963 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
1965 grub_free(chunk_list
->chunk
);
1966 chunk_list
->chunk
= NULL
;
1974 grub_file_close(file
);
1979 const char * ventoy_plugin_get_injection(const char *isopath
)
1982 injection_config
*node
= NULL
;
1984 if (!g_injection_head
)
1989 len
= (int)grub_strlen(isopath
);
1990 for (node
= g_injection_head
; node
; node
= node
->next
)
1992 if (node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
1994 return node
->archive
;
2001 const char * ventoy_plugin_get_menu_alias(int type
, const char *isopath
)
2004 menu_alias
*node
= NULL
;
2006 if (!g_menu_alias_head
)
2011 len
= (int)grub_strlen(isopath
);
2012 for (node
= g_menu_alias_head
; node
; node
= node
->next
)
2014 if (node
->type
== type
&& node
->pathlen
&&
2015 node
->pathlen
== len
&& grub_strcmp(node
->isopath
, isopath
) == 0)
2024 const char * ventoy_plugin_get_menu_class(int type
, const char *name
)
2027 menu_class
*node
= NULL
;
2029 if (!g_menu_class_head
)
2034 len
= (int)grub_strlen(name
);
2036 if (vtoy_class_image_file
== type
)
2038 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2040 if (node
->type
== type
&& node
->patlen
<= len
&& grub_strstr(name
, node
->pattern
))
2048 for (node
= g_menu_class_head
; node
; node
= node
->next
)
2050 if (node
->type
== type
&& node
->patlen
== len
&& grub_strncmp(name
, node
->pattern
, len
) == 0)
2060 int ventoy_plugin_check_memdisk(const char *isopath
)
2063 auto_memdisk
*node
= NULL
;
2065 if (!g_auto_memdisk_head
)
2070 len
= (int)grub_strlen(isopath
);
2071 for (node
= g_auto_memdisk_head
; node
; node
= node
->next
)
2073 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->isopath
, len
) == 0)
2082 int ventoy_plugin_get_image_list_index(int type
, const char *name
)
2086 image_list
*node
= NULL
;
2088 if (!g_image_list_head
)
2093 len
= (int)grub_strlen(name
);
2095 for (node
= g_image_list_head
; node
; node
= node
->next
, index
++)
2097 if (vtoy_class_directory
== type
)
2099 if (len
< node
->pathlen
&& grub_strncmp(name
, node
->isopath
, len
) == 0)
2106 if (len
== node
->pathlen
&& grub_strncmp(name
, node
->isopath
, len
) == 0)
2116 conf_replace
* ventoy_plugin_find_conf_replace(const char *iso
)
2121 if (!g_conf_replace_head
)
2126 len
= (int)grub_strlen(iso
);
2128 for (node
= g_conf_replace_head
; node
; node
= node
->next
)
2130 if (node
->pathlen
== len
&& grub_strncmp(iso
, node
->isopath
, len
) == 0)
2139 dud
* ventoy_plugin_find_dud(const char *iso
)
2149 len
= (int)grub_strlen(iso
);
2150 for (node
= g_dud_head
; node
; node
= node
->next
)
2152 if (node
->pathlen
== len
&& grub_strncmp(iso
, node
->isopath
, len
) == 0)
2161 int ventoy_plugin_load_dud(dud
*node
, const char *isopart
)
2167 for (i
= 0; i
< node
->dudnum
; i
++)
2169 if (node
->files
[i
].size
> 0)
2171 debug("file %d has been loaded\n", i
);
2175 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", isopart
, node
->dudpath
[i
].path
);
2178 buf
= grub_malloc(file
->size
);
2181 grub_file_read(file
, buf
, file
->size
);
2182 node
->files
[i
].size
= (int)file
->size
;
2183 node
->files
[i
].buf
= buf
;
2185 grub_file_close(file
);
2192 static const vtoy_password
* ventoy_plugin_get_password(const char *isopath
)
2195 menu_password
*node
= NULL
;
2197 if ((!g_pwd_head
) || (!isopath
))
2202 len
= (int)grub_strlen(isopath
);
2203 for (node
= g_pwd_head
; node
; node
= node
->next
)
2205 if (node
->pathlen
== len
&& grub_strncmp(isopath
, node
->isopath
, len
) == 0)
2207 return &(node
->password
);
2214 grub_err_t
ventoy_cmd_check_password(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2217 const vtoy_password
*pwd
= NULL
;
2222 pwd
= ventoy_plugin_get_password(args
[0]);
2225 if (0 == ventoy_check_password(pwd
, 1))
2243 grub_err_t
ventoy_cmd_plugin_check_json(grub_extcmd_context_t ctxt
, int argc
, char **args
)
2250 VTOY_JSON
*node
= NULL
;
2251 VTOY_JSON
*json
= NULL
;
2260 file
= ventoy_grub_file_open(GRUB_FILE_TYPE_LINUX_INITRD
, "%s/ventoy/ventoy.json", args
[0]);
2263 grub_printf("Plugin json file /ventoy/ventoy.json does NOT exist.\n");
2264 grub_printf("Attention: directory name and filename are both case-sensitive.\n");
2268 buf
= grub_malloc(file
->size
+ 1);
2271 grub_printf("Failed to malloc memory %lu.\n", (ulong
)(file
->size
+ 1));
2275 buf
[file
->size
] = 0;
2276 grub_file_read(file
, buf
, file
->size
);
2278 json
= vtoy_json_create();
2281 grub_printf("Failed to create json\n");
2285 ret
= vtoy_json_parse(json
, buf
);
2288 grub_printf("Syntax error detected in ventoy.json, please check it.\n");
2292 grub_snprintf(key
, sizeof(key
), "%s_%s", args
[1], g_arch_mode_suffix
);
2293 for (node
= json
->pstChild
; node
; node
= node
->pstNext
)
2295 if (grub_strcmp(node
->pcName
, args
[1]) == 0 || grub_strcmp(node
->pcName
, key
) == 0)
2303 grub_printf("%s is NOT found in ventoy.json\n", args
[1]);
2307 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
2309 if (grub_strcmp(g_plugin_entries
[i
].key
, args
[1]) == 0)
2311 if (g_plugin_entries
[i
].checkfunc
)
2313 ret
= g_plugin_entries
[i
].checkfunc(node
, args
[2]);
2320 check_free(file
, grub_file_close
);
2321 check_free(json
, vtoy_json_destroy
);
2322 grub_check_free(buf
);