fsvariable.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,279 行 · 第 1/3 页

C
1,279
字号

  //
  // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,
  // we return it.
  //
  if (InDeleteVariable != NULL) {
    PtrTrack->CurrPtr = InDeleteVariable;
    PtrTrack->Type    = (VARIABLE_STORAGE_TYPE) InDeleteIndex;
    return EFI_SUCCESS;
  }

  PtrTrack->CurrPtr = NULL;
  return EFI_NOT_FOUND;
}

EFI_STATUS
EFIAPI
GetVariable (
  IN      CHAR16            *VariableName,
  IN      EFI_GUID          *VendorGuid,
  OUT     UINT32            *Attributes OPTIONAL,
  IN OUT  UINTN             *DataSize,
  OUT     VOID              *Data
  )
/*++

Routine Description:

  This code finds variable in storage blocks (Volatile or Non-Volatile)

Arguments:

  VariableName                    Name of Variable to be found
  VendorGuid                      Variable vendor GUID
  Attributes OPTIONAL             Attribute value of the variable found
  DataSize                        Size of Data found. If size is less than the
                                  data, this value contains the required size.
  Data                            Data pointer

Returns:

  EFI STATUS

--*/
{
  VARIABLE_POINTER_TRACK  Variable;
  UINTN                   VarDataSize;
  EFI_STATUS              Status;

  if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  //
  // Find existing variable
  //
  Status = FindVariable (VariableName, VendorGuid, &Variable);

  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Get data size
  //
  VarDataSize = Variable.CurrPtr->DataSize;
  if (*DataSize >= VarDataSize) {
    if (Data == NULL) {
      return EFI_INVALID_PARAMETER;
    }
    EfiCopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);

    if (Attributes != NULL) {
      *Attributes = Variable.CurrPtr->Attributes;
    }

    *DataSize = VarDataSize;

    return EFI_SUCCESS;
  } else {
    *DataSize = VarDataSize;
    return EFI_BUFFER_TOO_SMALL;
  }
}

EFI_STATUS
EFIAPI
GetNextVariableName (
  IN OUT  UINTN             *VariableNameSize,
  IN OUT  CHAR16            *VariableName,
  IN OUT  EFI_GUID          *VendorGuid
  )
/*++

Routine Description:

  This code Finds the Next available variable

Arguments:

  VariableNameSize            Size of the variable
  VariableName                Pointer to variable name
  VendorGuid                  Variable Vendor Guid

Returns:

  EFI STATUS

--*/
{
  VARIABLE_POINTER_TRACK  Variable;
  UINTN                   VarNameSize;
  EFI_STATUS              Status;

  if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status = FindVariable (VariableName, VendorGuid, &Variable);

  if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
    return Status;
  }

  if (VariableName[0] != 0) {
    //
    // If variable name is not NULL, get next variable
    //
    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
  }

  while (TRUE) {
    //
    // The order we find variable is: 1). NonVolatile; 2). Volatile
    // If both volatile and non-volatile variable store are parsed,
    // return not found
    //
    if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
      if (Variable.Type == Volatile) {
        //
        // Since we met the end of Volatile storage, we have parsed all the stores.
        //
        return EFI_NOT_FOUND;
      }

      //
      // End of NonVolatile, continue to parse Volatile
      //
      Variable.Type = Volatile;
      Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);
      Variable.EndPtr   = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);

      Variable.CurrPtr = Variable.StartPtr;
      if (!IsValidVariableHeader (Variable.CurrPtr)) {
        continue;
      }
    }
    //
    // Variable is found
    //
    if (IsValidVariableHeader (Variable.CurrPtr) &&
        ((Variable.CurrPtr->State == VAR_ADDED) ||
         (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {
      if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
        VarNameSize = Variable.CurrPtr->NameSize;
        if (VarNameSize <= *VariableNameSize) {
          EfiCopyMem (
            VariableName,
            GET_VARIABLE_NAME_PTR (Variable.CurrPtr),
            VarNameSize
            );
          EfiCopyMem (
            VendorGuid,
            &Variable.CurrPtr->VendorGuid,
            sizeof (EFI_GUID)
            );
          Status = EFI_SUCCESS;
        } else {
          Status = EFI_BUFFER_TOO_SMALL;
        }

        *VariableNameSize = VarNameSize;
        return Status;
      }
    }

    Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
  }

  return EFI_NOT_FOUND;
}

EFI_STATUS
EFIAPI
SetVariable (
  IN CHAR16                  *VariableName,
  IN EFI_GUID                *VendorGuid,
  IN UINT32                  Attributes,
  IN UINTN                   DataSize,
  IN VOID                    *Data
  )
