summaryrefslogtreecommitdiff
path: root/Platform/Comcast/Library/RdkBootManagerLib/RdkFile.c
blob: c398d4b49931c6e50b6d3c994b57699c09756eea (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
#  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 MAX_VAR       6

#define ALLOCATE_STRING_MEM(X)  AllocateZeroPool((X + 1) * sizeof(CHAR16))

/**
 * list_for_each_entry	-	iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define LIST_FOR_EACH_ENTRY(Pos, Head, Member)				\
	for (Pos = BASE_CR((Head)->ForwardLink, typeof(*Pos), Member);	\
	     &Pos->Member != (Head);					\
	     Pos = BASE_CR(Pos->Member.ForwardLink, typeof(*Pos), Member))

/**
 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define LIST_FOR_EACH_ENTRY_SAFE(Pos, N, Head, Member)			\
	for (Pos = BASE_CR((Head)->ForwardLink, typeof(*Pos), Member),	\
		N = BASE_CR(Pos->Member.ForwardLink, typeof(*Pos), Member);	\
	     &Pos->Member != (Head);					\
	     Pos = N, N = BASE_CR(N->Member.ForwardLink, typeof(*N), Member))

/* ************************** */

typedef struct {
  CHAR16  *Name;
  LIST_ENTRY List;
} DIR_NODE;
/* ************************** */

STATIC UINT8    VarablesInitialzed = 0;
STATIC CHAR16   *VarResult[MAX_VAR][2];

STATIC
VOID
SaveString (
  OUT CHAR16    **Dest,
  IN  CHAR16    *String1,
  IN  CHAR16    *String2
  )
{
  *Dest = ALLOCATE_STRING_MEM (StrLen (String1) + StrLen (String2));
  ASSERT (Dest != NULL);
  StrCat (*Dest, String1);
  StrCat (*Dest, String2);
}

STATIC
EFI_STATUS
LsFiles (
  IN  CONST CHAR16  *DirPath,
  IN  CONST CHAR16  *TargetFile,
  OUT CHAR16        **Result,
  IN  LIST_ENTRY    *Head
  )
{
  EFI_STATUS          Status;
  EFI_FILE_INFO       *FileInfo;
  EFI_FILE_PROTOCOL   *FileHandle;
  BOOLEAN             NoFile;
  CHAR16              *TempPath;
  DIR_NODE            *Node;

  NoFile    = FALSE;
  TempPath  = ALLOCATE_STRING_MEM (StrLen(DirPath) + 1);
  StrCat (TempPath, DirPath);
  StrCat (TempPath, L"/");

  Status = GetFileHandler (&FileHandle, DirPath, EFI_FILE_MODE_READ);
  ASSERT_EFI_ERROR (Status);

  for ( Status = FileHandleFindFirstFile (FileHandle, &FileInfo);
    !EFI_ERROR (Status) && !NoFile;
    Status = FileHandleFindNextFile (FileHandle, FileInfo, &NoFile)
    ) {
    if ( (FileInfo->Attribute & EFI_FILE_DIRECTORY) &&
      (StrCmp (FileInfo->FileName, L".") != 0) &&
      (StrCmp (FileInfo->FileName, L"..") != 0)) {
      Node = AllocateZeroPool (sizeof (DIR_NODE));
      //append directory name to the path
      SaveString (&Node->Name, TempPath, FileInfo->FileName);
      InsertHeadList (Head, &Node->List);
    } else if (StrCmp (FileInfo->FileName, TargetFile) == 0) {
      //append file to the absolute path
      SaveString (Result, TempPath, FileInfo->FileName);
      Status = EFI_SUCCESS;
      goto Exit;
    }
  }

  Status = EFI_NOT_FOUND;

Exit:
  FreePool (TempPath);
  return Status;
}

STATIC
VOID
DelDirList (
  IN  LIST_ENTRY *Head
  )
{
  DIR_NODE  *Node;
  DIR_NODE  *Temp;

  LIST_FOR_EACH_ENTRY_SAFE (Node, Temp, Head, List) {
    RemoveEntryList (&Node->List);
    FreePool (Node->Name);
    FreePool (Node);
  }
}

STATIC
EFI_STATUS
FindFileInDir (
  IN  CONST CHAR16  *DevPath,
  IN  CONST CHAR16  *TargetFile,
  OUT CHAR16        **Result
  )
{
  UINT8       Current;
  UINT8       Next;
  DIR_NODE    *Temp;
  LIST_ENTRY  DirList[2];

  *Result           = NULL;
  EFI_STATUS Status = EFI_NOT_FOUND;

  InitializeListHead (&DirList[0]);
  InitializeListHead (&DirList[1]);

  for (Current = Next = 0, Status = LsFiles (\
    DevPath, TargetFile, Result, &DirList[Current]);
    !IsListEmpty (&DirList[Current]);
    Current = Next) {
    Next = Current ^ 1;
    DelDirList (&DirList[Next]);

    LIST_FOR_EACH_ENTRY (Temp, &DirList[Current], List) {
      Status = LsFiles (Temp->Name, TargetFile, Result, &DirList[Next]);
      if (!EFI_ERROR (Status)) {
        DelDirList (&DirList[Current]);
        break;
      }
    }
  }

  DelDirList (&DirList[Next]);
  return Status;
}

STATIC
UINTN
StrSpn (
  IN CHAR8    *String,
  IN CHAR8    *CharSet
  )
{
  UINTN Count;

  for (Count = 0;
    String[Count] && ! (String[Count] == CharSet[0]);
    Count++);
  return Count;
}

STATIC
EFI_STATUS
InitVarList (
  IN  CHAR8  *FileData,
  IN  UINTN   FileSize
  )
{
  UINTN       InnerLoopIndex;
  UINTN       OuterLoopIndex;
  UINTN       Current;
  UINTN       Next;
  CHAR8       *VarDelimiter[2];
  EFI_STATUS  Status;

  VarDelimiter[0] = "=";
  VarDelimiter[1] = "\"";
  Status          = EFI_SUCCESS;

  //Initialize to NULL
  for (OuterLoopIndex = 0; OuterLoopIndex < MAX_VAR; OuterLoopIndex++) {
      VarResult[OuterLoopIndex][0] = VarResult[OuterLoopIndex][1] = NULL;
  }

  for (OuterLoopIndex = 0, Next=0;
    OuterLoopIndex < MAX_VAR && Next < FileSize;
    OuterLoopIndex++) {
    for (InnerLoopIndex = 0; InnerLoopIndex < 2; InnerLoopIndex++) {
      Current = Next;
      Next += StrSpn (&FileData[Next], VarDelimiter[InnerLoopIndex]);
      FileData[Next] = '\0';
      if (VarResult[OuterLoopIndex][InnerLoopIndex]) {
        FreePool (VarResult[OuterLoopIndex][InnerLoopIndex]);
      }
      VarResult[OuterLoopIndex][InnerLoopIndex] = \
        ALLOCATE_STRING_MEM (AsciiStrLen (&FileData[Current]));
      AsciiStrToUnicodeStr (&FileData[Current], \
        VarResult[OuterLoopIndex][InnerLoopIndex]);
      //skip new line
      Next += 2;
    }
  }

  return Status;
}

STATIC
EFI_STATUS
InitRdkVariables (
  VOID
  )
{
  EFI_STATUS    Status;
  UINTN         RdkSize;
  UINT8         *RdkData;
  CHAR16        *Result;
  CONST CHAR16  *DevPath;
  CONST CHAR16  *RdkFileName;

  DevPath     = (CONST CHAR16 *)FixedPcdGetPtr (PcdRdkConfFileDevicePath);
  RdkFileName = (CONST CHAR16 *)FixedPcdGetPtr (PcdRdkConfFileName);

  Status = FindFileInDir (DevPath, RdkFileName, &Result);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to find file %s in %s\n", \
      RdkFileName, DevPath));
    return Status;
  }

  Status = RdkReadFile ((CONST CHAR16 *)Result, \
    (VOID**)&RdkData, &RdkSize);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to read file %s\n", RdkFileName));
    return Status;
  }

  Status = InitVarList ((CHAR8 *)RdkData, RdkSize);
  return Status;
}

