1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 namespace android {
24 namespace hardware {
25 namespace boot {
26 namespace V1_2 {
27 namespace implementation {
28 
29 #define GPT_SIGNATURE 0x5452415020494645UL
30 
31 typedef struct {
32     uint8_t type_guid[16];
33     uint8_t guid[16];
34     uint64_t first_lba;
35     uint64_t last_lba;
36     uint64_t attr;
37     uint16_t name[36];
38 } __attribute__((packed)) gpt_entry;
39 
40 typedef struct {
41     uint64_t signature;
42     uint32_t revision;
43     uint32_t header_size;
44     uint32_t crc32;
45     uint32_t reserved;
46     uint64_t current_lba;
47     uint64_t backup_lba;
48     uint64_t first_usable_lba;
49     uint64_t last_usable_lba;
50     uint8_t disk_guid[16];
51     uint64_t start_lba;
52     uint32_t entry_count;
53     uint32_t entry_size;
54     uint32_t entries_crc32;
55 } __attribute__((packed)) gpt_header;
56 
57 class GptUtils {
58   public:
59     GptUtils(const std::string dev_path);
60     int Load(void);
61     gpt_entry *GetPartitionEntry(std::string name);
62     int Sync(void);
63     ~GptUtils();
64 
65   private:
66     std::string dev_path;
67     int fd;
68     uint32_t block_size;
69     gpt_header gpt_primary;
70     gpt_header gpt_backup;
71     std::vector<gpt_entry> entry_array;
72     std::map<std::string, gpt_entry *> entries;
73 };
74 
75 }  // namespace implementation
76 }  // namespace V1_2
77 }  // namespace boot
78 }  // namespace hardware
79 }  // namespace android
80