From 1ab1799b72c374e71332060cae2cc9823d753ae8 Mon Sep 17 00:00:00 2001 From: longpanda Date: Sat, 22 Oct 2022 16:13:03 +0800 Subject: [PATCH] Code Optimization --- .../grub-2.04/grub-core/ventoy/ventoy.c | 15 ++++++++ .../grub-2.04/grub-core/ventoy/ventoy_cmd.c | 30 ++-------------- .../grub-2.04/grub-core/ventoy/ventoy_linux.c | 34 +++---------------- .../grub-2.04/grub-core/ventoy/ventoy_unix.c | 6 +--- .../grub-2.04/grub-core/ventoy/ventoy_vhd.c | 17 ++-------- .../grub-core/ventoy/ventoy_windows.c | 27 +++------------ GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h | 1 + 7 files changed, 32 insertions(+), 98 deletions(-) diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c index b3cdcbcd..8d4e5fda 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c @@ -188,6 +188,21 @@ void * ventoy_alloc_chain(grub_size_t size) return p; } +void ventoy_memfile_env_set(const char *prefix, const void *buf, unsigned long long len) +{ + char name[128]; + char val[64]; + + grub_snprintf(name, sizeof(name), "%s_addr", prefix); + grub_snprintf(val, sizeof(val), "0x%llx", (ulonglong)(ulong)buf); + grub_env_set(name, val); + + grub_snprintf(name, sizeof(name), "%s_size", prefix); + grub_snprintf(val, sizeof(val), "%llu", len); + grub_env_set(name, val); + + return; +} static int ventoy_arch_mode_init(void) { diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c index 3f7b4a01..935ad7a0 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c @@ -1036,8 +1036,6 @@ static grub_err_t ventoy_cmd_concat_efi_iso(grub_extcmd_context_t ctxt, int argc int totlen = 0; int offset = 0; grub_file_t file; - char name[32]; - char value[32]; char *buf = NULL; char *data = NULL; ventoy_iso9660_override *dirent; @@ -1097,13 +1095,7 @@ static grub_err_t ventoy_cmd_concat_efi_iso(grub_extcmd_context_t ctxt, int argc grub_file_read(file, data + sizeof(ventoy_chain_head) + len, file->size); grub_file_close(file); - grub_snprintf(name, sizeof(name), "%s_addr", args[1]); - grub_snprintf(value, sizeof(value), "0x%llx", (ulonglong)(ulong)data); - grub_env_set(name, value); - - grub_snprintf(name, sizeof(name), "%s_size", args[1]); - grub_snprintf(value, sizeof(value), "%d", (int)(totlen)); - grub_env_set(name, value); + ventoy_memfile_env_set(args[1], data, (ulonglong)totlen); return 0; } @@ -1170,8 +1162,6 @@ grub_ssize_t ventoy_load_file_with_prompt(grub_file_t file, void *buf, grub_ssiz static grub_err_t ventoy_cmd_load_file_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; enum grub_file_type type; @@ -1222,13 +1212,7 @@ static grub_err_t ventoy_cmd_load_file_to_mem(grub_extcmd_context_t ctxt, int ar grub_file_read(file, buf, file->size); } - grub_snprintf(name, sizeof(name), "%s_addr", args[2]); - 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[2]); - grub_snprintf(value, sizeof(value), "%llu", (unsigned long long)file->size); - grub_env_set(name, value); + ventoy_memfile_env_set(args[2], buf, (ulonglong)(file->size)); grub_file_close(file); rc = 0; @@ -1240,8 +1224,6 @@ static grub_err_t ventoy_cmd_load_img_memdisk(grub_extcmd_context_t ctxt, int ar { int rc = 1; int headlen; - char name[32]; - char value[32]; char *buf = NULL; grub_file_t file; @@ -1273,13 +1255,7 @@ static grub_err_t ventoy_cmd_load_img_memdisk(grub_extcmd_context_t ctxt, int ar grub_file_read(file, buf + headlen, 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); + ventoy_memfile_env_set(args[1], buf, (ulonglong)(file->size)); grub_file_close(file); rc = 0; diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c index c70ba9a2..47803de0 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c @@ -1572,7 +1572,6 @@ grub_err_t ventoy_cmd_trailer_cpio(grub_extcmd_context_t ctxt, int argc, char ** grub_uint8_t *bufend; cpio_newc_header *head; grub_file_t file; - char value[64]; const grub_uint8_t trailler[124] = { 0x30, 0x37, 0x30, 0x37, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, @@ -1643,11 +1642,8 @@ grub_err_t ventoy_cmd_trailer_cpio(grub_extcmd_context_t ctxt, int argc, char ** name = (char *)(head + 1); } } - - grub_snprintf(value, sizeof(value), "0x%llx", (ulonglong)(ulong)g_ventoy_cpio_buf); - ventoy_set_env("ventoy_cpio_addr", value); - grub_snprintf(value, sizeof(value), "%d", bufsize); - ventoy_set_env("ventoy_cpio_size", value); + + ventoy_memfile_env_set("ventoy_cpio", g_ventoy_cpio_buf, (ulonglong)bufsize); VENTOY_CMD_RETURN(GRUB_ERR_NONE); } @@ -1669,7 +1665,6 @@ grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, cha const char *pLastChain = NULL; const char *compatible; ventoy_chain_head *chain; - char envbuf[64]; (void)ctxt; (void)argc; @@ -1757,10 +1752,7 @@ grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, cha return 1; } - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain); - grub_env_set("vtoy_chain_mem_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%u", size); - grub_env_set("vtoy_chain_mem_size", envbuf); + ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size); grub_memset(chain, 0, sizeof(ventoy_chain_head)); @@ -1921,8 +1913,6 @@ out: grub_err_t ventoy_cmd_linux_systemd_menu(grub_extcmd_context_t ctxt, int argc, char **args) { static char *buf = NULL; - char name[128]; - char value[64]; grub_fs_t fs; char *device_name = NULL; grub_device_t dev = NULL; @@ -1967,13 +1957,7 @@ grub_err_t ventoy_cmd_linux_systemd_menu(grub_extcmd_context_t ctxt, int argc, c ctx.len = VTOY_LINUX_SYSTEMD_MENU_MAX_BUF; fs->fs_dir(dev, "/loader/entries", ventoy_systemd_conf_hook, &ctx); - grub_snprintf(name, sizeof(name), "%s_addr", args[1]); - grub_snprintf(value, sizeof(value), "0x%llx", (ulonglong)(ulong)buf); - grub_env_set(name, value); - - grub_snprintf(name, sizeof(name), "%s_size", args[1]); - grub_snprintf(value, sizeof(value), "%d", ctx.pos); - grub_env_set(name, value); + ventoy_memfile_env_set(args[1], buf, (ulonglong)(ctx.pos)); end: grub_check_free(device_name); @@ -2011,8 +1995,6 @@ grub_err_t ventoy_cmd_linux_limine_menu(grub_extcmd_context_t ctxt, int argc, ch char *start = NULL; char *nextline = NULL; grub_file_t file = NULL; - char name[128]; - char value[64]; char *title = NULL; char *kernel = NULL; char *initrd = NULL; @@ -2120,13 +2102,7 @@ grub_err_t ventoy_cmd_linux_limine_menu(grub_extcmd_context_t ctxt, int argc, ch sub = 0; } - grub_snprintf(name, sizeof(name), "%s_addr", args[1]); - grub_snprintf(value, sizeof(value), "0x%llx", (ulonglong)(ulong)buf); - grub_env_set(name, value); - - grub_snprintf(name, sizeof(name), "%s_size", args[1]); - grub_snprintf(value, sizeof(value), "%d", pos); - grub_env_set(name, value); + ventoy_memfile_env_set(args[1], buf, (ulonglong)pos); end: grub_check_free(filebuf); diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_unix.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_unix.c index 708d6f22..3c6086e6 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_unix.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_unix.c @@ -1118,7 +1118,6 @@ grub_err_t ventoy_cmd_unix_chain_data(grub_extcmd_context_t ctxt, int argc, char const char *pLastChain = NULL; const char *compatible; ventoy_chain_head *chain; - char envbuf[64]; (void)ctxt; (void)argc; @@ -1197,10 +1196,7 @@ grub_err_t ventoy_cmd_unix_chain_data(grub_extcmd_context_t ctxt, int argc, char return 1; } - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain); - grub_env_set("vtoy_chain_mem_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%u", size); - grub_env_set("vtoy_chain_mem_size", envbuf); + ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size); grub_memset(chain, 0, sizeof(ventoy_chain_head)); diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c index 7aa3e8f0..84468704 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c @@ -305,7 +305,6 @@ grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char * int patchoffset[2]; ventoy_patch_vhd *patch1; ventoy_patch_vhd *patch2; - char envbuf[64]; (void)ctxt; (void)argc; @@ -356,15 +355,9 @@ grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char * /* set buffer and size */ #ifdef GRUB_MACHINE_EFI - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (ulong)g_vhdboot_totbuf); - grub_env_set("vtoy_vhd_buf_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%d", (int)(g_vhdboot_isolen + sizeof(ventoy_chain_head))); - grub_env_set("vtoy_vhd_buf_size", envbuf); + ventoy_memfile_env_set("vtoy_vhd_buf", g_vhdboot_totbuf, (ulonglong)(g_vhdboot_isolen + sizeof(ventoy_chain_head))); #else - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (ulong)g_vhdboot_isobuf); - grub_env_set("vtoy_vhd_buf_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%d", g_vhdboot_isolen); - grub_env_set("vtoy_vhd_buf_size", envbuf); + ventoy_memfile_env_set("vtoy_vhd_buf", g_vhdboot_isobuf, (ulonglong)g_vhdboot_isolen); #endif VENTOY_CMD_RETURN(GRUB_ERR_NONE); @@ -645,7 +638,6 @@ grub_err_t ventoy_cmd_raw_chain_data(grub_extcmd_context_t ctxt, int argc, char grub_disk_t disk; const char *pLastChain = NULL; ventoy_chain_head *chain; - char envbuf[64]; (void)ctxt; (void)argc; @@ -695,10 +687,7 @@ grub_err_t ventoy_cmd_raw_chain_data(grub_extcmd_context_t ctxt, int argc, char return 1; } - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain); - grub_env_set("vtoy_chain_mem_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%u", size); - grub_env_set("vtoy_chain_mem_size", envbuf); + ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size); grub_env_export("vtoy_chain_mem_addr"); grub_env_export("vtoy_chain_mem_size"); diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c index 8105e0e3..9bddab14 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c @@ -2033,7 +2033,6 @@ grub_err_t ventoy_cmd_windows_wimboot_data(grub_extcmd_context_t ctxt, int argc, grub_uint8_t *param = NULL; grub_uint8_t *exe_data = NULL; ventoy_windows_data *rtdata = NULL; - char envbuf[64] = {0}; char exename[128] = {0}; wim_tail wim_data; @@ -2076,13 +2075,7 @@ grub_err_t ventoy_cmd_windows_wimboot_data(grub_extcmd_context_t ctxt, int argc, rtdata = (ventoy_windows_data *)(param + jump_align + sizeof(ventoy_os_param)); ventoy_fill_windows_rtdata(rtdata, chain->os_param.vtoy_img_path, dataflag); - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (ulong)param); - grub_env_set("vtoy_wimboot_mem_addr", envbuf); - debug("vtoy_wimboot_mem_addr: %s\n", envbuf); - - grub_snprintf(envbuf, sizeof(envbuf), "%u", wim_data.bin_align_len); - grub_env_set("vtoy_wimboot_mem_size", envbuf); - debug("vtoy_wimboot_mem_size: %s\n", envbuf); + ventoy_memfile_env_set("vtoy_wimboot_mem", param, (ulonglong)(wim_data.bin_align_len)); grub_env_set(args[1], exename); grub_env_set(args[2], wim64 ? "64" : "32"); @@ -2105,7 +2098,6 @@ grub_err_t ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt, int argc, c const char *pLastChain = NULL; const char *compatible; ventoy_chain_head *chain; - char envbuf[64]; (void)ctxt; (void)argc; @@ -2200,10 +2192,7 @@ grub_err_t ventoy_cmd_windows_chain_data(grub_extcmd_context_t ctxt, int argc, c return 1; } - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain); - grub_env_set("vtoy_chain_mem_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%u", size); - grub_env_set("vtoy_chain_mem_size", envbuf); + ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size); grub_memset(chain, 0, sizeof(ventoy_chain_head)); @@ -2437,7 +2426,6 @@ static grub_err_t ventoy_vlnk_wim_chain_data(grub_file_t wimfile) ventoy_img_chunk *chunknode; ventoy_override_chunk *override; ventoy_img_chunk_list wimchunk; - char envbuf[128]; debug("vlnk wim chain data begin <%s> ...\n", wimfile->name); @@ -2494,10 +2482,7 @@ static grub_err_t ventoy_vlnk_wim_chain_data(grub_file_t wimfile) return 1; } - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain); - grub_env_set("vtoy_chain_mem_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%u", size); - grub_env_set("vtoy_chain_mem_size", envbuf); + ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size); grub_memset(chain, 0, sizeof(ventoy_chain_head)); @@ -2602,7 +2587,6 @@ static grub_err_t ventoy_normal_wim_chain_data(grub_file_t wimfile) ventoy_img_chunk *chunknode; ventoy_override_chunk *override; ventoy_img_chunk_list wimchunk; - char envbuf[128]; debug("normal wim chain data begin <%s> ...\n", wimfile->name); @@ -2659,10 +2643,7 @@ static grub_err_t ventoy_normal_wim_chain_data(grub_file_t wimfile) return 1; } - grub_snprintf(envbuf, sizeof(envbuf), "0x%lx", (unsigned long)chain); - grub_env_set("vtoy_chain_mem_addr", envbuf); - grub_snprintf(envbuf, sizeof(envbuf), "%u", size); - grub_env_set("vtoy_chain_mem_size", envbuf); + ventoy_memfile_env_set("vtoy_chain_mem", chain, (ulonglong)size); grub_memset(chain, 0, sizeof(ventoy_chain_head)); diff --git a/GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h b/GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h index f5357253..be992ce7 100644 --- a/GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h +++ b/GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h @@ -304,6 +304,7 @@ grub_uint64_t grub_udf_get_last_file_attr_offset grub_uint64_t *fe_entry_size_offset ); int ventoy_is_efi_os(void); +void ventoy_memfile_env_set(const char *prefix, const void *buf, unsigned long long len); #endif /* __VENTOY_H__ */