summaryrefslogtreecommitdiff
path: root/IntelFrameworkModulePkg/Universal/FirmwareVolume/FwVolDxe/Ffs.c
blob: 1e3c839e2753053aa76bf9cc37885eed39141c20 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/** @file
  FFS file access utilities.

  Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>

  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 "FwVolDriver.h"

#define PHYSICAL_ADDRESS_TO_POINTER(Address)  ((VOID *) ((UINTN) Address))

/**
  Set File State in the FfsHeader.

  @param  State          File state to be set into FFS header.
  @param  FfsHeader      Points to the FFS file header

**/
VOID
SetFileState (
  IN UINT8                State,
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  //
  // Set File State in the FfsHeader
  //
  FfsHeader->State = (EFI_FFS_FILE_STATE) (FfsHeader->State ^ State);
  return ;
}

/**
  Get the FFS file state by checking the highest bit set in the header's state field.

  @param  ErasePolarity  Erase polarity attribute of the firmware volume
  @param  FfsHeader      Points to the FFS file header

  @return FFS File state

**/
EFI_FFS_FILE_STATE
GetFileState (
  IN UINT8                ErasePolarity,
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  EFI_FFS_FILE_STATE  FileState;
  UINT8               HighestBit;

  FileState = FfsHeader->State;

  if (ErasePolarity != 0) {
    FileState = (EFI_FFS_FILE_STATE)~FileState;
  }

  HighestBit = 0x80;
  while (HighestBit != 0 && ((HighestBit & FileState) == 0)) {
    HighestBit >>= 1;
  }

  return (EFI_FFS_FILE_STATE) HighestBit;
}

/**
  Convert the Buffer Address to LBA Entry Address.

  @param FvDevice        Cached FvDevice
  @param BufferAddress   Address of Buffer
  @param LbaListEntry    Pointer to the got LBA entry that contains the address.

  @retval EFI_NOT_FOUND  Buffer address is out of FvDevice.
  @retval EFI_SUCCESS    LBA entry is found for Buffer address.

**/
EFI_STATUS
Buffer2LbaEntry (
  IN     FV_DEVICE              *FvDevice,
  IN     EFI_PHYSICAL_ADDRESS   BufferAddress,
  OUT LBA_ENTRY                 **LbaListEntry
  )
{
  LBA_ENTRY   *LbaEntry;
  LIST_ENTRY  *Link;

  Link      = FvDevice->LbaHeader.ForwardLink;
  LbaEntry  = (LBA_ENTRY *) Link;

  //
  // Locate LBA which contains the address
  //
  while (&LbaEntry->Link != &FvDevice->LbaHeader) {
    if ((EFI_PHYSICAL_ADDRESS) (UINTN) (LbaEntry->StartingAddress) > BufferAddress) {
      break;
    }

    Link      = LbaEntry->Link.ForwardLink;
    LbaEntry  = (LBA_ENTRY *) Link;
  }

  if (&LbaEntry->Link == &FvDevice->LbaHeader) {
    return EFI_NOT_FOUND;
  }

  Link      = LbaEntry->Link.BackLink;
  LbaEntry  = (LBA_ENTRY *) Link;

  if (&LbaEntry->Link == &FvDevice->LbaHeader) {
    return EFI_NOT_FOUND;
  }

  *LbaListEntry = LbaEntry;

  return EFI_SUCCESS;
}

/**
  Convert the Buffer Address to LBA Address & Offset.

  @param FvDevice        Cached FvDevice
  @param BufferAddress   Address of Buffer
  @param Lba             Pointer to the gob Lba value
  @param Offset          Pointer to the got Offset

  @retval EFI_NOT_FOUND  Buffer address is out of FvDevice.
  @retval EFI_SUCCESS    LBA and Offset is found for Buffer address.

**/
EFI_STATUS
Buffer2Lba (
  IN     FV_DEVICE              *FvDevice,
  IN     EFI_PHYSICAL_ADDRESS   BufferAddress,
  OUT EFI_LBA                   *Lba,
  OUT UINTN                     *Offset
  )
{
  LBA_ENTRY   *LbaEntry;
  EFI_STATUS  Status;

  LbaEntry = NULL;

  Status = Buffer2LbaEntry (
            FvDevice,
            BufferAddress,
            &LbaEntry
            );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  *Lba    = LbaEntry->LbaIndex;
  *Offset = (UINTN) BufferAddress - (UINTN) LbaEntry->StartingAddress;

  return EFI_SUCCESS;
}

