-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.bat
220 lines (152 loc) · 4.02 KB
/
build.bat
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@ECHO OFF
SET EXITCODE=0
SET EXTRAMSG=
SET MNT="V:\"
SET VHD="%CD%\kernel.vhd"
SET ASM="%CD%\kernel.asm"
SET OBJ="%CD%\kernel.obj"
SET EFI="%CD%\kernel.efi"
SET OVMF="%CD%\OVMF.fd"
SET NASM="C:\Program Files\NASM\nasm"
SET QEMU="C:\Program Files\Qemu\qemu-system-x86_64"
ECHO.
ECHO NASM UEFI Build Script
ECHO.
IF "%VSCMD_ARG_HOST_ARCH%" NEQ "x64" GOTO :ErrorX64
IF "%VSCMD_ARG_TGT_ARCH%" NEQ "x64" GOTO :ErrorX64
NET SESSION > NUL 2>&1
IF %ERRORLEVEL% NEQ 0 GOTO :ErrorAdmin
ECHO 1) Build Virtual Hard Disk
ECHO 2) Mount / Unmount VHD
ECHO 3) Build UEFI Application
ECHO 4) Build UEFI ^& Boot Machine
ECHO 5) Clean Files
ECHO.
SET /P CHOICE=Choose #
ECHO.
IF %CHOICE% == 1 GOTO :BuildVHD
IF %CHOICE% == 2 GOTO :MountVHD
IF %CHOICE% == 3 GOTO :BuildUEFI
IF %CHOICE% == 4 GOTO :Boot
IF %CHOICE% == 5 GOTO :Clean
ECHO Error: You have entered an invalid #
GOTO :EOF
:ErrorX64
ECHO Error: You must run this script from the x64 Native Tools Command Prompt for VS 2017
GOTO :EOF
:ErrorAdmin
ECHO Error: You must run this script as an Administrator
GOTO :EOF
:BuildVHD
IF EXIST %MNT% CALL :MountVHD
IF %EXITCODE% NEQ 0 GOTO :EOF
IF EXIST %VHD% DEL %VHD%
ECHO create vdisk file=%VHD% maximum=300 > build.tmp
ECHO select vdisk file=%VHD% >> build.tmp
ECHO attach vdisk >> build.tmp
ECHO convert gpt >> build.tmp
ECHO create partition efi size=100 >> build.tmp
ECHO create partition primary >> build.tmp
ECHO format fs=fat32 quick >> build.tmp
ECHO assign letter=V >> build.tmp
ECHO exit >> build.tmp
SET EXTRAMSG=create
GOTO :RunDiskPart
:MountVHD
IF EXIST %MNT% (
IF EXIST %VHD% (
ECHO select vdisk file=%VHD% > build.tmp
ECHO detach vdisk >> build.tmp
ECHO exit >> build.tmp
SET EXTRAMSG=detach
) ELSE (
ECHO Error: You must build the VHD first
SET EXITCODE=1
GOTO :EOF
)
) ELSE (
ECHO select vdisk file=%VHD% > build.tmp
ECHO attach vdisk >> build.tmp
ECHO select partition 3 >> build.tmp
ECHO assign letter=V >> build.tmp
ECHO exit >> build.tmp
SET EXTRAMSG=attach
)
GOTO :RunDiskPart
:RunDiskPart
DISKPART /S build.tmp > build.err
IF %ERRORLEVEL% NEQ 0 (
ECHO Error: The diskpart command failed with the following output
ECHO.
ECHO ------
TYPE build.err
DEL build.err
SET EXITCODE=1
) ELSE (
ECHO VHD Success! ^(%EXTRAMSG%^)
)
DEL build.err
DEL build.tmp
GOTO :EOF
:BuildUEFI
%NASM% -f win64 %ASM% -o %OBJ% > NUL 2> build.err
IF %ERRORLEVEL% NEQ 0 (
ECHO Error: The nasm command failed with the following output
ECHO.
ECHO ------
ECHO.
TYPE build.err
DEL build.err
SET EXITCODE=1
GOTO :EOF
)
DEL build.err
link /subsystem:EFI_APPLICATION /entry:start /out:%EFI% %OBJ% > build.err
IF %ERRORLEVEL% NEQ 0 (
ECHO Error: The link command failed with the following output
ECHO.
ECHO ------
ECHO.
TYPE build.err
DEL build.err
SET EXITCODE=1
GOTO :EOF
)
DEL build.err
ECHO EFI Success!
GOTO :EOF
:Boot
IF NOT EXIST %VHD% CALL :BuildVHD && ECHO.
IF %EXITCODE% NEQ 0 GOTO :EOF
IF NOT EXIST %MNT% CALL :MountVHD && ECHO.
IF %EXITCODE% NEQ 0 GOTO :EOF
CALL :BuildUEFI
IF %EXITCODE% NEQ 0 GOTO :EOF
ECHO.
IF NOT EXIST %MNT%EFI MKDIR %MNT%EFI
IF NOT EXIST %MNT%EFI\BOOT MKDIR %MNT%EFI\BOOT
COPY /Y %EFI% %MNT%EFI\BOOT\BOOTX64.EFI > build.err
IF %ERRORLEVEL% NEQ 0 (
ECHO Error: The copy command failed with the following output
ECHO.
ECHO ------
ECHO.
TYPE build.err
DEL build.err
SET EXITCODE=1
GOTO :EOF
)
DEL build.err
CALL :MountVHD
IF %EXITCODE% NEQ 0 GOTO :EOF
%QEMU% -cpu qemu64 -bios %OVMF% -drive file=%VHD%,format=raw
GOTO :EOF
:Clean
IF EXIST %MNT% (CALL :MountVHD)
DEL %VHD%
DEL %OBJ%
DEL %EFI%
ECHO Clean!
GOTO :EOF
:EOF
EXIT /B %EXITCODE%