]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/grub-2.04/grub-core/ventoy/ventoy_plugin.c
1.0.07 release
[Ventoy.git] / GRUB2 / grub-2.04 / grub-core / ventoy / ventoy_plugin.c
1 /******************************************************************************
2 * ventoy_plugin.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20 #include <grub/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.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>
34 #include <grub/net.h>
35 #include <grub/time.h>
36 #include <grub/ventoy.h>
37 #include "ventoy_def.h"
38
39 GRUB_MOD_LICENSE ("GPLv3+");
40
41 static int ventoy_plugin_theme_entry(VTOY_JSON *json, const char *isodisk)
42 {
43 const char *value;
44 char filepath[256];
45
46 value = vtoy_json_get_string_ex(json->pstChild, "file");
47 if (value)
48 {
49 grub_snprintf(filepath, sizeof(filepath), "%s/ventoy/%s", isodisk, value);
50 if (ventoy_is_file_exist(filepath) == 0)
51 {
52 debug("Theme file %s does not exist\n", filepath);
53 return 0;
54 }
55
56 debug("vtoy_theme %s\n", filepath);
57 grub_env_set("vtoy_theme", filepath);
58 }
59
60 value = vtoy_json_get_string_ex(json->pstChild, "gfxmode");
61 if (value)
62 {
63 debug("vtoy_gfxmode %s\n", value);
64 grub_env_set("vtoy_gfxmode", value);
65 }
66
67 return 0;
68 }
69
70 static plugin_entry g_plugin_entries[] =
71 {
72 { "theme", ventoy_plugin_theme_entry },
73 };
74
75 static int ventoy_parse_plugin_config(VTOY_JSON *json, const char *isodisk)
76 {
77 int i;
78 VTOY_JSON *cur = json;
79
80 while (cur)
81 {
82 for (i = 0; i < (int)ARRAY_SIZE(g_plugin_entries); i++)
83 {
84 if (grub_strcmp(g_plugin_entries[i].key, cur->pcName) == 0)
85 {
86 debug("Plugin entry for %s\n", g_plugin_entries[i].key);
87 g_plugin_entries[i].entryfunc(cur, isodisk);
88 break;
89 }
90 }
91
92 cur = cur->pstNext;
93 }
94
95 return 0;
96 }
97
98 grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **args)
99 {
100 int ret = 0;
101 char *buf = NULL;
102 grub_file_t file;
103 VTOY_JSON *json = NULL;
104
105 (void)ctxt;
106 (void)argc;
107
108 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s/ventoy/ventoy.json", args[0]);
109 if (!file)
110 {
111 return GRUB_ERR_NONE;
112 }
113
114 debug("json configuration file size %d\n", (int)file->size);
115
116 buf = grub_malloc(file->size + 1);
117 if (!buf)
118 {
119 grub_file_close(file);
120 return 1;
121 }
122
123 buf[file->size] = 0;
124 grub_file_read(file, buf, file->size);
125 grub_file_close(file);
126
127 json = vtoy_json_create();
128 if (!json)
129 {
130 return 1;
131 }
132
133
134
135 ret = vtoy_json_parse(json, buf);
136 if (ret)
137 {
138 debug("Failed to parse json string %d\n", ret);
139 grub_free(buf);
140 return 1;
141 }
142
143 ventoy_parse_plugin_config(json->pstChild, args[0]);
144
145 vtoy_json_destroy(json);
146
147 grub_free(buf);
148
149 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
150 }
151