1 /*
2  * Copyright (C) 2018 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 #include <fcntl.h>
18 #include <inttypes.h>
19 #include <stdint.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 
23 #if defined(__linux__)
24 #include <linux/fs.h>
25 #include <sys/ioctl.h>
26 #endif
27 
28 #include <algorithm>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 #include <android-base/file.h>
34 #include <android-base/stringprintf.h>
35 #include <ext4_utils/ext4_utils.h>
36 #include <openssl/sha.h>
37 
38 #ifdef __ANDROID__
39 #include <cutils/android_get_control_file.h>
40 #endif
41 
42 #include "utility.h"
43 
44 namespace android {
45 namespace fs_mgr {
46 
GetDescriptorSize(int fd,uint64_t * size)47 bool GetDescriptorSize(int fd, uint64_t* size) {
48 #if !defined(_WIN32)
49     struct stat s;
50     if (fstat(fd, &s) < 0) {
51         PERROR << __PRETTY_FUNCTION__ << "fstat failed";
52         return false;
53     }
54 
55     if (S_ISBLK(s.st_mode)) {
56         *size = get_block_device_size(fd);
57         return *size != 0;
58     }
59 #endif
60 
61     int64_t result = SeekFile64(fd, 0, SEEK_END);
62     if (result == -1) {
63         PERROR << __PRETTY_FUNCTION__ << "lseek failed";
64         return false;
65     }
66 
67     *size = result;
68     return true;
69 }
70 
SeekFile64(int fd,int64_t offset,int whence)71 int64_t SeekFile64(int fd, int64_t offset, int whence) {
72     static_assert(sizeof(off_t) == sizeof(int64_t), "Need 64-bit lseek");
73     return lseek(fd, offset, whence);
74 }
75 
GetPrimaryGeometryOffset()76 int64_t GetPrimaryGeometryOffset() {
77     return LP_PARTITION_RESERVED_BYTES;
78 }
79 
GetBackupGeometryOffset()80 int64_t GetBackupGeometryOffset() {
81     return GetPrimaryGeometryOffset() + LP_METADATA_GEOMETRY_SIZE;
82 }
83 
GetPrimaryMetadataOffset(const LpMetadataGeometry & geometry,uint32_t slot_number)84 int64_t GetPrimaryMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number) {
85     CHECK(slot_number < geometry.metadata_slot_count);
86     int64_t offset = LP_PARTITION_RESERVED_BYTES + (LP_METADATA_GEOMETRY_SIZE * 2) +
87                      geometry.metadata_max_size * slot_number;
88     return offset;
89 }
90 
GetBackupMetadataOffset(const LpMetadataGeometry & geometry,uint32_t slot_number)91 int64_t GetBackupMetadataOffset(const LpMetadataGeometry& geometry, uint32_t slot_number) {
92     CHECK(slot_number < geometry.metadata_slot_count);
93     int64_t start = LP_PARTITION_RESERVED_BYTES + (LP_METADATA_GEOMETRY_SIZE * 2) +
94                     int64_t(geometry.metadata_max_size) * geometry.metadata_slot_count;
95     return start + int64_t(geometry.metadata_max_size * slot_number);
96 }
97 
GetTotalMetadataSize(uint32_t metadata_max_size,uint32_t max_slots)98 uint64_t GetTotalMetadataSize(uint32_t metadata_max_size, uint32_t max_slots) {
99     return LP_PARTITION_RESERVED_BYTES +
100            (LP_METADATA_GEOMETRY_SIZE + metadata_max_size * max_slots) * 2;
101 }
102 
GetMetadataSuperBlockDevice(const LpMetadata & metadata)103 const LpMetadataBlockDevice* GetMetadataSuperBlockDevice(const LpMetadata& metadata) {
104     if (metadata.block_devices.empty()) {
105         return nullptr;
106     }
107     return &metadata.block_devices[0];
108 }
109 
SHA256(const void * data,size_t length,uint8_t out[32])110 void SHA256(const void* data, size_t length, uint8_t out[32]) {
111     SHA256_CTX c;
112     SHA256_Init(&c);
113     SHA256_Update(&c, data, length);
114     SHA256_Final(out, &c);
115 }
116 
SlotNumberForSlotSuffix(const std::string & suffix)117 uint32_t SlotNumberForSlotSuffix(const std::string& suffix) {
118     if (suffix.empty() || suffix == "a" || suffix == "_a") {
119         return 0;
120     } else if (suffix == "b" || suffix == "_b") {
121         return 1;
122     } else {
123         LERROR << __PRETTY_FUNCTION__ << "slot '" << suffix
124                << "' does not have a recognized format.";
125         return 0;
126     }
127 }
128 
GetTotalSuperPartitionSize(const LpMetadata & metadata)129 uint64_t GetTotalSuperPartitionSize(const LpMetadata& metadata) {
130     uint64_t size = 0;
131     for (const auto& block_device : metadata.block_devices) {
132         size += block_device.size;
133     }
134     return size;
135 }
136 
GetBlockDevicePartitionNames(const LpMetadata & metadata)137 std::vector<std::string> GetBlockDevicePartitionNames(const LpMetadata& metadata) {
138     std::vector<std::string> list;
139     for (const auto& block_device : metadata.block_devices) {
140         list.emplace_back(GetBlockDevicePartitionName(block_device));
141     }
142     return list;
143 }
144 
FindPartition(const LpMetadata & metadata,const std::string & name)145 const LpMetadataPartition* FindPartition(const LpMetadata& metadata, const std::string& name) {
146     for (const auto& partition : metadata.partitions) {
147         if (GetPartitionName(partition) == name) {
148             return &partition;
149         }
150     }
151     return nullptr;
152 }
153 
GetPartitionSize(const LpMetadata & metadata,const LpMetadataPartition & partition)154 uint64_t GetPartitionSize(const LpMetadata& metadata, const LpMetadataPartition& partition) {
155     uint64_t total_size = 0;
156     for (uint32_t i = 0; i < partition.num_extents; i++) {
157         const auto& extent = metadata.extents[partition.first_extent_index + i];
158         total_size += extent.num_sectors * LP_SECTOR_SIZE;
159     }
160     return total_size;
161 }
162 
GetPartitionSlotSuffix(const std::string & partition_name)163 std::string GetPartitionSlotSuffix(const std::string& partition_name) {
164     if (partition_name.size() <= 2) {
165         return "";
166     }
167     std::string suffix = partition_name.substr(partition_name.size() - 2);
168     return (suffix == "_a" || suffix == "_b") ? suffix : "";
169 }
170 
SlotSuffixForSlotNumber(uint32_t slot_number)171 std::string SlotSuffixForSlotNumber(uint32_t slot_number) {
172     CHECK(slot_number == 0 || slot_number == 1);
173     return (slot_number == 0) ? "_a" : "_b";
174 }
175 
UpdateBlockDevicePartitionName(LpMetadataBlockDevice * device,const std::string & name)176 bool UpdateBlockDevicePartitionName(LpMetadataBlockDevice* device, const std::string& name) {
177     if (name.size() > sizeof(device->partition_name)) {
178         return false;
179     }
180     strncpy(device->partition_name, name.c_str(), sizeof(device->partition_name));
181     return true;
182 }
183 
UpdatePartitionGroupName(LpMetadataPartitionGroup * group,const std::string & name)184 bool UpdatePartitionGroupName(LpMetadataPartitionGroup* group, const std::string& name) {
185     if (name.size() > sizeof(group->name)) {
186         return false;
187     }
188     strncpy(group->name, name.c_str(), sizeof(group->name));
189     return true;
190 }
191 
UpdatePartitionName(LpMetadataPartition * partition,const std::string & name)192 bool UpdatePartitionName(LpMetadataPartition* partition, const std::string& name) {
193     if (name.size() > sizeof(partition->name)) {
194         return false;
195     }
196     strncpy(partition->name, name.c_str(), sizeof(partition->name));
197     return true;
198 }
199 
SetBlockReadonly(int fd,bool readonly)200 bool SetBlockReadonly(int fd, bool readonly) {
201 #if defined(__linux__)
202     int val = readonly;
203     return ioctl(fd, BLKROSET, &val) == 0;
204 #else
205     (void)fd;
206     (void)readonly;
207     return true;
208 #endif
209 }
210 
GetControlFileOrOpen(std::string_view path,int flags)211 base::unique_fd GetControlFileOrOpen(std::string_view path, int flags) {
212 #if defined(__ANDROID__)
213     int fd = android_get_control_file(path.data());
214     if (fd >= 0) {
215         int newfd = TEMP_FAILURE_RETRY(dup(fd));
216         if (newfd >= 0) {
217             return base::unique_fd(newfd);
218         }
219         PERROR << "Cannot dup fd for already controlled file: " << path << ", reopening...";
220     }
221 #endif
222     return base::unique_fd(open(path.data(), flags));
223 }
224 
UpdateMetadataForInPlaceSnapshot(LpMetadata * metadata,uint32_t source_slot_number,uint32_t target_slot_number)225 bool UpdateMetadataForInPlaceSnapshot(LpMetadata* metadata, uint32_t source_slot_number,
226                                       uint32_t target_slot_number) {
227     std::string source_slot_suffix = SlotSuffixForSlotNumber(source_slot_number);
228     std::string target_slot_suffix = SlotSuffixForSlotNumber(target_slot_number);
229 
230     // There can be leftover groups with target suffix on retrofit devices.
231     // They are useless now, so delete.
232     std::vector<LpMetadataPartitionGroup*> new_group_ptrs;
233     for (auto& group : metadata->groups) {
234         std::string group_name = GetPartitionGroupName(group);
235         std::string slot_suffix = GetPartitionSlotSuffix(group_name);
236         // Don't add groups with target slot suffix.
237         if (slot_suffix == target_slot_suffix) continue;
238         // Replace source slot suffix with target slot suffix.
239         if (slot_suffix == source_slot_suffix) {
240             std::string new_name = group_name.substr(0, group_name.size() - slot_suffix.size()) +
241                                    target_slot_suffix;
242             if (!UpdatePartitionGroupName(&group, new_name)) {
243                 LERROR << "Group name too long: " << new_name;
244                 return false;
245             }
246         }
247         new_group_ptrs.push_back(&group);
248     }
249 
250     std::vector<LpMetadataPartition*> new_partition_ptrs;
251     for (auto& partition : metadata->partitions) {
252         std::string partition_name = GetPartitionName(partition);
253         std::string slot_suffix = GetPartitionSlotSuffix(partition_name);
254         // Don't add partitions with target slot suffix.
255         if (slot_suffix == target_slot_suffix) continue;
256         // Replace source slot suffix with target slot suffix.
257         if (slot_suffix == source_slot_suffix) {
258             std::string new_name =
259                     partition_name.substr(0, partition_name.size() - slot_suffix.size()) +
260                     target_slot_suffix;
261             if (!UpdatePartitionName(&partition, new_name)) {
262                 LERROR << "Partition name too long: " << new_name;
263                 return false;
264             }
265         }
266         // Update group index.
267         auto it = std::find(new_group_ptrs.begin(), new_group_ptrs.end(),
268                             &metadata->groups[partition.group_index]);
269         if (it == new_group_ptrs.end()) {
270             LWARN << "Removing partition " << partition_name << " from group "
271                   << GetPartitionGroupName(metadata->groups[partition.group_index])
272                   << "; this partition should not belong to this group!";
273             continue;  // not adding to new_partition_ptrs
274         }
275         partition.attributes |= LP_PARTITION_ATTR_UPDATED;
276         partition.group_index = std::distance(new_group_ptrs.begin(), it);
277         new_partition_ptrs.push_back(&partition);
278     }
279 
280     std::vector<LpMetadataPartition> new_partitions;
281     for (auto* p : new_partition_ptrs) new_partitions.emplace_back(std::move(*p));
282     metadata->partitions = std::move(new_partitions);
283 
284     std::vector<LpMetadataPartitionGroup> new_groups;
285     for (auto* g : new_group_ptrs) new_groups.emplace_back(std::move(*g));
286     metadata->groups = std::move(new_groups);
287 
288     return true;
289 }
290 
ToHexString(uint64_t value)291 inline std::string ToHexString(uint64_t value) {
292     return android::base::StringPrintf("0x%" PRIx64, value);
293 }
294 
SetMetadataHeaderV0(LpMetadata * metadata)295 void SetMetadataHeaderV0(LpMetadata* metadata) {
296     if (metadata->header.minor_version <= LP_METADATA_MINOR_VERSION_MIN) {
297         return;
298     }
299     LINFO << "Forcefully setting metadata header version " << LP_METADATA_MAJOR_VERSION << "."
300           << metadata->header.minor_version << " to " << LP_METADATA_MAJOR_VERSION << "."
301           << LP_METADATA_MINOR_VERSION_MIN;
302     metadata->header.minor_version = LP_METADATA_MINOR_VERSION_MIN;
303     metadata->header.header_size = sizeof(LpMetadataHeaderV1_0);
304 
305     // Retrofit Virtual A/B devices should have version 10.1, so flags shouldn't be set.
306     // Warn if this is the case, but zero it out anyways.
307     if (metadata->header.flags) {
308         LWARN << "Zeroing unexpected flags: " << ToHexString(metadata->header.flags);
309     }
310 
311     // Zero out all fields beyond LpMetadataHeaderV0.
312     static_assert(sizeof(metadata->header) > sizeof(LpMetadataHeaderV1_0));
313     memset(reinterpret_cast<uint8_t*>(&metadata->header) + sizeof(LpMetadataHeaderV1_0), 0,
314            sizeof(metadata->header) - sizeof(LpMetadataHeaderV1_0));
315 
316     // Clear partition attributes unknown to V0.
317     // On retrofit Virtual A/B devices, UPDATED flag may be set, so only log info here.
318     for (auto& partition : metadata->partitions) {
319         if (partition.attributes & ~LP_PARTITION_ATTRIBUTE_MASK_V0) {
320             LINFO << "Clearing " << GetPartitionName(partition)
321                   << " partition attribute: " << ToHexString(partition.attributes);
322         }
323 
324         partition.attributes &= LP_PARTITION_ATTRIBUTE_MASK_V0;
325     }
326 }
327 
328 }  // namespace fs_mgr
329 }  // namespace android
330