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 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 239 | struct fstab *fs_mgr_read_fstab(const char *fstab_path) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 240 | { |
| 241 | FILE *fstab_file; |
| 242 | int cnt, entries; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 243 | ssize_t len; |
| 244 | size_t alloc_len = 0; |
| 245 | char *line = NULL; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 246 | const char *delim = " \t"; |
| 247 | char *save_ptr, *p; |
Irina Tirdea | d431b8d | 2013-09-18 23:00:54 +0300 | [diff] [blame] | 248 | struct fstab *fstab = NULL; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 249 | struct fstab_rec *recs; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 250 | struct fs_mgr_flag_values flag_vals; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 251 | #define FS_OPTIONS_LEN 1024 |
| 252 | char tmp_fs_options[FS_OPTIONS_LEN]; |
| 253 | |
| 254 | fstab_file = fopen(fstab_path, "r"); |
| 255 | if (!fstab_file) { |
| 256 | ERROR("Cannot open file %s\n", fstab_path); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | entries = 0; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 261 | while ((len = getline(&line, &alloc_len, fstab_file)) != -1) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 262 | /* 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] | 263 | if (line[len - 1] == '\n') { |
| 264 | line[len - 1] = '\0'; |
| 265 | } |
| 266 | /* Skip any leading whitespace */ |
| 267 | p = line; |
| 268 | while (isspace(*p)) { |
| 269 | p++; |
| 270 | } |
| 271 | /* ignore comments or empty lines */ |
| 272 | if (*p == '#' || *p == '\0') |
| 273 | continue; |
| 274 | entries++; |
| 275 | } |
| 276 | |
| 277 | if (!entries) { |
| 278 | ERROR("No entries found in fstab\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 279 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 282 | /* Allocate and init the fstab structure */ |
| 283 | fstab = calloc(1, sizeof(struct fstab)); |
| 284 | fstab->num_entries = entries; |
| 285 | fstab->fstab_filename = strdup(fstab_path); |
| 286 | fstab->recs = calloc(fstab->num_entries, sizeof(struct fstab_rec)); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 287 | |
| 288 | fseek(fstab_file, 0, SEEK_SET); |
| 289 | |
| 290 | cnt = 0; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 291 | while ((len = getline(&line, &alloc_len, fstab_file)) != -1) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 292 | /* 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] | 293 | if (line[len - 1] == '\n') { |
| 294 | line[len - 1] = '\0'; |
| 295 | } |
| 296 | |
| 297 | /* Skip any leading whitespace */ |
| 298 | p = line; |
| 299 | while (isspace(*p)) { |
| 300 | p++; |
| 301 | } |
| 302 | /* ignore comments or empty lines */ |
| 303 | if (*p == '#' || *p == '\0') |
| 304 | continue; |
| 305 | |
| 306 | /* If a non-comment entry is greater than the size we allocated, give an |
| 307 | * error and quit. This can happen in the unlikely case the file changes |
| 308 | * between the two reads. |
| 309 | */ |
| 310 | if (cnt >= entries) { |
| 311 | ERROR("Tried to process more entries than counted\n"); |
| 312 | break; |
| 313 | } |
| 314 | |
| 315 | if (!(p = strtok_r(line, delim, &save_ptr))) { |
| 316 | ERROR("Error parsing mount source\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 317 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 318 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 319 | fstab->recs[cnt].blk_device = strdup(p); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 320 | |
| 321 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 322 | ERROR("Error parsing mount_point\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 323 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 324 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 325 | fstab->recs[cnt].mount_point = strdup(p); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 326 | |
| 327 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
| 328 | ERROR("Error parsing fs_type\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 329 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 330 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 331 | fstab->recs[cnt].fs_type = strdup(p); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 332 | |
| 333 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
| 334 | ERROR("Error parsing mount_flags\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 335 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 336 | } |
| 337 | tmp_fs_options[0] = '\0'; |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 338 | fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL, |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 339 | tmp_fs_options, FS_OPTIONS_LEN); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 340 | |
| 341 | /* fs_options are optional */ |
| 342 | if (tmp_fs_options[0]) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 343 | fstab->recs[cnt].fs_options = strdup(tmp_fs_options); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 344 | } else { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 345 | fstab->recs[cnt].fs_options = NULL; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | if (!(p = strtok_r(NULL, delim, &save_ptr))) { |
| 349 | ERROR("Error parsing fs_mgr_options\n"); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 350 | goto err; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 351 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 352 | fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags, |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 353 | &flag_vals, NULL, 0); |
| 354 | fstab->recs[cnt].key_loc = flag_vals.key_loc; |
| 355 | fstab->recs[cnt].length = flag_vals.part_length; |
| 356 | fstab->recs[cnt].label = flag_vals.label; |
| 357 | fstab->recs[cnt].partnum = flag_vals.partnum; |
| 358 | fstab->recs[cnt].swap_prio = flag_vals.swap_prio; |
| 359 | fstab->recs[cnt].zram_size = flag_vals.zram_size; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 360 | cnt++; |
| 361 | } |
| 362 | fclose(fstab_file); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 363 | free(line); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 364 | return fstab; |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 365 | |
| 366 | err: |
Irina Tirdea | d431b8d | 2013-09-18 23:00:54 +0300 | [diff] [blame] | 367 | fclose(fstab_file); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 368 | free(line); |
Irina Tirdea | d431b8d | 2013-09-18 23:00:54 +0300 | [diff] [blame] | 369 | if (fstab) |
| 370 | fs_mgr_free_fstab(fstab); |
Irina Tirdea | e16d747 | 2013-09-06 19:19:44 +0300 | [diff] [blame] | 371 | return NULL; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 374 | void fs_mgr_free_fstab(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 375 | { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 376 | int i; |
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 | for (i = 0; i < fstab->num_entries; i++) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 379 | /* Free the pointers return by strdup(3) */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 380 | free(fstab->recs[i].blk_device); |
| 381 | free(fstab->recs[i].mount_point); |
| 382 | free(fstab->recs[i].fs_type); |
| 383 | free(fstab->recs[i].fs_options); |
| 384 | free(fstab->recs[i].key_loc); |
| 385 | free(fstab->recs[i].label); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 388 | /* Free the fstab_recs array created by calloc(3) */ |
| 389 | free(fstab->recs); |
| 390 | |
| 391 | /* Free the fstab filename */ |
| 392 | free(fstab->fstab_filename); |
| 393 | |
| 394 | /* Free fstab */ |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 395 | free(fstab); |
| 396 | } |
| 397 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 398 | static void check_fs(char *blk_device, char *fs_type, char *target) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 399 | { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 400 | int status; |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 401 | int ret; |
| 402 | long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; |
| 403 | char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro"; |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 404 | char *e2fsck_argv[] = { |
| 405 | E2FSCK_BIN, |
| 406 | "-y", |
| 407 | blk_device |
| 408 | }; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 409 | |
| 410 | /* Check for the types of filesystems we know how to check */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 411 | 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] | 412 | /* |
| 413 | * First try to mount and unmount the filesystem. We do this because |
| 414 | * the kernel is more efficient than e2fsck in running the journal and |
| 415 | * processing orphaned inodes, and on at least one device with a |
| 416 | * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes |
| 417 | * to do what the kernel does in about a second. |
| 418 | * |
| 419 | * After mounting and unmounting the filesystem, run e2fsck, and if an |
| 420 | * error is recorded in the filesystem superblock, e2fsck will do a full |
| 421 | * check. Otherwise, it does nothing. If the kernel cannot mount the |
| 422 | * filesytsem due to an error, e2fsck is still run to do a full check |
| 423 | * fix the filesystem. |
| 424 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 425 | ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts); |
| 426 | if (!ret) { |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 427 | umount(target); |
| 428 | } |
| 429 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 430 | INFO("Running %s on %s\n", E2FSCK_BIN, blk_device); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 431 | |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 432 | ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, |
| 433 | &status, true, LOG_KLOG, true); |
| 434 | |
| 435 | if (ret < 0) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 436 | /* 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] | 437 | ERROR("Failed trying to run %s\n", E2FSCK_BIN); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | static void remove_trailing_slashes(char *n) |
| 445 | { |
| 446 | int len; |
| 447 | |
| 448 | len = strlen(n) - 1; |
| 449 | while ((*(n + len) == '/') && len) { |
| 450 | *(n + len) = '\0'; |
| 451 | len--; |
| 452 | } |
| 453 | } |
| 454 | |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Mark the given block device as read-only, using the BLKROSET ioctl. |
| 457 | * Return 0 on success, and -1 on error. |
| 458 | */ |
| 459 | static void fs_set_blk_ro(const char *blockdev) |
| 460 | { |
| 461 | int fd; |
| 462 | int ON = 1; |
| 463 | |
| 464 | fd = open(blockdev, O_RDONLY); |
| 465 | if (fd < 0) { |
| 466 | // should never happen |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | ioctl(fd, BLKROSET, &ON); |
| 471 | close(fd); |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * __mount(): wrapper around the mount() system call which also |
| 476 | * sets the underlying block device to read-only if the mount is read-only. |
| 477 | * See "man 2 mount" for return values. |
| 478 | */ |
| 479 | static int __mount(const char *source, const char *target, |
| 480 | const char *filesystemtype, unsigned long mountflags, |
| 481 | const void *data) |
| 482 | { |
| 483 | int ret = mount(source, target, filesystemtype, mountflags, data); |
| 484 | |
| 485 | if ((ret == 0) && (mountflags & MS_RDONLY) != 0) { |
| 486 | fs_set_blk_ro(source); |
| 487 | } |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 492 | static int fs_match(char *in1, char *in2) |
| 493 | { |
| 494 | char *n1; |
| 495 | char *n2; |
| 496 | int ret; |
| 497 | |
| 498 | n1 = strdup(in1); |
| 499 | n2 = strdup(in2); |
| 500 | |
| 501 | remove_trailing_slashes(n1); |
| 502 | remove_trailing_slashes(n2); |
| 503 | |
| 504 | ret = !strcmp(n1, n2); |
| 505 | |
| 506 | free(n1); |
| 507 | free(n2); |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 512 | int fs_mgr_mount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 513 | { |
| 514 | int i = 0; |
| 515 | int encrypted = 0; |
| 516 | int ret = -1; |
| 517 | int mret; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 518 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 519 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 523 | for (i = 0; i < fstab->num_entries; i++) { |
| 524 | /* Don't mount entries that are managed by vold */ |
Ken Sumrall | 6c2c121 | 2013-02-22 17:36:21 -0800 | [diff] [blame] | 525 | if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 526 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 529 | /* Skip swap and raw partition entries such as boot, recovery, etc */ |
| 530 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 531 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 532 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 533 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 534 | } |
| 535 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 536 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 537 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 538 | } |
| 539 | |
| 540 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 541 | check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type, |
| 542 | fstab->recs[i].mount_point); |
| 543 | } |
| 544 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 545 | if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) { |
| 546 | if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) { |
| 547 | ERROR("Could not set up verified partition, skipping!"); |
| 548 | continue; |
| 549 | } |
| 550 | } |
| 551 | |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 552 | mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 553 | fstab->recs[i].fs_type, fstab->recs[i].flags, |
| 554 | fstab->recs[i].fs_options); |
| 555 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 556 | if (!mret) { |
| 557 | /* Success! Go get the next one */ |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | /* 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] | 562 | if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) && |
| 563 | !partition_wiped(fstab->recs[i].blk_device)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 564 | /* Need to mount a tmpfs at this mountpoint for now, and set |
| 565 | * properties that vold will query later for decrypting |
| 566 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 567 | if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs", |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 568 | MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) { |
| 569 | ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n", |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 570 | fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 571 | goto out; |
| 572 | } |
| 573 | encrypted = 1; |
| 574 | } else { |
| 575 | ERROR("Cannot mount filesystem on %s at %s\n", |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 576 | fstab->recs[i].blk_device, fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 577 | goto out; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (encrypted) { |
| 582 | ret = 1; |
| 583 | } else { |
| 584 | ret = 0; |
| 585 | } |
| 586 | |
| 587 | out: |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 588 | return ret; |
| 589 | } |
| 590 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 591 | /* 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] | 592 | * tmp mount we do to check the user password |
| 593 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 594 | int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, |
| 595 | char *tmp_mount_point) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 596 | { |
| 597 | int i = 0; |
| 598 | int ret = -1; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 599 | char *m; |
| 600 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 601 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 602 | return ret; |
| 603 | } |
| 604 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 605 | for (i = 0; i < fstab->num_entries; i++) { |
| 606 | if (!fs_match(fstab->recs[i].mount_point, n_name)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 607 | continue; |
| 608 | } |
| 609 | |
| 610 | /* We found our match */ |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 611 | /* If this swap or a raw partition, report an error */ |
| 612 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 613 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 614 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 615 | ERROR("Cannot mount filesystem of type %s on %s\n", |
| 616 | fstab->recs[i].fs_type, n_blk_device); |
| 617 | goto out; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 620 | /* First check the filesystem if requested */ |
| 621 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 622 | wait_for_file(n_blk_device, WAIT_TIMEOUT); |
| 623 | } |
| 624 | |
| 625 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 626 | check_fs(n_blk_device, fstab->recs[i].fs_type, |
| 627 | fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 630 | if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) { |
| 631 | if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) { |
| 632 | ERROR("Could not set up verified partition, skipping!"); |
| 633 | continue; |
| 634 | } |
| 635 | } |
| 636 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 637 | /* Now mount it where requested */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 638 | if (tmp_mount_point) { |
| 639 | m = tmp_mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 640 | } else { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 641 | m = fstab->recs[i].mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 642 | } |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 643 | if (__mount(n_blk_device, m, fstab->recs[i].fs_type, |
| 644 | fstab->recs[i].flags, fstab->recs[i].fs_options)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 645 | ERROR("Cannot mount filesystem on %s at %s\n", |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 646 | n_blk_device, m); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 647 | goto out; |
| 648 | } else { |
| 649 | ret = 0; |
| 650 | goto out; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* We didn't find a match, say so and return an error */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 655 | 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] | 656 | |
| 657 | out: |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * mount a tmpfs filesystem at the given point. |
| 663 | * return 0 on success, non-zero on failure. |
| 664 | */ |
| 665 | int fs_mgr_do_tmpfs_mount(char *n_name) |
| 666 | { |
| 667 | int ret; |
| 668 | |
| 669 | ret = mount("tmpfs", n_name, "tmpfs", |
| 670 | MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS); |
| 671 | if (ret < 0) { |
| 672 | ERROR("Cannot mount tmpfs filesystem at %s\n", n_name); |
| 673 | return -1; |
| 674 | } |
| 675 | |
| 676 | /* Success */ |
| 677 | return 0; |
| 678 | } |
| 679 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 680 | int fs_mgr_unmount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 681 | { |
| 682 | int i = 0; |
| 683 | int ret = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 684 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 685 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 686 | return -1; |
| 687 | } |
| 688 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 689 | while (fstab->recs[i].blk_device) { |
| 690 | if (umount(fstab->recs[i].mount_point)) { |
| 691 | ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 692 | ret = -1; |
| 693 | } |
| 694 | i++; |
| 695 | } |
| 696 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 697 | return ret; |
| 698 | } |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 699 | |
| 700 | /* This must be called after mount_all, because the mkswap command needs to be |
| 701 | * available. |
| 702 | */ |
| 703 | int fs_mgr_swapon_all(struct fstab *fstab) |
| 704 | { |
| 705 | int i = 0; |
| 706 | int flags = 0; |
| 707 | int err = 0; |
| 708 | int ret = 0; |
| 709 | int status; |
| 710 | char *mkswap_argv[2] = { |
| 711 | MKSWAP_BIN, |
| 712 | NULL |
| 713 | }; |
| 714 | |
| 715 | if (!fstab) { |
| 716 | return -1; |
| 717 | } |
| 718 | |
| 719 | for (i = 0; i < fstab->num_entries; i++) { |
| 720 | /* Skip non-swap entries */ |
| 721 | if (strcmp(fstab->recs[i].fs_type, "swap")) { |
| 722 | continue; |
| 723 | } |
| 724 | |
| 725 | if (fstab->recs[i].zram_size > 0) { |
| 726 | /* A zram_size was specified, so we need to configure the |
| 727 | * device. There is no point in having multiple zram devices |
| 728 | * on a system (all the memory comes from the same pool) so |
| 729 | * we can assume the device number is 0. |
| 730 | */ |
| 731 | FILE *zram_fp; |
| 732 | |
| 733 | zram_fp = fopen(ZRAM_CONF_DEV, "r+"); |
| 734 | if (zram_fp == NULL) { |
| 735 | ERROR("Unable to open zram conf device " ZRAM_CONF_DEV); |
| 736 | ret = -1; |
| 737 | continue; |
| 738 | } |
| 739 | fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size); |
| 740 | fclose(zram_fp); |
| 741 | } |
| 742 | |
| 743 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 744 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 745 | } |
| 746 | |
| 747 | /* Initialize the swap area */ |
| 748 | mkswap_argv[1] = fstab->recs[i].blk_device; |
| 749 | err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv, |
| 750 | &status, true, LOG_KLOG, false); |
| 751 | if (err) { |
| 752 | ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device); |
| 753 | ret = -1; |
| 754 | continue; |
| 755 | } |
| 756 | |
| 757 | /* If -1, then no priority was specified in fstab, so don't set |
| 758 | * SWAP_FLAG_PREFER or encode the priority */ |
| 759 | if (fstab->recs[i].swap_prio >= 0) { |
| 760 | flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) & |
| 761 | SWAP_FLAG_PRIO_MASK; |
| 762 | flags |= SWAP_FLAG_PREFER; |
| 763 | } else { |
| 764 | flags = 0; |
| 765 | } |
| 766 | err = swapon(fstab->recs[i].blk_device, flags); |
| 767 | if (err) { |
| 768 | ERROR("swapon failed for %s\n", fstab->recs[i].blk_device); |
| 769 | ret = -1; |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | return ret; |
| 774 | } |
| 775 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 776 | /* |
| 777 | * key_loc must be at least PROPERTY_VALUE_MAX bytes long |
| 778 | * |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 779 | * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 780 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 781 | 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] | 782 | { |
| 783 | int i = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 784 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 785 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 786 | return -1; |
| 787 | } |
| 788 | /* Initialize return values to null strings */ |
| 789 | if (key_loc) { |
| 790 | *key_loc = '\0'; |
| 791 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 792 | if (real_blk_device) { |
| 793 | *real_blk_device = '\0'; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* Look for the encryptable partition to find the data */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 797 | for (i = 0; i < fstab->num_entries; i++) { |
| 798 | /* Don't deal with vold managed enryptable partitions here */ |
| 799 | if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) { |
| 800 | continue; |
| 801 | } |
| 802 | if (!(fstab->recs[i].fs_mgr_flags & MF_CRYPT)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 803 | continue; |
| 804 | } |
| 805 | |
| 806 | /* We found a match */ |
| 807 | if (key_loc) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 808 | strlcpy(key_loc, fstab->recs[i].key_loc, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 809 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 810 | if (real_blk_device) { |
| 811 | strlcpy(real_blk_device, fstab->recs[i].blk_device, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 812 | } |
| 813 | break; |
| 814 | } |
| 815 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 816 | return 0; |
| 817 | } |
| 818 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 819 | /* Add an entry to the fstab, and return 0 on success or -1 on error */ |
| 820 | int fs_mgr_add_entry(struct fstab *fstab, |
| 821 | const char *mount_point, const char *fs_type, |
| 822 | const char *blk_device, long long length) |
| 823 | { |
| 824 | struct fstab_rec *new_fstab_recs; |
| 825 | int n = fstab->num_entries; |
| 826 | |
| 827 | new_fstab_recs = (struct fstab_rec *) |
| 828 | realloc(fstab->recs, sizeof(struct fstab_rec) * (n + 1)); |
| 829 | |
| 830 | if (!new_fstab_recs) { |
| 831 | return -1; |
| 832 | } |
| 833 | |
| 834 | /* A new entry was added, so initialize it */ |
| 835 | memset(&new_fstab_recs[n], 0, sizeof(struct fstab_rec)); |
| 836 | new_fstab_recs[n].mount_point = strdup(mount_point); |
| 837 | new_fstab_recs[n].fs_type = strdup(fs_type); |
| 838 | new_fstab_recs[n].blk_device = strdup(blk_device); |
| 839 | new_fstab_recs[n].length = 0; |
| 840 | |
| 841 | /* Update the fstab struct */ |
| 842 | fstab->recs = new_fstab_recs; |
| 843 | fstab->num_entries++; |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path) |
| 849 | { |
| 850 | int i; |
| 851 | |
| 852 | if (!fstab) { |
| 853 | return NULL; |
| 854 | } |
| 855 | |
| 856 | for (i = 0; i < fstab->num_entries; i++) { |
| 857 | int len = strlen(fstab->recs[i].mount_point); |
| 858 | if (strncmp(path, fstab->recs[i].mount_point, len) == 0 && |
| 859 | (path[len] == '\0' || path[len] == '/')) { |
| 860 | return &fstab->recs[i]; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | int fs_mgr_is_voldmanaged(struct fstab_rec *fstab) |
| 868 | { |
| 869 | return fstab->fs_mgr_flags & MF_VOLDMANAGED; |
| 870 | } |
| 871 | |
| 872 | int fs_mgr_is_nonremovable(struct fstab_rec *fstab) |
| 873 | { |
| 874 | return fstab->fs_mgr_flags & MF_NONREMOVABLE; |
| 875 | } |
| 876 | |
| 877 | int fs_mgr_is_encryptable(struct fstab_rec *fstab) |
| 878 | { |
| 879 | return fstab->fs_mgr_flags & MF_CRYPT; |
| 880 | } |
Ken Sumrall | 887f289 | 2013-09-11 19:42:51 -0700 | [diff] [blame] | 881 | |
| 882 | int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab) |
| 883 | { |
| 884 | return fstab->fs_mgr_flags & MF_NOEMULATEDSD; |
| 885 | } |