Skip to content

Commit

Permalink
fallback: Use a dynamic buffer when list var names
Browse files Browse the repository at this point in the history
Now that we are iterating over all EFI variables to look for duplicate
boot entries, we should use a dynamic buffer, so if the firmware tells
us the buffer is too small for the next variable name we can re-allocate
it to a big enough size.

Signed-off-by: João Paulo Rechi Vita <jprvita@endlessos.org>
  • Loading branch information
jprvita committed Apr 12, 2021
1 parent f428985 commit f5e1d7f
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions fallback.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,12 @@ find_boot_option(EFI_DEVICE_PATH *dp, EFI_DEVICE_PATH *fulldp,
cursor += DevicePathSize(dp);
StrCpy((CHAR16 *)cursor, arguments);

CHAR16 varname[256];
EFI_STATUS efi_status;
EFI_GUID vendor_guid = NullGuid;
UINTN buffer_size = 256 * sizeof(CHAR16);
CHAR16 *varname = AllocateZeroPool(buffer_size);
if (!varname)
return EFI_OUT_OF_RESOURCES;

UINTN max_candidate_size = calc_masked_boot_option_size(size);
CHAR8 *candidate = AllocateZeroPool(max_candidate_size);
Expand All @@ -426,15 +429,24 @@ find_boot_option(EFI_DEVICE_PATH *dp, EFI_DEVICE_PATH *fulldp,
return EFI_OUT_OF_RESOURCES;
}

varname[0] = 0;
while (1) {
UINTN varname_size = sizeof(varname);
UINTN varname_size = buffer_size;
efi_status = gRT->GetNextVariableName(&varname_size, varname,
&vendor_guid);
if (EFI_ERROR(efi_status)) {
if (efi_status == EFI_BUFFER_TOO_SMALL)
if (efi_status == EFI_BUFFER_TOO_SMALL) {
VerbosePrint(L"Buffer too small for next var "
L"name\n");
L"name, re-allocating it to be %d"
L" chars long and retrying\n",
varname_size);
varname = ReallocatePool(varname,
buffer_size,
varname_size);
if (!varname)
return EFI_OUT_OF_RESOURCES;
buffer_size = varname_size;
continue;
}

if (efi_status == EFI_DEVICE_ERROR)
VerbosePrint(L"The next variable name could "
Expand Down Expand Up @@ -487,6 +499,7 @@ find_boot_option(EFI_DEVICE_PATH *dp, EFI_DEVICE_PATH *fulldp,
}
FreePool(candidate);
FreePool(data);
FreePool(varname);
return efi_status;
}

Expand Down

0 comments on commit f5e1d7f

Please sign in to comment.