Skip to content

Commit

Permalink
Add new iso support
Browse files Browse the repository at this point in the history
  • Loading branch information
ventoy committed Jul 18, 2020
1 parent 88dccee commit 3c46432
Show file tree
Hide file tree
Showing 19 changed files with 558 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <Ventoy.h>

BOOLEAN gDebugPrint = FALSE;
BOOLEAN gLoadIsoEfi = FALSE;
ventoy_ram_disk g_ramdisk_param;
ventoy_chain_head *g_chain;
ventoy_img_chunk *g_chunk;
Expand All @@ -52,6 +53,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;

CONST CHAR16 gIso9660EfiDriverPath[] = ISO9660_EFI_DRIVER_PATH;

BOOLEAN g_fix_windows_1st_cdrom_issue = FALSE;

STATIC BOOLEAN g_hook_keyboard = FALSE;
Expand All @@ -67,7 +70,7 @@ CONST CHAR16 *gEfiBootFileName[] =
L"\\EFI\\BOOT\\GRUBX64.EFI",
L"\\EFI\\BOOT\\BOOTx64.EFI",
L"\\EFI\\BOOT\\bootx64.efi",
L"\\efi\\boot\\bootx64.efi",
L"\\efi\\boot\\bootx64.efi"
};

VOID EFIAPI VtoyDebug(IN CONST CHAR8 *Format, ...)
Expand Down Expand Up @@ -484,6 +487,93 @@ STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk(IN EFI_HANDLE ImageHandle)
}
}


