1 /*
2  *  Copyright (C) 2014-2022 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
3  *
4  *  Authors: Kevin Lin <klin@sourcefire.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  *  MA 02110-1301, USA.
19  */
20 
21 #ifndef __GPT_H
22 #define __GPT_H
23 
24 #if HAVE_CONFIG_H
25 #include "clamav-config.h"
26 #endif
27 
28 #include "clamav-types.h"
29 #include "others.h"
30 
31 /* GPT sector size is normally 512 bytes be can be set to much larger
32  * values. Sector size for GPT can be found by the offset the GPT header
33  * signature is located (marking the beginning of the second sector.
34 */
35 #define GPT_SIGNATURE 0x4546492050415254ULL
36 #define GPT_SIGNATURE_STR "EFI PART"
37 #define GPT_PRIMARY_HDR_LBA 1
38 #define GPT_HDR_RESERVED 0
39 
40 #ifndef HAVE_ATTRIB_PACKED
41 #define __attribute__(x)
42 #endif
43 
44 #ifdef HAVE_PRAGMA_PACK
45 #pragma pack(1)
46 #endif
47 
48 #ifdef HAVE_PRAGMA_PACK_HPPA
49 #pragma pack 1
50 #endif
51 
52 /* 92-byte gpt_header, these are little endian */
53 struct gpt_header {
54     uint64_t signature __attribute__((packed));
55     uint32_t revision __attribute__((packed));
56     uint32_t headerSize __attribute__((packed)); /* should be 92 bytes */
57     uint32_t headerCRC32 __attribute__((packed));
58     uint32_t reserved __attribute__((packed)); /* this MUST be zero */
59 
60     /* LBA values should be 1 and the last sector index */
61     uint64_t currentLBA __attribute__((packed));
62     uint64_t backupLBA __attribute__((packed));
63 
64     /* data not including the gpt_header and partition table */
65     uint64_t firstUsableLBA __attribute__((packed));
66     uint64_t lastUsableLBA __attribute__((packed));
67 
68     uint8_t DiskGUID[16];
69 
70     /* partition table information */
71     uint64_t tableStartLBA __attribute__((packed));
72     uint32_t tableNumEntries __attribute__((packed));
73     uint32_t tableEntrySize __attribute__((packed));
74     uint32_t tableCRC32 __attribute__((packed));
75     /* zeroes fill remainder of sector (420 bytes in 512 sector size) */
76 };
77 
78 /* 128-byte partition entry, part of an array of 128+ entries, in little_endian */
79 struct gpt_partition_entry {
80     uint8_t typeGUID[16];
81     uint8_t uniqueGUID[16];
82     uint64_t firstLBA __attribute__((packed));
83     uint64_t lastLBA __attribute__((packed));
84     uint64_t attributes __attribute__((packed));
85     uint16_t name[36] __attribute__((packed));
86 };
87 
88 #ifdef HAVE_PRAGMA_PACK
89 #pragma pack()
90 #endif
91 
92 #ifdef HAVE_PRAGMA_PACK_HPPA
93 #pragma pack
94 #endif
95 
96 size_t gpt_detect_size(fmap_t *map);
97 int cli_scangpt(cli_ctx *ctx, size_t sectorsize);
98 
99 #endif
100