11. EFI 메모리 맵 정보 얻기

주요 BIOS/UEFI 작업 중 하나는 OS 메모리 맵을 표시하는 것이다. 운영 체제는 시스템에서 사용 가능한 RAM의 크기와 OS에서 사용할 수 있는 영역 및 사용할 수 없는 영역을 알아야 한다.

이를 위해 UEFI 스펙에서는 EFI_BOOT_SERVICES.GetMemoryMap() 함수를 정의해 주었다.

EFI_BOOT_SERVICES.GetMemoryMap()

Summary:
Returns the current memory map.

Prototype:
typedef
EFI_STATUS
(EFIAPI *EFI_GET_MEMORY_MAP) (
 IN OUT UINTN *MemoryMapSize,
 IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap,
 OUT UINTN *MapKey,
 OUT UINTN *DescriptorSize,
 OUT UINT32 *DescriptorVersion
 );

Parameters:
MemoryMapSize 		A pointer to the size, in bytes, of the MemoryMap buffer. On input,
			this is the size of the buffer allocated by the caller. On output, it is
			the size of the buffer returned by the firmware if the buffer was
			large enough, or the size of the buffer needed to contain the map if
			the buffer was too small.
MemoryMap		A pointer to the buffer in which firmware places the current memory
			map. The map is an array of EFI_MEMORY_DESCRIPTORs.
MapKey 			A pointer to the location in which firmware returns the key for the
			current memory map.
DescriptorSize 		A pointer to the location in which firmware returns the size, in bytes,
			of an individual EFI_MEMORY_DESCRIPTOR.
DescriptorVersion 	A pointer to the location in which firmware returns the version
			number associated with the EFI_MEMORY_DESCRIPTOR.


Status Codes Returned:
EFI_SUCCESS 		The memory map was returned in the MemoryMap buffer.
EFI_BUFFER_TOO_SMALL 	The MemoryMap buffer was too small. The current buffer size needed to
			hold the memory map is returned in MemoryMapSize.
EFI_INVALID_PARAMETER 	MemoryMapSize is NULL.
EFI_INVALID_PARAMETER 	The MemoryMap buffer is not too small and MemoryMap is NULL.

EFI_BOOT_SERVICES.AllocatePages()는 스펙에 따라 4KB 페이지를 할당한다.

아래는 이번 애플리케이션의 코드이다.

첫 번째 GetMemoryMap()함수의 호출은 MemoryMapSize=0이 모든 메모리 디스크립터를 저장하기 충분하지 않기 때문에 EFI_BUFFER_TOO_SMALL과 함께 실패할 것이다.

하지만 이 첫 번째 호출로 인해서 할당해야 하는 실제 크기로 MemoryMapSize를 채워준다.

그래서 바로 다음에 gBS->AllocatePool를 호출하여 필요한 크기를 할당한다.

아까 작성한 코드를 보면 AllocatePool()의 첫번째 인자인 EFI_MEMORY_TYPE으로 우리는 BootServices에 연결되고 OS가 실행을 시작한 후 해제될 데이터를 나타내는 EfiBootServicesData를 전달한다.

EFI_MEMORY_TYPE에 대한 다른 가능한 값은 UEFI 스펙 문서나 여기에서 찾을 수 있다. (https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Uefi/UefiMultiPhase.h)

메모리가 성공적으로 할당되었으면 마지막에 gBS->FreePool 호출로 할당을 해제해야 한다.

GetMemoryMap 함수에 대한 두 번째 호출이 성공하면 MemoryMap 주소에서 시작하는 EFI_MEMORY_DESCRIPTOR 개체 배열을 가져온다.

우리는 이 배열을 살펴보고 각각의 디스크립터에 대한 정보를 출력하려고 한다.

메모리 type 값을 문자열로 변환하기 위해 memory_type_to_str 도우미 함수를 만든다.

또한 메모리 속성을 문자열로 변환하기 위해 memory_attrs_to_str 도우미 함수를 만든다.

매크로는 일반적으로 오류가 발생하기 쉬운 디자인으로 간주되기 때문에 이 매크로가 좋은 방법은 아니지만 EDKII 코드 베이스에서 EFI_MEMORY_XXX로 정의된 가능한 모든 특성을 잇는 쉬운 방법을 제공한다.

이 매크로에서는 일부 EDKII 문자열 조작 함수를 사용했다.

이러한 문자열 함수를 쓸 경우 헤더 파일을 하나 더 추가해야 한다.

OVMF에서 앱을 빌드하고 실행하면 다음과 같은 내용이 표시된다.

MemoryInfo.efi

efi Shell에서 memmap명령어를 실행하여 결과가 맞는지 확인이 가능하다.

memmap result

보다시피 메모리 구역이 우리가 만든 프로그램과 일치하는 것을 알 수 있다.

Last updated