]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_plugin.c
82de5130cda2f6b1c38ce2b0b2627bb8dc6463ae
[Ventoy.git] / GRUB2 / MOD_SRC / 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 install_template *g_install_template_head = NULL;
42
43 static int ventoy_plugin_control_entry(VTOY_JSON *json, const char *isodisk)
44 {
45 VTOY_JSON *pNode = NULL;
46 VTOY_JSON *pChild = NULL;
47
48 (void)isodisk;
49
50 if (json->enDataType != JSON_TYPE_ARRAY)
51 {
52 debug("Not array %d\n", json->enDataType);
53 return 0;
54 }
55
56 for (pNode = json->pstChild; pNode; pNode = pNode->pstNext)
57 {
58 if (pNode->enDataType == JSON_TYPE_OBJECT)
59 {
60 pChild = pNode->pstChild;
61 if (pChild->enDataType == JSON_TYPE_STRING && pChild->pcName && pChild->unData.pcStrVal)
62 {
63 ventoy_set_env(pChild->pcName, pChild->unData.pcStrVal);
64 }
65 }
66 }
67
68 return 0;
69 }
70
71 static int ventoy_plugin_theme_entry(VTOY_JSON *json, const char *isodisk)
72 {
73 const char *value;
74 char filepath[256];
75
76 value = vtoy_json_get_string_ex(json->pstChild, "file");
77 if (value)
78 {
79 if (value[0] == '/')
80 {
81 grub_snprintf(filepath, sizeof(filepath), "%s%s", isodisk, value);
82 }
83 else
84 {
85 grub_snprintf(filepath, sizeof(filepath), "%s/ventoy/%s", isodisk, value);
86 }
87
88 if (ventoy_is_file_exist(filepath) == 0)
89 {
90 debug("Theme file %s does not exist\n", filepath);
91 return 0;
92 }
93
94 debug("vtoy_theme %s\n", filepath);
95 grub_env_set("vtoy_theme", filepath);
96 }
97
98 value = vtoy_json_get_string_ex(json->pstChild, "gfxmode");
99 if (value)
100 {
101 debug("vtoy_gfxmode %s\n", value);
102 grub_env_set("vtoy_gfxmode", value);
103 }
104
105 return 0;
106 }
107
108
109 static int ventoy_plugin_auto_install_entry(VTOY_JSON *json, const char *isodisk)
110 {
111 const char *iso = NULL;
112 const char *script = NULL;
113 VTOY_JSON *pNode = NULL;
114 install_template *node = NULL;
115 install_template *next = NULL;
116
117 (void)isodisk;
118
119 if (json->enDataType != JSON_TYPE_ARRAY)
120 {
121 debug("Not array %d\n", json->enDataType);
122 return 0;
123 }
124
125 if (g_install_template_head)
126 {
127 for (node = g_install_template_head; node; node = next)
128 {
129 next = node->next;
130 grub_free(node);
131 }
132
133 g_install_template_head = NULL;
134 }
135
136 for (pNode = json->pstChild; pNode; pNode = pNode->pstNext)
137 {
138 iso = vtoy_json_get_string_ex(pNode->pstChild, "image");
139 if (iso && iso[0] == '/')
140 {
141 script = vtoy_json_get_string_ex(pNode->pstChild, "template");
142 if (script && script[0] == '/')
143 {
144 node = grub_zalloc(sizeof(install_template));
145 if (node)
146 {
147 grub_snprintf(node->isopath, sizeof(node->isopath), "%s", iso);
148 grub_snprintf(node->templatepath, sizeof(node->templatepath), "%s", script);
149
150 if (g_install_template_head)
151 {
152 node->next = g_install_template_head;
153 }
154
155 g_install_template_head = node;
156 }
157 }
158 }
159 }
160
161 return 0;
162 }
163
164
165 static plugin_entry g_plugin_entries[] =
166 {
167 { "control", ventoy_plugin_control_entry },
168 { "theme", ventoy_plugin_theme_entry },
169 { "auto_install", ventoy_plugin_auto_install_entry },
170 };
171
172 static int ventoy_parse_plugin_config(VTOY_JSON *json, const char *isodisk)
173 {
174 int i;
175 VTOY_JSON *cur = json;
176
177 while (cur)
178 {
179 for (i = 0; i < (int)ARRAY_SIZE(g_plugin_entries); i++)
180 {
181 if (grub_strcmp(g_plugin_entries[i].key, cur->pcName) == 0)
182 {
183 debug("Plugin entry for %s\n", g_plugin_entries[i].key);
184 g_plugin_entries[i].entryfunc(cur, isodisk);
185 break;
186 }
187 }
188
189 cur = cur->pstNext;
190 }
191
192 return 0;
193 }
194
195 grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **args)
196 {
197 int ret = 0;
198 char *buf = NULL;
199 grub_file_t file;
200 VTOY_JSON *json = NULL;
201
202 (void)ctxt;
203 (void)argc;
204
205 file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s/ventoy/ventoy.json", args[0]);
206 if (!file)
207 {
208 return GRUB_ERR_NONE;
209 }
210
211 debug("json configuration file size %d\n", (int)file->size);
212
213 buf = grub_malloc(file->size + 1);
214 if (!buf)
215 {
216 grub_file_close(file);
217 return 1;
218 }
219
220 buf[file->size] = 0;
221 grub_file_read(file, buf, file->size);
222 grub_file_close(file);
223
224 json = vtoy_json_create();
225 if (!json)
226 {
227 return 1;
228 }
229
230
231
232 ret = vtoy_json_parse(json, buf);
233 if (ret)
234 {
235 debug("Failed to parse json string %d\n", ret);
236 grub_free(buf);
237 return 1;
238 }
239
240 ventoy_parse_plugin_config(json->pstChild, args[0]);
241
242 vtoy_json_destroy(json);
243
244 grub_free(buf);
245
246 VENTOY_CMD_RETURN(GRUB_ERR_NONE);
247 }
248
249
250 void ventoy_plugin_dump_auto_install(void)
251 {
252 install_template *node = NULL;
253
254 for (node = g_install_template_head; node; node = node->next)
255 {
256 grub_printf("IMAGE:<%s>\n", node->isopath);
257 grub_printf("SCRIPT:<%s>\n\n", node->templatepath);
258 }
259
260 return;
261 }
262
263
264 char * ventoy_plugin_get_install_template(const char *isopath)
265 {
266 install_template *node = NULL;
267
268 for (node = g_install_template_head; node; node = node->next)
269 {
270 if (grub_strcmp(node->isopath, isopath) == 0)
271 {
272 return node->templatepath;
273 }
274 }
275
276 return NULL;
277 }
278