add support for linux vdisk(vhd/vdi/raw)

pull/494/head
longpanda 4 years ago
parent 3c649b281f
commit d02f184a8d

@ -273,19 +273,33 @@ static int ventoy_update_image_location(ventoy_os_param *param)
}
CopyMem(&location->guid, &param->guid, sizeof(ventoy_guid));
location->image_sector_size = 2048;
location->image_sector_size = gSector512Mode ? 512 : 2048;
location->disk_sector_size = g_chain->disk_sector_size;
location->region_count = g_img_chunk_num;
region = location->regions;
for (i = 0; i < g_img_chunk_num; i++)
if (gSector512Mode)
{
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
region->image_start_sector = chunk->img_start_sector;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
for (i = 0; i < g_img_chunk_num; i++)
{
region->image_sector_count = chunk->disk_end_sector - chunk->disk_start_sector + 1;
region->image_start_sector = chunk->img_start_sector * 4;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
}
else
{
for (i = 0; i < g_img_chunk_num; i++)
{
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
region->image_start_sector = chunk->img_start_sector;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
}
return 0;

@ -71,6 +71,7 @@ STATIC EFI_LOCATE_HANDLE g_org_locate_handle = NULL;
STATIC UINT8 g_sector_buf[2048];
STATIC EFI_BLOCK_READ g_sector_2048_read = NULL;
STATIC EFI_BLOCK_WRITE g_sector_2048_write = NULL;
BOOLEAN ventoy_is_cdrom_dp_exist(VOID)
{
@ -245,6 +246,87 @@ STATIC EFI_STATUS EFIAPI ventoy_read_iso_sector
return EFI_SUCCESS;
}
STATIC EFI_STATUS EFIAPI ventoy_write_iso_sector
(
IN UINT64 Sector,
IN UINTN Count,
IN VOID *Buffer
)
{
EFI_STATUS Status = EFI_SUCCESS;
EFI_LBA MapLba = 0;
UINT32 i = 0;
UINTN secLeft = 0;
UINTN secRead = 0;
UINT64 ReadStart = 0;
UINT64 ReadEnd = 0;
UINT8 *pCurBuf = (UINT8 *)Buffer;
ventoy_img_chunk *pchunk = g_chunk;
EFI_BLOCK_IO_PROTOCOL *pRawBlockIo = gBlockData.pRawBlockIo;
debug("write iso sector %lu count %u", Sector, Count);
ReadStart = Sector * 2048;
ReadEnd = (Sector + Count) * 2048;
for (i = 0; Count > 0 && i < g_img_chunk_num; i++, pchunk++)
{
if (Sector >= pchunk->img_start_sector && Sector <= pchunk->img_end_sector)
{
if (g_chain->disk_sector_size == 512)
{
MapLba = (Sector - pchunk->img_start_sector) * 4 + pchunk->disk_start_sector;
}
else
{
MapLba = (Sector - pchunk->img_start_sector) * 2048 / g_chain->disk_sector_size + pchunk->disk_start_sector;
}
secLeft = pchunk->img_end_sector + 1 - Sector;
secRead = (Count < secLeft) ? Count : secLeft;
Status = pRawBlockIo->WriteBlocks(pRawBlockIo, pRawBlockIo->Media->MediaId,
MapLba, secRead * 2048, pCurBuf);
if (EFI_ERROR(Status))
{
debug("Raw disk write block failed %r LBA:%lu Count:%u", Status, MapLba, secRead);
return Status;
}
Count -= secRead;
Sector += secRead;
pCurBuf += secRead * 2048;
}
}
return EFI_SUCCESS;
}
EFI_STATUS EFIAPI ventoy_block_io_ramdisk_write
(
IN EFI_BLOCK_IO_PROTOCOL *This,
IN UINT32 MediaId,
IN EFI_LBA Lba,
IN UINTN BufferSize,
IN VOID *Buffer
)
{
(VOID)This;
(VOID)MediaId;
(VOID)Lba;
(VOID)BufferSize;
(VOID)Buffer;
if (!gSector512Mode)
{
return EFI_WRITE_PROTECTED;
}
CopyMem(g_iso_data_buf + (Lba * 2048), Buffer, BufferSize);
return EFI_SUCCESS;
}
EFI_STATUS EFIAPI ventoy_block_io_ramdisk_read
(
IN EFI_BLOCK_IO_PROTOCOL *This,
@ -438,12 +520,21 @@ EFI_STATUS EFIAPI ventoy_block_io_write
IN VOID *Buffer
)
{
UINT32 secNum = 0;
UINT64 offset = 0;
(VOID)This;
(VOID)MediaId;
(VOID)Lba;
(VOID)BufferSize;
(VOID)Buffer;
return EFI_WRITE_PROTECTED;
if (!gSector512Mode)
{
return EFI_WRITE_PROTECTED;
}
secNum = BufferSize / 2048;
offset = Lba * 2048;
return ventoy_write_iso_sector(Lba, secNum, Buffer);
}
EFI_STATUS EFIAPI ventoy_block_io_flush(IN EFI_BLOCK_IO_PROTOCOL *This)
@ -633,6 +724,68 @@ EFI_STATUS EFIAPI ventoy_block_io_read_512
return Status;
}
EFI_STATUS EFIAPI ventoy_block_io_write_512
(
IN EFI_BLOCK_IO_PROTOCOL *This,
IN UINT32 MediaId,
IN EFI_LBA Lba,
IN UINTN BufferSize,
IN VOID *Buffer
)
{
EFI_LBA Mod;
UINTN ReadSize;
UINT8 *CurBuf = NULL;
EFI_STATUS Status = EFI_SUCCESS;
debug("ventoy_block_io_write_512 %lu %lu\n", Lba, BufferSize / 512);
CurBuf = (UINT8 *)Buffer;
Mod = Lba % 4;
if (Mod > 0)
{
Status |= g_sector_2048_read(This, MediaId, Lba / 4, 2048, g_sector_buf);
if (BufferSize <= (4 - Mod) * 512)
{
CopyMem(g_sector_buf + Mod * 512, CurBuf, BufferSize);
return g_sector_2048_write(This, MediaId, Lba / 4, 2048, g_sector_buf);
}
else
{
ReadSize = (4 - Mod) * 512;
CopyMem(g_sector_buf + Mod * 512, CurBuf, ReadSize);
g_sector_2048_write(This, MediaId, Lba / 4, 2048, g_sector_buf);
CurBuf += ReadSize;
Lba += (4 - Mod);
BufferSize -= ReadSize;
}
}
if (BufferSize >= 2048)
{
ReadSize = BufferSize / 2048 * 2048;
Status |= g_sector_2048_write(This, MediaId, Lba / 4, ReadSize, CurBuf);
CurBuf += ReadSize;
Lba += ReadSize / 512;
BufferSize -= ReadSize;
}
if (BufferSize > 0)
{
Status |= g_sector_2048_read(This, MediaId, Lba / 4, 2048, g_sector_buf);
CopyMem(g_sector_buf, CurBuf, BufferSize);
g_sector_2048_write(This, MediaId, Lba / 4, 2048, g_sector_buf);
}
return Status;
}
EFI_STATUS EFIAPI ventoy_install_blockio(IN EFI_HANDLE ImageHandle, IN UINT64 ImgSize)
{
EFI_STATUS Status = EFI_SUCCESS;
@ -665,14 +818,16 @@ EFI_STATUS EFIAPI ventoy_install_blockio(IN EFI_HANDLE ImageHandle, IN UINT64 Im
if (gSector512Mode)
{
g_sector_2048_read = gMemdiskMode ? ventoy_block_io_ramdisk_read : ventoy_block_io_read;
g_sector_2048_write = gMemdiskMode ? ventoy_block_io_ramdisk_write : ventoy_block_io_write;
pBlockIo->ReadBlocks = ventoy_block_io_read_512;
pBlockIo->WriteBlocks = ventoy_block_io_write_512;
}
else
{
pBlockIo->ReadBlocks = gMemdiskMode ? ventoy_block_io_ramdisk_read : ventoy_block_io_read;
pBlockIo->WriteBlocks = ventoy_block_io_write;
}
pBlockIo->WriteBlocks = ventoy_block_io_write;
pBlockIo->FlushBlocks = ventoy_block_io_flush;
Status = gBS->InstallMultipleProtocolInterfaces(&gBlockData.Handle,

@ -190,10 +190,17 @@ command-line or ESC to discard edits and return to the GRUB menu."),
}
else
{
char szLine[128];
const char *checkret = grub_env_get("VTOY_CHKDEV_RESULT_STRING");
if (checkret == NULL || checkret[0] != '0') {
grub_snprintf(szLine, sizeof(szLine), "%s [Unofficial Ventoy]", grub_env_get("VTOY_TEXT_MENU_VER"));
} else {
grub_snprintf(szLine, sizeof(szLine), "%s", grub_env_get("VTOY_TEXT_MENU_VER"));
}
ret += grub_print_message_indented_real("\n", STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
ret += grub_print_message_indented_real(grub_env_get("VTOY_TEXT_MENU_VER"),
STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
ret += grub_print_message_indented_real(szLine, STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
ret += grub_print_message_indented_real("\n", STANDARD_MARGIN, STANDARD_MARGIN, term, dry_run);
ret += grub_print_message_indented_real(grub_env_get("VTOY_HOTKEY_TIP"),

@ -40,6 +40,7 @@
#endif
#include <grub/time.h>
#include <grub/video.h>
#include <grub/acpi.h>
#include <grub/relocator.h>
#include <grub/charset.h>
#include <grub/ventoy.h>
@ -108,12 +109,12 @@ static ventoy_video_mode *g_video_mode_list = NULL;
static const char *g_menu_class[] =
{
"vtoyiso", "vtoywim", "vtoyefi", "vtoyimg", "vtoyvhd"
"vtoyiso", "vtoywim", "vtoyefi", "vtoyimg", "vtoyvhd", "vtoyvtoy"
};
static const char *g_menu_prefix[] =
{
"iso", "wim", "efi", "img", "vhd"
"iso", "wim", "efi", "img", "vhd", "vtoy"
};
void ventoy_debug(const char *fmt, ...)
@ -1195,6 +1196,10 @@ static int ventoy_colect_img_files(const char *filename, const struct grub_dirho
}
type = img_type_img;
}
else if (len >= 5 && 0 == grub_strcasecmp(filename + len - 5, ".vtoy"))
{
type = img_type_vtoy;
}
else
{
return 0;
@ -1495,19 +1500,21 @@ int ventoy_check_device_result(int ret)
{
char buf[32];
grub_snprintf(buf, sizeof(buf), "%d", ret);
ventoy_set_env("VTOY_CHKDEV_RESULT_STRING", buf);
grub_snprintf(buf, sizeof(buf), "%d", (ret & 0x7FFF));
grub_env_set("VTOY_CHKDEV_RESULT_STRING", buf);
grub_env_export("VTOY_CHKDEV_RESULT_STRING");
if (ret)
if (ret & 0x1000)
{
grub_printf(VTOY_WARNING"\n");
grub_printf(VTOY_WARNING"\n");
grub_printf(VTOY_WARNING"\n\n\n");
grub_printf("Unsatisfied conditions detected for Ventoy.\n\n");
grub_printf("This is NOT a standard Ventoy device and is NOT officially supported.\n\n");
grub_printf("Recommend to follow the instructions in https://www.ventoy.net to use Ventoy.\n");
grub_printf("\n\nWill continue to boot after 10 seconds ...... ");
grub_printf("\n\nWill exit after 10 seconds ...... ");
grub_refresh();
grub_sleep(10);
}
@ -1527,29 +1534,41 @@ int ventoy_check_device(grub_device_t dev)
if (dev->disk == NULL || dev->disk->partition == NULL)
{
return ventoy_check_device_result(1);
return ventoy_check_device_result(1 | 0x1000);
}
partition = dev->disk->partition;
if (partition->number != 0 || partition->start != 2048)
if (0 == ventoy_check_file_exist("(%s,2)/ventoy/ventoy.cpio", dev->disk->name) ||
0 == ventoy_check_file_exist("(%s,2)/grub/localboot.cfg", dev->disk->name) ||
0 == ventoy_check_file_exist("(%s,2)/tool/mount.exfat-fuse_64", dev->disk->name))
{
return ventoy_check_device_result(2);
return ventoy_check_device_result(2 | 0x1000);
}
offset = partition->start + partition->len;
/* We must have partition 2 */
file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "(%s,2)/ventoy/ventoy.cpio", dev->disk->name);
if (!file)
{
return ventoy_check_device_result(3);
return ventoy_check_device_result(3 | 0x1000);
}
if (NULL == grub_strstr(file->fs->name, "fat"))
{
grub_file_close(file);
return ventoy_check_device_result(4 | 0x1000);
}
partition = dev->disk->partition;
if (partition->number != 0 || partition->start != 2048)
{
return ventoy_check_device_result(5);
}
offset = partition->start + partition->len;
partition = file->device->disk->partition;
if ((partition->number != 1) || (partition->len != 65536) || (offset != partition->start))
{
grub_file_close(file);
return ventoy_check_device_result(4);
return ventoy_check_device_result(6);
}
grub_file_close(file);
@ -1557,21 +1576,21 @@ int ventoy_check_device(grub_device_t dev)
dev2 = grub_device_open(devname);
if (!dev2)
{
return ventoy_check_device_result(5);
return ventoy_check_device_result(7);
}
fs = grub_fs_probe(dev2);
if (!fs)
{
grub_device_close(dev2);
return ventoy_check_device_result(6);
return ventoy_check_device_result(8);
}
fs->fs_label(dev2, &label);
if ((!label) || grub_strncmp("VTOYEFI", label, 7))
{
grub_device_close(dev2);
return ventoy_check_device_result(7);
return ventoy_check_device_result(9);
}
grub_device_close(dev2);
@ -1624,9 +1643,6 @@ static grub_err_t ventoy_cmd_list_img(grub_extcmd_context_t ctxt, int argc, char
goto fail;
}
/* make sure that we are running in a correct Ventoy device */
ventoy_check_device(dev);
g_enum_fs = fs = grub_fs_probe(dev);
if (!fs)
{
@ -1869,7 +1885,7 @@ static grub_err_t ventoy_cmd_chosen_img_path(grub_extcmd_context_t ctxt, int arg
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid)
int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid, grub_uint8_t *signature)
{
grub_disk_t disk;
char *device_name;
@ -1903,6 +1919,7 @@ int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid)
if (disk)
{
grub_disk_read(disk, 0, 0x180, 16, guid);
grub_disk_read(disk, 0, 0x1b8, 4, signature);
grub_disk_close(disk);
}
else
@ -1999,7 +2016,7 @@ void ventoy_fill_os_param(grub_file_t file, ventoy_os_param *param)
grub_snprintf(param->vtoy_img_path, sizeof(param->vtoy_img_path), "%s", pos);
ventoy_get_disk_guid(file->name, param->vtoy_disk_guid);
ventoy_get_disk_guid(file->name, param->vtoy_disk_guid, param->vtoy_disk_signature);
param->vtoy_img_size = file->size;
@ -2112,7 +2129,10 @@ int ventoy_get_block_list(grub_file_t file, ventoy_img_chunk_list *chunklist, gr
}
len = (int)grub_strlen(file->name);
if (grub_strncasecmp(file->name + len - 4, ".img", 4) == 0)
if ((len > 4 && grub_strncasecmp(file->name + len - 4, ".img", 4) == 0) ||
(len > 4 && grub_strncasecmp(file->name + len - 4, ".vhd", 4) == 0) ||
(len > 5 && grub_strncasecmp(file->name + len - 5, ".vhdx", 5) == 0) ||
(len > 5 && grub_strncasecmp(file->name + len - 5, ".vtoy", 5) == 0))
{
for (i = 0; i < chunklist->cur_chunk; i++)
{
@ -2155,6 +2175,12 @@ static grub_err_t ventoy_cmd_img_sector(grub_extcmd_context_t ctxt, int argc, ch
grub_free(g_img_chunk_list.chunk);
}
if (ventoy_get_fs_type(file->fs->name) >= ventoy_fs_max)
{
grub_file_close(file);
return grub_error(GRUB_ERR_BAD_ARGUMENT, "Unsupported filesystem %s\n", file->fs->name);
}
/* get image chunk data */
grub_memset(&g_img_chunk_list, 0, sizeof(g_img_chunk_list));
g_img_chunk_list.chunk = grub_malloc(sizeof(ventoy_img_chunk) * DEFAULT_CHUNK_NUM);
@ -2977,6 +3003,119 @@ static grub_err_t ventoy_cmd_img_unhook_root(grub_extcmd_context_t ctxt, int arg
return 0;
}
static grub_err_t ventoy_cmd_acpi_param(grub_extcmd_context_t ctxt, int argc, char **args)
{
int i;
int buflen;
int datalen;
int loclen;
int img_chunk_num;
int image_sector_size;
char cmd[64];
ventoy_chain_head *chain;
ventoy_img_chunk *chunk;
ventoy_os_param *osparam;
ventoy_image_location *location;
ventoy_image_disk_region *region;
struct grub_acpi_table_header *acpi;
(void)ctxt;
if (argc != 2)
{
return 1;
}
debug("ventoy_cmd_acpi_param %s %s\n", args[0], args[1]);
chain = (ventoy_chain_head *)(ulong)grub_strtoul(args[0], NULL, 16);
if (!chain)
{
return 1;
}
image_sector_size = (int)grub_strtol(args[1], NULL, 10);
if (grub_memcmp(&g_ventoy_guid, &(chain->os_param.guid), 16))
{
debug("Invalid ventoy guid 0x%x\n", chain->os_param.guid.data1);
return 1;
}
img_chunk_num = chain->img_chunk_num;
loclen = sizeof(ventoy_image_location) + (img_chunk_num - 1) * sizeof(ventoy_image_disk_region);
datalen = sizeof(ventoy_os_param) + loclen;
buflen = sizeof(struct grub_acpi_table_header) + datalen;
acpi = grub_zalloc(buflen);
if (!acpi)
{
return 1;
}
/* Step1: Fill acpi table header */
grub_memcpy(acpi->signature, "VTOY", 4);
acpi->length = buflen;
acpi->revision = 1;
grub_memcpy(acpi->oemid, "VENTOY", 6);
grub_memcpy(acpi->oemtable, "OSPARAMS", 8);
acpi->oemrev = 1;
acpi->creator_id[0] = 1;
acpi->creator_rev = 1;
/* Step2: Fill data */
osparam = (ventoy_os_param *)(acpi + 1);
grub_memcpy(osparam, &chain->os_param, sizeof(ventoy_os_param));
osparam->vtoy_img_location_addr = 0;
osparam->vtoy_img_location_len = loclen;
osparam->chksum = 0;
osparam->chksum = 0x100 - grub_byte_checksum(osparam, sizeof(ventoy_os_param));
location = (ventoy_image_location *)(osparam + 1);
grub_memcpy(&location->guid, &osparam->guid, sizeof(ventoy_guid));
location->image_sector_size = image_sector_size;
location->disk_sector_size = chain->disk_sector_size;
location->region_count = img_chunk_num;
region = location->regions;
chunk = (ventoy_img_chunk *)((char *)chain + chain->img_chunk_offset);
if (512 == image_sector_size)
{
for (i = 0; i < img_chunk_num; i++)
{
region->image_sector_count = chunk->disk_end_sector - chunk->disk_start_sector + 1;
region->image_start_sector = chunk->img_start_sector * 4;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
}
else
{
for (i = 0; i < img_chunk_num; i++)
{
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
region->image_start_sector = chunk->img_start_sector;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
}
/* Step3: Fill acpi checksum */
acpi->checksum = 0;
acpi->checksum = 0x100 - grub_byte_checksum(acpi, acpi->length);
/* load acpi table */
grub_snprintf(cmd, sizeof(cmd), "acpi mem:0x%lx:size:%d", (ulong)acpi, acpi->length);
grub_script_execute_sourcecode(cmd);
grub_free(acpi);
VENTOY_CMD_RETURN(0);
}
static grub_err_t ventoy_cmd_push_last_entry(grub_extcmd_context_t ctxt, int argc, char **args)
{
(void)ctxt;
@ -3081,7 +3220,10 @@ end:
static grub_err_t ventoy_cmd_load_part_table(grub_extcmd_context_t ctxt, int argc, char **args)
{
char name[64];
int ret;
grub_disk_t disk;
grub_device_t dev;
(void)argc;
(void)ctxt;
@ -3101,7 +3243,21 @@ static grub_err_t ventoy_cmd_load_part_table(grub_extcmd_context_t ctxt, int arg
grub_disk_read(disk, 0, 0, sizeof(ventoy_gpt_info), g_ventoy_part_info);
grub_disk_close(disk);
grub_snprintf(name, sizeof(name), "%s,1", args[0]);
dev = grub_device_open(name);
if (dev)
{
/* make sure that we are running in a correct Ventoy device */
ret = ventoy_check_device(dev);
grub_device_close(dev);
if (ret & 0x1000)
{
grub_exit();
}
}
return 0;
}
@ -3492,6 +3648,8 @@ static int ventoy_env_init(void)
g_grub_param->grub_env_printf = (grub_env_printf_pf)grub_printf;
grub_snprintf(buf, sizeof(buf), "%p", g_grub_param);
grub_env_set("env_param", buf);
grub_env_set("ventoy_env_param", buf);
grub_env_export("ventoy_env_param");
}
return 0;
@ -3517,6 +3675,8 @@ static cmd_para ventoy_cmds[] =
{ "vt_load_wimboot", ventoy_cmd_load_wimboot, 0, NULL, "", "", NULL },
{ "vt_load_vhdboot", ventoy_cmd_load_vhdboot, 0, NULL, "", "", NULL },
{ "vt_patch_vhdboot", ventoy_cmd_patch_vhdboot, 0, NULL, "", "", NULL },
{ "vt_raw_chain_data", ventoy_cmd_raw_chain_data, 0, NULL, "", "", NULL },
{ "vt_get_vtoy_type", ventoy_cmd_get_vtoy_type, 0, NULL, "", "", NULL },
{ "vt_cpio_busybox64", ventoy_cmd_cpio_busybox_64, 0, NULL, "", "", NULL },
{ "vt_load_cpio", ventoy_cmd_load_cpio, 0, NULL, "", "", NULL },
@ -3597,6 +3757,7 @@ static cmd_para ventoy_cmds[] =
{ "vt_img_hook_root", ventoy_cmd_img_hook_root, 0, NULL, "", "", NULL },
{ "vt_img_unhook_root", ventoy_cmd_img_unhook_root, 0, NULL, "", "", NULL },
{ "vt_acpi_param", ventoy_cmd_acpi_param, 0, NULL, "", "", NULL },
};

@ -152,11 +152,12 @@ typedef struct ventoy_iso9660_vd
#pragma pack()
#define img_type_iso 0
#define img_type_wim 1
#define img_type_efi 2
#define img_type_img 3
#define img_type_vhd 4
#define img_type_iso 0
#define img_type_wim 1
#define img_type_efi 2
#define img_type_img 3
#define img_type_vhd 4
#define img_type_vtoy 5
typedef struct img_info
{
@ -656,6 +657,41 @@ typedef struct ventoy_gpt_info
ventoy_gpt_head Head;
ventoy_gpt_part_tbl PartTbl[128];
}ventoy_gpt_info;
typedef struct vhd_footer_t
{
char cookie[8]; // Cookie
grub_uint32_t features; // Features
grub_uint32_t ffversion; // File format version
grub_uint32_t dataoffset; // Data offset
grub_uint32_t timestamp; // Timestamp
grub_uint32_t creatorapp; // Creator application
grub_uint32_t creatorver; // Creator version
grub_uint32_t creatorhos; // Creator host OS
grub_uint32_t origsize; // Original size
grub_uint32_t currsize; // Current size
grub_uint32_t diskgeom; // Disk geometry
grub_uint32_t disktype; // Disk type
grub_uint32_t checksum; // Checksum
grub_uint8_t uniqueid[16]; // Unique ID
grub_uint8_t savedst; // Saved state
}vhd_footer_t;
#define VDI_IMAGE_FILE_INFO "<<< Oracle VM VirtualBox Disk Image >>>\n"
/** Image signature. */
#define VDI_IMAGE_SIGNATURE (0xbeda107f)
typedef struct VDIPREHEADER
{
/** Just text info about image type, for eyes only. */
char szFileInfo[64];
/** The image signature (VDI_IMAGE_SIGNATURE). */
grub_uint32_t u32Signature;
/** The image version (VDI_IMAGE_VERSION). */
grub_uint32_t u32Version;
} VDIPREHEADER, *PVDIPREHEADER;
#pragma pack()
typedef struct ventoy_video_mode
@ -792,7 +828,7 @@ grub_err_t ventoy_cmd_collect_wim_patch(grub_extcmd_context_t ctxt, int argc, ch
grub_err_t ventoy_cmd_wim_patch_count(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_locate_wim_patch(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_unix_chain_data(grub_extcmd_context_t ctxt, int argc, char **args);
int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid);
int ventoy_get_disk_guid(const char *filename, grub_uint8_t *guid, grub_uint8_t *signature);
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);
@ -803,6 +839,8 @@ int ventoy_check_device(grub_device_t dev);
void ventoy_debug_dump_guid(const char *prefix, grub_uint8_t *guid);
grub_err_t ventoy_cmd_load_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args);
grub_err_t ventoy_cmd_patch_vhdboot(grub_extcmd_context_t ctxt, int argc, char **args);
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);
#endif /* __VENTOY_DEF_H__ */

@ -194,6 +194,7 @@ static int ventoy_freebsd_append_conf(char *buf, const char *isopath)
grub_file_t isofile;
char uuid[64] = {0};
ventoy_img_chunk *chunk;
grub_uint8_t disk_sig[4];
grub_uint8_t disk_guid[16];
debug("ventoy_freebsd_append_conf %s\n", isopath);
@ -209,7 +210,7 @@ static int ventoy_freebsd_append_conf(char *buf, const char *isopath)
disk = isofile->device->disk;
ventoy_get_disk_guid(isofile->name, disk_guid);
ventoy_get_disk_guid(isofile->name, disk_guid, disk_sig);
for (i = 0; i < 16; i++)
{
@ -217,7 +218,8 @@ static int ventoy_freebsd_append_conf(char *buf, const char *isopath)
}
vtoy_ssprintf(buf, pos, "hint.ventoy.0.disksize=%llu\n", (ulonglong)(disk->total_sectors * (1 << disk->log_sector_size)));
vtoy_ssprintf(buf, pos, "hint.ventoy.0.diskuuid=\"%s\"\n", uuid);
vtoy_ssprintf(buf, pos, "hint.ventoy.0.diskuuid=\"%s\"\n", uuid);
vtoy_ssprintf(buf, pos, "hint.ventoy.0.disksignature=%02x%02x%02x%02x\n", disk_sig[0], disk_sig[1], disk_sig[2], disk_sig[3]);
vtoy_ssprintf(buf, pos, "hint.ventoy.0.segnum=%u\n", g_img_chunk_list.cur_chunk);
for (i = 0; i < g_img_chunk_list.cur_chunk; i++)

@ -273,3 +273,196 @@ grub_err_t ventoy_cmd_load_vhdboot(grub_extcmd_context_t ctxt, int argc, char **
return 0;
}
grub_err_t ventoy_cmd_get_vtoy_type(grub_extcmd_context_t ctxt, int argc, char **args)
{
int i;
int offset = -1;
grub_file_t file;
vhd_footer_t vhdfoot;
VDIPREHEADER vdihdr;
char type[16] = {0};
ventoy_mbr_head mbr;
ventoy_gpt_info *gpt;
(void)ctxt;
if (argc != 4)
{
return 0;
}
file = grub_file_open(args[0], VENTOY_FILE_TYPE);
if (!file)
{
debug("Failed to open file %s\n", args[0]);
return 0;
}
grub_snprintf(type, sizeof(type), "unknown");
grub_file_seek(file, file->size - 512);
grub_file_read(file, &vhdfoot, sizeof(vhdfoot));
if (grub_strncmp(vhdfoot.cookie, "conectix", 8) == 0)
{
offset = 0;
grub_snprintf(type, sizeof(type), "vhd%u", grub_swap_bytes32(vhdfoot.disktype));
}
else
{
grub_file_seek(file, 0);
grub_file_read(file, &vdihdr, sizeof(vdihdr));
if (vdihdr.u32Signature == VDI_IMAGE_SIGNATURE &&
grub_strncmp(vdihdr.szFileInfo, VDI_IMAGE_FILE_INFO, grub_strlen(VDI_IMAGE_FILE_INFO)) == 0)
{
offset = 2 * 1048576;
grub_snprintf(type, sizeof(type), "vdi");
}
else
{
offset = 0;
grub_snprintf(type, sizeof(type), "raw");
}
}
grub_env_set(args[1], type);
debug("<%s> vtoy type: <%s> ", args[0], type);
if (offset >= 0)
{
grub_file_seek(file, offset);
grub_file_read(file, &mbr, sizeof(mbr));
if (mbr.Byte55 != 0x55 || mbr.ByteAA != 0xAA)
{
grub_env_set(args[1], "unknown");
debug("invalid mbr signature: 0x%x 0x%x\n", mbr.Byte55, mbr.ByteAA);
goto end;
}
if (mbr.PartTbl[0].FsFlag == 0xEE)
{
grub_env_set(args[2], "gpt");
debug("part type: %s\n", "GPT");
gpt = grub_zalloc(sizeof(ventoy_gpt_info));
if (gpt)
{
grub_file_seek(file, offset);
grub_file_read(file, gpt, sizeof(ventoy_gpt_info));
for (i = 0; i < 128; i++)
{
if (grub_memcmp(gpt->PartTbl[i].PartType, "Hah!IdontNeedEFI", 16) == 0)
{
debug("part %d is grub_bios part\n", i);
grub_env_set(args[3], "1");
break;
}
else if (gpt->PartTbl[i].LastLBA == 0)
{
break;
}
}
grub_free(gpt);
}
}
else
{
grub_env_set(args[2], "mbr");
debug("part type: %s\n", "MBR");
}
}
else
{
debug("part type: %s\n", "xxx");
}
end:
grub_file_close(file);
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}
grub_err_t ventoy_cmd_raw_chain_data(grub_extcmd_context_t ctxt, int argc, char **args)
{
grub_uint32_t size = 0;
grub_uint32_t img_chunk_size = 0;
grub_file_t file;
grub_disk_t disk;
const char *pLastChain = NULL;
ventoy_chain_head *chain;
char envbuf[64];
(void)ctxt;
(void)argc;
if (NULL == g_img_chunk_list.chunk)
{
grub_printf("ventoy not ready\n");
return 1;
}
file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s", args[0]);
if (!file)
{
return 1;
}
img_chunk_size = g_img_chunk_list.cur_chunk * sizeof(ventoy_img_chunk);
size = sizeof(ventoy_chain_head) + img_chunk_size;
pLastChain = grub_env_get("vtoy_chain_mem_addr");
if (pLastChain)
{
chain = (ventoy_chain_head *)grub_strtoul(pLastChain, NULL, 16);
if (chain)
{
debug("free last chain memory %p\n", chain);
grub_free(chain);
}
}
chain = grub_malloc(size);
if (!chain)
{
grub_printf("Failed to alloc chain memory size %u\n", size);
grub_file_close(file);
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);
grub_env_export("vtoy_chain_mem_addr");
grub_env_export("vtoy_chain_mem_size");
grub_memset(chain, 0, sizeof(ventoy_chain_head));
/* part 1: os parameter */
g_ventoy_chain_type = ventoy_chain_linux;
ventoy_fill_os_param(file, &(chain->os_param));
/* part 2: chain head */
disk = file->device->disk;
chain->disk_drive = disk->id;
chain->disk_sector_size = (1 << disk->log_sector_size);
chain->real_img_size_in_bytes = file->size;
chain->virt_img_size_in_bytes = (file->size + 2047) / 2048 * 2048;
chain->boot_catalog = 0;
/* part 3: image chunk */
chain->img_chunk_offset = sizeof(ventoy_chain_head);
chain->img_chunk_num = g_img_chunk_list.cur_chunk;
grub_memcpy((char *)chain + chain->img_chunk_offset, g_img_chunk_list.chunk, img_chunk_size);
grub_file_seek(file, 0);
grub_file_read(file, chain->boot_catalog_sector, 512);
grub_file_close(file);
VENTOY_CMD_RETURN(GRUB_ERR_NONE);
}

@ -71,7 +71,7 @@ typedef struct ventoy_image_location
{
ventoy_guid guid;
/* image sector size, currently this value is always 2048 */
/* image sector size, 2048/512 */
grub_uint32_t image_sector_size;
/* disk sector size, normally the value is 512 */
@ -125,7 +125,9 @@ typedef struct ventoy_os_param
*/
grub_uint8_t vtoy_reserved[32]; // Internal use by ventoy
grub_uint8_t reserved[31];
grub_uint8_t vtoy_disk_signature[4];
grub_uint8_t reserved[27];
}ventoy_os_param;

@ -0,0 +1,66 @@
#!/ventoy/busybox/sh
#************************************************************************************
# Copyright (c) 2020, longpanda <admin@ventoy.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
#************************************************************************************
. /ventoy/hook/ventoy-hook-lib.sh
vtlog "######### $0 $* ############"
if is_ventoy_hook_finished; then
exit 0
fi
wait_for_usb_disk_ready
vtdiskname=$(get_ventoy_disk_name)
if [ "$vtdiskname" = "unknown" ]; then
vtlog "ventoy disk not found"
exit 0
fi
ventoy_udev_disk_common_hook "${vtdiskname#/dev/}2" "noreplace"
blkdev_num=$($VTOY_PATH/tool/dmsetup ls | grep ventoy | sed 's/.*(\([0-9][0-9]*\),.*\([0-9][0-9]*\).*/\1:\2/')
vtDM=$(ventoy_find_dm_id ${blkdev_num})
vtlog "blkdev_num=$blkdev_num vtDM=$vtDM ..."
while [ -n "Y" ]; do
if [ -b /dev/$vtDM ]; then
break
else
sleep 0.3
fi
done
if [ -n "$1" ]; then
vtlog "ln -s /dev/$vtDM $1"
ln -s /dev/$vtDM "$1"
else
vtLABEL=$($BUSYBOX_PATH/blkid /dev/$vtDM | $SED 's/.*LABEL="\([^"]*\)".*/\1/')
vtlog "vtLABEL is $vtLABEL"
if [ -z "$vtLABEL" ]; then
vtLABEL=$($SED "s/.*label=\([^ ]*\)/\1/" /proc/cmdline)
vtlog "vtLABEL is $vtLABEL from cmdline"
fi
ln -s /dev/$vtDM "/dev/disk/by-label/$vtLABEL"
fi
# OK finish
set_ventoy_hook_finish

@ -28,8 +28,22 @@ else
$CAT $VTOY_PATH/hook/default/13-dm-disk.rules > "$DISTRO_UDEV_DIR/13-dm-disk.rules"
fi
if $GREP -q '^mount_setup$' init; then
if $GREP -q '^"$mount_handler"' /init; then
echo 'use mount_handler ...' >> $VTLOG
vthookfile=/hooks/archiso
if [ -e /hooks/miso ]; then
vthookfile=/hooks/miso
$SED "/^\"\$mount_handler\"/i\ $BUSYBOX_PATH/sh $VTOY_PATH/hook/manjaro/ventoy-disk.sh \"\$misodevice\"" -i /init
else
$SED "/^\"\$mount_handler\"/i\ $BUSYBOX_PATH/sh $VTOY_PATH/hook/manjaro/ventoy-disk.sh \"\$archisodevice\"" -i /init
fi
if [ -f $vthookfile ]; then
$SED '/while ! poll_device "${dev}"/a\ if /ventoy/busybox/sh /ventoy/hook/manjaro/ventoy-timeout.sh ${dev}; then break; fi' -i $vthookfile
fi
elif $GREP -q '^mount_setup$' init; then
echo "Here use notify ..." >> $VTLOG
ventoy_set_inotify_script manjaro/ventoy-inotifyd-hook.sh

@ -87,7 +87,13 @@ wait_for_usb_disk_ready() {
usb_disk=$(get_ventoy_disk_name)
vtlog "wait_for_usb_disk_ready $usb_disk ..."
if [ -e "${usb_disk}2" ]; then
if echo $usb_disk | $EGREP -q "nvme|mmc"; then
vtpart2=${usb_disk}p2
else
vtpart2=${usb_disk}2
fi
if [ -e "${vtpart2}" ]; then
vtlog "wait_for_usb_disk_ready $usb_disk finish"
break
else
@ -105,7 +111,13 @@ is_ventoy_disk() {
}
not_ventoy_disk() {
if $VTOY_PATH/tool/vtoydump -f $VTOY_PATH/ventoy_os_param -c "$1"; then
if echo $1 | $EGREP -q "nvme.*p$|mmc.*p$"; then
vtDiskName=${1:0:-1}
else
vtDiskName=$1
fi
if $VTOY_PATH/tool/vtoydump -f $VTOY_PATH/ventoy_os_param -c "$vtDiskName"; then
$BUSYBOX_PATH/false
else
$BUSYBOX_PATH/true
@ -463,9 +475,12 @@ ventoy_create_persistent_link() {
fi
}
ventoy_udev_disk_common_hook() {
VTDISK="${1:0:-1}"
ventoy_udev_disk_common_hook() {
if echo $1 | $EGREP -q "nvme.*p[0-9]$|mmc.*p[0-9]$"; then
VTDISK="${1:0:-2}"
else
VTDISK="${1:0:-1}"
fi
if [ -e /vtoy/vtoy ]; then
VTRWMOD=""
@ -533,8 +548,14 @@ is_inotify_ventoy_part() {
if echo $1 | $GREP -q "2$"; then
if ! [ -e /sys/block/$1 ]; then
if [ -e /sys/class/block/$1 ]; then
if [ -e /dev/${1:0:-1} ]; then
$VTOY_PATH/tool/vtoydump -f $VTOY_PATH/ventoy_os_param -c ${1:0:-1}
if echo $1 | $EGREP -q "nvme|mmc"; then
vtShortName=${1:0:-2}
else
vtShortName=${1:0:-1}
fi
if [ -e /dev/$vtShortName ]; then
$VTOY_PATH/tool/vtoydump -f $VTOY_PATH/ventoy_os_param -c $vtShortName
return
fi
fi

Binary file not shown.

@ -252,7 +252,11 @@ function distro_specify_initrd_file_phase2 {
if [ "$grub_platform" != "pc" ]; then
vt_add_replace_file 0 "minimal\\x86_64\\rootfs.xz"
fi
elif [ -f (loop)/arch/boot/x86_64/archiso.img ]; then
vt_linux_specify_initrd_file /arch/boot/x86_64/archiso.img
if [ "$grub_platform" != "pc" ]; then
vt_add_replace_file 0 "EFI\\archiso\\archiso.img"
fi
fi
}
@ -440,6 +444,7 @@ function uefi_windows_menu_func {
}
function uefi_linux_menu_func {
if [ "$ventoy_compatible" = "NO" ]; then
if [ "$ventoy_fs_probe" = "udf" ]; then
@ -493,6 +498,12 @@ function uefi_linux_menu_func {
elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
fi
elif [ -d (loop)/parabola ]; then
if [ -f (loop)/parabola/boot/x86_64/parabola.img ]; then
vt_add_replace_file $vtindex "EFI\\parabola\\parabola.img"
elif [ -f (loop)/boot/initramfs_x86_64.img ]; then
vt_add_replace_file $vtindex "boot\\initramfs_x86_64.img"
fi
elif [ -f (loop)/EFI/BOOT/initrd.gz ]; then
vt_add_replace_file $vtindex "EFI\\BOOT\\initrd.gz"
elif [ -f (loop)/loader/entries/thinstation.conf ]; then
@ -508,7 +519,7 @@ function uefi_linux_menu_func {
fi
elif [ -d (loop)/EFI/BOOT/entries ]; then
if [ -f (loop)/parabola/boot/x86_64/parabolaiso.img ]; then
vt_add_replace_file 0 "EFI\\parabolaiso\\parabolaiso.img"
vt_add_replace_file 0 "EFI\\parabolaiso\\parabolaiso.img"
fi
elif [ -e (loop)/syslinux/alt0/full.cz ]; then
vt_add_replace_file 0 "EFI\\BOOT\\full.cz"
@ -646,9 +657,7 @@ function legacy_windows_menu_func {
ventoy_debug_pause
if [ -n "$vtoy_chain_mem_addr" ]; then
linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
set gfxmode=1920x1080,1366x768,1024x768,800x600,auto
terminal_output gfxterm
linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} ibft mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
boot
else
echo "chain empty failed"
@ -900,9 +909,7 @@ function wim_common_menuentry {
if [ -n "$vtoy_chain_mem_addr" ]; then
if [ "$grub_platform" = "pc" ]; then
linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
set gfxmode=1920x1080,1366x768,1024x768,800x600,auto
terminal_output gfxterm
linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
else
ventoy_cli_console
chainloader ${vtoy_path}/ventoy_x64.efi env_param=${env_param} isoefi=${LoadIsoEfiDriver} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
@ -945,7 +952,6 @@ function efi_unsupport_menuentry {
common_unsupport_menuentry
}
function vhd_common_menuentry {
if [ "$VTOY_VHD_NO_WARNING" != "1" ]; then
@ -959,17 +965,14 @@ function vhd_common_menuentry {
fi
vt_chosen_img_path vt_chosen_path vt_chosen_size
vt_patch_vhdboot ${vt_chosen_path}
vt_patch_vhdboot $vt_chosen_path
ventoy_debug_pause
if [ -n "$vtoy_vhd_buf_addr" ]; then
if [ "$grub_platform" = "pc" ]; then
linux16 $vtoy_path/memdisk iso raw
initrd16 mem:${vtoy_vhd_buf_addr}:size:${vtoy_vhd_buf_size}
set gfxmode=1920x1080,1366x768,1024x768,800x600,auto
terminal_output gfxterm
initrd16 mem:${vtoy_vhd_buf_addr}:size:${vtoy_vhd_buf_size}
boot
else
ventoy_cli_console
@ -978,7 +981,7 @@ function vhd_common_menuentry {
ventoy_gui_console
fi
else
echo "Failed to boot vhd file"
echo "Please put the right ventoy_vhdboot.img file to the 1st partition"
ventoy_pause
fi
}
@ -987,6 +990,68 @@ function vhd_unsupport_menuentry {
common_unsupport_menuentry
}
function vtoyboot_common_func {
set efigrubpart=0
set vtoysupport=0
vt_get_vtoy_type ${1} vtoytype parttype efigrubpart
if vt_str_begin $vtoytype vhd; then
set vtoysupport=1
elif [ "$vtoytype" = "raw" ]; then
set vtoysupport=1
fi
if [ $vtoysupport -eq 1 ]; then
if [ "$grub_platform" = "pc" ]; then
if [ "$parttype" = "gpt" -a $efigrubpart -eq 0 ]; then
echo "The OS in the vdisk was created in UEFI mode, but current is Legacy BIOS mode."
echo "虚拟磁盘内的系统是在UEFI模式下创建的而当前系统是Legacy BIOS模式可能无法正常启动。"
ventoy_pause
fi
else
if [ "$parttype" = "mbr" ]; then
echo "The OS in the vdisk was created in Legacy BIOS mode, but current is UEFI mode."
echo "虚拟磁盘内的系统是在Legacy BIOS模式下创建的而当前系统是UEFI模式可能无法正常启动。"
ventoy_pause
fi
fi
vt_img_sector ${1}
vt_raw_chain_data ${1}
ventoy_debug_pause
if [ -n "$vtoy_chain_mem_addr" ]; then
if [ "$grub_platform" = "pc" ]; then
vt_acpi_param ${vtoy_chain_mem_addr} 512
linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} sector512 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
boot
else
ventoy_cli_console
chainloader ${vtoy_path}/ventoy_x64.efi sector512 env_param=${ventoy_env_param} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
boot
ventoy_gui_console
fi
else
echo "chain empty failed!"
ventoy_pause
fi
else
echo "Unsupported vtoy type $vtoytype"
ventoy_pause
fi
}
function vtoy_common_menuentry {
vt_chosen_img_path vt_chosen_path vt_chosen_size
vtoyboot_common_func ${vtoy_iso_part}${vt_chosen_path}
}
function vtoy_unsupport_menuentry {
common_unsupport_menuentry
}
#
#============================================================#
# IMG file boot process #
@ -1228,10 +1293,11 @@ function img_common_menuentry {
ventoy_img_memtest86
fi
else
vt_linux_chain_data ${vtoy_iso_part}${vt_chosen_path}
if [ "$grub_platform" = "pc" ]; then
img_unsupport_tip
else
vt_linux_chain_data ${vtoy_iso_part}${vt_chosen_path}
linux16 $vtoy_path/ipxe.krn ${vtdebug_flag} sector512 mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
boot
else
chainloader ${vtoy_path}/ventoy_x64.efi sector512 env_param=${env_param} isoefi=${LoadIsoEfiDriver} FirstTry=${FirstTryBootFile} ${vtdebug_flag} mem:${vtoy_chain_mem_addr}:size:${vtoy_chain_mem_size}
boot
fi
@ -1355,6 +1421,7 @@ else
set gfxpayload=keep
fi
if [ "$vtoy_display_mode" = "CLI" ]; then
terminal_output console
elif [ "$vtoy_display_mode" = "serial" ]; then
@ -1382,14 +1449,14 @@ fi
export theme
export gfxmode
export vtoydev
export vtoy_path
export vtdebug_flag
export vtoy_iso_part
export vtoy_efi_part
export VENTOY_VERSION
export VTOY_CUR_VIDEO_MODE
#colect all image files (iso files)
set ventoy_img_count=0
vt_list_img $vtoy_iso_part ventoy_img_count

Binary file not shown.

@ -122,7 +122,7 @@ crypto:
part_bsd: part_msdos
cs5536: pci
biosdisk:
ventoy: ext2 fshelp font crypto exfat udf extcmd normal video gcry_sha1 relocator iso9660
ventoy: ext2 fshelp font crypto exfat udf extcmd normal video gcry_sha1 relocator iso9660 acpi
lsapm:
gcry_sha512: crypto
password: crypto normal

@ -119,7 +119,7 @@ ehci: cs5536 usb boot
crypto:
part_bsd: part_msdos
cs5536:
ventoy: ext2 fshelp crypto font exfat udf extcmd normal video gcry_sha1 iso9660
ventoy: ext2 fshelp font crypto exfat udf extcmd normal video gcry_sha1 iso9660
gcry_sha512: crypto
password: crypto normal
fshelp:

@ -25,6 +25,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ventoy.h>
int g_debug = 0;
int g_hddmode = 0;
char *g_cmdline_copy;
void *g_initrd_addr;
size_t g_initrd_len;
@ -55,6 +56,40 @@ uint64 g_fixup_iso9660_secover_tot_secs = 0;
static struct int13_disk_address __bss16 ( ventoy_address );
#define ventoy_address __use_data16 ( ventoy_address )
static uint64_t ventoy_remap_lba_hdd(uint64_t lba, uint32_t *count)
{
uint32_t i;
uint32_t max_sectors;
ventoy_img_chunk *cur;
if ((NULL == g_cur_chunk) || (lba < g_cur_chunk->img_start_sector) ||
(lba > g_cur_chunk->img_end_sector))
{
g_cur_chunk = NULL;
for (i = 0; i < g_img_chunk_num; i++)
{
cur = g_chunk + i;
if (lba >= cur->img_start_sector && lba <= cur->img_end_sector)
{
g_cur_chunk = cur;
break;
}
}
}
if (g_cur_chunk)
{
max_sectors = g_cur_chunk->img_end_sector - lba + 1;
if (*count > max_sectors)
{
*count = max_sectors;
}
return g_cur_chunk->disk_start_sector + (lba - g_cur_chunk->img_start_sector);
}
return lba;
}
static uint64_t ventoy_remap_lba(uint64_t lba, uint32_t *count)
{
uint32_t i;
@ -92,6 +127,72 @@ static uint64_t ventoy_remap_lba(uint64_t lba, uint32_t *count)
return lba;
}
static int ventoy_vdisk_read_real_hdd(uint64_t lba, unsigned int count, unsigned long buffer)
{
uint32_t left = 0;
uint32_t readcount = 0;
uint32_t tmpcount = 0;
uint16_t status = 0;
uint64_t curlba = 0;
uint64_t maplba = 0;
unsigned long phyaddr;
curlba = lba;
left = count;
#if VTOY_DEBUG
printf("ventoy_vdisk_read_real_hdd: %llu %u\n", lba, count);
#endif
while (left > 0)
{
readcount = left;
maplba = ventoy_remap_lba_hdd(curlba, &readcount);
tmpcount = readcount;
phyaddr = user_to_phys(buffer, 0);
while (tmpcount > 0)
{
/* Use INT 13, 42 to read the data from real disk */
ventoy_address.lba = maplba;
ventoy_address.buffer.segment = (uint16_t)(phyaddr >> 4);
ventoy_address.buffer.offset = (uint16_t)(phyaddr & 0x0F);
if (tmpcount >= 64) /* max sectors per transmit */
{
ventoy_address.count = 64;
tmpcount -= 64;
maplba += 64;
phyaddr += 32768;
}
else
{
ventoy_address.count = tmpcount;
tmpcount = 0;
}
__asm__ __volatile__ ( REAL_CODE ( "stc\n\t"
"sti\n\t"
"int $0x13\n\t"
"sti\n\t" /* BIOS bugs */
"jc 1f\n\t"
"xorw %%ax, %%ax\n\t"
"\n1:\n\t" )
: "=a" ( status )
: "a" ( 0x4200 ), "d" ( VENTOY_BIOS_FAKE_DRIVE ),
"S" ( __from_data16 ( &ventoy_address ) ) );
}
curlba += readcount;
left -= readcount;
buffer += (readcount * 512);
}
return 0;
}
static int ventoy_vdisk_read_real(uint64_t lba, unsigned int count, unsigned long buffer)
{
uint32_t i = 0;
@ -297,6 +398,13 @@ int ventoy_vdisk_read(struct san_device *sandev, uint64_t lba, unsigned int coun
ix86 = (struct i386_all_regs *)sandev->x86_regptr;
if (g_hddmode)
{
ventoy_vdisk_read_real_hdd(lba, count, buffer);
ix86->regs.dl = sandev->drive;
return 0;
}
/* Workaround for SSTR PE loader error */
if (g_fixup_iso9660_secover_start)
{
@ -441,9 +549,12 @@ static void ventoy_dump_chain(ventoy_chain_head *chain)
uint32_t i = 0;
uint8_t chksum = 0;
uint8_t *guid;
uint8_t *sig;
uint8_t *vtoy_reserve;
guid = chain->os_param.vtoy_disk_guid;
sig = chain->os_param.vtoy_disk_signature;
for (i = 0; i < sizeof(ventoy_os_param); i++)
{
chksum += *((uint8_t *)(&(chain->os_param)) + i);
@ -457,6 +568,7 @@ static void ventoy_dump_chain(ventoy_chain_head *chain)
printf("os_param->chksum=0x%x (%s)\n", chain->os_param.chksum, chksum ? "FAILED" : "SUCCESS");
printf("os_param->vtoy_disk_guid=%02x%02x%02x%02x\n", guid[0], guid[1], guid[2], guid[3]);
printf("os_param->vtoy_disk_signature=%02x%02x%02x%02x\n", sig[0], sig[1], sig[2], sig[3]);
printf("os_param->vtoy_disk_size=%llu\n", chain->os_param.vtoy_disk_size);
printf("os_param->vtoy_disk_part_id=%u\n", chain->os_param.vtoy_disk_part_id);
printf("os_param->vtoy_disk_part_type=%u\n", chain->os_param.vtoy_disk_part_type);
@ -530,19 +642,33 @@ static int ventoy_update_image_location(ventoy_os_param *param)
}
memcpy(&location->guid, &param->guid, sizeof(ventoy_guid));
location->image_sector_size = 2048;
location->image_sector_size = g_hddmode ? 512 : 2048;
location->disk_sector_size = g_chain->disk_sector_size;
location->region_count = g_img_chunk_num;
region = location->regions;
for (i = 0; i < g_img_chunk_num; i++)
if (g_hddmode)
{
for (i = 0; i < g_img_chunk_num; i++)
{
region->image_sector_count = chunk->disk_end_sector - chunk->disk_start_sector + 1;
region->image_start_sector = chunk->img_start_sector * 4;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
}
else
{
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
region->image_start_sector = chunk->img_start_sector;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
for (i = 0; i < g_img_chunk_num; i++)
{
region->image_sector_count = chunk->img_end_sector - chunk->img_start_sector + 1;
region->image_start_sector = chunk->img_start_sector;
region->disk_start_sector = chunk->disk_start_sector;
region++;
chunk++;
}
}
return 0;
@ -553,6 +679,7 @@ int ventoy_boot_vdisk(void *data)
uint8_t chksum = 0;
unsigned int i;
unsigned int drive;
ventoy_img_chunk *cur;
(void)data;
@ -562,8 +689,14 @@ int ventoy_boot_vdisk(void *data)
{
g_debug = 1;
printf("### ventoy chain boot begin... ###\n");
printf("cmdline: <%s>\n", g_cmdline_copy);
ventoy_debug_pause();
}
if (strstr(g_cmdline_copy, "sector512"))
{
g_hddmode = 1;
}
g_chain = (ventoy_chain_head *)g_initrd_addr;
g_chunk = (ventoy_img_chunk *)((char *)g_chain + g_chain->img_chunk_offset);
@ -601,6 +734,16 @@ int ventoy_boot_vdisk(void *data)
ventoy_dump_chain(g_chain);
}
if (g_hddmode)
{
for (i = 0; i < g_img_chunk_num; i++)
{
cur = g_chunk + i;
cur->img_start_sector *= 4;
cur->img_end_sector = cur->img_end_sector * 4 + 3;
}
}
drive = ventoy_int13_hook(g_chain);
if (g_debug)

@ -1051,6 +1051,10 @@ static __asmcall void int13 ( struct i386_all_regs *ix86 ) {
/* We simulate a cdrom, so no need to sync hd drive number */
//int13_check_num_drives();
#if VTOY_DEBUG
printf("int13 0x%x 0x%x\n", bios_drive, command); sleep(1);
#endif
if (bios_drive == VENTOY_BIOS_FAKE_DRIVE)
{
ix86->regs.dl = g_sandev->exdrive;
@ -1255,39 +1259,15 @@ static void int13_hook_vector ( void ) {
* @ret rc Return status code
*/
static int int13_load_mbr ( unsigned int drive, struct segoff *address ) {
uint16_t status;
int discard_b, discard_c, discard_d;
uint16_t magic;
/* Use INT 13, 02 to read the MBR */
address->segment = 0;
address->offset = 0x7c00;
__asm__ __volatile__ ( REAL_CODE ( "pushw %%es\n\t"
"pushl %%ebx\n\t"
"popw %%bx\n\t"
"popw %%es\n\t"
"stc\n\t"
"sti\n\t"
"int $0x13\n\t"
"sti\n\t" /* BIOS bugs */
"jc 1f\n\t"
"xorw %%ax, %%ax\n\t"
"\n1:\n\t"
"popw %%es\n\t" )
: "=a" ( status ), "=b" ( discard_b ),
"=c" ( discard_c ), "=d" ( discard_d )
: "a" ( 0x0201 ), "b" ( *address ),
"c" ( 1 ), "d" ( drive ) );
if ( status ) {
DBG ( "INT13 drive %02x could not read MBR (status %04x)\n",
drive, status );
return -EIO;
}
address->segment = 0;
address->offset = 0x7c00;
copy_to_real(address->segment, address->offset, g_sandev->boot_catalog_sector, 512);
/* Check magic signature */
get_real ( magic, address->segment,
( address->offset +
offsetof ( struct master_boot_record, magic ) ) );
get_real ( magic, address->segment, (address->offset + offsetof ( struct master_boot_record, magic ) ) );
if ( magic != INT13_MBR_MAGIC ) {
DBG ( "INT13 drive %02x does not contain a valid MBR\n",
drive );
@ -1443,8 +1423,14 @@ unsigned int ventoy_int13_hook (ventoy_chain_head *chain)
/* hook will copy num_drives to dl when int13 08 was called, so must initialize it's value */
get_real(num_drives, BDA_SEG, BDA_NUM_DRIVES);
//natural_drive = num_drives | 0x80;
natural_drive = 0xE0; /* just set a cdrom drive number 224 */
if (g_hddmode)
{
natural_drive = num_drives | 0x80;
}
else
{
natural_drive = 0xE0; /* just set a cdrom drive number 224 */
}
if (chain->disk_drive >= 0x80 && chain->drive_map >= 0x80)
{
@ -1456,8 +1442,8 @@ unsigned int ventoy_int13_hook (ventoy_chain_head *chain)
g_sandev = zalloc(sizeof(struct san_device) + sizeof(struct int13_data));
g_sandev->priv = int13 = (struct int13_data *)(g_sandev + 1);
g_sandev->drive = int13->natural_drive = natural_drive;
g_sandev->is_cdrom = 1;
g_sandev->blksize_shift = 2;
g_sandev->is_cdrom = g_hddmode ? 0 : 1;
g_sandev->blksize_shift = g_hddmode ? 0 : 2;
g_sandev->capacity.blksize = 512;
g_sandev->capacity.blocks = chain->virt_img_size_in_bytes / 512;
g_sandev->exdrive = chain->disk_drive;
@ -1521,9 +1507,20 @@ int ventoy_int13_boot ( unsigned int drive, void *imginfo, const char *cmdline)
struct ibft_table *ibft = NULL;
/* Look for a usable boot sector */
if ( ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) &&
( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ))
if (g_hddmode)
{
if ((rc = int13_load_mbr(drive, &address)) != 0)
{
printf("int13_load_mbr %d\n", rc);
return rc;
}
}
else
{
if ( ( ( rc = int13_load_eltorito ( drive, &address ) ) != 0 ) &&
( ( rc = int13_load_mbr ( drive, &address ) ) != 0 ))
return rc;
}
if (imginfo)
{

@ -4,6 +4,8 @@
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
//#define VTOY_DEBUG 1
#define grub_uint64_t uint64_t
#define grub_uint32_t uint32_t
#define grub_uint16_t uint16_t
@ -43,7 +45,7 @@ typedef struct ventoy_image_location
{
ventoy_guid guid;
/* image sector size, currently this value is always 2048 */
/* image sector size, 2048/512 */
grub_uint32_t image_sector_size;
/* disk sector size, normally the value is 512 */
@ -86,7 +88,9 @@ typedef struct ventoy_os_param
grub_uint64_t vtoy_reserved[4]; // Internal use by ventoy
grub_uint8_t reserved[31];
grub_uint8_t vtoy_disk_signature[4];
grub_uint8_t reserved[27];
}ventoy_os_param;
typedef struct ventoy_iso9660_override
@ -182,6 +186,7 @@ typedef struct ventoy_sector_flag
#define VENTOY_BOOT_FIXBIN_DRIVE 0xFD
extern int g_debug;
extern int g_hddmode;
extern char *g_cmdline_copy;
extern void *g_initrd_addr;
extern size_t g_initrd_len;

@ -20,7 +20,7 @@ get_disk_size() {
enum_disk() {
id=1
rm -f /device.list
ls /sys/block/ | grep 'd[a-z]' | while read dev; do
ls /sys/block/ | egrep 'd[a-z]|nvme|mmc' | while read dev; do
if ! [ -b /dev/$dev ]; then
continue
fi
@ -145,7 +145,7 @@ ventoy_configuration() {
elif [ "$Answer" = "1" ]; then
while true; do
read -p "Please input the preserve space in MB: " Answer
if echo $Answer | grep -q "^[1-9][0-9]*$"; then
if echo $Answer | grep -q "^[0-9][0-9]*$"; then
echo $Answer > /preserve.txt
break
fi

@ -507,9 +507,18 @@ static int vtoydm_print_linear_table(const char *img_map_file, const char *diskn
(sector_start << 2), disk_sector_num,
diskname, (unsigned long long)chunk[i].disk_start_sector);
#else
printf("%u %u linear %s1 %llu\n",
if (strstr(diskname, "nvme") || strstr(diskname, "mmc"))
{
printf("%u %u linear %sp1 %llu\n",
(sector_start << 2), disk_sector_num,
diskname, (unsigned long long)chunk[i].disk_start_sector - 2048);
}
else
{
printf("%u %u linear %s1 %llu\n",
(sector_start << 2), disk_sector_num,
diskname, (unsigned long long)chunk[i].disk_start_sector - 2048);
}
#endif
}

@ -120,7 +120,9 @@ typedef struct ventoy_os_param
uint64_t vtoy_reserved[4]; // Internal use by ventoy
uint8_t reserved[31];
uint8_t vtoy_disk_signature[4];
uint8_t reserved[27];
}ventoy_os_param;
#pragma pack()
@ -218,6 +220,11 @@ static void vtoy_dump_os_param(ventoy_os_param *param)
printf("param->vtoy_disk_guid = %02x %02x %02x %02x\n",
param->vtoy_disk_guid[0], param->vtoy_disk_guid[1],
param->vtoy_disk_guid[2], param->vtoy_disk_guid[3]);
printf("param->vtoy_disk_signature = %02x %02x %02x %02x\n",
param->vtoy_disk_signature[0], param->vtoy_disk_signature[1],
param->vtoy_disk_signature[2], param->vtoy_disk_signature[3]);
printf("param->vtoy_disk_size = %llu\n", (unsigned long long)param->vtoy_disk_size);
printf("param->vtoy_disk_part_id = %u\n", param->vtoy_disk_part_id);
printf("param->vtoy_disk_part_type = %u\n", param->vtoy_disk_part_type);
@ -231,7 +238,7 @@ static void vtoy_dump_os_param(ventoy_os_param *param)
printf("\n");
}
static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid)
static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid, uint8_t *vtsig)
{
int i = 0;
int fd = 0;
@ -244,6 +251,9 @@ static int vtoy_get_disk_guid(const char *diskname, uint8_t *vtguid)
{
lseek(fd, 0x180, SEEK_SET);
read(fd, vtguid, 16);
lseek(fd, 0x1b8, SEEK_SET);
read(fd, vtsig, 4);
close(fd);
debug("GUID for %s: <", devdisk);
@ -380,13 +390,14 @@ static int vtoy_find_disk_by_size(unsigned long long size, char *diskname)
return rc;
}
static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname)
static int vtoy_find_disk_by_guid(ventoy_os_param *param, char *diskname)
{
int rc = 0;
int count = 0;
DIR* dir = NULL;
struct dirent* p = NULL;
uint8_t vtguid[16];
uint8_t vtsig[16];
dir = opendir("/sys/block");
if (!dir)
@ -403,8 +414,9 @@ static int vtoy_find_disk_by_guid(uint8_t *guid, char *diskname)
}
memset(vtguid, 0, sizeof(vtguid));
rc = vtoy_get_disk_guid(p->d_name, vtguid);
if (rc == 0 && memcmp(vtguid, guid, 16) == 0)
rc = vtoy_get_disk_guid(p->d_name, vtguid, vtsig);
if (rc == 0 && memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 &&
memcmp(vtsig, param->vtoy_disk_signature, 4) == 0)
{
sprintf(diskname, "%s", p->d_name);
count++;
@ -430,11 +442,11 @@ static int vtoy_print_os_param(ventoy_os_param *param, char *diskname)
cnt = vtoy_find_disk_by_size(param->vtoy_disk_size, diskname);
if (cnt > 1)
{
cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname);
cnt = vtoy_find_disk_by_guid(param, diskname);
}
else if (cnt == 0)
{
cnt = vtoy_find_disk_by_guid(param->vtoy_disk_guid, diskname);
cnt = vtoy_find_disk_by_guid(param, diskname);
debug("find 0 disk by size, try with guid cnt=%d...\n", cnt);
}
@ -462,17 +474,19 @@ static int vtoy_check_device(ventoy_os_param *param, const char *device)
{
unsigned long long size;
uint8_t vtguid[16] = {0};
uint8_t vtsig[4] = {0};
debug("vtoy_check_device for <%s>\n", device);
size = vtoy_get_disk_size_in_byte(device);
vtoy_get_disk_guid(device, vtguid);
vtoy_get_disk_guid(device, vtguid, vtsig);
debug("param->vtoy_disk_size=%llu size=%llu\n",
(unsigned long long)param->vtoy_disk_size, (unsigned long long)size);
if ((param->vtoy_disk_size == size || param->vtoy_disk_size == size + 512) &&
memcmp(vtguid, param->vtoy_disk_guid, 16) == 0)
memcmp(vtguid, param->vtoy_disk_guid, 16) == 0 &&
memcmp(vtsig, param->vtoy_disk_signature, 4) == 0)
{
debug("<%s> is right ventoy disk\n", device);
return 0;

Binary file not shown.

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save