STATIC
EFI_STATUS
GetVarValue (
  IN  CONST CHAR16 *Name,
  OUT CONST CHAR16 **Value
  )
{
  UINTN         Count;
  EFI_STATUS    Status;

  if (!VarablesInitialzed) {
    Status = InitRdkVariables ();
    if (EFI_ERROR (Status)) {
      return Status;
    }

    VarablesInitialzed = 1;
  }

  //Initialize to NULL
  *Value = NULL;

  for (Count = 0; Count < MAX_VAR; Count++) {
    if (VarResult[Count][0] != NULL &&
      StrCmp (Name, VarResult[Count][0]) == 0) {
      *Value = VarResult[Count][1];
      return EFI_SUCCESS;
    }
  }

  return EFI_NOT_FOUND;
}

EFI_STATUS
GetRdkVariable (
  IN  CONST CHAR16  *Name,
  OUT CONST CHAR16  **Value
  )
{
  EFI_STATUS  Status;

  Status = GetVarValue (Name, Value);
  return Status;
}

EFI_STATUS
RdkReadFile (
    IN      CONST CHAR16  *Path,
    IN OUT  VOID          **BufferPtr,
    OUT     UINTN         *FileSize
)
{
  UINTN             BufferSize;
  UINT64            SourceFileSize;
  VOID              *Buffer;
  EFI_STATUS        Status;
  EFI_FILE_HANDLE   FileHandle;

  Status = GetFileHandler (&FileHandle, Path, EFI_FILE_MODE_READ);
  ASSERT_EFI_ERROR (Status);

  Buffer = NULL;

  // Get the file size
  Status = FileHandle->SetPosition (FileHandle, (UINT64) -1);
  if (EFI_ERROR (Status)) {
    goto Exit;
  }

  Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);
  if (EFI_ERROR (Status)) {
    goto Exit;
  }

  Status = FileHandle->SetPosition (FileHandle, 0);
  if (EFI_ERROR (Status)) {
    goto Exit;
  }

  BufferSize = (UINTN)SourceFileSize;
  Buffer =  AllocateZeroPool (BufferSize);
  if (Buffer == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  if (FileSize != NULL) *FileSize  = BufferSize;

  Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);
  if (EFI_ERROR (Status) || BufferSize != SourceFileSize) {
    FreePool (Buffer);
    Buffer = NULL;
    Status = EFI_BAD_BUFFER_SIZE;
    goto Exit;
  }

Exit:
  *BufferPtr = Buffer;
  return Status;
}

EFI_STATUS
RdkWriteFile (
  IN      CONST CHAR16    *Path,
  IN OUT  VOID            **BufferPtr,
  OUT     UINTN           *FileSize
)
{
  EFI_STATUS        Status;
  EFI_FILE_HANDLE   FileHandle;

  if (FileSize == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  Status = GetFileHandler (&FileHandle, Path, \
    EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE);
  ASSERT_EFI_ERROR (Status);

  Status = FileHandle->Write (FileHandle, FileSize, *BufferPtr);
  ASSERT_EFI_ERROR (Status);

  return Status;
}