blob: 29f55b96fd4ca556232d5e3bea8539d8845f13b9 [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
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <errno.h>
20#include <stdlib.h>
21#include <sys/mount.h>
22#include <sys/stat.h>
23#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070024#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <unistd.h>
Doug Zongkerd4208f92010-09-20 12:16:13 -070026#include <ctype.h>
Doug Zongkerf39989a2013-12-11 15:40:28 -080027#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070029#include <android-base/logging.h>
Jin Qianac318082017-04-21 14:36:12 -070030#include <android-base/properties.h>
31#include <android-base/stringprintf.h>
Jin Qian5e47d512017-07-24 10:34:35 -070032#include <android-base/unique_fd.h>
Tao Baode40ba52016-10-05 23:17:01 -070033#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080034#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070035
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070037#include "mounts.h"
Doug Zongkerf39989a2013-12-11 15:40:28 -080038#include "cryptfs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Kenny Root41dda822012-03-30 20:48:34 -070042extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050043
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080044void load_volume_table()
45{
46 int i;
47 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080048
Bowgo Tsai84a06482017-03-29 15:13:58 +080049 fstab = fs_mgr_read_fstab_default();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080050 if (!fstab) {
Bowgo Tsai84a06482017-03-29 15:13:58 +080051 LOG(ERROR) << "failed to read default fstab";
Doug Zongkerd4208f92010-09-20 12:16:13 -070052 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070054
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070055 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080056 if (ret < 0 ) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070057 LOG(ERROR) << "failed to add /tmp entry to fstab";
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080058 fs_mgr_free_fstab(fstab);
59 fstab = NULL;
60 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070062
Doug Zongkerd4208f92010-09-20 12:16:13 -070063 printf("recovery filesystem table\n");
64 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080065 for (i = 0; i < fstab->num_entries; ++i) {
66 Volume* v = &fstab->recs[i];
67 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
68 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070069 }
70 printf("\n");
71}
72
73Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080074 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075}
76
Tao Baoabb8f772015-07-30 14:43:27 -070077// Mount the volume specified by path at the given mount_point.
78int ensure_path_mounted_at(const char* path, const char* mount_point) {
Doug Zongkerd4208f92010-09-20 12:16:13 -070079 Volume* v = volume_for_path(path);
80 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070081 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082 return -1;
83 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070084 if (strcmp(v->fs_type, "ramdisk") == 0) {
85 // the ramdisk is always mounted.
86 return 0;
87 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070088
Elliott Hughes63a31922016-06-09 17:41:22 -070089 if (!scan_mounted_volumes()) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070090 LOG(ERROR) << "failed to scan mounted volumes";
Doug Zongkerd4208f92010-09-20 12:16:13 -070091 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070092 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093
Tao Baoabb8f772015-07-30 14:43:27 -070094 if (!mount_point) {
95 mount_point = v->mount_point;
96 }
97
Elliott Hughes63a31922016-06-09 17:41:22 -070098 MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -070099 if (mv) {
100 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 return 0;
102 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700103
Tao Baoabb8f772015-07-30 14:43:27 -0700104 mkdir(mount_point, 0755); // in case it doesn't already exist
Doug Zongkerd4208f92010-09-20 12:16:13 -0700105
Elliott Hughes63a31922016-06-09 17:41:22 -0700106 if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash522ea722015-06-29 18:57:14 -0700107 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108 strcmp(v->fs_type, "vfat") == 0) {
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200109 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
110 if (result == -1 && fs_mgr_is_formattable(v)) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700111 LOG(ERROR) << "failed to mount " << mount_point << " (" << strerror(errno)
112 << ") , formatting.....";
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200113 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
114 if (fs_mgr_do_format(v, crypt_footer) == 0) {
115 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
116 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700117 PLOG(ERROR) << "failed to format " << mount_point;
Johan Harvyl29dd6b62016-08-08 12:37:56 +0200118 return -1;
119 }
120 }
121
122 if (result == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700123 PLOG(ERROR) << "failed to mount " << mount_point;
Elliott Hughes63a31922016-06-09 17:41:22 -0700124 return -1;
125 }
126 return 0;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700127 }
128
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700129 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 return -1;
131}
132
Tao Baoabb8f772015-07-30 14:43:27 -0700133int ensure_path_mounted(const char* path) {
134 // Mount at the default mount point.
135 return ensure_path_mounted_at(path, nullptr);
136}
137
Doug Zongkerd4208f92010-09-20 12:16:13 -0700138int ensure_path_unmounted(const char* path) {
139 Volume* v = volume_for_path(path);
140 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700141 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142 return -1;
143 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700144 if (strcmp(v->fs_type, "ramdisk") == 0) {
145 // the ramdisk is always mounted; you can't unmount it.
146 return -1;
147 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148
Elliott Hughes63a31922016-06-09 17:41:22 -0700149 if (!scan_mounted_volumes()) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700150 LOG(ERROR) << "failed to scan mounted volumes";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700151 return -1;
152 }
153
Elliott Hughes63a31922016-06-09 17:41:22 -0700154 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700155 if (mv == NULL) {
156 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800157 return 0;
158 }
159
Doug Zongkerd4208f92010-09-20 12:16:13 -0700160 return unmount_mounted_volume(mv);
161}
162
JP Abgrall37aedb32014-06-16 19:07:39 -0700163static int exec_cmd(const char* path, char* const argv[]) {
164 int status;
165 pid_t child;
166 if ((child = vfork()) == 0) {
167 execv(path, argv);
Tao Bao3da88012017-02-03 13:09:23 -0800168 _exit(EXIT_FAILURE);
JP Abgrall37aedb32014-06-16 19:07:39 -0700169 }
170 waitpid(child, &status, 0);
171 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700172 LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700173 }
174 return WEXITSTATUS(status);
175}
176
Jin Qian5e47d512017-07-24 10:34:35 -0700177static ssize_t get_file_size(int fd, uint64_t reserve_len) {
178 struct stat buf;
179 int ret = fstat(fd, &buf);
180 if (ret) return 0;
181
182 ssize_t computed_size;
183 if (S_ISREG(buf.st_mode)) {
184 computed_size = buf.st_size - reserve_len;
185 } else if (S_ISBLK(buf.st_mode)) {
186 computed_size = get_block_device_size(fd) - reserve_len;
187 } else {
188 computed_size = 0;
189 }
190
191 return computed_size;
192}
193
Paul Lawrenced0db3372015-11-05 13:38:40 -0800194int format_volume(const char* volume, const char* directory) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700195 Volume* v = volume_for_path(volume);
196 if (v == NULL) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700197 LOG(ERROR) << "unknown volume \"" << volume << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700198 return -1;
199 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700200 if (strcmp(v->fs_type, "ramdisk") == 0) {
201 // you can't format the ramdisk.
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700202 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700203 return -1;
204 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700205 if (strcmp(v->mount_point, volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700206 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700207 return -1;
208 }
209
210 if (ensure_path_unmounted(volume) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700211 LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\"";
Doug Zongkerd4208f92010-09-20 12:16:13 -0700212 return -1;
213 }
214
JP Abgrall37aedb32014-06-16 19:07:39 -0700215 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800216 // if there's a key_loc that looks like a path, it should be a
217 // block device for storing encryption metadata. wipe it too.
218 if (v->key_loc != NULL && v->key_loc[0] == '/') {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700219 LOG(INFO) << "wiping " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800220 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
221 if (fd < 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700222 LOG(ERROR) << "format_volume: failed to open " << v->key_loc;
Doug Zongkerf39989a2013-12-11 15:40:28 -0800223 return -1;
224 }
225 wipe_block_device(fd, get_file_size(fd));
226 close(fd);
227 }
228
JP Abgrall37aedb32014-06-16 19:07:39 -0700229 ssize_t length = 0;
230 if (v->length != 0) {
231 length = v->length;
232 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
Jin Qian5e47d512017-07-24 10:34:35 -0700233 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
234 if (fd < 0) {
235 PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device;
236 return -1;
237 }
238 length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
239 if (length <= 0) {
240 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
241 return -1;
242 }
JP Abgrall37aedb32014-06-16 19:07:39 -0700243 }
244 int result;
245 if (strcmp(v->fs_type, "ext4") == 0) {
Jin Qianac318082017-04-21 14:36:12 -0700246 static constexpr int block_size = 4096;
247 int raid_stride = v->logical_blk_size / block_size;
248 int raid_stripe_width = v->erase_blk_size / block_size;
249
250 // stride should be the max of 8kb and logical block size
251 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
252 raid_stride = 8192 / block_size;
253 }
254
255 const char* mke2fs_argv[] = { "/sbin/mke2fs_static",
256 "-F",
257 "-t",
258 "ext4",
259 "-b",
260 nullptr,
261 nullptr,
262 nullptr,
263 nullptr,
264 nullptr,
265 nullptr };
266
267 int i = 5;
268 std::string block_size_str = std::to_string(block_size);
269 mke2fs_argv[i++] = block_size_str.c_str();
270
271 std::string ext_args;
272 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
273 ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride,
274 raid_stripe_width);
275 mke2fs_argv[i++] = "-E";
276 mke2fs_argv[i++] = ext_args.c_str();
277 }
278
279 mke2fs_argv[i++] = v->blk_device;
280
281 std::string size_str = std::to_string(length / block_size);
282 if (length != 0) {
283 mke2fs_argv[i++] = size_str.c_str();
284 }
285
286 result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
287 if (result == 0 && directory != nullptr) {
288 const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static",
289 "-e",
Jin Qianac318082017-04-21 14:36:12 -0700290 "-f",
291 directory,
292 "-a",
293 volume,
294 v->blk_device,
295 nullptr };
296
297 result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
Tao Bao7af933b2017-07-11 16:45:04 -0700298 }
JP Abgrall37aedb32014-06-16 19:07:39 -0700299 } else { /* Has to be f2fs because we checked earlier. */
Jin Qianadeb41a2017-04-28 16:15:13 -0700300 char *num_sectors = nullptr;
301 if (length >= 512 && asprintf(&num_sectors, "%zd", length / 512) <= 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700302 LOG(ERROR) << "format_volume: failed to create " << v->fs_type
303 << " command for " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700304 return -1;
305 }
306 const char *f2fs_path = "/sbin/mkfs.f2fs";
Jin Qianadeb41a2017-04-28 16:15:13 -0700307 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, nullptr};
JP Abgrall37aedb32014-06-16 19:07:39 -0700308
309 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
310 free(num_sectors);
311 }
312 if (result != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700313 PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device;
JP Abgrall37aedb32014-06-16 19:07:39 -0700314 return -1;
315 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700316 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700319 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800320 return -1;
321}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700322
Paul Lawrenced0db3372015-11-05 13:38:40 -0800323int format_volume(const char* volume) {
324 return format_volume(volume, NULL);
325}
326
Doug Zongker239ac6a2013-08-20 16:03:25 -0700327int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700328 if (fstab == nullptr) {
329 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
330 return -1;
331 }
332 for (int i = 0; i < fstab->num_entries; ++i) {
333 const Volume* v = fstab->recs + i;
334
335 // We don't want to do anything with "/".
336 if (strcmp(v->mount_point, "/") == 0) {
337 continue;
338 }
339
340 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
341 if (ensure_path_mounted(v->mount_point) != 0) {
342 LOG(ERROR) << "failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700343 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700344 }
345 } else {
346 if (ensure_path_unmounted(v->mount_point) != 0) {
347 LOG(ERROR) << "failed to unmount " << v->mount_point;
348 return -1;
349 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700350 }
Tao Bao57130c42017-05-10 12:11:21 -0700351 }
352 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700353}