Skip to content

Commit 60dbb3f

Browse files
committed
netboot: Convert file not found error to EFI_NOT_FOUND
This will result in falling back to the default second stage loader like disk based lookups. The error code is available in the TftpError field of the Mode interface. Unfortunately, some firmware doesn't fill in the error fields, so a generic EFI_TFTP_ERROR to EFI_NOT_FOUND conversion is included.
1 parent e064e7d commit 60dbb3f

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

netboot.c

+38-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
#define ntohs(x) __builtin_bswap16(x) /* supported both by GCC and clang */
1717
#define htons(x) ntohs(x)
1818

19+
/* TFTP error codes from RFC 1350 */
20+
#define TFTP_ERROR_NOT_DEFINED 0 /* Not defined, see error message (if any). */
21+
#define TFTP_ERROR_NOT_FOUND 1 /* File not found. */
22+
#define TFTP_ERROR_ACCESS 2 /* Access violation. */
23+
#define TFTP_ERROR_NO_SPACE 3 /* Disk full or allocation exceeded. */
24+
#define TFTP_ERROR_ILLEGAL_OP 4 /* Illegal TFTP operation. */
25+
#define TFTP_ERROR_UNKNOWN_ID 5 /* Unknown transfer ID. */
26+
#define TFTP_ERROR_EXISTS 6 /* File already exists. */
27+
#define TFTP_ERROR_NO_USER 7 /* No such user. */
28+
1929
static EFI_PXE_BASE_CODE *pxe;
2030
static EFI_IP_ADDRESS tftp_addr;
2131
static CHAR8 *full_path;
@@ -362,8 +372,34 @@ EFI_STATUS FetchNetbootimage(EFI_HANDLE image_handle UNUSED, VOID **buffer, UINT
362372
goto try_again;
363373
}
364374

365-
if (EFI_ERROR(efi_status) && *buffer) {
366-
FreePool(*buffer);
375+
if (EFI_ERROR(efi_status)) {
376+
if (pxe->Mode->TftpErrorReceived) {
377+
console_print(L"TFTP error %u: %a\n",
378+
pxe->Mode->TftpError.ErrorCode,
379+
pxe->Mode->TftpError.ErrorString);
380+
381+
switch (pxe->Mode->TftpError.ErrorCode) {
382+
case TFTP_ERROR_NOT_FOUND:
383+
efi_status = EFI_NOT_FOUND;
384+
break;
385+
default:
386+
break;
387+
}
388+
} else if (efi_status == EFI_TFTP_ERROR) {
389+
/*
390+
* Unfortunately, some firmware doesn't fill in the
391+
* error details. Treat all TFTP errors like file not
392+
* found so shim falls back to the default loader.
393+
*
394+
* https://github.com/tianocore/edk2/pull/6287
395+
*/
396+
console_print(L"Unknown TFTP error, treating as file not found\n");
397+
efi_status = EFI_NOT_FOUND;
398+
}
399+
400+
if (*buffer) {
401+
FreePool(*buffer);
402+
}
367403
}
368404
return efi_status;
369405
}

0 commit comments

Comments
 (0)