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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/* $NetBSD: efiblock.c,v 1.5.6.2 2019/09/28 07:24:29 martin Exp $ */

/*-
 * Copyright (c) 2016 Kimihiro Nonaka <nonaka@netbsd.org>
 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
 * 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.
 */

#define FSTYPENAMES

#include <sys/param.h>
#include <sys/md5.h>
#include <sys/uuid.h>

#include "efiboot.h"
#include "efiblock.h"

static EFI_HANDLE *efi_block;
static UINTN efi_nblock;
static struct efi_block_part *efi_block_booted = NULL;

static TAILQ_HEAD(, efi_block_dev) efi_block_devs = TAILQ_HEAD_INITIALIZER(efi_block_devs);

static int
efi_block_parse(const char *fname, struct efi_block_part **pbpart, char **pfile)
{
	struct efi_block_dev *bdev;
	struct efi_block_part *bpart;
	char pathbuf[PATH_MAX], *default_device, *ep = NULL;
	const char *full_path;
	intmax_t dev;
	int part;

	default_device = get_default_device();
	if (strchr(fname, ':') == NULL) {
		if (strlen(default_device) > 0) {
			snprintf(pathbuf, sizeof(pathbuf), "%s:%s", default_device, fname);
			full_path = pathbuf;
			*pfile = __UNCONST(fname);
		} else {
			return EINVAL;
		}
	} else {
		full_path = fname;
		*pfile = strchr(fname, ':') + 1;
	}

	if (strncasecmp(full_path, "hd", 2) != 0)
		return EINVAL;
	dev = strtoimax(full_path + 2, &ep, 10);
	if (dev < 0 || dev >= efi_nblock)
		return ENXIO;
	if (ep[0] < 'a' || ep[0] >= 'a' + MAXPARTITIONS || ep[1] != ':')
		return EINVAL;
	part = ep[0] - 'a';
	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
		if (bdev->index == dev) {
			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
				if (bpart->index == part) {
					*pbpart = bpart;
					return 0;
				}
			}
		}
	}

	return ENOENT;
}

static void
efi_block_generate_hash_mbr(struct efi_block_part *bpart, struct mbr_sector *mbr)
{
	MD5_CTX md5ctx;

	MD5Init(&md5ctx);
	MD5Update(&md5ctx, (void *)mbr, sizeof(*mbr));
	MD5Final(bpart->hash, &md5ctx);
}

static void *
efi_block_allocate_device_buffer(struct efi_block_dev *bdev, UINTN size,
	void **buf_start)
{
	void *buf;

	if (bdev->bio->Media->IoAlign <= 1)
		*buf_start = buf = AllocatePool(size);
	else {
		buf = AllocatePool(size + bdev->bio->Media->IoAlign - 1);
		*buf_start = (buf == NULL) ? NULL :
		    (void *)roundup2((intptr_t)buf, bdev->bio->Media->IoAlign);
	}

	return buf;
}

static int
efi_block_find_partitions_disklabel(struct efi_block_dev *bdev, struct mbr_sector *mbr, uint32_t start, uint32_t size)
{
	struct efi_block_part *bpart;
	struct disklabel d;
	struct partition *p;
	EFI_STATUS status;
	EFI_LBA lba;
	void *buf, *buf_start;
	UINT32 sz;
	int n;

	sz = __MAX(sizeof(d), bdev->bio->Media->BlockSize);
	sz = roundup(sz, bdev->bio->Media->BlockSize);
	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
		return ENOMEM;

	lba = (((EFI_LBA)start + LABELSECTOR) * DEV_BSIZE) / bdev->bio->Media->BlockSize;
	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
		lba, sz, buf_start);
	if (EFI_ERROR(status) || getdisklabel(buf_start, &d) != NULL) {
		FreePool(buf);
		return EIO;
	}
	FreePool(buf);

	if (le32toh(d.d_magic) != DISKMAGIC || le32toh(d.d_magic2) != DISKMAGIC)
		return EINVAL;
	if (le16toh(d.d_npartitions) > MAXPARTITIONS)
		return EINVAL;

	for (n = 0; n < le16toh(d.d_npartitions); n++) {
		p = &d.d_partitions[n];
		switch (p->p_fstype) {
		case FS_BSDFFS:
		case FS_MSDOS:
		case FS_BSDLFS:
			break;
		default:
			continue;
		}

		bpart = alloc(sizeof(*bpart));
		bpart->index = n;
		bpart->bdev = bdev;
		bpart->type = EFI_BLOCK_PART_DISKLABEL;
		bpart->disklabel.secsize = le32toh(d.d_secsize);
		bpart->disklabel.part = *p;
		efi_block_generate_hash_mbr(bpart, mbr);
		TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);
	}

	return 0;
}

