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/time.h>
36 #include <grub/ventoy.h>
37 #include "ventoy_def.h"
39 GRUB_MOD_LICENSE ("GPLv3+");
41 static install_template
*g_install_template_head
= NULL
;
43 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
45 VTOY_JSON
*pNode
= NULL
;
46 VTOY_JSON
*pChild
= NULL
;
50 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
52 debug("Not array %d\n", json
->enDataType
);
56 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
58 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
60 pChild
= pNode
->pstChild
;
61 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
63 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
71 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
76 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
81 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
85 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
88 if (ventoy_is_file_exist(filepath
) == 0)
90 debug("Theme file %s does not exist\n", filepath
);
94 debug("vtoy_theme %s\n", filepath
);
95 grub_env_set("vtoy_theme", filepath
);
98 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
101 debug("vtoy_gfxmode %s\n", value
);
102 grub_env_set("vtoy_gfxmode", value
);
109 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
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
;
119 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
121 debug("Not array %d\n", json
->enDataType
);
125 if (g_install_template_head
)
127 for (node
= g_install_template_head
; node
; node
= next
)
133 g_install_template_head
= NULL
;
136 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
138 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
139 if (iso
&& iso
[0] == '/')
141 script
= vtoy_json_get_string_ex(pNode
->pstChild
, "template");
142 if (script
&& script
[0] == '/')
144 node
= grub_zalloc(sizeof(install_template
));
147 grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
148 grub_snprintf(node
->templatepath
, sizeof(node
->templatepath
), "%s", script
);
150 if (g_install_template_head
)
152 node
->next
= g_install_template_head
;
155 g_install_template_head
= node
;
165 static plugin_entry g_plugin_entries
[] =
167 { "control", ventoy_plugin_control_entry
},
168 { "theme", ventoy_plugin_theme_entry
},
169 { "auto_install", ventoy_plugin_auto_install_entry
},
172 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
175 VTOY_JSON
*cur
= json
;
179 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
181 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
183 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
184 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
195 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
200 VTOY_JSON
*json
= NULL
;
205 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
208 return GRUB_ERR_NONE
;
211 debug("json configuration file size %d\n", (int)file
->size
);
213 buf
= grub_malloc(file
->size
+ 1);
216 grub_file_close(file
);
221 grub_file_read(file
, buf
, file
->size
);
222 grub_file_close(file
);
224 json
= vtoy_json_create();
232 ret
= vtoy_json_parse(json
, buf
);
235 debug("Failed to parse json string %d\n", ret
);
240 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
242 vtoy_json_destroy(json
);
246 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
250 void ventoy_plugin_dump_auto_install(void)
252 install_template
*node
= NULL
;
254 for (node
= g_install_template_head
; node
; node
= node
->next
)
256 grub_printf("IMAGE:<%s>\n", node
->isopath
);
257 grub_printf("SCRIPT:<%s>\n\n", node
->templatepath
);
264 char * ventoy_plugin_get_install_template(const char *isopath
)
266 install_template
*node
= NULL
;
268 for (node
= g_install_template_head
; node
; node
= node
->next
)
270 if (grub_strcmp(node
->isopath
, isopath
) == 0)
272 return node
->templatepath
;