summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Library/GptLib/UnitTest/GptLibUnitTestCommon.h
blob: 677dbc4c4f37738a411fcc46443eef718e9e7963 (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
/** @file
  Shared mock disk and GPT construction helpers used by the GptLib
  host-based unit tests.

  Copyright (c) 2026, SUSE LLC. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#pragma once

#include <Uefi.h>
#include <Protocol/BlockIo.h>
#include <Protocol/DiskIo.h>
#include <Library/GptLib.h>

#define SECTOR_SIZE            512U
#define DISK_SECTORS           4096U
#define DISK_IMAGE_SIZE        (DISK_SECTORS * SECTOR_SIZE)
#define LAST_LBA               (DISK_SECTORS - 1U)
#define NUM_PARTITION_ENTRIES  128U
#define PARTITION_ENTRY_SIZE   128U
#define PART_ARRAY_SIZE        (NUM_PARTITION_ENTRIES * PARTITION_ENTRY_SIZE)
#define FIRST_USABLE_LBA       34U
#define LAST_USABLE_LBA        (DISK_SECTORS - 34U)

//
// The only GPT header revision defined by the UEFI specification, and the
// minimum on-disk header size (through PartitionEntryArrayCRC32).
//
#define TEST_GPT_REVISION_V1      0x00010000U
#define TEST_GPT_HEADER_MIN_SIZE  92U

extern UINT8  mDiskImage[DISK_IMAGE_SIZE];

extern EFI_BLOCK_IO_MEDIA     mBlockIoMedia;
extern EFI_BLOCK_IO_PROTOCOL  mBlockIo;
extern EFI_DISK_IO_PROTOCOL   mDiskIo;
extern BOOLEAN                mWriteProtected;

extern CONST EFI_GUID  mEspTypeGuid;
extern CONST EFI_GUID  mPartitionGuid1;
extern CONST EFI_GUID  mPartitionGuid2;

UINT8 *
GetDiskLba (
  IN EFI_LBA  Lba
  );

EFI_PARTITION_TABLE_HEADER *
GetPrimaryHeader (
  VOID
  );

VOID
UpdateGptHeaderCrc (
  IN OUT EFI_PARTITION_TABLE_HEADER  *Header
  );

VOID
FillPartitionEntry (
  IN OUT EFI_PARTITION_ENTRY  *Entry,
  IN CONST EFI_GUID           *UniquePartitionGuid,
  IN EFI_LBA                  StartingLba,
  IN EFI_LBA                  EndingLba
  );

/**
  Write a self-consistent GPT table (header plus partition entry array) to the
  mock disk at the requested locations, computing correct header and
  entry-array CRC32 values.

  @param[in]  HeaderLba      LBA where the GPT header is written. Also stored
                             in the header's MyLBA field.
  @param[in]  AlternateLba   Value stored in the header's AlternateLBA field.
  @param[in]  EntryArrayLba  LBA where the partition entry array is written.
  @param[in]  NumEntries     NumberOfPartitionEntries value.
  @param[in]  EntrySize      SizeOfPartitionEntry value.
  @param[in]  NumPartitions  Number of non-empty partition entries to populate.
**/
VOID
WriteGptTableAt (
  IN EFI_LBA  HeaderLba,
  IN EFI_LBA  AlternateLba,
  IN EFI_LBA  EntryArrayLba,
  IN UINT32   NumEntries,
  IN UINT32   EntrySize,
  IN UINT32   NumPartitions
  );

/**
  Reset the mock disk to a single valid primary GPT: header at LBA 1, a
  128 x 128-byte entry array at LBA 2 holding two partitions.
**/
VOID
SetupDiskWithValidPrimary (
  VOID
  );

BOOLEAN
ValidateAt (
  IN  EFI_LBA                     Lba,
  OUT EFI_PARTITION_TABLE_HEADER  *Header
  );

VOID
InitCheckEntryHeader (
  OUT EFI_PARTITION_TABLE_HEADER  *Header,
  IN  UINT32                      NumEntries,
  IN  UINT32                      EntrySize
  );