blob: dc347848a7b29151e15f0b345b743cfc8d9576e0 [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
Elliott Hughes63a31922016-06-09 17:41:22 -070017#include "roots.h"
18
Tao Baobb10e582017-07-22 16:30:34 -070019#include <ctype.h>
20#include <fcntl.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053021#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070023#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024#include <sys/mount.h>
25#include <sys/stat.h>
26#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070027#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include <unistd.h>
29
Tao Bao3c00fac2017-07-22 16:46:54 -070030#include <algorithm>
31#include <string>
32#include <vector>
33
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070034#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070035#include <android-base/properties.h>
36#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070037#include <android-base/unique_fd.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>
David Anderson2b2f4232018-10-29 18:48:56 -070041#include <fs_mgr_dm_linear.h>
Tao Baode40ba52016-10-05 23:17:01 -070042
Tao Bao9a319f02018-01-04 13:19:11 -080043#include "otautil/mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Tao Baobb10e582017-07-22 16:30:34 -070045static struct fstab* fstab = nullptr;
David Anderson2b2f4232018-10-29 18:48:56 -070046static bool did_map_logical_partitions = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Tao Baobb10e582017-07-22 16:30:34 -070048extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050049
Tao Baobb10e582017-07-22 16:30:34 -070050void load_volume_table() {
51 fstab = fs_mgr_read_fstab_default();
52 if (!fstab) {
53 LOG(ERROR) << "Failed to read default fstab";
54 return;
55 }
Doug Zongker2810ced2011-02-17 15:55:21 -080056
Tao Baobb10e582017-07-22 16:30:34 -070057 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
58 if (ret == -1) {
59 LOG(ERROR) << "Failed to add /tmp entry to fstab";
60 fs_mgr_free_fstab(fstab);
61 fstab = nullptr;
62 return;
63 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070064
Tao Baobb10e582017-07-22 16:30:34 -070065 printf("recovery filesystem table\n");
66 printf("=========================\n");
67 for (int i = 0; i < fstab->num_entries; ++i) {
68 const Volume* v = &fstab->recs[i];
69 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
70 }
71 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070072}
73
Tao Bao3e18f2b2017-09-29 17:11:13 -070074Volume* volume_for_mount_point(const std::string& mount_point) {
75 return fs_mgr_get_entry_for_mount_point(fstab, mount_point);
76}
77
Tao Bao2dfc1a32017-09-27 13:12:11 -070078// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
79// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
80// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
81// first match or nullptr.
Tao Bao3e18f2b2017-09-29 17:11:13 -070082static Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070083 if (path == nullptr || path[0] == '\0') return nullptr;
84 std::string str(path);
85 while (true) {
Tao Baoad774b22017-09-29 10:39:08 -070086 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str);
Tao Bao2dfc1a32017-09-27 13:12:11 -070087 if (result != nullptr || str == "/") {
88 return result;
89 }
90 size_t slash = str.find_last_of('/');
91 if (slash == std::string::npos) return nullptr;
92 if (slash == 0) {
93 str = "/";
94 } else {
95 str = str.substr(0, slash);
96 }
97 }
98 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099}
100
Tao Baoabb8f772015-07-30 14:43:27 -0700101// Mount the volume specified by path at the given mount_point.
102int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -0700103 Volume* v = volume_for_path(path);
104 if (v == nullptr) {
105 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800106 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700107 }
108 if (strcmp(v->fs_type, "ramdisk") == 0) {
109 // The ramdisk is always mounted.
110 return 0;
111 }
112
113 if (!scan_mounted_volumes()) {
114 LOG(ERROR) << "Failed to scan mounted volumes";
115 return -1;
116 }
117
118 if (!mount_point) {
119 mount_point = v->mount_point;
120 }
121
David Anderson2b2f4232018-10-29 18:48:56 -0700122 // If we can't acquire the block device for a logical partition, it likely
123 // was never created. In that case we try to create it.
124 if (fs_mgr_is_logical(v) && !fs_mgr_update_logical_partition(v)) {
125 if (did_map_logical_partitions) {
126 LOG(ERROR) << "Failed to find block device for partition";
127 return -1;
128 }
129 std::string super_name = fs_mgr_get_super_partition_name();
130 if (!android::fs_mgr::CreateLogicalPartitions(super_name)) {
131 LOG(ERROR) << "Failed to create logical partitions";
132 return -1;
133 }
134 did_map_logical_partitions = true;
135 if (!fs_mgr_update_logical_partition(v)) {
136 LOG(ERROR) << "Failed to find block device for partition";
137 return -1;
138 }
139 }
140
Tao Baobb10e582017-07-22 16:30:34 -0700141 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
142 if (mv != nullptr) {
143 // Volume is already mounted.
144 return 0;
145 }
146
147 mkdir(mount_point, 0755); // in case it doesn't already exist
148
149 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
Lianjun Huang5d7be6b2018-09-03 18:07:35 +0800150 strcmp(v->fs_type, "vfat") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700151 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
152 if (result == -1 && fs_mgr_is_formattable(v)) {
153 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
154 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
155 if (fs_mgr_do_format(v, crypt_footer) == 0) {
156 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
157 } else {
158 PLOG(ERROR) << "Failed to format " << mount_point;
159 return -1;
160 }
161 }
162
163 if (result == -1) {
164 PLOG(ERROR) << "Failed to mount " << mount_point;
165 return -1;
166 }
167 return 0;
168 }
169
170 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
171 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172}
173
Tao Baoabb8f772015-07-30 14:43:27 -0700174int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700175 // Mount at the default mount point.
176 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700177}
178
Doug Zongkerd4208f92010-09-20 12:16:13 -0700179int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700180 const Volume* v = volume_for_path(path);
181 if (v == nullptr) {
182 LOG(ERROR) << "unknown volume for path [" << path << "]";
183 return -1;
184 }
185 if (strcmp(v->fs_type, "ramdisk") == 0) {
186 // The ramdisk is always mounted; you can't unmount it.
187 return -1;
188 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800189
Tao Baobb10e582017-07-22 16:30:34 -0700190 if (!scan_mounted_volumes()) {
191 LOG(ERROR) << "Failed to scan mounted volumes";
192 return -1;
193 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700194
Tao Baobb10e582017-07-22 16:30:34 -0700195 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
196 if (mv == nullptr) {
197 // Volume is already unmounted.
198 return 0;
199 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200
Tao Baobb10e582017-07-22 16:30:34 -0700201 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700202}
203
Tao Bao3c00fac2017-07-22 16:46:54 -0700204static int exec_cmd(const std::vector<std::string>& args) {
205 CHECK_NE(static_cast<size_t>(0), args.size());
206
207 std::vector<char*> argv(args.size());
208 std::transform(args.cbegin(), args.cend(), argv.begin(),
209 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
210 argv.push_back(nullptr);
211
212 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800213 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700214 execv(argv[0], argv.data());
215 _exit(EXIT_FAILURE);
216 }
217
218 int status;
219 waitpid(child, &status, 0);
220 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
221 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
222 }
223 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700224}
225
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530226static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700227 struct stat buf;
228 int ret = fstat(fd, &buf);
229 if (ret) return 0;
230
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530231 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700232 if (S_ISREG(buf.st_mode)) {
233 computed_size = buf.st_size - reserve_len;
234 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530235 uint64_t block_device_size = get_block_device_size(fd);
236 if (block_device_size < reserve_len ||
237 block_device_size > std::numeric_limits<int64_t>::max()) {
238 computed_size = 0;
239 } else {
240 computed_size = block_device_size - reserve_len;
241 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700242 } else {
243 computed_size = 0;
244 }
245
246 return computed_size;
247}
248
Paul Lawrenced0db3372015-11-05 13:38:40 -0800249int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700250 const Volume* v = volume_for_path(volume);
251 if (v == nullptr) {
252 LOG(ERROR) << "unknown volume \"" << volume << "\"";
253 return -1;
254 }
255 if (strcmp(v->fs_type, "ramdisk") == 0) {
256 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
257 return -1;
258 }
259 if (strcmp(v->mount_point, volume) != 0) {
260 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
261 return -1;
262 }
263 if (ensure_path_unmounted(volume) != 0) {
264 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
265 return -1;
266 }
267 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700268 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700270 }
271
272 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
273 // metadata. Wipe it too.
274 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
275 LOG(INFO) << "Wiping " << v->key_loc;
276 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
277 if (fd == -1) {
278 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
279 return -1;
280 }
281 wipe_block_device(fd, get_file_size(fd));
282 close(fd);
283 }
284
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530285 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800286 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700287 length = v->length;
Jin Qiancc100082017-11-17 23:53:22 -0800288 } else if (v->length < 0 ||
289 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700290 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
291 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530292 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700293 return -1;
294 }
Jin Qiancc100082017-11-17 23:53:22 -0800295 length =
296 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700297 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800298 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
299 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700300 return -1;
301 }
302 }
303
304 if (strcmp(v->fs_type, "ext4") == 0) {
305 static constexpr int kBlockSize = 4096;
306 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900307 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700308 };
309
310 int raid_stride = v->logical_blk_size / kBlockSize;
311 int raid_stripe_width = v->erase_blk_size / kBlockSize;
312 // stride should be the max of 8KB and logical block size
313 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
314 raid_stride = 8192 / kBlockSize;
315 }
316 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
317 mke2fs_args.push_back("-E");
318 mke2fs_args.push_back(
319 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
320 }
321 mke2fs_args.push_back(v->blk_device);
322 if (length != 0) {
323 mke2fs_args.push_back(std::to_string(length / kBlockSize));
324 }
325
326 int result = exec_cmd(mke2fs_args);
327 if (result == 0 && directory != nullptr) {
328 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900329 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700330 };
331 result = exec_cmd(e2fsdroid_args);
332 }
333
334 if (result != 0) {
335 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
336 return -1;
337 }
338 return 0;
339 }
340
341 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700342 static constexpr int kSectorSize = 4096;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800343 std::string cmd("/sbin/mkfs.f2fs");
Jaegeuk Kim43582622018-04-02 13:37:35 -0700344 // clang-format off
345 std::vector<std::string> make_f2fs_cmd = {
346 cmd,
347 "-d1",
348 "-f",
349 "-O", "encrypt",
350 "-O", "quota",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700351 "-O", "verity",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700352 "-w", std::to_string(kSectorSize),
353 v->blk_device,
354 };
355 // clang-format on
356 if (length >= kSectorSize) {
357 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700358 }
359
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800360 int result = exec_cmd(make_f2fs_cmd);
361 if (result == 0 && directory != nullptr) {
362 cmd = "/sbin/sload.f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700363 // clang-format off
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800364 std::vector<std::string> sload_f2fs_cmd = {
Jaegeuk Kim43582622018-04-02 13:37:35 -0700365 cmd,
366 "-f", directory,
367 "-t", volume,
368 v->blk_device,
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800369 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700370 // clang-format on
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800371 result = exec_cmd(sload_f2fs_cmd);
372 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700373 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800374 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700375 return -1;
376 }
377 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800378}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700379
Paul Lawrenced0db3372015-11-05 13:38:40 -0800380int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700381 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800382}
383
Doug Zongker239ac6a2013-08-20 16:03:25 -0700384int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700385 if (fstab == nullptr) {
386 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
387 return -1;
388 }
389 for (int i = 0; i < fstab->num_entries; ++i) {
390 const Volume* v = fstab->recs + i;
391
392 // We don't want to do anything with "/".
393 if (strcmp(v->mount_point, "/") == 0) {
394 continue;
395 }
396
397 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
398 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700399 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700400 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700401 }
402 } else {
403 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700404 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700405 return -1;
406 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700407 }
Tao Bao57130c42017-05-10 12:11:21 -0700408 }
409 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700410}
David Anderson2b2f4232018-10-29 18:48:56 -0700411
412bool logical_partitions_mapped() {
413 return did_map_logical_partitions;
414}