#include <grub/datetime.h>
#include <grub/i18n.h>
#include <grub/net.h>
+#ifdef GRUB_MACHINE_EFI
+#include <grub/efi/efi.h>
+#endif
#include <grub/time.h>
#include <grub/ventoy.h>
#include "ventoy_def.h"
cpio_newc_header *g_ventoy_initrd_head = NULL;
grub_uint8_t *g_ventoy_runtime_buf = NULL;
-ventoy_grub_param g_grub_param;
+ventoy_grub_param *g_grub_param = NULL;
ventoy_guid g_ventoy_guid = VENTOY_GUID;
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
+static grub_err_t ventoy_cmd_file_size(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int rc = 1;
+ char buf[32];
+ grub_file_t file;
+
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ if (argc != 2)
+ {
+ return rc;
+ }
+
+ file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
+ if (file == NULL)
+ {
+ debug("failed to open file <%s> for udf check\n", args[0]);
+ return 1;
+ }
+
+ grub_snprintf(buf, sizeof(buf), "%llu", (unsigned long long)file->size);
+
+ grub_env_set(args[1], buf);
+
+ grub_file_close(file);
+ rc = 0;
+
+ return rc;
+}
+
+static grub_err_t ventoy_cmd_load_iso_to_mem(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int rc = 1;
+ char name[32];
+ char value[32];
+ char *buf = NULL;
+ grub_file_t file;
+
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ if (argc != 2)
+ {
+ return rc;
+ }
+
+ file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
+ if (file == NULL)
+ {
+ debug("failed to open file <%s> for udf check\n", args[0]);
+ return 1;
+ }
+
+#ifdef GRUB_MACHINE_EFI
+ buf = (char *)grub_efi_allocate_iso_buf(file->size);
+#else
+ buf = (char *)grub_malloc(file->size);
+#endif
+
+ grub_file_read(file, buf, file->size);
+
+ grub_snprintf(name, sizeof(name), "%s_addr", args[1]);
+ grub_snprintf(value, sizeof(value), "0x%llx", (unsigned long long)(unsigned long)buf);
+ grub_env_set(name, value);
+
+ grub_snprintf(name, sizeof(name), "%s_size", args[1]);
+ grub_snprintf(value, sizeof(value), "%llu", (unsigned long long)file->size);
+ grub_env_set(name, value);
+
+ grub_file_close(file);
+ rc = 0;
+
+ return rc;
+}
+
static grub_err_t ventoy_cmd_is_udf(grub_extcmd_context_t ctxt, int argc, char **args)
{
int i;
grub_file_close(file);
+ grub_memset(&g_grub_param->file_replace, 0, sizeof(g_grub_param->file_replace));
+
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
+static grub_err_t ventoy_cmd_add_replace_file(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int i;
+ ventoy_grub_param_file_replace *replace = NULL;
+
+ (void)ctxt;
+ (void)argc;
+ (void)args;
+
+ if (argc >= 2)
+ {
+ replace = &(g_grub_param->file_replace);
+ replace->magic = GRUB_FILE_REPLACE_MAGIC;
+
+ replace->old_name_cnt = 0;
+ for (i = 0; i < 4 && i + 1 < argc; i++)
+ {
+ replace->old_name_cnt++;
+ grub_snprintf(replace->old_file_name[i], sizeof(replace->old_file_name[i]), "%s", args[i + 1]);
+ }
+
+ replace->new_file_virtual_id = (grub_uint32_t)grub_strtoul(args[0], NULL, 10);
+ }
+
+ VENTOY_CMD_RETURN(GRUB_ERR_NONE);
+}
+
grub_file_t ventoy_grub_file_open(enum grub_file_type type, const char *fmt, ...)
{
va_list ap;
grub_env_set("vtdebug_flag", "");
ventoy_filt_register(0, ventoy_wrapper_open);
-
- g_grub_param.grub_env_get = grub_env_get;
- grub_snprintf(buf, sizeof(buf), "%p", &g_grub_param);
- grub_env_set("env_param", buf);
+
+ g_grub_param = (ventoy_grub_param *)grub_zalloc(sizeof(ventoy_grub_param));
+ if (g_grub_param)
+ {
+ g_grub_param->grub_env_get = grub_env_get;
+ grub_snprintf(buf, sizeof(buf), "%p", g_grub_param);
+ grub_env_set("env_param", buf);
+ }
return 0;
}
{ "vt_load_cpio", ventoy_cmd_load_cpio, 0, NULL, "", "", NULL },
{ "vt_is_udf", ventoy_cmd_is_udf, 0, NULL, "", "", NULL },
+ { "vt_file_size", ventoy_cmd_file_size, 0, NULL, "", "", NULL },
+ { "vt_load_iso_to_mem", ventoy_cmd_load_iso_to_mem, 0, NULL, "", "", NULL },
{ "vt_linux_parse_initrd_isolinux", ventoy_cmd_isolinux_initrd_collect, 0, NULL, "{cfgfile}", "", NULL },
{ "vt_linux_parse_initrd_grub", ventoy_cmd_grub_initrd_collect, 0, NULL, "{cfgfile}", "", NULL },
{ "vt_linux_clear_initrd", ventoy_cmd_clear_initrd_list, 0, NULL, "", "", NULL },
{ "vt_linux_dump_initrd", ventoy_cmd_dump_initrd_list, 0, NULL, "", "", NULL },
{ "vt_linux_initrd_count", ventoy_cmd_initrd_count, 0, NULL, "", "", NULL },
+ { "vt_linux_valid_initrd_count", ventoy_cmd_valid_initrd_count, 0, NULL, "", "", NULL },
{ "vt_linux_locate_initrd", ventoy_cmd_linux_locate_initrd, 0, NULL, "", "", NULL },
{ "vt_linux_chain_data", ventoy_cmd_linux_chain_data, 0, NULL, "", "", NULL },
{ "vt_windows_reset", ventoy_cmd_wimdows_reset, 0, NULL, "", "", NULL },
{ "vt_windows_locate_wim", ventoy_cmd_wimdows_locate_wim, 0, NULL, "", "", NULL },
{ "vt_windows_chain_data", ventoy_cmd_windows_chain_data, 0, NULL, "", "", NULL },
+
+ { "vt_add_replace_file", ventoy_cmd_add_replace_file, 0, NULL, "", "", NULL },
+
{ "vt_load_plugin", ventoy_cmd_load_plugin, 0, NULL, "", "", NULL },
};