summaryrefslogtreecommitdiffstats
path: root/RedfishPkg/Library/DxeRestExLib/DxeRestExLib.c
blob: d9acad24dec1805d7dd77896f1dcdc56f99d4123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/** @file
  This library provides help functions for REST EX Protocol.

  (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Uefi.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/NetLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/Http.h>
#include <Protocol/RestEx.h>

#define REST_EX_CONFIG_DATA_LEN_UNKNOWN  0xff

/**
  This function allows the caller to create child handle for specific
  REST server.

  @param[in]  Image                The image handle used to open service.
  @param[in]  AccessMode           Access mode of REST server.
  @param[in]  ConfigType           Underlying configuration to communicate with REST server.
  @param[in]  ServiceType          REST service type.
  @param[out] ChildInstanceHandle  The handle to receive the create child.

  @retval  EFI_SUCCESS            Can't create the corresponding REST EX child instance.
  @retval  EFI_INVALID_PARAMETERS Any of input parameters is improper.

**/
EFI_STATUS
RestExLibCreateChild (
  IN EFI_HANDLE                       Image,
  IN EFI_REST_EX_SERVICE_ACCESS_MODE  AccessMode,
  IN EFI_REST_EX_CONFIG_TYPE          ConfigType,
  IN EFI_REST_EX_SERVICE_TYPE         ServiceType,
  OUT EFI_HANDLE                      *ChildInstanceHandle
  )
{
  EFI_STATUS                Status;
  UINTN                     NoBuffer;
  EFI_HANDLE                *Handle;
  EFI_HANDLE                ChildHandle;
  EFI_REST_EX_PROTOCOL      *RestEx;
  EFI_REST_EX_SERVICE_INFO  *RestExServiceInfo;
  UINT8                     LenOfConfig;

  if ((Image == NULL) ||
      (AccessMode >= EfiRestExServiceModeMax) ||
      (ConfigType >= EfiRestExConfigTypeMax) ||
      (ServiceType >= EfiRestExServiceTypeMax) ||
      (ChildInstanceHandle == NULL)
      )
  {
    return EFI_INVALID_PARAMETER;
  }

  *ChildInstanceHandle = NULL;
  //
  // Locate all REST EX binding service.
  //
  Handle   = NULL;
  NoBuffer = 0;
  Status   = gBS->LocateHandleBuffer (
                    ByProtocol,
                    &gEfiRestExServiceBindingProtocolGuid,
                    NULL,
                    &NoBuffer,
                    &Handle
                    );
  if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
    return Status;
  }

  Handle = (EFI_HANDLE *)AllocateZeroPool (sizeof (EFI_HANDLE) * NoBuffer);
  if (Handle == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  &gEfiRestExServiceBindingProtocolGuid,
                  NULL,
                  &NoBuffer,
                  &Handle
                  );
  if (EFI_ERROR (Status)) {
    FreePool (Handle);
    return Status;
  }

  //
  // Search for the proper REST EX instance.
  //
  while (NoBuffer != 0) {
    ChildHandle = NULL;
    Status      = NetLibCreateServiceChild (
                    *(Handle + (NoBuffer - 1)),
                    Image,
                    &gEfiRestExServiceBindingProtocolGuid,
                    &ChildHandle
                    );
    if (!EFI_ERROR (Status)) {
      Status = gBS->OpenProtocol (
                      ChildHandle,
                      &gEfiRestExProtocolGuid,
                      (VOID **)&RestEx,
                      Image,
                      NULL,
                      EFI_OPEN_PROTOCOL_GET_PROTOCOL
                      );
      if (EFI_ERROR (Status)) {
        goto ON_ERROR;
      }

      //
      // Get the information of REST service provided by this EFI REST EX driver
      //
      Status = RestEx->GetService (
                         RestEx,
                         &RestExServiceInfo
                         );
      if (EFI_ERROR (Status)) {
        goto ON_ERROR;
      }

      //
      // Check REST EX property.
      //
      switch (ConfigType) {
        case EfiRestExConfigHttp:
          LenOfConfig = sizeof (EFI_REST_EX_HTTP_CONFIG_DATA);
          break;

        case EfiRestExConfigUnspecific:
          LenOfConfig = REST_EX_CONFIG_DATA_LEN_UNKNOWN;
          break;

        default:
          goto ON_ERROR;
      }

      if ((RestExServiceInfo->EfiRestExServiceInfoV10.RestServiceAccessMode != AccessMode) ||
          (RestExServiceInfo->EfiRestExServiceInfoV10.RestServiceType != ServiceType) ||
          (RestExServiceInfo->EfiRestExServiceInfoV10.RestExConfigType != ConfigType) ||
          ((LenOfConfig != REST_EX_CONFIG_DATA_LEN_UNKNOWN) && (RestExServiceInfo->EfiRestExServiceInfoV10.RestExConfigDataLength != LenOfConfig)))
      {
        goto ON_ERROR;
      }
    }

    //
    // This is proper REST EX instance.
    //
    *ChildInstanceHandle = ChildHandle;
    FreePool (Handle);
    return EFI_SUCCESS;

ON_ERROR:;
    NetLibDestroyServiceChild (
      *(Handle + (NoBuffer - 1)),
      Image,
      &gEfiRestExServiceBindingProtocolGuid,
      ChildHandle
      );
    NoBuffer--;
  }

  FreePool (Handle);
  return EFI_NOT_FOUND;
}