Skip to content

Commit

Permalink
1.0.27 release
Browse files Browse the repository at this point in the history
  • Loading branch information
ventoy committed Oct 31, 2020
1 parent 062c71b commit 147a23c
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 23 deletions.
82 changes: 81 additions & 1 deletion GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ char *g_wimiso_path = NULL;

int g_vhdboot_enable = 0;

grub_uint64_t g_conf_replace_offset = 0;
conf_replace *g_conf_replace_node = NULL;
grub_uint8_t *g_conf_replace_new_buf = NULL;
int g_conf_replace_new_len = 0;
int g_conf_replace_new_len_align = 0;

ventoy_gpt_info *g_ventoy_part_info = NULL;

static char *g_tree_script_buf = NULL;
Expand Down Expand Up @@ -2362,6 +2368,9 @@ static grub_err_t ventoy_cmd_img_sector(grub_extcmd_context_t ctxt, int argc, ch
return grub_error(GRUB_ERR_BAD_ARGUMENT, "Can't open file %s\n", args[0]);
}

g_conf_replace_node = NULL;
g_conf_replace_offset = 0;

if (g_img_chunk_list.chunk)
{
grub_free(g_img_chunk_list.chunk);
Expand Down Expand Up @@ -2400,6 +2409,75 @@ static grub_err_t ventoy_cmd_img_sector(grub_extcmd_context_t ctxt, int argc, ch
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}

static grub_err_t ventoy_select_conf_replace(grub_extcmd_context_t ctxt, int argc, char **args)
{
grub_uint64_t offset = 0;
grub_uint32_t align = 0;
grub_file_t file = NULL;
conf_replace *node = NULL;

(void)ctxt;
(void)argc;
(void)args;

debug("select conf replace argc:%d\n", argc);

if (argc < 2)
{
return 0;
}

node = ventoy_plugin_find_conf_replace(args[1]);
if (!node)
{
debug("Conf replace not found for %s\n", args[1]);
goto end;
}

debug("Find conf replace for %s\n", args[1]);

file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "(loop)%s", node->orgconf);
if (!file)
{
debug("<(loop)%s> NOT exist\n", node->orgconf);
goto end;
}

offset = grub_iso9660_get_last_file_dirent_pos(file);
grub_file_close(file);

file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s%s", args[0], node->newconf);
if (!file)
{
debug("New config file <%s%s> NOT exist\n", args[0], node->newconf);
goto end;
}

align = ((int)file->size + 2047) / 2048 * 2048;

if (align > vtoy_max_replace_file_size)
{
debug("New config file <%s%s> too big\n", args[0], node->newconf);
goto end;
}

grub_file_read(file, g_conf_replace_new_buf, file->size);
g_conf_replace_new_len = (int)file->size;
g_conf_replace_new_len_align = align;

g_conf_replace_node = node;
g_conf_replace_offset = offset + 2;

debug("conf_replace OK: newlen: %d\n", g_conf_replace_new_len);

end:
if (file)
{
grub_file_close(file);
}
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}

