blob: 0360bde16d51ec9ed6c040a88fb1695a84f3bd33 [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
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070028#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070029#include <android-base/properties.h>
30#include <android-base/stringprintf.h>
Tao Baobb10e582017-07-22 16:30:34 -070031#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070032#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080033#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070034
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070036#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Tao Baobb10e582017-07-22 16:30:34 -070038static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Tao Baobb10e582017-07-22 16:30:34 -070040extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050041
Tao Baobb10e582017-07-22 16:30:34 -070042void load_volume_table() {
43 fstab = fs_mgr_read_fstab_default();
44 if (!fstab) {
45 LOG(ERROR) << "Failed to read default fstab";
46 return;
47 }
Doug Zongker2810ced2011-02-17 15:55:21 -080048
Tao Baobb10e582017-07-22 16:30:34 -070049 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
50 if (ret == -1) {
51 LOG(ERROR) << "Failed to add /tmp entry to fstab";
52 fs_mgr_free_fstab(fstab);
53 fstab = nullptr;
54 return;
55 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070056
Tao Baobb10e582017-07-22 16:30:34 -070057 printf("recovery filesystem table\n");
58 printf("=========================\n");
59 for (int i = 0; i < fstab->num_entries; ++i) {
60 const Volume* v = &fstab->recs[i];
61 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
62 }
63 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070064}
65
66Volume* volume_for_path(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -070067 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068}
69
Tao Baoabb8f772015-07-30 14:43:27 -070070// Mount the volume specified by path at the given mount_point.
71int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -070072 Volume* v = volume_for_path(path);
73 if (v == nullptr) {
74 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075 return -1;
Tao Baobb10e582017-07-22 16:30:34 -070076 }
77 if (strcmp(v->fs_type, "ramdisk") == 0) {
78 // The ramdisk is always mounted.
79 return 0;
80 }
81
82 if (!scan_mounted_volumes()) {
83 LOG(ERROR) << "Failed to scan mounted volumes";
84 return -1;
85 }
86
87 if (!mount_point) {
88 mount_point = v->mount_point;
89 }
90
91 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
92 if (mv != nullptr) {
93 // Volume is already mounted.
94 return 0;
95 }
96
97 mkdir(mount_point, 0755); // in case it doesn't already exist
98
99 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
100 strcmp(v->fs_type, "vfat") == 0) {
101 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
102 if (result == -1 && fs_mgr_is_formattable(v)) {
103 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
104 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
105 if (fs_mgr_do_format(v, crypt_footer) == 0) {
106 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
107 } else {
108 PLOG(ERROR) << "Failed to format " << mount_point;
109 return -1;
110 }
111 }
112
113 if (result == -1) {
114 PLOG(ERROR) << "Failed to mount " << mount_point;
115 return -1;
116 }
117 return 0;
118 }
119
120 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
121 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122}
123
Tao Baoabb8f772015-07-30 14:43:27 -0700124int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700125 // Mount at the default mount point.
126 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700127}
128
Doug Zongkerd4208f92010-09-20 12:16:13 -0700129int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700130 const Volume* v = volume_for_path(path);
131 if (v == nullptr) {
132 LOG(ERROR) << "unknown volume for path [" << path << "]";
133 return -1;
134 }
135 if (strcmp(v->fs_type, "ramdisk") == 0) {
136 // The ramdisk is always mounted; you can't unmount it.
137 return -1;
138 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139
Tao Baobb10e582017-07-22 16:30:34 -0700140 if (!scan_mounted_volumes()) {
141 LOG(ERROR) << "Failed to scan mounted volumes";
142 return -1;
143 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700144
Tao Baobb10e582017-07-22 16:30:34 -0700145 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
146 if (mv == nullptr) {
147 // Volume is already unmounted.
148 return 0;
149 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150
Tao Baobb10e582017-07-22 16:30:34 -0700151 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700152}
153
JP Abgrall37aedb32014-06-16 19:07:39 -0700154static int exec_cmd(const char* path, char* const argv[]) {
155 int status;
156 pid_t child;
157 if ((child = vfork()) == 0) {
158 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800159 _exit(EXIT_FAILURE);
JP Abgrall37aedb32014-06-16 19:07:39 -0700160 }
161 waitpid(child, &status, 0);
162 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700163 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700164 }
165 return WEXITSTATUS(status);
166}
167
Paul Lawrenced0db3372015-11-05 13:38:40 -0800168int format_volume(const char* volume, const char* directory) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700169 Volume* v = volume_for_path(volume);
170 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700171 LOG(ERROR) << "unknown volume \"" << volume << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700172 return -1;
173 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700174 if (strcmp(v->fs_type, "ramdisk") == 0) {
175 // you can't format the ramdisk.
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700176 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700177 return -1;
178 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700179 if (strcmp(v->mount_point, volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700180 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181 return -1;
182 }
183
184 if (ensure_path_unmounted(volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700185 LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700186 return -1;
187 }
188
JP Abgrall37aedb32014-06-16 19:07:39 -0700189 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800190 // if there's a key_loc that looks like a path, it should be a
191 // block device for storing encryption metadata. wipe it too.
192 if (v->key_loc != NULL && v->key_loc[0] == '/') {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700193 LOG(INFO) << "wiping " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800194 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
195 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700196 LOG(ERROR) << "format_volume: failed to open " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800197 return -1;
198 }
199 wipe_block_device(fd, get_file_size(fd));
200 close(fd);
201 }
202
JP Abgrall37aedb32014-06-16 19:07:39 -0700203 ssize_t length = 0;
204 if (v->length != 0) {
205 length = v->length;
206 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
207 length = -CRYPT_FOOTER_OFFSET;
208 }
209 int result;
210 if (strcmp(v->fs_type, "ext4") == 0) {
Jin Qianded2dac2017-04-21 14:36:12 -0700211 static constexpr int block_size = 4096;
212 int raid_stride = v->logical_blk_size / block_size;
213 int raid_stripe_width = v->erase_blk_size / block_size;
214
215 // stride should be the max of 8kb and logical block size
216 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
217 raid_stride = 8192 / block_size;
218 }
219
220 const char* mke2fs_argv[] = { "/sbin/mke2fs_static",
221 "-F",
222 "-t",
223 "ext4",
224 "-b",
225 nullptr,
226 nullptr,
227 nullptr,
228 nullptr,
229 nullptr,
230 nullptr };
231
232 int i = 5;
233 std::string block_size_str = std::to_string(block_size);
234 mke2fs_argv[i++] = block_size_str.c_str();
235
236 std::string ext_args;
237 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
238 ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride,
239 raid_stripe_width);
240 mke2fs_argv[i++] = "-E";
241 mke2fs_argv[i++] = ext_args.c_str();
242 }
243
244 mke2fs_argv[i++] = v->blk_device;
245
246 std::string size_str = std::to_string(length / block_size);
247 if (length != 0) {
248 mke2fs_argv[i++] = size_str.c_str();
249 }
250
251 result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
252 if (result == 0 && directory != nullptr) {
253 const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static",
254 "-e",
Jin Qianded2dac2017-04-21 14:36:12 -0700255 "-f",
256 directory,
257 "-a",
258 volume,
259 v->blk_device,
260 nullptr };
261
262 result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
Tao Bao338be532017-07-11 16:45:04 -0700263 }
JP Abgrall37aedb32014-06-16 19:07:39 -0700264 } else { /* Has to be f2fs because we checked earlier. */
265 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700266 LOG(ERROR) << "format_volume: crypt footer + negative length (" << length
267 << ") not supported on " << v->fs_type;
JP Abgrall37aedb32014-06-16 19:07:39 -0700268 return -1;
269 }
270 if (length < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700271 LOG(ERROR) << "format_volume: negative length (" << length
272 << ") not supported on " << v->fs_type;
JP Abgrall37aedb32014-06-16 19:07:39 -0700273 return -1;
274 }
Jin Qianadeb41a2017-04-28 16:15:13 -0700275 char *num_sectors = nullptr;
276 if (length >= 512 && asprintf(&num_sectors, "%zd", length / 512) <= 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700277 LOG(ERROR) << "format_volume: failed to create " << v->fs_type
278 << " command for " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700279 return -1;
280 }
281 const char *f2fs_path = "/sbin/mkfs.f2fs";
Jin Qianadeb41a2017-04-28 16:15:13 -0700282 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700283
284 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
285 free(num_sectors);
286 }
287 if (result != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700288 PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700289 return -1;
290 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700291 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800292 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700293
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700294 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295 return -1;
296}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700297
Paul Lawrenced0db3372015-11-05 13:38:40 -0800298int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700299 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800300}
301
Doug Zongker239ac6a2013-08-20 16:03:25 -0700302int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700303 if (fstab == nullptr) {
304 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
305 return -1;
306 }
307 for (int i = 0; i < fstab->num_entries; ++i) {
308 const Volume* v = fstab->recs + i;
309
310 // We don't want to do anything with "/".
311 if (strcmp(v->mount_point, "/") == 0) {
312 continue;
313 }
314
315 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
316 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700317 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700318 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700319 }
320 } else {
321 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700322 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700323 return -1;
324 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700325 }
Tao Bao57130c42017-05-10 12:11:21 -0700326 }
327 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700328}