blob: 29ae791a7d5d3f91017781aaee1627e333dce05d [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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
Tao Baoe3f09a72019-10-01 11:55:36 -070017#include "recovery_utils/roots.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070018
Tao Baobb10e582017-07-22 16:30:34 -070019#include <fcntl.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053020#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070022#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <sys/stat.h>
24#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070025#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include <unistd.h>
Simon Shieldsdbc4d662019-10-02 00:21:45 +100027#include <sys/mount.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Yifan Hongd81b8e32018-12-17 14:29:06 -080029#include <iostream>
Tao Bao3c00fac2017-07-22 16:46:54 -070030#include <string>
31#include <vector>
32
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070033#include <android-base/logging.h>
Daniel Rosenbergf25b9772019-12-16 18:32:00 -080034#include <android-base/properties.h>
Jin Qianded2dac2017-04-21 14:36:12 -070035#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070036#include <android-base/unique_fd.h>
Alessandro Astone7a3ce382020-03-01 13:33:27 +010037#include <blkid/blkid.h>
Tao Baobb10e582017-07-22 16:30:34 -070038#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070039#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040#include <fs_mgr.h>
Yifan Hong0f339e22018-12-03 13:44:01 -080041#include <fs_mgr/roots.h>
Erfan Abdia24643e2020-04-14 23:07:33 +043042#include <fs_mgr_dm_linear.h>
Tao Baode40ba52016-10-05 23:17:01 -070043
Tao Bao3d69f0d2018-12-20 09:44:06 -080044#include "otautil/sysutil.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tom Cherry72a114a2019-01-30 15:59:53 -080046using android::fs_mgr::Fstab;
47using android::fs_mgr::FstabEntry;
48using android::fs_mgr::ReadDefaultFstab;
aleastob4eca162021-01-17 20:31:22 +010049using android::dm::DeviceMapper;
50using android::dm::DmDeviceState;
Tom Cherry72a114a2019-01-30 15:59:53 -080051
Steve Kondikbed2b292013-11-24 21:40:09 -080052static void write_fstab_entry(const FstabEntry& entry, FILE* file) {
53 if (entry.fs_type != "emmc" && !entry.fs_mgr_flags.vold_managed && !entry.blk_device.empty() &&
54 entry.blk_device[0] == '/' && !entry.mount_point.empty() && entry.mount_point[0] == '/') {
55 fprintf(file, "%s ", entry.blk_device.c_str());
56 fprintf(file, "%s ", entry.mount_point.c_str());
57 fprintf(file, "%s ", entry.fs_type.c_str());
58 fprintf(file, "%s 0 0\n", !entry.fs_options.empty() ? entry.fs_options.c_str() : "defaults");
59 }
60}
61
Yifan Hongd81b8e32018-12-17 14:29:06 -080062static Fstab fstab;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080063
Tianjie Xu164c60a2019-05-15 13:59:39 -070064constexpr const char* CACHE_ROOT = "/cache";
65
Alessandro Astone7a3ce382020-03-01 13:33:27 +010066FstabEntry* fstab_entry_for_mount_point_detect_fs(const std::string& path) {
67 FstabEntry* found = android::fs_mgr::GetEntryForMountPoint(&fstab, path);
68 if (found == nullptr) {
69 return nullptr;
70 }
71
72 if (char* detected_fs_type = blkid_get_tag_value(nullptr, "TYPE", found->blk_device.c_str())) {
73 for (auto& entry : fstab) {
74 if (entry.mount_point == path && entry.fs_type == detected_fs_type) {
75 found = &entry;
76 break;
77 }
78 }
79 free(detected_fs_type);
80 }
81
82 return found;
83}
84
Tao Baobb10e582017-07-22 16:30:34 -070085void load_volume_table() {
Yifan Hongd81b8e32018-12-17 14:29:06 -080086 if (!ReadDefaultFstab(&fstab)) {
Tao Baobb10e582017-07-22 16:30:34 -070087 LOG(ERROR) << "Failed to read default fstab";
88 return;
89 }
Doug Zongker2810ced2011-02-17 15:55:21 -080090
Yifan Hongd81b8e32018-12-17 14:29:06 -080091 fstab.emplace_back(FstabEntry{
Nick Desaulniers1cb510d2019-10-10 16:33:58 -070092 .blk_device = "ramdisk",
93 .mount_point = "/tmp",
94 .fs_type = "ramdisk",
95 .length = 0,
96 });
Doug Zongkerd4208f92010-09-20 12:16:13 -070097
Alessandro Astone7a3ce382020-03-01 13:33:27 +010098 Fstab fake_fstab;
Yifan Hongd81b8e32018-12-17 14:29:06 -080099 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
100 for (size_t i = 0; i < fstab.size(); ++i) {
101 const auto& entry = fstab[i];
102 std::cout << " " << i << " " << entry.mount_point << " "
103 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
104 << std::endl;
Alessandro Astone7a3ce382020-03-01 13:33:27 +0100105
106 if (std::find_if(fake_fstab.begin(), fake_fstab.end(), [entry](const FstabEntry& e) {
107 return entry.mount_point == e.mount_point;
108 }) == fake_fstab.end()) {
109 FstabEntry* entry_detectfs = fstab_entry_for_mount_point_detect_fs(entry.mount_point);
110 if (entry_detectfs == &entry) {
111 fake_fstab.emplace_back(entry);
112 }
Steve Kondikbed2b292013-11-24 21:40:09 -0800113 }
Tao Baobb10e582017-07-22 16:30:34 -0700114 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800115 std::cout << std::endl;
Steve Kondikbed2b292013-11-24 21:40:09 -0800116
Alessandro Astone7a3ce382020-03-01 13:33:27 +0100117 // Create a boring /etc/fstab so tools like Busybox work
118 FILE* file = fopen("/etc/fstab", "w");
Steve Kondikbed2b292013-11-24 21:40:09 -0800119 if (file) {
Alessandro Astone7a3ce382020-03-01 13:33:27 +0100120 for (auto& entry : fake_fstab) {
121 write_fstab_entry(entry, file);
122 }
Steve Kondikbed2b292013-11-24 21:40:09 -0800123 fclose(file);
Alessandro Astone7a3ce382020-03-01 13:33:27 +0100124 } else {
125 LOG(ERROR) << "Unable to create /etc/fstab";
Steve Kondikbed2b292013-11-24 21:40:09 -0800126 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700127}
128
Tao Bao3e18f2b2017-09-29 17:11:13 -0700129Volume* volume_for_mount_point(const std::string& mount_point) {
Tom Cherry72a114a2019-01-30 15:59:53 -0800130 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
Tao Bao3e18f2b2017-09-29 17:11:13 -0700131}
132
Tao Baoabb8f772015-07-30 14:43:27 -0700133// Mount the volume specified by path at the given mount_point.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800134int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
135 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136}
137
Yifan Hongd81b8e32018-12-17 14:29:06 -0800138int ensure_path_mounted(const std::string& path) {
Tao Baobb10e582017-07-22 16:30:34 -0700139 // Mount at the default mount point.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800140 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
Tao Baoabb8f772015-07-30 14:43:27 -0700141}
142
Yifan Hongd81b8e32018-12-17 14:29:06 -0800143int ensure_path_unmounted(const std::string& path) {
144 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700145}
146
Simon Shieldsdbc4d662019-10-02 00:21:45 +1000147int ensure_volume_unmounted(const std::string& blk_device) {
148 android::fs_mgr::Fstab mounted_fstab;
149 if (!android::fs_mgr::ReadFstabFromFile("/proc/mounts", &mounted_fstab)) {
150 LOG(ERROR) << "Failed to read /proc/mounts";
151 return -1;
152 }
153
154 /* find any entries with the volume */
155 for (auto& entry : mounted_fstab) {
156 if (entry.blk_device == blk_device) {
157 int result = umount(entry.mount_point.c_str());
158 if (result == -1) {
159 LOG(ERROR) << "Failed to unmount " << blk_device << " from " << entry.mount_point << ": "
160 << errno;
161 return -1;
162 }
163 }
164 }
165 return 0;
166}
167
Tao Bao3c00fac2017-07-22 16:46:54 -0700168static int exec_cmd(const std::vector<std::string>& args) {
Tao Bao3d69f0d2018-12-20 09:44:06 -0800169 CHECK(!args.empty());
170 auto argv = StringVectorToNullTerminatedArray(args);
Tao Bao3c00fac2017-07-22 16:46:54 -0700171
172 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800173 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700174 execv(argv[0], argv.data());
175 _exit(EXIT_FAILURE);
176 }
177
178 int status;
179 waitpid(child, &status, 0);
180 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
181 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
182 }
183 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700184}
185
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530186static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700187 struct stat buf;
188 int ret = fstat(fd, &buf);
189 if (ret) return 0;
190
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530191 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700192 if (S_ISREG(buf.st_mode)) {
193 computed_size = buf.st_size - reserve_len;
194 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530195 uint64_t block_device_size = get_block_device_size(fd);
196 if (block_device_size < reserve_len ||
197 block_device_size > std::numeric_limits<int64_t>::max()) {
198 computed_size = 0;
199 } else {
200 computed_size = block_device_size - reserve_len;
201 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700202 } else {
203 computed_size = 0;
204 }
205
206 return computed_size;
207}
208
Yifan Hongd81b8e32018-12-17 14:29:06 -0800209int format_volume(const std::string& volume, const std::string& directory) {
210 const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
Tao Bao3c00fac2017-07-22 16:46:54 -0700211 if (v == nullptr) {
212 LOG(ERROR) << "unknown volume \"" << volume << "\"";
213 return -1;
214 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800215 if (v->fs_type == "ramdisk") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700216 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
217 return -1;
218 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800219 if (v->mount_point != volume) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700220 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
221 return -1;
222 }
Alessandro Astone606b3a72020-03-30 00:01:21 +0200223 if (ensure_volume_unmounted(v->blk_device) != 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700224 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
225 return -1;
226 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800227 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700228 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700230 }
231
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800232 bool needs_casefold = false;
233 bool needs_projid = false;
234
235 if (volume == "/data") {
Martijn Coenen5a4a7ff2020-04-15 11:52:21 +0200236 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
237 needs_projid = android::base::GetBoolProperty("external_storage.projid.enabled", false);
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800238 }
239
Tao Bao3c00fac2017-07-22 16:46:54 -0700240 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
241 // metadata. Wipe it too.
Yifan Hongd81b8e32018-12-17 14:29:06 -0800242 if (!v->key_loc.empty() && v->key_loc[0] == '/') {
Tao Bao3c00fac2017-07-22 16:46:54 -0700243 LOG(INFO) << "Wiping " << v->key_loc;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800244 int fd = open(v->key_loc.c_str(), O_WRONLY | O_CREAT, 0644);
Tao Bao3c00fac2017-07-22 16:46:54 -0700245 if (fd == -1) {
246 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
247 return -1;
248 }
249 wipe_block_device(fd, get_file_size(fd));
250 close(fd);
251 }
252
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530253 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800254 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700255 length = v->length;
Yifan Hongd81b8e32018-12-17 14:29:06 -0800256 } else if (v->length < 0 || v->key_loc == "footer") {
257 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
Tao Bao3c00fac2017-07-22 16:46:54 -0700258 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530259 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700260 return -1;
261 }
xunchang24788852019-03-22 16:08:52 -0700262 length = get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700263 if (length <= 0) {
xunchang24788852019-03-22 16:08:52 -0700264 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700265 return -1;
266 }
267 }
268
Yifan Hongd81b8e32018-12-17 14:29:06 -0800269 if (v->fs_type == "ext4") {
Tao Bao3c00fac2017-07-22 16:46:54 -0700270 static constexpr int kBlockSize = 4096;
271 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900272 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700273 };
274
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800275 // Project ID's require wider inodes. The Quotas themselves are enabled by tune2fs on boot.
276 if (needs_projid) {
277 mke2fs_args.push_back("-I");
278 mke2fs_args.push_back("512");
279 }
280
Jaegeuk Kima8d36e12020-02-11 15:24:54 -0800281 if (v->fs_mgr_flags.ext_meta_csum) {
282 mke2fs_args.push_back("-O");
283 mke2fs_args.push_back("metadata_csum");
284 mke2fs_args.push_back("-O");
285 mke2fs_args.push_back("64bit");
286 mke2fs_args.push_back("-O");
287 mke2fs_args.push_back("extent");
288 }
289
Tao Bao3c00fac2017-07-22 16:46:54 -0700290 int raid_stride = v->logical_blk_size / kBlockSize;
291 int raid_stripe_width = v->erase_blk_size / kBlockSize;
292 // stride should be the max of 8KB and logical block size
293 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
294 raid_stride = 8192 / kBlockSize;
295 }
296 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
297 mke2fs_args.push_back("-E");
298 mke2fs_args.push_back(
299 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
300 }
301 mke2fs_args.push_back(v->blk_device);
302 if (length != 0) {
303 mke2fs_args.push_back(std::to_string(length / kBlockSize));
304 }
305
306 int result = exec_cmd(mke2fs_args);
Yifan Hongd81b8e32018-12-17 14:29:06 -0800307 if (result == 0 && !directory.empty()) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700308 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900309 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700310 };
311 result = exec_cmd(e2fsdroid_args);
312 }
313
314 if (result != 0) {
315 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
316 return -1;
317 }
318 return 0;
319 }
320
321 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700322 static constexpr int kSectorSize = 4096;
Jaegeuk Kim43582622018-04-02 13:37:35 -0700323 std::vector<std::string> make_f2fs_cmd = {
Tao Baoc674dfb2018-12-20 14:25:15 -0800324 "/system/bin/make_f2fs",
325 "-g",
326 "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700327 };
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800328 if (needs_projid) {
329 make_f2fs_cmd.push_back("-O");
330 make_f2fs_cmd.push_back("project_quota,extra_attr");
331 }
332 if (needs_casefold) {
333 make_f2fs_cmd.push_back("-O");
334 make_f2fs_cmd.push_back("casefold");
335 make_f2fs_cmd.push_back("-C");
336 make_f2fs_cmd.push_back("utf8");
337 }
Jaegeuk Kim32106002020-01-14 11:00:37 -0800338 if (v->fs_mgr_flags.fs_compress) {
339 make_f2fs_cmd.push_back("-O");
340 make_f2fs_cmd.push_back("compression");
341 make_f2fs_cmd.push_back("-O");
342 make_f2fs_cmd.push_back("extra_attr");
343 }
Daniel Rosenbergf25b9772019-12-16 18:32:00 -0800344 make_f2fs_cmd.push_back(v->blk_device);
Jaegeuk Kim43582622018-04-02 13:37:35 -0700345 if (length >= kSectorSize) {
346 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700347 }
348
Tao Baoc674dfb2018-12-20 14:25:15 -0800349 if (exec_cmd(make_f2fs_cmd) != 0) {
350 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700351 return -1;
352 }
Tao Baoc674dfb2018-12-20 14:25:15 -0800353 if (!directory.empty()) {
354 std::vector<std::string> sload_f2fs_cmd = {
355 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
356 };
357 if (exec_cmd(sload_f2fs_cmd) != 0) {
358 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
359 return -1;
360 }
361 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700362 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800363}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700364
Yifan Hongd81b8e32018-12-17 14:29:06 -0800365int format_volume(const std::string& volume) {
366 return format_volume(volume, "");
Paul Lawrenced0db3372015-11-05 13:38:40 -0800367}
368
Doug Zongker239ac6a2013-08-20 16:03:25 -0700369int setup_install_mounts() {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800370 if (fstab.empty()) {
Tao Bao57130c42017-05-10 12:11:21 -0700371 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
372 return -1;
373 }
Yifan Hongd81b8e32018-12-17 14:29:06 -0800374 for (const FstabEntry& entry : fstab) {
Tao Bao57130c42017-05-10 12:11:21 -0700375 // We don't want to do anything with "/".
Yifan Hongd81b8e32018-12-17 14:29:06 -0800376 if (entry.mount_point == "/") {
Tao Bao57130c42017-05-10 12:11:21 -0700377 continue;
378 }
379
Yifan Hongd81b8e32018-12-17 14:29:06 -0800380 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
381 if (ensure_path_mounted(entry.mount_point) != 0) {
382 LOG(ERROR) << "Failed to mount " << entry.mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700383 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700384 }
385 } else {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800386 if (ensure_path_unmounted(entry.mount_point) != 0) {
387 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700388 return -1;
389 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700390 }
Tao Bao57130c42017-05-10 12:11:21 -0700391 }
392 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700393}
Tianjie Xu164c60a2019-05-15 13:59:39 -0700394
395bool HasCache() {
396 CHECK(!fstab.empty());
397 static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
398 return has_cache;
399}
Erfan Abdia24643e2020-04-14 23:07:33 +0430400
aleastob4eca162021-01-17 20:31:22 +0100401static bool logical_partitions_auto_mapped = false;
402
403void map_logical_partitions() {
404 if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
405 !logical_partitions_mapped()) {
406 std::string super_name = fs_mgr_get_super_partition_name();
407 if (!android::fs_mgr::CreateLogicalPartitions("/dev/block/by-name/" + super_name)) {
408 LOG(ERROR) << "Failed to map logical partitions";
409 } else {
410 logical_partitions_auto_mapped = true;
411 }
412 }
413}
414
415bool dm_find_system() {
416 auto rec = GetEntryForPath(&fstab, android::fs_mgr::GetSystemRoot());
417 if (!rec->fs_mgr_flags.logical) {
418 return false;
419 }
420 // If the fstab entry for system it's a path instead of a name, then it was already mapped
421 if (rec->blk_device[0] != '/') {
422 if (DeviceMapper::Instance().GetState(rec->blk_device) == DmDeviceState::INVALID) {
423 return false;
424 }
425 }
426 return true;
427}
428
Erfan Abdia24643e2020-04-14 23:07:33 +0430429bool logical_partitions_mapped() {
aleastob4eca162021-01-17 20:31:22 +0100430 return android::fs_mgr::LogicalPartitionsMapped() || logical_partitions_auto_mapped ||
431 dm_find_system();
Erfan Abdia24643e2020-04-14 23:07:33 +0430432}