• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "bootcontrolhal"
18 
19 #include "GptUtils.h"
20 
21 #include <errno.h>
22 #include <log/log.h>
23 #include <android-base/file.h>
24 #include <linux/fs.h>
25 #include <zlib.h>
26 
27 namespace android {
28 namespace hardware {
29 namespace boot {
30 namespace V1_0 {
31 namespace implementation {
32 
33 namespace {
34 
ValidateGptHeader(gpt_header * gpt)35 static int ValidateGptHeader(gpt_header *gpt)
36 {
37     if (gpt->signature != GPT_SIGNATURE) {
38         ALOGE("invalid gpt signature 0x%lx\n", gpt->signature);
39         return -1;
40     }
41 
42     if (gpt->header_size != sizeof(gpt_header)) {
43         ALOGE("invalid gpt header size %u\n", gpt->header_size);
44         return -1;
45     }
46 
47     if (gpt->entry_size != sizeof(gpt_entry)) {
48         ALOGE("invalid gpt entry size %u\n", gpt->entry_size);
49         return -1;
50     }
51 
52     return 0;
53 }
54 
55 }
56 
GptUtils(const std::string dev_path)57 GptUtils::GptUtils(const std::string dev_path) : dev_path(dev_path), fd(0) {}
58 
Load(void)59 int GptUtils::Load(void)
60 {
61     fd = open(dev_path.c_str(), O_RDWR);
62     if (fd < 0) {
63         ALOGE("failed to open block dev %s, %d\n", dev_path.c_str(), errno);
64         return -1;
65     }
66 
67     int ret = ioctl(fd, BLKSSZGET, &block_size);
68     if (ret < 0) {
69         ALOGE("failed to get block size %d\n", errno);
70         return -1;
71     }
72 
73     // read primary header
74     lseek64(fd, block_size, SEEK_SET);
75     ret = read(fd, &gpt_primary, sizeof gpt_primary);
76     if (ret < 0) {
77         ALOGE("failed to read gpt primary header %d\n", errno);
78         return -1;
79     }
80 
81     if (ValidateGptHeader(&gpt_primary)) {
82         ALOGE("error validating gpt header\n");
83         return -1;
84     }
85 
86     // read partition entries
87     entry_array.resize(gpt_primary.entry_count);
88     uint32_t entries_size = gpt_primary.entry_size * gpt_primary.entry_count;
89     lseek64(fd, block_size * gpt_primary.start_lba, SEEK_SET);
90     ret = read(fd, entry_array.data(), entries_size);
91     if (ret < 0) {
92         ALOGE("failed to read gpt partition entries %d\n", errno);
93         return -1;
94     }
95 
96     // read gpt back header
97     lseek64(fd, block_size * gpt_primary.backup_lba, SEEK_SET);
98     ret = read(fd, &gpt_backup, sizeof gpt_backup);
99     if (ret < 0) {
100         ALOGE("failed to read gpt backup header %d\n", errno);
101         return -1;
102     }
103 
104     if (ValidateGptHeader(&gpt_backup)) {
105         ALOGW("error validating gpt backup\n"); // just warn about it, not fail
106     }
107 
108     // Create map <partition name, gpt_entry pointer>
109     auto get_name = [](const uint16_t *efi_name) {
110         char name[37] = {};
111         for (int i = 0; efi_name[i] && i < sizeof name - 1; ++i)
112             name[i] = efi_name[i];
113         return std::string(name);
114     };
115 
116     for (auto const &e: entry_array) {
117         if (e.name[0] == 0)
118             break;   // stop at the first partition with no name
119         std::string s = get_name(e.name);
120         entries[s] = const_cast<gpt_entry *>(&e);
121     }
122 
123     return 0;
124 }
125 
GetPartitionEntry(std::string name)126 gpt_entry *GptUtils::GetPartitionEntry(std::string name)
127 {
128     return entries.find(name) != entries.end() ?  entries[name] : nullptr;
129 }
130 
Sync(void)131 int GptUtils::Sync(void)
132 {
133     if (!fd)
134         return -1;
135 
136     // calculate crc and check if we need to update gpt
137     gpt_primary.entries_crc32 = crc32(0, reinterpret_cast<uint8_t *>(entry_array.data()),
138                                 entry_array.size() * sizeof(gpt_entry));
139 
140     // save old crc
141     uint32_t crc = gpt_primary.crc32;
142     gpt_primary.crc32 = 0;
143 
144     gpt_primary.crc32 = crc32(0, reinterpret_cast<uint8_t *>(&gpt_primary), sizeof gpt_primary);
145     if (crc == gpt_primary.crc32)
146         return 0; // nothing to do (no changes)
147 
148     ALOGI("updating GPT\n");
149 
150     lseek64(fd, block_size * gpt_primary.current_lba, SEEK_SET);
151     int ret = write(fd, &gpt_primary, sizeof gpt_primary);
152     if (ret < 0) {
153         ALOGE("failed to write gpt primary header %d\n", errno);
154         return -1;
155     }
156 
157     lseek64(fd, block_size * gpt_primary.start_lba, SEEK_SET);
158     ret = write(fd, entry_array.data(), entry_array.size() * sizeof(gpt_entry));
159     if (ret < 0) {
160         ALOGE("failed to write gpt partition entries %d\n", errno);
161         return -1;
162     }
163 
164     //update GPT backup entries and backup
165     lseek64(fd, block_size * gpt_backup.start_lba, SEEK_SET);
166     ret = write(fd, entry_array.data(), entry_array.size() * sizeof(gpt_entry));
167     if (ret < 0) {
168         ALOGE("failed to write gpt backup partition entries %d\n", errno);
169         return -1;
170     }
171 
172     gpt_backup.entries_crc32 = gpt_primary.entries_crc32;
173     gpt_backup.crc32 = 0;
174     gpt_backup.crc32 = crc32(0, reinterpret_cast<uint8_t *>(&gpt_backup), sizeof gpt_backup);
175     lseek64(fd, block_size * gpt_primary.backup_lba, SEEK_SET);
176     ret = write(fd, &gpt_backup, sizeof gpt_backup);
177     if (ret < 0) {
178         ALOGE("failed to write gpt backup header %d\n", errno);
179         return -1;
180     }
181 
182     fsync(fd);
183 
184     return 0;
185 }
186 
~GptUtils()187 GptUtils::~GptUtils()
188 {
189     if (fd) {
190         Sync();
191         close(fd);
192     }
193 }
194 
195 }  // namespace implementation
196 }  // namespace V1_0
197 }  // namespace boot
198 }  // namespace hardware
199 }  // namespace android
200