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