Correctly delete UEFI variables

UEFI runtime service GetVariable with DataSize NULL, will fail and get
EFI_INVALID_PARAMETER returned. Set DataSize 0 and allocate the buffer for
getting attributes for the deleted variable.

Also, fix the real reason Boot#### was never found.
This commit is contained in:
Ivan Hu 2020-01-17 15:50:28 +08:00 committed by Richard Hughes
parent 2a026147ce
commit 44f55e2ee6
2 changed files with 18 additions and 5 deletions

View File

@ -17,16 +17,27 @@ fwup_delete_variable(CHAR16 *name, EFI_GUID *guid)
{
EFI_STATUS rc;
UINT32 attrs = 0;
UINTN size = 0;
/* get the attrs so we can delete it */
rc = uefi_call_wrapper(RT->GetVariable, 5, name, guid, &attrs, NULL, NULL);
rc = uefi_call_wrapper(RT->GetVariable, 5, name, guid, &attrs, &size, NULL);
if (EFI_ERROR(rc)) {
if (rc == EFI_NOT_FOUND) {
if (rc == EFI_BUFFER_TOO_SMALL) {
_cleanup_free VOID *buf = fwup_malloc(size);
if (buf == NULL)
return EFI_OUT_OF_RESOURCES;
rc = uefi_call_wrapper(RT->GetVariable, 5, name, guid, &attrs, &size, buf);
if (EFI_ERROR(rc)) {
fwup_debug(L"Could not get attr: %r", rc);
return rc;
}
} else if (rc == EFI_NOT_FOUND) {
fwup_debug(L"Not deleting variable '%s' as not found", name);
return EFI_SUCCESS;
} else {
fwup_debug(L"Could not get variable '%s' for delete: %r", name, rc);
return rc;
}
fwup_debug(L"Could not get variable '%s' for delete: %r", name, rc);
return rc;
}
return uefi_call_wrapper(RT->SetVariable, 5, name, guid, attrs, 0, NULL);
}

View File

@ -408,7 +408,9 @@ fwup_delete_boot_entry(VOID)
/* check if the variable name is Boot#### */
if (CompareGuid(&vendor_guid, &global_variable_guid) != 0)
continue;
if (StrCmp(variable_name, L"Boot") != 0)
if (StrLen(variable_name) != 8)
continue;
if (StrnCmp(variable_name, L"Boot", 4) != 0)
continue;
UINTN info_size = 0;