AllocPages.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2013 Jerry Hoemann <jerry.hoemann@hp.com>
  3. *
  4. *
  5. * Application to allocate memory at EFI. Syntax of command
  6. * mimics the EFI Boot Service "AllocatePages."
  7. *
  8. * See UEFI spec 2.3, Section 6.2.
  9. *
  10. *
  11. FS1:\> memmap
  12. Type Start End #pages Attributes
  13. BS_Code 0000000000000000-0000000000000FFF 0000000000000001 000000000000000F
  14. Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F
  15. Reserved 000000000008E000-000000000008FFFF 0000000000000002 000000000000000F
  16. Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F
  17. Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F
  18. BS_Code 0000000010000000-0000000010061FFF 0000000000000062 000000000000000F
  19. Available 0000000010062000-000000005CDFFFFF 000000000004CD9E 000000000000000F
  20. ACPI_NVS 000000005CE00000-000000005DDFFFFF 0000000000001000 000000000000000F
  21. BS_Data 000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F
  22. Available 000000005E000000-000000005EF1CFFF 0000000000000F1D 000000000000000F
  23. BS_Data 000000005EF1D000-00000000709FBFFF 0000000000011ADF 000000000000000F
  24. Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F
  25. LoaderCode 00000000710E4000-00000000711FEFFF 000000000000011B 000000000000000F
  26. Available 00000000711FF000-0000000071901FFF 0000000000000703 000000000000000F
  27. BS_Code 0000000071902000-00000000721FEFFF 00000000000008FD 000000000000000F
  28. Example to allocat 5 pages type BootCode at address 20000000 (hex)
  29. FS1:\> AllocPages.efi 2 3 5 20000000
  30. AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__]
  31. __AllocType__ {0,1,2} -- Any, MaxAddr, Addr
  32. __MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ...
  33. __NumPages__ {0..F000000}
  34. [__Addr__] 0... 3FFFFFFFFFFF
  35. All numbers in hex no leading 0x
  36. AllocatPage(2,3,5,20000000)
  37. Example to allocat 5 pages type BootCode at address 30000000 (hex)
  38. FS1:\> AllocPages.efi 2 3 5 30000000
  39. AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__]
  40. __AllocType__ {0,1,2} -- Any, MaxAddr, Addr
  41. __MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ...
  42. __NumPages__ {0..F000000}
  43. [__Addr__] 0... 3FFFFFFFFFFF
  44. All numbers in hex no leading 0x
  45. FS1:\> memmap
  46. Type Start End #pages Attributes
  47. BS_Code 0000000000000000-0000000000000FFF 0000000000000001 000000000000000F
  48. Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F
  49. Reserved 000000000008E000-000000000008FFFF 0000000000000002 000000000000000F
  50. Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F
  51. Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F
  52. BS_Code 0000000010000000-0000000010061FFF 0000000000000062 000000000000000F
  53. Available 0000000010062000-000000001FFFFFFF 000000000000FF9E 000000000000000F
  54. BS_Code 0000000020000000-0000000020004FFF 0000000000000005 000000000000000F
  55. Available 0000000020005000-000000002FFFFFFF 000000000000FFFB 000000000000000F
  56. BS_Code 0000000030000000-0000000030004FFF 0000000000000005 000000000000000F
  57. Available 0000000030005000-000000005CDFFFFF 000000000002CDFB 000000000000000F
  58. ACPI_NVS 000000005CE00000-000000005DDFFFFF 0000000000001000 000000000000000F
  59. BS_Data 000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F
  60. Available 000000005E000000-000000005EF1CFFF 0000000000000F1D 000000000000000F
  61. BS_Data 000000005EF1D000-00000000709FBFFF 0000000000011ADF 000000000000000F
  62. Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F
  63. LoaderCode 00000000710E4000-00000000711FEFFF 000000000000011B 000000000000000F
  64. Available 00000000711FF000-0000000071901FFF 0000000000000703 000000000000000F
  65. BS_Code 0000000071902000-00000000721FEFFF 00000000000008FD 000000000000000F
  66. */
  67. #include <efi.h>
  68. #include <efilib.h>
  69. #define MAX_NUM_PAGES 0x000000000F000000
  70. #define MAX_ADDR ((1ULL << 46) - 1)
  71. #define MAX_ARGS 256
  72. #define CHAR_SPACE L' '
  73. #define DEBUG 0
  74. INTN
  75. argify(CHAR16 *buf, UINTN len, CHAR16 **argv)
  76. {
  77. UINTN i=0, j=0;
  78. CHAR16 *p = buf;
  79. if (buf == 0) {
  80. argv[0] = NULL;
  81. return 0;
  82. }
  83. /* len represents the number of bytes, not the number of 16 bytes chars */
  84. len = len >> 1;
  85. /*
  86. * Here we use CHAR_NULL as the terminator rather than the length
  87. * because it seems like the EFI shell return rather bogus values for it.
  88. * Apparently, we are guaranteed to find the '\0' character in the buffer
  89. * where the real input arguments stop, so we use it instead.
  90. */
  91. for(;;) {
  92. while (buf[i] == CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
  93. if (buf[i] == CHAR_NULL || i == len) goto end;
  94. p = buf+i;
  95. i++;
  96. while (buf[i] != CHAR_SPACE && buf[i] != CHAR_NULL && i < len) i++;
  97. argv[j++] = p;
  98. if (buf[i] == CHAR_NULL) goto end;
  99. buf[i] = CHAR_NULL;
  100. if (i == len) goto end;
  101. i++;
  102. if (j == MAX_ARGS-1) {
  103. Print(L"too many arguments (%d) truncating\n", j);
  104. goto end;
  105. }
  106. }
  107. end:
  108. argv[j] = NULL;
  109. return j;
  110. }
  111. EFI_STATUS
  112. efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
  113. {
  114. EFI_STATUS efi_status;
  115. EFI_GUID LoadedImageProtocol = LOADED_IMAGE_PROTOCOL;
  116. EFI_LOADED_IMAGE *info;
  117. CHAR16 arglist[MAX_ARGS+1] = {0};
  118. CHAR16 *argv[MAX_ARGS];
  119. INTN argc = 0;
  120. INTN err = 0;
  121. INTN AllocType = -1;
  122. INTN MemType = -1;
  123. INTN NumPages = -1;
  124. UINTN Addr = 0;
  125. InitializeLib(image, systab);
  126. efi_status = uefi_call_wrapper( BS->HandleProtocol, 3, image,
  127. &LoadedImageProtocol, &info);
  128. Print(L"AllocatePage: __AllocType__ __MemType__ __NumPages__ [__Addr__]\n");
  129. Print(L"__AllocType__ {0,1,2} -- Any, MaxAddr, Addr\n");
  130. Print(L"__MemType__ {0..13}, Reserved ==0, LCode==1, LData==2, BSCode==3, BSData==4, ...\n");
  131. Print(L"__NumPages__ {0..%x}\n", MAX_NUM_PAGES);
  132. Print(L"[__Addr__] 0... %llx\n", MAX_ADDR);
  133. Print(L"All numbers in hex no leading 0x\n");
  134. Print(L"\n");
  135. #if DEBUG
  136. Print(L"%s\n", info->LoadOptions);
  137. #endif
  138. #if DEBUG
  139. Print(L"Set up arglist\n");
  140. #endif
  141. CopyMem(arglist, info->LoadOptions, info->LoadOptionsSize);
  142. #if DEBUG
  143. Print(L"arglist = <%s>\n", arglist);
  144. #endif
  145. #if DEBUG
  146. Print(L"Now try argify\n");
  147. #endif
  148. argc = argify(arglist, info->LoadOptionsSize, argv);
  149. #if DEBUG
  150. Print(L"argc = %d\n", argc);
  151. #endif
  152. #if DEBUG
  153. for (c = 0; c < argc; c++ ) {
  154. Print(L"argv[%d] = <%s>\n", c, argv[c]);
  155. }
  156. #endif
  157. if ( (argc < 3) || (argc > 5) ) {
  158. Print(L"Wrong argument count\n");
  159. return EFI_SUCCESS;
  160. }
  161. AllocType = xtoi(argv[1]);
  162. MemType = xtoi(argv[2]);
  163. NumPages = xtoi(argv[3]);
  164. if ( argc == 5 ) Addr = xtoi(argv[4]);
  165. if ( (AllocType < 0) || (AllocType > 2)) {
  166. Print(L"Invalid AllocType\n");
  167. err++;
  168. }
  169. if ( (MemType < 0) || (MemType > 13) ) {
  170. Print(L"Invalid MemType\n");
  171. err++;
  172. }
  173. if ( (NumPages < 0) || (NumPages > MAX_NUM_PAGES) ) {
  174. Print(L"Inavlid NumPages\n");
  175. err++;
  176. }
  177. if ( Addr > MAX_ADDR ) {
  178. Print(L"Inavlid Address\n");
  179. err++;
  180. }
  181. if ( err ) {
  182. return EFI_INVALID_PARAMETER;
  183. }
  184. Print(L"AllocatPage(%d,%d,%d,%lx)\n", AllocType, MemType, NumPages, Addr);
  185. efi_status = uefi_call_wrapper(BS->AllocatePages, 4, AllocType, MemType, NumPages, &Addr);
  186. if ( EFI_ERROR(efi_status) ) {
  187. Print(L"Allocate Pages Failed: %d\n", efi_status);
  188. return efi_status;
  189. }
  190. return EFI_SUCCESS;
  191. }