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