STATIC EFI_STATUS EFIAPI ventoy_find_iso_disk_fs(IN EFI_HANDLE ImageHandle)
{
UINTN i = 0;
UINTN Count = 0;
EFI_HANDLE Parent = NULL;
EFI_HANDLE *Handles = NULL;
EFI_STATUS Status = EFI_SUCCESS;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pFile = NULL;
EFI_DEVICE_PATH_PROTOCOL *pDevPath = NULL;

Status = gBS->LocateHandleBuffer(ByProtocol, &gEfiSimpleFileSystemProtocolGuid,
NULL, &Count, &Handles);
if (EFI_ERROR(Status))
{
return Status;
}

debug("ventoy_find_iso_disk_fs fs count:%u", Count);

for (i = 0; i < Count; i++)
{
Status = gBS->HandleProtocol(Handles[i], &gEfiSimpleFileSystemProtocolGuid, (VOID **)&pFile);
if (EFI_ERROR(Status))
{
continue;
}

Status = gBS->OpenProtocol(Handles[i], &gEfiDevicePathProtocolGuid,
(VOID **)&pDevPath,
ImageHandle,
Handles[i],
EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (EFI_ERROR(Status))
{
debug("Failed to open device path protocol %r", Status);
continue;
}

debug("Handle:%p FS DP: <%s>", Handles[i], ConvertDevicePathToText(pDevPath, FALSE, FALSE));
Parent = ventoy_get_parent_handle(pDevPath);

if (Parent == gBlockData.RawBlockIoHandle)
{
debug("Find ventoy disk fs");
gBlockData.DiskFsHandle = Handles[i];
gBlockData.pDiskFs = pFile;
gBlockData.pDiskFsDevPath = pDevPath;
break;
}
}

FreePool(Handles);

return EFI_SUCCESS;
}

STATIC EFI_STATUS EFIAPI ventoy_load_isoefi_driver(IN EFI_HANDLE ImageHandle)
{
EFI_HANDLE Image = NULL;
EFI_STATUS Status = EFI_SUCCESS;
CHAR16 LogVar[4] = L"5";

Status = ventoy_load_image(ImageHandle, gBlockData.pDiskFsDevPath,
gIso9660EfiDriverPath,
sizeof(gIso9660EfiDriverPath),
&Image);
debug("load iso efi driver status:%r", Status);

if (gDebugPrint)
{
gRT->SetVariable(L"FS_LOGGING", &gShellVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
sizeof(LogVar), LogVar);
}

gRT->SetVariable(L"FS_NAME_NOCASE", &gShellVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
sizeof(LogVar), LogVar);

gBlockData.IsoDriverImage = Image;
Status = gBS->StartImage(Image, NULL, NULL);
debug("Start iso efi driver status:%r", Status);

return EFI_SUCCESS;
}

STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
{
UINT32 i = 0;
Expand Down Expand Up @@ -513,6 +603,11 @@ STATIC EFI_STATUS EFIAPI ventoy_parse_cmdline(IN EFI_HANDLE ImageHandle)
gDebugPrint = TRUE;
}

if (StrStr(pCmdLine, L"isoefi=on"))
{
gLoadIsoEfi = TRUE;
}

pPos = StrStr(pCmdLine, L"FirstTry=@");
if (pPos)
{
Expand Down Expand Up @@ -639,6 +734,11 @@ EFI_STATUS EFIAPI ventoy_clean_env(VOID)
FreePool(g_sector_flag);
g_sector_flag_num = 0;

if (gLoadIsoEfi && gBlockData.IsoDriverImage)
{
gBS->UnloadImage(gBlockData.IsoDriverImage);
}

gBS->DisconnectController(gBlockData.Handle, NULL, NULL);

gBS->UninstallMultipleProtocolInterfaces(gBlockData.Handle,
Expand Down Expand Up @@ -857,6 +957,12 @@ EFI_STATUS EFIAPI VentoyEfiMain
ventoy_save_variable();
ventoy_find_iso_disk(ImageHandle);

if (gLoadIsoEfi)
{
ventoy_find_iso_disk_fs(ImageHandle);
ventoy_load_isoefi_driver(ImageHandle);
}

ventoy_debug_pause();

ventoy_install_blockio(ImageHandle, g_chain->virt_img_size_in_bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ typedef struct ventoy_virt_chunk
#define VTOY_BLOCK_DEVICE_PATH_GUID \
{ 0x37b87ac6, 0xc180, 0x4583, { 0xa7, 0x05, 0x41, 0x4d, 0xa8, 0xf7, 0x7e, 0xd2 }}

#define ISO9660_EFI_DRIVER_PATH L"\\ventoy\\iso9660_x64.efi"

#define VTOY_BLOCK_DEVICE_PATH_NAME L"ventoy"

#if defined (MDE_CPU_IA32)
Expand Down Expand Up @@ -208,6 +210,7 @@ typedef struct vtoy_block_data
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *pDiskFs;
EFI_DEVICE_PATH_PROTOCOL *pDiskFsDevPath;

EFI_HANDLE IsoDriverImage;
}vtoy_block_data;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,16 @@ STATIC EFI_STATUS EFIAPI
ventoy_wrapper_file_set_pos(EFI_FILE_HANDLE This, UINT64 Position)
{
(VOID)This;

if (Position <= g_efi_file_replace.FileSizeBytes)
{
g_efi_file_replace.CurPos = Position;
}
else
{
g_efi_file_replace.CurPos = g_efi_file_replace.FileSizeBytes;
}

g_efi_file_replace.CurPos = Position;
return EFI_SUCCESS;
}

Expand Down Expand Up @@ -815,6 +823,8 @@ STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
CHAR8 TmpName[256];
ventoy_virt_chunk *virt = NULL;

debug("## ventoy_wrapper_file_open <%s> ", Name);

Status = g_original_fopen(This, New, Name, Mode, Attributes);
if (EFI_ERROR(Status))
{
Expand Down Expand Up @@ -850,6 +860,11 @@ STATIC EFI_STATUS EFIAPI ventoy_wrapper_file_open
return Status;
}
}

if (StrCmp(Name, L"\\EFI\\BOOT") == 0)
{
(*New)->Open = ventoy_wrapper_file_open;
}
}

return Status;
Expand Down
12 changes: 12 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 @@ -1459,6 +1459,7 @@ grub_uint32_t ventoy_get_iso_boot_catlog(grub_file_t file)
int ventoy_has_efi_eltorito(grub_file_t file, grub_uint32_t sector)
{
int i;
int x86count = 0;
grub_uint8_t buf[512];

grub_file_seek(file, sector * 2048);
Expand All @@ -1470,13 +1471,24 @@ int ventoy_has_efi_eltorito(grub_file_t file, grub_uint32_t sector)
return 1;
}

if (buf[0] == 0x01 && buf[1] == 0x00)
{
x86count++;
}

for (i = 64; i < (int)sizeof(buf); i += 32)
{
if ((buf[i] == 0x90 || buf[i] == 0x91) && buf[i + 1] == 0xEF)
{
debug("%s efi eltorito offset %d 0x%02x\n", file->name, i, buf[i]);
return 1;
}

if (buf[i] == 0x91 && buf[i + 1] == 0x00 && x86count == 1)
{
debug("0x9100 assume %s efi eltorito offset %d 0x%02x\n", file->name, i, buf[i]);
return 1;
}
}

debug("%s does not contain efi eltorito\n", file->name);
Expand Down
4 changes: 2 additions & 2 deletions IMG/cpio/ventoy/hook/cdlinux/ventoy-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ done
$BUSYBOX_PATH/umount /vtmnt && $BUSYBOX_PATH/rm -rf /vtmnt
$BUSYBOX_PATH/cp -a /ventoy /ventoy_rdroot

echo "CDL_DEV=/dev/mapper/ventoy" >> /ventoy_rdroot/etc/default/cdlinux
echo 'echo "CDL_DEV=/dev/mapper/ventoy" >>"$VAR_FILE"' >> /ventoy_rdroot/etc/rc.d/rc.var

ventoy_set_rule_dir_prefix /ventoy_rdroot
ventoy_systemd_udevd_work_around
ventoy_add_udev_rule "$VTOY_PATH/hook/default/udev_disk_hook.sh %k"
ventoy_add_udev_rule "$VTOY_PATH/hook/default/udev_disk_hook.sh %k noreplace"
52 changes: 52 additions & 0 deletions IMG/cpio/ventoy/hook/cucumber/disk-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/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

if is_ventoy_hook_finished; then
exit 0
fi

vtlog "####### $0 $* ########"

VTPATH_OLD=$PATH; PATH=$BUSYBOX_PATH:$VTOY_PATH/tool:$PATH

wait_for_usb_disk_ready

vtdiskname=$(get_ventoy_disk_name)
if [ "$vtdiskname" = "unknown" ]; then
vtlog "ventoy disk not found"
PATH=$VTPATH_OLD
exit 0
fi

ventoy_udev_disk_common_hook "${vtdiskname#/dev/}2" "noreplace"

blkdev_num=$(dmsetup ls | grep ventoy | sed 's/.*(\([0-9][0-9]*\),.*\([0-9][0-9]*\).*/\1:\2/')
vtDM=$(ventoy_find_dm_id ${blkdev_num})

vtlog "mount media /dev/$vtDM ..."
if ! [ -e /media/install ]; then
mkdir -p /media/install
fi
mount /dev/$vtDM /media/install

PATH=$VTPATH_OLD

set_ventoy_hook_finish
26 changes: 26 additions & 0 deletions IMG/cpio/ventoy/hook/cucumber/ventoy-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/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/>.
#
#************************************************************************************

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

if [ -e /sbin/mount_installer ]; then
echo "hook at mount_installer ..." >> $VTLOG
$SED "1 a $BUSYBOX_PATH/sh $VTOY_PATH/hook/cucumber/disk-hook.sh" -i /sbin/mount_installer
fi

43 changes: 43 additions & 0 deletions IMG/cpio/ventoy/hook/fatdog/disk-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/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

if is_ventoy_hook_finished; then
exit 0
fi

vtlog "####### $0 $* ########"

VTPATH_OLD=$PATH; PATH=$BUSYBOX_PATH:$VTOY_PATH/tool:$PATH

wait_for_usb_disk_ready

vtdiskname=$(get_ventoy_disk_name)
if [ "$vtdiskname" = "unknown" ]; then
vtlog "ventoy disk not found"
PATH=$VTPATH_OLD
exit 0
fi

ventoy_udev_disk_common_hook "${vtdiskname#/dev/}2" "noreplace"

PATH=$VTPATH_OLD

set_ventoy_hook_finish
Loading

0 comments on commit 3c46432

Please sign in to comment.