• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "utility.h"
18 
19 #include <dirent.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <android-base/properties.h>
27 #include <android-base/strings.h>
28 #include <fs_mgr.h>
29 #include <fs_mgr/roots.h>
30 #include <fs_mgr_dm_linear.h>
31 #include <liblp/builder.h>
32 #include <liblp/liblp.h>
33 
34 #include "fastboot_device.h"
35 
36 using namespace android::fs_mgr;
37 using namespace std::chrono_literals;
38 using android::base::unique_fd;
39 using android::hardware::boot::V1_0::Slot;
40 
41 namespace {
42 
OpenPhysicalPartition(const std::string & name,PartitionHandle * handle)43 bool OpenPhysicalPartition(const std::string& name, PartitionHandle* handle) {
44     std::optional<std::string> path = FindPhysicalPartition(name);
45     if (!path) {
46         return false;
47     }
48     *handle = PartitionHandle(*path);
49     return true;
50 }
51 
OpenLogicalPartition(FastbootDevice * device,const std::string & partition_name,PartitionHandle * handle)52 bool OpenLogicalPartition(FastbootDevice* device, const std::string& partition_name,
53                           PartitionHandle* handle) {
54     std::string slot_suffix = GetSuperSlotSuffix(device, partition_name);
55     uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
56     auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number));
57     if (!path) {
58         return false;
59     }
60 
61     CreateLogicalPartitionParams params = {
62             .block_device = *path,
63             .metadata_slot = slot_number,
64             .partition_name = partition_name,
65             .force_writable = true,
66             .timeout_ms = 5s,
67     };
68     std::string dm_path;
69     if (!CreateLogicalPartition(params, &dm_path)) {
70         LOG(ERROR) << "Could not map partition: " << partition_name;
71         return false;
72     }
73     auto closer = [partition_name]() -> void { DestroyLogicalPartition(partition_name); };
74     *handle = PartitionHandle(dm_path, std::move(closer));
75     return true;
76 }
77 
78 }  // namespace
79 
OpenPartition(FastbootDevice * device,const std::string & name,PartitionHandle * handle,int flags)80 bool OpenPartition(FastbootDevice* device, const std::string& name, PartitionHandle* handle,
81                    int flags) {
82     // We prioritize logical partitions over physical ones, and do this
83     // consistently for other partition operations (like getvar:partition-size).
84     if (LogicalPartitionExists(device, name)) {
85         if (!OpenLogicalPartition(device, name, handle)) {
86             return false;
87         }
88     } else if (!OpenPhysicalPartition(name, handle)) {
89         LOG(ERROR) << "No such partition: " << name;
90         return false;
91     }
92 
93     return handle->Open(flags);
94 }
95 
FindPhysicalPartition(const std::string & name)96 std::optional<std::string> FindPhysicalPartition(const std::string& name) {
97     // Check for an invalid file name
98     if (android::base::StartsWith(name, "../") || name.find("/../") != std::string::npos) {
99         return {};
100     }
101     std::string path = "/dev/block/by-name/" + name;
102     if (access(path.c_str(), W_OK) < 0) {
103         return {};
104     }
105     return path;
106 }
107 
FindLogicalPartition(const LpMetadata & metadata,const std::string & name)108 static const LpMetadataPartition* FindLogicalPartition(const LpMetadata& metadata,
109                                                        const std::string& name) {
110     for (const auto& partition : metadata.partitions) {
111         if (GetPartitionName(partition) == name) {
112             return &partition;
113         }
114     }
115     return nullptr;
116 }
117 
LogicalPartitionExists(FastbootDevice * device,const std::string & name,bool * is_zero_length)118 bool LogicalPartitionExists(FastbootDevice* device, const std::string& name, bool* is_zero_length) {
119     std::string slot_suffix = GetSuperSlotSuffix(device, name);
120     uint32_t slot_number = SlotNumberForSlotSuffix(slot_suffix);
121     auto path = FindPhysicalPartition(fs_mgr_get_super_partition_name(slot_number));
122     if (!path) {
123         return false;
124     }
125 
126     std::unique_ptr<LpMetadata> metadata = ReadMetadata(path->c_str(), slot_number);
127     if (!metadata) {
128         return false;
129     }
130     const LpMetadataPartition* partition = FindLogicalPartition(*metadata.get(), name);
131     if (!partition) {
132         return false;
133     }
134     if (is_zero_length) {
135         *is_zero_length = (partition->num_extents == 0);
136     }
137     return true;
138 }
139 
GetSlotNumber(const std::string & slot,Slot * number)140 bool GetSlotNumber(const std::string& slot, Slot* number) {
141     if (slot.size() != 1) {
142         return false;
143     }
144     if (slot[0] < 'a' || slot[0] > 'z') {
145         return false;
146     }
147     *number = slot[0] - 'a';
148     return true;
149 }
150 
ListPartitions(FastbootDevice * device)151 std::vector<std::string> ListPartitions(FastbootDevice* device) {
152     std::vector<std::string> partitions;
153 
154     // First get physical partitions.
155     struct dirent* de;
156     std::unique_ptr<DIR, decltype(&closedir)> by_name(opendir("/dev/block/by-name"), closedir);
157     while ((de = readdir(by_name.get())) != nullptr) {
158         if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
159             continue;
160         }
161         struct stat s;
162         std::string path = "/dev/block/by-name/" + std::string(de->d_name);
163         if (!stat(path.c_str(), &s) && S_ISBLK(s.st_mode)) {
164             partitions.emplace_back(de->d_name);
165         }
166     }
167 
168     // Find metadata in each super partition (on retrofit devices, there will
169     // be two).
170     std::vector<std::unique_ptr<LpMetadata>> metadata_list;
171 
172     uint32_t current_slot = SlotNumberForSlotSuffix(device->GetCurrentSlot());
173     std::string super_name = fs_mgr_get_super_partition_name(current_slot);
174     if (auto metadata = ReadMetadata(super_name, current_slot)) {
175         metadata_list.emplace_back(std::move(metadata));
176     }
177 
178     uint32_t other_slot = (current_slot == 0) ? 1 : 0;
179     std::string other_super = fs_mgr_get_super_partition_name(other_slot);
180     if (super_name != other_super) {
181         if (auto metadata = ReadMetadata(other_super, other_slot)) {
182             metadata_list.emplace_back(std::move(metadata));
183         }
184     }
185 
186     for (const auto& metadata : metadata_list) {
187         for (const auto& partition : metadata->partitions) {
188             std::string partition_name = GetPartitionName(partition);
189             if (std::find(partitions.begin(), partitions.end(), partition_name) ==
190                 partitions.end()) {
191                 partitions.emplace_back(partition_name);
192             }
193         }
194     }
195     return partitions;
196 }
197 
GetDeviceLockStatus()198 bool GetDeviceLockStatus() {
199     return android::base::GetProperty("ro.boot.verifiedbootstate", "") != "orange";
200 }
201 
UpdateAllPartitionMetadata(FastbootDevice * device,const std::string & super_name,const android::fs_mgr::LpMetadata & metadata)202 bool UpdateAllPartitionMetadata(FastbootDevice* device, const std::string& super_name,
203                                 const android::fs_mgr::LpMetadata& metadata) {
204     size_t num_slots = 1;
205     auto boot_control_hal = device->boot_control_hal();
206     if (boot_control_hal) {
207         num_slots = boot_control_hal->getNumberSlots();
208     }
209 
210     bool ok = true;
211     for (size_t i = 0; i < num_slots; i++) {
212         ok &= UpdatePartitionTable(super_name, metadata, i);
213     }
214     return ok;
215 }
216 
GetSuperSlotSuffix(FastbootDevice * device,const std::string & partition_name)217 std::string GetSuperSlotSuffix(FastbootDevice* device, const std::string& partition_name) {
218     // If the super partition does not have a slot suffix, this is not a
219     // retrofit device, and we should take the current slot.
220     std::string current_slot_suffix = device->GetCurrentSlot();
221     uint32_t current_slot_number = SlotNumberForSlotSuffix(current_slot_suffix);
222     std::string super_partition = fs_mgr_get_super_partition_name(current_slot_number);
223     if (GetPartitionSlotSuffix(super_partition).empty()) {
224         return current_slot_suffix;
225     }
226 
227     // Otherwise, infer the slot from the partition name.
228     std::string slot_suffix = GetPartitionSlotSuffix(partition_name);
229     if (!slot_suffix.empty()) {
230         return slot_suffix;
231     }
232     return current_slot_suffix;
233 }
234 
AutoMountMetadata()235 AutoMountMetadata::AutoMountMetadata() {
236     android::fs_mgr::Fstab proc_mounts;
237     if (!ReadFstabFromFile("/proc/mounts", &proc_mounts)) {
238         LOG(ERROR) << "Could not read /proc/mounts";
239         return;
240     }
241 
242     if (GetEntryForMountPoint(&proc_mounts, "/metadata")) {
243         mounted_ = true;
244         return;
245     }
246 
247     if (!ReadDefaultFstab(&fstab_)) {
248         LOG(ERROR) << "Could not read default fstab";
249         return;
250     }
251     mounted_ = EnsurePathMounted(&fstab_, "/metadata");
252     should_unmount_ = true;
253 }
254 
~AutoMountMetadata()255 AutoMountMetadata::~AutoMountMetadata() {
256     if (mounted_ && should_unmount_) {
257         EnsurePathUnmounted(&fstab_, "/metadata");
258     }
259 }
260