blob: e2d5d65437cd2605cd67d70c6805c8152084fb1d [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);
131 if (result == -1 && fs_mgr_is_formattable(v)) {
132 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
133 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
134 if (fs_mgr_do_format(v, crypt_footer) == 0) {
135 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
136 } else {
137 PLOG(ERROR) << "Failed to format " << mount_point;
138 return -1;
139 }
140 }
141
142 if (result == -1) {
143 PLOG(ERROR) << "Failed to mount " << mount_point;
144 return -1;
145 }
146 return 0;
147 }
148
149 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
150 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151}
152
Tao Baoabb8f772015-07-30 14:43:27 -0700153int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700154 // Mount at the default mount point.
155 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700156}
157
Doug Zongkerd4208f92010-09-20 12:16:13 -0700158int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700159 const Volume* v = volume_for_path(path);
160 if (v == nullptr) {
161 LOG(ERROR) << "unknown volume for path [" << path << "]";
162 return -1;
163 }
164 if (strcmp(v->fs_type, "ramdisk") == 0) {
165 // The ramdisk is always mounted; you can't unmount it.
166 return -1;
167 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Tao Baobb10e582017-07-22 16:30:34 -0700169 if (!scan_mounted_volumes()) {
170 LOG(ERROR) << "Failed to scan mounted volumes";
171 return -1;
172 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700173
Tao Baobb10e582017-07-22 16:30:34 -0700174 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
175 if (mv == nullptr) {
176 // Volume is already unmounted.
177 return 0;
178 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179
Tao Baobb10e582017-07-22 16:30:34 -0700180 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181}
182
Tao Bao3c00fac2017-07-22 16:46:54 -0700183static int exec_cmd(const std::vector<std::string>& args) {
184 CHECK_NE(static_cast<size_t>(0), args.size());
185
186 std::vector<char*> argv(args.size());
187 std::transform(args.cbegin(), args.cend(), argv.begin(),
188 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
189 argv.push_back(nullptr);
190
191 pid_t child;
192 if ((child = vfork()) == 0) {
193 execv(argv[0], argv.data());
194 _exit(EXIT_FAILURE);
195 }
196
197 int status;
198 waitpid(child, &status, 0);
199 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
200 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
201 }
202 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700203}
204
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530205static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700206 struct stat buf;
207 int ret = fstat(fd, &buf);
208 if (ret) return 0;
209
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530210 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700211 if (S_ISREG(buf.st_mode)) {
212 computed_size = buf.st_size - reserve_len;
213 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530214 uint64_t block_device_size = get_block_device_size(fd);
215 if (block_device_size < reserve_len ||
216 block_device_size > std::numeric_limits<int64_t>::max()) {
217 computed_size = 0;
218 } else {
219 computed_size = block_device_size - reserve_len;
220 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700221 } else {
222 computed_size = 0;
223 }
224
225 return computed_size;
226}
227
Paul Lawrenced0db3372015-11-05 13:38:40 -0800228int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700229 const Volume* v = volume_for_path(volume);
230 if (v == nullptr) {
231 LOG(ERROR) << "unknown volume \"" << volume << "\"";
232 return -1;
233 }
234 if (strcmp(v->fs_type, "ramdisk") == 0) {
235 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
236 return -1;
237 }
238 if (strcmp(v->mount_point, volume) != 0) {
239 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
240 return -1;
241 }
242 if (ensure_path_unmounted(volume) != 0) {
243 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
244 return -1;
245 }
246 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700247 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700249 }
250
251 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
252 // metadata. Wipe it too.
253 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
254 LOG(INFO) << "Wiping " << v->key_loc;
255 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
256 if (fd == -1) {
257 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
258 return -1;
259 }
260 wipe_block_device(fd, get_file_size(fd));
261 close(fd);
262 }
263
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530264 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800265 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700266 length = v->length;
Jin Qiancc100082017-11-17 23:53:22 -0800267 } else if (v->length < 0 ||
268 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700269 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
270 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530271 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700272 return -1;
273 }
Jin Qiancc100082017-11-17 23:53:22 -0800274 length =
275 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700276 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800277 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
278 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700279 return -1;
280 }
281 }
282
283 if (strcmp(v->fs_type, "ext4") == 0) {
284 static constexpr int kBlockSize = 4096;
285 std::vector<std::string> mke2fs_args = {
286 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
287 };
288
289 int raid_stride = v->logical_blk_size / kBlockSize;
290 int raid_stripe_width = v->erase_blk_size / kBlockSize;
291 // stride should be the max of 8KB and logical block size
292 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
293 raid_stride = 8192 / kBlockSize;
294 }
295 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
296 mke2fs_args.push_back("-E");
297 mke2fs_args.push_back(
298 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
299 }
300 mke2fs_args.push_back(v->blk_device);
301 if (length != 0) {
302 mke2fs_args.push_back(std::to_string(length / kBlockSize));
303 }
304
305 int result = exec_cmd(mke2fs_args);
306 if (result == 0 && directory != nullptr) {
307 std::vector<std::string> e2fsdroid_args = {
308 "/sbin/e2fsdroid_static",
309 "-e",
310 "-f",
311 directory,
312 "-a",
313 volume,
314 v->blk_device,
315 };
316 result = exec_cmd(e2fsdroid_args);
317 }
318
319 if (result != 0) {
320 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
321 return -1;
322 }
323 return 0;
324 }
325
326 // Has to be f2fs because we checked earlier.
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800327 std::string cmd("/sbin/mkfs.f2fs");
328 std::vector<std::string> make_f2fs_cmd = { cmd, "-d1", "-f", "-O",
329 "encrypt", "-O", "quota", v->blk_device };
Tao Bao3c00fac2017-07-22 16:46:54 -0700330 if (length >= 512) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800331 make_f2fs_cmd.push_back(std::to_string(length / 512));
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";
337 std::vector<std::string> sload_f2fs_cmd = {
338 cmd, "-f", directory, "-t", volume, v->blk_device,
339 };
340 result = exec_cmd(sload_f2fs_cmd);
341 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700342 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800343 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700344 return -1;
345 }
346 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800347}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700348
Paul Lawrenced0db3372015-11-05 13:38:40 -0800349int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700350 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800351}
352
Doug Zongker239ac6a2013-08-20 16:03:25 -0700353int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700354 if (fstab == nullptr) {
355 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
356 return -1;
357 }
358 for (int i = 0; i < fstab->num_entries; ++i) {
359 const Volume* v = fstab->recs + i;
360
361 // We don't want to do anything with "/".
362 if (strcmp(v->mount_point, "/") == 0) {
363 continue;
364 }
365
366 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
367 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700368 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700369 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700370 }
371 } else {
372 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700373 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700374 return -1;
375 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700376 }
Tao Bao57130c42017-05-10 12:11:21 -0700377 }
378 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700379}