2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ke/freeldr.c
5 * PURPOSE: FreeLDR Bootstrap Support
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES *****************************************************************/
16 #include <ppcmmu/mmu.h>
17 #define KERNEL_RVA(x) RVA(x,0x80800000)
18 #define KERNEL_DESCRIPTOR_PAGE(x) (((ULONG_PTR)x + KernelBase) >> PAGE_SHIFT)
20 #define KERNEL_RVA(x) RVA(x,KSEG0_BASE)
21 #define KERNEL_DESCRIPTOR_PAGE(x) (((ULONG_PTR)x &~ KSEG0_BASE) >> PAGE_SHIFT)
24 typedef struct _BIOS_MEMORY_DESCRIPTOR
28 } BIOS_MEMORY_DESCRIPTOR
, *PBIOS_MEMORY_DESCRIPTOR
;
30 /* GLOBALS *******************************************************************/
32 /* FreeLDR Loader Data */
33 PROS_LOADER_PARAMETER_BLOCK KeRosLoaderBlock
;
34 ADDRESS_RANGE KeMemoryMap
[64];
35 ULONG KeMemoryMapRangeCount
;
37 /* NT Loader Module/Descriptor Count */
41 /* NT Loader Data. Eats up about 100KB! */
42 LOADER_PARAMETER_BLOCK BldrLoaderBlock
; // 0x0000
43 LOADER_PARAMETER_EXTENSION BldrExtensionBlock
; // 0x0060
44 CHAR BldrCommandLine
[256]; // 0x00DC
45 CHAR BldrArcBootPath
[64]; // 0x01DC
46 CHAR BldrArcHalPath
[64]; // 0x021C
47 CHAR BldrNtHalPath
[64]; // 0x025C
48 CHAR BldrNtBootPath
[64]; // 0x029C
49 LDR_DATA_TABLE_ENTRY BldrModules
[64]; // 0x02DC
50 MEMORY_ALLOCATION_DESCRIPTOR BldrMemoryDescriptors
[60]; // 0x14DC
51 WCHAR BldrModuleStrings
[64][260]; // 0x19DC
52 WCHAR BldrModuleStringsFull
[64][260]; // 0x9BDC
53 NLS_DATA_BLOCK BldrNlsDataBlock
; // 0x11DDC
54 SETUP_LOADER_BLOCK BldrSetupBlock
; // 0x11DE8
55 ARC_DISK_INFORMATION BldrArcDiskInfo
; // 0x12134
56 CHAR BldrArcNames
[32][256]; // 0x1213C
57 ARC_DISK_SIGNATURE BldrDiskInfo
[32]; // 0x1413C
58 CHAR BldrArcHwBuffer
[16 * 1024]; // 0x1843C
61 BIOS_MEMORY_DESCRIPTOR BiosMemoryDescriptors
[16] = { { 0, 0 }, };
62 PBIOS_MEMORY_DESCRIPTOR BiosMemoryDescriptorList
= BiosMemoryDescriptors
;
65 ULONG NumberDescriptors
= 0;
66 MEMORY_DESCRIPTOR MDArray
[60] = { { 0, 0, 0 }, };
73 /* Old boot style IDT */
74 KIDTENTRY KiBootIdt
[256];
77 KGDTENTRY KiBootGdt
[256] =
79 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_NULL */
80 {0xffff, 0x0000, {{0x00, 0x9b, 0xcf, 0x00}}}, /* KGDT_R0_CODE */
81 {0xffff, 0x0000, {{0x00, 0x93, 0xcf, 0x00}}}, /* KGDT_R0_DATA */
82 {0xffff, 0x0000, {{0x00, 0xfb, 0xcf, 0x00}}}, /* KGDT_R3_CODE */
83 {0xffff, 0x0000, {{0x00, 0xf3, 0xcf, 0x00}}}, /* KGDT_R3_DATA*/
84 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_TSS */
85 {0x0001, 0xf000, {{0xdf, 0x93, 0xc0, 0xff}}}, /* KGDT_R0_PCR */
86 {0x0fff, 0x0000, {{0x00, 0xf3, 0x40, 0x00}}}, /* KGDT_R3_TEB */
87 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_UNUSED */
88 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_LDT */
89 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}}, /* KGDT_DF_TSS */
90 {0x0000, 0x0000, {{0x00, 0x00, 0x00, 0x00}}} /* KGDT_NMI_TSS */
94 KDESCRIPTOR KiGdtDescriptor
= {0, sizeof(KiBootGdt
) - 1, (ULONG
)KiBootGdt
};
98 /* FUNCTIONS *****************************************************************/
100 PMEMORY_ALLOCATION_DESCRIPTOR
102 KiRosGetMdFromArray(VOID
)
104 /* Return the next MD from the list, but make sure we don't overflow */
105 if (BldrCurrentMd
> 60) ASSERT(FALSE
);
106 return &BldrMemoryDescriptors
[BldrCurrentMd
++];
111 KiRosAddBiosBlock(ULONG Address
,
114 PBIOS_MEMORY_DESCRIPTOR BiosBlock
= BiosMemoryDescriptorList
;
116 /* Loop our BIOS Memory Descriptor List */
117 while (BiosBlock
->BlockSize
> 0)
119 /* Check if we've found a matching head block */
120 if (Address
+ Size
== BiosBlock
->BlockBase
)
122 /* Simply enlarge and rebase it */
123 BiosBlock
->BlockBase
= Address
;
124 BiosBlock
->BlockSize
+= Size
;
128 /* Check if we've found a matching tail block */
129 if (Address
== (BiosBlock
->BlockBase
+ BiosBlock
->BlockSize
))
131 /* Simply enlarge it */
132 BiosBlock
->BlockSize
+= Size
;
136 /* Nothing suitable found, try the next block */
140 /* No usable blocks found, found a free block instead */
141 if (!BiosBlock
->BlockSize
)
144 BiosBlock
->BlockBase
= Address
;
145 BiosBlock
->BlockSize
= Size
;
147 /* Create a new block and mark it as the end of the array */
149 BiosBlock
->BlockBase
= BiosBlock
->BlockSize
= 0L;
155 KiRosBuildBiosMemoryMap(VOID
)
157 ULONG BlockBegin
, BlockEnd
;
160 /* Loop the BIOS Memory Map */
161 for (j
= 0; j
< KeMemoryMapRangeCount
; j
++)
163 /* Get the start and end addresses */
164 BlockBegin
= KeMemoryMap
[j
].BaseAddrLow
;
165 BlockEnd
= KeMemoryMap
[j
].BaseAddrLow
+ KeMemoryMap
[j
].LengthLow
- 1;
167 /* Make sure this isn't a > 4GB descriptor */
168 if (!KeMemoryMap
[j
].BaseAddrHigh
)
170 /* Make sure we don't overflow */
171 if (BlockEnd
< BlockBegin
) BlockEnd
= 0xFFFFFFFF;
173 /* Check if this is free memory */
174 if (KeMemoryMap
[j
].Type
== 1)
176 /* Add it to our BIOS descriptors */
177 KiRosAddBiosBlock(BlockBegin
, BlockEnd
- BlockBegin
+ 1);
185 KiRosAllocateArcDescriptor(IN ULONG PageBegin
,
187 IN MEMORY_TYPE MemoryType
)
191 /* Loop all our descriptors */
192 for (i
= 0; i
< NumberDescriptors
; i
++)
194 /* Attempt to fing a free block that describes our region */
195 if ((MDArray
[i
].MemoryType
== MemoryFree
) &&
196 (MDArray
[i
].BasePage
<= PageBegin
) &&
197 (MDArray
[i
].BasePage
+ MDArray
[i
].PageCount
> PageBegin
) &&
198 (MDArray
[i
].BasePage
+ MDArray
[i
].PageCount
>= PageEnd
))
205 /* Check if we found no free blocks, and fail if so */
206 if (i
== NumberDescriptors
) return ENOMEM
;
208 /* Check if the block has our base address */
209 if (MDArray
[i
].BasePage
== PageBegin
)
211 /* Check if it also has our ending address */
212 if ((MDArray
[i
].BasePage
+ MDArray
[i
].PageCount
) == PageEnd
)
214 /* Then convert this region into our new memory type */
215 MDArray
[i
].MemoryType
= MemoryType
;
219 /* Otherwise, make sure we have enough descriptors */
220 if (NumberDescriptors
== 60) return ENOMEM
;
222 /* Cut this descriptor short */
223 MDArray
[i
].BasePage
= PageEnd
;
224 MDArray
[i
].PageCount
-= (PageEnd
- PageBegin
);
226 /* And allocate a new descriptor for our memory range */
227 MDArray
[NumberDescriptors
].BasePage
= PageBegin
;
228 MDArray
[NumberDescriptors
].PageCount
= PageEnd
- PageBegin
;
229 MDArray
[NumberDescriptors
].MemoryType
= MemoryType
;
233 else if ((MDArray
[i
].BasePage
+ MDArray
[i
].PageCount
) == PageEnd
)
235 /* This block has our end address, make sure we have a free block */
236 if (NumberDescriptors
== 60) return ENOMEM
;
238 /* Rebase this descriptor */
239 MDArray
[i
].PageCount
= PageBegin
- MDArray
[i
].BasePage
;
241 /* And allocate a new descriptor for our memory range */
242 MDArray
[NumberDescriptors
].BasePage
= PageBegin
;
243 MDArray
[NumberDescriptors
].PageCount
= PageEnd
- PageBegin
;
244 MDArray
[NumberDescriptors
].MemoryType
= MemoryType
;
249 /* We'll need two descriptors, make sure they're available */
250 if ((NumberDescriptors
+ 1) >= 60) return ENOMEM
;
252 /* Allocate a free memory descriptor for what follows us */
253 MDArray
[NumberDescriptors
].BasePage
= PageEnd
;
254 MDArray
[NumberDescriptors
].PageCount
= MDArray
[i
].PageCount
-
255 (PageEnd
- MDArray
[i
].BasePage
);
256 MDArray
[NumberDescriptors
].MemoryType
= MemoryFree
;
259 /* Cut down the current free descriptor */
260 MDArray
[i
].PageCount
= PageBegin
- MDArray
[i
].BasePage
;
262 /* Allocate a new memory descriptor for our memory range */
263 MDArray
[NumberDescriptors
].BasePage
= PageBegin
;
264 MDArray
[NumberDescriptors
].PageCount
= PageEnd
- PageBegin
;
265 MDArray
[NumberDescriptors
].MemoryType
= MemoryType
;
269 /* Everything went well */
270 return STATUS_SUCCESS
;
275 KiRosConfigureArcDescriptor(IN ULONG PageBegin
,
277 IN TYPE_OF_MEMORY MemoryType
)
280 ULONG BlockBegin
, BlockEnd
;
281 MEMORY_TYPE BlockType
;
282 BOOLEAN Combined
= FALSE
;
284 /* If this descriptor seems bogus, just return */
285 if (PageEnd
<= PageBegin
) return STATUS_SUCCESS
;
287 /* Loop every ARC descriptor, trying to find one we can modify */
288 for (i
= 0; i
< NumberDescriptors
; i
++)
290 /* Get its settings */
291 BlockBegin
= MDArray
[i
].BasePage
;
292 BlockEnd
= MDArray
[i
].BasePage
+ MDArray
[i
].PageCount
;
293 BlockType
= MDArray
[i
].MemoryType
;
295 /* Check if we can fit inside this block */
296 if (BlockBegin
< PageBegin
)
298 /* Check if we are larger then it */
299 if ((BlockEnd
> PageBegin
) && (BlockEnd
<= PageEnd
))
301 /* Make it end where we start */
302 BlockEnd
= PageBegin
;
305 /* Check if it ends after we do */
306 if (BlockEnd
> PageEnd
)
308 /* Make sure we can allocate a descriptor */
309 if (NumberDescriptors
== 60) return ENOMEM
;
311 /* Create a descriptor for whatever memory we're not part of */
312 MDArray
[NumberDescriptors
].MemoryType
= BlockType
;
313 MDArray
[NumberDescriptors
].BasePage
= PageEnd
;
314 MDArray
[NumberDescriptors
].PageCount
= BlockEnd
- PageEnd
;
317 /* The next block ending is now where we begin */
318 BlockEnd
= PageBegin
;
323 /* Check if the blog begins inside our range */
324 if (BlockBegin
< PageEnd
)
326 /* Check if it ends before we do */
327 if (BlockEnd
< PageEnd
)
329 /* Then make it disappear */
330 BlockEnd
= BlockBegin
;
334 /* Otherwise make it start where we end */
335 BlockBegin
= PageEnd
;
340 /* Check if the block matches us, and we haven't tried combining yet */
341 if (((TYPE_OF_MEMORY
)BlockType
== MemoryType
) && !(Combined
))
343 /* Check if it starts where we end */
344 if (BlockBegin
== PageEnd
)
346 /* Make it start with us, and combine us */
347 BlockBegin
= PageBegin
;
350 else if (BlockEnd
== PageBegin
)
352 /* Otherwise, it ends where we begin, combine its ending */
358 /* Check the original block data matches with what we came up with */
359 if ((MDArray
[i
].BasePage
== BlockBegin
) &&
360 (MDArray
[i
].PageCount
== BlockEnd
- BlockBegin
))
366 /* Otherwise, set our new settings for this block */
367 MDArray
[i
].BasePage
= BlockBegin
;
368 MDArray
[i
].PageCount
= BlockEnd
- BlockBegin
;
370 /* Check if we are killing the block */
371 if (BlockBegin
== BlockEnd
)
373 /* Delete this block and restart the loop properly */
375 if (i
< NumberDescriptors
) MDArray
[i
] = MDArray
[NumberDescriptors
];
380 /* If we got here without combining, we need to allocate a new block */
381 if (!(Combined
) && (MemoryType
< LoaderMaximum
))
383 /* Make sure there's enough descriptors */
384 if (NumberDescriptors
== 60) return ENOMEM
;
386 /* Allocate a new block with our data */
387 MDArray
[NumberDescriptors
].MemoryType
= MemoryType
;
388 MDArray
[NumberDescriptors
].BasePage
= PageBegin
;
389 MDArray
[NumberDescriptors
].PageCount
= PageEnd
- PageBegin
;
393 /* Changes complete, return success */
394 return STATUS_SUCCESS
;
399 KiRosBuildOsMemoryMap(VOID
)
401 PBIOS_MEMORY_DESCRIPTOR MdBlock
;
402 ULONG BlockStart
, BlockEnd
, BiasedStart
, BiasedEnd
, PageStart
, PageEnd
;
403 NTSTATUS Status
= STATUS_SUCCESS
;
404 ULONG BiosPage
= 0xA0;
406 /* Loop the BIOS Memory Descriptor List */
407 MdBlock
= BiosMemoryDescriptorList
;
408 while (MdBlock
->BlockSize
)
410 /* Get the statrt and end addresses */
411 BlockStart
= MdBlock
->BlockBase
;
412 BlockEnd
= BlockStart
+ MdBlock
->BlockSize
- 1;
414 /* Align them to page boundaries */
415 BiasedStart
= BlockStart
& (PAGE_SIZE
- 1);
416 if (BiasedStart
) BlockStart
= BlockStart
+ PAGE_SIZE
- BiasedStart
;
417 BiasedEnd
= (BlockEnd
+ 1) & (ULONG
)(PAGE_SIZE
- 1);
418 if (BiasedEnd
) BlockEnd
-= BiasedEnd
;
420 /* Get the actual page numbers */
421 PageStart
= BlockStart
>> PAGE_SHIFT
;
422 PageEnd
= (BlockEnd
+ 1) >> PAGE_SHIFT
;
424 /* If we're starting at page 0, then put the BIOS page at the end */
425 if (!PageStart
) BiosPage
= PageEnd
;
427 /* Check if we did any alignment */
430 /* Mark that region as reserved */
431 Status
= KiRosConfigureArcDescriptor(PageStart
- 1,
433 MemorySpecialMemory
);
434 if (Status
!= STATUS_SUCCESS
) break;
437 /* Check if we did any alignment */
440 /* Mark that region as reserved */
441 Status
= KiRosConfigureArcDescriptor(PageEnd
- 1,
443 MemorySpecialMemory
);
444 if (Status
!= STATUS_SUCCESS
) break;
446 /* If the bios page was the last page, use the next one instead */
447 if (BiosPage
== PageEnd
) BiosPage
+= 1;
450 /* Check if the page is below the 16MB Memory hole */
451 if (PageEnd
<= 0xFC0)
453 /* It is, mark the memory a free */
454 Status
= KiRosConfigureArcDescriptor(PageStart
,
458 else if (PageStart
>= 0x1000)
460 /* It's over 16MB, so that memory gets marked as reserve */
461 Status
= KiRosConfigureArcDescriptor(PageStart
,
467 /* Check if it starts below the memory hole */
468 if (PageStart
< 0xFC0)
470 /* Mark that part as free */
471 Status
= KiRosConfigureArcDescriptor(PageStart
,
474 if (Status
!= STATUS_SUCCESS
) break;
476 /* And update the page start for the code below */
480 /* Any code in the memory hole region ends up as reserve */
481 Status
= KiRosConfigureArcDescriptor(PageStart
,
486 /* If we failed, break out, otherwise, go to the next BIOS block */
487 if (Status
!= STATUS_SUCCESS
) break;
491 /* If anything failed until now, return error code */
492 if (Status
!= STATUS_SUCCESS
) return Status
;
495 /* Set the top 16MB region as reserved */
496 Status
= KiRosConfigureArcDescriptor(0xFC0, 0x1000, MemorySpecialMemory
);
497 if (Status
!= STATUS_SUCCESS
) return Status
;
499 /* Setup the BIOS region as reserved */
500 KiRosConfigureArcDescriptor(0xA0, 0x100, LoaderMaximum
);
501 KiRosConfigureArcDescriptor(BiosPage
, 0x100, MemoryFirmwarePermanent
);
503 /* Build an entry for the IVT */
504 Status
= KiRosAllocateArcDescriptor(0, 1, MemoryFirmwarePermanent
);
505 if (Status
!= STATUS_SUCCESS
) return Status
;
508 /* Build an entry for the KPCR and KUSER_SHARED_DATA */
509 Status
= KiRosAllocateArcDescriptor(1, 3, LoaderStartupPcrPage
);
510 if (Status
!= STATUS_SUCCESS
) return Status
;
512 /* Build an entry for the PDE and return the status */
513 Status
= KiRosAllocateArcDescriptor(KeRosLoaderBlock
->
514 PageDirectoryStart
>> PAGE_SHIFT
,
516 PageDirectoryEnd
>> PAGE_SHIFT
,
524 KiRosBuildReservedMemoryMap(VOID
)
527 ULONG BlockBegin
, BlockEnd
, BiasedPage
;
529 /* Loop the BIOS Memory Map */
530 for (j
= 0; j
< KeMemoryMapRangeCount
; j
++)
532 /* Get the start and end addresses */
533 BlockBegin
= KeMemoryMap
[j
].BaseAddrLow
;
534 BlockEnd
= BlockBegin
+ KeMemoryMap
[j
].LengthLow
- 1;
536 /* Make sure it wasn't a > 4GB descriptor */
537 if (!KeMemoryMap
[j
].BaseAddrHigh
)
539 /* Make sure it doesn't overflow */
540 if (BlockEnd
< BlockBegin
) BlockEnd
= 0xFFFFFFFF;
542 /* Check if this was free memory */
543 if (KeMemoryMap
[j
].Type
== 1)
545 /* Get the page-aligned addresses */
546 BiasedPage
= BlockBegin
& (PAGE_SIZE
- 1);
547 BlockBegin
>>= PAGE_SHIFT
;
548 if (BiasedPage
) BlockBegin
++;
549 BlockEnd
= (BlockEnd
>> PAGE_SHIFT
) + 1;
551 /* Check if the block is within the 16MB memory hole */
552 if ((BlockBegin
< 0xFC0) && (BlockEnd
>= 0xFC0))
554 /* Don't allow it to cross this boundary */
558 /* Check if the boundary is across 16MB */
559 if ((BlockEnd
> 0xFFF) && (BlockBegin
<= 0xFFF))
561 /* Don't let it cross */
565 /* Check if the block describes the memory hole */
566 if ((BlockBegin
>= 0xFC0) && (BlockEnd
<= 0xFFF))
568 /* Set this region as temporary */
569 KiRosConfigureArcDescriptor(BlockBegin
,
571 MemoryFirmwareTemporary
);
576 /* Get the page-aligned addresses */
577 BlockBegin
>>= PAGE_SHIFT
;
578 BiasedPage
= (BlockEnd
+ 1) & (PAGE_SIZE
- 1);
579 BlockEnd
>>= PAGE_SHIFT
;
580 if (BiasedPage
) BlockEnd
++;
582 /* Set this memory as reserved */
583 KiRosConfigureArcDescriptor(BlockBegin
,
585 MemorySpecialMemory
);
594 KiRosInsertNtDescriptor(IN PMEMORY_ALLOCATION_DESCRIPTOR NewDescriptor
)
596 PLIST_ENTRY ListHead
, PreviousEntry
, NextEntry
;
597 PMEMORY_ALLOCATION_DESCRIPTOR Descriptor
= NULL
, NextDescriptor
= NULL
;
599 /* Loop the memory descriptor list */
600 ListHead
= &KeLoaderBlock
->MemoryDescriptorListHead
;
601 PreviousEntry
= ListHead
;
602 NextEntry
= ListHead
->Flink
;
603 while (NextEntry
!= ListHead
)
605 /* Get the current descriptor and check if it's below ours */
606 NextDescriptor
= CONTAINING_RECORD(NextEntry
,
607 MEMORY_ALLOCATION_DESCRIPTOR
,
609 if (NewDescriptor
->BasePage
< NextDescriptor
->BasePage
) break;
611 /* It isn't, save the previous entry and descriptor, and try again */
612 PreviousEntry
= NextEntry
;
613 Descriptor
= NextDescriptor
;
614 NextEntry
= NextEntry
->Flink
;
617 /* So we found the right spot to insert. Is this free memory? */
618 if (NewDescriptor
->MemoryType
!= LoaderFree
)
620 /* It isn't, so insert us before the last descriptor */
621 InsertHeadList(PreviousEntry
, &NewDescriptor
->ListEntry
);
625 /* We're free memory. Check if the entry we found is also free memory */
626 if ((PreviousEntry
!= ListHead
) &&
627 ((Descriptor
->MemoryType
== LoaderFree
) ||
628 (Descriptor
->MemoryType
== LoaderReserve
)) &&
629 ((Descriptor
->BasePage
+ Descriptor
->PageCount
) ==
630 NewDescriptor
->BasePage
))
632 /* It's free memory, and we're right after it. Enlarge that block */
633 Descriptor
->PageCount
+= NewDescriptor
->PageCount
;
634 NewDescriptor
= Descriptor
;
638 /* Our range scan't be combined, so just insert us separately */
639 InsertHeadList(PreviousEntry
, &NewDescriptor
->ListEntry
);
642 /* Check if we merged with an existing free memory block */
643 if ((NextEntry
!= ListHead
) &&
644 ((NextDescriptor
->MemoryType
== LoaderFree
) ||
645 (NextDescriptor
->MemoryType
== LoaderReserve
)) &&
646 ((NewDescriptor
->BasePage
+ NewDescriptor
->PageCount
) ==
647 NextDescriptor
->BasePage
))
649 /* Update our own block */
650 NewDescriptor
->PageCount
+= NextDescriptor
->PageCount
;
652 /* Remove the next block */
653 RemoveEntryList(&NextDescriptor
->ListEntry
);
660 KiRosBuildNtDescriptor(IN PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
,
661 IN MEMORY_TYPE MemoryType
,
665 PMEMORY_ALLOCATION_DESCRIPTOR Descriptor
, NextDescriptor
= NULL
;
667 TYPE_OF_MEMORY CurrentType
;
670 /* Check how many pages we'll be consuming */
671 Delta
= BasePage
- MemoryDescriptor
->BasePage
;
672 if (!(Delta
) && (PageCount
== MemoryDescriptor
->PageCount
))
674 /* We can simply convert the current descriptor into our new type */
675 MemoryDescriptor
->MemoryType
= MemoryType
;
679 /* Get the current memory type of the descriptor, and reserve it */
680 CurrentType
= MemoryDescriptor
->MemoryType
;
681 MemoryDescriptor
->MemoryType
= LoaderSpecialMemory
;
683 /* Check if we'll need another descriptor for what's left of memory */
684 UseNext
= ((BasePage
!= MemoryDescriptor
->BasePage
) &&
685 (Delta
+ PageCount
!= MemoryDescriptor
->PageCount
));
687 /* Get a descriptor */
688 Descriptor
= KiRosGetMdFromArray();
689 if (!Descriptor
) return STATUS_INSUFFICIENT_RESOURCES
;
691 /* Check if we are using another descriptor */
694 /* Allocate that one too */
695 NextDescriptor
= KiRosGetMdFromArray();
696 if (!NextDescriptor
) return STATUS_INSUFFICIENT_RESOURCES
;
699 /* Build the descriptor we got */
700 Descriptor
->MemoryType
= MemoryType
;
701 Descriptor
->BasePage
= BasePage
;
702 Descriptor
->PageCount
= PageCount
;
704 /* Check if we're starting at the same place as the old one */
705 if (BasePage
== MemoryDescriptor
->BasePage
)
707 /* Simply decrease the old descriptor and rebase it */
708 MemoryDescriptor
->BasePage
+= PageCount
;
709 MemoryDescriptor
->PageCount
-= PageCount
;
710 MemoryDescriptor
->MemoryType
= CurrentType
;
712 else if (Delta
+ PageCount
== MemoryDescriptor
->PageCount
)
714 /* We finish where the old one did, shorten it */
715 MemoryDescriptor
->PageCount
-= PageCount
;
716 MemoryDescriptor
->MemoryType
= CurrentType
;
720 /* We're inside the current block, mark our free region */
721 NextDescriptor
->MemoryType
= LoaderFree
;
722 NextDescriptor
->BasePage
= BasePage
+ PageCount
;
723 NextDescriptor
->PageCount
= MemoryDescriptor
->PageCount
-
726 /* And cut down the current descriptor */
727 MemoryDescriptor
->PageCount
= Delta
;
728 MemoryDescriptor
->MemoryType
= CurrentType
;
730 /* Finally, insert our new free descriptor into the list */
731 KiRosInsertNtDescriptor(NextDescriptor
);
734 /* Insert the descriptor we allocated */
735 KiRosInsertNtDescriptor(Descriptor
);
739 return STATUS_SUCCESS
;
742 PMEMORY_ALLOCATION_DESCRIPTOR
744 KiRosFindNtDescriptor(IN ULONG BasePage
)
746 PMEMORY_ALLOCATION_DESCRIPTOR MdBlock
= NULL
;
747 PLIST_ENTRY NextEntry
, ListHead
;
749 /* Scan the memory descriptor list */
750 ListHead
= &KeLoaderBlock
->MemoryDescriptorListHead
;
751 NextEntry
= ListHead
->Flink
;
752 while (NextEntry
!= ListHead
)
754 /* Get the current descriptor */
755 MdBlock
= CONTAINING_RECORD(NextEntry
,
756 MEMORY_ALLOCATION_DESCRIPTOR
,
759 /* Check if it can contain our memory range */
760 if ((MdBlock
->BasePage
<= BasePage
) &&
761 (MdBlock
->BasePage
+ MdBlock
->PageCount
> BasePage
))
763 /* It can, break out */
767 /* Go to the next descriptor */
768 NextEntry
= NextEntry
->Flink
;
771 /* Return the descriptor we found, if any */
777 KiRosAllocateNtDescriptor(IN TYPE_OF_MEMORY MemoryType
,
781 OUT PULONG ReturnedBase
)
783 PMEMORY_ALLOCATION_DESCRIPTOR MdBlock
;
784 ULONG AlignedBase
, AlignedLimit
;
785 PMEMORY_ALLOCATION_DESCRIPTOR ActiveMdBlock
;
786 ULONG ActiveAlignedBase
= 0;
787 PLIST_ENTRY NextEntry
, ListHead
;
789 /* If no information was given, make some assumptions */
790 if (!Alignment
) Alignment
= 1;
791 if (!PageCount
) PageCount
= 1;
793 /* Start looking for a matching descvriptor */
796 /* Calculate the limit of the range */
797 AlignedLimit
= PageCount
+ BasePage
;
799 /* Find a descriptor that already contains our base address */
800 MdBlock
= KiRosFindNtDescriptor(BasePage
);
804 /* If it contains our limit as well, break out early */
805 if ((MdBlock
->PageCount
+ MdBlock
->BasePage
) >= AlignedLimit
) break;
808 /* Loop the memory list */
809 ActiveMdBlock
= NULL
;
810 ListHead
= &KeLoaderBlock
->MemoryDescriptorListHead
;
811 NextEntry
= ListHead
->Flink
;
812 while (NextEntry
!= ListHead
)
814 /* Get the current descriptors */
815 MdBlock
= CONTAINING_RECORD(NextEntry
,
816 MEMORY_ALLOCATION_DESCRIPTOR
,
819 /* Align the base address and our limit */
820 AlignedBase
= (MdBlock
->BasePage
+ (Alignment
- 1)) &~ Alignment
;
821 AlignedLimit
= MdBlock
->PageCount
-
825 /* Check if this is a free block that can satisfy us */
826 if ((MdBlock
->MemoryType
== LoaderFree
) &&
827 (AlignedLimit
<= MdBlock
->PageCount
) &&
828 (PageCount
<= AlignedLimit
))
830 /* It is, stop searching */
831 ActiveMdBlock
= MdBlock
;
832 ActiveAlignedBase
= AlignedBase
;
836 /* Try the next block */
837 NextEntry
= NextEntry
->Flink
;
840 /* See if we came up with an adequate block */
843 /* Generate a descriptor in it */
844 *ReturnedBase
= AlignedBase
;
845 return KiRosBuildNtDescriptor(ActiveMdBlock
,
852 /* We found a matching block, generate a descriptor with it */
853 *ReturnedBase
= BasePage
;
854 return KiRosBuildNtDescriptor(MdBlock
, MemoryType
, BasePage
, PageCount
);
859 KiRosBuildArcMemoryList(VOID
)
861 PMEMORY_ALLOCATION_DESCRIPTOR Descriptor
;
862 MEMORY_DESCRIPTOR
*Memory
;
865 /* Loop all BIOS Memory Descriptors */
866 for (i
= 0; i
< NumberDescriptors
; i
++)
868 /* Get the current descriptor */
869 Memory
= &MDArray
[i
];
871 /* Allocate an NT Memory Descriptor */
872 Descriptor
= KiRosGetMdFromArray();
873 if (!Descriptor
) return ENOMEM
;
875 /* Copy the memory type */
876 Descriptor
->MemoryType
= Memory
->MemoryType
;
877 if (Memory
->MemoryType
== MemoryFreeContiguous
)
879 /* Convert this to free */
880 Descriptor
->MemoryType
= LoaderFree
;
882 else if (Memory
->MemoryType
== MemorySpecialMemory
)
884 /* Convert this to special memory */
885 Descriptor
->MemoryType
= LoaderSpecialMemory
;
888 /* Copy the range data */
889 Descriptor
->BasePage
= Memory
->BasePage
;
890 Descriptor
->PageCount
= Memory
->PageCount
;
892 /* Insert the descriptor */
893 if (Descriptor
->PageCount
) KiRosInsertNtDescriptor(Descriptor
);
897 return STATUS_SUCCESS
;
902 KiRosFixupComponentTree(IN PCONFIGURATION_COMPONENT_DATA p
,
905 PCONFIGURATION_COMPONENT pp
;
907 /* Loop each entry */
910 /* Grab the component entry */
911 pp
= &p
->ComponentEntry
;
913 /* Fixup the pointers */
914 if (pp
->Identifier
) pp
->Identifier
= (PVOID
)((ULONG_PTR
)pp
->Identifier
+ i
);
915 if (p
->ConfigurationData
) p
->ConfigurationData
= (PVOID
)((ULONG_PTR
)p
->ConfigurationData
+ i
);
916 if (p
->Parent
) p
->Parent
= (PVOID
)((ULONG_PTR
)p
->Parent
+ i
);
917 if (p
->Sibling
) p
->Sibling
= (PVOID
)((ULONG_PTR
)p
->Sibling
+ i
);
918 if (p
->Child
) p
->Child
= (PVOID
)((ULONG_PTR
)p
->Child
+ i
);
920 /* Check if we have a child */
921 if (p
->Child
) KiRosFixupComponentTree(p
->Child
, i
);
923 /* Get to the next entry */
930 KiRosFrldrLpbToNtLpb(IN PROS_LOADER_PARAMETER_BLOCK RosLoaderBlock
,
931 IN PLOADER_PARAMETER_BLOCK
*NtLoaderBlock
)
933 PLOADER_PARAMETER_BLOCK LoaderBlock
;
934 PLDR_DATA_TABLE_ENTRY LdrEntry
;
935 PLOADER_MODULE RosEntry
= NULL
;
939 PCHAR BootPath
, HalPath
;
940 CHAR CommandLine
[256];
941 PARC_DISK_SIGNATURE RosDiskInfo
, ArcDiskInfo
;
942 PIMAGE_NT_HEADERS NtHeader
;
943 WCHAR PathToDrivers
[] = L
"\\SystemRoot\\System32\\drivers\\";
944 WCHAR PathToSystem32
[] = L
"\\SystemRoot\\System32\\";
945 WCHAR PathSetup
[] = L
"\\SystemRoot\\";
946 CHAR DriverNameLow
[256];
949 ULONG KernelBase
= RosLoaderBlock
->ModsAddr
[0].ModStart
;
952 /* Set the NT Loader block and initialize it */
953 *NtLoaderBlock
= KeLoaderBlock
= LoaderBlock
= &BldrLoaderBlock
;
954 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
956 /* Set the NLS Data block */
957 LoaderBlock
->NlsData
= &BldrNlsDataBlock
;
959 /* Set the ARC Data block */
960 LoaderBlock
->ArcDiskInformation
= &BldrArcDiskInfo
;
962 /* Assume this is from FreeLDR's SetupLdr */
963 LoaderBlock
->SetupLdrBlock
= &BldrSetupBlock
;
965 /* Setup the list heads */
966 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
967 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
968 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
969 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
971 /* Build the free memory map, which uses BIOS Descriptors */
972 KiRosBuildBiosMemoryMap();
974 /* Build entries for ReactOS memory ranges, which uses ARC Descriptors */
975 KiRosBuildOsMemoryMap();
978 /* Build entries for the reserved map, which uses ARC Descriptors */
979 KiRosBuildReservedMemoryMap();
982 /* Now convert the BIOS and ARC Descriptors into NT Memory Descirptors */
983 KiRosBuildArcMemoryList();
985 /* Loop boot driver list */
986 for (i
= 0; i
< RosLoaderBlock
->ModsCount
; i
++)
988 /* Get the ROS loader entry */
989 RosEntry
= &RosLoaderBlock
->ModsAddr
[i
];
990 DriverName
= (PCHAR
)RosEntry
->String
;
991 ModStart
= (PVOID
)RosEntry
->ModStart
;
992 ModSize
= RosEntry
->ModEnd
- (ULONG_PTR
)ModStart
;
995 ModStart
-= KernelBase
;
998 /* Check if this is any of the NLS files */
999 if (!_stricmp(DriverName
, "ansi.nls"))
1001 /* ANSI Code page */
1002 LoaderBlock
->NlsData
->AnsiCodePageData
= KERNEL_RVA(ModStart
);
1004 /* Create an MD for it */
1005 KiRosAllocateNtDescriptor(LoaderNlsData
,
1006 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1007 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1012 else if (!_stricmp(DriverName
, "oem.nls"))
1015 LoaderBlock
->NlsData
->OemCodePageData
= KERNEL_RVA(ModStart
);
1017 /* Create an MD for it */
1018 KiRosAllocateNtDescriptor(LoaderNlsData
,
1019 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1020 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1025 else if (!_stricmp(DriverName
, "casemap.nls"))
1027 /* Unicode Code page */
1028 LoaderBlock
->NlsData
->UnicodeCodePageData
= KERNEL_RVA(ModStart
);
1030 /* Create an MD for it */
1031 KiRosAllocateNtDescriptor(LoaderNlsData
,
1032 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1033 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1039 /* Check if this is the SYSTEM hive */
1040 if (!(_stricmp(DriverName
, "system")) ||
1041 !(_stricmp(DriverName
, "system.hiv")))
1043 /* Save registry data */
1044 LoaderBlock
->RegistryBase
= KERNEL_RVA(ModStart
);
1045 LoaderBlock
->RegistryLength
= ModSize
;
1047 /* Disable setup mode */
1048 LoaderBlock
->SetupLdrBlock
= NULL
;
1050 /* Create an MD for it */
1051 KiRosAllocateNtDescriptor(LoaderRegistryData
,
1052 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1053 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1059 /* Check if this is the HARDWARE hive */
1060 if (!(_stricmp(DriverName
, "hardware")) ||
1061 !(_stricmp(DriverName
, "hardware.hiv")))
1063 /* Create an MD for it */
1064 KiRosAllocateNtDescriptor(LoaderRegistryData
,
1065 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1066 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1072 /* Check if this is the kernel */
1073 if (!(_stricmp(DriverName
, "ntoskrnl.exe")))
1075 /* Create an MD for it */
1076 KiRosAllocateNtDescriptor(LoaderSystemCode
,
1077 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1078 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1082 else if (!(_stricmp(DriverName
, "hal.dll")))
1084 /* Create an MD for the HAL */
1085 KiRosAllocateNtDescriptor(LoaderHalCode
,
1086 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1087 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1093 /* Create an MD for any driver */
1094 KiRosAllocateNtDescriptor(LoaderBootDriver
,
1095 KERNEL_DESCRIPTOR_PAGE(ModStart
),
1096 (ModSize
+ PAGE_SIZE
- 1)>> PAGE_SHIFT
,
1102 ModStart
+= 0x80800000;
1105 /* Lowercase the drivername so we can check its extension later */
1106 strcpy(DriverNameLow
, DriverName
);
1107 _strlwr(DriverNameLow
);
1109 /* Setup the loader entry */
1110 LdrEntry
= &BldrModules
[i
];
1111 RtlZeroMemory(LdrEntry
, sizeof(LDR_DATA_TABLE_ENTRY
));
1113 /* Convert driver name from ANSI to Unicode */
1114 for (j
= 0; j
< strlen(DriverName
); j
++)
1116 BldrModuleStrings
[i
][j
] = DriverName
[j
];
1119 /* Setup driver name */
1120 RtlInitUnicodeString(&LdrEntry
->BaseDllName
, BldrModuleStrings
[i
]);
1122 /* Construct a correct full name */
1123 BldrModuleStringsFull
[i
][0] = 0;
1124 LdrEntry
->FullDllName
.MaximumLength
= 260 * sizeof(WCHAR
);
1125 LdrEntry
->FullDllName
.Length
= 0;
1126 LdrEntry
->FullDllName
.Buffer
= BldrModuleStringsFull
[i
];
1128 /* Guess the path */
1129 if (LoaderBlock
->SetupLdrBlock
)
1131 UNICODE_STRING TempString
;
1132 RtlInitUnicodeString(&TempString
, PathSetup
);
1133 RtlAppendUnicodeStringToString(&LdrEntry
->FullDllName
, &TempString
);
1135 else if (strstr(DriverNameLow
, ".dll") || strstr(DriverNameLow
, ".exe"))
1137 UNICODE_STRING TempString
;
1138 RtlInitUnicodeString(&TempString
, PathToSystem32
);
1139 RtlAppendUnicodeStringToString(&LdrEntry
->FullDllName
, &TempString
);
1143 UNICODE_STRING TempString
;
1144 RtlInitUnicodeString(&TempString
, PathToDrivers
);
1145 RtlAppendUnicodeStringToString(&LdrEntry
->FullDllName
, &TempString
);
1148 /* Append base name of the driver */
1149 RtlAppendUnicodeStringToString(&LdrEntry
->FullDllName
, &LdrEntry
->BaseDllName
);
1151 /* Copy data from Freeldr Module Entry */
1152 LdrEntry
->DllBase
= ModStart
;
1153 LdrEntry
->SizeOfImage
= ModSize
;
1155 /* Copy additional data */
1156 NtHeader
= RtlImageNtHeader(ModStart
);
1157 LdrEntry
->EntryPoint
= RVA(ModStart
,
1159 OptionalHeader
.AddressOfEntryPoint
);
1161 /* Initialize other data */
1162 LdrEntry
->LoadCount
= 1;
1163 LdrEntry
->Flags
= LDRP_IMAGE_DLL
|
1164 LDRP_ENTRY_PROCESSED
;
1165 if (RosEntry
->Reserved
) LdrEntry
->Flags
|= LDRP_ENTRY_INSERTED
;
1167 /* Check if this is HAL */
1168 if (!(_stricmp(DriverName
, "hal.dll")))
1170 /* Check if there is a second entry already */
1171 if (LoaderBlock
->LoadOrderListHead
.Flink
->Flink
!=
1172 &LoaderBlock
->LoadOrderListHead
)
1174 PLIST_ENTRY OldSecondEntry
;
1176 /* Get the second entry */
1178 LoaderBlock
->LoadOrderListHead
.Flink
->Flink
;
1180 /* Set up our entry correctly */
1181 LdrEntry
->InLoadOrderLinks
.Flink
= OldSecondEntry
;
1182 LdrEntry
->InLoadOrderLinks
.Blink
= OldSecondEntry
->Blink
;
1184 /* Make the first entry (always the kernel) point to us */
1185 LoaderBlock
->LoadOrderListHead
.Flink
->Flink
=
1186 &LdrEntry
->InLoadOrderLinks
;
1188 /* Make the old entry point back to us and continue looping */
1189 OldSecondEntry
->Blink
= &LdrEntry
->InLoadOrderLinks
;
1194 /* Insert it into the loader block */
1195 InsertTailList(&LoaderBlock
->LoadOrderListHead
,
1196 &LdrEntry
->InLoadOrderLinks
);
1199 /* Now mark the remainder of the FreeLDR 6MB area as "in use" */
1200 KiRosAllocateNtDescriptor(LoaderMemoryData
,
1201 KERNEL_DESCRIPTOR_PAGE(RosEntry
->ModEnd
),
1202 KERNEL_DESCRIPTOR_PAGE((0x80800000 + 0x600000)) -
1203 KERNEL_DESCRIPTOR_PAGE(RosEntry
->ModEnd
),
1208 // Check if we have a ramdisk
1210 if ((RosLoaderBlock
->RdAddr
) && (RosLoaderBlock
->RdLength
))
1213 // Build a descriptor for it
1215 KiRosAllocateNtDescriptor(LoaderXIPRom
,
1216 KERNEL_DESCRIPTOR_PAGE(RosLoaderBlock
->RdAddr
),
1217 (RosLoaderBlock
->RdLength
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
,
1222 /* Setup command line */
1223 LoaderBlock
->LoadOptions
= BldrCommandLine
;
1224 strcpy(BldrCommandLine
, RosLoaderBlock
->CommandLine
);
1226 /* Setup the extension block */
1227 LoaderBlock
->Extension
= &BldrExtensionBlock
;
1228 LoaderBlock
->Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
1229 LoaderBlock
->Extension
->MajorVersion
= 5;
1230 LoaderBlock
->Extension
->MinorVersion
= 2;
1232 /* FreeLDR hackllocates 1536 static pages for the initial boot images */
1233 LoaderBlock
->Extension
->LoaderPagesSpanned
= 1536 * PAGE_SIZE
;
1235 /* ReactOS always boots the kernel at 0x80800000 (just like NT 5.2) */
1236 LoaderBlock
->Extension
->LoaderPagesSpanned
+= 0x80800000 - KSEG0_BASE
;
1238 /* Now convert to pages */
1239 LoaderBlock
->Extension
->LoaderPagesSpanned
/= PAGE_SIZE
;
1241 /* Check if FreeLdr detected a ACPI table */
1242 if (RosLoaderBlock
->Flags
& MB_FLAGS_ACPI_TABLE
)
1244 /* Set the pointer to something for compatibility */
1245 LoaderBlock
->Extension
->AcpiTable
= (PVOID
)1;
1248 /* Now setup the setup block if we have one */
1249 if (LoaderBlock
->SetupLdrBlock
)
1251 /* All we'll setup right now is the flag for text-mode setup */
1252 LoaderBlock
->SetupLdrBlock
->Flags
= SETUPLDR_TEXT_MODE
;
1255 /* Make a copy of the command line */
1256 strcpy(CommandLine
, LoaderBlock
->LoadOptions
);
1258 /* Find the first \, separating the ARC path from NT path */
1259 BootPath
= strchr(CommandLine
, '\\');
1260 *BootPath
= ANSI_NULL
;
1261 strncpy(BldrArcBootPath
, CommandLine
, 63);
1262 LoaderBlock
->ArcBootDeviceName
= BldrArcBootPath
;
1264 /* The rest of the string is the NT path */
1265 HalPath
= strchr(BootPath
+ 1, ' ');
1266 *HalPath
= ANSI_NULL
;
1267 BldrNtBootPath
[0] = '\\';
1268 strncat(BldrNtBootPath
, BootPath
+ 1, 63);
1269 strcat(BldrNtBootPath
,"\\");
1270 LoaderBlock
->NtBootPathName
= BldrNtBootPath
;
1272 /* Set the HAL paths */
1273 strncpy(BldrArcHalPath
, BldrArcBootPath
, 63);
1274 LoaderBlock
->ArcHalDeviceName
= BldrArcHalPath
;
1275 strcpy(BldrNtHalPath
, "\\");
1276 LoaderBlock
->NtHalPathName
= BldrNtHalPath
;
1278 /* Use this new command line */
1279 strncpy(LoaderBlock
->LoadOptions
, HalPath
+ 2, 255);
1281 /* Parse it and change every slash to a space */
1282 BootPath
= LoaderBlock
->LoadOptions
;
1283 do {if (*BootPath
== '/') *BootPath
= ' ';} while (*BootPath
++);
1285 /* Now let's loop ARC disk information */
1286 for (i
= 0; i
< RosLoaderBlock
->DrivesCount
; i
++)
1288 /* Get the ROS loader entry */
1289 RosDiskInfo
= &RosLoaderBlock
->DrivesAddr
[i
];
1291 /* Get the ARC structure */
1292 ArcDiskInfo
= &BldrDiskInfo
[i
];
1294 /* Copy the data over */
1295 ArcDiskInfo
->Signature
= RosDiskInfo
->Signature
;
1296 ArcDiskInfo
->CheckSum
= RosDiskInfo
->CheckSum
;
1298 /* Copy the ARC Name */
1299 strcpy(BldrArcNames
[i
], RosDiskInfo
->ArcName
);
1300 ArcDiskInfo
->ArcName
= BldrArcNames
[i
];
1302 /* Insert into the list */
1303 InsertTailList(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
,
1304 &ArcDiskInfo
->ListEntry
);
1307 /* Copy the ARC Hardware Tree */
1308 RtlCopyMemory(BldrArcHwBuffer
, (PVOID
)RosLoaderBlock
->ArchExtra
, 16 * 1024);
1309 LoaderBlock
->ConfigurationRoot
= (PVOID
)BldrArcHwBuffer
;
1312 KiRosFixupComponentTree(LoaderBlock
->ConfigurationRoot
,
1313 (ULONG_PTR
)BldrArcHwBuffer
-
1314 RosLoaderBlock
->ArchExtra
);
1319 KiSetupSyscallHandler();
1323 KiRosPrepareForSystemStartup(IN PROS_LOADER_PARAMETER_BLOCK LoaderBlock
)
1325 PLOADER_PARAMETER_BLOCK NtLoaderBlock
;
1326 ULONG size
, i
= 0, *ent
;
1329 PKGDTENTRY TssEntry
;
1330 KDESCRIPTOR IdtDescriptor
;
1332 __sidt(&IdtDescriptor
.Limit
);
1333 RtlCopyMemory(KiBootIdt
, (PVOID
)IdtDescriptor
.Base
, IdtDescriptor
.Limit
+ 1);
1334 IdtDescriptor
.Base
= (ULONG
)&KiBootIdt
;
1335 IdtDescriptor
.Limit
= sizeof(KiBootIdt
) - 1;
1337 /* Load the GDT and IDT */
1338 Ke386SetGlobalDescriptorTable(&KiGdtDescriptor
.Limit
);
1339 __lidt(&IdtDescriptor
.Limit
);
1341 /* Initialize the boot TSS */
1343 TssEntry
= &KiBootGdt
[KGDT_TSS
/ sizeof(KGDTENTRY
)];
1344 TssEntry
->HighWord
.Bits
.Type
= I386_TSS
;
1345 TssEntry
->HighWord
.Bits
.Pres
= 1;
1346 TssEntry
->HighWord
.Bits
.Dpl
= 0;
1347 TssEntry
->BaseLow
= (USHORT
)((ULONG_PTR
)Tss
& 0xFFFF);
1348 TssEntry
->HighWord
.Bytes
.BaseMid
= (UCHAR
)((ULONG_PTR
)Tss
>> 16);
1349 TssEntry
->HighWord
.Bytes
.BaseHi
= (UCHAR
)((ULONG_PTR
)Tss
>> 24);
1351 /* Set the TSS selector */
1352 Ke386SetTr(KGDT_TSS
);
1356 // Zero bats. We might have residual bats set that will interfere with
1357 // our mapping of ofwldr.
1358 for (i
= 0; i
< 4; i
++)
1360 SetBat(i
, 0, 0, 0); SetBat(i
, 1, 0, 0);
1362 KiSetupSyscallHandler();
1363 DbgPrint("Kernel Power (%08x)\n", LoaderBlock
);
1364 DbgPrint("ArchExtra (%08x)!\n", LoaderBlock
->ArchExtra
);
1367 /* Save pointer to ROS Block */
1368 KeRosLoaderBlock
= LoaderBlock
;
1370 /* Save memory manager data */
1371 KeMemoryMapRangeCount
= 0;
1372 if (LoaderBlock
->Flags
& MB_FLAGS_MMAP_INFO
)
1374 /* We have a memory map from the nice BIOS */
1375 ent
= ((PULONG
)(LoaderBlock
->MmapAddr
- sizeof(ULONG
)));
1379 /* Map it until we run out of size */
1380 while (i
< LoaderBlock
->MmapLength
)
1382 /* Copy into the Kernel Memory Map */
1383 memcpy (&KeMemoryMap
[KeMemoryMapRangeCount
],
1384 (PVOID
)(LoaderBlock
->MmapAddr
+ i
),
1385 sizeof(ADDRESS_RANGE
));
1387 /* Increase Memory Map Count */
1388 KeMemoryMapRangeCount
++;
1395 LoaderBlock
->MmapLength
= KeMemoryMapRangeCount
* sizeof(ADDRESS_RANGE
);
1396 LoaderBlock
->MmapAddr
= (ULONG
)KeMemoryMap
;
1400 /* Nothing from BIOS */
1401 LoaderBlock
->MmapLength
= 0;
1402 LoaderBlock
->MmapAddr
= (ULONG
)KeMemoryMap
;
1405 /* Convert the loader block */
1406 KiRosFrldrLpbToNtLpb(KeRosLoaderBlock
, &NtLoaderBlock
);
1409 DbgPrint("Finished KiRosFrldrLpbToNtLpb\n");
1412 /* Do general System Startup */
1413 KiSystemStartup(NtLoaderBlock
);