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