Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*	$NetBSD: efimemory.c,v 1.10 2023/05/14 09:07:54 riastradh Exp $	*/

/*-
 * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "efiboot.h"

#include <bootinfo.h>

static const char *efi_memory_type[] = {
	[EfiReservedMemoryType]		= "Reserved Memory Type",
	[EfiLoaderCode]			= "Loader Code",
	[EfiLoaderData]			= "Loader Data",
	[EfiBootServicesCode]		= "Boot Services Code",
	[EfiBootServicesData]		= "Boot Services Data",
	[EfiRuntimeServicesCode]	= "Runtime Services Code",
	[EfiRuntimeServicesData]	= "Runtime Services Data",
	[EfiConventionalMemory]		= "Conventional Memory",
	[EfiUnusableMemory]		= "Unusable Memory",
	[EfiACPIReclaimMemory]		= "ACPI Reclaim Memory",
	[EfiACPIMemoryNVS]		= "ACPI Memory NVS",
	[EfiMemoryMappedIO]		= "MMIO",
	[EfiMemoryMappedIOPortSpace]	= "MMIO (Port Space)",
	[EfiPalCode]			= "Pal Code",
	[EfiPersistentMemory]		= "Persistent Memory",
};

#ifndef KERN_LOADSPACE_SIZE
#define KERN_LOADSPACE_SIZE	(128 * 1024 * 1024)	/* 128MiB */
#endif

static int
getmemtype(EFI_MEMORY_DESCRIPTOR *md)
{

	switch (md->Type) {
	case EfiLoaderCode:
	case EfiLoaderData:
	case EfiBootServicesCode:
	case EfiBootServicesData:
	case EfiConventionalMemory:
		return (md->Attribute & EFI_MEMORY_WB) ?
		    BIM_Memory : BIM_Reserved;

	case EfiACPIReclaimMemory:
		return BIM_ACPI;

	case EfiACPIMemoryNVS:
		return BIM_NVS;

	case EfiPersistentMemory:
		return BIM_PMEM;

	case EfiReservedMemoryType:
	case EfiRuntimeServicesCode:
	case EfiRuntimeServicesData:
	case EfiUnusableMemory:
	case EfiMemoryMappedIO:
	case EfiMemoryMappedIOPortSpace:
	case EfiPalCode:
	case EfiMaxMemoryType:
	default:
		return BIM_Reserved;
	}
}

EFI_MEMORY_DESCRIPTOR *
efi_memory_get_map(UINTN *NoEntries, UINTN *MapKey, UINTN *DescriptorSize,
    UINT32 *DescriptorVersion, bool sorted)
{
	EFI_MEMORY_DESCRIPTOR *desc, *md, *next, *target, *tmp;
	UINTN i, j;

	*NoEntries = 0;
	desc = LibMemoryMap(NoEntries, MapKey, DescriptorSize,
	    DescriptorVersion);
	if (desc == NULL)
		panic("efi_memory_get_map failed");

	if (!sorted)
		return desc;

	tmp = alloc(*DescriptorSize);
	if (tmp == NULL)
		return desc;

	for (i = 0, md = desc; i < *NoEntries - 1; i++, md = next) {
		target = next = NextMemoryDescriptor(md, *DescriptorSize);
		for (j = i + 1; j < *NoEntries; j++) {
			if (md->PhysicalStart > target->PhysicalStart) {
				CopyMem(tmp, md, *DescriptorSize);
				CopyMem(md, target, *DescriptorSize);
				CopyMem(target, tmp, *DescriptorSize);
			}
			target = NextMemoryDescriptor(target, *DescriptorSize);
		}
	}
	dealloc(tmp, *DescriptorSize);

	return desc;
}

