blob: 303381ef075c6777978c7d2d116a704ccf0c64cd [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>
Tao Baode40ba52016-10-05 23:17:01 -070041
Elliott Hughes63a31922016-06-09 17:41:22 -070042#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Tao Baobb10e582017-07-22 16:30:34 -070044static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tao Baobb10e582017-07-22 16:30:34 -070046extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050047
Tao Baobb10e582017-07-22 16:30:34 -070048void load_volume_table() {
49 fstab = fs_mgr_read_fstab_default();
50 if (!fstab) {
51 LOG(ERROR) << "Failed to read default fstab";
52 return;
53 }
Doug Zongker2810ced2011-02-17 15:55:21 -080054
Tao Baobb10e582017-07-22 16:30:34 -070055 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
56 if (ret == -1) {
57 LOG(ERROR) << "Failed to add /tmp entry to fstab";
58 fs_mgr_free_fstab(fstab);
59 fstab = nullptr;
60 return;
61 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070062
Tao Baobb10e582017-07-22 16:30:34 -070063 printf("recovery filesystem table\n");
64 printf("=========================\n");
65 for (int i = 0; i < fstab->num_entries; ++i) {
66 const Volume* v = &fstab->recs[i];
67 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
68 }
69 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070070}
71
Tao Bao3e18f2b2017-09-29 17:11:13 -070072Volume* volume_for_mount_point(const std::string& mount_point) {
73 return fs_mgr_get_entry_for_mount_point(fstab, mount_point);
74}
75
Tao Bao2dfc1a32017-09-27 13:12:11 -070076// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
77// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
78// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
79// first match or nullptr.
Tao Bao3e18f2b2017-09-29 17:11:13 -070080static Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070081 if (path == nullptr || path[0] == '\0') return nullptr;
82 std::string str(path);
83 while (true) {
Tao Baoad774b22017-09-29 10:39:08 -070084 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str);
Tao Bao2dfc1a32017-09-27 13:12:11 -070085 if (result != nullptr || str == "/") {
86 return result;
87 }
88 size_t slash = str.find_last_of('/');
89 if (slash == std::string::npos) return nullptr;
90 if (slash == 0) {
91 str = "/";
92 } else {
93 str = str.substr(0, slash);
94 }
95 }
96 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097}
98
Tao Baoabb8f772015-07-30 14:43:27 -070099// Mount the volume specified by path at the given mount_point.
100int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -0700101 Volume* v = volume_for_path(path);
102 if (v == nullptr) {
103 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700105 }
106 if (strcmp(v->fs_type, "ramdisk") == 0) {
107 // The ramdisk is always mounted.
108 return 0;
109 }
110
111 if (!scan_mounted_volumes()) {
112 LOG(ERROR) << "Failed to scan mounted volumes";
113 return -1;
114 }
115
116 if (!mount_point) {
117 mount_point = v->mount_point;
118 }
119
120 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
121 if (mv != nullptr) {
122 // Volume is already mounted.
123 return 0;
124 }
125
126 mkdir(mount_point, 0755); // in case it doesn't already exist
127
128 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
129 strcmp(v->fs_type, "vfat") == 0) {
130 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
Tao Baobb10e582017-07-22 16:30:34 -0700131 if (result == -1) {
132 PLOG(ERROR) << "Failed to mount " << mount_point;
133 return -1;
134 }
135 return 0;
136 }
137
138 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
139 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140}
141
Tao Baoabb8f772015-07-30 14:43:27 -0700142int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700143 // Mount at the default mount point.
144 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700145}
146
Doug Zongkerd4208f92010-09-20 12:16:13 -0700147int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700148 const Volume* v = volume_for_path(path);
149 if (v == nullptr) {
150 LOG(ERROR) << "unknown volume for path [" << path << "]";
151 return -1;
152 }
153 if (strcmp(v->fs_type, "ramdisk") == 0) {
154 // The ramdisk is always mounted; you can't unmount it.
155 return -1;
156 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157
Tao Baobb10e582017-07-22 16:30:34 -0700158 if (!scan_mounted_volumes()) {
159 LOG(ERROR) << "Failed to scan mounted volumes";
160 return -1;
161 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700162
Tao Baobb10e582017-07-22 16:30:34 -0700163 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
164 if (mv == nullptr) {
165 // Volume is already unmounted.
166 return 0;
167 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Tao Baobb10e582017-07-22 16:30:34 -0700169 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700170}
171
Tao Bao3c00fac2017-07-22 16:46:54 -0700172static int exec_cmd(const std::vector<std::string>& args) {
173 CHECK_NE(static_cast<size_t>(0), args.size());
174
175 std::vector<char*> argv(args.size());
176 std::transform(args.cbegin(), args.cend(), argv.begin(),
177 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
178 argv.push_back(nullptr);
179
180 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800181 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700182 execv(argv[0], argv.data());
183 _exit(EXIT_FAILURE);
184 }
185
186 int status;
187 waitpid(child, &status, 0);
188 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
189 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
190 }
191 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700192}
193
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530194static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700195 struct stat buf;
196 int ret = fstat(fd, &buf);
197 if (ret) return 0;
198
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530199 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700200 if (S_ISREG(buf.st_mode)) {
201 computed_size = buf.st_size - reserve_len;
202 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530203 uint64_t block_device_size = get_block_device_size(fd);
204 if (block_device_size < reserve_len ||
205 block_device_size > std::numeric_limits<int64_t>::max()) {
206 computed_size = 0;
207 } else {
208 computed_size = block_device_size - reserve_len;
209 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700210 } else {
211 computed_size = 0;
212 }
213
214 return computed_size;
215}
216
Paul Lawrenced0db3372015-11-05 13:38:40 -0800217int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700218 const Volume* v = volume_for_path(volume);
219 if (v == nullptr) {
220 LOG(ERROR) << "unknown volume \"" << volume << "\"";
221 return -1;
222 }
223 if (strcmp(v->fs_type, "ramdisk") == 0) {
224 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
225 return -1;
226 }
227 if (strcmp(v->mount_point, volume) != 0) {
228 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
229 return -1;
230 }
231 if (ensure_path_unmounted(volume) != 0) {
232 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
233 return -1;
234 }
235 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700236 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700238 }
239
240 // 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.
242 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
243 LOG(INFO) << "Wiping " << v->key_loc;
244 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
245 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;
Jin Qiancc100082017-11-17 23:53:22 -0800256 } else if (v->length < 0 ||
257 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700258 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
259 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530260 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700261 return -1;
262 }
Jin Qiancc100082017-11-17 23:53:22 -0800263 length =
264 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700265 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800266 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
267 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700268 return -1;
269 }
270 }
271
272 if (strcmp(v->fs_type, "ext4") == 0) {
273 static constexpr int kBlockSize = 4096;
274 std::vector<std::string> mke2fs_args = {
275 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
276 };
277
278 int raid_stride = v->logical_blk_size / kBlockSize;
279 int raid_stripe_width = v->erase_blk_size / kBlockSize;
280 // stride should be the max of 8KB and logical block size
281 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
282 raid_stride = 8192 / kBlockSize;
283 }
284 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
285 mke2fs_args.push_back("-E");
286 mke2fs_args.push_back(
287 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
288 }
289 mke2fs_args.push_back(v->blk_device);
290 if (length != 0) {
291 mke2fs_args.push_back(std::to_string(length / kBlockSize));
292 }
293
294 int result = exec_cmd(mke2fs_args);
295 if (result == 0 && directory != nullptr) {
296 std::vector<std::string> e2fsdroid_args = {
297 "/sbin/e2fsdroid_static",
298 "-e",
299 "-f",
300 directory,
301 "-a",
302 volume,
303 v->blk_device,
304 };
305 result = exec_cmd(e2fsdroid_args);
306 }
307
308 if (result != 0) {
309 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
310 return -1;
311 }
312 return 0;
313 }
314
315 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700316 static constexpr int kSectorSize = 4096;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800317 std::string cmd("/sbin/mkfs.f2fs");
Jaegeuk Kim43582622018-04-02 13:37:35 -0700318 // clang-format off
319 std::vector<std::string> make_f2fs_cmd = {
320 cmd,
321 "-d1",
322 "-f",
323 "-O", "encrypt",
324 "-O", "quota",
Jaegeuk Kim2e5dc842018-04-05 22:42:13 -0700325 "-O", "verity",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700326 "-w", std::to_string(kSectorSize),
327 v->blk_device,
328 };
329 // clang-format on
330 if (length >= kSectorSize) {
331 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700332 }
333
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800334 int result = exec_cmd(make_f2fs_cmd);
335 if (result == 0 && directory != nullptr) {
336 cmd = "/sbin/sload.f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700337 // clang-format off
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800338 std::vector<std::string> sload_f2fs_cmd = {
Jaegeuk Kim43582622018-04-02 13:37:35 -0700339 cmd,
340 "-f", directory,
341 "-t", volume,
342 v->blk_device,
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800343 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700344 // clang-format on
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800345 result = exec_cmd(sload_f2fs_cmd);
346 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700347 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800348 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700349 return -1;
350 }
351 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700353
Paul Lawrenced0db3372015-11-05 13:38:40 -0800354int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700355 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800356}
357
Doug Zongker239ac6a2013-08-20 16:03:25 -0700358int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700359 if (fstab == nullptr) {
360 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
361 return -1;
362 }
363 for (int i = 0; i < fstab->num_entries; ++i) {
364 const Volume* v = fstab->recs + i;
365
366 // We don't want to do anything with "/".
367 if (strcmp(v->mount_point, "/") == 0) {
368 continue;
369 }
370
371 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
372 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700373 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700374 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700375 }
376 } else {
377 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700378 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700379 return -1;
380 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700381 }
Tao Bao57130c42017-05-10 12:11:21 -0700382 }
383 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700384}