static int
efi_block_find_partitions_mbr(struct efi_block_dev *bdev)
{
	struct mbr_sector mbr;
	struct mbr_partition *mbr_part;
	EFI_STATUS status;
	void *buf, *buf_start;
	UINT32 sz;
	int n;

	sz = __MAX(sizeof(mbr), bdev->bio->Media->BlockSize);
	sz = roundup(sz, bdev->bio->Media->BlockSize);
	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
		return ENOMEM;

	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
		0, sz, buf_start);
	if (EFI_ERROR(status)) {
		FreePool(buf);
		return EIO;
	}
	memcpy(&mbr, buf_start, sizeof(mbr));
	FreePool(buf);

	if (le32toh(mbr.mbr_magic) != MBR_MAGIC)
		return ENOENT;

	for (n = 0; n < MBR_PART_COUNT; n++) {
		mbr_part = &mbr.mbr_parts[n];
		if (le32toh(mbr_part->mbrp_size) == 0)
			continue;
		if (mbr_part->mbrp_type == MBR_PTYPE_NETBSD) {
			efi_block_find_partitions_disklabel(bdev, &mbr, le32toh(mbr_part->mbrp_start), le32toh(mbr_part->mbrp_size));
			break;
		}
	}

	return 0;
}

static const struct {
	struct uuid guid;
	uint8_t fstype;
} gpt_guid_to_str[] = {
	{ GPT_ENT_TYPE_NETBSD_FFS,		FS_BSDFFS },
	{ GPT_ENT_TYPE_NETBSD_LFS,		FS_BSDLFS },
	{ GPT_ENT_TYPE_NETBSD_RAIDFRAME,	FS_RAID },
	{ GPT_ENT_TYPE_NETBSD_CCD,		FS_CCD },
	{ GPT_ENT_TYPE_NETBSD_CGD,		FS_CGD },
	{ GPT_ENT_TYPE_MS_BASIC_DATA,		FS_MSDOS },	/* or NTFS? ambiguous */
};

static int
efi_block_find_partitions_gpt_entry(struct efi_block_dev *bdev, struct gpt_hdr *hdr, struct gpt_ent *ent, UINT32 index)
{
	struct efi_block_part *bpart;
	uint8_t fstype = FS_UNUSED;
	struct uuid uuid;
	int n;

	memcpy(&uuid, ent->ent_type, sizeof(uuid));
	for (n = 0; n < __arraycount(gpt_guid_to_str); n++)
		if (memcmp(ent->ent_type, &gpt_guid_to_str[n].guid, sizeof(ent->ent_type)) == 0) {
			fstype = gpt_guid_to_str[n].fstype;
			break;
		}
	if (fstype == FS_UNUSED)
		return 0;

	bpart = alloc(sizeof(*bpart));
	bpart->index = index;
	bpart->bdev = bdev;
	bpart->type = EFI_BLOCK_PART_GPT;
	bpart->gpt.fstype = fstype;
	bpart->gpt.ent = *ent;
	memcpy(bpart->hash, ent->ent_guid, sizeof(bpart->hash));
	TAILQ_INSERT_TAIL(&bdev->partitions, bpart, entries);

	return 0;
}

