blob: a4ef7dcd3671b2bbc8e735fbecba9780fca2eafc [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
Ken Sumrallab6b8522013-02-13 12:58:40 -0800239struct fstab *fs_mgr_read_fstab(const char *fstab_path)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800240{
241 FILE *fstab_file;
242 int cnt, entries;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300243 ssize_t len;
244 size_t alloc_len = 0;
245 char *line = NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800246 const char *delim = " \t";
247 char *save_ptr, *p;
Irina Tirdead431b8d2013-09-18 23:00:54 +0300248 struct fstab *fstab = NULL;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800249 struct fstab_rec *recs;
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700250 struct fs_mgr_flag_values flag_vals;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800251#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 Tirdeae16d7472013-09-06 19:19:44 +0300261 while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800262 /* if the last character is a newline, shorten the string by 1 byte */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800263 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 Tirdeae16d7472013-09-06 19:19:44 +0300279 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800280 }
281
Ken Sumrallab6b8522013-02-13 12:58:40 -0800282 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800287
288 fseek(fstab_file, 0, SEEK_SET);
289
290 cnt = 0;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300291 while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800292 /* if the last character is a newline, shorten the string by 1 byte */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800293 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 Tirdeae16d7472013-09-06 19:19:44 +0300317 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800318 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800319 fstab->recs[cnt].blk_device = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800320
321 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800322 ERROR("Error parsing mount_point\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300323 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800324 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800325 fstab->recs[cnt].mount_point = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800326
327 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
328 ERROR("Error parsing fs_type\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300329 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800330 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800331 fstab->recs[cnt].fs_type = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800332
333 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
334 ERROR("Error parsing mount_flags\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300335 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800336 }
337 tmp_fs_options[0] = '\0';
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700338 fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL,
Ken Sumrallab6b8522013-02-13 12:58:40 -0800339 tmp_fs_options, FS_OPTIONS_LEN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800340
341 /* fs_options are optional */
342 if (tmp_fs_options[0]) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800343 fstab->recs[cnt].fs_options = strdup(tmp_fs_options);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800344 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800345 fstab->recs[cnt].fs_options = NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800346 }
347
348 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
349 ERROR("Error parsing fs_mgr_options\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300350 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800351 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800352 fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700353 &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 Sumrallc1bf8962012-01-06 19:09:42 -0800360 cnt++;
361 }
362 fclose(fstab_file);
Irina Tirdeae16d7472013-09-06 19:19:44 +0300363 free(line);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800364 return fstab;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300365
366err:
Irina Tirdead431b8d2013-09-18 23:00:54 +0300367 fclose(fstab_file);
Irina Tirdeae16d7472013-09-06 19:19:44 +0300368 free(line);
Irina Tirdead431b8d2013-09-18 23:00:54 +0300369 if (fstab)
370 fs_mgr_free_fstab(fstab);
Irina Tirdeae16d7472013-09-06 19:19:44 +0300371 return NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800372}
373
Ken Sumrallab6b8522013-02-13 12:58:40 -0800374void fs_mgr_free_fstab(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800375{
Ken Sumrallab6b8522013-02-13 12:58:40 -0800376 int i;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800377
Ken Sumrallab6b8522013-02-13 12:58:40 -0800378 for (i = 0; i < fstab->num_entries; i++) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800379 /* Free the pointers return by strdup(3) */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800380 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 Sumrallc1bf8962012-01-06 19:09:42 -0800386 }
387
Ken Sumrallab6b8522013-02-13 12:58:40 -0800388 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800395 free(fstab);
396}
397
Ken Sumrallab6b8522013-02-13 12:58:40 -0800398static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800399{
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800400 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700401 int ret;
402 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
403 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -0700404 char *e2fsck_argv[] = {
405 E2FSCK_BIN,
406 "-y",
407 blk_device
408 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800409
410 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800411 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700412 /*
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 Sumrallab6b8522013-02-13 12:58:40 -0800425 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
426 if (!ret) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700427 umount(target);
428 }
429
Ken Sumrallab6b8522013-02-13 12:58:40 -0800430 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800431
Ken Sumrallbf021b42013-03-19 19:38:44 -0700432 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
433 &status, true, LOG_KLOG, true);
434
435 if (ret < 0) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800436 /* No need to check for error in fork, we can't really handle it now */
Ken Sumrallbf021b42013-03-19 19:38:44 -0700437 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800438 }
439 }
440
441 return;
442}
443
444static 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 Kraleviche18c0d52013-04-16 16:41:32 -0700455/*
456 * Mark the given block device as read-only, using the BLKROSET ioctl.
457 * Return 0 on success, and -1 on error.
458 */
459static 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 */
479static 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 Sumrallc1bf8962012-01-06 19:09:42 -0800492static 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 Sumrallab6b8522013-02-13 12:58:40 -0800512int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800513{
514 int i = 0;
515 int encrypted = 0;
516 int ret = -1;
517 int mret;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800518
Ken Sumrallab6b8522013-02-13 12:58:40 -0800519 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800520 return ret;
521 }
522
Ken Sumrallab6b8522013-02-13 12:58:40 -0800523 for (i = 0; i < fstab->num_entries; i++) {
524 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800525 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800526 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800527 }
528
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700529 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800532 !strcmp(fstab->recs[i].fs_type, "mtd")) {
533 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800534 }
535
Ken Sumrallab6b8522013-02-13 12:58:40 -0800536 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 Condra3ad3d1c2013-02-22 18:11:41 -0800545 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 Kraleviche18c0d52013-04-16 16:41:32 -0700552 mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point,
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800553 fstab->recs[i].fs_type, fstab->recs[i].flags,
554 fstab->recs[i].fs_options);
555
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800556 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 Sumrallab6b8522013-02-13 12:58:40 -0800562 if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) &&
563 !partition_wiped(fstab->recs[i].blk_device)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800564 /* Need to mount a tmpfs at this mountpoint for now, and set
565 * properties that vold will query later for decrypting
566 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800567 if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs",
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800568 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
569 ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800570 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800571 goto out;
572 }
573 encrypted = 1;
574 } else {
575 ERROR("Cannot mount filesystem on %s at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800576 fstab->recs[i].blk_device, fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800577 goto out;
578 }
579 }
580
581 if (encrypted) {
582 ret = 1;
583 } else {
584 ret = 0;
585 }
586
587out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800588 return ret;
589}
590
Ken Sumrallab6b8522013-02-13 12:58:40 -0800591/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800592 * tmp mount we do to check the user password
593 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800594int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
595 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800596{
597 int i = 0;
598 int ret = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800599 char *m;
600
Ken Sumrallab6b8522013-02-13 12:58:40 -0800601 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800602 return ret;
603 }
604
Ken Sumrallab6b8522013-02-13 12:58:40 -0800605 for (i = 0; i < fstab->num_entries; i++) {
606 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800607 continue;
608 }
609
610 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700611 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800614 !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 Sumrallc1bf8962012-01-06 19:09:42 -0800618 }
619
Ken Sumrallab6b8522013-02-13 12:58:40 -0800620 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800628 }
629
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800630 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 Sumrallc1bf8962012-01-06 19:09:42 -0800637 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800638 if (tmp_mount_point) {
639 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800640 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800641 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800642 }
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700643 if (__mount(n_blk_device, m, fstab->recs[i].fs_type,
644 fstab->recs[i].flags, fstab->recs[i].fs_options)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800645 ERROR("Cannot mount filesystem on %s at %s\n",
Ken Sumrallab6b8522013-02-13 12:58:40 -0800646 n_blk_device, m);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800647 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 Sumrallab6b8522013-02-13 12:58:40 -0800655 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800656
657out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800658 return ret;
659}
660
661/*
662 * mount a tmpfs filesystem at the given point.
663 * return 0 on success, non-zero on failure.
664 */
665int 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 Sumrallab6b8522013-02-13 12:58:40 -0800680int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800681{
682 int i = 0;
683 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800684
Ken Sumrallab6b8522013-02-13 12:58:40 -0800685 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800686 return -1;
687 }
688
Ken Sumrallab6b8522013-02-13 12:58:40 -0800689 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 Sumrallc1bf8962012-01-06 19:09:42 -0800692 ret = -1;
693 }
694 i++;
695 }
696
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800697 return ret;
698}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700699
700/* This must be called after mount_all, because the mkswap command needs to be
701 * available.
702 */
703int 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 Sumrallc1bf8962012-01-06 19:09:42 -0800776/*
777 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
778 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800779 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800780 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800781int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800782{
783 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800784
Ken Sumrallab6b8522013-02-13 12:58:40 -0800785 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800786 return -1;
787 }
788 /* Initialize return values to null strings */
789 if (key_loc) {
790 *key_loc = '\0';
791 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800792 if (real_blk_device) {
793 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800794 }
795
796 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800797 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 Sumrallc1bf8962012-01-06 19:09:42 -0800803 continue;
804 }
805
806 /* We found a match */
807 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800808 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800809 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800810 if (real_blk_device) {
811 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800812 }
813 break;
814 }
815
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800816 return 0;
817}
818
Ken Sumrallab6b8522013-02-13 12:58:40 -0800819/* Add an entry to the fstab, and return 0 on success or -1 on error */
820int 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
848struct 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
867int fs_mgr_is_voldmanaged(struct fstab_rec *fstab)
868{
869 return fstab->fs_mgr_flags & MF_VOLDMANAGED;
870}
871
872int fs_mgr_is_nonremovable(struct fstab_rec *fstab)
873{
874 return fstab->fs_mgr_flags & MF_NONREMOVABLE;
875}
876
877int fs_mgr_is_encryptable(struct fstab_rec *fstab)
878{
879 return fstab->fs_mgr_flags & MF_CRYPT;
880}
Ken Sumrall887f2892013-09-11 19:42:51 -0700881
882int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab)
883{
884 return fstab->fs_mgr_flags & MF_NOEMULATEDSD;
885}