blob: ea60cc899d34ccea952a6b2bfb4e188b129ea746 [file] [log] [blame]
Ken Sumrallc1bf8962012-01-06 19:09:42 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ken Sumrallc1bf8962012-01-06 19:09:42 -080017#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <errno.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <libgen.h>
29#include <time.h>
Ken Sumrall5bc31a22013-07-08 19:11:55 -070030#include <sys/swap.h>
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 Sumrallc1bf8962012-01-06 19:09:42 -080036
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080037#include <linux/loop.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080038#include <private/android_filesystem_config.h>
39#include <cutils/partition_utils.h>
40#include <cutils/properties.h>
Ken Sumrallbf021b42013-03-19 19:38:44 -070041#include <logwrap/logwrap.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080042
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080043#include "mincrypt/rsa.h"
44#include "mincrypt/sha.h"
45#include "mincrypt/sha256.h"
46
Ken Sumrallc1bf8962012-01-06 19:09:42 -080047#include "fs_mgr_priv.h"
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080048#include "fs_mgr_priv_verity.h"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080049
50#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
51#define KEY_IN_FOOTER "footer"
52
53#define E2FSCK_BIN "/system/bin/e2fsck"
Ken Sumrall5bc31a22013-07-08 19:11:55 -070054#define MKSWAP_BIN "/system/bin/mkswap"
55
56#define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080057
Ken Sumrallbf021b42013-03-19 19:38:44 -070058#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
59
Ken Sumrallc1bf8962012-01-06 19:09:42 -080060struct flag_list {
61 const char *name;
62 unsigned flag;
63};
64
65static 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 Sharkeye50ac5f2012-08-14 11:34:34 -070074 { "bind", MS_BIND },
75 { "rec", MS_REC },
76 { "unbindable", MS_UNBINDABLE },
77 { "private", MS_PRIVATE },
78 { "slave", MS_SLAVE },
79 { "shared", MS_SHARED },
Ken Sumrallc1bf8962012-01-06 19:09:42 -080080 { "defaults", 0 },
81 { 0, 0 },
82};
83
84static struct flag_list fs_mgr_flags[] = {
85 { "wait", MF_WAIT },
86 { "check", MF_CHECK },
87 { "encryptable=",MF_CRYPT },
Ken Sumrallab6b8522013-02-13 12:58:40 -080088 { "nonremovable",MF_NONREMOVABLE },
89 { "voldmanaged=",MF_VOLDMANAGED},
90 { "length=", MF_LENGTH },
Ken Sumrall6c2c1212013-02-22 17:36:21 -080091 { "recoveryonly",MF_RECOVERYONLY },
Ken Sumrall5bc31a22013-07-08 19:11:55 -070092 { "swapprio=", MF_SWAPPRIO },
93 { "zramsize=", MF_ZRAMSIZE },
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080094 { "verify", MF_VERIFY },
Ken Sumrall887f2892013-09-11 19:42:51 -070095 { "noemulatedsd", MF_NOEMULATEDSD },
Ken Sumrallc1bf8962012-01-06 19:09:42 -080096 { "defaults", 0 },
97 { 0, 0 },
98};
99
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700100struct 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 Sumrallc1bf8962012-01-06 19:09:42 -0800109/*
110 * gettime() - returns the time in seconds of the system's monotonic clock or
111 * zero on error.
112 */
113static 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
127static 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 Sumrallab6b8522013-02-13 12:58:40 -0800139static int parse_flags(char *flags, struct flag_list *fl,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700140 struct fs_mgr_flag_values *flag_vals,
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800141 char *fs_options, int fs_options_len)
142{
143 int f = 0;
144 int i;
145 char *p;
146 char *savep;
147
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700148 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800154 }
155
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800156 /* 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 Sumrall5bc31a22013-07-08 19:11:55 -0700169 if ((fl[i].flag == MF_CRYPT) && flag_vals) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800170 /* The encryptable flag is followed by an = and the
171 * location of the keys. Get it and return it.
172 */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700173 flag_vals->key_loc = strdup(strchr(p, '=') + 1);
174 } else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800175 /* The length flag is followed by an = and the
176 * size of the partition. Get it and return it.
177 */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700178 flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0);
179 } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800180 /* 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 Sumrall5bc31a22013-07-08 19:11:55 -0700193 flag_vals->label = strndup(label_start,
194 (int) (label_end - label_start));
Ken Sumrallab6b8522013-02-13 12:58:40 -0800195 part_start = strchr(p, ':') + 1;
196 if (!strcmp(part_start, "auto")) {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700197 flag_vals->partnum = -1;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800198 } else {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700199 flag_vals->partnum = strtol(part_start, NULL, 0);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800200 }
201 } else {
202 ERROR("Warning: voldmanaged= flag malformed\n");
203 }
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700204 } 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 Sumrallc1bf8962012-01-06 19:09:42 -0800208 }
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
230out:
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 Tirdea295b82b2012-08-27 19:10:57 +0300244static char *fs_getline(char *buf, int size, FILE *file)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800245{
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 Sumrallab6b8522013-02-13 12:58:40 -0800298struct fstab *fs_mgr_read_fstab(const char *fstab_path)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800299{
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 Sumrallab6b8522013-02-13 12:58:40 -0800306 struct fstab *fstab;
307 struct fstab_rec *recs;
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700308 struct fs_mgr_flag_values flag_vals;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800309#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 Tirdea295b82b2012-08-27 19:10:57 +0300319 while (fs_getline(line, sizeof(line), fstab_file)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800320 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800341 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800346
347 fseek(fstab_file, 0, SEEK_SET);
348
349 cnt = 0;
Irina Tirdea295b82b2012-08-27 19:10:57 +0300350 while (fs_getline(line, sizeof(line), fstab_file)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800351 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800379 fstab->recs[cnt].blk_device = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800380
381 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800382 ERROR("Error parsing mount_point\n");
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800383 return 0;
384 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800385 fstab->recs[cnt].mount_point = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800386
387 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
388 ERROR("Error parsing fs_type\n");
389 return 0;
390 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800391 fstab->recs[cnt].fs_type = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800392
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 Sumrall5bc31a22013-07-08 19:11:55 -0700398 fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL,
Ken Sumrallab6b8522013-02-13 12:58:40 -0800399 tmp_fs_options, FS_OPTIONS_LEN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800400
401 /* fs_options are optional */
402 if (tmp_fs_options[0]) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800403 fstab->recs[cnt].fs_options = strdup(tmp_fs_options);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800404 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800405 fstab->recs[cnt].fs_options = NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800406 }
407
408 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
409 ERROR("Error parsing fs_mgr_options\n");
410 return 0;
411 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800412 fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700413 &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 Sumrallc1bf8962012-01-06 19:09:42 -0800420 cnt++;
421 }
422 fclose(fstab_file);
423
424 return fstab;
425}
426
Ken Sumrallab6b8522013-02-13 12:58:40 -0800427void fs_mgr_free_fstab(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800428{
Ken Sumrallab6b8522013-02-13 12:58:40 -0800429 int i;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800430
Ken Sumrallab6b8522013-02-13 12:58:40 -0800431 for (i = 0; i < fstab->num_entries; i++) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800432 /* Free the pointers return by strdup(3) */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800433 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 Sumrallc1bf8962012-01-06 19:09:42 -0800439 i++;
440 }
441
Ken Sumrallab6b8522013-02-13 12:58:40 -0800442 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800449 free(fstab);
450}
451
Ken Sumrallab6b8522013-02-13 12:58:40 -0800452static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800453{
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800454 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700455 int ret;
456 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
457 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -0700458 char *e2fsck_argv[] = {
459 E2FSCK_BIN,
460 "-y",
461 blk_device
462 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800463
464 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800465 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700466 /*
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 Sumrallab6b8522013-02-13 12:58:40 -0800479 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
480 if (!ret) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700481 umount(target);
482 }
483
Ken Sumrallab6b8522013-02-13 12:58:40 -0800484 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800485
Ken Sumrallbf021b42013-03-19 19:38:44 -0700486 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
487 &status, true, LOG_KLOG, true);
488
489 if (ret < 0) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800490 /* No need to check for error in fork, we can't really handle it now */
Ken Sumrallbf021b42013-03-19 19:38:44 -0700491 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800492 }
493 }
494
495 return;
496}
497
498static 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 Kraleviche18c0d52013-04-16 16:41:32 -0700509/*
510 * Mark the given block device as read-only, using the BLKROSET ioctl.
511 * Return 0 on success, and -1 on error.
512 */
513static 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 */
533static 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 Sumrallc1bf8962012-01-06 19:09:42 -0800546static 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 Sumrallab6b8522013-02-13 12:58:40 -0800566int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800567{
568 int i = 0;
569 int encrypted = 0;
570 int ret = -1;
571 int mret;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800572
Ken Sumrallab6b8522013-02-13 12:58:40 -0800573 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800574 return ret;
575 }
576
Ken Sumrallab6b8522013-02-13 12:58:40 -0800577 for (i = 0; i < fstab->num_entries; i++) {
578 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800579 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800580 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800581 }
582
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700583 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800586 !strcmp(fstab->recs[i].fs_type, "mtd")) {
587 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800588 }
589
Ken Sumrallab6b8522013-02-13 12:58:40 -0800590 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 Condra3ad3d1c2013-02-22 18:11:41 -0800599 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 Kraleviche18c0d52013-04-16 16:41:32 -0700606 mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point,
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800607 fstab->recs[i].fs_type, fstab->recs[i].flags,
608 fstab->recs[i].fs_options);
609
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800610 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 Sumrallab6b8522013-02-13 12:58:40 -0800616 if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) &&
617 !partition_wiped(fstab->recs[i].blk_device)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800618 /* Need to mount a tmpfs at this mountpoint for now, and set
619 * properties that vold will query later for decrypting
620 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800621 if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs",
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800622 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
623 ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800624 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800625 goto out;
626 }
627 encrypted = 1;
628 } else {
629 ERROR("Cannot mount filesystem on %s at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800630 fstab->recs[i].blk_device, fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800631 goto out;
632 }
633 }
634
635 if (encrypted) {
636 ret = 1;
637 } else {
638 ret = 0;
639 }
640
641out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800642 return ret;
643}
644
Ken Sumrallab6b8522013-02-13 12:58:40 -0800645/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800646 * tmp mount we do to check the user password
647 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800648int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
649 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800650{
651 int i = 0;
652 int ret = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800653 char *m;
654
Ken Sumrallab6b8522013-02-13 12:58:40 -0800655 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800656 return ret;
657 }
658
Ken Sumrallab6b8522013-02-13 12:58:40 -0800659 for (i = 0; i < fstab->num_entries; i++) {
660 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800661 continue;
662 }
663
664 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700665 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800668 !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 Sumrallc1bf8962012-01-06 19:09:42 -0800672 }
673
Ken Sumrallab6b8522013-02-13 12:58:40 -0800674 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800682 }
683
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800684 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 Sumrallc1bf8962012-01-06 19:09:42 -0800691 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800692 if (tmp_mount_point) {
693 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800694 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800695 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800696 }
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700697 if (__mount(n_blk_device, m, fstab->recs[i].fs_type,
698 fstab->recs[i].flags, fstab->recs[i].fs_options)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800699 ERROR("Cannot mount filesystem on %s at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800700 n_blk_device, m);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800701 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 Sumrallab6b8522013-02-13 12:58:40 -0800709 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800710
711out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800712 return ret;
713}
714
715/*
716 * mount a tmpfs filesystem at the given point.
717 * return 0 on success, non-zero on failure.
718 */
719int 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 Sumrallab6b8522013-02-13 12:58:40 -0800734int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800735{
736 int i = 0;
737 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800738
Ken Sumrallab6b8522013-02-13 12:58:40 -0800739 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800740 return -1;
741 }
742
Ken Sumrallab6b8522013-02-13 12:58:40 -0800743 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 Sumrallc1bf8962012-01-06 19:09:42 -0800746 ret = -1;
747 }
748 i++;
749 }
750
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800751 return ret;
752}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700753
754/* This must be called after mount_all, because the mkswap command needs to be
755 * available.
756 */
757int 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 Sumrallc1bf8962012-01-06 19:09:42 -0800830/*
831 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
832 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800833 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800834 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800835int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800836{
837 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800838
Ken Sumrallab6b8522013-02-13 12:58:40 -0800839 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800840 return -1;
841 }
842 /* Initialize return values to null strings */
843 if (key_loc) {
844 *key_loc = '\0';
845 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800846 if (real_blk_device) {
847 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800848 }
849
850 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800851 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 Sumrallc1bf8962012-01-06 19:09:42 -0800857 continue;
858 }
859
860 /* We found a match */
861 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800862 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800863 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800864 if (real_blk_device) {
865 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800866 }
867 break;
868 }
869
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800870 return 0;
871}
872
Ken Sumrallab6b8522013-02-13 12:58:40 -0800873/* Add an entry to the fstab, and return 0 on success or -1 on error */
874int 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
902struct 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
921int fs_mgr_is_voldmanaged(struct fstab_rec *fstab)
922{
923 return fstab->fs_mgr_flags & MF_VOLDMANAGED;
924}
925
926int fs_mgr_is_nonremovable(struct fstab_rec *fstab)
927{
928 return fstab->fs_mgr_flags & MF_NONREMOVABLE;
929}
930
931int fs_mgr_is_encryptable(struct fstab_rec *fstab)
932{
933 return fstab->fs_mgr_flags & MF_CRYPT;
934}
Ken Sumrall887f2892013-09-11 19:42:51 -0700935
936int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab)
937{
938 return fstab->fs_mgr_flags & MF_NOEMULATEDSD;
939}