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> |
| 31 | /* XXX These need to be obtained from kernel headers. See b/9336527 */ |
| 32 | #define SWAP_FLAG_PREFER 0x8000 |
| 33 | #define SWAP_FLAG_PRIO_MASK 0x7fff |
| 34 | #define SWAP_FLAG_PRIO_SHIFT 0 |
| 35 | #define SWAP_FLAG_DISCARD 0x10000 |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 36 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 37 | #include <linux/loop.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 38 | #include <private/android_filesystem_config.h> |
| 39 | #include <cutils/partition_utils.h> |
| 40 | #include <cutils/properties.h> |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 41 | #include <logwrap/logwrap.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 42 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 43 | #include "mincrypt/rsa.h" |
| 44 | #include "mincrypt/sha.h" |
| 45 | #include "mincrypt/sha256.h" |
| 46 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 47 | #include "fs_mgr_priv.h" |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 48 | #include "fs_mgr_priv_verity.h" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 49 | |
| 50 | #define KEY_LOC_PROP "ro.crypto.keyfile.userdata" |
| 51 | #define KEY_IN_FOOTER "footer" |
| 52 | |
| 53 | #define E2FSCK_BIN "/system/bin/e2fsck" |
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 | struct flag_list { |
| 63 | const char *name; |
| 64 | unsigned flag; |
| 65 | }; |
| 66 | |
| 67 | static struct flag_list mount_flags[] = { |
| 68 | { "noatime", MS_NOATIME }, |
| 69 | { "noexec", MS_NOEXEC }, |
| 70 | { "nosuid", MS_NOSUID }, |
| 71 | { "nodev", MS_NODEV }, |
| 72 | { "nodiratime", MS_NODIRATIME }, |
| 73 | { "ro", MS_RDONLY }, |
| 74 | { "rw", 0 }, |
| 75 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 76 | { "bind", MS_BIND }, |
| 77 | { "rec", MS_REC }, |
| 78 | { "unbindable", MS_UNBINDABLE }, |
| 79 | { "private", MS_PRIVATE }, |
| 80 | { "slave", MS_SLAVE }, |
| 81 | { "shared", MS_SHARED }, |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 82 | { "defaults", 0 }, |
| 83 | { 0, 0 }, |
| 84 | }; |
| 85 | |
| 86 | static struct flag_list fs_mgr_flags[] = { |
| 87 | { "wait", MF_WAIT }, |
| 88 | { "check", MF_CHECK }, |
| 89 | { "encryptable=",MF_CRYPT }, |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 90 | { "nonremovable",MF_NONREMOVABLE }, |
| 91 | { "voldmanaged=",MF_VOLDMANAGED}, |
| 92 | { "length=", MF_LENGTH }, |
Ken Sumrall | 6c2c121 | 2013-02-22 17:36:21 -0800 | [diff] [blame] | 93 | { "recoveryonly",MF_RECOVERYONLY }, |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 94 | { "swapprio=", MF_SWAPPRIO }, |
| 95 | { "zramsize=", MF_ZRAMSIZE }, |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 96 | { "verify", MF_VERIFY }, |
Ken Sumrall | 887f289 | 2013-09-11 19:42:51 -0700 | [diff] [blame] | 97 | { "noemulatedsd", MF_NOEMULATEDSD }, |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 98 | { "defaults", 0 }, |
| 99 | { 0, 0 }, |
| 100 | }; |
| 101 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 102 | struct fs_mgr_flag_values { |
| 103 | char *key_loc; |
| 104 | long long part_length; |
| 105 | char *label; |
| 106 | int partnum; |
| 107 | int swap_prio; |
| 108 | unsigned int zram_size; |
| 109 | }; |
| 110 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 111 | /* |
| 112 | * gettime() - returns the time in seconds of the system's monotonic clock or |
| 113 | * zero on error. |
| 114 | */ |
| 115 | static time_t gettime(void) |
| 116 | { |
| 117 | struct timespec ts; |
| 118 | int ret; |
| 119 | |
| 120 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 121 | if (ret < 0) { |
| 122 | ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno)); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | return ts.tv_sec; |
| 127 | } |
| 128 | |
| 129 | static int wait_for_file(const char *filename, int timeout) |
| 130 | { |
| 131 | struct stat info; |
| 132 | time_t timeout_time = gettime() + timeout; |
| 133 | int ret = -1; |
| 134 | |
| 135 | while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0)) |
| 136 | usleep(10000); |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 141 | static int parse_flags(char *flags, struct flag_list *fl, |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 142 | struct fs_mgr_flag_values *flag_vals, |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 143 | char *fs_options, int fs_options_len) |
| 144 | { |
| 145 | int f = 0; |
| 146 | int i; |
| 147 | char *p; |
| 148 | char *savep; |
| 149 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 150 | /* initialize flag values. If we find a relevant flag, we'll |
| 151 | * update the value */ |
| 152 | if (flag_vals) { |
| 153 | memset(flag_vals, 0, sizeof(*flag_vals)); |
| 154 | flag_vals->partnum = -1; |
| 155 | flag_vals->swap_prio = -1; /* negative means it wasn't specified. */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 158 | /* initialize fs_options to the null string */ |
| 159 | if (fs_options && (fs_options_len > 0)) { |
| 160 | fs_options[0] = '\0'; |
| 161 | } |
| 162 | |
| 163 | p = strtok_r(flags, ",", &savep); |
| 164 | while (p) { |
| 165 | /* Look for the flag "p" in the flag list "fl" |
| 166 | * If not found, the loop exits with fl[i].name being null. |
| 167 | */ |
| 168 | for (i = 0; fl[i].name; i++) { |
| 169 | if (!strncmp(p, fl[i].name, strlen(fl[i].name))) { |
| 170 | f |= fl[i].flag; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 171 | if ((fl[i].flag == MF_CRYPT) && flag_vals) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 172 | /* The encryptable flag is followed by an = and the |
| 173 | * location of the keys. Get it and return it. |
| 174 | */ |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 175 | flag_vals->key_loc = strdup(strchr(p, '=') + 1); |
| 176 | } else if ((fl[i].flag == MF_LENGTH) && flag_vals) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 177 | /* The length flag is followed by an = and the |
| 178 | * size of the partition. Get it and return it. |
| 179 | */ |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 180 | flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0); |
| 181 | } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 182 | /* The voldmanaged flag is followed by an = and the |
| 183 | * label, a colon and the partition number or the |
| 184 | * word "auto", e.g. |
| 185 | * voldmanaged=sdcard:3 |
| 186 | * Get and return them. |
| 187 | */ |
| 188 | char *label_start; |
| 189 | char *label_end; |
| 190 | char *part_start; |
| 191 | |
| 192 | label_start = strchr(p, '=') + 1; |
| 193 | label_end = strchr(p, ':'); |
| 194 | if (label_end) { |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 195 | flag_vals->label = strndup(label_start, |
| 196 | (int) (label_end - label_start)); |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 197 | part_start = strchr(p, ':') + 1; |
| 198 | if (!strcmp(part_start, "auto")) { |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 199 | flag_vals->partnum = -1; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 200 | } else { |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 201 | flag_vals->partnum = strtol(part_start, NULL, 0); |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 202 | } |
| 203 | } else { |
| 204 | ERROR("Warning: voldmanaged= flag malformed\n"); |
| 205 | } |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 206 | } else if ((fl[i].flag == MF_SWAPPRIO) && flag_vals) { |
| 207 | flag_vals->swap_prio = strtoll(strchr(p, '=') + 1, NULL, 0); |
| 208 | } else if ((fl[i].flag == MF_ZRAMSIZE) && flag_vals) { |
| 209 | flag_vals->zram_size = strtoll(strchr(p, '=') + 1, NULL, 0); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 210 | } |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (!fl[i].name) { |
| 216 | if (fs_options) { |
| 217 | /* It's not a known flag, so it must be a filesystem specific |
| 218 | * option. Add it to fs_options if it was passed in. |
| 219 | */ |
| 220 | strlcat(fs_options, p, fs_options_len); |
| 221 | strlcat(fs_options, ",", fs_options_len); |
| 222 | } else { |
| 223 | /* fs_options was not passed in, so if the flag is unknown |
| 224 | * it's an error. |
| 225 | */ |
| 226 | ERROR("Warning: unknown flag %s\n", p); |
| 227 | } |
| 228 | } |
| 229 | p = strtok_r(NULL, ",", &savep); |
| 230 | } |
| 231 | |
| 232 | out: |
| 233 | if (fs_options && fs_options[0]) { |
| 234 | /* remove the last trailing comma from the list of options */ |
| 235 | fs_options[strlen(fs_options) - 1] = '\0'; |
| 236 | } |
| 237 | |
| 238 | return f; |
| 239 | } |
| 240 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 241 | struct fstab *fs_mgr_read_fstab(const char *fstab_path) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 242 | { |
| 243 | FILE *fstab_file; |
| 244 | int cnt, entries; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 245 | ssize_t len; |
| 246 | size_t alloc_len = 0; |
| 247 | char *line = NULL; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 248 | const char *delim = " \t"; |
| 249 | char *save_ptr, *p; |
Irina Tirdea | d431b8d | 2013-09-18 23:00:54 +0300 | [diff] [blame] | 250 | struct fstab *fstab = NULL; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 251 | struct fstab_rec *recs; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 252 | struct fs_mgr_flag_values flag_vals; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 253 | #define FS_OPTIONS_LEN 1024 |
| 254 | char tmp_fs_options[FS_OPTIONS_LEN]; |
| 255 | |
| 256 | fstab_file = fopen(fstab_path, "r"); |
| 257 | if (!fstab_file) { |
| 258 | ERROR("Cannot open file %s\n", fstab_path); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | entries = 0; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 263 | while ((len = getline(&line, &alloc_len, fstab_file)) != -1) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 264 | /* if the last character is a newline, shorten the string by 1 byte */ |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 265 | if (line[len - 1] == '\n') { |
| 266 | line[len - 1] = '\0'; |
| 267 | } |
| 268 | /* Skip any leading whitespace */ |
| 269 | p = line; |
| 270 | while (isspace(*p)) { |
| 271 | p++; |
| 272 | } |
| 273 | /* ignore comments or empty lines */ |
| 274 | if (*p == '#' || *p == '\0') |
| 275 | continue; |
| 276 | entries++; |
| 277 | } |
| 278 | |
| 279 | if (!entries) { |
| 280 | ERROR("No entries found in fstab\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 281 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 284 | /* Allocate and init the fstab structure */ |
| 285 | fstab = calloc(1, sizeof(struct fstab)); |
| 286 | fstab->num_entries = entries; |
| 287 | fstab->fstab_filename = strdup(fstab_path); |
| 288 | fstab->recs = calloc(fstab->num_entries, sizeof(struct fstab_rec)); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 289 | |
| 290 | fseek(fstab_file, 0, SEEK_SET); |
| 291 | |
| 292 | cnt = 0; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 293 | while ((len = getline(&line, &alloc_len, fstab_file)) != -1) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 294 | /* if the last character is a newline, shorten the string by 1 byte */ |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 295 | if (line[len - 1] == '\n') { |
| 296 | line[len - 1] = '\0'; |
| 297 | } |
| 298 | |
| 299 | /* Skip any leading whitespace */ |
| 300 | p = line; |
| 301 | while (isspace(*p)) { |
| 302 | p++; |
| 303 | } |
| 304 | /* ignore comments or empty lines */ |
| 305 | if (*p == '#' || *p == '\0') |
| 306 | continue; |
| 307 | |
| 308 | /* If a non-comment entry is greater than the size we allocated, give an |
| 309 | * error and quit. This can happen in the unlikely case the file changes |
| 310 | * between the two reads. |
| 311 | */ |
| 312 | if (cnt >= entries) { |
| 313 | ERROR("Tried to process more entries than counted\n"); |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | if (!(p = strtok_r(line, delim, &save_ptr))) { |
| 318 | ERROR("Error parsing mount source\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 319 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 320 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 321 | fstab->recs[cnt].blk_device = strdup(p); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 322 | |
| 323 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 324 | ERROR("Error parsing mount_point\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 325 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 326 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 327 | fstab->recs[cnt].mount_point = strdup(p); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 328 | |
| 329 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
| 330 | ERROR("Error parsing fs_type\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 331 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 332 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 333 | fstab->recs[cnt].fs_type = strdup(p); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 334 | |
| 335 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
| 336 | ERROR("Error parsing mount_flags\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 337 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 338 | } |
| 339 | tmp_fs_options[0] = '\0'; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 340 | fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL, |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 341 | tmp_fs_options, FS_OPTIONS_LEN); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 342 | |
| 343 | /* fs_options are optional */ |
| 344 | if (tmp_fs_options[0]) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 345 | fstab->recs[cnt].fs_options = strdup(tmp_fs_options); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 346 | } else { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 347 | fstab->recs[cnt].fs_options = NULL; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
| 351 | ERROR("Error parsing fs_mgr_options\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 352 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 353 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 354 | fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags, |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 355 | &flag_vals, NULL, 0); |
| 356 | fstab->recs[cnt].key_loc = flag_vals.key_loc; |
| 357 | fstab->recs[cnt].length = flag_vals.part_length; |
| 358 | fstab->recs[cnt].label = flag_vals.label; |
| 359 | fstab->recs[cnt].partnum = flag_vals.partnum; |
| 360 | fstab->recs[cnt].swap_prio = flag_vals.swap_prio; |
| 361 | fstab->recs[cnt].zram_size = flag_vals.zram_size; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 362 | cnt++; |
| 363 | } |
| 364 | fclose(fstab_file); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 365 | free(line); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 366 | return fstab; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 367 | |
| 368 | err: |
Irina Tirdea | d431b8d | 2013-09-18 23:00:54 +0300 | [diff] [blame] | 369 | fclose(fstab_file); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 370 | free(line); |
Irina Tirdea | d431b8d | 2013-09-18 23:00:54 +0300 | [diff] [blame] | 371 | if (fstab) |
| 372 | fs_mgr_free_fstab(fstab); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 373 | return NULL; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 376 | void fs_mgr_free_fstab(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 377 | { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 378 | int i; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 379 | |
Rom Lemarchand | 397a364 | 2013-09-24 10:49:21 -0700 | [diff] [blame] | 380 | if (!fstab) { |
| 381 | return; |
| 382 | } |
| 383 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 384 | for (i = 0; i < fstab->num_entries; i++) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 385 | /* Free the pointers return by strdup(3) */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 386 | free(fstab->recs[i].blk_device); |
| 387 | free(fstab->recs[i].mount_point); |
| 388 | free(fstab->recs[i].fs_type); |
| 389 | free(fstab->recs[i].fs_options); |
| 390 | free(fstab->recs[i].key_loc); |
| 391 | free(fstab->recs[i].label); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 394 | /* Free the fstab_recs array created by calloc(3) */ |
| 395 | free(fstab->recs); |
| 396 | |
| 397 | /* Free the fstab filename */ |
| 398 | free(fstab->fstab_filename); |
| 399 | |
| 400 | /* Free fstab */ |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 401 | free(fstab); |
| 402 | } |
| 403 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 404 | static void check_fs(char *blk_device, char *fs_type, char *target) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 405 | { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 406 | int status; |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 407 | int ret; |
| 408 | long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; |
| 409 | char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro"; |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 410 | char *e2fsck_argv[] = { |
| 411 | E2FSCK_BIN, |
| 412 | "-y", |
| 413 | blk_device |
| 414 | }; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 415 | |
| 416 | /* Check for the types of filesystems we know how to check */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 417 | 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] | 418 | /* |
| 419 | * First try to mount and unmount the filesystem. We do this because |
| 420 | * the kernel is more efficient than e2fsck in running the journal and |
| 421 | * processing orphaned inodes, and on at least one device with a |
| 422 | * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes |
| 423 | * to do what the kernel does in about a second. |
| 424 | * |
| 425 | * After mounting and unmounting the filesystem, run e2fsck, and if an |
| 426 | * error is recorded in the filesystem superblock, e2fsck will do a full |
| 427 | * check. Otherwise, it does nothing. If the kernel cannot mount the |
| 428 | * filesytsem due to an error, e2fsck is still run to do a full check |
| 429 | * fix the filesystem. |
| 430 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 431 | ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts); |
| 432 | if (!ret) { |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 433 | umount(target); |
| 434 | } |
| 435 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 436 | INFO("Running %s on %s\n", E2FSCK_BIN, blk_device); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 437 | |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 438 | ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 439 | &status, true, LOG_KLOG | LOG_FILE, |
| 440 | true, FSCK_LOG_FILE); |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 441 | |
| 442 | if (ret < 0) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 443 | /* No need to check for error in fork, we can't really handle it now */ |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 444 | ERROR("Failed trying to run %s\n", E2FSCK_BIN); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | static void remove_trailing_slashes(char *n) |
| 452 | { |
| 453 | int len; |
| 454 | |
| 455 | len = strlen(n) - 1; |
| 456 | while ((*(n + len) == '/') && len) { |
| 457 | *(n + len) = '\0'; |
| 458 | len--; |
| 459 | } |
| 460 | } |
| 461 | |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 462 | /* |
| 463 | * Mark the given block device as read-only, using the BLKROSET ioctl. |
| 464 | * Return 0 on success, and -1 on error. |
| 465 | */ |
| 466 | static void fs_set_blk_ro(const char *blockdev) |
| 467 | { |
| 468 | int fd; |
| 469 | int ON = 1; |
| 470 | |
| 471 | fd = open(blockdev, O_RDONLY); |
| 472 | if (fd < 0) { |
| 473 | // should never happen |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | ioctl(fd, BLKROSET, &ON); |
| 478 | close(fd); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * __mount(): wrapper around the mount() system call which also |
| 483 | * sets the underlying block device to read-only if the mount is read-only. |
| 484 | * See "man 2 mount" for return values. |
| 485 | */ |
| 486 | static int __mount(const char *source, const char *target, |
| 487 | const char *filesystemtype, unsigned long mountflags, |
| 488 | const void *data) |
| 489 | { |
| 490 | int ret = mount(source, target, filesystemtype, mountflags, data); |
| 491 | |
| 492 | if ((ret == 0) && (mountflags & MS_RDONLY) != 0) { |
| 493 | fs_set_blk_ro(source); |
| 494 | } |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 499 | static int fs_match(char *in1, char *in2) |
| 500 | { |
| 501 | char *n1; |
| 502 | char *n2; |
| 503 | int ret; |
| 504 | |
| 505 | n1 = strdup(in1); |
| 506 | n2 = strdup(in2); |
| 507 | |
| 508 | remove_trailing_slashes(n1); |
| 509 | remove_trailing_slashes(n2); |
| 510 | |
| 511 | ret = !strcmp(n1, n2); |
| 512 | |
| 513 | free(n1); |
| 514 | free(n2); |
| 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 519 | int fs_mgr_mount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 520 | { |
| 521 | int i = 0; |
| 522 | int encrypted = 0; |
| 523 | int ret = -1; |
| 524 | int mret; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 525 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 526 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 527 | return ret; |
| 528 | } |
| 529 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 530 | for (i = 0; i < fstab->num_entries; i++) { |
| 531 | /* Don't mount entries that are managed by vold */ |
Ken Sumrall | 6c2c121 | 2013-02-22 17:36:21 -0800 | [diff] [blame] | 532 | if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 533 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 536 | /* Skip swap and raw partition entries such as boot, recovery, etc */ |
| 537 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 538 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 539 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 540 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 541 | } |
| 542 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 543 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 544 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 545 | } |
| 546 | |
| 547 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 548 | check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type, |
| 549 | fstab->recs[i].mount_point); |
| 550 | } |
| 551 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 552 | if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) { |
| 553 | if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) { |
| 554 | ERROR("Could not set up verified partition, skipping!"); |
| 555 | continue; |
| 556 | } |
| 557 | } |
| 558 | |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 559 | mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 560 | fstab->recs[i].fs_type, fstab->recs[i].flags, |
| 561 | fstab->recs[i].fs_options); |
| 562 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 563 | if (!mret) { |
| 564 | /* Success! Go get the next one */ |
| 565 | continue; |
| 566 | } |
| 567 | |
| 568 | /* mount(2) returned an error, check if it's encrypted and deal with it */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 569 | if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) && |
| 570 | !partition_wiped(fstab->recs[i].blk_device)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 571 | /* Need to mount a tmpfs at this mountpoint for now, and set |
| 572 | * properties that vold will query later for decrypting |
| 573 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 574 | if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs", |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 575 | MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) { |
| 576 | ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n", |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 577 | fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 578 | goto out; |
| 579 | } |
| 580 | encrypted = 1; |
| 581 | } else { |
| 582 | ERROR("Cannot mount filesystem on %s at %s\n", |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 583 | fstab->recs[i].blk_device, fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 584 | goto out; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | if (encrypted) { |
| 589 | ret = 1; |
| 590 | } else { |
| 591 | ret = 0; |
| 592 | } |
| 593 | |
| 594 | out: |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 595 | return ret; |
| 596 | } |
| 597 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 598 | /* 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] | 599 | * tmp mount we do to check the user password |
| 600 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 601 | int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, |
| 602 | char *tmp_mount_point) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 603 | { |
| 604 | int i = 0; |
| 605 | int ret = -1; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 606 | char *m; |
| 607 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 608 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 609 | return ret; |
| 610 | } |
| 611 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 612 | for (i = 0; i < fstab->num_entries; i++) { |
| 613 | if (!fs_match(fstab->recs[i].mount_point, n_name)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 614 | continue; |
| 615 | } |
| 616 | |
| 617 | /* We found our match */ |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 618 | /* If this swap or a raw partition, report an error */ |
| 619 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 620 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 621 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 622 | ERROR("Cannot mount filesystem of type %s on %s\n", |
| 623 | fstab->recs[i].fs_type, n_blk_device); |
| 624 | goto out; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 625 | } |
| 626 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 627 | /* First check the filesystem if requested */ |
| 628 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 629 | wait_for_file(n_blk_device, WAIT_TIMEOUT); |
| 630 | } |
| 631 | |
| 632 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 633 | check_fs(n_blk_device, fstab->recs[i].fs_type, |
| 634 | fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 637 | if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) { |
| 638 | if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) { |
| 639 | ERROR("Could not set up verified partition, skipping!"); |
| 640 | continue; |
| 641 | } |
| 642 | } |
| 643 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 644 | /* Now mount it where requested */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 645 | if (tmp_mount_point) { |
| 646 | m = tmp_mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 647 | } else { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 648 | m = fstab->recs[i].mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 649 | } |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 650 | if (__mount(n_blk_device, m, fstab->recs[i].fs_type, |
| 651 | fstab->recs[i].flags, fstab->recs[i].fs_options)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 652 | ERROR("Cannot mount filesystem on %s at %s\n", |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 653 | n_blk_device, m); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 654 | goto out; |
| 655 | } else { |
| 656 | ret = 0; |
| 657 | goto out; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /* We didn't find a match, say so and return an error */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 662 | ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 663 | |
| 664 | out: |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * mount a tmpfs filesystem at the given point. |
| 670 | * return 0 on success, non-zero on failure. |
| 671 | */ |
| 672 | int fs_mgr_do_tmpfs_mount(char *n_name) |
| 673 | { |
| 674 | int ret; |
| 675 | |
| 676 | ret = mount("tmpfs", n_name, "tmpfs", |
| 677 | MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS); |
| 678 | if (ret < 0) { |
| 679 | ERROR("Cannot mount tmpfs filesystem at %s\n", n_name); |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | /* Success */ |
| 684 | return 0; |
| 685 | } |
| 686 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 687 | int fs_mgr_unmount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 688 | { |
| 689 | int i = 0; |
| 690 | int ret = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 691 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 692 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 693 | return -1; |
| 694 | } |
| 695 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 696 | while (fstab->recs[i].blk_device) { |
| 697 | if (umount(fstab->recs[i].mount_point)) { |
| 698 | ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 699 | ret = -1; |
| 700 | } |
| 701 | i++; |
| 702 | } |
| 703 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 704 | return ret; |
| 705 | } |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 706 | |
| 707 | /* This must be called after mount_all, because the mkswap command needs to be |
| 708 | * available. |
| 709 | */ |
| 710 | int fs_mgr_swapon_all(struct fstab *fstab) |
| 711 | { |
| 712 | int i = 0; |
| 713 | int flags = 0; |
| 714 | int err = 0; |
| 715 | int ret = 0; |
| 716 | int status; |
| 717 | char *mkswap_argv[2] = { |
| 718 | MKSWAP_BIN, |
| 719 | NULL |
| 720 | }; |
| 721 | |
| 722 | if (!fstab) { |
| 723 | return -1; |
| 724 | } |
| 725 | |
| 726 | for (i = 0; i < fstab->num_entries; i++) { |
| 727 | /* Skip non-swap entries */ |
| 728 | if (strcmp(fstab->recs[i].fs_type, "swap")) { |
| 729 | continue; |
| 730 | } |
| 731 | |
| 732 | if (fstab->recs[i].zram_size > 0) { |
| 733 | /* A zram_size was specified, so we need to configure the |
| 734 | * device. There is no point in having multiple zram devices |
| 735 | * on a system (all the memory comes from the same pool) so |
| 736 | * we can assume the device number is 0. |
| 737 | */ |
| 738 | FILE *zram_fp; |
| 739 | |
| 740 | zram_fp = fopen(ZRAM_CONF_DEV, "r+"); |
| 741 | if (zram_fp == NULL) { |
| 742 | ERROR("Unable to open zram conf device " ZRAM_CONF_DEV); |
| 743 | ret = -1; |
| 744 | continue; |
| 745 | } |
| 746 | fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size); |
| 747 | fclose(zram_fp); |
| 748 | } |
| 749 | |
| 750 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 751 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 752 | } |
| 753 | |
| 754 | /* Initialize the swap area */ |
| 755 | mkswap_argv[1] = fstab->recs[i].blk_device; |
| 756 | err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv, |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 757 | &status, true, LOG_KLOG, false, NULL); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 758 | if (err) { |
| 759 | ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device); |
| 760 | ret = -1; |
| 761 | continue; |
| 762 | } |
| 763 | |
| 764 | /* If -1, then no priority was specified in fstab, so don't set |
| 765 | * SWAP_FLAG_PREFER or encode the priority */ |
| 766 | if (fstab->recs[i].swap_prio >= 0) { |
| 767 | flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) & |
| 768 | SWAP_FLAG_PRIO_MASK; |
| 769 | flags |= SWAP_FLAG_PREFER; |
| 770 | } else { |
| 771 | flags = 0; |
| 772 | } |
| 773 | err = swapon(fstab->recs[i].blk_device, flags); |
| 774 | if (err) { |
| 775 | ERROR("swapon failed for %s\n", fstab->recs[i].blk_device); |
| 776 | ret = -1; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | return ret; |
| 781 | } |
| 782 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 783 | /* |
| 784 | * key_loc must be at least PROPERTY_VALUE_MAX bytes long |
| 785 | * |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 786 | * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 787 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 788 | 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] | 789 | { |
| 790 | int i = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 791 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 792 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 793 | return -1; |
| 794 | } |
| 795 | /* Initialize return values to null strings */ |
| 796 | if (key_loc) { |
| 797 | *key_loc = '\0'; |
| 798 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 799 | if (real_blk_device) { |
| 800 | *real_blk_device = '\0'; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* Look for the encryptable partition to find the data */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 804 | for (i = 0; i < fstab->num_entries; i++) { |
| 805 | /* Don't deal with vold managed enryptable partitions here */ |
| 806 | if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) { |
| 807 | continue; |
| 808 | } |
| 809 | if (!(fstab->recs[i].fs_mgr_flags & MF_CRYPT)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 810 | continue; |
| 811 | } |
| 812 | |
| 813 | /* We found a match */ |
| 814 | if (key_loc) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 815 | strlcpy(key_loc, fstab->recs[i].key_loc, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 816 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 817 | if (real_blk_device) { |
| 818 | strlcpy(real_blk_device, fstab->recs[i].blk_device, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 819 | } |
| 820 | break; |
| 821 | } |
| 822 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 823 | return 0; |
| 824 | } |
| 825 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 826 | /* Add an entry to the fstab, and return 0 on success or -1 on error */ |
| 827 | int fs_mgr_add_entry(struct fstab *fstab, |
| 828 | const char *mount_point, const char *fs_type, |
| 829 | const char *blk_device, long long length) |
| 830 | { |
| 831 | struct fstab_rec *new_fstab_recs; |
| 832 | int n = fstab->num_entries; |
| 833 | |
| 834 | new_fstab_recs = (struct fstab_rec *) |
| 835 | realloc(fstab->recs, sizeof(struct fstab_rec) * (n + 1)); |
| 836 | |
| 837 | if (!new_fstab_recs) { |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | /* A new entry was added, so initialize it */ |
| 842 | memset(&new_fstab_recs[n], 0, sizeof(struct fstab_rec)); |
| 843 | new_fstab_recs[n].mount_point = strdup(mount_point); |
| 844 | new_fstab_recs[n].fs_type = strdup(fs_type); |
| 845 | new_fstab_recs[n].blk_device = strdup(blk_device); |
| 846 | new_fstab_recs[n].length = 0; |
| 847 | |
| 848 | /* Update the fstab struct */ |
| 849 | fstab->recs = new_fstab_recs; |
| 850 | fstab->num_entries++; |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path) |
| 856 | { |
| 857 | int i; |
| 858 | |
| 859 | if (!fstab) { |
| 860 | return NULL; |
| 861 | } |
| 862 | |
| 863 | for (i = 0; i < fstab->num_entries; i++) { |
| 864 | int len = strlen(fstab->recs[i].mount_point); |
| 865 | if (strncmp(path, fstab->recs[i].mount_point, len) == 0 && |
| 866 | (path[len] == '\0' || path[len] == '/')) { |
| 867 | return &fstab->recs[i]; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return NULL; |
| 872 | } |
| 873 | |
| 874 | int fs_mgr_is_voldmanaged(struct fstab_rec *fstab) |
| 875 | { |
| 876 | return fstab->fs_mgr_flags & MF_VOLDMANAGED; |
| 877 | } |
| 878 | |
| 879 | int fs_mgr_is_nonremovable(struct fstab_rec *fstab) |
| 880 | { |
| 881 | return fstab->fs_mgr_flags & MF_NONREMOVABLE; |
| 882 | } |
| 883 | |
| 884 | int fs_mgr_is_encryptable(struct fstab_rec *fstab) |
| 885 | { |
| 886 | return fstab->fs_mgr_flags & MF_CRYPT; |
| 887 | } |
Ken Sumrall | 887f289 | 2013-09-11 19:42:51 -0700 | [diff] [blame] | 888 | |
| 889 | int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab) |
| 890 | { |
| 891 | return fstab->fs_mgr_flags & MF_NOEMULATEDSD; |
| 892 | } |