This repository was archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyRamDisk.c
More file actions
176 lines (150 loc) · 4.84 KB
/
MyRamDisk.c
File metadata and controls
176 lines (150 loc) · 4.84 KB
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
47
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/** @file
在UEFI环境下帮助iso文件直接启动,也支持ixpe启动
**/
#include "MyRamDisk.h"
#include "ver.h"
///全局私有数据指针,由驱动部分访问
DIDO_DISK_PRIVATE_DATA *pridata;
///全局命令行参数指针
DIDO_OPTION_STATUS *OptionStatus;
///程序入口,作为启动器,貌似不能用main作为入口
EFI_STATUS EFIAPI UefiMain(
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_FILE_HANDLE BootFileHandleInRamDisk=NULL;
EFI_DEVICE_PATH_PROTOCOL *CurrDirDP;
EFI_FILE_HANDLE IsoFileHandle;
EFI_FILE_HANDLE CurrDirHandle;
///显示版本号
Print(L"Version: %s Build time: %s\n",ImgbootRevision,ImgbootBuildTime);
///初始化命令行参数状态
OptionStatus=AllocateZeroPool(sizeof(DIDO_OPTION_STATUS));
OptionStatus->LoadInMemory=FALSE;
OptionStatus->DebugDropToShell=FALSE;
OptionStatus->ImageFileType=UNKNOWNTYPE;
OptionStatus->ImageFileName=NULL;
OptionStatus->WaitTimeSec=0;
OptionStatus->DevicePathToFindImage=NULL;
OptionStatus->UseBuildInNtfsDriver=FALSE;
///初始化全局指针pridata,分配3个私有数据结构,第一个用于整个盘,第二个未使用,第三个用于启动分区
pridata=AllocateZeroPool(3*sizeof(DIDO_DISK_PRIVATE_DATA)+8);
/*//测试4g边界
{///获取当前目录
CHAR8 *buffer;
UINTN sss=512;
UINTN i;
CurrDirDP=GetCurrDirDP(gImageHandle,L"ggg.vhd");
if(NULL==CurrDirDP){
Print(L"GetCurrDirDP Error\n");
return EFI_SUCCESS;
}
///打开当前目录
CurrDirHandle=OpenFileByDevicePath(CurrDirDP);
if(NULL==CurrDirHandle){
Print(L"Open CurrDir Error\n");
return EFI_SUCCESS;
}
buffer=AllocateZeroPool(512);
FileHandleSetPosition(CurrDirHandle,0x100000000);
FileHandleRead(CurrDirHandle,&sss,buffer);
for(i=0;i<505;i+=8)
Print(L"%016llX ",*(UINTN *)(buffer+i));
if(NULL!=CurrDirHandle)return EFI_SUCCESS;
}
//测试结束
*/
///获取当前目录
CurrDirDP=GetCurrDirDP(gImageHandle,L"");
if(NULL==CurrDirDP){
Print(L"GetCurrDirDP Error\n");
return EFI_SUCCESS;
}
///打开当前目录
CurrDirHandle=OpenFileByDevicePath(CurrDirDP);
if(NULL==CurrDirHandle){
Print(L"Open CurrDir Error\n");
return EFI_SUCCESS;
}
///处理命令行参数
Status=ProcCmdLine(OptionStatus);
if(NULL==OptionStatus->ImageFileName){
///处理配置文件
Status=ProcCfgFile(OptionStatus,CurrDirHandle,L"imgboot.cfg");
}
///加载ntfs驱动
if(OptionStatus->UseBuildInNtfsDriver)
Status=LoadNtfsDriver();
///打开指定镜像文件
IsoFileHandle=OpenFileInOptionStatus(OptionStatus,CurrDirHandle);
if(NULL==IsoFileHandle){
//在当前目录搜索第一个iso文件并打开
OptionStatus->ImageFileType=ISOFILE;
IsoFileHandle=OpenFirstIsoFileInDir(CurrDirHandle);
if(NULL==IsoFileHandle){
Print(L"Open ISO file Error\n");
goto errordroptoshell;
}
}
///安装虚拟盘
Status=FileDiskInstall(IsoFileHandle);
if(Status==EFI_NOT_FOUND) {
Print(L"Sorry!Can't boot this file.\n");
goto errordroptoshell;
}
///打开虚拟盘上的启动文件
if(pridata[2].VirDiskHandle!=NULL)
BootFileHandleInRamDisk=LoadBootFileInVirtualDisk(pridata[2].VirDiskHandle);
///查找启动文件
if(BootFileHandleInRamDisk==NULL) {
BootFileHandleInRamDisk=FindAndLoadBootFileInVirtualDisk();
}
if(BootFileHandleInRamDisk==NULL) {
Print(L"Sorry!Can't boot this file.\n");
goto errordroptoshell;
}
///检查调试开关
if(OptionStatus->DebugDropToShell==TRUE){
Print(L"Debug drop to shell\n");
goto errordroptoshell;
}
///必须降低运行级别才能等待,并且避免实机进入pe的logo后死机重启
gBS->RestoreTPL(TPL_APPLICATION);
///等待WaitTimeSec秒,并降低运行级别到application级,否则实机死机重启
DidoWaitSec(OptionStatus->WaitTimeSec);
///启动
Status=gBS->StartImage( BootFileHandleInRamDisk,0, NULL);
Print(L"Image returned\n");
return EFI_SUCCESS;
errordroptoshell:
///失败载入一个shellx64.efi
Print(L"Loading shellx64.efi\n");
CurrDirDP=GetCurrDirDP(gImageHandle,L"shellx64.efi");
if(NULL==CurrDirDP){
return EFI_SUCCESS;
}
Status=gBS->LoadImage(
FALSE,
gImageHandle, //parent不能为空,传入本文件的Handle
CurrDirDP, //文件的devicepath
NULL,
0,
(VOID**)&BootFileHandleInRamDisk //传入HANDLE地址
);
if(EFI_ERROR(Status)){
Print(L"Load shellx64.efi failed!\n");
return EFI_SUCCESS;
}
///必须降低运行级别才能等待,并且避免实机进入pe的logo后死机重启
gBS->RestoreTPL(TPL_APPLICATION);
///等待30秒
DidoWaitSec(OptionStatus->WaitTimeSec>30?OptionStatus->WaitTimeSec:30);
Status=gBS->StartImage( BootFileHandleInRamDisk,0, NULL);
if(EFI_ERROR (Status)) {
Print(L"Start shellx64.efi failed!\n");
return EFI_SUCCESS;
}
/**/ return EFI_SUCCESS;
}