VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/Library/CommonLib/EfiMem.c
blob: 872d3debd8ee886d8bb9018b05944afe25cf907d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/** @file
EFI memory helpers routines/wrappers

Copyright (c) 2016. Disk Cryptography Services for EFI (DCS), Alex Kolotnikov
Copyright (c) 2016. VeraCrypt, Mounir IDRASSI 

This program and the accompanying materials are licensed and made available
under the terms and conditions of the GNU Lesser General Public License, version 3.0 (LGPL-3.0).

The full text of the license may be found at
https://opensource.org/licenses/LGPL-3.0
**/

#include <Uefi.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseMemoryLib.h>

#include "Library/CommonLib.h"


//////////////////////////////////////////////////////////////////////////
// Memory procedures wrappers
//////////////////////////////////////////////////////////////////////////

VOID*
MemAlloc(
   IN UINTN size
   ) {
   return AllocateZeroPool(size);
}

VOID*
MemRealloc(
	IN UINTN  OldSize,
	IN UINTN  NewSize,
	IN VOID   *OldBuffer  OPTIONAL
	) {
	return ReallocatePool(OldSize, NewSize, OldBuffer);
}

VOID
MemFree(
   IN VOID* ptr
   ) {
	if(ptr != NULL) FreePool(ptr);
}

//////////////////////////////////////////////////////////////////////////
// Memory mapped IO
//////////////////////////////////////////////////////////////////////////

EFI_STATUS
PrepareMemory(
   IN UINTN    address,
   IN UINTN    len,
   OUT VOID**  mem)
{
   EFI_STATUS              status;
   EFI_PHYSICAL_ADDRESS    ptr;
   VOID*                   buf;
   UINTN                   pages;
   pages = ((len & ~0x0FFF) + 0x1000) >> 12;
   ptr = address & ~0x0FFF;
//	OUT_PRINT(L"mem try %0x, %0x\n", pages, (UINTN)ptr);
   status = gBS->AllocatePages(AllocateAddress, EfiMemoryMappedIO, pages, &ptr);
   if (EFI_ERROR(status)) {
      return status;
   }
   buf = (void*)(UINTN)ptr;
   SetMem(buf, pages << 12, 0);
   *mem = buf;
   return status;
}

//////////////////////////////////////////////////////////////////////////
// Memory misc
//////////////////////////////////////////////////////////////////////////
EFI_STATUS MemoryHasPattern (
	CONST VOID* buffer,
	UINTN bufferLen,
	CONST VOID* pattern,
	UINTN patternLen)
{
	EFI_STATUS status = EFI_NOT_FOUND;
	if (patternLen <= bufferLen)
	{
		UINTN i;
		CONST UINT8* memPtr = (CONST UINT8*) buffer;
		for (i = 0; i <= (bufferLen - patternLen); ++i)
		{
			if (CompareMem (&memPtr[i], pattern, patternLen) == 0)
			{
				status = EFI_SUCCESS;
				break;
			}
		}
	}

	return status;
}