]> glassweightruler.freedombox.rocks Git - Ventoy.git/commitdiff
Code Optimization
authorlongpanda <admin@ventoy.net>
Sat, 22 Oct 2022 08:13:03 +0000 (16:13 +0800)
committerlongpanda <admin@ventoy.net>
Sat, 22 Oct 2022 08:13:03 +0000 (16:13 +0800)
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_unix.c
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c
GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_windows.c
GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h

index b3cdcbcd79462cd3cfb52654f12a15afc78893bf..8d4e5fda6edef9d829b926610f06ceac20fe224c 100644 (file)
@@ -188,6 +188,21 @@ void * ventoy_alloc_chain(grub_size_t size)
     return p;
 }
 
     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)
 {
 
 static int ventoy_arch_mode_init(void)
 {
index 3f7b4a01dbc65806817116ed66e987bfb6ed0c2f..935ad7a0cf21f34425b3401946b98c666e588005 100644 (file)
@@ -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;
     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;
     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_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;
 }
 
     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;
 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;
     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_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;
 
     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;
 {
     int rc = 1;
     int headlen;
-    char name[32];
-    char value[32];
     char *buf = NULL;
     grub_file_t file;
     
     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_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;
 
     grub_file_close(file); 
     rc = 0;
index c70ba9a27c72375dbf6c36ea42476e198abbcb93..47803de0c77bec64c3bf2119a692a9db2d8a8024 100644 (file)
@@ -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;
     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,
     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);
         }
     }
             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);
 }
 
     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;
     const char *pLastChain = NULL;
     const char *compatible;
     ventoy_chain_head *chain;
-    char envbuf[64];
     
     (void)ctxt;
     (void)argc;
     
     (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;
     }
 
         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));
 
 
     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;
 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;
     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);
 
     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);
 
 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 *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;
     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;
     }
 
         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);
 
 end:
     grub_check_free(filebuf);
index 708d6f2222ab2eed3f6ee4ac3be1bb0d5041e7c6..3c6086e6a62f5b256c408d7ad547c3605eee5dd7 100644 (file)
@@ -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;
     const char *pLastChain = NULL;
     const char *compatible;
     ventoy_chain_head *chain;
-    char envbuf[64];
     
     (void)ctxt;
     (void)argc;
     
     (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;
     }
 
         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));
 
 
     grub_memset(chain, 0, sizeof(ventoy_chain_head));
 
index 7aa3e8f0094a46591cf5eabc917fa937c552ebdc..84468704acb2c93dfb754ddaea1814174d547f07 100644 (file)
@@ -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;
     int patchoffset[2];
     ventoy_patch_vhd *patch1;
     ventoy_patch_vhd *patch2;
-    char envbuf[64];
 
     (void)ctxt;
     (void)argc;
 
     (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
 
     /* 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
 #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);
 #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;
     grub_disk_t disk;
     const char *pLastChain = NULL;
     ventoy_chain_head *chain;
-    char envbuf[64];
     
     (void)ctxt;
     (void)argc;
     
     (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;
     }
 
         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");
 
     grub_env_export("vtoy_chain_mem_addr");
     grub_env_export("vtoy_chain_mem_size");
index 8105e0e3562d64f36d3a0dcf9b56df51a97185bc..9bddab14e3bfd60308c053e6ec58832dd4014265 100644 (file)
@@ -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;
     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;
 
     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);
 
     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");
 
     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;
     const char *pLastChain = NULL;
     const char *compatible;
     ventoy_chain_head *chain;
-    char envbuf[64];
     
     (void)ctxt;
     (void)argc;
     
     (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;
     }
 
         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));
 
 
     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;
     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);
 
     
     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;
     }
 
         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));
 
 
     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;
     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);
 
     
     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;
     }
 
         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));
 
 
     grub_memset(chain, 0, sizeof(ventoy_chain_head));
 
index f5357253762c0421762f94c51ee1727537df49cc..be992ce79b4fab433ea455dd047564a76f447702 100644 (file)
@@ -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);
     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__ */
 
 
 #endif /* __VENTOY_H__ */