Coverage Report

Created: 2024-10-17 06:29

/src/hbfa-fl/HBFA/UefiHostFuzzTestCasePkg/TestStub/DiskStubLib/DiskStubLib.c
Line
Count
Source (jump to first uncovered line)
1
/** @file
2
3
Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
4
SPDX-License-Identifier: BSD-2-Clause-Patent
5
6
**/
7
8
#include <Uefi.h>
9
#include <Protocol/BlockIo.h>
10
#include <Protocol/DiskIo.h>
11
12
#include <Library/BaseLib.h>
13
#include <Library/DebugLib.h>
14
#include <Library/BaseMemoryLib.h>
15
#include <Library/MemoryAllocationLib.h>
16
17
typedef struct {
18
  UINTN                   Signature;
19
  EFI_BLOCK_IO_PROTOCOL   BlockIo;
20
  EFI_BLOCK_IO_MEDIA      Media;
21
  EFI_DISK_IO_PROTOCOL    DiskIo;
22
  UINT64                  StartingAddr;
23
  UINT64                  Size;
24
} UDF_TEST_PRIVATE;
25
26
UDF_TEST_PRIVATE *UdfTestPrivate = NULL;
27
28
2.69k
#define DATA_BUFFER_BLOCK_NUM           (64)
29
30
#define UDF_TEST_PRIVATE_SIGNATURE          SIGNATURE_32 ('U', 'D', 'F', 'T')
31
32
6.21k
#define UDF_TEST_PRIVATE_FROM_BLOCK_IO(a)      CR (a, UDF_TEST_PRIVATE, BlockIo, UDF_TEST_PRIVATE_SIGNATURE)
33
2.69k
#define UDF_TEST_PRIVATE_FROM_DISK_IO(a)       CR (a, UDF_TEST_PRIVATE, DiskIo, UDF_TEST_PRIVATE_SIGNATURE)
34
35
/**
36
  Reset the Block Device.
37
38
  @param  This                 Indicates a pointer to the calling context.
39
  @param  ExtendedVerification Driver may perform diagnostics on reset.
40
41
  @retval EFI_SUCCESS          The device was reset.
42
  @retval EFI_DEVICE_ERROR     The device is not functioning properly and could
43
                               not be reset.
44
45
**/
46
EFI_STATUS
47
EFIAPI
48
Reset (
49
  IN EFI_BLOCK_IO_PROTOCOL          *This,
50
  IN BOOLEAN                        ExtendedVerification
51
  )
52
0
{
53
0
  return EFI_SUCCESS;
54
0
}
55
56
/**
57
  Read BufferSize bytes from Lba into Buffer.
58
59
  @param  This       Indicates a pointer to the calling context.
60
  @param  MediaId    Id of the media, changes every time the media is replaced.
61
  @param  Lba        The starting Logical Block Address to read from
62
  @param  BufferSize Size of Buffer, must be a multiple of device block size.
63
  @param  Buffer     A pointer to the destination buffer for the data. The caller is
64
                     responsible for either having implicit or explicit ownership of the buffer.
65
66
  @retval EFI_SUCCESS           The data was read correctly from the device.
67
  @retval EFI_DEVICE_ERROR      The device reported an error while performing the read.
68
  @retval EFI_NO_MEDIA          There is no media in the device.
69
  @retval EFI_MEDIA_CHANGED     The MediaId does not matched the current device.
70
  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
71
  @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
72
                                or the buffer is not on proper alignment.
73
74
**/
75
EFI_STATUS
76
EFIAPI
77
ReadBlocks (
78
  IN EFI_BLOCK_IO_PROTOCOL          *This,
79
  IN UINT32                         MediaId,
80
  IN EFI_LBA                        Lba,
81
  IN UINTN                          BufferSize,
82
  OUT VOID                          *Buffer
83
  )