static int
efi_block_find_partitions_gpt(struct efi_block_dev *bdev)
{
	struct gpt_hdr hdr;
	struct gpt_ent ent;
	EFI_STATUS status;
	void *buf, *buf_start;
	UINT32 sz, entry;

	sz = __MAX(sizeof(hdr), bdev->bio->Media->BlockSize);
	sz = roundup(sz, bdev->bio->Media->BlockSize);
	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
		return ENOMEM;

	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
		GPT_HDR_BLKNO, sz, buf_start);
	if (EFI_ERROR(status)) {
		FreePool(buf);
		return EIO;
	}
	memcpy(&hdr, buf_start, sizeof(hdr));
	FreePool(buf);

	if (memcmp(hdr.hdr_sig, GPT_HDR_SIG, sizeof(hdr.hdr_sig)) != 0)
		return ENOENT;
	if (le32toh(hdr.hdr_entsz) < sizeof(ent))
		return EINVAL;

	sz = __MAX(le32toh(hdr.hdr_entsz) * le32toh(hdr.hdr_entries), bdev->bio->Media->BlockSize);
	sz = roundup(sz, bdev->bio->Media->BlockSize);
	if ((buf = efi_block_allocate_device_buffer(bdev, sz, &buf_start)) == NULL)
		return ENOMEM;

	status = uefi_call_wrapper(bdev->bio->ReadBlocks, 5, bdev->bio, bdev->media_id,
		le64toh(hdr.hdr_lba_table), sz, buf_start);
	if (EFI_ERROR(status)) {
		FreePool(buf);
		return EIO;
	}

	for (entry = 0; entry < le32toh(hdr.hdr_entries); entry++) {
		memcpy(&ent, buf_start + (entry * le32toh(hdr.hdr_entsz)),
			sizeof(ent));
		efi_block_find_partitions_gpt_entry(bdev, &hdr, &ent, entry);
	}

	FreePool(buf);

	return 0;
}

static int
efi_block_find_partitions(struct efi_block_dev *bdev)
{
	int error;

	error = efi_block_find_partitions_gpt(bdev);
	if (error)
		error = efi_block_find_partitions_mbr(bdev);

	return error;
}

void
efi_block_probe(void)
{
	struct efi_block_dev *bdev;
	struct efi_block_part *bpart;
	EFI_BLOCK_IO *bio;
	EFI_STATUS status;
	uint16_t devindex = 0;
	int depth = -1;
	int n;

	status = LibLocateHandle(ByProtocol, &BlockIoProtocol, NULL, &efi_nblock, &efi_block);
	if (EFI_ERROR(status))
		return;

	if (efi_bootdp) {
		depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
		if (depth == 0)
			depth = 1;
		else if (depth == -1)
			depth = 2;
	}

	for (n = 0; n < efi_nblock; n++) {
		status = uefi_call_wrapper(BS->HandleProtocol, 3, efi_block[n], &BlockIoProtocol, (void **)&bio);
		if (EFI_ERROR(status) || !bio->Media->MediaPresent)
			continue;

		if (bio->Media->LogicalPartition)
			continue;

		bdev = alloc(sizeof(*bdev));
		bdev->index = devindex++;
		bdev->bio = bio;
		bdev->media_id = bio->Media->MediaId;
		bdev->path = DevicePathFromHandle(efi_block[n]);
		TAILQ_INIT(&bdev->partitions);
		TAILQ_INSERT_TAIL(&efi_block_devs, bdev, entries);

		efi_block_find_partitions(bdev);

		if (depth > 0 && efi_device_path_ncmp(efi_bootdp, DevicePathFromHandle(efi_block[n]), depth) == 0) {
			TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
				uint8_t fstype = FS_UNUSED;
				switch (bpart->type) {
				case EFI_BLOCK_PART_DISKLABEL:
					fstype = bpart->disklabel.part.p_fstype;
					break;
				case EFI_BLOCK_PART_GPT:
					fstype = bpart->gpt.fstype;
					break;
				}
				if (fstype == FS_BSDFFS) {
					char devname[9];
					snprintf(devname, sizeof(devname), "hd%u%c", bdev->index, bpart->index + 'a');
					set_default_device(devname);
					break;
				}
			}
		}
	}
}

