Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ventoy committed Jan 5, 2021
1 parent 6434e45 commit f2ed81b
Show file tree
Hide file tree
Showing 25 changed files with 289 additions and 20 deletions.
1 change: 1 addition & 0 deletions GRUB2/MOD_SRC/grub-2.04/grub-core/Makefile.core.def
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,7 @@ module = {
common = ventoy/lzx.c;
common = ventoy/xpress.c;
common = ventoy/huffman.c;
common = ventoy/miniz.c;
};

module = {
Expand Down
64 changes: 64 additions & 0 deletions GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
#include <grub/acpi.h>
#include <grub/charset.h>
#include <grub/crypto.h>
#include <grub/lib/crc.h>
#include <grub/ventoy.h>
#include "ventoy_def.h"
#include "miniz.h"

GRUB_MOD_LICENSE ("GPLv3+");

Expand Down Expand Up @@ -105,6 +107,7 @@ int g_conf_replace_new_len = 0;
int g_conf_replace_new_len_align = 0;

ventoy_gpt_info *g_ventoy_part_info = NULL;
grub_uint64_t g_ventoy_disk_size = 0;

static char *g_tree_script_buf = NULL;
static int g_tree_script_pos = 0;
Expand Down Expand Up @@ -3437,6 +3440,22 @@ static grub_err_t ventoy_cmd_pop_last_entry(grub_extcmd_context_t ctxt, int argc
return 0;
}

grub_uint64_t ventoy_get_part1_size(ventoy_gpt_info *gpt)
{
grub_uint64_t sectors;

if (grub_strncmp(gpt->Head.Signature, "EFI PART", 8) == 0)
{
sectors = gpt->PartTbl[0].LastLBA + 1 - gpt->PartTbl[0].StartLBA;
}
else
{
sectors = gpt->MBR.PartTbl[0].SectorCount;
}

return sectors * 512;
}

static int ventoy_lib_module_callback(const char *filename, const struct grub_dirhook_info *info, void *data)
{
const char *pos = filename + 1;
Expand Down Expand Up @@ -3539,6 +3558,8 @@ static grub_err_t ventoy_cmd_load_part_table(grub_extcmd_context_t ctxt, int arg
return 1;
}

g_ventoy_disk_size = disk->total_sectors * (1U << disk->log_sector_size);

grub_disk_read(disk, 0, 0, sizeof(ventoy_gpt_info), g_ventoy_part_info);
grub_disk_close(disk);

Expand Down Expand Up @@ -3926,6 +3947,47 @@ int ventoy_is_dir_exist(const char *fmt, ...)
return 0;
}

int ventoy_gzip_compress(void *mem_in, int mem_in_len, void *mem_out, int mem_out_len)
{
mz_stream s;
grub_uint8_t *outbuf;
grub_uint8_t gzHdr[10] =
{
0x1F, 0x8B, /* magic */
8, /* z method */
0, /* flags */
0,0,0,0, /* mtime */
4, /* xfl */
3, /* OS */
};

grub_memset(&s, 0, sizeof(mz_stream));

mz_deflateInit2(&s, 1, MZ_DEFLATED, -MZ_DEFAULT_WINDOW_BITS, 6, MZ_DEFAULT_STRATEGY);

outbuf = (grub_uint8_t *)mem_out;

mem_out_len -= sizeof(gzHdr) + 8;
grub_memcpy(outbuf, gzHdr, sizeof(gzHdr));
outbuf += sizeof(gzHdr);

s.avail_in = mem_in_len;
s.next_in = mem_in;

s.avail_out = mem_out_len;
s.next_out = outbuf;

mz_deflate(&s, MZ_FINISH);

mz_deflateEnd(&s);

outbuf += s.total_out;
*(grub_uint32_t *)outbuf = grub_getcrc32c(0, outbuf, s.total_out);
*(grub_uint32_t *)(outbuf + 4) = (grub_uint32_t)(s.total_out);

return s.total_out + sizeof(gzHdr) + 8;
}

static int ventoy_env_init(void)
{
char buf[64];
Expand Down Expand Up @@ -4055,6 +4117,8 @@ static cmd_para ventoy_cmds[] =
{ "vt_unix_reset", ventoy_cmd_unix_reset, 0, NULL, "", "", NULL },
{ "vt_unix_replace_conf", ventoy_cmd_unix_replace_conf, 0, NULL, "", "", NULL },
{ "vt_unix_replace_ko", ventoy_cmd_unix_replace_ko, 0, NULL, "", "", NULL },
{ "vt_unix_fill_image_desc", ventoy_cmd_unix_fill_image_desc, 0, NULL, "", "", NULL },
{ "vt_unix_gzip_new_ko", ventoy_cmd_unix_gzip_newko, 0, NULL, "", "", NULL },
{ "vt_unix_chain_data", ventoy_cmd_unix_chain_data, 0, NULL, "", "", NULL },

{ "vt_img_hook_root", ventoy_cmd_img_hook_root, 0, NULL, "", "", NULL },
Expand Down
5 changes: 5 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 @@ -875,6 +875,7 @@ 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;
extern grub_uint64_t g_ventoy_disk_size;

#define ventoy_unix_fill_virt(new_data, new_len) \
{ \
Expand Down Expand Up @@ -924,6 +925,8 @@ int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid, grub_uint8_t
grub_err_t ventoy_cmd_unix_reset(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_unix_replace_conf(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_unix_replace_ko(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_unix_fill_image_desc(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_unix_gzip_newko(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_unix_freebsd_ver(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_parse_freenas_ver(grub_extcmd_context_t ctxt, int argc, char **args);
int ventoy_check_device_result(int ret);
Expand All @@ -934,6 +937,8 @@ grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char *
grub_err_t ventoy_cmd_raw_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char **args);
int ventoy_check_password(const vtoy_password *pwd, int retry);
int ventoy_gzip_compress(void *mem_in, int mem_in_len, void *mem_out, int mem_out_len);
grub_uint64_t ventoy_get_part1_size(ventoy_gpt_info *gpt);

#endif /* __VENTOY_DEF_H__ */

119 changes: 119 additions & 0 deletions GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,21 @@ static int ventoy_freebsd_append_conf(char *buf, const char *isopath)
return pos;
}

static int ventoy_dragonfly_append_conf(char *buf, const char *isopath)
{
int pos = 0;

debug("ventoy_dragonfly_append_conf %s\n", isopath);

vtoy_ssprintf(buf, pos, "tmpfs_load=\"%s\"\n", "YES");
vtoy_ssprintf(buf, pos, "dm_target_linear_load=\"%s\"\n", "YES");
vtoy_ssprintf(buf, pos, "initrd.img_load=\"%s\"\n", "YES");
vtoy_ssprintf(buf, pos, "initrd.img_type=\"%s\"\n", "md_image");
vtoy_ssprintf(buf, pos, "vfs.root.mountfrom=\"%s\"\n", "ufs:md0s0");

return pos;
}

grub_err_t ventoy_cmd_unix_reset(grub_extcmd_context_t ctxt, int argc, char **args)
{
(void)ctxt;
Expand Down Expand Up @@ -431,6 +446,10 @@ grub_err_t ventoy_cmd_unix_replace_conf(grub_extcmd_context_t ctxt, int argc, ch
{
g_conf_new_len += ventoy_freebsd_append_conf(data + file->size, args[1]);
}
else if (grub_strcmp(args[0], "DragonFly") == 0)
{
g_conf_new_len += ventoy_dragonfly_append_conf(data + file->size, args[1]);
}

VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
Expand Down Expand Up @@ -474,6 +493,7 @@ grub_err_t ventoy_cmd_unix_replace_ko(grub_extcmd_context_t ctxt, int argc, char
data = grub_malloc(file->size);
if (!data)
{
debug("Failed to alloc memory for new ko %d\n", (int)file->size);
grub_file_close(file);
return 1;
}
Expand All @@ -487,6 +507,105 @@ grub_err_t ventoy_cmd_unix_replace_ko(grub_extcmd_context_t ctxt, int argc, char
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}

grub_err_t ventoy_cmd_unix_fill_image_desc(grub_extcmd_context_t ctxt, int argc, char **args)
{
int i;
grub_uint8_t *byte;
grub_uint32_t memsize;
ventoy_image_desc *desc;
grub_uint8_t flag[32] = {
0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};

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

debug("ventoy_cmd_unix_fill_image_desc %p\n", g_mod_new_data);

if (!g_mod_new_data)
{
goto end;
}

byte = (grub_uint8_t *)g_mod_new_data;
for (i = 0; i < g_mod_new_len - 32; i += 16)
{
if (byte[i] == 0xFF && byte[i + 1] == 0xEE)
{
if (grub_memcmp(flag, byte + i, 32) == 0)
{
debug("Find position flag at %d(0x%x)\n", i, i);
break;
}
}
}

if (i >= g_mod_new_len - 32)
{
debug("Failed to find position flag %d\n", i);
goto end;
}

desc = (ventoy_image_desc *)(byte + i);
desc->disk_size = g_ventoy_disk_size;
desc->part1_size = ventoy_get_part1_size(g_ventoy_part_info);
grub_memcpy(desc->disk_uuid, g_ventoy_part_info->MBR.BootCode + 0x180, 16);
grub_memcpy(desc->disk_signature, g_ventoy_part_info->MBR.BootCode + 0x1B8, 4);

desc->img_chunk_count = g_img_chunk_list.cur_chunk;
memsize = g_img_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);

debug("image chunk count:%u memsize:%u\n", desc->img_chunk_count, memsize);

if (memsize >= VTOY_SIZE_1MB * 8)
{
grub_printf("image chunk count:%u memsize:%u too big\n", desc->img_chunk_count, memsize);
goto end;
}

grub_memcpy(desc + 1, g_img_chunk_list.chunk, memsize);

end:
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}

grub_err_t ventoy_cmd_unix_gzip_newko(grub_extcmd_context_t ctxt, int argc, char **args)
{
int newlen;
grub_uint8_t *buf;

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

debug("ventoy_cmd_unix_gzip_newko %p\n", g_mod_new_data);

if (!g_mod_new_data)
{
goto end;
}

buf = grub_malloc(g_mod_new_len);
if (!buf)
{
goto end;
}

newlen = ventoy_gzip_compress(g_mod_new_data, g_mod_new_len, buf, g_mod_new_len);

grub_free(g_mod_new_data);

debug("gzip org len:%d newlen:%d\n", g_mod_new_len, newlen);

g_mod_new_data = (char *)buf;
g_mod_new_len = newlen;

end:
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}

grub_err_t ventoy_cmd_unix_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
{
int ventoy_compatible = 0;
Expand Down
18 changes: 18 additions & 0 deletions GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_vhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ static int ventoy_raw_trim_head(grub_uint64_t offset)
grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char **args)
{
int i;
int altboot = 0;
int offset = -1;
grub_file_t file;
grub_uint8_t data = 0;
vhd_footer_t vhdfoot;
VDIPREHEADER vdihdr;
char type[16] = {0};
Expand Down Expand Up @@ -427,6 +429,7 @@ grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char *
if (grub_memcmp(gpt->PartTbl[i].PartType, "Hah!IdontNeedEFI", 16) == 0)
{
debug("part %d is grub_bios part\n", i);
altboot = 1;
grub_env_set(args[3], "1");
break;
}
Expand All @@ -436,6 +439,20 @@ grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char *
}
}
}

if (!altboot)
{
if (gpt->MBR.BootCode[92] == 0x22)
{
grub_file_seek(file, offset + 17908);
grub_file_read(file, &data, 1);
if (data == 0x23)
{
altboot = 1;
grub_env_set(args[3], "1");
}
}
}
}
else
{
Expand All @@ -447,6 +464,7 @@ grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char *
if (gpt->MBR.PartTbl[i].FsFlag == 0xEF)
{
debug("part %d is esp part in MBR mode\n", i);
altboot = 1;
grub_env_set(args[3], "1");
break;
}
Expand Down
14 changes: 12 additions & 2 deletions GRUB2/MOD_SRC/grub-2.04/include/grub/ventoy.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ typedef struct ventoy_secure_data
grub_uint8_t magic2[16]; /* VENTOY_GUID */
}ventoy_secure_data;



#pragma pack()

// compile assert check : sizeof(ventoy_os_param) must be 512
Expand Down Expand Up @@ -188,6 +186,18 @@ typedef struct ventoy_chain_head
grub_uint32_t virt_chunk_num;
}ventoy_chain_head;

typedef struct ventoy_image_desc
{
grub_uint64_t disk_size;
grub_uint64_t part1_size;
grub_uint8_t disk_uuid[16];
grub_uint8_t disk_signature[4];
grub_uint32_t img_chunk_count;
/* ventoy_img_chunk list */
}ventoy_image_desc;



typedef struct ventoy_img_chunk
{
grub_uint32_t img_start_sector; // sector size: 2KB
Expand Down
8 changes: 7 additions & 1 deletion IMG/cpio/ventoy/hook/wifislax/ventoy-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

. $VTOY_PATH/hook/ventoy-os-lib.sh

$SED "/mount.*devtmpfs/a $BUSYBOX_PATH/sh $VTOY_PATH/hook/wifislax/disk_hook.sh" -i /linuxrc
if [ -e /linuxrc ]; then
INITFILE=/linuxrc
elif [ -e /init ]; then
INITFILE=/init
fi

$SED "/mount.*devtmpfs/a $BUSYBOX_PATH/sh $VTOY_PATH/hook/wifislax/disk_hook.sh" -i $INITFILE

#replace original blkid
$BUSYBOX_PATH/rm -f /usr/bin/blkid
Expand Down
Binary file modified INSTALL/EFI/BOOT/BOOTAA64.EFI
Binary file not shown.
Binary file modified INSTALL/EFI/BOOT/grubia32_real.efi
Binary file not shown.
Binary file modified INSTALL/EFI/BOOT/grubx64_real.efi
Binary file not shown.
Loading

0 comments on commit f2ed81b

Please sign in to comment.