1 /*
2  * Copyright (c) 2016-2023, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef GPT_H
8 #define GPT_H
9 
10 #include "stdint.h"
11 #include "stddef.h"
12 #include "uuid.h"
13 
14 #define PARTITION_TYPE_GPT     0xee
15 #define EFI_NAMELEN_MAX        36
16 #define GPT_HEADER_OFFSET      PLAT_PARTITION_BLOCK_SIZE
17 #define GPT_ENTRY_OFFSET       (GPT_HEADER_OFFSET + PLAT_PARTITION_BLOCK_SIZE)
18 #define GPT_SIGNATURE          "EFI PART"
19 #define PRIMARY_FIP_GPT_NAME   "FIP_A"
20 #define SECONDARY_FIP_GPT_NAME "FIP_B"
21 
22 typedef struct gpt_entry {
23     struct efi_guid type_uuid;
24     struct efi_guid unique_uuid;
25     uint64_t        first_lba;
26     uint64_t        last_lba;
27     uint64_t        attr;
28     uint16_t        name[EFI_NAMELEN_MAX];
29 } gpt_entry_t;
30 
31 typedef struct gpt_header {
32     uint8_t         signature[8];
33     uint32_t        revision;
34     uint32_t        size;
35     uint32_t        header_crc;
36     uint32_t        reserved;
37     uint64_t        current_lba;
38     uint64_t        backup_lba;
39     uint64_t        first_lba;
40     uint64_t        last_lba;
41     struct efi_guid disk_uuid;
42     /* starting LBA of array of partition entries */
43     uint64_t        list_lba;
44     /* number of partition entries in array */
45     uint32_t        list_num;
46     /* size of a single partition entry (usually 128) */
47     uint32_t        list_entry_size;
48     uint32_t        list_crc;
49 } gpt_header_t;
50 
51 /**
52  * \brief                    Read a GPT header from flash into a struct.
53  *
54  * \param[in]  table_base    The RSE address mapped to the GPT table base in
55  *                           host flash.
56  * \param[in]  atu_slot_size The size of the ATU region that was mapped for
57  *                           access to this FIP. This is used to prevent reads
58  *                           outside the mapped region.
59  * \param[out] header        Pointer to a gpt_header_t struct that will be
60  *                           filled with the header data.
61  * \return                   0 if operation completed successfully, another
62  *                           value on error.
63  */
64 int gpt_get_header(uint32_t table_base, size_t atu_slot_size,
65                    gpt_header_t *header);
66 
67 /**
68  * \brief                       Read a GPT list entry from flash into a struct.
69  *                              Which list entry should be read is determined
70  *                              by matching the name of the partition.
71  *
72  * \param[in]  list_base        The RSE address mapped to the GPT entry list base
73  *                              in host flash.
74  * \param[in]  list_num_entries The number of entries in the GPT entry list, as
75  *                              read from the header.
76  * \param[in]  list_entry_size  The size of entries in the GPT entry list, as
77  *                              read from the header.
78  * \param[in]  name             The name of the partition whose list entry
79  *                              should be read into the struct. Unlike the GPT
80  *                              list entry spec, this must be an ascii-encoded
81  *                              string.
82  * \param[in]  name_size        The size of the name string.
83  * \param[in]  atu_slot_size    The size of the ATU region that was mapped for
84  *                              access to this FIP. This is used to prevent
85  *                              reads outside the mapped region.
86  * \param[out] entry            Pointer to a gpt_entry_t struct that will be
87  *                              filled with the list entry data.
88  * \return                      0 if operation completed successfully, another
89  *                              value on error.
90  */
91 int gpt_get_list_entry_by_name(uint32_t list_base, uint32_t list_num_entries,
92                                size_t list_entry_size, uint8_t *name,
93                                size_t name_size, size_t atu_slot_size,
94                                gpt_entry_t *entry);
95 
96 #endif /* GPT_H */
97