forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetupLdr.dpr
535 lines (488 loc) · 20.3 KB
/
SetupLdr.dpr
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
program SetupLdr;
{
Inno Setup
Copyright (C) 1997-2024 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Setup Loader
}
uses
SafeDLLPath in '..\Components\SafeDLLPath.pas',
SetupLdrAndSetup.XPTheme in 'Src\SetupLdrAndSetup.XPTheme.pas',
Windows,
Messages,
SysUtils,
Compression.Base in 'Src\Compression.Base.pas',
Compression.LZMA1SmallDecompressor in 'Src\Compression.LZMA1SmallDecompressor.pas',
Shared.SetupEntFunc in 'Src\Shared.SetupEntFunc.pas',
PathFunc in '..\Components\PathFunc.pas',
Shared.CommonFunc in 'Src\Shared.CommonFunc.pas',
SetupLdrAndSetup.Messages in 'Src\SetupLdrAndSetup.Messages.pas',
Shared.SetupMessageIDs in 'Src\Shared.SetupMessageIDs.pas',
Shared.Struct in 'Src\Shared.Struct.pas',
SetupLdrAndSetup.InstFunc in 'Src\SetupLdrAndSetup.InstFunc.pas',
Shared.FileClass in 'Src\Shared.FileClass.pas',
Shared.Int64Em in 'Src\Shared.Int64Em.pas',
SHA256 in '..\Components\SHA256.pas',
SetupLdrAndSetup.RedirFunc in 'Src\SetupLdrAndSetup.RedirFunc.pas',
Shared.VerInfoFunc in 'Src\Shared.VerInfoFunc.pas';
{$SETPEOSVERSION 6.1}
{$SETPESUBSYSVERSION 6.1}
{$WEAKLINKRTTI ON}
{$R Res\Setup.icon.res}
{$R Res\SetupLdr.version.res}
{$R Res\SetupLdr.offsettable.res}
procedure RaiseLastError(const Msg: TSetupMessageID);
var
ErrorCode: DWORD;
begin
ErrorCode := GetLastError;
raise Exception.Create(FmtSetupMessage(msgLastErrorMessage,
[SetupMessages[Msg], IntToStr(ErrorCode), Win32ErrorString(ErrorCode)]));
end;
procedure ShowExceptionMsg;
begin
if ExceptObject is EAbort then
Exit;
MessageBox(0, PChar(GetExceptMessage), Pointer(SetupMessages[msgErrorTitle]),
MB_OK or MB_ICONSTOP);
{ ^ use a Pointer cast instead of a PChar cast so that it will use "nil"
if SetupMessages[msgErrorTitle] is empty due to the messages not being
loaded yet. MessageBox displays 'Error' as the caption if the lpCaption
parameter is nil. }
end;
const
{ Exit codes that are returned by SetupLdr.
Note: Setup also returns exit codes with the same numbers. }
ecInitializationError = 1; { Setup failed to initialize. }
ecCancelledBeforeInstall = 2; { User clicked Cancel before the actual
installation started. }
type
PLanguageEntryArray = ^TLanguageEntryArray;
TLanguageEntryArray = array[0..999999] of TSetupLanguageEntry;
var
InitShowHelp: Boolean = False;
InitDisableStartupPrompt: Boolean = False;
InitLang: String;
ActiveLanguage: Integer = -1;
PendingNewLanguage: Integer = -1;
SetupHeader: TSetupHeader;
LanguageEntries: PLanguageEntryArray;
LanguageEntryCount: Integer;
SetupLdrExitCode: Integer = ecInitializationError;
SetupLdrWnd: HWND = 0;
OrigWndProc: Pointer;
RestartSystem: Boolean = False;
procedure ProcessCommandLine;
var
I: Integer;
Name: String;
begin
for I := 1 to NewParamCount do begin
Name := NewParamStr(I);
if (CompareText(Name, '/SP-') = 0) or
(CompareText(Copy(Name, 1, 10), '/SPAWNWND=') = 0) then
InitDisableStartupPrompt := True
else if CompareText(Copy(Name, 1, 6), '/Lang=') = 0 then
InitLang := Copy(Name, 7, Maxint)
else if (CompareText(Name, '/HELP') = 0) or
(CompareText(Name, '/?') = 0) then
InitShowHelp := True;
end;
end;
procedure SetActiveLanguage(const I: Integer);
{ Activates the specified language }
begin
if (I >= 0) and (I < LanguageEntryCount) and (I <> ActiveLanguage) then begin
AssignSetupMessages(LanguageEntries[I].Data[1], Length(LanguageEntries[I].Data));
ActiveLanguage := I;
end;
end;
function GetLanguageEntryProc(Index: Integer; var Entry: PSetupLanguageEntry): Boolean;
begin
Result := False;
if Index < LanguageEntryCount then begin
Entry := @LanguageEntries[Index];
Result := True;
end;
end;
procedure ActivateDefaultLanguage;
{ Auto-detects the most appropriate language and activates it.
Note: A like-named version of this function is also present in Main.pas. }
var
I: Integer;
begin
DetermineDefaultLanguage(GetLanguageEntryProc, SetupHeader.LanguageDetectionMethod,
InitLang, I);
SetActiveLanguage(I);
end;
function SetupLdrWndProc(Wnd: HWND; Msg: UINT; WParam: WPARAM; LParam: LPARAM): LRESULT;
stdcall;
begin
Result := 0;
case Msg of
WM_QUERYENDSESSION: begin
{ Return zero so that a shutdown attempt can't kill SetupLdr }
end;
WM_USER + 150: begin
if WParam = 10000 then begin
{ Setup wants SetupLdr to restart the computer before it exits }
RestartSystem := True;
end
else if WParam = 10001 then begin
{ Setup wants SetupLdr to change its active language }
PendingNewLanguage := LParam;
end;
end;
else
Result := CallWindowProc(OrigWndProc, Wnd, Msg, WParam, LParam);
end;
end;
procedure ProcessMessages;
var
Msg: TMsg;
begin
while PeekMessage(Msg, 0, 0, 0, PM_REMOVE) do begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
procedure ExecAndWait(const Filename, Parms: String; var ExitCode: Integer);
var
CmdLine: String;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
CmdLine := '"' + Filename + '" ' + Parms;
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb := SizeOf(StartupInfo);
if not CreateProcess(nil, PChar(CmdLine), nil, nil, False, 0, nil, nil,
StartupInfo, ProcessInfo) then
RaiseLastError(msgLdrCannotExecTemp);
CloseHandle(ProcessInfo.hThread);
{ Wait for the process to terminate, processing messages in the meantime }
repeat
ProcessMessages;
until MsgWaitForMultipleObjects(1, ProcessInfo.hProcess, False, INFINITE,
QS_ALLINPUT) <> WAIT_OBJECT_0+1;
{ Now that the process has exited, process any remaining messages.
(There may be an asynchronously-sent "restart request" message still
queued if MWFMO saw the process terminate before checking for new
messages.) }
ProcessMessages;
GetExitCodeProcess(ProcessInfo.hProcess, DWORD(ExitCode));
CloseHandle(ProcessInfo.hProcess);
end;
procedure SetupCorruptError;
begin
if SetupMessages[msgSetupFileCorrupt] <> '' then
raise Exception.Create(SetupMessages[msgSetupFileCorrupt])
else
{ In case the messages haven't been loaded yet, use the constant }
raise Exception.Create(SSetupFileCorrupt);
end;
procedure RunImageLocally(const Module: HMODULE);
{ Force all of the specified module to be paged in to ensure subsequent
accesses don't cause the disk image to be read.
Based on code from http://www.microsoft.com/msj/0398/win320398.htm, with
some fixes incorporated. }
procedure Touch(var X: DWORD);
{ Note: Uses asm to ensure it isn't optimized away }
asm
xor edx, edx
lock or [eax], edx
end;
var
SysInfo: TSystemInfo;
CurAddr: Pointer;
MemInfo: TMemoryBasicInformation;
ChangedProtection: Boolean;
OrigProtect: DWORD;
Offset: Cardinal;
begin
{ Get system's page size }
GetSystemInfo(SysInfo);
CurAddr := Pointer(Module);
if VirtualQuery(CurAddr, MemInfo, SizeOf(MemInfo)) = 0 then
Exit;
{ Perform this loop until we find a region beyond the end of the file }
while MemInfo.AllocationBase = Pointer(Module) do begin
{ We can only force committed pages into RAM.
We do not want to trigger guard pages and confuse the application. }
if (MemInfo.State = MEM_COMMIT) and (MemInfo.Protect and PAGE_GUARD = 0) then begin
ChangedProtection := False;
{ Determine if the pages in this region are nonwriteable }
if (MemInfo.Protect = PAGE_NOACCESS) or
(MemInfo.Protect = PAGE_READONLY) or
(MemInfo.Protect = PAGE_EXECUTE) or
(MemInfo.Protect = PAGE_EXECUTE_READ) then begin
{ Nonwriteable region, make it writeable (with the least protection) }
if VirtualProtect(MemInfo.BaseAddress, MemInfo.RegionSize,
PAGE_EXECUTE_READWRITE, @OrigProtect) then
ChangedProtection := True;
end;
{ Write to every page in the region.
This forces the page to be in RAM and swapped to the paging file. }
Offset := 0;
while Offset < Cardinal(MemInfo.RegionSize) do begin
Touch(PDWORD(Cardinal(MemInfo.BaseAddress) + Offset)^);
Inc(Offset, SysInfo.dwPageSize);
end;
{ If we changed the protection, change it back }
if ChangedProtection then
VirtualProtect(MemInfo.BaseAddress, MemInfo.RegionSize, OrigProtect,
@OrigProtect);
end;
{ Get next region }
Cardinal(CurAddr) := Cardinal(MemInfo.BaseAddress) + MemInfo.RegionSize;
if VirtualQuery(CurAddr, MemInfo, SizeOf(MemInfo)) = 0 then
Break;
end;
end;
function GetSetupLdrOffsetTable: PSetupLdrOffsetTable;
{ Locates the offset table resource, and returns a pointer to it }
var
Rsrc: HRSRC;
ResData: HGLOBAL;
begin
Rsrc := FindResource(0, MAKEINTRESOURCE(SetupLdrOffsetTableResID), RT_RCDATA);
if Rsrc = 0 then
SetupCorruptError;
if SizeofResource(0, Rsrc) <> SizeOf(Result^) then
SetupCorruptError;
ResData := LoadResource(0, Rsrc);
if ResData = 0 then
SetupCorruptError;
Result := LockResource(ResData);
if Result = nil then
SetupCorruptError;
end;
procedure ShowHelp(const CustomNote: String);
const
SNewLine = #13#10;
var
PrNote, Help: String;
begin
{ do not localize }
if proCommandLine in SetupHeader.PrivilegesRequiredOverridesAllowed then begin
PrNote := '/ALLUSERS' + SNewLine +
'Instructs Setup to install in administrative install mode.' + SNewLine +
'/CURRENTUSER' + SNewLine +
'Instructs Setup to install in non administrative install mode.' + SNewLine;
end else
PrNote := '';
Help := 'The Setup program accepts optional command line parameters.' + SNewLine +
SNewLine +
'/HELP, /?' + SNewLine +
'Shows this information.' + SNewLine +
'/SP-' + SNewLine +
'Disables the "This will install... Do you wish to continue?" message box at the beginning of Setup.' + SNewLine +
'/SILENT, /VERYSILENT' + SNewLine +
'Instructs Setup to be silent or very silent.' + SNewLine +
'/SUPPRESSMSGBOXES' + SNewLine +
'Instructs Setup to suppress message boxes.' + SNewLine +
'/LOG' + SNewLine +
'Causes Setup to create a log file in the user''s TEMP directory.' + SNewLine +
'/LOG="filename"' + SNewLine +
'Same as /LOG, except it allows you to specify a fixed path/filename to use for the log file.' + SNewLine +
'/NOCANCEL' + SNewLine +
'Prevents the user from cancelling during the installation process.' + SNewLine +
'/NORESTART' + SNewLine +
'Prevents Setup from restarting the system following a successful installation, or after a Preparing to Install failure that requests a restart.' + SNewLine +
'/RESTARTEXITCODE=exit code' + SNewLine +
'Specifies a custom exit code that Setup is to return when the system needs to be restarted.' + SNewLine +
'/CLOSEAPPLICATIONS' + SNewLine +
'Instructs Setup to close applications using files that need to be updated.' + SNewLine +
'/NOCLOSEAPPLICATIONS' + SNewLine +
'Prevents Setup from closing applications using files that need to be updated.' + SNewLine +
'/FORCECLOSEAPPLICATIONS' + SNewLine +
'Instructs Setup to force close when closing applications.' + SNewLine +
'/FORCENOCLOSEAPPLICATIONS' + SNewLine +
'Prevents Setup from force closing when closing applications.' + SNewLine +
'/LOGCLOSEAPPLICATIONS' + SNewLine +
'Instructs Setup to create extra logging when closing applications for debugging purposes.' + SNewLine +
'/RESTARTAPPLICATIONS' + SNewLine +
'Instructs Setup to restart applications.' + SNewLine +
'/NORESTARTAPPLICATIONS' + SNewLine +
'Prevents Setup from restarting applications.' + SNewLine +
'/LOADINF="filename"' + SNewLine +
'Instructs Setup to load the settings from the specified file after having checked the command line.' + SNewLine +
'/SAVEINF="filename"' + SNewLine +
'Instructs Setup to save installation settings to the specified file.' + SNewLine +
'/LANG=language' + SNewLine +
'Specifies the internal name of the language to use.' + SNewLine +
'/DIR="x:\dirname"' + SNewLine +
'Overrides the default directory name.' + SNewLine +
'/GROUP="folder name"' + SNewLine +
'Overrides the default folder name.' + SNewLine +
'/NOICONS' + SNewLine +
'Instructs Setup to initially check the Don''t create a Start Menu folder check box.' + SNewLine +
'/TYPE=type name' + SNewLine +
'Overrides the default setup type.' + SNewLine +
'/COMPONENTS="comma separated list of component names"' + SNewLine +
'Overrides the default component settings.' + SNewLine +
'/TASKS="comma separated list of task names"' + SNewLine +
'Specifies a list of tasks that should be initially selected.' + SNewLine +
'/MERGETASKS="comma separated list of task names"' + SNewLine +
'Like the /TASKS parameter, except the specified tasks will be merged with the set of tasks that would have otherwise been selected by default.' + SNewLine +
'/PASSWORD=password' + SNewLine +
'Specifies the password to use.' + SNewLine +
PrNote +
CustomNote +
SNewLine +
'For more detailed information, please visit https://jrsoftware.org/ishelp/index.php?topic=setupcmdline';
MessageBox(0, PChar(Help), 'Setup', MB_OK or MB_ICONSTOP);
end;
var
SelfFilename: String;
SourceF, DestF: TFile;
OffsetTable: PSetupLdrOffsetTable;
TempDir: String;
S: String;
TempFile: String;
TestID: TSetupID;
P: Pointer;
Reader: TCompressedBlockReader;
I: Integer;
begin
try
{ Ensure all of SetupLdr is paged in so that in the case of a disk spanning
install Windows will never complain when the disk/CD containing SetupLdr
is ejected. }
RunImageLocally(HInstance);
ProcessCommandLine;
SelfFilename := NewParamStr(0);
SourceF := TFile.Create(SelfFilename, fdOpenExisting, faRead, fsRead);
try
OffsetTable := GetSetupLdrOffsetTable;
{ Note: We don't check the OffsetTable.ID here because it would put a
copy of the ID in the data section, and that would confuse external
programs that search for the offset table by ID. }
if (OffsetTable.Version <> SetupLdrOffsetTableVersion) or
(GetCRC32(OffsetTable^, SizeOf(OffsetTable^) - SizeOf(OffsetTable.TableCRC)) <> OffsetTable.TableCRC) or
((SourceF.Size.Hi = 0) and (SourceF.Size.Lo < OffsetTable.TotalSize)) then
SetupCorruptError;
SourceF.Seek(OffsetTable.Offset0);
SourceF.ReadBuffer(TestID, SizeOf(TestID));
if TestID <> SetupID then
SetupCorruptError;
try
Reader := TCompressedBlockReader.Create(SourceF, TLZMA1SmallDecompressor);
try
SECompressedBlockRead(Reader, SetupHeader, SizeOf(SetupHeader),
SetupHeaderStrings, SetupHeaderAnsiStrings);
LanguageEntryCount := SetupHeader.NumLanguageEntries;
LanguageEntries := AllocMem(LanguageEntryCount * SizeOf(TSetupLanguageEntry));
for I := 0 to LanguageEntryCount-1 do
SECompressedBlockRead(Reader, LanguageEntries[I], SizeOf(LanguageEntries[I]),
SetupLanguageEntryStrings, SetupLanguageEntryAnsiStrings);
finally
Reader.Free;
end;
except
on ECompressDataError do
SetupCorruptError;
end;
ActivateDefaultLanguage;
if InitShowHelp then begin
{ Show the command line help. }
ShowHelp(SetupMessages[msgHelpTextNote]);
SetupLdrExitCode := 0;
end else begin
{ Show the startup prompt. If this is enabled, SetupHeader.AppName won't
have constants. }
if not(shDisableStartupPrompt in SetupHeader.Options) and
not InitDisableStartupPrompt and
(MessageBox(0, PChar(FmtSetupMessage1(msgSetupLdrStartupMessage, SetupHeader.AppName)),
PChar(SetupMessages[msgSetupAppTitle]), MB_YESNO or MB_ICONQUESTION) <> IDYES) then begin
SetupLdrExitCode := ecCancelledBeforeInstall;
Abort;
end;
{ Create a temporary directory, and extract the embedded setup program
there }
Randomize;
TempDir := CreateTempDir(IsAdminLoggedOn);
S := AddBackslash(TempDir) + PathChangeExt(PathExtractName(SelfFilename), '.tmp');
TempFile := S; { assign only if string was successfully constructed }
SourceF.Seek(OffsetTable.OffsetEXE);
try
P := nil;
DestF := TFile.Create(TempFile, fdCreateAlways, faWrite, fsNone);
try
GetMem(P, OffsetTable.UncompressedSizeEXE);
FillChar(P^, OffsetTable.UncompressedSizeEXE, 0);
try
Reader := TCompressedBlockReader.Create(SourceF, TLZMA1SmallDecompressor);
try
Reader.Read(P^, OffsetTable.UncompressedSizeEXE);
finally
Reader.Free;
end;
except
on ECompressDataError do
SetupCorruptError;
end;
TransformCallInstructions(P^, OffsetTable.UncompressedSizeEXE, False, 0);
if GetCRC32(P^, OffsetTable.UncompressedSizeEXE) <> OffsetTable.CRCEXE then
SetupCorruptError;
{ Preallocate the bytes to avoid file system fragmentation }
DestF.Seek(OffsetTable.UncompressedSizeEXE);
DestF.Truncate;
DestF.Seek(0);
DestF.WriteBuffer(P^, OffsetTable.UncompressedSizeEXE);
finally
FreeMem(P);
DestF.Free;
end;
except
on E: EFileError do begin
SetLastError(E.ErrorCode);
RaiseLastError(msgLdrCannotCreateTemp);
end;
end;
FreeAndNil(SourceF);
{ Create SetupLdrWnd, which is used by Setup to communicate with
SetupLdr }
SetupLdrWnd := CreateWindowEx(0, 'STATIC', 'InnoSetupLdrWindow', 0,
0, 0, 0, 0, HWND_DESKTOP, 0, HInstance, nil);
Longint(OrigWndProc) := SetWindowLong(SetupLdrWnd, GWL_WNDPROC,
Longint(@SetupLdrWndProc));
{ Now execute Setup. Use the exit code it returns as our exit code. }
ExecAndWait(TempFile, Format('/SL5="$%x,%d,%d,',
[SetupLdrWnd, OffsetTable.Offset0, OffsetTable.Offset1]) +
SelfFilename + '" ' + GetCmdTail, SetupLdrExitCode);
{ Synchronize our active language with Setup's, in case we need to
display any messages below }
if PendingNewLanguage <> -1 then
SetActiveLanguage(PendingNewLanguage);
end;
finally
SourceF.Free;
if TempFile <> '' then
{ Even though Setup has terminated by now, the system may still have
the file locked for a short period of time (esp. on multiprocessor
systems), so use DelayDeleteFile to delete it. }
DelayDeleteFile(False, TempFile, 13, 50, 250);
if TempDir <> '' then
RemoveDirectory(PChar(TempDir));
if SetupLdrWnd <> 0 then
{ SetupLdrWnd must be destroyed before RestartComputer is called,
otherwise SetupLdrWndProc would deny the shutdown request }
DestroyWindow(SetupLdrWnd);
if Assigned(LanguageEntries) then begin
Finalize(LanguageEntries[0], LanguageEntryCount);
FreeMem(LanguageEntries);
LanguageEntries := nil;
end;
end;
if RestartSystem then begin
if not RestartComputer then
MessageBox(0, PChar(SetupMessages[msgErrorRestartingComputer]),
PChar(SetupMessages[msgErrorTitle]), MB_OK or MB_ICONEXCLAMATION or
MB_SETFOREGROUND);
end;
except
ShowExceptionMsg;
end;
Halt(SetupLdrExitCode);
end.