VtoyUtil.c 4.94 KB
Newer Older
longpanda's avatar
update  
longpanda committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/******************************************************************************
 * VtoyUtil.c
 *
 * 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/>.
 *
 */

#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Protocol/LoadedImage.h>
#include <Guid/FileInfo.h>
#include <Guid/FileSystemInfo.h>
#include <Protocol/BlockIo.h>
#include <Protocol/RamDisk.h>
#include <Protocol/SimpleFileSystem.h>
#include <VtoyUtil.h>

BOOLEAN gVtoyDebugPrint = FALSE;
STATIC CONST CHAR16 *gCurFeature= NULL;
STATIC CHAR16 *gCmdLine = NULL;
STATIC grub_env_printf_pf g_env_printf = NULL;

STATIC VtoyUtilFeature gFeatureList[] = 
{
    { L"fix_windows_mmap", FixWindowsMemhole },
47
    { L"show_efi_drivers", ShowEfiDrivers    },
longpanda's avatar
update  
longpanda committed
48
49
};

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
EFI_STATUS VtoyGetComponentName(IN UINTN Ver, IN VOID *Protocol, OUT CHAR16 **DriverName)
{
    EFI_STATUS Status = EFI_SUCCESS;
    CHAR16 *DrvName = NULL;
    EFI_COMPONENT_NAME_PROTOCOL *NameProtocol = NULL;
    EFI_COMPONENT_NAME2_PROTOCOL *Name2Protocol = NULL;

    if (1 == Ver)
    {
        NameProtocol = (EFI_COMPONENT_NAME_PROTOCOL *)Protocol;
        Status = NameProtocol->GetDriverName(Protocol, "en", &DrvName);
        if (EFI_ERROR(Status) || NULL == DrvName)
        {
            Status = NameProtocol->GetDriverName(Protocol, "eng", &DrvName);
        }
    }
    else
    {
        Name2Protocol = (EFI_COMPONENT_NAME2_PROTOCOL *)Protocol;
        Status = Name2Protocol->GetDriverName(Protocol, "en", &DrvName);
        if (EFI_ERROR(Status) || NULL == DrvName)
        {
            Status = Name2Protocol->GetDriverName(Protocol, "eng", &DrvName);
        }
    }

    *DriverName = DrvName;
    return Status;
}

longpanda's avatar
update  
longpanda committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
VOID EFIAPI VtoyUtilDebug(IN CONST CHAR8  *Format, ...)
{
    VA_LIST  Marker;
    CHAR8    Buffer[512];

    VA_START (Marker, Format);
    AsciiVSPrint(Buffer, sizeof(Buffer), Format, Marker);
    VA_END (Marker);

    if (g_env_printf)
    {
        g_env_printf("%s", Buffer);
    }
}

STATIC EFI_STATUS ParseCmdline(IN EFI_HANDLE ImageHandle)
{   
    CHAR16 *pPos = NULL;
    CHAR16 *pCmdLine = NULL;
    EFI_STATUS Status = EFI_SUCCESS;
    ventoy_grub_param *pGrubParam = NULL;
    EFI_LOADED_IMAGE_PROTOCOL *pImageInfo = NULL;

    Status = gBS->HandleProtocol(ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&pImageInfo);
    if (EFI_ERROR(Status))
    {
        return Status;
    }

    pCmdLine = (CHAR16 *)AllocatePool(pImageInfo->LoadOptionsSize + 4);
    SetMem(pCmdLine, pImageInfo->LoadOptionsSize + 4, 0);
    CopyMem(pCmdLine, pImageInfo->LoadOptions, pImageInfo->LoadOptionsSize);

longpanda's avatar
longpanda committed
113
114
115
116
117
118
119
120
    if (StrStr(pCmdLine, L"vtoyefitest"))
    {
        gST->ConOut->OutputString(gST->ConOut, L"\r\n##########################");
        gST->ConOut->OutputString(gST->ConOut, L"\r\n#########  VTOY  #########");
        gST->ConOut->OutputString(gST->ConOut, L"\r\n##########################");
        return EFI_SUCCESS;
    }
    
longpanda's avatar
update  
longpanda committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    if (StrStr(pCmdLine, L"debug"))
    {
        gVtoyDebugPrint = TRUE;
    }

    pPos = StrStr(pCmdLine, L"env_param=");
    if (!pPos)
    {
        return EFI_INVALID_PARAMETER;
    }

    pGrubParam = (ventoy_grub_param *)StrHexToUintn(pPos + StrLen(L"env_param="));
    g_env_printf = pGrubParam->grub_env_printf;

    pPos = StrStr(pCmdLine, L"feature=");
    if (!pPos)
    {
        return EFI_INVALID_PARAMETER;
    }

    gCurFeature = pPos + StrLen(L"feature=");
longpanda's avatar
longpanda committed
142
    
longpanda's avatar
update  
longpanda committed
143
    gCmdLine = pCmdLine;
longpanda's avatar
longpanda committed
144
    
longpanda's avatar
update  
longpanda committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    return EFI_SUCCESS;
}

EFI_STATUS EFIAPI VtoyUtilEfiMain
(
    IN EFI_HANDLE         ImageHandle,
    IN EFI_SYSTEM_TABLE  *SystemTable
)
{
    UINTN i;
    UINTN Len;
    
    ParseCmdline(ImageHandle);

longpanda's avatar
longpanda committed
159
    for (i = 0; gCurFeature && i < ARRAY_SIZE(gFeatureList); i++)
longpanda's avatar
update  
longpanda committed
160
161
162
163
164
165
166
167
168
169
    {
        Len = StrLen(gFeatureList[i].Cmd);
        if (StrnCmp(gFeatureList[i].Cmd, gCurFeature, Len) == 0)
        {
            debug("Find main proc <%s>", gFeatureList[i].Cmd);
            gFeatureList[i].MainProc(ImageHandle, gCurFeature + Len);
            break;
        }
    }

longpanda's avatar
longpanda committed
170
171
172
173
174
    if (gCmdLine)
    {
        FreePool(gCmdLine);
        gCmdLine = NULL;        
    }
longpanda's avatar
update  
longpanda committed
175
176
177
178

    return EFI_SUCCESS;
}