summaryrefslogtreecommitdiffstats
path: root/EmulatorPkg/Application/RedfishPlatformConfig/RedfishPlatformConfig.c
blob: 89d4c760a77d77a2a99278d23efed6545c3ff697 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/** @file
  The implementation for Redfish Platform Configuration application.

  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>

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

**/

#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/NetLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Protocol/ShellParameters.h>

UINTN  Argc;
CHAR16 **Argv;

/**

  This function parse application ARG.

  @return Status
**/
EFI_STATUS
GetArg (
  VOID
  )
{
  EFI_STATUS                    Status;
  EFI_SHELL_PARAMETERS_PROTOCOL *ShellParameters;

  Status = gBS->HandleProtocol (
                  gImageHandle,
                  &gEfiShellParametersProtocolGuid,
                  (VOID**)&ShellParameters
                  );
  if (EFI_ERROR(Status)) {
    return Status;
  }

  Argc = ShellParameters->Argc;
  Argv = ShellParameters->Argv;
  return EFI_SUCCESS;
}

/**

  This function print the help message.

**/
VOID
PrintHelp (
  VOID
  )
{
  Print (L"\n");
  Print (L"Format (Only Ipv4 Address is supported):\n");
  Print (L"RedfishPlatformConfig.efi -s HostIpAddress HostIpMask RedfishServiceIpAddress RedfishServiceIpMask RedfishServiceIpPort\n");
  Print (L"OR:\n");
  Print (L"RedfishPlatformConfig.efi -a RedfishServiceIpAddress RedfishServiceIpMask RedfishServiceIpPort\n");
  Print (L"\n");
}

/**
  The user Entry Point for Application. The user code starts with this function
  as the real entry point for the application.

  @param[in] ImageHandle    The firmware allocated handle for the EFI image.
  @param[in] SystemTable    A pointer to the EFI System Table.

  @retval EFI_SUCCESS       The entry point is executed successfully.
  @retval other             Some error occurs when executing this entry point.

**/
EFI_STATUS
EFIAPI
UefiMain (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS    Status;
  RETURN_STATUS ReturnStatus;

  UINT8             HostIpAssignmentType;
  EFI_IPv4_ADDRESS  HostIpAddress;
  EFI_IPv4_ADDRESS  HostIpMask;
  EFI_IPv4_ADDRESS  RedfishServiceIpAddress;
  EFI_IPv4_ADDRESS  RedfishServiceIpMask;
  UINTN             RedfishServiceIpPort;

  Status = GetArg();
  if (EFI_ERROR(Status)) {
    return Status;
  }

  //
  // Format is like :
  // RedfishPlatformConfig.efi -s HostIpAddress HostIpMask RedfishServiceIpAddress RedfishServiceIpMask RedfishServiceIpPort
  // RedfishPlatformConfig.efi -a RedfishServiceIpAddress RedfishServiceIpMask RedfishServiceIpPort
  //
  if (Argc != 7 && Argc != 5) {

    PrintHelp();
    return EFI_UNSUPPORTED;
  }

  if (StrCmp(Argv[1], L"-s") == 0) {

    HostIpAssignmentType = 1;

    Status = NetLibStrToIp4 (Argv[2], &HostIpAddress);
    if (EFI_ERROR (Status)) {
      PrintHelp();
      return Status;
    }
    Status = NetLibStrToIp4 (Argv[3], &HostIpMask);
    if (EFI_ERROR (Status)) {
      PrintHelp();
      return Status;
    }
    Status = NetLibStrToIp4 (Argv[4], &RedfishServiceIpAddress);
    if (EFI_ERROR (Status)) {
      PrintHelp();
      return Status;
    }
    Status = NetLibStrToIp4 (Argv[5], &RedfishServiceIpMask);
    if (EFI_ERROR (Status)) {
      PrintHelp();
      return Status;
    }
    ReturnStatus = StrDecimalToUintnS (Argv[6], NULL, &RedfishServiceIpPort);
    if (RETURN_ERROR (ReturnStatus)) {
      PrintHelp();
      return Status;
    }

    Status = gRT->SetVariable (
                    L"HostIpAssignmentType",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (UINT8),
                    &HostIpAssignmentType
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"HostIpAddress",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (EFI_IPv4_ADDRESS),
                    &HostIpAddress
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"HostIpMask",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (EFI_IPv4_ADDRESS),
                    &HostIpMask
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"RedfishServiceIpAddress",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (EFI_IPv4_ADDRESS),
                    &RedfishServiceIpAddress
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"RedfishServiceIpMask",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (EFI_IPv4_ADDRESS),
                    &RedfishServiceIpMask
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"RedfishServiceIpPort",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (UINT16),
                    &RedfishServiceIpPort
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Print (L"\n");
    Print (L"HostIpAssignmentType is Static!\n");
    Print (L"HostIpAddress: %s has been set Successfully!\n", Argv[2]);
    Print (L"HostIpMask: %s has been set Successfully!\n", Argv[3]);
    Print (L"RedfishServiceIpAddress: %s has been set Successfully!\n", Argv[4]);
    Print (L"RedfishServiceIpMask: %s has been set Successfully!\n", Argv[5]);
    Print (L"RedfishServiceIpPort: %s has been set Successfully!\n", Argv[6]);
    Print (L"Please Restart!\n");

  } else if (StrCmp(Argv[1], L"-a") == 0) {

    HostIpAssignmentType = 3;

    Status = NetLibStrToIp4 (Argv[2], &RedfishServiceIpAddress);
    if (EFI_ERROR (Status)) {
      PrintHelp();
      return Status;
    }
    Status = NetLibStrToIp4 (Argv[3], &RedfishServiceIpMask);
    if (EFI_ERROR (Status)) {
      PrintHelp();
      return Status;
    }
    ReturnStatus = StrDecimalToUintnS (Argv[4], NULL, &RedfishServiceIpPort);
    if (RETURN_ERROR (ReturnStatus)) {
      PrintHelp();
      return Status;
    }

    Status = gRT->SetVariable (
                    L"HostIpAssignmentType",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (UINT8),
                    &HostIpAssignmentType
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"RedfishServiceIpAddress",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (EFI_IPv4_ADDRESS),
                    &RedfishServiceIpAddress
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"RedfishServiceIpMask",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (EFI_IPv4_ADDRESS),
                    &RedfishServiceIpMask
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Status = gRT->SetVariable (
                    L"RedfishServiceIpPort",
                    &gEmuRedfishServiceGuid,
                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
                    sizeof (UINT16),
                    &RedfishServiceIpPort
                    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    Print (L"\n");
    Print (L"HostIpAssignmentType is Auto!\n");
    Print (L"RedfishServiceIpAddress: %s has been set Successfully!\n", Argv[2]);
    Print (L"RedfishServiceIpMask: %s has been set Successfully!\n", Argv[3]);
    Print (L"RedfishServiceIpPort: %s has been set Successfully!\n", Argv[4]);
    Print (L"Please Restart!\n");
  } else if (StrCmp(Argv[1], L"-h") == 0 || StrCmp(Argv[1], L"-help") == 0) {

    PrintHelp();
  } else {

    PrintHelp();
    return EFI_UNSUPPORTED;
  }

  return EFI_SUCCESS;
}