2 * netboot - trivial UEFI first-stage bootloader netboot support
4 * Copyright 2012 Red Hat, Inc <mjg@redhat.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Significant portions of this code are derived from Tianocore
32 * (http://tianocore.sf.net) and are Copyright 2009-2012 Intel
41 #define ntohs(x) __builtin_bswap16(x) /* supported both by GCC and clang */
42 #define htons(x) ntohs(x)
44 static EFI_PXE_BASE_CODE
*pxe
;
45 static EFI_IP_ADDRESS tftp_addr
;
46 static CHAR8
*full_path
;
53 } EFI_DHCP6_PACKET_OPTION
;
57 * Returns TRUE if we identify a protocol that is enabled and Providing us with
58 * the needed information to fetch a grubx64.efi image
60 BOOLEAN
findNetboot(EFI_HANDLE device
)
64 status
= uefi_call_wrapper(BS
->HandleProtocol
, 3, device
,
65 &PxeBaseCodeProtocol
, (VOID
**)&pxe
);
66 if (status
!= EFI_SUCCESS
) {
71 if (!pxe
|| !pxe
->Mode
) {
76 if (!pxe
->Mode
->Started
|| !pxe
->Mode
->DhcpAckReceived
) {
82 * We've located a pxe protocol handle thats been started and has
83 * received an ACK, meaning its something we'll be able to get
84 * tftp server info out of
89 static CHAR8
*get_v6_bootfile_url(EFI_PXE_BASE_CODE_DHCPV6_PACKET
*pkt
)
91 void *optr
= NULL
, *end
= NULL
;
92 EFI_DHCP6_PACKET_OPTION
*option
= NULL
;
96 optr
= pkt
->DhcpOptions
;
97 end
= optr
+ sizeof(pkt
->DhcpOptions
);
100 option
= (EFI_DHCP6_PACKET_OPTION
*)optr
;
102 if (ntohs(option
->OpCode
) == 0)
105 if (ntohs(option
->OpCode
) == 59) {
106 /* This is the bootfile url option */
107 urllen
= ntohs(option
->Length
);
108 if ((void *)(option
->Data
+ urllen
) > end
)
110 url
= AllocateZeroPool(urllen
+ 1);
113 memcpy(url
, option
->Data
, urllen
);
116 optr
+= 4 + ntohs(option
->Length
);
117 if (optr
+ sizeof(EFI_DHCP6_PACKET_OPTION
) > end
)
124 static CHAR16
str2ns(CHAR8
*str
)
129 if ('0' <= *str
&& *str
<= '9')
131 else if ('A' <= *str
&& *str
<= 'F')
133 else if ('a' <= *str
&& *str
<= 'f')
137 ret
= (ret
<< 4) + v
;
142 static CHAR8
*str2ip6(CHAR8
*str
)
144 UINT8 i
= 0, j
= 0, p
= 0;
145 size_t len
= 0, dotcount
= 0;
146 enum { MAX_IP6_DOTS
= 7 };
147 CHAR8
*a
= NULL
, *b
= NULL
, t
= 0;
150 memset(ip
, 0, sizeof(ip
));
152 /* Count amount of ':' to prevent overflows.
153 * max. count = 7. Returns an invalid ip6 that
154 * can be checked against
156 for (a
= str
; *a
!= 0; ++a
) {
160 if (dotcount
> MAX_IP6_DOTS
)
165 for (i
= p
= 0; i
< len
; i
++, b
++) {
176 for (j
= len
, p
= 7; j
> i
; j
--, a
--) {
181 ip
[p
--] = str2ns(a
+1);
188 static BOOLEAN
extract_tftp_info(CHAR8
*url
)
193 CHAR8
*template = (CHAR8
*)translate_slashes(DEFAULT_LOADER_CHAR
);
195 // to check against str2ip6() errors
196 memset(ip6inv
, 0, sizeof(ip6inv
));
198 if (strncmp((UINT8
*)url
, (UINT8
*)"tftp://", 7)) {
199 Print(L
"URLS MUST START WITH tftp://\n");
204 Print(L
"TFTP SERVER MUST BE ENCLOSED IN [..]\n");
210 while ((*end
!= '\0') && (*end
!= ']')) {
212 if (end
- start
>= (int)sizeof(ip6str
)) {
213 Print(L
"TFTP URL includes malformed IPv6 address\n");
218 Print(L
"TFTP SERVER MUST BE ENCLOSED IN [..]\n");
221 memset(ip6str
, 0, sizeof(ip6str
));
222 memcpy(ip6str
, start
, end
- start
);
224 memcpy(&tftp_addr
.v6
, str2ip6(ip6str
), 16);
225 if (memcmp(&tftp_addr
.v6
, ip6inv
, sizeof(ip6inv
)) == 0)
227 full_path
= AllocateZeroPool(strlen(end
)+strlen(template)+1);
230 memcpy(full_path
, end
, strlen(end
));
231 end
= (CHAR8
*)strrchr((char *)full_path
, '/');
233 end
= (CHAR8
*)full_path
;
234 memcpy(end
, template, strlen(template));
235 end
[strlen(template)] = '\0';
240 static EFI_STATUS
parseDhcp6()
242 EFI_PXE_BASE_CODE_DHCPV6_PACKET
*packet
= (EFI_PXE_BASE_CODE_DHCPV6_PACKET
*)&pxe
->Mode
->DhcpAck
.Raw
;
245 bootfile_url
= get_v6_bootfile_url(packet
);
247 return EFI_NOT_FOUND
;
248 if (extract_tftp_info(bootfile_url
) == FALSE
) {
249 FreePool(bootfile_url
);
250 return EFI_NOT_FOUND
;
252 FreePool(bootfile_url
);
256 static EFI_STATUS
parseDhcp4()
258 CHAR8
*template = (CHAR8
*)translate_slashes(DEFAULT_LOADER_CHAR
);
259 INTN template_len
= strlen(template) + 1;
261 INTN dir_len
= strnlena(pxe
->Mode
->DhcpAck
.Dhcpv4
.BootpBootFile
, 127);
263 UINT8
*dir
= pxe
->Mode
->DhcpAck
.Dhcpv4
.BootpBootFile
;
265 for (i
= dir_len
; i
>= 0; i
--) {
269 dir_len
= (i
>= 0) ? i
+ 1 : 0;
271 full_path
= AllocateZeroPool(dir_len
+ template_len
);
274 return EFI_OUT_OF_RESOURCES
;
277 strncpya(full_path
, dir
, dir_len
);
278 if (full_path
[dir_len
-1] == '/' && template[0] == '/')
279 full_path
[dir_len
-1] = '\0';
281 if (dir_len
== 0 && dir
[0] != '/' && template[0] == '/')
283 strcata(full_path
, template);
284 memcpy(&tftp_addr
.v4
, pxe
->Mode
->DhcpAck
.Dhcpv4
.BootpSiAddr
, 4);
289 EFI_STATUS
parseNetbootinfo(EFI_HANDLE image_handle
)
295 return EFI_NOT_READY
;
297 memset((UINT8
*)&tftp_addr
, 0, sizeof(tftp_addr
));
300 * If we've discovered an active pxe protocol figure out
301 * if its ipv4 or ipv6
303 if (pxe
->Mode
->UsingIpv6
){
310 EFI_STATUS
FetchNetbootimage(EFI_HANDLE image_handle
, VOID
**buffer
, UINT64
*bufsiz
)
313 EFI_PXE_BASE_CODE_TFTP_OPCODE read
= EFI_PXE_BASE_CODE_TFTP_READ_FILE
;
314 BOOLEAN overwrite
= FALSE
;
315 BOOLEAN nobuffer
= FALSE
;
318 Print(L
"Fetching Netboot Image\n");
319 if (*buffer
== NULL
) {
320 *buffer
= AllocatePool(4096 * 1024);
322 return EFI_OUT_OF_RESOURCES
;
323 *bufsiz
= 4096 * 1024;
327 rc
= uefi_call_wrapper(pxe
->Mtftp
, 10, pxe
, read
, *buffer
, overwrite
,
328 bufsiz
, &blksz
, &tftp_addr
, full_path
, NULL
, nobuffer
);
330 if (rc
== EFI_BUFFER_TOO_SMALL
) {
331 /* try again, doubling buf size */
334 *buffer
= AllocatePool(*bufsiz
);
336 return EFI_OUT_OF_RESOURCES
;
340 if (rc
!= EFI_SUCCESS
&& *buffer
) {