summaryrefslogtreecommitdiff
path: root/sys/stand/efiboot/efipxe.c
blob: 3c0355358085cfafc203442428bb6d7f177d5273 (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
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
/*	$NetBSD: efipxe.c,v 1.2 2018/11/15 23:52:33 jmcneill Exp $	*/
/*	$OpenBSD: efipxe.c,v 1.3 2018/01/30 20:19:06 naddy Exp $	*/

/*
 * Copyright (c) 2017 Patrick Wildt <patrick@blueri.se>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/queue.h>

#include "efiboot.h"

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <lib/libsa/bootp.h>	/* for VM_RFC1048 */


struct efipxeinfo {
	TAILQ_ENTRY(efipxeinfo)	list;

	EFI_PXE_BASE_CODE	*pxe;
	EFI_SIMPLE_NETWORK	*net;
	EFI_MAC_ADDRESS		mac;
	UINT32			addrsz;
};
TAILQ_HEAD(efipxeinfo_lh, efipxeinfo);
static struct efipxeinfo_lh efi_pxelist;
static int nefipxes;

void
efi_pxe_probe(void)
{
	struct efipxeinfo *epi;
	EFI_PXE_BASE_CODE *pxe;
	EFI_DEVICE_PATH *dp;
	EFI_SIMPLE_NETWORK *net;
	EFI_HANDLE *handles;
	EFI_STATUS status;
	UINTN nhandles;
	int i, depth;
	bool found;

	TAILQ_INIT(&efi_pxelist);

        status = LibLocateHandle(ByProtocol, &PxeBaseCodeProtocol, NULL,
	    &nhandles, &handles);
	if (EFI_ERROR(status))
		return;

	for (i = 0; i < nhandles; i++) {
		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
		    &DevicePathProtocol, (void **)&dp);
		if (EFI_ERROR(status))
			continue;

		depth = efi_device_path_depth(efi_bootdp, MEDIA_DEVICE_PATH);
		if (efi_device_path_ncmp(efi_bootdp, dp, depth))
			continue;

		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
		    &PxeBaseCodeProtocol, (void **)&pxe);
		if (EFI_ERROR(status))
			continue;

		if (pxe->Mode == NULL ||
		    (!pxe->Mode->DhcpAckReceived && !pxe->Mode->PxeReplyReceived))
			continue;

		status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[i],
		    &SimpleNetworkProtocol, (void **)&net);
		if (EFI_ERROR(status))
			continue;

		if (net->Mode == NULL)
			continue;

		found = false;
		TAILQ_FOREACH(epi, &efi_pxelist, list) {
			if (net->Mode->HwAddressSize == epi->addrsz &&
			    memcmp(net->Mode->CurrentAddress.Addr, epi->mac.Addr,
			      net->Mode->HwAddressSize) == 0) {
				found = true;
				break;
			}
		}
		if (found)
			continue;

		epi = alloc(sizeof(*epi));
		if (epi == NULL)
			continue;

		memset(epi, 0, sizeof(*epi));
		epi->pxe = pxe;
		epi->net = net;
		epi->addrsz = net->Mode->HwAddressSize;
		memcpy(epi->mac.Addr, net->Mode->CurrentAddress.Addr, epi->addrsz);

		TAILQ_INSERT_TAIL(&efi_pxelist, epi, list);
		nefipxes++;
	}
}

bool
efi_pxe_match_booted_interface(const EFI_MAC_ADDRESS *mac, UINT32 addrsz)
{
	const struct efipxeinfo *epi;

	TAILQ_FOREACH(epi, &efi_pxelist, list) {
		if (addrsz == epi->addrsz &&
		    memcmp(mac->Addr, epi->mac.Addr, addrsz) == 0)
			return true;
	}
	return false;
}