/**
  Check if a block of buffer is erased.

  @param  ErasePolarity  Erase polarity attribute of the firmware volume
  @param  Buffer         The buffer to be checked
  @param  BufferSize     Size of the buffer in bytes

  @retval TRUE           The block of buffer is erased
  @retval FALSE          The block of buffer is not erased

**/
BOOLEAN
IsBufferErased (
  IN UINT8    ErasePolarity,
  IN UINT8    *Buffer,
  IN UINTN    BufferSize
  )
{
  UINTN Count;
  UINT8 EraseByte;

  if (ErasePolarity == 1) {
    EraseByte = 0xFF;
  } else {
    EraseByte = 0;
  }

  for (Count = 0; Count < BufferSize; Count++) {
    if (Buffer[Count] != EraseByte) {
      return FALSE;
    }
  }

  return TRUE;
}

/**
  Verify checksum of the firmware volume header.

  @param  FvHeader       Points to the firmware volume header to be checked

  @retval TRUE           Checksum verification passed
  @retval FALSE          Checksum verification failed

**/
BOOLEAN
VerifyFvHeaderChecksum (
  IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader
  )
{
  UINT16  Checksum;

  Checksum = CalculateSum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength);

  if (Checksum == 0) {
    return TRUE;
  } else {
    return FALSE;
  }
}

/**
  Verify checksum of the FFS file header.

  @param  FfsHeader      Points to the FFS file header to be checked

  @retval TRUE           Checksum verification passed
  @retval FALSE          Checksum verification failed

**/
BOOLEAN
VerifyHeaderChecksum (
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  UINT8 HeaderChecksum;

  HeaderChecksum = CalculateSum8 ((UINT8 *) FfsHeader, sizeof (EFI_FFS_FILE_HEADER));
  HeaderChecksum = (UINT8) (HeaderChecksum - FfsHeader->State - FfsHeader->IntegrityCheck.Checksum.File);

  if (HeaderChecksum == 0) {
    return TRUE;
  } else {
    return FALSE;
  }
}

/**
  Verify checksum of the FFS file data.

  @param  FfsHeader      Points to the FFS file header to be checked

  @retval TRUE           Checksum verification passed
  @retval FALSE          Checksum verification failed

**/
BOOLEAN
VerifyFileChecksum (
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  UINT8                   FileChecksum;
  EFI_FV_FILE_ATTRIBUTES  Attributes;
  UINT32                  FileSize;

  Attributes = FfsHeader->Attributes;

  if ((Attributes & FFS_ATTRIB_CHECKSUM) != 0) {

    FileSize = *(UINT32 *) FfsHeader->Size & 0x00FFFFFF;

    //
    // Check checksum of FFS data
    //
    FileChecksum = CalculateSum8 ((UINT8 *) FfsHeader + sizeof (EFI_FFS_FILE_HEADER), FileSize - sizeof (EFI_FFS_FILE_HEADER));
    FileChecksum = (UINT8) (FileChecksum + FfsHeader->IntegrityCheck.Checksum.File);

    if (FileChecksum == 0) {
      return TRUE;
    } else {
      return FALSE;
    }

  } else {

    if (FfsHeader->IntegrityCheck.Checksum.File != FFS_FIXED_CHECKSUM) {
      return FALSE;
    } else {
      return TRUE;
    }
  }

}

