Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 17 | #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 Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 30 | #include <sys/swap.h> |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 31 | #include <dirent.h> |
| 32 | #include <ext4.h> |
| 33 | #include <ext4_sb.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 34 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 35 | #include <linux/loop.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 37 | #include <cutils/android_reboot.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 38 | #include <cutils/partition_utils.h> |
| 39 | #include <cutils/properties.h> |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 40 | #include <logwrap/logwrap.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 41 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 42 | #include "mincrypt/rsa.h" |
| 43 | #include "mincrypt/sha.h" |
| 44 | #include "mincrypt/sha256.h" |
| 45 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 46 | #include "fs_mgr_priv.h" |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 47 | #include "fs_mgr_priv_verity.h" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 48 | |
| 49 | #define KEY_LOC_PROP "ro.crypto.keyfile.userdata" |
| 50 | #define KEY_IN_FOOTER "footer" |
| 51 | |
| 52 | #define E2FSCK_BIN "/system/bin/e2fsck" |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 53 | #define F2FS_FSCK_BIN "/system/bin/fsck.f2fs" |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 54 | #define MKSWAP_BIN "/system/bin/mkswap" |
| 55 | |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 56 | #define FSCK_LOG_FILE "/dev/fscklogs/log" |
| 57 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 58 | #define ZRAM_CONF_DEV "/sys/block/zram0/disksize" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 59 | |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 60 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 61 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 62 | /* |
| 63 | * gettime() - returns the time in seconds of the system's monotonic clock or |
| 64 | * zero on error. |
| 65 | */ |
| 66 | static 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 | |
| 80 | static 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 Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 92 | static void check_fs(char *blk_device, char *fs_type, char *target) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 93 | { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 94 | int status; |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 95 | int ret; |
| 96 | long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; |
| 97 | char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro"; |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 98 | char *e2fsck_argv[] = { |
| 99 | E2FSCK_BIN, |
| 100 | "-y", |
| 101 | blk_device |
| 102 | }; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 103 | |
| 104 | /* Check for the types of filesystems we know how to check */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 105 | if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) { |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 106 | /* |
| 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 Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 119 | ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts); |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 120 | INFO("%s(): mount(%s,%s,%s)=%d\n", __func__, blk_device, target, fs_type, ret); |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 121 | if (!ret) { |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 122 | umount(target); |
| 123 | } |
| 124 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 125 | /* |
| 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 Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 134 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 135 | ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, |
| 136 | &status, true, LOG_KLOG | LOG_FILE, |
| 137 | true, FSCK_LOG_FILE); |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 138 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 139 | 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 Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 143 | } |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 144 | } 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 Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | static 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 Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 174 | /* |
| 175 | * Mark the given block device as read-only, using the BLKROSET ioctl. |
| 176 | * Return 0 on success, and -1 on error. |
| 177 | */ |
| 178 | static 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 Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 198 | static int __mount(const char *source, const char *target, const struct fstab_rec *rec) |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 199 | { |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 200 | unsigned long mountflags = rec->flags; |
| 201 | int ret; |
| 202 | int save_errno; |
Daniel Rosenberg | f67d6bd | 2014-06-26 14:55:04 -0700 | [diff] [blame] | 203 | |
| 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 Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 211 | mkdir(target, 0755); |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 212 | 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 Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 215 | if ((ret == 0) && (mountflags & MS_RDONLY) != 0) { |
| 216 | fs_set_blk_ro(source); |
| 217 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 218 | errno = save_errno; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 222 | static 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 Condra | cd642fc | 2014-04-02 13:42:06 -0700 | [diff] [blame] | 242 | static 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 Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 251 | static 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 Lawrence | 2f7ee6b | 2015-01-07 11:44:51 -0800 | [diff] [blame] | 261 | static 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 Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 270 | /* |
| 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 | */ |
| 281 | static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx) |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 282 | { |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 283 | 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 Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 292 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 293 | |
| 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 Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 340 | static 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 | |
| 412 | out: |
| 413 | closedir(blockdir); |
| 414 | return ret; |
| 415 | } |
| 416 | |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 417 | /* 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 Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 420 | * Returns -1 on error, and FS_MGR_MNTALL_* otherwise. |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 421 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 422 | int fs_mgr_mount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 423 | { |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 424 | int i = 0; |
| 425 | int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 426 | int error_count = 0; |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 427 | int mret = -1; |
| 428 | int mount_errno = 0; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 429 | int attempted_idx = -1; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 430 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 431 | if (!fstab) { |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 432 | return -1; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 435 | for (i = 0; i < fstab->num_entries; i++) { |
| 436 | /* Don't mount entries that are managed by vold */ |
Ken Sumrall | 6c2c121 | 2013-02-22 17:36:21 -0800 | [diff] [blame] | 437 | if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 438 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 441 | /* 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 Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 444 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 445 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 448 | /* 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 Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 459 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 460 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 461 | } |
| 462 | |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 463 | 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 Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 468 | ERROR("Could not set up verified partition, skipping!\n"); |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 469 | continue; |
| 470 | } |
| 471 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 472 | 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 Abgrall | f786fe5 | 2014-06-18 07:28:14 +0000 | [diff] [blame] | 476 | |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 477 | /* Deal with encryptability. */ |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 478 | if (!mret) { |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 479 | /* If this is encryptable, need to trigger encryption */ |
Paul Lawrence | 36d0eae | 2015-01-21 09:57:01 -0800 | [diff] [blame^] | 480 | if (fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) { |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 481 | if (umount(fstab->recs[attempted_idx].mount_point) == 0) { |
| 482 | if (encryptable == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
| 483 | ERROR("Will try to encrypt %s %s\n", fstab->recs[attempted_idx].mount_point, |
| 484 | fstab->recs[attempted_idx].fs_type); |
| 485 | encryptable = FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION; |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 486 | } else { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 487 | ERROR("Only one encryptable/encrypted partition supported\n"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 488 | encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED; |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 489 | } |
| 490 | } else { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 491 | INFO("Could not umount %s - allow continue unencrypted\n", |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 492 | fstab->recs[attempted_idx].mount_point); |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 493 | continue; |
| 494 | } |
| 495 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 496 | /* Success! Go get the next one */ |
| 497 | continue; |
| 498 | } |
| 499 | |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 500 | /* mount(2) returned an error, check if it's encryptable and deal with it */ |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 501 | if (mret && mount_errno != EBUSY && mount_errno != EACCES && |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 502 | fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) { |
| 503 | if(partition_wiped(fstab->recs[attempted_idx].blk_device)) { |
| 504 | ERROR("%s(): %s is wiped and %s %s is encryptable. Suggest recovery...\n", __func__, |
| 505 | fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point, |
| 506 | fstab->recs[attempted_idx].fs_type); |
| 507 | encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 508 | continue; |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 509 | } else { |
| 510 | /* Need to mount a tmpfs at this mountpoint for now, and set |
| 511 | * properties that vold will query later for decrypting |
| 512 | */ |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 513 | ERROR("%s(): possibly an encryptable blkdev %s for mount %s type %s )\n", __func__, |
| 514 | fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point, |
| 515 | fstab->recs[attempted_idx].fs_type); |
| 516 | if (fs_mgr_do_tmpfs_mount(fstab->recs[attempted_idx].mount_point) < 0) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 517 | ++error_count; |
| 518 | continue; |
| 519 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 520 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 521 | encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 522 | } else { |
William Roberts | 071f28a | 2014-01-14 14:33:44 -0500 | [diff] [blame] | 523 | ERROR("Failed to mount an un-encryptable or wiped partition on" |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 524 | "%s at %s options: %s error: %s\n", |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 525 | fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point, |
| 526 | fstab->recs[attempted_idx].fs_options, strerror(mount_errno)); |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 527 | ++error_count; |
| 528 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 532 | if (error_count) { |
| 533 | return -1; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 534 | } else { |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 535 | return encryptable; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 536 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 539 | /* If tmp_mount_point is non-null, mount the filesystem there. This is for the |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 540 | * tmp mount we do to check the user password |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 541 | * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one |
| 542 | * in turn, and stop on 1st success, or no more match. |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 543 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 544 | int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, |
| 545 | char *tmp_mount_point) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 546 | { |
| 547 | int i = 0; |
Paul Lawrence | cf234dc | 2014-09-09 10:44:51 -0700 | [diff] [blame] | 548 | int ret = FS_MGR_DOMNT_FAILED; |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 549 | int mount_errors = 0; |
| 550 | int first_mount_errno = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 551 | char *m; |
| 552 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 553 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 557 | for (i = 0; i < fstab->num_entries; i++) { |
| 558 | if (!fs_match(fstab->recs[i].mount_point, n_name)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 559 | continue; |
| 560 | } |
| 561 | |
| 562 | /* We found our match */ |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 563 | /* If this swap or a raw partition, report an error */ |
| 564 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 565 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 566 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 567 | ERROR("Cannot mount filesystem of type %s on %s\n", |
| 568 | fstab->recs[i].fs_type, n_blk_device); |
| 569 | goto out; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 572 | /* First check the filesystem if requested */ |
| 573 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 574 | wait_for_file(n_blk_device, WAIT_TIMEOUT); |
| 575 | } |
| 576 | |
| 577 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 578 | check_fs(n_blk_device, fstab->recs[i].fs_type, |
| 579 | fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Paul Lawrence | ec900bb | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 582 | if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) { |
| 583 | int rc = fs_mgr_setup_verity(&fstab->recs[i]); |
| 584 | if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) { |
| 585 | INFO("Verity disabled"); |
| 586 | } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 587 | ERROR("Could not set up verified partition, skipping!\n"); |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 588 | continue; |
| 589 | } |
| 590 | } |
| 591 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 592 | /* Now mount it where requested */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 593 | if (tmp_mount_point) { |
| 594 | m = tmp_mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 595 | } else { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 596 | m = fstab->recs[i].mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 597 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 598 | if (__mount(n_blk_device, m, &fstab->recs[i])) { |
| 599 | if (!first_mount_errno) first_mount_errno = errno; |
| 600 | mount_errors++; |
| 601 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 602 | } else { |
| 603 | ret = 0; |
| 604 | goto out; |
| 605 | } |
| 606 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 607 | if (mount_errors) { |
| 608 | ERROR("Cannot mount filesystem on %s at %s. error: %s\n", |
| 609 | n_blk_device, m, strerror(first_mount_errno)); |
Paul Lawrence | cf234dc | 2014-09-09 10:44:51 -0700 | [diff] [blame] | 610 | if (first_mount_errno == EBUSY) { |
| 611 | ret = FS_MGR_DOMNT_BUSY; |
| 612 | } else { |
| 613 | ret = FS_MGR_DOMNT_FAILED; |
| 614 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 615 | } else { |
| 616 | /* We didn't find a match, say so and return an error */ |
| 617 | ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point); |
| 618 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 619 | |
| 620 | out: |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * mount a tmpfs filesystem at the given point. |
| 626 | * return 0 on success, non-zero on failure. |
| 627 | */ |
| 628 | int fs_mgr_do_tmpfs_mount(char *n_name) |
| 629 | { |
| 630 | int ret; |
| 631 | |
| 632 | ret = mount("tmpfs", n_name, "tmpfs", |
| 633 | MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS); |
| 634 | if (ret < 0) { |
| 635 | ERROR("Cannot mount tmpfs filesystem at %s\n", n_name); |
| 636 | return -1; |
| 637 | } |
| 638 | |
| 639 | /* Success */ |
| 640 | return 0; |
| 641 | } |
| 642 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 643 | int fs_mgr_unmount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 644 | { |
| 645 | int i = 0; |
| 646 | int ret = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 647 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 648 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 649 | return -1; |
| 650 | } |
| 651 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 652 | while (fstab->recs[i].blk_device) { |
| 653 | if (umount(fstab->recs[i].mount_point)) { |
| 654 | ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 655 | ret = -1; |
| 656 | } |
| 657 | i++; |
| 658 | } |
| 659 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 660 | return ret; |
| 661 | } |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 662 | |
| 663 | /* This must be called after mount_all, because the mkswap command needs to be |
| 664 | * available. |
| 665 | */ |
| 666 | int fs_mgr_swapon_all(struct fstab *fstab) |
| 667 | { |
| 668 | int i = 0; |
| 669 | int flags = 0; |
| 670 | int err = 0; |
| 671 | int ret = 0; |
| 672 | int status; |
| 673 | char *mkswap_argv[2] = { |
| 674 | MKSWAP_BIN, |
| 675 | NULL |
| 676 | }; |
| 677 | |
| 678 | if (!fstab) { |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | for (i = 0; i < fstab->num_entries; i++) { |
| 683 | /* Skip non-swap entries */ |
| 684 | if (strcmp(fstab->recs[i].fs_type, "swap")) { |
| 685 | continue; |
| 686 | } |
| 687 | |
| 688 | if (fstab->recs[i].zram_size > 0) { |
| 689 | /* A zram_size was specified, so we need to configure the |
| 690 | * device. There is no point in having multiple zram devices |
| 691 | * on a system (all the memory comes from the same pool) so |
| 692 | * we can assume the device number is 0. |
| 693 | */ |
| 694 | FILE *zram_fp; |
| 695 | |
| 696 | zram_fp = fopen(ZRAM_CONF_DEV, "r+"); |
| 697 | if (zram_fp == NULL) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 698 | ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 699 | ret = -1; |
| 700 | continue; |
| 701 | } |
| 702 | fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size); |
| 703 | fclose(zram_fp); |
| 704 | } |
| 705 | |
| 706 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 707 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 708 | } |
| 709 | |
| 710 | /* Initialize the swap area */ |
| 711 | mkswap_argv[1] = fstab->recs[i].blk_device; |
| 712 | err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv, |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 713 | &status, true, LOG_KLOG, false, NULL); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 714 | if (err) { |
| 715 | ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device); |
| 716 | ret = -1; |
| 717 | continue; |
| 718 | } |
| 719 | |
| 720 | /* If -1, then no priority was specified in fstab, so don't set |
| 721 | * SWAP_FLAG_PREFER or encode the priority */ |
| 722 | if (fstab->recs[i].swap_prio >= 0) { |
| 723 | flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) & |
| 724 | SWAP_FLAG_PRIO_MASK; |
| 725 | flags |= SWAP_FLAG_PREFER; |
| 726 | } else { |
| 727 | flags = 0; |
| 728 | } |
| 729 | err = swapon(fstab->recs[i].blk_device, flags); |
| 730 | if (err) { |
| 731 | ERROR("swapon failed for %s\n", fstab->recs[i].blk_device); |
| 732 | ret = -1; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 739 | /* |
| 740 | * key_loc must be at least PROPERTY_VALUE_MAX bytes long |
| 741 | * |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 742 | * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 743 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 744 | int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 745 | { |
| 746 | int i = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 747 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 748 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 749 | return -1; |
| 750 | } |
| 751 | /* Initialize return values to null strings */ |
| 752 | if (key_loc) { |
| 753 | *key_loc = '\0'; |
| 754 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 755 | if (real_blk_device) { |
| 756 | *real_blk_device = '\0'; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* Look for the encryptable partition to find the data */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 760 | for (i = 0; i < fstab->num_entries; i++) { |
| 761 | /* Don't deal with vold managed enryptable partitions here */ |
| 762 | if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) { |
| 763 | continue; |
| 764 | } |
Paul Lawrence | 2e5ae0a | 2014-04-04 09:34:19 -0700 | [diff] [blame] | 765 | if (!(fstab->recs[i].fs_mgr_flags & (MF_CRYPT | MF_FORCECRYPT))) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 766 | continue; |
| 767 | } |
| 768 | |
| 769 | /* We found a match */ |
| 770 | if (key_loc) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 771 | strlcpy(key_loc, fstab->recs[i].key_loc, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 772 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 773 | if (real_blk_device) { |
| 774 | strlcpy(real_blk_device, fstab->recs[i].blk_device, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 775 | } |
| 776 | break; |
| 777 | } |
| 778 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 779 | return 0; |
| 780 | } |