static void
print_guid(const uint8_t *guid)
{
	const int index[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
	int i;

	for (i = 0; i < 16; i++) {
		printf("%02x", guid[index[i]]);
		if (i == 3 || i == 5 || i == 7 || i == 9)
			printf("-");
	}
}

void
efi_block_show(void)
{
	struct efi_block_dev *bdev;
	struct efi_block_part *bpart;
	uint64_t size;
	CHAR16 *path;

	TAILQ_FOREACH(bdev, &efi_block_devs, entries) {
		printf("hd%u (", bdev->index);

		/* Size in MB */
		size = ((bdev->bio->Media->LastBlock + 1) * bdev->bio->Media->BlockSize) / (1024 * 1024);
		if (size >= 10000)
			printf("%"PRIu64" GB", size / 1024);
		else
			printf("%"PRIu64" MB", size);
		printf("): ");

		path = DevicePathToStr(bdev->path);
		Print(L"%s", path);
		FreePool(path);

		printf("\n");

		TAILQ_FOREACH(bpart, &bdev->partitions, entries) {
			switch (bpart->type) {
			case EFI_BLOCK_PART_DISKLABEL:
				printf("  hd%u%c (", bdev->index, bpart->index + 'a');

				/* Size in MB */
				size = ((uint64_t)bpart->disklabel.secsize * bpart->disklabel.part.p_size) / (1024 * 1024);
				if (size >= 10000)
					printf("%"PRIu64" GB", size / 1024);
				else
					printf("%"PRIu64" MB", size);
				printf("): ");

				printf("%s\n", fstypenames[bpart->disklabel.part.p_fstype]);
				break;
			case EFI_BLOCK_PART_GPT:
				printf("  hd%u%c ", bdev->index, bpart->index + 'a');

				if (bpart->gpt.ent.ent_name[0] == 0x0000) {
					printf("\"");
					print_guid(bpart->gpt.ent.ent_guid);
					printf("\"");
				} else {
					Print(L"\"%s\"", bpart->gpt.ent.ent_name);
				}
			
				/* Size in MB */
				size = (le64toh(bpart->gpt.ent.ent_lba_end) - le64toh(bpart->gpt.ent.ent_lba_start)) * bdev->bio->Media->BlockSize;
				size /= (1024 * 1024);
				if (size >= 10000)
					printf(" (%"PRIu64" GB): ", size / 1024);
				else
					printf(" (%"PRIu64" MB): ", size);

				printf("%s\n", fstypenames[bpart->gpt.fstype]);
				break;
			default:
				break;
			}
		}
	}
}

struct efi_block_part *
efi_block_boot_part(void)
{
	return efi_block_booted;
}

int
efi_block_open(struct open_file *f, ...)
{
	struct efi_block_part *bpart;
	const char *fname;
	char **file;
	char *path;
	va_list ap;
	int rv, n;
	
	va_start(ap, f);
	fname = va_arg(ap, const char *);
	file = va_arg(ap, char **);
	va_end(ap);

	rv = efi_block_parse(fname, &bpart, &path);
	if (rv != 0)
		return rv;

	for (n = 0; n < ndevs; n++)
		if (strcmp(DEV_NAME(&devsw[n]), "efiblock") == 0) {
			f->f_dev = &devsw[n];
			break;
		}
	if (n == ndevs)
		return ENXIO;

	f->f_devdata = bpart;

	*file = path;

	efi_block_booted = bpart;

	return 0;
}

int
efi_block_close(struct open_file *f)
{
	return 0;
}

int
efi_block_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
{
	struct efi_block_part *bpart = devdata;
	EFI_STATUS status;
	void *allocated_buf, *aligned_buf;

	if (rw != F_READ)
		return EROFS;

	switch (bpart->type) {
	case EFI_BLOCK_PART_DISKLABEL:
		if (bpart->bdev->bio->Media->BlockSize != bpart->disklabel.secsize) {
			printf("%s: unsupported block size %d (expected %d)\n", __func__,
			    bpart->bdev->bio->Media->BlockSize, bpart->disklabel.secsize);
			return EIO;
		}
		dblk += bpart->disklabel.part.p_offset;
		break;
	case EFI_BLOCK_PART_GPT:
		if (bpart->bdev->bio->Media->BlockSize != DEV_BSIZE) {
			printf("%s: unsupported block size %d (expected %d)\n", __func__,
			    bpart->bdev->bio->Media->BlockSize, DEV_BSIZE);
			return EIO;
		}
		dblk += le64toh(bpart->gpt.ent.ent_lba_start);
		break;
	default:
		return EINVAL;
	}

	if ((bpart->bdev->bio->Media->IoAlign <= 1) ||
		((intptr_t)buf & (bpart->bdev->bio->Media->IoAlign - 1)) == 0) {
		allocated_buf = NULL;
		aligned_buf = buf;
	} else if ((allocated_buf = efi_block_allocate_device_buffer(bpart->bdev,
		size, &aligned_buf)) == NULL)
		return ENOMEM;

	status = uefi_call_wrapper(bpart->bdev->bio->ReadBlocks, 5,
		bpart->bdev->bio, bpart->bdev->media_id, dblk, size, aligned_buf);
	if (EFI_ERROR(status)) {
		if (allocated_buf != NULL)
			FreePool(allocated_buf);
		return EIO;
	}
	if (allocated_buf != NULL) {
		memcpy(buf, aligned_buf, size);
		FreePool(allocated_buf);
	}

	*rsize = size;

	return 0;
}