summaryrefslogtreecommitdiff
path: root/Platform/Comcast/Library/RdkBootManagerLib/HttpBoot.c
blob: 85f71040457d5d4b5d24dd0f1c783991b226ca82 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
#  Copyright (c) 2014-2018, Linaro Limited. All rights reserved.
#
#  This program and the accompanying materials
#  are licensed and made available under the terms and conditions of the BSD License
#  which accompanies this distribution.  The full text of the license may be found at
#  http://opensource.org/licenses/bsd-license.php
#
#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
*/
#include <RdkBootManagerLib.h>

#define FILE_HDR_SIZE 16

STATIC EFI_LOAD_FILE_PROTOCOL  *LoadFile = NULL;
STATIC HTTP_BOOT_PRIVATE_DATA  *Private  = NULL;

STATIC
VOID
HttpPrivateFromLoadFile (
  IN   EFI_LOAD_FILE_PROTOCOL   *LoadFile,
  OUT  HTTP_BOOT_PRIVATE_DATA   **Private
  )
{
  HTTP_BOOT_VIRTUAL_NIC  *Ip4Nic = NULL;

  UINTN Offset = (UINTN)&Ip4Nic->LoadFile;

  Ip4Nic = (VOID *)((CHAR8 *)LoadFile - Offset);
  ASSERT (Ip4Nic->Signature == HTTP_BOOT_VIRTUAL_NIC_SIGNATURE);
  *Private = Ip4Nic->Private;
}

STATIC
VOID
HttpGetLoadFileHandle (
  OUT EFI_LOAD_FILE_PROTOCOL  **LoadFile
  )
{
  EFI_STATUS                        Status;
  UINTN                             LoopIndex;
  UINTN                             NumHandles;
  EFI_HANDLE                        *AllHandles;
  EFI_HANDLE                        Handle;
  EFI_DEVICE_PATH_PROTOCOL          *DevicePath;
  EFI_DEVICE_PATH_TO_TEXT_PROTOCOL  *DevPathToText;
  UINT16                            *DeviceFullPath;

  Status = gBS->LocateProtocol (
    &gEfiDevicePathToTextProtocolGuid,
    NULL,
    (VOID **) &DevPathToText
    );
  ASSERT_EFI_ERROR (Status);

  // Get every LoadFile protocol instance installed in the system
  Status = gBS->LocateHandleBuffer (
    ByProtocol,
    &gEfiLoadFileProtocolGuid,
    NULL,
    &NumHandles,
    &AllHandles
    );
  ASSERT_EFI_ERROR (Status);

  // Get HTTP driver handle from AllHandles
  for (LoopIndex = 0; LoopIndex < NumHandles; LoopIndex++) {

    Handle = AllHandles[LoopIndex];

    // Get the device path for the handle
    Status = gBS->OpenProtocol (
      Handle,
      &gEfiDevicePathProtocolGuid,
      (VOID **) &DevicePath,
      gImageHandle,
      NULL,
      EFI_OPEN_PROTOCOL_GET_PROTOCOL
      );
    ASSERT_EFI_ERROR (Status);

    DeviceFullPath = DevPathToText->ConvertDevicePathToText (
      DevicePath,
      FALSE,
      TRUE
      );

    ASSERT (DeviceFullPath != NULL);

    if (StrStr (DeviceFullPath, L"IPv4") != NULL) {
      DEBUG((DEBUG_INFO, "IPv4 protocol found\n"));
      Status = gBS->OpenProtocol (
        Handle,
        &gEfiLoadFileProtocolGuid,
        (VOID **) LoadFile,
        gImageHandle,
        NULL,
        EFI_OPEN_PROTOCOL_GET_PROTOCOL
        );
      ASSERT_EFI_ERROR (Status);

      FreePool (AllHandles);
      break;
    }
  }

  ASSERT (LoopIndex < NumHandles);
}

STATIC
EFI_STATUS
HttpUpdatePath (
  IN   CHAR16                   *Uri,
  OUT  EFI_DEVICE_PATH_PROTOCOL **NewDevicePath
  )
{
  EFI_DEV_PATH              *Node;
  EFI_DEVICE_PATH_PROTOCOL  *TmpDevicePath;
  EFI_STATUS                Status;
  UINTN                     Index;
  UINTN                     Length;
  CHAR8                     AsciiUri[URI_STR_MAX_SIZE];

  Node           = NULL;
  TmpDevicePath  = NULL;
  Status         = EFI_SUCCESS;

  // Convert the scheme to all lower case.
  for (Index = 0; Index < StrLen (Uri); Index++) {
    if (Uri[Index] == L':') {
      break;
    }
    if (Uri[Index] >= L'A' && Uri[Index] <= L'Z') {
      Uri[Index] -= (CHAR16)(L'A' - L'a');
    }
  }

  // Only accept empty URI, or http and https URI.
  if ((StrLen (Uri) != 0) &&
    (StrnCmp (Uri, L"http://", 7) != 0) &&
    (StrnCmp (Uri, L"https://", 8) != 0)) {
    return EFI_INVALID_PARAMETER;
  }

  // Create a new device path by appending the IP node and URI node to
  // the driver's parent device path
  Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH));
  if (Node == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto Exit;
  }
  Node->Ipv4.Header.Type    = MESSAGING_DEVICE_PATH;
  Node->Ipv4.Header.SubType = MSG_IPv4_DP;
  SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH));
  TmpDevicePath = AppendDevicePathNode (\
    Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*)Node);
  FreePool (Node);
  if (TmpDevicePath == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  // Update the URI node with the input boot file URI.
  UnicodeStrToAsciiStrS (Uri, AsciiUri, sizeof (AsciiUri));
  Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (AsciiUri);
  Node = AllocatePool (Length);
  if (Node == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    FreePool (TmpDevicePath);
    goto Exit;
  }
  Node->DevPath.Type    = MESSAGING_DEVICE_PATH;
  Node->DevPath.SubType = MSG_URI_DP;

  SetDevicePathNodeLength (Node, Length);
  CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), \
    AsciiUri, AsciiStrSize (AsciiUri));
  *NewDevicePath = AppendDevicePathNode (TmpDevicePath, \
    (EFI_DEVICE_PATH_PROTOCOL*) Node);

  FreePool (Node);
  FreePool (TmpDevicePath);

  if (*NewDevicePath == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto Exit;
  }

