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