/**
  Check if it's a valid FFS file header.

  @param  ErasePolarity  Erase polarity attribute of the firmware volume
  @param  FfsHeader      Points to the FFS file header to be checked

  @retval TRUE           Valid FFS file header
  @retval FALSE          Invalid FFS file header

**/
BOOLEAN
IsValidFFSHeader (
  IN UINT8                ErasePolarity,
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  EFI_FFS_FILE_STATE  FileState;

  //
  // Check if it is a free space
  //
  if (IsBufferErased (
        ErasePolarity,
        (UINT8 *) FfsHeader,
        sizeof (EFI_FFS_FILE_HEADER)
        )) {
    return FALSE;
  }

  FileState = GetFileState (ErasePolarity, FfsHeader);

  switch (FileState) {
  case EFI_FILE_HEADER_CONSTRUCTION:
    //
    // fall through
    //
  case EFI_FILE_HEADER_INVALID:
    return FALSE;

  case EFI_FILE_HEADER_VALID:
    //
    // fall through
    //
  case EFI_FILE_DATA_VALID:
    //
    // fall through
    //
  case EFI_FILE_MARKED_FOR_UPDATE:
    //
    // fall through
    //
  case EFI_FILE_DELETED:
    //
    // Here we need to verify header checksum
    //
    if (!VerifyHeaderChecksum (FfsHeader)) {
      return FALSE;
    }
    break;

  default:
    //
    // return
    //
    return FALSE;
  }

  return TRUE;
}

/**
  Get next possible of Firmware File System Header.

  @param  ErasePolarity  Erase polarity attribute of the firmware volume
  @param  FfsHeader      Points to the FFS file header to be skipped.

  @return  Pointer to next FFS header.

**/
EFI_PHYSICAL_ADDRESS
GetNextPossibleFileHeader (
  IN UINT8                ErasePolarity,
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  UINT32  FileLength;
  UINT32  SkipLength;

  if (!IsValidFFSHeader (ErasePolarity, FfsHeader)) {
    //
    // Skip this header
    //
    return (EFI_PHYSICAL_ADDRESS) (UINTN) FfsHeader + sizeof (EFI_FFS_FILE_HEADER);
  }

  FileLength = *(UINT32 *) FfsHeader->Size & 0x00FFFFFF;

  //
  // Since FileLength is not multiple of 8, we need skip some bytes
  // to get next possible header
  //
  SkipLength = FileLength;
  while ((SkipLength & 0x07) != 0) {
    SkipLength++;
  }

  return (EFI_PHYSICAL_ADDRESS) (UINTN) FfsHeader + SkipLength;
}

/**
  Search FFS file with the same FFS name in FV Cache.

  @param  FvDevice     Cached FV image.
  @param  FfsHeader    Points to the FFS file header to be skipped.
  @param  StateBit     FFS file state bit to be checked.

  @return  Pointer to next found FFS header. NULL will return if no found.

**/
EFI_FFS_FILE_HEADER *
DuplicateFileExist (
  IN FV_DEVICE            *FvDevice,
  IN EFI_FFS_FILE_HEADER  *FfsHeader,
  IN EFI_FFS_FILE_STATE   StateBit
  )
{
  UINT8               *Ptr;
  EFI_FFS_FILE_HEADER *NextFfsFile;

  //
  // Search duplicate file, not from the beginning of FV,
  // just search the next ocurrence of this file
  //
  NextFfsFile = FfsHeader;

  do {
    Ptr = (UINT8 *) PHYSICAL_ADDRESS_TO_POINTER (
                      GetNextPossibleFileHeader (FvDevice->ErasePolarity,
                      NextFfsFile)
                      );
    NextFfsFile = (EFI_FFS_FILE_HEADER *) Ptr;

    if ((UINT8 *) PHYSICAL_ADDRESS_TO_POINTER (FvDevice->CachedFv) + FvDevice->FwVolHeader->FvLength - Ptr <
        sizeof (EFI_FFS_FILE_HEADER)
          ) {
      break;
    }

    if (!IsValidFFSHeader (FvDevice->ErasePolarity, NextFfsFile)) {
      continue;
    }

    if (!VerifyFileChecksum (NextFfsFile)) {
      continue;
    }

    if (CompareGuid (&NextFfsFile->Name, &FfsHeader->Name)) {
      if (GetFileState (FvDevice->ErasePolarity, NextFfsFile) == StateBit) {
        return NextFfsFile;
      }
    }
  } while (Ptr < (UINT8 *) PHYSICAL_ADDRESS_TO_POINTER (FvDevice->CachedFv) + FvDevice->FwVolHeader->FvLength);

  return NULL;
}