84
6.21k
{
85
6.21k
  UDF_TEST_PRIVATE                *PrivateData;
86
6.21k
  UINTN                           NumberOfBlocks;
87
88
6.21k
  PrivateData = UDF_TEST_PRIVATE_FROM_BLOCK_IO (This);
89
90
6.21k
  if (MediaId != PrivateData->Media.MediaId) {
91
0
    return EFI_MEDIA_CHANGED;
92
0
  }
93
94
6.21k
  if (Buffer == NULL) {
95
0
    return EFI_INVALID_PARAMETER;
96
0
  }
97
98
6.21k
  if (BufferSize == 0) {
99
0
    return EFI_SUCCESS;
100
0
  }
101
102
6.21k
  if ((BufferSize % PrivateData->Media.BlockSize) != 0) {
103
0
    return EFI_BAD_BUFFER_SIZE;
104
0
  }
105
106
6.21k
  if (Lba > PrivateData->Media.LastBlock) {
107
25
    return EFI_INVALID_PARAMETER;
108
25
  }
109
110
6.18k
  NumberOfBlocks = BufferSize / PrivateData->Media.BlockSize;
111
6.18k
  if ((Lba + NumberOfBlocks - 1) > PrivateData->Media.LastBlock) {
112
6
    return EFI_INVALID_PARAMETER;
113
6
  }
114
115
6.18k
  CopyMem (
116
6.18k
    Buffer,
117
6.18k
    (VOID *)(UINTN)(PrivateData->StartingAddr + MultU64x32 (Lba, PrivateData->Media.BlockSize)),
118
6.18k
    BufferSize
119
6.18k
    );
120
121
6.18k
  return EFI_SUCCESS;
122
6.18k
}
123
124
/**
125
  Write BufferSize bytes from Lba into Buffer.
126
127
  @param  This       Indicates a pointer to the calling context.
128
  @param  MediaId    The media ID that the write request is for.
129
  @param  Lba        The starting logical block address to be written. The caller is
130
                     responsible for writing to only legitimate locations.
131
  @param  BufferSize Size of Buffer, must be a multiple of device block size.
132
  @param  Buffer     A pointer to the source buffer for the data.
133
134
  @retval EFI_SUCCESS           The data was written correctly to the device.
135
  @retval EFI_WRITE_PROTECTED   The device can not be written to.
136
  @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.
137
  @retval EFI_NO_MEDIA          There is no media in the device.
138
  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
139
  @retval EFI_BAD_BUFFER_SIZE   The Buffer was not a multiple of the block size of the device.
140
  @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
141
                                or the buffer is not on proper alignment.
142
143
**/
144
EFI_STATUS
145
EFIAPI
146
WriteBlocks (
147
  IN EFI_BLOCK_IO_PROTOCOL          *This,
148
  IN UINT32                         MediaId,
149
  IN EFI_LBA                        Lba,
150
  IN UINTN                          BufferSize,
151
  IN VOID                           *Buffer
152
  )
153
0
{
154
0
  UDF_TEST_PRIVATE                *PrivateData;
155
0
  UINTN                           NumberOfBlocks;
156
157
0
  PrivateData = UDF_TEST_PRIVATE_FROM_BLOCK_IO (This);
158
159
0
  if (MediaId != PrivateData->Media.MediaId) {
160
0
    return EFI_MEDIA_CHANGED;
161
0
  }
162
163
0
  if (TRUE == PrivateData->Media.ReadOnly) {
164
0
    return EFI_WRITE_PROTECTED;
165
0
  }
166
167
0
  if (Buffer == NULL) {
168
0
    return EFI_INVALID_PARAMETER;
169
0
  }
170
171
0
  if (BufferSize == 0) {
172
0
    return EFI_SUCCESS;
173
0
  }
174
175
0
  if ((BufferSize % PrivateData->Media.BlockSize) != 0) {
176
0
    return EFI_BAD_BUFFER_SIZE;
177
0
  }
178
179
0
  if (Lba > PrivateData->Media.LastBlock) {
180
0
    return EFI_INVALID_PARAMETER;
181
0
  }
182
183
0
  NumberOfBlocks = BufferSize / PrivateData->Media.BlockSize;
184
0
  if ((Lba + NumberOfBlocks - 1) > PrivateData->Media.LastBlock) {
185
0
    return EFI_INVALID_PARAMETER;
186
0
  }
187
188
0
  CopyMem (
189
0
    (VOID *)(UINTN)(PrivateData->StartingAddr + MultU64x32 (Lba, PrivateData->Media.BlockSize)),
190
0
    Buffer,
191
0
    BufferSize
192
0
    );
193
194
0
  return EFI_SUCCESS;
195
0
}
196
197
/**
198
  Flush the Block Device.
199
200
  @param  This              Indicates a pointer to the calling context.
201
202
  @retval EFI_SUCCESS       All outstanding data was written to the device
203
  @retval EFI_DEVICE_ERROR  The device reported an error while writting back the data
204
  @retval EFI_NO_MEDIA      There is no media in the device.
205
206
**/
207
EFI_STATUS
208
EFIAPI
209
FlushBlocks (
210
  IN EFI_BLOCK_IO_PROTOCOL  *This
211
  )
