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_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
48 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
53 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
57 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
60 if (ventoy_is_file_exist(filepath
) == 0)
62 debug("Theme file %s does not exist\n", filepath
);
66 debug("vtoy_theme %s\n", filepath
);
67 grub_env_set("vtoy_theme", filepath
);
70 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
73 debug("vtoy_gfxmode %s\n", value
);
74 grub_env_set("vtoy_gfxmode", value
);
81 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
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
;
91 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
93 debug("Not array %d\n", json
->enDataType
);
97 if (g_install_template_head
)
99 for (node
= g_install_template_head
; node
; node
= next
)
105 g_install_template_head
= NULL
;
108 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
110 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
111 if (iso
&& iso
[0] == '/')
113 script
= vtoy_json_get_string_ex(pNode
->pstChild
, "template");
114 if (script
&& script
[0] == '/')
116 node
= grub_zalloc(sizeof(install_template
));
119 grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
120 grub_snprintf(node
->templatepath
, sizeof(node
->templatepath
), "%s", script
);
122 if (g_install_template_head
)
124 node
->next
= g_install_template_head
;
127 g_install_template_head
= node
;
137 static plugin_entry g_plugin_entries
[] =
139 { "theme", ventoy_plugin_theme_entry
},
140 { "auto_install", ventoy_plugin_auto_install_entry
},
143 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
146 VTOY_JSON
*cur
= json
;
150 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
152 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
154 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
155 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
166 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
171 VTOY_JSON
*json
= NULL
;
176 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
179 return GRUB_ERR_NONE
;
182 debug("json configuration file size %d\n", (int)file
->size
);
184 buf
= grub_malloc(file
->size
+ 1);
187 grub_file_close(file
);
192 grub_file_read(file
, buf
, file
->size
);
193 grub_file_close(file
);
195 json
= vtoy_json_create();
203 ret
= vtoy_json_parse(json
, buf
);
206 debug("Failed to parse json string %d\n", ret
);
211 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
213 vtoy_json_destroy(json
);
217 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
221 void ventoy_plugin_dump_auto_install(void)
223 install_template
*node
= NULL
;
225 for (node
= g_install_template_head
; node
; node
= node
->next
)
227 grub_printf("IMAGE:<%s>\n", node
->isopath
);
228 grub_printf("SCRIPT:<%s>\n\n", node
->templatepath
);
235 char * ventoy_plugin_get_install_template(const char *isopath
)
237 install_template
*node
= NULL
;
239 for (node
= g_install_template_head
; node
; node
= node
->next
)
241 if (grub_strcmp(node
->isopath
, isopath
) == 0)
243 return node
->templatepath
;