/*++

Routine Description:

  This code sets variable in storage blocks (Volatile or Non-Volatile)

Arguments:

  VariableName                    Name of Variable to be found
  VendorGuid                      Variable vendor GUID
  Attributes                      Attribute value of the variable found
  DataSize                        Size of Data found. If size is less than the
                                  data, this value contains the required size.
  Data                            Data pointer

Returns:

  EFI STATUS
  EFI_INVALID_PARAMETER           - Invalid parameter
  EFI_SUCCESS                     - Set successfully
  EFI_OUT_OF_RESOURCES            - Resource not enough to set variable
  EFI_NOT_FOUND                   - Not found

--*/
{
  VARIABLE_POINTER_TRACK  Variable;
  EFI_STATUS              Status;
  VARIABLE_HEADER         *NextVariable;
  UINTN                   VarNameSize;
  UINTN                   VarNameOffset;
  UINTN                   VarDataOffset;
  UINTN                   VarSize;
  UINT8                   State;
  BOOLEAN                 Reclaimed;
  VARIABLE_STORAGE_TYPE   StorageType;

  Reclaimed = FALSE;

  if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status = FindVariable (VariableName, VendorGuid, &Variable);

  if (Status == EFI_INVALID_PARAMETER) {
    return Status;
  }
  //
  //  The size of the VariableName, including the Unicode Null in bytes plus
  //  the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.
  else if ((DataSize > MAX_VARIABLE_SIZE) ||
           (sizeof (VARIABLE_HEADER) + EfiStrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {
    return EFI_INVALID_PARAMETER;
  }
  //
  //  Make sure if runtime bit is set, boot service bit is set also
  //
  else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS
          ) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Runtime but Attribute is not Runtime
  //
  else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Cannot set volatile variable in Runtime
  //
  else if (EfiAtRuntime () && Attributes && !(Attributes & EFI_VARIABLE_NON_VOLATILE)) {
    return EFI_INVALID_PARAMETER;
  }
  //
  // Setting a data variable with no access, or zero DataSize attributes
  // specified causes it to be deleted.
  //
  else if (DataSize == 0 || Attributes == 0) {
    if (!EFI_ERROR (Status)) {
      //
      // Found this variable in storage
      //
      State = Variable.CurrPtr->State;
      State &= VAR_DELETED;

      Status = mGlobal->VariableStore[Variable.Type]->Write (
                                                        mGlobal->VariableStore[Variable.Type],
                                                        VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
                                                        sizeof (Variable.CurrPtr->State),
                                                        &State
                                                        );
      //
      // NOTE: Write operation at least can write data to memory cache
      //       Discard file writing failure here.
      //
      return EFI_SUCCESS;
    }

    return EFI_NOT_FOUND;
  } else {
    if (!EFI_ERROR (Status)) {
      //
      // Found this variable in storage
      // If the variable is marked valid and the same data has been passed in
      // then return to the caller immediately.
      //
      if ((Variable.CurrPtr->DataSize == DataSize) &&
          (EfiCompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)
            ) {
        return EFI_SUCCESS;
      } else if ((Variable.CurrPtr->State == VAR_ADDED) ||
                 (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
        //
        // Mark the old variable as in delete transition
        //
        State = Variable.CurrPtr->State;
        State &= VAR_IN_DELETED_TRANSITION;

        Status = mGlobal->VariableStore[Variable.Type]->Write (
                                                          mGlobal->VariableStore[Variable.Type],
                                                          VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
                                                          sizeof (Variable.CurrPtr->State),
                                                          &State
                                                          );
        //
        // NOTE: Write operation at least can write data to memory cache
        //       Discard file writing failure here.
        //
      }
    }
    //
    // Create a new variable and copy the data.
    // We can firstly write all the data in memory, then write them to file
    // This can reduce the times of write operation
    //
    NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;

    NextVariable->StartId     = VARIABLE_DATA;
    NextVariable->Attributes  = Attributes;
    NextVariable->State       = VAR_ADDED;
    NextVariable->Reserved    = 0;
    VarNameOffset             = sizeof (VARIABLE_HEADER);
    VarNameSize               = EfiStrSize (VariableName);
    EfiCopyMem (
      (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
      VariableName,
      VarNameSize
      );
    VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
    EfiCopyMem (
      (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
      Data,
      DataSize
      );
    EfiCopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
    //
    // There will be pad bytes after Data, the NextVariable->NameSize and
    // NextVariable->DataSize should not include pad size so that variable
    // service can get actual size in GetVariable
    //
    NextVariable->NameSize  = (UINT32)VarNameSize;
    NextVariable->DataSize  = (UINT32)DataSize;

    //
    // The actual size of the variable that stores in storage should
    // include pad size.
    // VarDataOffset: offset from begin of current variable header
    //
    VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);

    StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;

    if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >
        ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size
        ) {
      if ((StorageType == NonVolatile) && EfiAtRuntime ()) {
        return EFI_OUT_OF_RESOURCES;
      }
      //
      // Perform garbage collection & reclaim operation
      //
      Status = Reclaim (StorageType, Variable.CurrPtr);
      if (EFI_ERROR (Status)) {
        //
        // Reclaim error
        // we cannot restore to original state, fetal error, report to user
        //
        DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));
        return Status;
      }
      //
      // If still no enough space, return out of resources
      //
      if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >
          ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size
         ) {
        return EFI_OUT_OF_RESOURCES;
      }

      Reclaimed = TRUE;
    }
    Status = mGlobal->VariableStore[StorageType]->Write (
                                                    mGlobal->VariableStore[StorageType],
                                                    mGlobal->LastVariableOffset[StorageType],
                                                    VarSize,
                                                    NextVariable
                                                    );
    //
    // NOTE: Write operation at least can write data to memory cache
    //       Discard file writing failure here.
    //
    mGlobal->LastVariableOffset[StorageType] += VarSize;

    //
    // Mark the old variable as deleted
    //
    if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {
      State = Variable.CurrPtr->State;
      State &= VAR_DELETED;

      Status = mGlobal->VariableStore[StorageType]->Write (
                                                      mGlobal->VariableStore[StorageType],
                                                      VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable.CurrPtr - (UINTN) Variable.StartPtr),
                                                      sizeof (Variable.CurrPtr->State),
                                                      &State
                                                      );
      //

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?