init.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. Abstract:
  5. Revision History
  6. --*/
  7. #include "lib.h"
  8. VOID
  9. EFIDebugVariable (
  10. VOID
  11. );
  12. VOID
  13. InitializeLib (
  14. IN EFI_HANDLE ImageHandle,
  15. IN EFI_SYSTEM_TABLE *SystemTable
  16. )
  17. /*++
  18. Routine Description:
  19. Initializes EFI library for use
  20. Arguments:
  21. Firmware's EFI system table
  22. Returns:
  23. None
  24. --*/
  25. {
  26. EFI_LOADED_IMAGE *LoadedImage;
  27. EFI_STATUS Status;
  28. CHAR8 *LangCode;
  29. if (!LibInitialized) {
  30. LibInitialized = TRUE;
  31. LibFwInstance = FALSE;
  32. //
  33. // Set up global pointer to the system table, boot services table,
  34. // and runtime services table
  35. //
  36. ST = SystemTable;
  37. BS = SystemTable->BootServices;
  38. RT = SystemTable->RuntimeServices;
  39. // ASSERT (CheckCrc(0, &ST->Hdr));
  40. // ASSERT (CheckCrc(0, &BS->Hdr));
  41. // ASSERT (CheckCrc(0, &RT->Hdr));
  42. //
  43. // Initialize pool allocation type
  44. //
  45. if (ImageHandle) {
  46. Status = uefi_call_wrapper(
  47. BS->HandleProtocol,
  48. 3,
  49. ImageHandle,
  50. &LoadedImageProtocol,
  51. (VOID*)&LoadedImage
  52. );
  53. if (!EFI_ERROR(Status)) {
  54. PoolAllocationType = LoadedImage->ImageDataType;
  55. }
  56. EFIDebugVariable ();
  57. }
  58. //
  59. // Initialize Guid table
  60. //
  61. InitializeGuid();
  62. InitializeLibPlatform(ImageHandle,SystemTable);
  63. }
  64. //
  65. //
  66. //
  67. if (ImageHandle && UnicodeInterface == &LibStubUnicodeInterface) {
  68. LangCode = LibGetVariable (VarLanguage, &EfiGlobalVariable);
  69. InitializeUnicodeSupport (LangCode);
  70. if (LangCode) {
  71. FreePool (LangCode);
  72. }
  73. }
  74. }
  75. VOID
  76. InitializeUnicodeSupport (
  77. CHAR8 *LangCode
  78. )
  79. {
  80. EFI_UNICODE_COLLATION_INTERFACE *Ui;
  81. EFI_STATUS Status;
  82. CHAR8 *Languages;
  83. UINTN Index, Position, Length;
  84. UINTN NoHandles;
  85. EFI_HANDLE *Handles;
  86. //
  87. // If we don't know it, lookup the current language code
  88. //
  89. LibLocateHandle (ByProtocol, &UnicodeCollationProtocol, NULL, &NoHandles, &Handles);
  90. if (!LangCode || !NoHandles) {
  91. goto Done;
  92. }
  93. //
  94. // Check all driver's for a matching language code
  95. //
  96. for (Index=0; Index < NoHandles; Index++) {
  97. Status = uefi_call_wrapper(BS->HandleProtocol, 3, Handles[Index], &UnicodeCollationProtocol, (VOID*)&Ui);
  98. if (EFI_ERROR(Status)) {
  99. continue;
  100. }
  101. //
  102. // Check for a matching language code
  103. //
  104. Languages = Ui->SupportedLanguages;
  105. Length = strlena(Languages);
  106. for (Position=0; Position < Length; Position += ISO_639_2_ENTRY_SIZE) {
  107. //
  108. // If this code matches, use this driver
  109. //
  110. if (CompareMem (Languages+Position, LangCode, ISO_639_2_ENTRY_SIZE) == 0) {
  111. UnicodeInterface = Ui;
  112. goto Done;
  113. }
  114. }
  115. }
  116. Done:
  117. //
  118. // Cleanup
  119. //
  120. if (Handles) {
  121. FreePool (Handles);
  122. }
  123. }
  124. #ifndef MAX_ARGV_CONTENTS_SIZE
  125. # define MAX_CMDLINE_SIZE 1024
  126. #endif
  127. #ifndef MAX_ARGC
  128. # define MAX_CMDLINE_ARGC 32
  129. #endif
  130. INTN
  131. GetShellArgcArgv(
  132. EFI_HANDLE ImageHandle,
  133. CHAR16 **ResultArgv[]
  134. )
  135. {
  136. EFI_STATUS Status;
  137. EFI_LOADED_IMAGE *LoadedImage = NULL;
  138. static CHAR16 ArgvContents[MAX_CMDLINE_SIZE];
  139. static CHAR16 *Argv[MAX_CMDLINE_ARGC], *ArgStart, *c;
  140. UINTN Argc = 0, BufLen;
  141. Status = uefi_call_wrapper(BS->HandleProtocol, 3,
  142. ImageHandle, &LoadedImageProtocol, &LoadedImage);
  143. if (EFI_ERROR(Status))
  144. return -1;
  145. BufLen = LoadedImage->LoadOptionsSize;
  146. if (BufLen < 2) /* We are expecting at least a \0 */
  147. return -1;
  148. else if (BufLen > sizeof(ArgvContents))
  149. BufLen = sizeof(ArgvContents);
  150. CopyMem(ArgvContents, LoadedImage->LoadOptions, BufLen);
  151. ArgvContents[MAX_CMDLINE_SIZE - 1] = L'\0';
  152. for (c = ArgStart = ArgvContents ; *c != L'\0' ; ++c) {
  153. if (*c == L' ') {
  154. *c = L'\0';
  155. if (Argc < MAX_CMDLINE_ARGC) Argv[Argc++] = ArgStart;
  156. ArgStart = c + 1;
  157. }
  158. }
  159. if ((*ArgStart != L'\0') && (Argc < MAX_CMDLINE_ARGC))
  160. Argv[Argc++] = ArgStart;
  161. *ResultArgv = Argv;
  162. return Argc;
  163. }
  164. VOID
  165. EFIDebugVariable (
  166. VOID
  167. )
  168. {
  169. EFI_STATUS Status;
  170. UINT32 Attributes;
  171. UINTN DataSize;
  172. UINTN NewEFIDebug;
  173. DataSize = sizeof(EFIDebug);
  174. Status = uefi_call_wrapper(RT->GetVariable, 5, L"EFIDebug", &EfiGlobalVariable, &Attributes, &DataSize, &NewEFIDebug);
  175. if (!EFI_ERROR(Status)) {
  176. EFIDebug = NewEFIDebug;
  177. }
  178. }