212
0
{
213
0
  return EFI_SUCCESS;
214
0
}
215
216
/**
217
  Read BufferSize bytes from Offset into Buffer.
218
219
  @param  This                  Protocol instance pointer.
220
  @param  MediaId               Id of the media, changes every time the media is replaced.
221
  @param  Offset                The starting byte offset to read from
222
  @param  BufferSize            Size of Buffer
223
  @param  Buffer                Buffer containing read data
224
225
  @retval EFI_SUCCESS           The data was read correctly from the device.
226
  @retval EFI_DEVICE_ERROR      The device reported an error while performing the read.
227
  @retval EFI_NO_MEDIA          There is no media in the device.
228
  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
229
  @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not
230
                                valid for the device.
231
232
**/
233
EFI_STATUS
234
EFIAPI
235
ReadDisk (
236
  IN EFI_DISK_IO_PROTOCOL         *This,
237
  IN UINT32                       MediaId,
238
  IN UINT64                       Offset,
239
  IN UINTN                        BufferSize,
240
  OUT VOID                        *Buffer
241
  )
242
2.69k
{
243
2.69k
  EFI_STATUS            Status;
244
2.69k
  UDF_TEST_PRIVATE      *Private;
245
2.69k
  EFI_BLOCK_IO_PROTOCOL *BlockIo;
246
2.69k
  EFI_BLOCK_IO_MEDIA    *Media;
247
2.69k
  UINT32                BlockSize;
248
2.69k
  UINT64                Lba;
249
2.69k
  UINT64                OverRunLba;
250
2.69k
  UINT32                UnderRun;
251
2.69k
  UINT32                OverRun;
252
2.69k
  BOOLEAN               TransactionComplete;
253
2.69k
  UINTN                 WorkingBufferSize;
254
2.69k
  UINT8                 *WorkingBuffer;
255
2.69k
  UINTN                 Length;
256
2.69k
  UINT8                 *Data;
257
2.69k
  UINT8                 *PreData;
258
2.69k
  UINTN                 IsBufferAligned;
259
2.69k
  UINTN                 DataBufferSize;
260
2.69k
  BOOLEAN               LastRead;
261
262
2.69k
  Private   = UDF_TEST_PRIVATE_FROM_DISK_IO (This);
263
264
2.69k
  BlockIo   = &Private->BlockIo;
265
2.69k
  Media     = BlockIo->Media;
266
2.69k
  BlockSize = Media->BlockSize;
267
268
2.69k
  if (Media->MediaId != MediaId) {
269
0
    return EFI_MEDIA_CHANGED;
270
0
  }
271
272
2.69k
  WorkingBuffer     = Buffer;
273
2.69k
  WorkingBufferSize = BufferSize;
274
275
  //
276
  // Allocate a temporary buffer for operation
277
  //
278
2.69k
  DataBufferSize = BlockSize * DATA_BUFFER_BLOCK_NUM;
279
280
2.69k
  if (Media->IoAlign > 1) {
281
0
    PreData = AllocatePool (DataBufferSize + Media->IoAlign);
282
0
    Data    = PreData - ((UINTN) PreData & (Media->IoAlign - 1)) + Media->IoAlign;
283
2.69k
  } else {
284
2.69k
    PreData = AllocatePool (DataBufferSize);
285
2.69k
    Data    = PreData;
286
2.69k
  }
287
288
2.69k
  if (PreData == NULL) {
289
0
    return EFI_OUT_OF_RESOURCES;
290
0
  }
291
292
2.69k
  Lba                 = DivU64x32Remainder (Offset, BlockSize, &UnderRun);
293
294
2.69k
  Length              = BlockSize - UnderRun;
295
2.69k
  TransactionComplete = FALSE;
296
297
2.69k
  Status              = EFI_SUCCESS;
298
2.69k
  if (UnderRun != 0) {
299
    //
300
    // Offset starts in the middle of an Lba, so read the entire block.
301
    //
302
0
    Status = BlockIo->ReadBlocks (
303
0
                        BlockIo,
304
0
                        MediaId,
305
0
                        Lba,
306
0
                        BlockSize,
307
0
                        Data
308
0
                        );
309
310
0
    if (EFI_ERROR (Status)) {
311
0
      goto Done;
312
0
    }
313
314
0
    if (Length > BufferSize) {
315
0
      Length              = BufferSize;
316
0
      TransactionComplete = TRUE;
317
0
    }
318
319
0
    CopyMem (WorkingBuffer, Data + UnderRun, Length);
320
321
0
    WorkingBuffer += Length;
322
323
0
    WorkingBufferSize -= Length;
324
0
    if (WorkingBufferSize == 0) {
325
0
      goto Done;
326
0
    }
327
328
0
    Lba += 1;
329
0
  }
330
331
2.69k
  OverRunLba = Lba + DivU64x32Remainder (WorkingBufferSize, BlockSize, &OverRun);
332
333
2.69k
  if (!TransactionComplete && WorkingBufferSize >= BlockSize) {
334
    //
335
    // If the DiskIo maps directly to a BlockIo device do the read.
336
    //
337
1.06k
    if (OverRun != 0) {
338
18
      WorkingBufferSize -= OverRun;
339
18
    }
340
    //
341
    // Check buffer alignment
342
    //
343
1.06k
    IsBufferAligned = (UINTN) WorkingBuffer & (UINTN) (Media->IoAlign - 1);
344
345
1.06k
    if (Media->IoAlign <= 1 || IsBufferAligned == 0) {
346
      //
347
      // Alignment is satisfied, so read them together
348
      //
349
1.06k
      Status = BlockIo->ReadBlocks (
350
1.06k
                          BlockIo,
351
1.06k
                          MediaId,
352
1.06k
                          Lba,
353
1.06k
                          WorkingBufferSize,
354
1.06k
                          WorkingBuffer
355
1.06k
                          );
356
357
1.06k
      if (EFI_ERROR (Status)) {
358
30
        goto Done;
359
30
      }
360
361
1.03k
      WorkingBuffer += WorkingBufferSize;
362
363
1.03k
    } else {
364
      //
365
      // Use the allocated buffer instead of the original buffer
366
      // to avoid alignment issue.
367
      // Here, the allocated buffer (8-byte align) can satisfy the alignment
368
      //
369
0
      LastRead = FALSE;
370
0
      do {
371
0
        if (WorkingBufferSize <= DataBufferSize) {
372
          //
373
          // It is the last calling to readblocks in this loop
374
          //
375
0
          DataBufferSize  = WorkingBufferSize;
376
0
          LastRead        = TRUE;
377
0
        }
378
379
0
        Status = BlockIo->ReadBlocks (
380
0
                            BlockIo,
381
0
                            MediaId,
382
0
                            Lba,
383
0
                            DataBufferSize,
384
0
                            Data
385
0
                            );
386
0
        if (EFI_ERROR (Status)) {
387
0
          goto Done;
388
0
        }
389
390
0
        CopyMem (WorkingBuffer, Data, DataBufferSize);
391
0
        WorkingBufferSize -= DataBufferSize;
392
0
        WorkingBuffer += DataBufferSize;
393
0
        Lba += DATA_BUFFER_BLOCK_NUM;
394
0
      } while (!LastRead);
395
0
    }
396
1.06k
  }
397
398
2.66k
  if (!TransactionComplete && OverRun != 0) {
399
    //
400
    // Last read is not a complete block.
401
    //
402
1.64k
    Status = BlockIo->ReadBlocks (
403
1.64k
                        BlockIo,
404
1.64k
                        MediaId,
405
1.64k
                        OverRunLba,
406
1.64k
                        BlockSize,
407
1.64k
                        Data
408
1.64k
                        );
409
410
1.64k
    if (EFI_ERROR (Status)) {
411
1
      goto Done;
412
1
    }
413
414
1.64k
    CopyMem (WorkingBuffer, Data, OverRun);
415
1.64k
  }
416
417
2.69k
Done:
418
2.69k
  if (PreData != NULL) {
419
2.69k
    FreePool (PreData);
420
2.69k
  }
421
422
2.69k
  return Status;
423
2.66k
}
424
425
/**
426
  Writes a specified number of bytes to a device.
427
428
  @param  This       Indicates a pointer to the calling context.
429
  @param  MediaId    ID of the medium to be written.
430
  @param  Offset     The starting byte offset on the logical block I/O device to write.
431
  @param  BufferSize The size in bytes of Buffer. The number of bytes to write to the device.
432
  @param  Buffer     A pointer to the buffer containing the data to be written.
433
434
  @retval EFI_SUCCESS           The data was written correctly to the device.
435
  @retval EFI_WRITE_PROTECTED   The device can not be written to.
436
  @retval EFI_DEVICE_ERROR      The device reported an error while performing the write.
437
  @retval EFI_NO_MEDIA          There is no media in the device.
438
  @retval EFI_MEDIA_CHNAGED     The MediaId does not matched the current device.
439
  @retval EFI_INVALID_PARAMETER The write request contains device addresses that are not
440
                                 valid for the device.
441
442
**/
443
EFI_STATUS
444
EFIAPI
445
WriteDisk (
446
  IN EFI_DISK_IO_PROTOCOL         *This,
447
  IN UINT32                       MediaId,
448
  IN UINT64                       Offset,
449
  IN UINTN                        BufferSize,
450
  IN VOID                         *Buffer
451
  )
