Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ventoy committed Jul 3, 2020
1 parent 0ddc76a commit 1d034f0
Show file tree
Hide file tree
Showing 33 changed files with 2,361 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ static grub_env_get_pf grub_env_get = NULL;
ventoy_grub_param_file_replace *g_file_replace_list = NULL;
ventoy_efi_file_replace g_efi_file_replace;

BOOLEAN g_fix_windows_1st_cdrom_issue = FALSE;

STATIC BOOLEAN g_hook_keyboard = FALSE;

CHAR16 gFirstTryBootFile[256] = {0};
Expand Down Expand Up @@ -493,6 +495,7 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
EFI_STATUS Status = EFI_SUCCESS;
ventoy_grub_param *pGrubParam = NULL;
EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;
ventoy_chain_head *chain = NULL;

Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
if (EFI_ERROR(Status))
Expand Down Expand Up @@ -559,12 +562,15 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
);

pPos = StrStr(pCmdLine, L"mem:");
g_chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);
chain = (ventoy_chain_head *)StrHexToUintn(pPos + 4);

pPos = StrStr(pPos, L"size:");
size = StrDecimalToUintn(pPos + 5);

debug("memory addr:%p size:%lu", g_chain, size);
debug("memory addr:%p size:%lu", chain, size);

g_chain = AllocatePool(size);
CopyMem(g_chain, chain, size);

if (StrStr(pCmdLine, L"memdisk"))
{
Expand All @@ -583,12 +589,12 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
g_os_param_reserved = (UINT8 *)(g_chain->os_param.vtoy_reserved);

/* Workaround for Windows & ISO9660 */
if (g_os_param_reserved[2] == 1 && g_os_param_reserved[3] == 0)
if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[3] == 0)
{
g_fixup_iso9660_secover_enable = TRUE;
}

if (g_os_param_reserved[2] == 1 && g_os_param_reserved[4] != 1)
if (g_os_param_reserved[2] == ventoy_chain_windows && g_os_param_reserved[4] != 1)
{
g_hook_keyboard = TRUE;
}
Expand All @@ -613,6 +619,17 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
}
}

g_fix_windows_1st_cdrom_issue = FALSE;
if (ventoy_chain_windows == g_os_param_reserved[2] ||
ventoy_chain_wim == g_os_param_reserved[2])
{
if (ventoy_is_cdrom_dp_exist())
{
debug("fixup the 1st cdrom influences when boot windows ...");
g_fix_windows_1st_cdrom_issue = TRUE;
}
}

FreePool(pCmdLine);
return EFI_SUCCESS;
}
Expand All @@ -636,6 +653,44 @@ EFI_STATUS EFIAPI ventoy_clean_env(VOID)
FreePool((VOID *)(UINTN)g_chain->os_param.vtoy_img_location_addr);
}

FreePool(g_chain);

return EFI_SUCCESS;
}

STATIC EFI_STATUS ventoy_hook_start(VOID)
{
/* don't add debug print in this function */

if (g_fix_windows_1st_cdrom_issue)
{
ventoy_hook_1st_cdrom_start();
}

/* let this the last */
if (g_hook_keyboard)
{
ventoy_hook_keyboard_start();
}

return EFI_SUCCESS;
}

STATIC EFI_STATUS ventoy_hook_stop(VOID)
{
/* don't add debug print in this function */

if (g_fix_windows_1st_cdrom_issue)
{
ventoy_hook_1st_cdrom_stop();
}

/* let this the last */
if (g_hook_keyboard)
{
ventoy_hook_keyboard_stop();
}

return EFI_SUCCESS;
}

Expand Down Expand Up @@ -725,17 +780,11 @@ EFI_STATUS EFIAPI ventoy_boot(IN EFI_HANDLE ImageHandle)
pFile->OpenVolume = ventoy_wrapper_open_volume;
}

if (g_hook_keyboard)
{
ventoy_hook_keyboard_start();
}
ventoy_hook_start();
/* can't add debug print here */
//ventoy_wrapper_system();
Status = gBS->StartImage(Image, NULL, NULL);
if (g_hook_keyboard)
{
ventoy_hook_keyboard_stop();
}
ventoy_hook_stop();

if (EFI_ERROR(Status))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@

