ReactOS  0.4.13-dev-455-g28ed234
mboot.c
Go to the documentation of this file.
1 /*
2  * FreeLoader
3  * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4  * Copyright (C) 2005 Alex Ionescu <alex@relsoft.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <freeldr.h>
22 #include <elf/elf.h>
23 #include <elf/reactos.h>
24 #include <of.h>
25 #include "ppcmmu/mmu.h"
26 #include "compat.h"
27 
28 #include <debug.h>
29 
30 /* We'll check this to see if we're in OFW land */
31 extern of_proxy ofproxy;
32 
34 
35 /* Bits to shift to convert a Virtual Address into an Offset in the Page Table */
36 #define PFN_SHIFT 12
37 
38 /* Bits to shift to convert a Virtual Address into an Offset in the Page Directory */
39 #define PDE_SHIFT 22
40 #define PDE_SHIFT_PAE 18
41 
42 #define STARTUP_BASE 0xC0000000
43 #define HYPERSPACE_BASE 0xC0400000
44 #define HYPERSPACE_PAE_BASE 0xC0800000
45 #define APIC_BASE 0xFEC00000
46 #define KPCR_BASE 0xFF000000
47 
48 #define LowMemPageTableIndex 0
49 #define StartupPageTableIndex (STARTUP_BASE >> 22)
50 #define HyperspacePageTableIndex (HYPERSPACE_BASE >> 22)
51 #define KpcrPageTableIndex (KPCR_BASE >> 22)
52 #define ApicPageTableIndex (APIC_BASE >> 22)
53 
54 #define BAT_GRANULARITY (64 * 1024)
55 #define KernelMemorySize (8 * 1024 * 1024)
56 #define XROUNDUP(x,n) ((((ULONG)x) + ((n) - 1)) & (~((n) - 1)))
57 
58 #define TAG_MBOOT 'oobM'
59 
60 char reactos_module_strings[64][256]; // Array to hold module names
61 
62 /* Load Address of Next Module */
64 
65 /* Currently Opened Module */
66 PLOADER_MODULE CurrentModule = NULL;
67 
68 /* Unrelocated Kernel Base in Virtual Memory */
70 
71 /* Wether PAE is to be used or not */
73 
74 /* Kernel Entrypoint in Physical Memory */
76 
77 /* Dummy to bring in memmove */
79 
80 PLOADER_MODULE
81 NTAPI
83 
85 NTAPI
86 LdrPEFixupImports(IN PVOID DllBase,
87  IN PCHAR DllName);
88 
90 
91 /* FUNCTIONS *****************************************************************/
92 
93 /*++
94  * FrLdrStartup
95  * INTERNAL
96  *
97  * Prepares the system for loading the Kernel.
98  *
99  * Params:
100  * Magic - Multiboot Magic
101  *
102  * Returns:
103  * None.
104  *
105  * Remarks:
106  * None.
107  *
108  *--*/
109 
110 typedef void (*KernelEntryFn)( void * );
111 
112 int MmuPageMiss(int trapCode, ppc_trap_frame_t *trap)
113 {
114  int i;
115  printf("TRAP %x\n", trapCode);
116  for( i = 0; i < 40; i++ )
117  printf("r[%d] %x\n", i, trap->gpr[i]);
118  printf("HALT!\n");
119  while(1);
120 }
121 
122 typedef struct _ppc_map_set_t {
123  int mapsize;
124  int usecount;
126 } ppc_map_set_t;
127 
128 extern int mmu_handle;
129 paddr_t MmuTranslate(paddr_t possibly_virtual)
130 {
131  if (ofproxy)
132  {
133  /* Openfirmware takes liberties with boot-time memory.
134  * if you're in a unitary kernel, it's not as difficult, but since
135  * we rely on loading things into virtual space from here, we need
136  * to detect the mappings so far.
137  */
138  int args[2];
139  args[0] = possibly_virtual;
140  args[1] = 1; /* Marker to tell we want a physical addr */
141  return (paddr_t)ofw_callmethod_ret("translate", mmu_handle, 2, args, 3);
142  }
143  else
144  {
145  /* Other booters don't remap ram */
146  return possibly_virtual;
147  }
148 }
149 
150 VOID
151 NTAPI
153 {
154  int j;
155  paddr_t page = ROUND_DOWN(phys, (1<<PFN_SHIFT));
156 
157  if (virt == 0)
158  virt = ROUND_DOWN(page, (1<<PFN_SHIFT));
159  else
160  virt = ROUND_DOWN(virt, (1<<PFN_SHIFT));
161 
163 
164  //printf("Mapping virt [%x] to phys [%x (from) %x]\n", virt, page, phys);
165 
166  for( j = 0; j < set->usecount; j++ )
167  {
168  if(set->info[j].addr == page) return;
169  }
170 
171  if (!set->mapsize)
172  {
173  set->mapsize = 0x80;
174  set->info = MmAllocateMemory(0x80 * sizeof(*set->info));
175  }
176  else if (set->mapsize <= set->usecount)
177  {
178  ppc_map_info_t *newinfo = MmAllocateMemory(set->mapsize * 2 * sizeof(*set->info));
179  memcpy(newinfo, set->info, set->mapsize * sizeof(*set->info));
180  MmFreeMemory(set->info);
181  set->info = newinfo;
182  set->mapsize *= 2;
183  }
184 
185  set->info[set->usecount].flags = MMU_ALL_RW;
186  set->info[set->usecount].proc = proc;
187  set->info[set->usecount].addr = virt;
188  set->info[set->usecount].phys = page;
189  set->usecount++;
190 }
191 
192 extern int _start[], _end[];
193 
194 VOID
195 NTAPI
197 {
198  ULONG_PTR i, tmp, OldModCount = 0;
199  PCHAR ModHeader;
200  CHAR ModulesTreated[64] = { 0 };
201  ULONG NumberOfEntries = 0, UsedEntries = 0;
202  PPAGE_LOOKUP_TABLE_ITEM FreeLdrMap = MmGetMemoryMap(&NumberOfEntries);
203  ppc_map_set_t memmap = { };
204 
205  printf("FrLdrStartup\n");
206 
207  /* Disable EE */
208  __asm__("mfmsr %0" : "=r" (tmp));
209  tmp &= 0x7fff;
210  __asm__("mtmsr %0" : : "r" (tmp));
211 
212  while(OldModCount != LoaderBlock.ModsCount)
213  {
214  printf("Added %d modules last pass\n",
215  LoaderBlock.ModsCount - OldModCount);
216 
217  OldModCount = LoaderBlock.ModsCount;
218 
219  for(i = 0; i < LoaderBlock.ModsCount; i++)
220  {
221  if (!ModulesTreated[i])
222  {
223  ModulesTreated[i] = 1;
224  ModHeader = ((PCHAR)reactos_modules[i].ModStart);
225  if(ModHeader[0] == 'M' && ModHeader[1] == 'Z')
227  ((PVOID)reactos_modules[i].ModStart,
228  (PCHAR)reactos_modules[i].String);
229  }
230  }
231  }
232 
233  printf("Starting mmu\n");
234 
235  PpcInitializeMmu(0);
236 
237  printf("Allocating vsid 0 (kernel)\n");
238  MmuAllocVsid(0, 0xff00);
239 
240  /* We'll use vsid 1 for freeldr (expendable) */
241  printf("Allocating vsid 1 (freeldr)\n");
242  MmuAllocVsid(1, 0xff);
243 
244  printf("Mapping Freeldr Code (%x-%x)\n", _start, _end);
245 
246  /* Map memory zones */
247  /* Freeldr itself */
248  for( i = (int)_start;
249  i < (int)_end;
250  i += (1<<PFN_SHIFT) ) {
251  FrLdrAddPageMapping(&memmap, 1, i, 0);
252  }
253 
254  printf("KernelBase %x\n", KernelBase);
255 
256  /* Heap pages -- this gets the entire freeldr heap */
257  for( i = 0; i < NumberOfEntries; i++ ) {
258  tmp = i<<PFN_SHIFT;
259  if (FreeLdrMap[i].PageAllocated == LoaderSystemCode) {
260  UsedEntries++;
261  if (tmp >= (ULONG)KernelMemory &&
263  FrLdrAddPageMapping(&memmap, 0, tmp, KernelBase + tmp - (ULONG)KernelMemory);
264  } else {
265  FrLdrAddPageMapping(&memmap, 1, tmp, 0);
266  }
267  }
268  }
269 
270  MmuMapPage(memmap.info, memmap.usecount);
271 
272  printf("Finished Mapping the Freeldr Heap (used %d pages)\n", UsedEntries);
273 
274  printf("Setting initial segments\n");
275  MmuSetVsid(0, 8, 1);
276  MmuSetVsid(8, 16, 0);
277 
278  printf("Segments set!\n");
279 
280  MmuTurnOn((KernelEntryFn)KernelEntryPoint, &LoaderBlock);
281 
282  /* Nothing more */
283  while(1);
284 }
285 
286 /*++
287  * FrLdrSetupPae
288  * INTERNAL
289  *
290  * Configures PAE on a MP System, and sets the PDBR if it's supported, or if
291  * the system is UP.
292  *
293  * Params:
294  * Magic - Multiboot Magic
295  *
296  * Returns:
297  * None.
298  *
299  * Remarks:
300  * None.
301  *
302  *--*/
303 VOID
304 FASTCALL
306 {
307 }
308 
309 /*++
310  * FrLdrGetKernelBase
311  * INTERNAL
312  *
313  * Gets the Kernel Base to use.
314  *
315  * Params:
316  *
317  * Returns:
318  * None.
319  *
320  * Remarks:
321  * Sets both the FreeLdr internal variable as well as the one which
322  * will be used by the Kernel.
323  *
324  *--*/
325 VOID
326 FASTCALL
328 {
329  PCHAR p;
330 
331  /* Default kernel base at 2GB */
332  KernelBase = 0x80800000;
333 
334  /* Set KernelBase */
335  LoaderBlock.KernelBase = 0x80000000;
336 
337  /* Read Command Line */
338  p = (PCHAR)LoaderBlock.CommandLine;
339  while ((p = strchr(p, '/')) != NULL) {
340 
341  /* Find "/3GB" */
342  if (!_strnicmp(p + 1, "3GB", 3)) {
343 
344  /* Make sure there's nothing following it */
345  if (p[4] == ' ' || p[4] == 0) {
346 
347  /* Use 3GB */
348  KernelBase = 0xE0000000;
349  LoaderBlock.KernelBase = 0xC0000000;
350  }
351  }
352 
353  p++;
354  }
355 }
356 
357 /*++
358  * FrLdrGetPaeMode
359  * INTERNAL
360  *
361  * Determines whether PAE mode should be enabled or not.
362  *
363  * Params:
364  * None.
365  *
366  * Returns:
367  * None.
368  *
369  * Remarks:
370  * None.
371  *
372  *--*/
373 VOID
374 FASTCALL
376 {
377 }
378 
379 /*++
380  * FrLdrSetupPageDirectory
381  * INTERNAL
382  *
383  * Sets up the ReactOS Startup Page Directory.
384  *
385  * Params:
386  * None.
387  *
388  * Returns:
389  * None.
390  *
391  * Remarks:
392  * We are setting PDEs, but using the equivalent (for our purpose) PTE structure.
393  * As such, please note that PageFrameNumber == PageEntryNumber.
394  *
395  *--*/
396 VOID
397 FASTCALL
399 {
400 }
401 
402 /*++
403  * FrLdrMapModule
404  * INTERNAL
405  *
406  * Loads the indicated elf image as PE. The target will appear to be
407  * a PE image whose ImageBase has ever been KernelAddr.
408  *
409  * Params:
410  * Image -- File to load
411  * ImageName -- Name of image for the modules list
412  * MemLoadAddr -- Freeldr address of module
413  * KernelAddr -- Kernel address of module
414  *--*/
415 #define ELF_SECTION(n) ((Elf32_Shdr*)(sptr + (n * shsize)))
416 #define COFF_FIRST_SECTION(h) ((PIMAGE_SECTION_HEADER) ((DWORD)h+FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader)+(SWAPW(((PIMAGE_NT_HEADERS)(h))->FileHeader.SizeOfOptionalHeader))))
417 
418 BOOLEAN
419 NTAPI
420 FrLdrMapModule(FILE *KernelImage, PCHAR ImageName, PCHAR MemLoadAddr, ULONG KernelAddr)
421 {
422  PIMAGE_DOS_HEADER ImageHeader = 0;
423  PIMAGE_NT_HEADERS NtHeader = 0;
424  PIMAGE_SECTION_HEADER Section;
425  ULONG SectionCount;
426  ULONG ImageSize;
427  INT i, j;
428  PLOADER_MODULE ModuleData;
429  //int phsize, phnum;
430  int shsize, shnum, relsize, SectionAddr = 0;
431  PCHAR sptr;
432  Elf32_Ehdr ehdr;
433  Elf32_Shdr *shdr;
434  LPSTR TempName;
435 
436  TempName = strrchr(ImageName, '\\');
437  if(TempName) TempName++; else TempName = (LPSTR)ImageName;
438  ModuleData = LdrGetModuleObject(TempName);
439 
440  if(ModuleData)
441  {
442  return TRUE;
443  }
444 
445  if(!KernelAddr)
446  KernelAddr = (ULONG)NextModuleBase - (ULONG)KernelMemory + KernelBase;
447  if(!MemLoadAddr)
448  MemLoadAddr = (PCHAR)NextModuleBase;
449 
450  ModuleData = &reactos_modules[LoaderBlock.ModsCount];
451  //printf("Loading file (elf at %x)\n", KernelAddr);
452 
453  /* Load the first 1024 bytes of the kernel image so we can read the PE header */
454  if (!FsReadFile(KernelImage, sizeof(ehdr), NULL, &ehdr)) {
455 
456  /* Fail if we couldn't read */
457  printf("Couldn't read the elf header\n");
458  return FALSE;
459  }
460 
461  /* Start by getting elf headers */
462  //phsize = ehdr.e_phentsize;
463  //phnum = ehdr.e_phnum;
464  shsize = ehdr.e_shentsize;
465  shnum = ehdr.e_shnum;
466  sptr = (PCHAR)FrLdrTempAlloc(shnum * shsize, TAG_MBOOT);
467 
468  /* Read section headers */
469  FsSetFilePointer(KernelImage, ehdr.e_shoff);
470  FsReadFile(KernelImage, shsize * shnum, NULL, sptr);
471 
472  /* Now we'll get the PE Header */
473  for( i = 0; i < shnum; i++ )
474  {
475  shdr = ELF_SECTION(i);
476  shdr->sh_addr = 0;
477 
478  /* Find the PE Header */
479  if (shdr->sh_type == TYPE_PEHEADER)
480  {
481  FsSetFilePointer(KernelImage, shdr->sh_offset);
482  FsReadFile(KernelImage, shdr->sh_size, NULL, MemLoadAddr);
483  ImageHeader = (PIMAGE_DOS_HEADER)MemLoadAddr;
484  NtHeader = (PIMAGE_NT_HEADERS)((PCHAR)MemLoadAddr + SWAPD(ImageHeader->e_lfanew));
485 #if 0
486  printf("NtHeader at %x\n", SWAPD(ImageHeader->e_lfanew));
487  printf("SectionAlignment %x\n",
489  SectionAddr = ROUND_UP
490  (shdr->sh_size, SWAPD(NtHeader->OptionalHeader.SectionAlignment));
491  printf("Header ends at %x\n", SectionAddr);
492 #endif
493  break;
494  }
495  }
496 
497  if(i == shnum)
498  {
499  printf("No peheader section encountered :-(\n");
500  return 0;
501  }
502 #if 0
503  else
504  {
505  printf("DOS SIG: %s\n", (PCHAR)MemLoadAddr);
506  }
507 #endif
508 
509  /* Save the Image Base */
510  NtHeader->OptionalHeader.ImageBase = SWAPD(KernelAddr);
511 
512  /* Load the file image */
513  Section = COFF_FIRST_SECTION(NtHeader);
514  SectionCount = SWAPW(NtHeader->FileHeader.NumberOfSections);
515 
516  /* Walk each section */
517  for (i=0; i < SectionCount; i++, Section++)
518  {
519  shdr = ELF_SECTION((SWAPD(Section->PointerToRawData)+1));
520 
521  shdr->sh_addr = SectionAddr = SWAPD(Section->VirtualAddress);
522  shdr->sh_addr += KernelAddr;
523 
524  Section->PointerToRawData = SWAPD((Section->VirtualAddress - KernelAddr));
525 
526  if (shdr->sh_type != SHT_NOBITS)
527  {
528  /* Content area */
529  printf("Loading section %d at %x (real: %x:%d)\n", i, KernelAddr + SectionAddr, MemLoadAddr+SectionAddr, shdr->sh_size);
530  FsSetFilePointer(KernelImage, shdr->sh_offset);
531  FsReadFile(KernelImage, shdr->sh_size, NULL, MemLoadAddr + SectionAddr);
532  }
533  else
534  {
535  /* Zero it out */
536  printf("BSS section %d at %x\n", i, KernelAddr + SectionAddr);
537  memset(MemLoadAddr + SectionAddr, 0,
538  ROUND_UP(shdr->sh_size,
540  }
541  }
542 
543  ImageSize = SWAPD(NtHeader->OptionalHeader.SizeOfImage);
544  printf("Total image size is %x\n", ImageSize);
545 
546  /* Handle relocation sections */
547  for (i = 0; i < shnum; i++) {
548  Elf32_Rela reloc = { };
549  ULONG *Target32;
550  USHORT *Target16;
551  int numreloc, relstart, targetSection;
552  Elf32_Sym symbol;
553  PCHAR RelocSection, SymbolSection;
554 
555  shdr = ELF_SECTION(i);
556  /* Only relocs here */
557  if((shdr->sh_type != SHT_REL) &&
558  (shdr->sh_type != SHT_RELA)) continue;
559 
560  relstart = shdr->sh_offset;
561  relsize = shdr->sh_type == SHT_RELA ? 12 : 8;
562  numreloc = shdr->sh_size / relsize;
563  targetSection = shdr->sh_info;
564 
565  if (!ELF_SECTION(targetSection)->sh_addr) continue;
566 
567  RelocSection = FrLdrTempAlloc(shdr->sh_size, TAG_MBOOT);
568  FsSetFilePointer(KernelImage, relstart);
569  FsReadFile(KernelImage, shdr->sh_size, NULL, RelocSection);
570 
571  /* Get the symbol section */
572  shdr = ELF_SECTION(shdr->sh_link);
573 
574  SymbolSection = FrLdrTempAlloc(shdr->sh_size, TAG_MBOOT);
575  FsSetFilePointer(KernelImage, shdr->sh_offset);
576  FsReadFile(KernelImage, shdr->sh_size, NULL, SymbolSection);
577 
578  for(j = 0; j < numreloc; j++)
579  {
580  ULONG S,A,P;
581 
582  /* Get the reloc */
583  memcpy(&reloc, RelocSection + (j * relsize), sizeof(reloc));
584 
585  /* Get the symbol */
586  memcpy(&symbol, SymbolSection + (ELF32_R_SYM(reloc.r_info) * sizeof(symbol)), sizeof(symbol));
587 
588  /* Compute addends */
589  S = symbol.st_value + ELF_SECTION(symbol.st_shndx)->sh_addr;
590  A = reloc.r_addend;
591  P = reloc.r_offset + ELF_SECTION(targetSection)->sh_addr;
592 
593 #if 0
594  printf("Symbol[%d] %d -> %d(%x:%x) -> %x(+%x)@%x\n",
595  ELF32_R_TYPE(reloc.r_info),
596  ELF32_R_SYM(reloc.r_info),
597  symbol.st_shndx,
598  ELF_SECTION(symbol.st_shndx)->sh_addr,
599  symbol.st_value,
600  S,
601  A,
602  P);
603 #endif
604 
605  Target32 = (ULONG*)(((PCHAR)MemLoadAddr) + (P - KernelAddr));
606  Target16 = (USHORT *)Target32;
607 
608  switch (ELF32_R_TYPE(reloc.r_info))
609  {
610  case R_PPC_NONE:
611  break;
612  case R_PPC_ADDR32:
613  *Target32 = S + A;
614  break;
615  case R_PPC_REL32:
616  *Target32 = S + A - P;
617  break;
618  case R_PPC_UADDR32: /* Special: Treat as RVA */
619  *Target32 = S + A - KernelAddr;
620  break;
621  case R_PPC_ADDR24:
622  *Target32 = (ADDR24_MASK & (S+A)) | (*Target32 & ~ADDR24_MASK);
623  break;
624  case R_PPC_REL24:
625  *Target32 = (ADDR24_MASK & (S+A-P)) | (*Target32 & ~ADDR24_MASK);
626  break;
627  case R_PPC_ADDR16_LO:
628  *Target16 = S + A;
629  break;
630  case R_PPC_ADDR16_HA:
631  *Target16 = (S + A + 0x8000) >> 16;
632  break;
633  default:
634  break;
635  }
636 
637 #if 0
638  printf("reloc[%d:%x]: (type %x sym %d val %d) off %x add %x (old %x new %x)\n",
639  j,
640  ((ULONG)Target32) - ((ULONG)MemLoadAddr),
641  ELF32_R_TYPE(reloc.r_info),
642  ELF32_R_SYM(reloc.r_info),
643  symbol.st_value,
644  reloc.r_offset, reloc.r_addend,
645  x, *Target32);
646 #endif
647  }
648 
649  FrLdrTempFree(SymbolSection, TAG_MBOOT);
650  FrLdrTempFree(RelocSection, TAG_MBOOT);
651  }
652 
653  FrLdrTempFree(sptr, TAG_MBOOT);
654 
655  ModuleData->ModStart = (ULONG)MemLoadAddr;
656  /* Increase the next Load Base */
657  NextModuleBase = ROUND_UP((ULONG)MemLoadAddr + ImageSize, PAGE_SIZE);
658  ModuleData->ModEnd = NextModuleBase;
660  strcpy((PCHAR)ModuleData->String, ImageName);
661  printf("Module %s (%x-%x) next at %x\n",
662  ModuleData->String,
663  ModuleData->ModStart,
664  ModuleData->ModEnd,
666  LoaderBlock.ModsCount++;
667 
668  /* Return Success */
669  return TRUE;
670 }
671 
672 /*++
673  * FrLdrMapKernel
674  * INTERNAL
675  *
676  * Maps the Kernel into memory, does PE Section Mapping, initializes the
677  * uninitialized data sections, and relocates the image.
678  *
679  * Params:
680  * KernelImage - FILE Structure representing the ntoskrnl image file.
681  *
682  * Returns:
683  * TRUE if the Kernel was mapped.
684  *
685  * Remarks:
686  * None.
687  *
688  *--*/
689 BOOLEAN
690 NTAPI
691 FrLdrMapKernel(FILE *KernelImage)
692 {
693  /* Get Kernel Base */
695 
696  /* Allocate kernel memory */
698 
699  return FrLdrMapModule(KernelImage, "ntoskrnl.exe", KernelMemory, KernelBase);
700 }
701 
702 ULONG_PTR
703 NTAPI
704 FrLdrLoadModule(FILE *ModuleImage,
706  PULONG ModuleSize)
707 {
708  ULONG LocalModuleSize;
709  ULONG_PTR ThisModuleBase = NextModuleBase;
710  PLOADER_MODULE ModuleData;
711  LPSTR NameBuffer;
712  LPSTR TempName;
713 
714  /* Get current module data structure and module name string array */
715  ModuleData = &reactos_modules[LoaderBlock.ModsCount];
716 
717  /* Get only the Module Name */
718  do {
719 
720  TempName = strchr(ModuleName, '\\');
721 
722  if(TempName) {
723  ModuleName = TempName + 1;
724  }
725 
726  } while(TempName);
727  NameBuffer = reactos_module_strings[LoaderBlock.ModsCount];
728 
729 
730  /* Get Module Size */
731  LocalModuleSize = FsGetFileSize(ModuleImage);
732 
733  /* Fill out Module Data Structure */
734  ModuleData->ModStart = NextModuleBase;
735  ModuleData->ModEnd = NextModuleBase + LocalModuleSize;
736 
737  /* Save name */
738  strcpy(NameBuffer, ModuleName);
739  ModuleData->String = (ULONG_PTR)NameBuffer;
740 
741  /* Load the file image */
742  FsReadFile(ModuleImage, LocalModuleSize, NULL, (PVOID)NextModuleBase);
743 
744  /* Move to next memory block and increase Module Count */
746  LoaderBlock.ModsCount++;
747 
748  /* Return Module Size if required */
749  if (ModuleSize != NULL) {
750  *ModuleSize = LocalModuleSize;
751  }
752 
753  printf("Module %s (%x-%x) next at %x\n",
754  ModuleData->String,
755  ModuleData->ModStart,
756  ModuleData->ModEnd,
758 
759  return ThisModuleBase;
760 }
761 
762 PVOID
763 NTAPI
765 {
766  PVOID Result = NULL;
767 
768  printf("Loading image %s (type %d)\n", ShortName, ImageType);
769 
770  if (ImageType == 1)
771  {
772  if(FrLdrMapKernel(Image))
774  }
775  else
776  {
777  PVOID ModuleBase = (PVOID)NextModuleBase;
778 
779  if(FrLdrMapModule(Image, ShortName, 0, 0))
780  Result = ModuleBase;
781  }
782  return Result;
783 }
784 
785 ULONG_PTR
786 NTAPI
788 {
789  PLOADER_MODULE ModuleData;
790  LPSTR NameBuffer;
791 
792  /* Get current module data structure and module name string array */
793  ModuleData = &reactos_modules[LoaderBlock.ModsCount];
794  NameBuffer = reactos_module_strings[LoaderBlock.ModsCount];
795 
796  /* Set up the structure */
797  ModuleData->ModStart = NextModuleBase;
798  ModuleData->ModEnd = -1;
799 
800  /* Copy the name */
801  strcpy(NameBuffer, ModuleName);
802  ModuleData->String = (ULONG_PTR)NameBuffer;
803 
804  /* Set the current Module */
806 
807  /* Return Module Base Address */
808  return(ModuleData->ModStart);
809 }
810 
811 BOOLEAN
812 NTAPI
814  ULONG ModuleSize)
815 {
816  PLOADER_MODULE ModuleData = CurrentModule;
817 
818  /* Make sure a module is opened */
819  if (ModuleData) {
820 
821  /* Make sure this is the right module and that it hasn't been closed */
822  if ((ModuleBase == ModuleData->ModStart) && (ModuleData->ModEnd == MAXULONG_PTR)) {
823 
824  /* Close the Module */
825  ModuleData->ModEnd = ModuleData->ModStart + ModuleSize;
826 
827  /* Set the next Module Base and increase the number of modules */
829  LoaderBlock.ModsCount++;
830 
831  /* Close the currently opened module */
833 
834  /* Success */
835  return(TRUE);
836  }
837  }
838 
839  /* Failure path */
840  return(FALSE);
841 }
VOID FsSetFilePointer(PFILE FileHandle, ULONG NewFilePointer)
Definition: fs.c:367
IN PDCB IN POEM_STRING IN PUNICODE_STRING IN OUT POEM_STRING ShortName
Definition: fatprocs.h:1294
signed char * PCHAR
Definition: retypes.h:7
Elf32_Size sh_size
Definition: elf32.h:76
#define IN
Definition: typedefs.h:38
#define memmove(s1, s2, n)
Definition: mkisofs.h:881
#define max(a, b)
Definition: svc.c:63
int usecount
Definition: mboot.c:124
#define COFF_FIRST_SECTION(h)
Definition: mboot.c:416
Elf32_Off e_shoff
Definition: elf32.h:55
#define TRUE
Definition: types.h:120
Elf32_Addr st_value
Definition: elf32.h:140
#define KernelMemorySize
Definition: mboot.c:55
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
PLOADER_MODULE NTAPI LdrGetModuleObject(PCHAR ModuleName)
Definition: loader.c:40
#define SHT_NOBITS
Definition: common.h:254
VOID NTAPI FrLdrAddPageMapping(ppc_map_set_t *set, int proc, paddr_t phys, vaddr_t virt)
Definition: mboot.c:152
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
#define SWAPW(x)
Definition: bytesex.h:8
int mapsize
Definition: mboot.c:123
of_proxy ofproxy
Definition: mach.c:29
#define TYPE_PEHEADER
Definition: reactos.h:4
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
Elf32_Half e_shnum
Definition: elf32.h:61
char CHAR
Definition: xmlstorage.h:175
#define R_PPC_ADDR24
Definition: elf-powerpc.h:121
Elf32_Word sh_link
Definition: elf32.h:77
int(* of_proxy)(int table_off, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5, void *arg6)
Definition: of.h:10
LONG NTSTATUS
Definition: precomp.h:26
int ofw_callmethod_ret(const char *method, int handle, int nargs, int *args, int ret)
Definition: ofw_method.c:15
int _start[]
#define SWAPD(x)
Definition: bytesex.h:7
struct _ppc_map_set_t ppc_map_set_t
#define MAXULONG_PTR
Definition: basetsd.h:103
#define S(x)
Definition: test.h:190
Elf32_Sword r_addend
Definition: elf32.h:124
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
static WCHAR String[]
Definition: stringtable.c:55
ACPI_BUFFER *RetBuffer ACPI_BUFFER *RetBuffer char ACPI_WALK_RESOURCE_CALLBACK void *Context ACPI_BUFFER *RetBuffer UINT16 ACPI_RESOURCE **ResourcePtr ACPI_GENERIC_ADDRESS *Reg UINT32 *ReturnValue UINT8 UINT8 *Slp_TypB ACPI_PHYSICAL_ADDRESS PhysicalAddress64 UINT32 UINT32 *TimeElapsed UINT32 ACPI_STATUS const char UINT32 ACPI_STATUS const char UINT32 const char const char * ModuleName
Definition: acpixf.h:1252
static HANDLE proc()
Definition: pdb.c:32
VOID FASTCALL FrLdrSetupPae(ULONG Magic)
Definition: mboot.c:305
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
Definition: movable.cpp:7
DWORD PointerToRawData
Definition: pedump.c:290
char * LPSTR
Definition: xmlstorage.h:182
#define R_PPC_NONE
Definition: elf-powerpc.h:119
int mmu_handle
Definition: mach.c:32
Elf32_Word sh_info
Definition: elf32.h:78
#define FASTCALL
Definition: nt_native.h:50
Definition: match.c:390
unsigned long paddr_t
Definition: mmu.h:89
int32_t INT
Definition: typedefs.h:56
#define SHT_REL
Definition: common.h:255
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
BOOLEAN PaeModeEnabled
Definition: mboot.c:72
PVOID memmove_dummy
Definition: mboot.c:78
char reactos_module_strings[64][256]
Definition: mboot.c:60
uint32_t ULONG_PTR
Definition: typedefs.h:63
NTSTATUS NTAPI LdrPEFixupImports(IN PVOID DllBase, IN PCHAR DllName)
Definition: loader.c:316
#define A(row, col)
while(1)
Definition: macro.lex.yy.c:740
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
PVOID NTAPI FrLdrMapImage(IN FILE *Image, IN PCHAR ShortName, IN ULONG ImageType)
Definition: mboot.c:764
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
Elf32_Word r_info
Definition: elf32.h:123
__asm__("\t.globl GetPhys\n" "GetPhys:\t\n" "mflr 0\n\t" "stwu 0,-16(1)\n\t" "mfmsr 5\n\t" "andi. 6,5,0xffef\n\t" "mtmsr 6\n\t" "isync\n\t" "sync\n\t" "lwz 3,0(3)\n\t" "mtmsr 5\n\t" "isync\n\t" "sync\n\t" "lwz 0,0(1)\n\t" "addi 1,1,16\n\t" "mtlr 0\n\t" "blr")
Elf32_Addr sh_addr
Definition: elf32.h:74
unsigned long vaddr_t
Definition: mmu.h:90
_Check_return_ _CRTIMP int __cdecl _strnicmp(_In_reads_or_z_(_MaxCount) const char *_Str1, _In_reads_or_z_(_MaxCount) const char *_Str2, _In_ size_t _MaxCount)
#define R_PPC_REL32
Definition: elf-powerpc.h:145
paddr_t MmuTranslate(paddr_t possibly_virtual)
Definition: mboot.c:129
FORCEINLINE PVOID FrLdrTempAlloc(_In_ SIZE_T Size, _In_ ULONG Tag)
Definition: mm.h:177
unsigned char BOOLEAN
#define ELF_SECTION(n)
Definition: mboot.c:415
smooth NULL
Definition: ftsmooth.c:416
struct _IMAGE_DOS_HEADER * PIMAGE_DOS_HEADER
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
Definition: module.h:566
const char * LPCSTR
Definition: xmlstorage.h:183
void * PVOID
Definition: retypes.h:9
#define ELF32_R_TYPE(info)
Definition: elf32.h:129
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
Elf32_Addr r_offset
Definition: elf32.h:122
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define PCHAR
Definition: match.c:90
PLOADER_MODULE CurrentModule
Definition: mboot.c:66
int _end[]
PVOID MmAllocateMemory(SIZE_T MemorySize)
PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries)
Definition: mm.c:297
static void * MmuAllocVsid(int vsid, int mask)
Definition: mmu.h:242
ULONG_PTR NextModuleBase
Definition: mboot.c:63
BOOLEAN NTAPI FrLdrCloseModule(ULONG_PTR ModuleBase, ULONG ModuleSize)
Definition: mboot.c:813
BOOLEAN NTAPI FrLdrMapModule(FILE *KernelImage, PCHAR ImageName, PCHAR MemLoadAddr, ULONG KernelAddr)
Definition: mboot.c:420
ULONG_PTR KernelEntryPoint
Definition: mboot.c:75
ULONG_PTR KernelBase
Definition: mboot.c:69
#define SHT_RELA
Definition: common.h:250
Elf32_Half e_shentsize
Definition: elf32.h:60
#define ADDR24_MASK
Definition: reactos.h:5
static const char * ImageName
Definition: image.c:34
Definition: ttei1.cpp:12
ULONG_PTR NTAPI FrLdrLoadModule(FILE *ModuleImage, LPCSTR ModuleName, PULONG ModuleSize)
Definition: mboot.c:704
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define PAGE_SIZE
Definition: env_spec_w32.h:49
VOID NTAPI FrLdrStartup(ULONG Magic)
Definition: mboot.c:196
PVOID KernelMemory
Definition: mboot.c:33
Elf32_Half st_shndx
Definition: elf32.h:144
#define TAG_MBOOT
Definition: mboot.c:58
static int MmuMapPage(ppc_map_info_t *info, int count)
Definition: mmu.h:197
#define P(row, col)
VOID MmFreeMemory(PVOID MemoryPointer)
Definition: mm.c:215
#define PFN_SHIFT
Definition: mboot.c:36
ULONG_PTR NTAPI FrLdrCreateModule(LPCSTR ModuleName)
Definition: mboot.c:787
#define ROUND_DOWN(n, align)
Definition: eventvwr.h:30
Elf32_Word sh_type
Definition: elf32.h:72
unsigned short USHORT
Definition: pedump.c:61
#define R_PPC_ADDR16_LO
Definition: elf-powerpc.h:123
unsigned long gpr[32]
Definition: mmu.h:113
void(* KernelEntryFn)(void *)
Definition: mboot.c:110
int MmuPageMiss(int trapCode, ppc_trap_frame_t *trap)
Definition: mboot.c:112
VOID PpcInitializeMmu(int max)
unsigned int * PULONG
Definition: retypes.h:1
#define MMU_ALL_RW
Definition: mmu.h:78
VOID FASTCALL FrLdrGetKernelBase(VOID)
Definition: mboot.c:327
VOID FASTCALL FrLdrGetPaeMode(VOID)
Definition: mboot.c:375
char * strchr(const char *String, int ch)
Definition: utclib.c:501
ImageType
Definition: gdiplusenums.h:191
BOOLEAN NTAPI FrLdrMapKernel(FILE *KernelImage)
Definition: mboot.c:691
ULONG FsGetFileSize(PFILE FileHandle)
Definition: fs.c:344
Elf32_Off sh_offset
Definition: elf32.h:75
static int MmuTurnOn(void *fun, void *arg)
Definition: mmu.h:227
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
static void MmuSetVsid(int start, int end, int vsid)
Definition: mmu.h:207
#define R_PPC_UADDR32
Definition: elf-powerpc.h:143
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
#define R_PPC_ADDR16_HA
Definition: elf-powerpc.h:125
GLfloat GLfloat p
Definition: glext.h:8902
#define R_PPC_ADDR32
Definition: elf-powerpc.h:120
Definition: _set.h:46
#define memset(x, y, z)
Definition: compat.h:39
VOID FASTCALL FrLdrSetupPageDirectory(VOID)
Definition: mboot.c:398
#define R_PPC_REL24
Definition: elf-powerpc.h:129
ppc_map_info_t * info
Definition: mboot.c:125
BOOLEAN FsReadFile(PFILE FileHandle, ULONG BytesToRead, ULONG *BytesRead, PVOID Buffer)
Definition: fs.c:324
#define ELF32_R_SYM(info)
Definition: elf32.h:128
#define printf
Definition: config.h:203
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
FORCEINLINE VOID FrLdrTempFree(PVOID Allocation, ULONG Tag)
Definition: mm.h:186