452
0
{
453
0
  EFI_STATUS            Status;
454
0
  UDF_TEST_PRIVATE      *Private;
455
0
  EFI_BLOCK_IO_PROTOCOL *BlockIo;
456
0
  EFI_BLOCK_IO_MEDIA    *Media;
457
0
  UINT32                BlockSize;
458
0
  UINT64                Lba;
459
0
  UINT64                OverRunLba;
460
0
  UINT32                UnderRun;
461
0
  UINT32                OverRun;
462
0
  BOOLEAN               TransactionComplete;
463
0
  UINTN                 WorkingBufferSize;
464
0
  UINT8                 *WorkingBuffer;
465
0
  UINTN                 Length;
466
0
  UINT8                 *Data;
467
0
  UINT8                 *PreData;
468
0
  UINTN                 IsBufferAligned;
469
0
  UINTN                 DataBufferSize;
470
0
  BOOLEAN               LastWrite;
471
472
0
  Private   = UDF_TEST_PRIVATE_FROM_DISK_IO (This);
473
474
0
  BlockIo   = &Private->BlockIo;
475
0
  Media     = BlockIo->Media;
476
0
  BlockSize = Media->BlockSize;
477
478
0
  if (Media->ReadOnly) {
479
0
    return EFI_WRITE_PROTECTED;
480
0
  }
481
482
0
  if (Media->MediaId != MediaId) {
483
0
    return EFI_MEDIA_CHANGED;
484
0
  }
485
486
0
  DataBufferSize = BlockSize * DATA_BUFFER_BLOCK_NUM;
487
488
0
  if (Media->IoAlign > 1) {
489
0
    PreData = AllocatePool (DataBufferSize + Media->IoAlign);
490
0
    Data    = PreData - ((UINTN) PreData & (Media->IoAlign - 1)) + Media->IoAlign;
491
0
  } else {
492
0
    PreData = AllocatePool (DataBufferSize);
493
0
    Data    = PreData;
494
0
  }
495
496
0
  if (PreData == NULL) {
497
0
    return EFI_OUT_OF_RESOURCES;
498
0
  }
499
500
0
  WorkingBuffer       = Buffer;
501
0
  WorkingBufferSize   = BufferSize;
502
503
0
  Lba                 = DivU64x32Remainder (Offset, BlockSize, &UnderRun);
504
505
0
  Length              = BlockSize - UnderRun;
506
0
  TransactionComplete = FALSE;
507
508
0
  Status              = EFI_SUCCESS;
509
0
  if (UnderRun != 0) {
510
    //
511
    // Offset starts in the middle of an Lba, so do read modify write.
512
    //
513
0
    Status = BlockIo->ReadBlocks (
514
0
                        BlockIo,
515
0
                        MediaId,
516
0
                        Lba,
517
0
                        BlockSize,
518
0
                        Data
519
0
                        );
520
521
0
    if (EFI_ERROR (Status)) {
522
0
      goto Done;
523
0
    }
524
525
0
    if (Length > BufferSize) {
526
0
      Length              = BufferSize;
527
0
      TransactionComplete = TRUE;
528
0
    }
529
530
0
    CopyMem (Data + UnderRun, WorkingBuffer, Length);
531
532
0
    Status = BlockIo->WriteBlocks (
533
0
                        BlockIo,
534
0
                        MediaId,
535
0
                        Lba,
536
0
                        BlockSize,
537
0
                        Data
538
0
                        );
539
0
    if (EFI_ERROR (Status)) {
540
0
      goto Done;
541
0
    }
542
543
0
    WorkingBuffer += Length;
544
0
    WorkingBufferSize -= Length;
545
0
    if (WorkingBufferSize == 0) {
546
0
      goto Done;
547
0
    }
548
549
0
    Lba += 1;
550
0
  }
551
552
0
  OverRunLba = Lba + DivU64x32Remainder (WorkingBufferSize, BlockSize, &OverRun);
553
554
0
  if (!TransactionComplete && WorkingBufferSize >= BlockSize) {
555
    //
556
    // If the DiskIo maps directly to a BlockIo device do the write.
557
    //
558
0
    if (OverRun != 0) {
559
0
      WorkingBufferSize -= OverRun;
560
0
    }
561
    //
562
    // Check buffer alignment
563
    //
564
0
    IsBufferAligned = (UINTN) WorkingBuffer & (UINTN) (Media->IoAlign - 1);
565
566
0
    if (Media->IoAlign <= 1 || IsBufferAligned == 0) {
567
      //
568
      // Alignment is satisfied, so write them together
569
      //
570
0
      Status = BlockIo->WriteBlocks (
571
0
                          BlockIo,
572
0
                          MediaId,
573
0
                          Lba,
574
0
                          WorkingBufferSize,
575
0
                          WorkingBuffer
576
0
                          );
577
578
0
      if (EFI_ERROR (Status)) {
579
0
        goto Done;
580
0
      }
581
582
0
      WorkingBuffer += WorkingBufferSize;
583
584
0
    } else {
585
      //
586
      // The buffer parameter is not aligned with the request
587
      // So use the allocated instead.
588
      // It can fit almost all the cases.
589
      //
590
0
      LastWrite = FALSE;
591
0
      do {
592
0
        if (WorkingBufferSize <= DataBufferSize) {
593
          //
594
          // It is the last calling to writeblocks in this loop
595
          //
596
0
          DataBufferSize  = WorkingBufferSize;
597
0
          LastWrite       = TRUE;
598
0
        }
599
600
0
        CopyMem (Data, WorkingBuffer, DataBufferSize);
601
0
        Status = BlockIo->WriteBlocks (
602
0
                            BlockIo,
603
0
                            MediaId,
604
0
                            Lba,
605
0
                            DataBufferSize,
606
0
                            Data
607
0
                            );
608
0
        if (EFI_ERROR (Status)) {
609
0
          goto Done;
610
0
        }
611
612
0
        WorkingBufferSize -= DataBufferSize;
613
0
        WorkingBuffer += DataBufferSize;
614
0
        Lba += DATA_BUFFER_BLOCK_NUM;
615
0
      } while (!LastWrite);
616
0
    }
617
0
  }
618
619
0
  if (!TransactionComplete && OverRun != 0) {
620
    //
621
    // Last bit is not a complete block, so do a read modify write.
622
    //
623
0
    Status = BlockIo->ReadBlocks (
624
0
                        BlockIo,
625
0
                        MediaId,
626
0
                        OverRunLba,
627
0
                        BlockSize,
628
0
                        Data
629
0
                        );
630
631
0
    if (EFI_ERROR (Status)) {
632
0
      goto Done;
633
0
    }
634
635
0
    CopyMem (Data, WorkingBuffer, OverRun);
636
637
0
    Status = BlockIo->WriteBlocks (
638
0
                        BlockIo,
639
0
                        MediaId,
640
0
                        OverRunLba,
641
0
                        BlockSize,
642
0
                        Data
643
0
                        );
644
0
    if (EFI_ERROR (Status)) {
645
0
      goto Done;
646
0
    }
647
0
  }
648
649
0
Done:
650
0
  if (PreData != NULL) {
651
0
    FreePool (PreData);
652
0
  }
653
654
0
  return Status;
655
0
}
656
657
UDF_TEST_PRIVATE mUdfTestPrivate = {
658
  UDF_TEST_PRIVATE_SIGNATURE,
659
  {
660
    EFI_BLOCK_IO_PROTOCOL_REVISION,
661
    &mUdfTestPrivate.Media,
662
    Reset,
663
    ReadBlocks,
664
    WriteBlocks,
665
    FlushBlocks,
666
  },
667
  {
668
    0,     // MediaId;
669
    FALSE, // RemovableMedia;
670
    TRUE,  // MediaPresent;
671
    FALSE, // LogicalPartition;
672
    FALSE, // ReadOnly;
673
    FALSE, // WriteCaching;
674
    0,     // BlockSize;
675
    1,     // IoAlign;
676
    0,     // LastBlock;
677
    0, // LowestAlignedLba;
678
    0, // LogicalBlocksPerPhysicalBlock;
679
    0, // OptimalTransferLengthGranularity;
680
  },
681
  {
682
    EFI_DISK_IO_PROTOCOL_REVISION,
683
    ReadDisk,
684
    WriteDisk,
685
  },
686
  0, // StartingAddr
687
  0, // Size
688
};
689
690
EFI_STATUS
691
EFIAPI
692
DiskStubInitialize (
693
  IN  VOID                   *Buffer,
694
  IN  UINTN                  BufferSize,
695
  IN  UINT32                 BlockSize,
696
  IN  UINT32                 IoAlign,
697
  OUT EFI_BLOCK_IO_PROTOCOL  **BlockIo,
698
  OUT EFI_DISK_IO_PROTOCOL   **DiskIo
699
  )