Exit:

  return Status;
}

STATIC
EFI_STATUS
HttpGetImage (
  IN   CHAR16  *Uri,
  OUT  UINT8   **FileBuffer,
  OUT  UINTN   *FileSize
  )
{
  EFI_DEVICE_PATH_PROTOCOL  *NewDevicePath;
  EFI_STATUS                Status;

  *FileBuffer   = NULL;
  NewDevicePath = NULL;
  *FileSize     = 0;

  // Get the LoadFile Handle and
  // Private structure of HTTP driver
  if (LoadFile == NULL) {
    HttpGetLoadFileHandle (&LoadFile);
    HttpPrivateFromLoadFile (LoadFile, &Private);
  }

  // Update URI path
  Status = HttpUpdatePath (Uri, &NewDevicePath);
  if (EFI_ERROR (Status)) {
    goto Exit;
  }

  // Get the HTTP image from server
  Status = LoadFile->LoadFile (LoadFile, NewDevicePath, \
    TRUE, FileSize, *FileBuffer);
  if((Status != EFI_WARN_FILE_SYSTEM) && \
    (Status != EFI_BUFFER_TOO_SMALL)) {
    goto Exit;
  }

  *FileBuffer = AllocatePool (*FileSize);
  if (*FileBuffer == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto Exit;
  }

  Status = LoadFile->LoadFile (LoadFile, NewDevicePath, \
    TRUE, FileSize, *FileBuffer);
  if (EFI_ERROR (Status)) {
    FreePool (FileBuffer);
    goto Exit;
  }

Exit:

  if (NewDevicePath != NULL) {
    FreePool (NewDevicePath);
  }

  return Status;
}

UINTN
ParseHeader (
  VOID * Str
  )
{
  UINTN i, Size;
  UINT8 *Ptr;

  Ptr = Str;
  for (i = 0, Size = 0; i < FILE_HDR_SIZE; i++) {
      Size = (Ptr[i] - '0') + (Size * 10);
  }

  return Size;
}

EFI_STATUS
RdkHttpBoot (
  VOID
  )
{
  EFI_STATUS  	Status;
  VOID        	*FilePtr;
  UINT8       	*FileBuffer;
  UINT16      	*Uri;
  UINTN       	FileSize;
  UINTN       	LoopIndex;
  UINTN       	Size;
  CONST CHAR16  *DtbPath;
  CONST CHAR16	*ImagePath;
  CONST CHAR16  *ServerUrlPath;

  Status = GetRdkVariable (L"URL", &ServerUrlPath);
  ASSERT_EFI_ERROR (Status);

  // Get the Server name stored in file Server.url
  Status = RdkReadFile (ServerUrlPath, (VOID **)&FileBuffer, &FileSize);
  ASSERT_EFI_ERROR (Status);

  Uri = AllocateZeroPool (sizeof(*Uri) * (FileSize + 1));
  if (Uri == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    ASSERT_EFI_ERROR (Status);
  }

  for(LoopIndex = 0; LoopIndex < FileSize; LoopIndex++) {
    Uri[LoopIndex] = FileBuffer[LoopIndex];
  }

  if(FileBuffer[FileSize-1] == '\n') {
    Uri[FileSize-1] = '\0';
  }

  FreePool (FileBuffer);
  FileBuffer=NULL;

  // Disable watchdog
  Status = gBS->SetWatchdogTimer (0, 0x10000, 0, NULL);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_WARN, \
      "HttpBoot: Couldn't disable watchdog timer: %r\n", Status));
  }

  // Get the File from server using it's URI
  Status = HttpGetImage (Uri, &FileBuffer, &FileSize);
  ASSERT_EFI_ERROR (Status);

  // Write the received image to flash
  FilePtr   = FileBuffer;
  Size      = ParseHeader (FilePtr);
  FilePtr  += FILE_HDR_SIZE;
  Status    = PartitionWrite ((CHAR8 *)FixedPcdGetPtr (\
    PcdRdkSystemPartitionName), FilePtr, Size);
  ASSERT_EFI_ERROR (Status);

  FilePtr  += Size;
  Size      = ParseHeader (FilePtr);
  FilePtr  += FILE_HDR_SIZE;
  Status    = GetRdkVariable (L"IMAGE", &ImagePath);
  ASSERT_EFI_ERROR (Status);
  Status    = RdkWriteFile (ImagePath, &FilePtr, &Size);
  ASSERT_EFI_ERROR (Status);

  if ( FixedPcdGetBool (PcdDtbAvailable) ) {
  FilePtr  += Size;
  Size      = ParseHeader (FilePtr);
  FilePtr  += FILE_HDR_SIZE;
  Status    = GetRdkVariable (L"DTB", &DtbPath);
  ASSERT_EFI_ERROR (Status);
  Status    = RdkWriteFile (DtbPath, &FilePtr, &Size);
  ASSERT_EFI_ERROR (Status);
  }

  FreePool (FileBuffer);
  FreePool (Uri);

  return Status;
}