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