700
476
{
701
476
  UdfTestPrivate = AllocatePool (sizeof(UDF_TEST_PRIVATE));
702
476
  CopyMem (UdfTestPrivate, &mUdfTestPrivate, sizeof(UDF_TEST_PRIVATE));
703
704
476
  UdfTestPrivate->BlockIo.Media = &UdfTestPrivate->Media;
705
706
476
  UdfTestPrivate->StartingAddr = (UINTN)Buffer;
707
476
  UdfTestPrivate->Size         = BufferSize;
708
709
476
  UdfTestPrivate->Media.IoAlign   = IoAlign;
710
476
  UdfTestPrivate->Media.BlockSize = BlockSize;
711
476
  UdfTestPrivate->Media.LastBlock = (BufferSize + BlockSize - 1) / BlockSize - 1;
712
713
476
  if (BlockIo != NULL) {
714
476
    *BlockIo = &UdfTestPrivate->BlockIo;
715
476
  }
716
476
  if (DiskIo != NULL) {
717
476
    *DiskIo = &UdfTestPrivate->DiskIo;
718
476
  }
719
720
476
  return EFI_SUCCESS;
721
476
}
722
723
EFI_STATUS
724
EFIAPI
725
DiskStubDestory (
726
  VOID
727
  )
728
476
{
729
476
  if (UdfTestPrivate != NULL) {
730
476
    FreePool(UdfTestPrivate);
731
476
    UdfTestPrivate = NULL;
732
476
  }
733
  
734
476
  return EFI_SUCCESS;
735
476
}