EFI_MEMORY_DESCRIPTOR *
efi_memory_compact_map(EFI_MEMORY_DESCRIPTOR *desc, UINTN *NoEntries,
    UINTN DescriptorSize)
{
	EFI_MEMORY_DESCRIPTOR *md, *next, *target, *tmp;
	UINTN i, j;
	UINT32 type;
	bool first = true, do_compact;

	for (i = 0, md = target = desc; i < *NoEntries; i++, md = next) {
		type = md->Type;
		switch (type) {
		case EfiLoaderCode:
		case EfiLoaderData:
		case EfiBootServicesCode:
		case EfiBootServicesData:
		case EfiConventionalMemory:
			if ((md->Attribute & EFI_MEMORY_WB) != 0)
				type = EfiConventionalMemory;
			if (md->Attribute == target->Attribute) {
				do_compact = true;
				break;
			}
			/* FALLTHROUGH */
		case EfiACPIReclaimMemory:
		case EfiACPIMemoryNVS:
		case EfiPersistentMemory:
		case EfiReservedMemoryType:
		case EfiRuntimeServicesCode:
		case EfiRuntimeServicesData:
		case EfiUnusableMemory:
		case EfiMemoryMappedIO:
		case EfiMemoryMappedIOPortSpace:
		case EfiPalCode:
		default:
			do_compact = false;
			break;
		}

		if (first) {
			first = false;
		} else if (do_compact &&
		    type == target->Type &&
		    md->Attribute == target->Attribute &&
		    md->PhysicalStart == target->PhysicalStart + target->NumberOfPages * EFI_PAGE_SIZE) {
			/* continuous region */
			target->NumberOfPages += md->NumberOfPages;

			tmp = md;
			for (j = i + 1; j < *NoEntries; j++) {
				next = NextMemoryDescriptor(md, DescriptorSize);
				CopyMem(md, next, DescriptorSize);
				md = next;
			}
			next = tmp;

			i--;
			(*NoEntries)--;
			continue;
		} else {
			target = md;
		}

		target->Type = type;
		next = NextMemoryDescriptor(md, DescriptorSize);
	}

	return desc;
}

int
efi_memory_get_memmap(struct bi_memmap_entry **memmapp, size_t *num)
{
	EFI_STATUS status;
	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
	UINTN i, NoEntries, MapKey, DescriptorSize;
	UINT32 DescriptorVersion;
	UINTN cols, rows;
	struct bi_memmap_entry *memmap;

	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
	    ST->ConOut->Mode->Mode, &cols, &rows);
	if (EFI_ERROR(status) || rows <= 2)
		return -1;

	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
	    &DescriptorVersion, true);
	efi_memory_compact_map(mdtop, &NoEntries, DescriptorSize);

	memmap = alloc(sizeof(*memmap) * NoEntries);

	for (i = 0, md = mdtop; i < NoEntries; i++, md = next) {
		memmap[i].addr = md->PhysicalStart;
		memmap[i].size = md->NumberOfPages * EFI_PAGE_SIZE;
		memmap[i].type = getmemtype(md);

		next = NextMemoryDescriptor(md, DescriptorSize);
	}

	*memmapp = memmap;
	*num = NoEntries;
	return 0;
}

/*
 * get memory size below 1MB
 */
int
getbasemem(void)
{
	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
	UINTN i, NoEntries, MapKey, DescriptorSize, MappingSize;
	UINT32 DescriptorVersion;
	EFI_PHYSICAL_ADDRESS basemem = 0, epa;

	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
	    &DescriptorVersion, true);

	for (i = 0, md = mdtop; i < NoEntries; i++, md = next) {
		next = NextMemoryDescriptor(md, DescriptorSize);
		if (getmemtype(md) != BIM_Memory)
			continue;
		if (md->PhysicalStart >= 1 * 1024 * 1024)
			continue;
		if (basemem != md->PhysicalStart)
			continue;

		MappingSize = md->NumberOfPages * EFI_PAGE_SIZE;
		epa = md->PhysicalStart + MappingSize;
		if (epa == 0 || epa > 1 * 1024 * 1024)
			epa = 1 * 1024 * 1024;
		basemem = epa;
	}

	FreePool(mdtop);

	return basemem / 1024;	/* KiB */
}

/*
 * get memory size above 1MB below 4GB
 */
int
getextmemx(void)
{
	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
	UINTN i, NoEntries, MapKey, DescriptorSize, MappingSize;
	UINT32 DescriptorVersion;
	EFI_PHYSICAL_ADDRESS extmem16m = 0;	/* 0-16MB */
	EFI_PHYSICAL_ADDRESS extmem4g = 0;	/* 16MB-4GB */
	EFI_PHYSICAL_ADDRESS pa, epa;
	bool first16m = true, first4g = true;
	int extmem;

	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
	    &DescriptorVersion, true);

	for (i = 0, md = mdtop; i < NoEntries; i++, md = next) {
		next = NextMemoryDescriptor(md, DescriptorSize);
		if (getmemtype(md) == BIM_Reserved)
			continue;
		if (md->PhysicalStart >= 4 * 1024 * 1024 * 1024ULL)
			continue;

		MappingSize = md->NumberOfPages * EFI_PAGE_SIZE;
		epa = md->PhysicalStart + MappingSize;
		if (epa == 0 || epa > 4 * 1024 * 1024 * 1024LL)
			epa = 4 * 1024 * 1024 * 1024LL;

		if (epa <= 1 * 1024 * 1024)
			continue;

		pa = md->PhysicalStart;
		if (pa < 16 * 1024 * 1024) {
			if (first16m || extmem16m == pa) {
				first16m = false;
				if (epa >= 16 * 1024 * 1024) {
					extmem16m = 16 * 1024 * 1024;
					pa = 16 * 1024 * 1024;
				} else
					extmem16m = epa;
			}
		}
		if (pa >= 16 * 1024 * 1024) {
			if (first4g || extmem4g == pa) {
				first4g = false;
				extmem4g = epa;
			}
		}
	}

	FreePool(mdtop);

	if (extmem16m > 1 * 1024 * 1024)
		extmem16m -= 1 * 1024 * 1024;	/* below 1MB */

	extmem = extmem16m / 1024;
	if (extmem == 15 * 1024)
		extmem += extmem4g / 1024;
	return extmem;
}