/**
  Change FFS file header state and write to FV.

  @param  FvDevice     Cached FV image.
  @param  FfsHeader    Points to the FFS file header to be updated.
  @param  State        FFS file state to be set.

  @retval EFI_SUCCESS  File state is writen into FV.
  @retval others       File state can't be writen into FV.

**/
EFI_STATUS
UpdateHeaderBit (
  IN FV_DEVICE            *FvDevice,
  IN EFI_FFS_FILE_HEADER  *FfsHeader,
  IN EFI_FFS_FILE_STATE   State
  )
{
  EFI_STATUS  Status;
  EFI_LBA     Lba;
  UINTN       Offset;
  UINTN       NumBytesWritten;

  Lba    = 0;
  Offset = 0;

  SetFileState (State, FfsHeader);

  Buffer2Lba (
    FvDevice,
    (EFI_PHYSICAL_ADDRESS) (UINTN) (&FfsHeader->State),
    &Lba,
    &Offset
    );
  //
  // Write the state byte into FV
  //
  NumBytesWritten = sizeof (EFI_FFS_FILE_STATE);
  Status = FvDevice->Fvb->Write (
                            FvDevice->Fvb,
                            Lba,
                            Offset,
                            &NumBytesWritten,
                            &FfsHeader->State
                            );
  return Status;
}

/**
  Check if it's a valid FFS file.
  Here we are sure that it has a valid FFS file header since we must call IsValidFfsHeader() first.

  @param  FvDevice       Cached FV image.
  @param  FfsHeader      Points to the FFS file to be checked

  @retval TRUE           Valid FFS file
  @retval FALSE          Invalid FFS file

**/
BOOLEAN
IsValidFFSFile (
  IN FV_DEVICE            *FvDevice,
  IN EFI_FFS_FILE_HEADER  *FfsHeader
  )
{
  EFI_FFS_FILE_STATE  FileState;
  UINT8               ErasePolarity;

  ErasePolarity = FvDevice->ErasePolarity;

  FileState     = GetFileState (ErasePolarity, FfsHeader);

  switch (FileState) {
  case EFI_FILE_DATA_VALID:
    if (!VerifyFileChecksum (FfsHeader)) {
      return FALSE;
    }

    if (FfsHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {
      break;
    }
    //
    // Check if there is another duplicated file with the EFI_FILE_DATA_VALID
    //
    if (DuplicateFileExist (FvDevice, FfsHeader, EFI_FILE_DATA_VALID) != NULL) {
      return FALSE;
    }

    break;

  case EFI_FILE_MARKED_FOR_UPDATE:
    if (!VerifyFileChecksum (FfsHeader)) {
      return FALSE;
    }

    if (FfsHeader->Type == EFI_FV_FILETYPE_FFS_PAD) {
      //
      // since its data area is not unperturbed, it cannot be reclaimed,
      // marked it as deleted
      //
      UpdateHeaderBit (FvDevice, FfsHeader, EFI_FILE_DELETED);
      return TRUE;

    } else if (DuplicateFileExist (FvDevice, FfsHeader, EFI_FILE_DATA_VALID) != NULL) {
      //
      // Here the found file is more recent than this file,
      // mark it as deleted
      //
      UpdateHeaderBit (FvDevice, FfsHeader, EFI_FILE_DELETED);
      return TRUE;

    } else {
      return TRUE;
    }

    break;

  case EFI_FILE_DELETED:
    if (!VerifyFileChecksum (FfsHeader)) {
      return FALSE;
    }

    break;

  default:
    return FALSE;
  }

  return TRUE;
}

/**
  Locate the first file in FV.

  @param  FvDevice  Cached FV image.
  @param  FirstFile Points to the got first FFS file header.

  @retval EFI_NOT_FOUND   No FFS file is found in FV.
  @retval EFI_SUCCESS     The first FFS file is got.

**/
EFI_STATUS
FvLocateFirstFile (
  IN     FV_DEVICE              *FvDevice,
  OUT EFI_FFS_FILE_HEADER       **FirstFile
  )
{
  FFS_FILE_LIST_ENTRY *TmpFileList;
  LIST_ENTRY      *Link;

  Link = FvDevice->FfsFileListHeader.ForwardLink;

  if (Link == &FvDevice->FfsFileListHeader) {
    return EFI_NOT_FOUND;
  }

  TmpFileList = (FFS_FILE_LIST_ENTRY *) Link;
  *FirstFile  = (EFI_FFS_FILE_HEADER *) TmpFileList->FfsHeader;

  return EFI_SUCCESS;
}