#define VENTOY_GUID { 0x77772020, 0x2e77, 0x6576, { 0x6e, 0x74, 0x6f, 0x79, 0x2e, 0x6e, 0x65, 0x74 }}

typedef enum ventoy_chain_type
{
ventoy_chain_linux = 0, /* 0: linux */
ventoy_chain_windows, /* 1: windows */
ventoy_chain_wim, /* 2: wim */

ventoy_chain_max
}ventoy_chain_type;

#pragma pack(1)

typedef struct ventoy_guid
Expand Down Expand Up @@ -216,6 +225,7 @@ if (gDebugPrint) \
}

typedef const char * (*grub_env_get_pf)(const char *name);
typedef int (*grub_env_printf_pf)(const char *fmt, ...);

#pragma pack(1)

Expand All @@ -242,8 +252,8 @@ typedef struct ventoy_grub_param_file_replace
typedef struct ventoy_grub_param
{
grub_env_get_pf grub_env_get;

ventoy_grub_param_file_replace file_replace;
grub_env_printf_pf grub_env_printf;
}ventoy_grub_param;

typedef struct ventoy_ram_disk
Expand Down Expand Up @@ -279,6 +289,18 @@ typedef struct ventoy_system_wrapper

EFI_OPEN_PROTOCOL NewOpenProtocol;
EFI_OPEN_PROTOCOL OriOpenProtocol;

EFI_LOCATE_HANDLE_BUFFER NewLocateHandleBuffer;
EFI_LOCATE_HANDLE_BUFFER OriLocateHandleBuffer;

EFI_PROTOCOLS_PER_HANDLE NewProtocolsPerHandle;
EFI_PROTOCOLS_PER_HANDLE OriProtocolsPerHandle;

EFI_LOCATE_HANDLE NewLocateHandle;
EFI_LOCATE_HANDLE OriLocateHandle;

EFI_LOCATE_DEVICE_PATH NewLocateDevicePath;
EFI_LOCATE_DEVICE_PATH OriLocateDevicePath;
} ventoy_system_wrapper;

#define ventoy_wrapper(bs, wrapper, func, newfunc) \
Expand Down Expand Up @@ -317,6 +339,7 @@ extern UINTN g_iso_buf_size;
extern ventoy_grub_param_file_replace *g_file_replace_list;
extern BOOLEAN g_fixup_iso9660_secover_enable;
extern EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *g_con_simple_input_ex;
extern BOOLEAN g_fix_windows_1st_cdrom_issue;

