blob: 835a1dda069ad9afd31295b875f697b82e2db663 [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>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
22#include <sys/mount.h>
23#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>
27
Tao Bao3c00fac2017-07-22 16:46:54 -070028#include <algorithm>
29#include <string>
30#include <vector>
31
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070032#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070033#include <android-base/properties.h>
34#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070035#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070036#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070037#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080038#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070039
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070041#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Tao Baobb10e582017-07-22 16:30:34 -070043static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Tao Baobb10e582017-07-22 16:30:34 -070045extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050046
Tao Baobb10e582017-07-22 16:30:34 -070047void load_volume_table() {
48 fstab = fs_mgr_read_fstab_default();
49 if (!fstab) {
50 LOG(ERROR) << "Failed to read default fstab";
51 return;
52 }
Doug Zongker2810ced2011-02-17 15:55:21 -080053
Tao Baobb10e582017-07-22 16:30:34 -070054 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
55 if (ret == -1) {
56 LOG(ERROR) << "Failed to add /tmp entry to fstab";
57 fs_mgr_free_fstab(fstab);
58 fstab = nullptr;
59 return;
60 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070061
Tao Baobb10e582017-07-22 16:30:34 -070062 printf("recovery filesystem table\n");
63 printf("=========================\n");
64 for (int i = 0; i < fstab->num_entries; ++i) {
65 const Volume* v = &fstab->recs[i];
66 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
67 }
68 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070069}
70
Tao Bao2dfc1a32017-09-27 13:12:11 -070071// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
72// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
73// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
74// first match or nullptr.
Doug Zongkerd4208f92010-09-20 12:16:13 -070075Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070076 if (path == nullptr || path[0] == '\0') return nullptr;
77 std::string str(path);
78 while (true) {
79 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str.c_str());
80 if (result != nullptr || str == "/") {
81 return result;
82 }
83 size_t slash = str.find_last_of('/');
84 if (slash == std::string::npos) return nullptr;
85 if (slash == 0) {
86 str = "/";
87 } else {
88 str = str.substr(0, slash);
89 }
90 }
91 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092}
93
Tao Baoabb8f772015-07-30 14:43:27 -070094// Mount the volume specified by path at the given mount_point.
95int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -070096 Volume* v = volume_for_path(path);
97 if (v == nullptr) {
98 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700100 }
101 if (strcmp(v->fs_type, "ramdisk") == 0) {
102 // The ramdisk is always mounted.
103 return 0;
104 }
105
106 if (!scan_mounted_volumes()) {
107 LOG(ERROR) << "Failed to scan mounted volumes";
108 return -1;
109 }
110
111 if (!mount_point) {
112 mount_point = v->mount_point;
113 }
114
115 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
116 if (mv != nullptr) {
117 // Volume is already mounted.
118 return 0;
119 }
120
121 mkdir(mount_point, 0755); // in case it doesn't already exist
122
123 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
124 strcmp(v->fs_type, "vfat") == 0) {
125 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
126 if (result == -1 && fs_mgr_is_formattable(v)) {
127 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
128 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
129 if (fs_mgr_do_format(v, crypt_footer) == 0) {
130 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
131 } else {
132 PLOG(ERROR) << "Failed to format " << mount_point;
133 return -1;
134 }
135 }
136
137 if (result == -1) {
138 PLOG(ERROR) << "Failed to mount " << mount_point;
139 return -1;
140 }
141 return 0;
142 }
143
144 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
145 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146}
147
Tao Baoabb8f772015-07-30 14:43:27 -0700148int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700149 // Mount at the default mount point.
150 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700151}
152
Doug Zongkerd4208f92010-09-20 12:16:13 -0700153int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700154 const Volume* v = volume_for_path(path);
155 if (v == nullptr) {
156 LOG(ERROR) << "unknown volume for path [" << path << "]";
157 return -1;
158 }
159 if (strcmp(v->fs_type, "ramdisk") == 0) {
160 // The ramdisk is always mounted; you can't unmount it.
161 return -1;
162 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163
Tao Baobb10e582017-07-22 16:30:34 -0700164 if (!scan_mounted_volumes()) {
165 LOG(ERROR) << "Failed to scan mounted volumes";
166 return -1;
167 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700168
Tao Baobb10e582017-07-22 16:30:34 -0700169 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
170 if (mv == nullptr) {
171 // Volume is already unmounted.
172 return 0;
173 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174
Tao Baobb10e582017-07-22 16:30:34 -0700175 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700176}
177
Tao Bao3c00fac2017-07-22 16:46:54 -0700178static int exec_cmd(const std::vector<std::string>& args) {
179 CHECK_NE(static_cast<size_t>(0), args.size());
180
181 std::vector<char*> argv(args.size());
182 std::transform(args.cbegin(), args.cend(), argv.begin(),
183 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
184 argv.push_back(nullptr);
185
186 pid_t child;
187 if ((child = vfork()) == 0) {
188 execv(argv[0], argv.data());
189 _exit(EXIT_FAILURE);
190 }
191
192 int status;
193 waitpid(child, &status, 0);
194 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
195 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
196 }
197 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700198}
199
Jin Qianf3ccad52017-07-24 10:34:35 -0700200static ssize_t get_file_size(int fd, uint64_t reserve_len) {
201 struct stat buf;
202 int ret = fstat(fd, &buf);
203 if (ret) return 0;
204
205 ssize_t computed_size;
206 if (S_ISREG(buf.st_mode)) {
207 computed_size = buf.st_size - reserve_len;
208 } else if (S_ISBLK(buf.st_mode)) {
209 computed_size = get_block_device_size(fd) - reserve_len;
210 } 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
253 ssize_t length = 0;
254 if (v->length != 0) {
255 length = v->length;
256 } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) {
257 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
258 if (fd == -1) {
259 PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device;
260 return -1;
261 }
262 length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
263 if (length <= 0) {
264 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
265 return -1;
266 }
267 }
268
269 if (strcmp(v->fs_type, "ext4") == 0) {
270 static constexpr int kBlockSize = 4096;
271 std::vector<std::string> mke2fs_args = {
272 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
273 };
274
275 int raid_stride = v->logical_blk_size / kBlockSize;
276 int raid_stripe_width = v->erase_blk_size / kBlockSize;
277 // stride should be the max of 8KB and logical block size
278 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
279 raid_stride = 8192 / kBlockSize;
280 }
281 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
282 mke2fs_args.push_back("-E");
283 mke2fs_args.push_back(
284 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
285 }
286 mke2fs_args.push_back(v->blk_device);
287 if (length != 0) {
288 mke2fs_args.push_back(std::to_string(length / kBlockSize));
289 }
290
291 int result = exec_cmd(mke2fs_args);
292 if (result == 0 && directory != nullptr) {
293 std::vector<std::string> e2fsdroid_args = {
294 "/sbin/e2fsdroid_static",
295 "-e",
296 "-f",
297 directory,
298 "-a",
299 volume,
300 v->blk_device,
301 };
302 result = exec_cmd(e2fsdroid_args);
303 }
304
305 if (result != 0) {
306 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
307 return -1;
308 }
309 return 0;
310 }
311
312 // Has to be f2fs because we checked earlier.
313 std::vector<std::string> f2fs_args = { "/sbin/mkfs.f2fs", "-t", "-d1", v->blk_device };
314 if (length >= 512) {
315 f2fs_args.push_back(std::to_string(length / 512));
316 }
317
318 int result = exec_cmd(f2fs_args);
319 if (result != 0) {
320 PLOG(ERROR) << "format_volume: Failed to make f2fs on " << v->blk_device;
321 return -1;
322 }
323 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800324}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700325
Paul Lawrenced0db3372015-11-05 13:38:40 -0800326int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700327 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800328}
329
Doug Zongker239ac6a2013-08-20 16:03:25 -0700330int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700331 if (fstab == nullptr) {
332 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
333 return -1;
334 }
335 for (int i = 0; i < fstab->num_entries; ++i) {
336 const Volume* v = fstab->recs + i;
337
338 // We don't want to do anything with "/".
339 if (strcmp(v->mount_point, "/") == 0) {
340 continue;
341 }
342
343 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
344 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700345 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700346 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700347 }
348 } else {
349 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700350 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700351 return -1;
352 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700353 }
Tao Bao57130c42017-05-10 12:11:21 -0700354 }
355 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700356}