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 aidl::android::hardware::boot {
24 
25 #define GPT_SIGNATURE 0x5452415020494645UL
26 
27 typedef struct {
28     uint8_t type_guid[16];
29     uint8_t guid[16];
30     uint64_t first_lba;
31     uint64_t last_lba;
32     uint64_t attr;
33     uint16_t name[36];
34 } __attribute__((packed)) gpt_entry;
35 
36 typedef struct {
37     uint64_t signature;
38     uint32_t revision;
39     uint32_t header_size;
40     uint32_t crc32;
41     uint32_t reserved;
42     uint64_t current_lba;
43     uint64_t backup_lba;
44     uint64_t first_usable_lba;
45     uint64_t last_usable_lba;
46     uint8_t disk_guid[16];
47     uint64_t start_lba;
48     uint32_t entry_count;
49     uint32_t entry_size;
50     uint32_t entries_crc32;
51 } __attribute__((packed)) gpt_header;
52 
53 class GptUtils {
54   public:
55     GptUtils(const std::string dev_path);
56     int Load(void);
57     gpt_entry *GetPartitionEntry(std::string name);
58     int Sync(void);
59     ~GptUtils();
60 
61   private:
62     std::string dev_path;
63     int fd;
64     uint32_t block_size;
65     gpt_header gpt_primary;
66     gpt_header gpt_backup;
67     std::vector<gpt_entry> entry_array;
68     std::map<std::string, gpt_entry *> entries;
69 };
70 
71 }  // namespace aidl::android::hardware::boot