static grub_err_t ventoy_cmd_sel_auto_install(grub_extcmd_context_t ctxt, int argc, char **args)
{
int i = 0;
Expand Down Expand Up @@ -3829,6 +3907,7 @@ static int ventoy_env_init(void)
g_part_list_buf = grub_malloc(VTOY_PART_BUF_LEN);
g_tree_script_buf = grub_malloc(VTOY_MAX_SCRIPT_BUF);
g_list_script_buf = grub_malloc(VTOY_MAX_SCRIPT_BUF);
g_conf_replace_new_buf = grub_malloc(vtoy_max_replace_file_size);

ventoy_filt_register(0, ventoy_wrapper_open);

Expand Down Expand Up @@ -3897,6 +3976,7 @@ static cmd_para ventoy_cmds[] =
{ "vt_dump_persistence", ventoy_cmd_dump_persistence, 0, NULL, "", "", NULL },
{ "vt_select_auto_install", ventoy_cmd_sel_auto_install, 0, NULL, "", "", NULL },
{ "vt_select_persistence", ventoy_cmd_sel_persistence, 0, NULL, "", "", NULL },
{ "vt_select_conf_replace", ventoy_select_conf_replace, 0, NULL, "", "", NULL },

{ "vt_iso9660_nojoliet", ventoy_cmd_iso9660_nojoliet, 0, NULL, "", "", NULL },
{ "vt_is_udf", ventoy_cmd_is_udf, 0, NULL, "", "", NULL },
Expand Down Expand Up @@ -3961,7 +4041,7 @@ GRUB_MOD_INIT(ventoy)
cmd_para *cur = NULL;

ventoy_env_init();

for (i = 0; i < ARRAY_SIZE(ventoy_cmds); i++)
{
cur = ventoy_cmds + i;
Expand Down
17 changes: 17 additions & 0 deletions GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,17 @@ typedef struct menu_class
struct menu_class *next;
}menu_class;

#define vtoy_max_replace_file_size (2 * 1024 * 1024)
typedef struct conf_replace
{
int pathlen;
char isopath[256];
char orgconf[256];
char newconf[256];

struct conf_replace *next;
}conf_replace;

typedef struct injection_config
{
int pathlen;
Expand Down Expand Up @@ -806,6 +817,11 @@ extern grub_uint8_t g_ventoy_chain_type;
extern int g_vhdboot_enable;
extern int g_plugin_image_list;
extern ventoy_gpt_info *g_ventoy_part_info;
extern grub_uint64_t g_conf_replace_offset;
extern conf_replace *g_conf_replace_node;
extern grub_uint8_t *g_conf_replace_new_buf;
extern int g_conf_replace_new_len;
extern int g_conf_replace_new_len_align;

#define ventoy_unix_fill_virt(new_data, new_len) \
{ \
Expand Down Expand Up @@ -838,6 +854,7 @@ const char * ventoy_plugin_get_menu_alias(int type, const char *isopath);
const char * ventoy_plugin_get_menu_class(int type, const char *name);
int ventoy_plugin_check_memdisk(const char *isopath);
int ventoy_plugin_get_image_list_index(int type, const char *name);
conf_replace * ventoy_plugin_find_conf_replace(const char *iso);
int ventoy_get_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, grub_disk_addr_t start);
int ventoy_check_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, grub_disk_addr_t start);
void ventoy_plugin_dump_persistence(void);
Expand Down
127 changes: 109 additions & 18 deletions GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ grub_err_t ventoy_cmd_isolinux_initrd_collect(grub_extcmd_context_t ctxt, int ar
static grub_err_t ventoy_grub_cfg_initrd_collect(const char *fileName)
{
int i = 0;
int dollar = 0;
grub_file_t file = NULL;
char *buf = NULL;
char *start = NULL;
Expand Down Expand Up @@ -388,13 +389,18 @@ static grub_err_t ventoy_grub_cfg_initrd_collect(const char *fileName)
{
break;
}


dollar = 0;
for (i = 0; i < 255 && (0 == ventoy_is_word_end(*start)); i++)
{
img->name[i] = *start++;
if (img->name[i] == '$')
{
dollar = 1;
}
}

if (ventoy_find_initrd_by_name(g_initrd_img_list, img->name))
if (dollar == 1 || ventoy_find_initrd_by_name(g_initrd_img_list, img->name))
{
grub_free(img);
}
Expand Down Expand Up @@ -625,9 +631,30 @@ int ventoy_cpio_newc_fill_head(void *buf, int filesize, const void *filedata, co
return headlen;
}

static grub_uint32_t ventoy_linux_get_virt_chunk_count(void)
{
grub_uint32_t count = g_valid_initrd_count;

if (g_conf_replace_offset > 0)
{
count++;
}

return count;
}

static grub_uint32_t ventoy_linux_get_virt_chunk_size(void)
{
return (sizeof(ventoy_virt_chunk) + g_ventoy_cpio_size) * g_valid_initrd_count;
grub_uint32_t size;

size = (sizeof(ventoy_virt_chunk) + g_ventoy_cpio_size) * g_valid_initrd_count;

if (g_conf_replace_offset > 0)
{
size += sizeof(ventoy_virt_chunk) + g_conf_replace_new_len_align;
}

return size;
}

static void ventoy_linux_fill_virt_data( grub_uint64_t isosize, ventoy_chain_head *chain)
Expand All @@ -646,7 +673,7 @@ static void ventoy_linux_fill_virt_data( grub_uint64_t isosize, ventoy_chain_
sector = (isosize + 2047) / 2048;
cpio_secs = g_ventoy_cpio_size / 2048;

offset = g_valid_initrd_count * sizeof(ventoy_virt_chunk);
offset = ventoy_linux_get_virt_chunk_count() * sizeof(ventoy_virt_chunk);
cur = (ventoy_virt_chunk *)override;

for (node = g_initrd_img_list; node; node = node->next)
Expand Down Expand Up @@ -682,12 +709,51 @@ static void ventoy_linux_fill_virt_data( grub_uint64_t isosize, ventoy_chain_
cur++;
}

if (g_conf_replace_offset > 0)
{
cpio_secs = g_conf_replace_new_len_align / 2048;

cur->mem_sector_start = sector;
cur->mem_sector_end = cur->mem_sector_start + cpio_secs;
cur->mem_sector_offset = offset;
cur->remap_sector_start = 0;
cur->remap_sector_end = 0;
cur->org_sector_start = 0;

grub_memcpy(override + offset, g_conf_replace_new_buf, g_conf_replace_new_len);

chain->virt_img_size_in_bytes += g_conf_replace_new_len_align;

offset += g_conf_replace_new_len_align;
sector += cpio_secs;
cur++;
}

return;
}

static grub_uint32_t ventoy_linux_get_override_chunk_count(void)
{
grub_uint32_t count = g_valid_initrd_count;

if (g_conf_replace_offset > 0)
{
count++;
}

return count;
}

static grub_uint32_t ventoy_linux_get_override_chunk_size(void)
{
return sizeof(ventoy_override_chunk) * g_valid_initrd_count;
int count = g_valid_initrd_count;

if (g_conf_replace_offset > 0)
{
count++;
}

return sizeof(ventoy_override_chunk) * count;
}

static void ventoy_linux_fill_override_data( grub_uint64_t isosize, void *override)
Expand All @@ -697,6 +763,8 @@ static void ventoy_linux_fill_override_data( grub_uint64_t isosize, void *ove
grub_uint32_t newlen;
grub_uint64_t sector;
ventoy_override_chunk *cur;
ventoy_iso9660_override *dirent;
ventoy_udf_override *udf;

sector = (isosize + 2047) / 2048;

Expand All @@ -712,12 +780,12 @@ static void ventoy_linux_fill_override_data( grub_uint64_t isosize, void *ove
mod = newlen % 4;
if (mod > 0)
{
newlen += 4 - mod;
newlen += 4 - mod; /* cpio must align with 4 */
}

if (node->iso_type == 0)
{
ventoy_iso9660_override *dirent = (ventoy_iso9660_override *)node->override_data;
dirent = (ventoy_iso9660_override *)node->override_data;

node->override_length = sizeof(ventoy_iso9660_override);
dirent->first_sector = (grub_uint32_t)sector;
Expand All @@ -729,7 +797,7 @@ static void ventoy_linux_fill_override_data( grub_uint64_t isosize, void *ove
}
else
{
ventoy_udf_override *udf = (ventoy_udf_override *)node->override_data;
udf = (ventoy_udf_override *)node->override_data;

node->override_length = sizeof(ventoy_udf_override);
udf->length = newlen;
Expand All @@ -744,6 +812,23 @@ static void ventoy_linux_fill_override_data( grub_uint64_t isosize, void *ove
cur++;
}

if (g_conf_replace_offset > 0)
{
cur->img_offset = g_conf_replace_offset;
cur->override_size = sizeof(ventoy_iso9660_override);

newlen = (grub_uint32_t)(g_conf_replace_new_len);

dirent = (ventoy_iso9660_override *)cur->override_data;
dirent->first_sector = (grub_uint32_t)sector;
dirent->size = newlen;
dirent->first_sector_be = grub_swap_bytes32(dirent->first_sector);
dirent->size_be = grub_swap_bytes32(dirent->size);

sector += (dirent->size + 2047) / 2048;
cur++;
}

return;
}

Expand Down Expand Up @@ -1241,7 +1326,9 @@ grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, cha
grub_uint64_t isosize = 0;
grub_uint32_t boot_catlog = 0;
grub_uint32_t img_chunk_size = 0;
grub_uint32_t override_count = 0;
grub_uint32_t override_size = 0;
grub_uint32_t virt_chunk_count = 0;
grub_uint32_t virt_chunk_size = 0;
grub_file_t file;
grub_disk_t disk;
Expand Down Expand Up @@ -1294,6 +1381,9 @@ grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, cha
}

img_chunk_size = g_img_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);

override_count = ventoy_linux_get_override_chunk_count();
virt_chunk_count = ventoy_linux_get_virt_chunk_count();

if (ventoy_compatible)
{
Expand Down Expand Up @@ -1360,20 +1450,21 @@ grub_err_t ventoy_cmd_linux_chain_data(grub_extcmd_context_t ctxt, int argc, cha
return 0;
}

if (g_valid_initrd_count == 0)
/* part 4: override chunk */
if (override_count > 0)
{
return 0;
chain->override_chunk_offset = chain->img_chunk_offset + img_chunk_size;
chain->override_chunk_num = override_count;
ventoy_linux_fill_override_data(isosize, (char *)chain + chain->override_chunk_offset);
}

/* part 4: override chunk */
chain->override_chunk_offset = chain->img_chunk_offset + img_chunk_size;
chain->override_chunk_num = g_valid_initrd_count;
ventoy_linux_fill_override_data(isosize, (char *)chain + chain->override_chunk_offset);

/* part 5: virt chunk */
chain->virt_chunk_offset = chain->override_chunk_offset + override_size;
chain->virt_chunk_num = g_valid_initrd_count;
ventoy_linux_fill_virt_data(isosize, chain);
if (virt_chunk_count > 0)
{
chain->virt_chunk_offset = chain->override_chunk_offset + override_size;
chain->virt_chunk_num = virt_chunk_count;
ventoy_linux_fill_virt_data(isosize, chain);
}

VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
Expand Down
Loading

0 comments on commit 147a23c

Please sign in to comment.