Home | History | Annotate | Line # | Download | only in apps
      1 /*	$NetBSD: FreePages.c,v 1.1.1.1 2018/08/16 18:17:47 jmcneill Exp $	*/
      2 
      3 
      4 
      5 /*
      6  * Copyright (C) 2013 Jerry Hoemann <jerry.hoemann (at) hp.com>
      7  *
      8  * Application to allocate memory at EFI.  Syntax of command
      9  * mimics the EFI Boot Service "FreePages."
     10  *
     11  * See UEFI spec 2.3, Section 6.2.
     12  *
     13 
     14 Example freeing a 5 page BS_Code setment at address: 0000000020000000 (hex)
     15 
     16 
     17 FS1:\> memmap
     18 Type      Start            End              #pages             Attributes
     19 BS_Code   0000000000000000-0000000000000FFF 0000000000000001 000000000000000F
     20 Available 0000000000001000-000000000008DFFF 000000000000008D 000000000000000F
     21 Reserved  000000000008E000-000000000008FFFF 0000000000000002 000000000000000F
     22 Available 0000000000090000-000000000009FFFF 0000000000000010 000000000000000F
     23 Available 0000000000100000-000000000FFFFFFF 000000000000FF00 000000000000000F
     24 BS_Code   0000000010000000-0000000010061FFF 0000000000000062 000000000000000F
     25 Available 0000000010062000-000000001FFFFFFF 000000000000FF9E 000000000000000F
     26 BS_Code   0000000020000000-0000000020004FFF 0000000000000005 000000000000000F
     27 Available 0000000020005000-000000005DDFFFFF 000000000003DDFB 000000000000000F
     28 BS_Data   000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F
     29 Available 000000005E000000-000000006DE7CFFF 000000000000FE7D 000000000000000F
     30 ACPI_NVS  000000006DE7D000-000000006EE7CFFF 0000000000001000 000000000000000F
     31 BS_Data   000000006EE7D000-00000000709FBFFF 0000000000001B7F 000000000000000F
     32 Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F
     33 
     34 
     35 FS1:\> FreePages 0000000020000000 5
     36 FreePages: __PhysAddr__ __PgCnt__
     37 __PhysAddr__   0... 3FFFFFFFFFFF
     38 __PgCnt__     [0..F000000]
     39 All numbers hex w/ no leading 0x
     40 
     41 FreePages(20000000,5)
     42 
     43 
     44 
     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-000000005DDFFFFF 000000000004DD9E 000000000000000F
     54 BS_Data   000000005DE00000-000000005DFFFFFF 0000000000000200 000000000000000F
     55 Available 000000005E000000-000000006DE7CFFF 000000000000FE7D 000000000000000F
     56 ACPI_NVS  000000006DE7D000-000000006EE7CFFF 0000000000001000 000000000000000F
     57 BS_Data   000000006EE7D000-00000000709FBFFF 0000000000001B7F 000000000000000F
     58 Available 00000000709FC000-00000000710E3FFF 00000000000006E8 000000000000000F
     59 
     60 
     61  */
     62 
     63 #include <efi.h>
     64 #include <efilib.h>
     65 
     66 /*
     67  * FreePages:  __PhysAddr__ __PgCnt__
     68  *
     69  */
     70 
     71 #define MAX_NUM_PAGES 0x000000000F000000
     72 
     73 #define MAX_ADDR ((1ULL << 46) - 1)
     74 
     75 #ifdef DEBUG
     76 #undef DEBUG
     77 #endif
     78 #define DEBUG 0
     79 
     80 
     81 EFI_STATUS
     82 efi_main (EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
     83 {
     84 
     85 	EFI_STATUS efi_status;
     86 	CHAR16 **argv;
     87 	INTN argc = 0;
     88 #if DEBUG
     89 	INTN c = 0;
     90 #endif
     91 	INTN err = 0;
     92 
     93 	INTN PgCnt = -1;
     94 	EFI_PHYSICAL_ADDRESS PhysAddr = 0;
     95 
     96 	InitializeLib(image, systab);
     97 
     98 	Print(L"FreePages: __PhysAddr__ __PgCnt__\n");
     99 	Print(L"__PhysAddr__   0... %llx\n", MAX_ADDR);
    100 	Print(L"__PgCnt__     [0..%lx]\n", MAX_NUM_PAGES);
    101 	Print(L"All numbers hex w/ no leading 0x\n");
    102 	Print(L"\n");
    103 
    104 #if DEBUG
    105 	Print(L"Now parse argc/argv\n");
    106 #endif
    107 	argc = GetShellArgcArgv(image, &argv);
    108 #if DEBUG
    109 	Print(L"argc = %d\n", argc);
    110 #endif
    111 
    112 #if DEBUG
    113 	for (c = 0;  c < argc;  c++ ) {
    114 		Print(L"argv[%d] = <%s>\n", c, argv[c]);
    115 	}
    116 #endif
    117 	if (argc != 3) {
    118 		Print(L"Invalid argument count\n");
    119 		return EFI_SUCCESS;
    120 	}
    121 
    122 	PhysAddr = xtoi(argv[1]);
    123 	PgCnt	 = xtoi(argv[2]);
    124 
    125 	if ( (PgCnt < 0) || (PgCnt > MAX_NUM_PAGES) ) {
    126 		Print(L"Inavlid PgCnt\n");
    127 		err++;
    128 	}
    129 	if ( PhysAddr > MAX_ADDR ) {
    130 		Print(L"Inavlid Address\n");
    131 		err++;
    132 	}
    133 	if ( err ) {
    134 		return EFI_SUCCESS;
    135 	}
    136 
    137 	Print(L"FreePages(%lx,%d)\n", PhysAddr, PgCnt);
    138 
    139 	efi_status = uefi_call_wrapper(BS->FreePages, 2, PhysAddr, PgCnt);
    140 
    141 	if ( EFI_ERROR(efi_status) ) {
    142 		Print(L"Free Pages Failed: %d\n", efi_status);
    143 		return efi_status;
    144 	}
    145 
    146 	return EFI_SUCCESS;
    147 }
    148