/* Caution: This function may receive untrusted input. The data size external input, so this function will validate it carefully to avoid buffer overflow. */
EFI_STATUS InitCommunicateBuffer(OUT VOID **DataPtr OPTIONAL, IN UINTN DataSize, IN UINTN Function)
/* Caution: This function may receive untrusted input. The data size external input, so this function will validate it carefully to avoid buffer overflow. */
EFI_STATUS InitCommunicateBuffer(OUT VOID **DataPtr OPTIONAL, IN UINTN DataSize, IN UINTN Function)
{
  EFI_MM_COMMUNICATE_HEADER        *SmmCommunicateHeader;
  SMM_VARIABLE_COMMUNICATE_HEADER  *SmmVariableFunctionHeader;
  if (DataSize + SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE > mVariableBufferSize) {
    return EFI_INVALID_PARAMETER;
  }
  SmmCommunicateHeader = (EFI_MM_COMMUNICATE_HEADER *)mVariableBuffer;
  CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGuid);
  SmmCommunicateHeader->MessageLength = DataSize + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE;
  SmmVariableFunctionHeader           = (SMM_VARIABLE_COMMUNICATE_HEADER *)SmmCommunicateHeader->Data;
  SmmVariableFunctionHeader->Function = Function;
  if (DataPtr != NULL) {
    *DataPtr = SmmVariableFunctionHeader->Data;
  }
  return EFI_SUCCESS;
}