EFI_STATUS EFIAPI ventoy_wrapper_open_volume
(
Expand All @@ -327,6 +350,9 @@ EFI_STATUS EFIAPI ventoy_install_blockio(IN EFI_HANDLE ImageHandle, IN UINT64 Im
EFI_STATUS EFIAPI ventoy_wrapper_push_openvolume(IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_OPEN_VOLUME OpenVolume);
EFI_STATUS ventoy_hook_keyboard_start(VOID);
EFI_STATUS ventoy_hook_keyboard_stop(VOID);
BOOLEAN ventoy_is_cdrom_dp_exist(VOID);
EFI_STATUS ventoy_hook_1st_cdrom_start(VOID);
EFI_STATUS ventoy_hook_1st_cdrom_stop(VOID);

#endif

Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
#include <Protocol/SimpleFileSystem.h>
#include <Ventoy.h>

#define PROCOTOL_SLEEP_SECONDS 0
#define PROCOTOL_SLEEP_MSECONDS 0

#define debug_sleep() if (PROCOTOL_SLEEP_SECONDS) sleep(PROCOTOL_SLEEP_SECONDS)
#define debug_sleep() if (PROCOTOL_SLEEP_MSECONDS) gBS->Stall(1000 * PROCOTOL_SLEEP_MSECONDS)

STATIC ventoy_system_wrapper g_system_wrapper;

Expand Down Expand Up @@ -126,7 +126,7 @@ STATIC EFI_STATUS EFIAPI ventoy_open_protocol
IN UINT32 Attributes
)
{
debug("ventoy_open_protocol:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
debug("ventoy_open_protocol:<%p> %a", Handle, ventoy_get_guid_name(Protocol)); debug_sleep();
return g_system_wrapper.OriOpenProtocol(Handle, Protocol, Interface, AgentHandle, ControllerHandle, Attributes);
}

Expand All @@ -141,11 +141,87 @@ STATIC EFI_STATUS EFIAPI ventoy_locate_protocol
return g_system_wrapper.OriLocateProtocol(Protocol, Registration, Interface);
}

STATIC EFI_STATUS EFIAPI ventoy_locate_handle_buffer
(
IN EFI_LOCATE_SEARCH_TYPE SearchType,
IN EFI_GUID *Protocol, OPTIONAL
IN VOID *SearchKey, OPTIONAL
IN OUT UINTN *NoHandles,
OUT EFI_HANDLE **Buffer
)
{
debug("ventoy_locate_handle_buffer:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
return g_system_wrapper.OriLocateHandleBuffer(SearchType, Protocol, SearchKey, NoHandles, Buffer);
}

STATIC EFI_STATUS EFIAPI ventoy_protocol_per_handle
(
IN EFI_HANDLE Handle,
OUT EFI_GUID ***ProtocolBuffer,
OUT UINTN *ProtocolBufferCount
)
{
debug("ventoy_protocol_per_handle:%p", Handle); debug_sleep();
return g_system_wrapper.OriProtocolsPerHandle(Handle, ProtocolBuffer, ProtocolBufferCount);
}

EFI_STATUS EFIAPI ventoy_locate_handle
(
IN EFI_LOCATE_SEARCH_TYPE SearchType,
IN EFI_GUID *Protocol, OPTIONAL
IN VOID *SearchKey, OPTIONAL
IN OUT UINTN *BufferSize,
OUT EFI_HANDLE *Buffer
)
{
UINTN i;
EFI_HANDLE Handle;
EFI_STATUS Status = EFI_SUCCESS;

debug("ventoy_locate_handle: %d %a %p", SearchType, ventoy_get_guid_name(Protocol), SearchKey);
Status = g_system_wrapper.OriLocateHandle(SearchType, Protocol, SearchKey, BufferSize, Buffer);
debug("ventoy_locate_handle: %r Handle Count:%u", Status, *BufferSize/sizeof(EFI_HANDLE));

if (EFI_SUCCESS == Status)
{
for (i = 0; i < *BufferSize / sizeof(EFI_HANDLE); i++)
{
if (Buffer[i] == gBlockData.Handle)
{
Handle = Buffer[0];
Buffer[0] = Buffer[i];
Buffer[i] = Handle;
debug("####### Handle at %u", i);
break;
}
}
}

debug_sleep();

return Status;
}

STATIC EFI_STATUS EFIAPI ventoy_locate_device_path
(
IN EFI_GUID *Protocol,
IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath,
OUT EFI_HANDLE *Device
)
{
debug("ventoy_locate_device_path:%a", ventoy_get_guid_name(Protocol)); debug_sleep();
return g_system_wrapper.OriLocateDevicePath(Protocol, DevicePath, Device);
}

EFI_STATUS EFIAPI ventoy_wrapper_system(VOID)
{
ventoy_wrapper(gBS, g_system_wrapper, LocateProtocol, ventoy_locate_protocol);
ventoy_wrapper(gBS, g_system_wrapper, HandleProtocol, ventoy_handle_protocol);
ventoy_wrapper(gBS, g_system_wrapper, OpenProtocol, ventoy_open_protocol);
ventoy_wrapper(gBS, g_system_wrapper, LocateProtocol, ventoy_locate_protocol);
ventoy_wrapper(gBS, g_system_wrapper, HandleProtocol, ventoy_handle_protocol);
ventoy_wrapper(gBS, g_system_wrapper, OpenProtocol, ventoy_open_protocol);
ventoy_wrapper(gBS, g_system_wrapper, LocateHandleBuffer, ventoy_locate_handle_buffer);
ventoy_wrapper(gBS, g_system_wrapper, ProtocolsPerHandle, ventoy_protocol_per_handle);
ventoy_wrapper(gBS, g_system_wrapper, LocateHandle, ventoy_locate_handle);
ventoy_wrapper(gBS, g_system_wrapper, LocateDevicePath, ventoy_locate_device_path);

return EFI_SUCCESS;
}
Expand Down
Loading

0 comments on commit 1d034f0

Please sign in to comment.