'UEFI function running wrong part in if else

I am writing a UEFI function which used to show status.

But when I give it a EFI_STATUS type "2" as "state" argument, it execute "else" part, instead of "state == 2" part.

I don't know which detail I missed.

EFI_STATUS ShowState(const CHAR16 * name, UINTN state) {
    if (state == 0) {
        Print(L"[ INFO ] %s success.\n", name);
    } else {
        CHAR16 * code;

        if (state == 1) {
            code = L"EFI_LOAD_ERROR";
        } else if (state == 2) {
            code = L"EFI_INVALID_PARAMETER";
        } else {
            code = L"Unknown";
        }
    
        if (code[0] == 'U') {
            Print(L"[ ERRO ] %s failed with [%d].\n", name, state);
        } else { // (code[0] == 'E')
            Print(L"[ ERRO ] %s failed with %s.\n", name, code);
        }
    }
    return EFI_SUCCESS;
}


Solution 1:[1]

I read UEFI spec again, and as prl said, EFI_STATUS is a hex value, so we have some resolution:

  • Use EFIERR(2) instead of 2
  • Directly use EFI_INVALID_PARAMETER marco

And when I use Print() to print the status value, it only prints decimal "2", that is the main reason which makes it confusing.

Edit: Thanks to MiSimon, now I know should use %r for print a status code.

Solution 2:[2]

There are 2 possible errors here - either the state you send is wrong, or the test you made is wrong. To test the state - add a print to show the state to test your test - just always print the code. so just add this line before the if:

Print(L"[ ERRO ] %s failed with [%d][%s].\n", name, state, code);

Also note that you're getting a UINTN for input, but talking about it as EFI_STATUS. an EFI_STATUS is a UINTN, but you need to make sure you passed the correct variable.

Or just post here the function the passes information to ShowState, plus the output you got

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2