mk/efi.mk: Add libefi.a dependency
[syslinux.git] / efi / pxe.c
blob62fddb0811ff09fdbd2990b2bfbf31aa52c579e7
1 /*
2 * Copyright 2013-2014 Intel Corporation - All Rights Reserved
3 */
5 #include <syslinux/firmware.h>
6 #include <syslinux/pxe_api.h>
7 #include "efi.h"
8 #include "net.h"
9 #include "fs/pxe/pxe.h"
11 const struct url_scheme url_schemes[] = {
12 { "tftp", tftp_open, 0 },
13 { "http", http_open, O_DIRECTORY },
14 { "ftp", ftp_open, O_DIRECTORY },
15 { NULL, NULL, 0 },
18 /**
19 * Network stack-specific initialization
21 void net_core_init(void)
23 http_bake_cookies();
26 void pxe_init_isr(void) {}
27 void gpxe_init(void) {}
28 void pxe_idle_init(void) {}
30 int reset_pxe(void)
32 return 0;
35 #define DNS_MAX_SERVERS 4 /* Max no of DNS servers */
36 uint32_t dns_server[DNS_MAX_SERVERS] = {0, };
38 __export uint32_t dns_resolv(const char *name)
41 * Return failure on an empty input... this can happen during
42 * some types of URL parsing, and this is the easiest place to
43 * check for it.
45 if (!name || !*name)
46 return 0;
48 return 0;
51 int pxe_init(bool quiet)
53 EFI_HANDLE *handles;
54 EFI_STATUS status;
55 UINTN nr_handles;
57 status = LibLocateHandle(ByProtocol, &PxeBaseCodeProtocol,
58 NULL, &nr_handles, &handles);
59 if (status != EFI_SUCCESS) {
60 if (!quiet)
61 Print(L"No PXE Base Code Protocol\n");
62 return -1;
65 return 0;
68 void net_parse_dhcp(void)
70 EFI_PXE_BASE_CODE_MODE *mode;
71 EFI_PXE_BASE_CODE *bc;
72 unsigned int pkt_len = sizeof(EFI_PXE_BASE_CODE_PACKET);
73 EFI_STATUS status;
74 EFI_HANDLE *handles = NULL;
75 UINTN nr_handles = 0;
76 uint8_t hardlen;
77 uint32_t ip;
78 char dst[256];
80 status = LibLocateHandle(ByProtocol, &PxeBaseCodeProtocol,
81 NULL, &nr_handles, &handles);
82 if (status != EFI_SUCCESS)
83 return;
85 /* Probably want to use IPv4 protocol to decide which handle to use */
86 status = uefi_call_wrapper(BS->HandleProtocol, 3, handles[0],
87 &PxeBaseCodeProtocol, (void **)&bc);
88 if (status != EFI_SUCCESS) {
89 Print(L"Failed to lookup PxeBaseCodeProtocol\n");
92 mode = bc->Mode;
95 * Get the DHCP client identifiers (query info 1)
97 Print(L"Getting cached packet ");
98 parse_dhcp(&mode->DhcpDiscover.Dhcpv4, pkt_len);
100 * We don't use flags from the request packet, so
101 * this is a good time to initialize DHCPMagic...
102 * Initialize it to 1 meaning we will accept options found;
103 * in earlier versions of PXELINUX bit 0 was used to indicate
104 * we have found option 208 with the appropriate magic number;
105 * we no longer require that, but MAY want to re-introduce
106 * it in the future for vendor encapsulated options.
108 *(char *)&DHCPMagic = 1;
111 * Get the BOOTP/DHCP packet that brought us file (and an IP
112 * address). This lives in the DHCPACK packet (query info 2)
114 parse_dhcp(&mode->DhcpAck.Dhcpv4, pkt_len);
116 * Save away MAC address (assume this is in query info 2. If this
117 * turns out to be problematic it might be better getting it from
118 * the query info 1 packet
120 hardlen = mode->DhcpAck.Dhcpv4.BootpHwAddrLen;
121 MAC_len = hardlen > 16 ? 0 : hardlen;
122 MAC_type = mode->DhcpAck.Dhcpv4.BootpHwType;
123 memcpy(MAC, mode->DhcpAck.Dhcpv4.BootpHwAddr, MAC_len);
126 * Get the boot file and other info. This lives in the CACHED_REPLY
127 * packet (query info 3)
129 parse_dhcp(&mode->PxeReply.Dhcpv4, pkt_len);
130 Print(L"\n");
132 ip = IPInfo.myip;
133 sprintf(dst, "%u.%u.%u.%u",
134 ((const uint8_t *)&ip)[0],
135 ((const uint8_t *)&ip)[1],
136 ((const uint8_t *)&ip)[2],
137 ((const uint8_t *)&ip)[3]);
139 Print(L"My IP is %a\n", dst);