Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
4.4 KiB

  1. #include <efi.h>
  2. #include <efilib.h>
  3. //
  4. // Prototype
  5. //
  6. void TrimNonPrint(CHAR16 * str);
  7. void Launch (CHAR16 *exePath);
  8. //
  9. //Globals
  10. //
  11. EFI_HANDLE ExeHdl;
  12. EFI_LOADED_IMAGE *ExeImage;
  13. //
  14. // Defines
  15. //
  16. #define REGISTER1 L"*register"
  17. #define REGISTER2 L"*register*"
  18. #define STARTFILE L"startup.nsh"
  19. #define BOOTOFILE L"boot.nvr"
  20. #define OSLOADOPT L"OSLOADER"
  21. #define PARTENT L"partition"
  22. #define PARTENTRE L"*partition*"
  23. #define APPNAME_TOLAUNCH L"setupldr.efi"
  24. EFI_STATUS
  25. EfiMain ( IN EFI_HANDLE ImageHandle,
  26. IN EFI_SYSTEM_TABLE *SystemTable)
  27. {
  28. EFI_STATUS Status;
  29. EFI_FILE_HANDLE bootFile;
  30. UINTN Count;
  31. BOOLEAN LaunchTheApplication;
  32. CHAR16 App[30];
  33. InitializeLib (ImageHandle, SystemTable);
  34. ExeHdl = ImageHandle;
  35. BS->HandleProtocol (ImageHandle, &LoadedImageProtocol, &ExeImage);
  36. LaunchTheApplication = FALSE;
  37. ST->ConOut->ClearScreen(ST->ConOut);
  38. Print (L"Press any key to boot from CD-ROM...");
  39. for (Count = 0; Count < 3; Count++) {
  40. Status = WaitForSingleEvent (ST->ConIn->WaitForKey,1*10000000);
  41. if (Status != EFI_TIMEOUT){
  42. LaunchTheApplication = TRUE;
  43. break;
  44. }
  45. Print(L".");
  46. }
  47. if (!LaunchTheApplication) {
  48. BS->Exit(ExeHdl,EFI_TIMEOUT,0,NULL);
  49. }
  50. StrCpy(App, APPNAME_TOLAUNCH);
  51. Launch( App );
  52. //
  53. // If we get here, we failed to load the OS
  54. //
  55. Print(L"Failed to launch SetupLDR.");
  56. return EFI_SUCCESS;
  57. }
  58. void
  59. Launch (CHAR16 *exePath)
  60. {
  61. EFI_HANDLE exeHdl=NULL;
  62. UINTN i;
  63. EFI_DEVICE_PATH *ldrDevPath;
  64. EFI_STATUS Status;
  65. EFI_FILE_IO_INTERFACE *Vol;
  66. EFI_FILE_HANDLE RootFs;
  67. EFI_FILE_HANDLE CurDir;
  68. EFI_FILE_HANDLE FileHandle;
  69. CHAR16 FileName[100],*DevicePathAsString;
  70. //
  71. // Open the volume for the device where the exe was loaded from.
  72. //
  73. Status = BS->HandleProtocol (ExeImage->DeviceHandle,
  74. &FileSystemProtocol,
  75. &Vol
  76. );
  77. if (EFI_ERROR(Status)) {
  78. Print(L"Can not get a FileSystem handle for ExeImage->DeviceHandle\n");
  79. BS->Exit(ExeHdl,EFI_SUCCESS,0,NULL);
  80. }
  81. Status = Vol->OpenVolume (Vol, &RootFs);
  82. if (EFI_ERROR(Status)) {
  83. Print(L"Can not open the volume for the file system\n");
  84. BS->Exit(ExeHdl,EFI_SUCCESS,0,NULL);
  85. }
  86. CurDir = RootFs;
  87. //
  88. // Open the file relative to the root.
  89. //
  90. DevicePathAsString = DevicePathToStr(ExeImage->FilePath);
  91. if (DevicePathAsString!=NULL) {
  92. StrCpy(FileName,DevicePathAsString);
  93. FreePool(DevicePathAsString);
  94. }
  95. FileName[0] = 0;
  96. StrCat(FileName,exePath);
  97. // size = StrLen(FileName);
  98. // Print(L"Length of filename is %d\n", size);
  99. // DumpHex(4, 0, 10, &FileName[size - 4]);
  100. //
  101. // Get rid of trailing spaces, new lines, whatever
  102. //
  103. TrimNonPrint(FileName);
  104. Status = CurDir->Open (CurDir,
  105. &FileHandle,
  106. FileName,
  107. EFI_FILE_MODE_READ,
  108. 0
  109. );
  110. if (EFI_ERROR(Status)) {
  111. Print(L"Can not open the file ->%s<-, error was %X\n",FileName, Status);
  112. BS->Exit(ExeHdl,EFI_SUCCESS,0,NULL);
  113. } else {
  114. // Print(L"Opened %s\n",FileName);
  115. }
  116. ldrDevPath = FileDevicePath (ExeImage->DeviceHandle,FileName);
  117. /*
  118. if (ldrDevPath) {
  119. Print (L"Type: %d\nSub-Type: %d\nLength[0][1]: [%d][%d]\n",ldrDevPath->Type,
  120. ldrDevPath->SubType,ldrDevPath->Length[0],ldrDevPath->Length[1]);
  121. }else {
  122. Print (L"bad dev path\n");
  123. }
  124. */
  125. // DumpHex (4,0,ldrDevPath->Length[0],ldrDevPath);
  126. Status = BS->LoadImage (FALSE,ExeHdl,ldrDevPath,NULL,0,&exeHdl);
  127. if (!(EFI_ERROR (Status))) {
  128. // Print (L"Image loaded!\n");
  129. }else {
  130. Print (L"Load Error: %X\n",Status);
  131. BS->Exit(ExeHdl,EFI_SUCCESS,0,NULL);
  132. }
  133. FreePool (ldrDevPath);
  134. BS->StartImage (exeHdl,&i,NULL);
  135. return;
  136. }
  137. void
  138. TrimNonPrint(
  139. CHAR16 * str
  140. )
  141. {
  142. INTN i,size;
  143. if ((NULL == str) || (L'\0' == *str)) {
  144. return;
  145. }
  146. size = (INTN) StrLen(str);
  147. // Print(L"Size is %d\n", size);
  148. // DumpHex(4, 0, 2, &str[size]);
  149. for (i = size; i > 0; i--) {
  150. if (str[i] <= 0x20) {
  151. str[i] = L'\0';
  152. }
  153. else {
  154. // Leave when we hit a legit character
  155. break;
  156. }
  157. }
  158. }