void
efi_memory_probe(void)
{
	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
	EFI_STATUS status;
	EFI_PHYSICAL_ADDRESS bouncebuf;
	UINTN i, n, NoEntries, MapKey, DescriptorSize, MappingSize;
	UINT32 DescriptorVersion;
	int memtype;

	bouncebuf = EFI_ALLOCATE_MAX_ADDRESS;
	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
	    EfiLoaderData, EFI_SIZE_TO_PAGES(KERN_LOADSPACE_SIZE), &bouncebuf);
	if (EFI_ERROR(status))
		panic("couldn't allocate kernel space.");
	efi_loadaddr = bouncebuf;

	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
	    &DescriptorVersion, false);
	printf(" mem[");
	for (i = 0, n = 0, md = mdtop; i < NoEntries; i++, md = next) {
		next = NextMemoryDescriptor(md, DescriptorSize);

		memtype = getmemtype(md);
		if (memtype != BIM_Memory)
			continue;

		MappingSize = md->NumberOfPages * EFI_PAGE_SIZE;
		if (MappingSize < 12 * 1024)	/* XXX Why? from OpenBSD */
			continue;

		if (n++ > 0)
			printf(" ");
		printf("0x%" PRIxMAX "-0x%" PRIxMAX, (uintmax_t)md->PhysicalStart,
		    (uintmax_t)(md->PhysicalStart + MappingSize - 1));
	}
	printf("]\n");

	FreePool(mdtop);
}

void
efi_memory_show_map(bool sorted, bool compact)
{
	EFI_STATUS status;
	EFI_MEMORY_DESCRIPTOR *mdtop, *md, *next;
	UINTN i, NoEntries, MapKey, DescriptorSize;
	UINT32 DescriptorVersion;
	char efimemstr[32];
	UINTN cols, rows, row;

	status = uefi_call_wrapper(ST->ConOut->QueryMode, 4, ST->ConOut,
	    ST->ConOut->Mode->Mode, &cols, &rows);
	if (EFI_ERROR(status) || rows <= 2)
		rows = 0;
	else
		rows -= 2;

	mdtop = efi_memory_get_map(&NoEntries, &MapKey, &DescriptorSize,
	    &DescriptorVersion, sorted);
	if (compact)
		efi_memory_compact_map(mdtop, &NoEntries, DescriptorSize);

	printf("%-22s  %-16s  %-16s  %-16s\n", "Type", "Start", "End", "Attributes");
	printf("----------------------  ----------------  ----------------  ----------------\n");
	row = 2;

	for (i = 0, md = mdtop; i < NoEntries; i++, md = next) {
		next = NextMemoryDescriptor(md, DescriptorSize);

		if (md->Type >= __arraycount(efi_memory_type))
			snprintf(efimemstr, sizeof(efimemstr), "unknown (%d)",
			    md->Type);
		printf("%-22s  %016" PRIxMAX "  %016" PRIxMAX "  %016" PRIxMAX "\n",
		    md->Type >= __arraycount(efi_memory_type) ?
		      efimemstr : efi_memory_type[md->Type],
		    (uintmax_t)md->PhysicalStart,
		    (uintmax_t)md->PhysicalStart +
		      md->NumberOfPages * EFI_PAGE_SIZE - 1,
		    (uintmax_t)md->Attribute);

		if (++row >= rows) {
			row = 0;
			printf("Press Any Key to continue :");
			(void) awaitkey(-1, 0);
			printf("\n");
		}
	}

	FreePool(mdtop);
}

void
vpbcopy(const void *va, void *pa, size_t n)
{
	memmove(pa, va, n);
}

void
pvbcopy(const void *pa, void *va, size_t n)
{
	memmove(va, pa, n);
}

void
pbzero(void *pa, size_t n)
{
	memset(pa, 0, n);
}

physaddr_t
vtophys(void *va)
{
	return (physaddr_t)va;
}