blob: 8e3aa11c91ae390e9f9f20bff11c416f4f9a5f9c [file] [log] [blame]
Ken Sumrallc1bf8962012-01-06 19:09:42 -08001/*
2 * Copyright (C) 2012 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
Ken Sumrallc1bf8962012-01-06 19:09:42 -080017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <errno.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <libgen.h>
29#include <time.h>
Ken Sumrall5bc31a22013-07-08 19:11:55 -070030#include <sys/swap.h>
Christoffer Dall82982342014-12-17 21:26:54 +010031#include <dirent.h>
32#include <ext4.h>
33#include <ext4_sb.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080034
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080035#include <linux/loop.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080036#include <private/android_filesystem_config.h>
JP Abgrall4bb7bba2014-06-19 22:12:20 -070037#include <cutils/android_reboot.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080038#include <cutils/partition_utils.h>
39#include <cutils/properties.h>
Ken Sumrallbf021b42013-03-19 19:38:44 -070040#include <logwrap/logwrap.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080041
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080042#include "mincrypt/rsa.h"
43#include "mincrypt/sha.h"
44#include "mincrypt/sha256.h"
45
Ken Sumrallc1bf8962012-01-06 19:09:42 -080046#include "fs_mgr_priv.h"
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080047#include "fs_mgr_priv_verity.h"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080048
49#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
50#define KEY_IN_FOOTER "footer"
51
52#define E2FSCK_BIN "/system/bin/e2fsck"
JP Abgrall12351582014-06-17 17:01:14 -070053#define F2FS_FSCK_BIN "/system/bin/fsck.f2fs"
Ken Sumrall5bc31a22013-07-08 19:11:55 -070054#define MKSWAP_BIN "/system/bin/mkswap"
55
Ken Sumrall4eaf9052013-09-18 17:49:21 -070056#define FSCK_LOG_FILE "/dev/fscklogs/log"
57
Ken Sumrall5bc31a22013-07-08 19:11:55 -070058#define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080059
Ken Sumrallbf021b42013-03-19 19:38:44 -070060#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
61
Ken Sumrallc1bf8962012-01-06 19:09:42 -080062/*
63 * gettime() - returns the time in seconds of the system's monotonic clock or
64 * zero on error.
65 */
66static time_t gettime(void)
67{
68 struct timespec ts;
69 int ret;
70
71 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
72 if (ret < 0) {
73 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
74 return 0;
75 }
76
77 return ts.tv_sec;
78}
79
80static int wait_for_file(const char *filename, int timeout)
81{
82 struct stat info;
83 time_t timeout_time = gettime() + timeout;
84 int ret = -1;
85
86 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
87 usleep(10000);
88
89 return ret;
90}
91
Ken Sumrallab6b8522013-02-13 12:58:40 -080092static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -080093{
Ken Sumrallc1bf8962012-01-06 19:09:42 -080094 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -070095 int ret;
96 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
97 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -070098 char *e2fsck_argv[] = {
99 E2FSCK_BIN,
100 "-y",
101 blk_device
102 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800103
104 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800105 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700106 /*
107 * First try to mount and unmount the filesystem. We do this because
108 * the kernel is more efficient than e2fsck in running the journal and
109 * processing orphaned inodes, and on at least one device with a
110 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
111 * to do what the kernel does in about a second.
112 *
113 * After mounting and unmounting the filesystem, run e2fsck, and if an
114 * error is recorded in the filesystem superblock, e2fsck will do a full
115 * check. Otherwise, it does nothing. If the kernel cannot mount the
116 * filesytsem due to an error, e2fsck is still run to do a full check
117 * fix the filesystem.
118 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800119 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
JP Abgrall5c01dac2014-06-18 14:54:37 -0700120 INFO("%s(): mount(%s,%s,%s)=%d\n", __func__, blk_device, target, fs_type, ret);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800121 if (!ret) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700122 umount(target);
123 }
124
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100125 /*
126 * Some system images do not have e2fsck for licensing reasons
127 * (e.g. recent SDK system images). Detect these and skip the check.
128 */
129 if (access(E2FSCK_BIN, X_OK)) {
130 INFO("Not running %s on %s (executable not in system image)\n",
131 E2FSCK_BIN, blk_device);
132 } else {
133 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800134
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100135 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
136 &status, true, LOG_KLOG | LOG_FILE,
137 true, FSCK_LOG_FILE);
Ken Sumrallbf021b42013-03-19 19:38:44 -0700138
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100139 if (ret < 0) {
140 /* No need to check for error in fork, we can't really handle it now */
141 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
142 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800143 }
JP Abgrall12351582014-06-17 17:01:14 -0700144 } else if (!strcmp(fs_type, "f2fs")) {
145 char *f2fs_fsck_argv[] = {
146 F2FS_FSCK_BIN,
147 blk_device
148 };
149 INFO("Running %s on %s\n", F2FS_FSCK_BIN, blk_device);
150
151 ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv,
152 &status, true, LOG_KLOG | LOG_FILE,
153 true, FSCK_LOG_FILE);
154 if (ret < 0) {
155 /* No need to check for error in fork, we can't really handle it now */
156 ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN);
157 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800158 }
159
160 return;
161}
162
163static void remove_trailing_slashes(char *n)
164{
165 int len;
166
167 len = strlen(n) - 1;
168 while ((*(n + len) == '/') && len) {
169 *(n + len) = '\0';
170 len--;
171 }
172}
173
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700174/*
175 * Mark the given block device as read-only, using the BLKROSET ioctl.
176 * Return 0 on success, and -1 on error.
177 */
178static void fs_set_blk_ro(const char *blockdev)
179{
180 int fd;
181 int ON = 1;
182
183 fd = open(blockdev, O_RDONLY);
184 if (fd < 0) {
185 // should never happen
186 return;
187 }
188
189 ioctl(fd, BLKROSET, &ON);
190 close(fd);
191}
192
193/*
194 * __mount(): wrapper around the mount() system call which also
195 * sets the underlying block device to read-only if the mount is read-only.
196 * See "man 2 mount" for return values.
197 */
JP Abgrall5c01dac2014-06-18 14:54:37 -0700198static int __mount(const char *source, const char *target, const struct fstab_rec *rec)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700199{
JP Abgrall5c01dac2014-06-18 14:54:37 -0700200 unsigned long mountflags = rec->flags;
201 int ret;
202 int save_errno;
Daniel Rosenbergf67d6bd2014-06-26 14:55:04 -0700203
204 /* We need this because sometimes we have legacy symlinks
205 * that are lingering around and need cleaning up.
206 */
207 struct stat info;
208 if (!lstat(target, &info))
209 if ((info.st_mode & S_IFMT) == S_IFLNK)
210 unlink(target);
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700211 mkdir(target, 0755);
JP Abgrall5c01dac2014-06-18 14:54:37 -0700212 ret = mount(source, target, rec->fs_type, mountflags, rec->fs_options);
213 save_errno = errno;
214 INFO("%s(source=%s,target=%s,type=%s)=%d\n", __func__, source, target, rec->fs_type, ret);
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700215 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
216 fs_set_blk_ro(source);
217 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700218 errno = save_errno;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700219 return ret;
220}
221
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800222static int fs_match(char *in1, char *in2)
223{
224 char *n1;
225 char *n2;
226 int ret;
227
228 n1 = strdup(in1);
229 n2 = strdup(in2);
230
231 remove_trailing_slashes(n1);
232 remove_trailing_slashes(n2);
233
234 ret = !strcmp(n1, n2);
235
236 free(n1);
237 free(n2);
238
239 return ret;
240}
241
Geremy Condracd642fc2014-04-02 13:42:06 -0700242static int device_is_debuggable() {
243 int ret = -1;
244 char value[PROP_VALUE_MAX];
245 ret = __system_property_get("ro.debuggable", value);
246 if (ret < 0)
247 return ret;
248 return strcmp(value, "1") ? 0 : 1;
249}
250
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000251static int device_is_secure() {
252 int ret = -1;
253 char value[PROP_VALUE_MAX];
254 ret = __system_property_get("ro.secure", value);
255 /* If error, we want to fail secure */
256 if (ret < 0)
257 return 1;
258 return strcmp(value, "0") ? 1 : 0;
259}
260
Paul Lawrence2f7ee6b2015-01-07 11:44:51 -0800261static int device_is_force_encrypted() {
262 int ret = -1;
263 char value[PROP_VALUE_MAX];
264 ret = __system_property_get("ro.vold.forceencryption", value);
265 if (ret < 0)
266 return 0;
267 return strcmp(value, "1") ? 0 : 1;
268}
269
JP Abgrallf22b7452014-07-02 13:16:04 -0700270/*
271 * Tries to mount any of the consecutive fstab entries that match
272 * the mountpoint of the one given by fstab->recs[start_idx].
273 *
274 * end_idx: On return, will be the last rec that was looked at.
275 * attempted_idx: On return, will indicate which fstab rec
276 * succeeded. In case of failure, it will be the start_idx.
277 * Returns
278 * -1 on failure with errno set to match the 1st mount failure.
279 * 0 on success.
280 */
281static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx)
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700282{
JP Abgrallf22b7452014-07-02 13:16:04 -0700283 int i;
284 int mount_errno = 0;
285 int mounted = 0;
286
287 if (!end_idx || !attempted_idx || start_idx >= fstab->num_entries) {
288 errno = EINVAL;
289 if (end_idx) *end_idx = start_idx;
290 if (attempted_idx) *end_idx = start_idx;
291 return -1;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700292 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700293
294 /* Hunt down an fstab entry for the same mount point that might succeed */
295 for (i = start_idx;
296 /* We required that fstab entries for the same mountpoint be consecutive */
297 i < fstab->num_entries && !strcmp(fstab->recs[start_idx].mount_point, fstab->recs[i].mount_point);
298 i++) {
299 /*
300 * Don't try to mount/encrypt the same mount point again.
301 * Deal with alternate entries for the same point which are required to be all following
302 * each other.
303 */
304 if (mounted) {
305 ERROR("%s(): skipping fstab dup mountpoint=%s rec[%d].fs_type=%s already mounted as %s.\n", __func__,
306 fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type);
307 continue;
308 }
309
310 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
311 check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
312 fstab->recs[i].mount_point);
313 }
314 if (!__mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, &fstab->recs[i])) {
315 *attempted_idx = i;
316 mounted = 1;
317 if (i != start_idx) {
318 ERROR("%s(): Mounted %s on %s with fs_type=%s instead of %s\n", __func__,
319 fstab->recs[i].blk_device, fstab->recs[i].mount_point, fstab->recs[i].fs_type,
320 fstab->recs[start_idx].fs_type);
321 }
322 } else {
323 /* back up errno for crypto decisions */
324 mount_errno = errno;
325 }
326 }
327
328 /* Adjust i for the case where it was still withing the recs[] */
329 if (i < fstab->num_entries) --i;
330
331 *end_idx = i;
332 if (!mounted) {
333 *attempted_idx = start_idx;
334 errno = mount_errno;
335 return -1;
336 }
337 return 0;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700338}
339
Christoffer Dall82982342014-12-17 21:26:54 +0100340static int translate_ext_labels(struct fstab_rec *rec)
341{
342 DIR *blockdir = NULL;
343 struct dirent *ent;
344 char *label;
345 size_t label_len;
346 int ret = -1;
347
348 if (strncmp(rec->blk_device, "LABEL=", 6))
349 return 0;
350
351 label = rec->blk_device + 6;
352 label_len = strlen(label);
353
354 if (label_len > 16) {
355 ERROR("FS label is longer than allowed by filesystem\n");
356 goto out;
357 }
358
359
360 blockdir = opendir("/dev/block");
361 if (!blockdir) {
362 ERROR("couldn't open /dev/block\n");
363 goto out;
364 }
365
366 while ((ent = readdir(blockdir))) {
367 int fd;
368 char super_buf[1024];
369 struct ext4_super_block *sb;
370
371 if (ent->d_type != DT_BLK)
372 continue;
373
374 fd = openat(dirfd(blockdir), ent->d_name, O_RDONLY);
375 if (fd < 0) {
376 ERROR("Cannot open block device /dev/block/%s\n", ent->d_name);
377 goto out;
378 }
379
380 if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 ||
381 TEMP_FAILURE_RETRY(read(fd, super_buf, 1024)) != 1024) {
382 /* Probably a loopback device or something else without a readable
383 * superblock.
384 */
385 close(fd);
386 continue;
387 }
388
389 sb = (struct ext4_super_block *)super_buf;
390 if (sb->s_magic != EXT4_SUPER_MAGIC) {
391 INFO("/dev/block/%s not ext{234}\n", ent->d_name);
392 continue;
393 }
394
395 if (!strncmp(label, sb->s_volume_name, label_len)) {
396 char *new_blk_device;
397
398 if (asprintf(&new_blk_device, "/dev/block/%s", ent->d_name) < 0) {
399 ERROR("Could not allocate block device string\n");
400 goto out;
401 }
402
403 INFO("resolved label %s to %s\n", rec->blk_device, new_blk_device);
404
405 free(rec->blk_device);
406 rec->blk_device = new_blk_device;
407 ret = 0;
408 break;
409 }
410 }
411
412out:
413 closedir(blockdir);
414 return ret;
415}
416
JP Abgrall5c01dac2014-06-18 14:54:37 -0700417/* When multiple fstab records share the same mount_point, it will
418 * try to mount each one in turn, and ignore any duplicates after a
419 * first successful mount.
JP Abgrallf22b7452014-07-02 13:16:04 -0700420 * Returns -1 on error, and FS_MGR_MNTALL_* otherwise.
JP Abgrall5c01dac2014-06-18 14:54:37 -0700421 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800422int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800423{
JP Abgrallf22b7452014-07-02 13:16:04 -0700424 int i = 0;
425 int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700426 int error_count = 0;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700427 int mret = -1;
428 int mount_errno = 0;
JP Abgrallf22b7452014-07-02 13:16:04 -0700429 int attempted_idx = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800430
Ken Sumrallab6b8522013-02-13 12:58:40 -0800431 if (!fstab) {
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700432 return -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800433 }
434
Ken Sumrallab6b8522013-02-13 12:58:40 -0800435 for (i = 0; i < fstab->num_entries; i++) {
436 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800437 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800438 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800439 }
440
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700441 /* Skip swap and raw partition entries such as boot, recovery, etc */
442 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
443 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800444 !strcmp(fstab->recs[i].fs_type, "mtd")) {
445 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800446 }
447
Christoffer Dall82982342014-12-17 21:26:54 +0100448 /* Translate LABEL= file system labels into block devices */
449 if (!strcmp(fstab->recs[i].fs_type, "ext2") ||
450 !strcmp(fstab->recs[i].fs_type, "ext3") ||
451 !strcmp(fstab->recs[i].fs_type, "ext4")) {
452 int tret = translate_ext_labels(&fstab->recs[i]);
453 if (tret < 0) {
454 ERROR("Could not translate label to block device\n");
455 continue;
456 }
457 }
458
Ken Sumrallab6b8522013-02-13 12:58:40 -0800459 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
460 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
461 }
462
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000463 if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
464 int rc = fs_mgr_setup_verity(&fstab->recs[i]);
465 if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
466 INFO("Verity disabled");
467 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700468 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800469 continue;
470 }
471 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700472 int last_idx_inspected;
473 mret = mount_with_alternatives(fstab, i, &last_idx_inspected, &attempted_idx);
474 i = last_idx_inspected;
475 mount_errno = errno;
JP Abgrallf786fe52014-06-18 07:28:14 +0000476
JP Abgrall5c01dac2014-06-18 14:54:37 -0700477 /* Deal with encryptability. */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800478 if (!mret) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800479 /* If this is encryptable, need to trigger encryption */
Paul Lawrence2f7ee6b2015-01-07 11:44:51 -0800480 if ( (fstab->recs[attempted_idx].fs_mgr_flags & MF_FORCECRYPT)
481 || (device_is_force_encrypted()
482 && fs_mgr_is_encryptable(&fstab->recs[attempted_idx]))) {
JP Abgrallf22b7452014-07-02 13:16:04 -0700483 if (umount(fstab->recs[attempted_idx].mount_point) == 0) {
484 if (encryptable == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
485 ERROR("Will try to encrypt %s %s\n", fstab->recs[attempted_idx].mount_point,
486 fstab->recs[attempted_idx].fs_type);
487 encryptable = FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION;
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800488 } else {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700489 ERROR("Only one encryptable/encrypted partition supported\n");
JP Abgrallf22b7452014-07-02 13:16:04 -0700490 encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800491 }
492 } else {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700493 INFO("Could not umount %s - allow continue unencrypted\n",
JP Abgrallf22b7452014-07-02 13:16:04 -0700494 fstab->recs[attempted_idx].mount_point);
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800495 continue;
496 }
497 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800498 /* Success! Go get the next one */
499 continue;
500 }
501
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700502 /* mount(2) returned an error, check if it's encryptable and deal with it */
JP Abgrall5c01dac2014-06-18 14:54:37 -0700503 if (mret && mount_errno != EBUSY && mount_errno != EACCES &&
JP Abgrallcee20682014-07-02 14:26:54 -0700504 fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) {
505 if(partition_wiped(fstab->recs[attempted_idx].blk_device)) {
506 ERROR("%s(): %s is wiped and %s %s is encryptable. Suggest recovery...\n", __func__,
507 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
508 fstab->recs[attempted_idx].fs_type);
509 encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700510 continue;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700511 } else {
512 /* Need to mount a tmpfs at this mountpoint for now, and set
513 * properties that vold will query later for decrypting
514 */
JP Abgrallf22b7452014-07-02 13:16:04 -0700515 ERROR("%s(): possibly an encryptable blkdev %s for mount %s type %s )\n", __func__,
516 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
517 fstab->recs[attempted_idx].fs_type);
518 if (fs_mgr_do_tmpfs_mount(fstab->recs[attempted_idx].mount_point) < 0) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700519 ++error_count;
520 continue;
521 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800522 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700523 encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800524 } else {
William Roberts071f28a2014-01-14 14:33:44 -0500525 ERROR("Failed to mount an un-encryptable or wiped partition on"
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700526 "%s at %s options: %s error: %s\n",
JP Abgrallf22b7452014-07-02 13:16:04 -0700527 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
528 fstab->recs[attempted_idx].fs_options, strerror(mount_errno));
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700529 ++error_count;
530 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800531 }
532 }
533
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700534 if (error_count) {
535 return -1;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700536 } else {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800537 return encryptable;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700538 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800539}
540
Ken Sumrallab6b8522013-02-13 12:58:40 -0800541/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800542 * tmp mount we do to check the user password
JP Abgrall5c01dac2014-06-18 14:54:37 -0700543 * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one
544 * in turn, and stop on 1st success, or no more match.
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800545 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800546int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
547 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800548{
549 int i = 0;
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700550 int ret = FS_MGR_DOMNT_FAILED;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700551 int mount_errors = 0;
552 int first_mount_errno = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800553 char *m;
554
Ken Sumrallab6b8522013-02-13 12:58:40 -0800555 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800556 return ret;
557 }
558
Ken Sumrallab6b8522013-02-13 12:58:40 -0800559 for (i = 0; i < fstab->num_entries; i++) {
560 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800561 continue;
562 }
563
564 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700565 /* If this swap or a raw partition, report an error */
566 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
567 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800568 !strcmp(fstab->recs[i].fs_type, "mtd")) {
569 ERROR("Cannot mount filesystem of type %s on %s\n",
570 fstab->recs[i].fs_type, n_blk_device);
571 goto out;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800572 }
573
Ken Sumrallab6b8522013-02-13 12:58:40 -0800574 /* First check the filesystem if requested */
575 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
576 wait_for_file(n_blk_device, WAIT_TIMEOUT);
577 }
578
579 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
580 check_fs(n_blk_device, fstab->recs[i].fs_type,
581 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800582 }
583
Paul Lawrenceec900bb2014-10-09 14:22:49 +0000584 if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
585 int rc = fs_mgr_setup_verity(&fstab->recs[i]);
586 if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
587 INFO("Verity disabled");
588 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700589 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800590 continue;
591 }
592 }
593
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800594 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800595 if (tmp_mount_point) {
596 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800597 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800598 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800599 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700600 if (__mount(n_blk_device, m, &fstab->recs[i])) {
601 if (!first_mount_errno) first_mount_errno = errno;
602 mount_errors++;
603 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800604 } else {
605 ret = 0;
606 goto out;
607 }
608 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700609 if (mount_errors) {
610 ERROR("Cannot mount filesystem on %s at %s. error: %s\n",
611 n_blk_device, m, strerror(first_mount_errno));
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700612 if (first_mount_errno == EBUSY) {
613 ret = FS_MGR_DOMNT_BUSY;
614 } else {
615 ret = FS_MGR_DOMNT_FAILED;
616 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700617 } else {
618 /* We didn't find a match, say so and return an error */
619 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
620 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800621
622out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800623 return ret;
624}
625
626/*
627 * mount a tmpfs filesystem at the given point.
628 * return 0 on success, non-zero on failure.
629 */
630int fs_mgr_do_tmpfs_mount(char *n_name)
631{
632 int ret;
633
634 ret = mount("tmpfs", n_name, "tmpfs",
635 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
636 if (ret < 0) {
637 ERROR("Cannot mount tmpfs filesystem at %s\n", n_name);
638 return -1;
639 }
640
641 /* Success */
642 return 0;
643}
644
Ken Sumrallab6b8522013-02-13 12:58:40 -0800645int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800646{
647 int i = 0;
648 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800649
Ken Sumrallab6b8522013-02-13 12:58:40 -0800650 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800651 return -1;
652 }
653
Ken Sumrallab6b8522013-02-13 12:58:40 -0800654 while (fstab->recs[i].blk_device) {
655 if (umount(fstab->recs[i].mount_point)) {
656 ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800657 ret = -1;
658 }
659 i++;
660 }
661
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800662 return ret;
663}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700664
665/* This must be called after mount_all, because the mkswap command needs to be
666 * available.
667 */
668int fs_mgr_swapon_all(struct fstab *fstab)
669{
670 int i = 0;
671 int flags = 0;
672 int err = 0;
673 int ret = 0;
674 int status;
675 char *mkswap_argv[2] = {
676 MKSWAP_BIN,
677 NULL
678 };
679
680 if (!fstab) {
681 return -1;
682 }
683
684 for (i = 0; i < fstab->num_entries; i++) {
685 /* Skip non-swap entries */
686 if (strcmp(fstab->recs[i].fs_type, "swap")) {
687 continue;
688 }
689
690 if (fstab->recs[i].zram_size > 0) {
691 /* A zram_size was specified, so we need to configure the
692 * device. There is no point in having multiple zram devices
693 * on a system (all the memory comes from the same pool) so
694 * we can assume the device number is 0.
695 */
696 FILE *zram_fp;
697
698 zram_fp = fopen(ZRAM_CONF_DEV, "r+");
699 if (zram_fp == NULL) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700700 ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700701 ret = -1;
702 continue;
703 }
704 fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size);
705 fclose(zram_fp);
706 }
707
708 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
709 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
710 }
711
712 /* Initialize the swap area */
713 mkswap_argv[1] = fstab->recs[i].blk_device;
714 err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv,
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700715 &status, true, LOG_KLOG, false, NULL);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700716 if (err) {
717 ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device);
718 ret = -1;
719 continue;
720 }
721
722 /* If -1, then no priority was specified in fstab, so don't set
723 * SWAP_FLAG_PREFER or encode the priority */
724 if (fstab->recs[i].swap_prio >= 0) {
725 flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) &
726 SWAP_FLAG_PRIO_MASK;
727 flags |= SWAP_FLAG_PREFER;
728 } else {
729 flags = 0;
730 }
731 err = swapon(fstab->recs[i].blk_device, flags);
732 if (err) {
733 ERROR("swapon failed for %s\n", fstab->recs[i].blk_device);
734 ret = -1;
735 }
736 }
737
738 return ret;
739}
740
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800741/*
742 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
743 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800744 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800745 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800746int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800747{
748 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800749
Ken Sumrallab6b8522013-02-13 12:58:40 -0800750 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800751 return -1;
752 }
753 /* Initialize return values to null strings */
754 if (key_loc) {
755 *key_loc = '\0';
756 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800757 if (real_blk_device) {
758 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800759 }
760
761 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800762 for (i = 0; i < fstab->num_entries; i++) {
763 /* Don't deal with vold managed enryptable partitions here */
764 if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) {
765 continue;
766 }
Paul Lawrence2e5ae0a2014-04-04 09:34:19 -0700767 if (!(fstab->recs[i].fs_mgr_flags & (MF_CRYPT | MF_FORCECRYPT))) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800768 continue;
769 }
770
771 /* We found a match */
772 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800773 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800774 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800775 if (real_blk_device) {
776 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800777 }
778 break;
779 }
780
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800781 return 0;
782}