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 char g_iso_disk_name
[128];
42 static install_template
*g_install_template_head
= NULL
;
43 static persistence_config
*g_persistence_head
= NULL
;
45 static int ventoy_plugin_control_entry(VTOY_JSON
*json
, const char *isodisk
)
47 VTOY_JSON
*pNode
= NULL
;
48 VTOY_JSON
*pChild
= NULL
;
52 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
54 debug("Not array %d\n", json
->enDataType
);
58 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
60 if (pNode
->enDataType
== JSON_TYPE_OBJECT
)
62 pChild
= pNode
->pstChild
;
63 if (pChild
->enDataType
== JSON_TYPE_STRING
&& pChild
->pcName
&& pChild
->unData
.pcStrVal
)
65 ventoy_set_env(pChild
->pcName
, pChild
->unData
.pcStrVal
);
73 static int ventoy_plugin_theme_entry(VTOY_JSON
*json
, const char *isodisk
)
78 value
= vtoy_json_get_string_ex(json
->pstChild
, "file");
83 grub_snprintf(filepath
, sizeof(filepath
), "%s%s", isodisk
, value
);
87 grub_snprintf(filepath
, sizeof(filepath
), "%s/ventoy/%s", isodisk
, value
);
90 if (ventoy_is_file_exist(filepath
) == 0)
92 debug("Theme file %s does not exist\n", filepath
);
96 debug("vtoy_theme %s\n", filepath
);
97 grub_env_set("vtoy_theme", filepath
);
100 value
= vtoy_json_get_string_ex(json
->pstChild
, "gfxmode");
103 debug("vtoy_gfxmode %s\n", value
);
104 grub_env_set("vtoy_gfxmode", value
);
107 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_left");
110 grub_env_set("VTLE_LFT", value
);
113 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_top");
116 grub_env_set("VTLE_TOP", value
);
119 value
= vtoy_json_get_string_ex(json
->pstChild
, "ventoy_color");
122 grub_env_set("VTLE_CLR", value
);
129 static int ventoy_plugin_auto_install_entry(VTOY_JSON
*json
, const char *isodisk
)
131 const char *iso
= NULL
;
132 const char *script
= NULL
;
133 VTOY_JSON
*pNode
= NULL
;
134 install_template
*node
= NULL
;
135 install_template
*next
= NULL
;
139 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
141 debug("Not array %d\n", json
->enDataType
);
145 if (g_install_template_head
)
147 for (node
= g_install_template_head
; node
; node
= next
)
153 g_install_template_head
= NULL
;
156 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
158 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
159 if (iso
&& iso
[0] == '/')
161 script
= vtoy_json_get_string_ex(pNode
->pstChild
, "template");
162 if (script
&& script
[0] == '/')
164 node
= grub_zalloc(sizeof(install_template
));
167 grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
168 grub_snprintf(node
->templatepath
, sizeof(node
->templatepath
), "%s", script
);
170 if (g_install_template_head
)
172 node
->next
= g_install_template_head
;
175 g_install_template_head
= node
;
185 static int ventoy_plugin_persistence_entry(VTOY_JSON
*json
, const char *isodisk
)
187 const char *iso
= NULL
;
188 const char *persist
= NULL
;
189 VTOY_JSON
*pNode
= NULL
;
190 persistence_config
*node
= NULL
;
191 persistence_config
*next
= NULL
;
195 if (json
->enDataType
!= JSON_TYPE_ARRAY
)
197 debug("Not array %d\n", json
->enDataType
);
201 if (g_persistence_head
)
203 for (node
= g_persistence_head
; node
; node
= next
)
209 g_persistence_head
= NULL
;
212 for (pNode
= json
->pstChild
; pNode
; pNode
= pNode
->pstNext
)
214 iso
= vtoy_json_get_string_ex(pNode
->pstChild
, "image");
215 if (iso
&& iso
[0] == '/')
217 persist
= vtoy_json_get_string_ex(pNode
->pstChild
, "backend");
218 if (persist
&& persist
[0] == '/')
220 node
= grub_zalloc(sizeof(persistence_config
));
223 grub_snprintf(node
->isopath
, sizeof(node
->isopath
), "%s", iso
);
224 grub_snprintf(node
->filepath
, sizeof(node
->filepath
), "%s", persist
);
226 if (g_persistence_head
)
228 node
->next
= g_persistence_head
;
231 g_persistence_head
= node
;
241 static plugin_entry g_plugin_entries
[] =
243 { "control", ventoy_plugin_control_entry
},
244 { "theme", ventoy_plugin_theme_entry
},
245 { "auto_install", ventoy_plugin_auto_install_entry
},
246 { "persistence", ventoy_plugin_persistence_entry
},
249 static int ventoy_parse_plugin_config(VTOY_JSON
*json
, const char *isodisk
)
252 VTOY_JSON
*cur
= json
;
254 grub_snprintf(g_iso_disk_name
, sizeof(g_iso_disk_name
), "%s", isodisk
);
258 for (i
= 0; i
< (int)ARRAY_SIZE(g_plugin_entries
); i
++)
260 if (grub_strcmp(g_plugin_entries
[i
].key
, cur
->pcName
) == 0)
262 debug("Plugin entry for %s\n", g_plugin_entries
[i
].key
);
263 g_plugin_entries
[i
].entryfunc(cur
, isodisk
);
274 grub_err_t
ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt
, int argc
, char **args
)
279 VTOY_JSON
*json
= NULL
;
284 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s/ventoy/ventoy.json", args
[0]);
287 return GRUB_ERR_NONE
;
290 debug("json configuration file size %d\n", (int)file
->size
);
292 buf
= grub_malloc(file
->size
+ 1);
295 grub_file_close(file
);
300 grub_file_read(file
, buf
, file
->size
);
301 grub_file_close(file
);
303 json
= vtoy_json_create();
311 ret
= vtoy_json_parse(json
, buf
);
314 debug("Failed to parse json string %d\n", ret
);
319 ventoy_parse_plugin_config(json
->pstChild
, args
[0]);
321 vtoy_json_destroy(json
);
325 VENTOY_CMD_RETURN(GRUB_ERR_NONE
);
329 void ventoy_plugin_dump_auto_install(void)
331 install_template
*node
= NULL
;
333 for (node
= g_install_template_head
; node
; node
= node
->next
)
335 grub_printf("IMAGE:<%s>\n", node
->isopath
);
336 grub_printf("SCRIPT:<%s>\n\n", node
->templatepath
);
342 void ventoy_plugin_dump_persistence(void)
345 persistence_config
*node
= NULL
;
346 ventoy_img_chunk_list chunk_list
;
348 for (node
= g_persistence_head
; node
; node
= node
->next
)
350 grub_printf("IMAGE:<%s>\n", node
->isopath
);
351 grub_printf("PERSIST:<%s>", node
->filepath
);
353 rc
= ventoy_plugin_get_persistent_chunklist(node
->isopath
, &chunk_list
);
356 grub_printf(" [ SUCCESS ]\n\n");
357 grub_free(chunk_list
.chunk
);
361 grub_printf(" [ FAILED ]\n\n");
369 char * ventoy_plugin_get_install_template(const char *isopath
)
371 install_template
*node
= NULL
;
373 for (node
= g_install_template_head
; node
; node
= node
->next
)
375 if (grub_strcmp(node
->isopath
, isopath
) == 0)
377 return node
->templatepath
;
384 int ventoy_plugin_get_persistent_chunklist(const char *isopath
, ventoy_img_chunk_list
*chunk_list
)
387 grub_uint64_t start
= 0;
388 grub_file_t file
= NULL
;
389 persistence_config
*node
= NULL
;
391 for (node
= g_persistence_head
; node
; node
= node
->next
)
393 if (grub_strcmp(node
->isopath
, isopath
) == 0)
404 file
= ventoy_grub_file_open(VENTOY_FILE_TYPE
, "%s%s", g_iso_disk_name
, node
->filepath
);
407 debug("Failed to open file %s%s\n", g_iso_disk_name
, node
->filepath
);
411 grub_memset(chunk_list
, 0, sizeof(ventoy_img_chunk_list
));
412 chunk_list
->chunk
= grub_malloc(sizeof(ventoy_img_chunk
) * DEFAULT_CHUNK_NUM
);
413 if (NULL
== chunk_list
->chunk
)
418 chunk_list
->max_chunk
= DEFAULT_CHUNK_NUM
;
419 chunk_list
->cur_chunk
= 0;
421 start
= file
->device
->disk
->partition
->start
;
422 ventoy_get_block_list(file
, chunk_list
, start
);
424 if (0 != ventoy_check_block_list(file
, chunk_list
, start
))
426 grub_free(chunk_list
->chunk
);
427 chunk_list
->chunk
= NULL
;
435 grub_file_close(file
);