- Merge from trunk up to r45543
[reactos.git] / boot / freeldr / freeldr / windows / amd64 / ntsetup.c
1 /*
2 * PROJECT: EFI Windows Loader
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: freeldr/windows/i386/ntsetup.c
5 * PURPOSE: i386-specific setup for Windows boot
6 * PROGRAMMERS: Aleksey Bragin (aleksey@reactos.org)
7 */
8
9 /* INCLUDES ***************************************************************/
10
11 #include <freeldr.h>
12 #include <debug.h>
13
14 // this is needed for new IDT filling
15 #if 0
16 extern ULONG_PTR i386DivideByZero;
17 extern ULONG_PTR i386DebugException;
18 extern ULONG_PTR i386NMIException;
19 extern ULONG_PTR i386Breakpoint;
20 extern ULONG_PTR i386Overflow;
21 extern ULONG_PTR i386BoundException;
22 extern ULONG_PTR i386InvalidOpcode;
23 extern ULONG_PTR i386FPUNotAvailable;
24 extern ULONG_PTR i386DoubleFault;
25 extern ULONG_PTR i386CoprocessorSegment;
26 extern ULONG_PTR i386InvalidTSS;
27 extern ULONG_PTR i386SegmentNotPresent;
28 extern ULONG_PTR i386StackException;
29 extern ULONG_PTR i386GeneralProtectionFault;
30 extern ULONG_PTR i386PageFault; // exc 14
31 extern ULONG_PTR i386CoprocessorError; // exc 16
32 extern ULONG_PTR i386AlignmentCheck; // exc 17
33 #endif
34
35 /* FUNCTIONS **************************************************************/
36
37 // Last step before going virtual
38 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock,
39 PVOID *GdtIdt,
40 ULONG *PcrBasePage,
41 ULONG *TssBasePage)
42 {
43 ULONG TssSize;
44 ULONG TssPages;
45 ULONG_PTR Pcr = 0;
46 ULONG_PTR Tss = 0;
47 ULONG BlockSize, NumPages;
48
49 LoaderBlock->u.I386.CommonDataArea = (PVOID)DbgPrint; // HACK
50 LoaderBlock->u.I386.MachineType = MACHINE_TYPE_ISA;
51
52 /* Allocate 2 pages for PCR */
53 Pcr = (ULONG_PTR)MmAllocateMemoryWithType(2 * MM_PAGE_SIZE, LoaderStartupPcrPage);
54 *PcrBasePage = Pcr >> MM_PAGE_SHIFT;
55 RtlZeroMemory((PVOID)Pcr, 2 * MM_PAGE_SIZE);
56
57 if (Pcr == 0)
58 {
59 UiMessageBox("Can't allocate PCR\n");
60 return;
61 }
62
63 /* Allocate TSS */
64 TssSize = (sizeof(KTSS) + MM_PAGE_SIZE) & ~(MM_PAGE_SIZE - 1);
65 TssPages = TssSize / MM_PAGE_SIZE;
66
67 Tss = (ULONG_PTR)MmAllocateMemoryWithType(TssSize, LoaderMemoryData);
68
69 *TssBasePage = Tss >> MM_PAGE_SHIFT;
70
71 /* Allocate space for new GDT + IDT */
72 BlockSize = NUM_GDT*sizeof(KGDTENTRY) + NUM_IDT*sizeof(KIDTENTRY);//FIXME: Use GDT/IDT limits here?
73 NumPages = (BlockSize + MM_PAGE_SIZE - 1) >> MM_PAGE_SHIFT;
74 *GdtIdt = (PKGDTENTRY)MmAllocateMemoryWithType(NumPages * MM_PAGE_SIZE, LoaderMemoryData);
75
76 if (*GdtIdt == NULL)
77 {
78 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
79 return;
80 }
81
82 /* Zero newly prepared GDT+IDT */
83 RtlZeroMemory(*GdtIdt, NumPages << MM_PAGE_SHIFT);
84 }