blob: 24ce806dfe192dbe98ce0a8eb9b666f0e88fddbd [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>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080031
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080032#include <linux/loop.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080033#include <private/android_filesystem_config.h>
34#include <cutils/partition_utils.h>
35#include <cutils/properties.h>
Ken Sumrallbf021b42013-03-19 19:38:44 -070036#include <logwrap/logwrap.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080037
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080038#include "mincrypt/rsa.h"
39#include "mincrypt/sha.h"
40#include "mincrypt/sha256.h"
41
Ken Sumrallc1bf8962012-01-06 19:09:42 -080042#include "fs_mgr_priv.h"
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080043#include "fs_mgr_priv_verity.h"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080044
45#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
46#define KEY_IN_FOOTER "footer"
47
48#define E2FSCK_BIN "/system/bin/e2fsck"
Ken Sumrall5bc31a22013-07-08 19:11:55 -070049#define MKSWAP_BIN "/system/bin/mkswap"
50
Ken Sumrall4eaf9052013-09-18 17:49:21 -070051#define FSCK_LOG_FILE "/dev/fscklogs/log"
52
Ken Sumrall5bc31a22013-07-08 19:11:55 -070053#define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080054
Ken Sumrallbf021b42013-03-19 19:38:44 -070055#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
56
Ken Sumrallc1bf8962012-01-06 19:09:42 -080057struct flag_list {
58 const char *name;
59 unsigned flag;
60};
61
62static struct flag_list mount_flags[] = {
63 { "noatime", MS_NOATIME },
64 { "noexec", MS_NOEXEC },
65 { "nosuid", MS_NOSUID },
66 { "nodev", MS_NODEV },
67 { "nodiratime", MS_NODIRATIME },
68 { "ro", MS_RDONLY },
69 { "rw", 0 },
70 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -070071 { "bind", MS_BIND },
72 { "rec", MS_REC },
73 { "unbindable", MS_UNBINDABLE },
74 { "private", MS_PRIVATE },
75 { "slave", MS_SLAVE },
76 { "shared", MS_SHARED },
Ken Sumrallc1bf8962012-01-06 19:09:42 -080077 { "defaults", 0 },
78 { 0, 0 },
79};
80
81static struct flag_list fs_mgr_flags[] = {
82 { "wait", MF_WAIT },
83 { "check", MF_CHECK },
84 { "encryptable=",MF_CRYPT },
Ken Sumrallab6b8522013-02-13 12:58:40 -080085 { "nonremovable",MF_NONREMOVABLE },
86 { "voldmanaged=",MF_VOLDMANAGED},
87 { "length=", MF_LENGTH },
Ken Sumrall6c2c1212013-02-22 17:36:21 -080088 { "recoveryonly",MF_RECOVERYONLY },
Ken Sumrall5bc31a22013-07-08 19:11:55 -070089 { "swapprio=", MF_SWAPPRIO },
90 { "zramsize=", MF_ZRAMSIZE },
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080091 { "verify", MF_VERIFY },
Ken Sumrall887f2892013-09-11 19:42:51 -070092 { "noemulatedsd", MF_NOEMULATEDSD },
Ken Sumrallc1bf8962012-01-06 19:09:42 -080093 { "defaults", 0 },
94 { 0, 0 },
95};
96
Ken Sumrall5bc31a22013-07-08 19:11:55 -070097struct fs_mgr_flag_values {
98 char *key_loc;
99 long long part_length;
100 char *label;
101 int partnum;
102 int swap_prio;
103 unsigned int zram_size;
104};
105
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800106/*
107 * gettime() - returns the time in seconds of the system's monotonic clock or
108 * zero on error.
109 */
110static time_t gettime(void)
111{
112 struct timespec ts;
113 int ret;
114
115 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
116 if (ret < 0) {
117 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
118 return 0;
119 }
120
121 return ts.tv_sec;
122}
123
124static int wait_for_file(const char *filename, int timeout)
125{
126 struct stat info;
127 time_t timeout_time = gettime() + timeout;
128 int ret = -1;
129
130 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
131 usleep(10000);
132
133 return ret;
134}
135
Ken Sumrallab6b8522013-02-13 12:58:40 -0800136static int parse_flags(char *flags, struct flag_list *fl,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700137 struct fs_mgr_flag_values *flag_vals,
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800138 char *fs_options, int fs_options_len)
139{
140 int f = 0;
141 int i;
142 char *p;
143 char *savep;
144
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700145 /* initialize flag values. If we find a relevant flag, we'll
146 * update the value */
147 if (flag_vals) {
148 memset(flag_vals, 0, sizeof(*flag_vals));
149 flag_vals->partnum = -1;
150 flag_vals->swap_prio = -1; /* negative means it wasn't specified. */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800151 }
152
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800153 /* initialize fs_options to the null string */
154 if (fs_options && (fs_options_len > 0)) {
155 fs_options[0] = '\0';
156 }
157
158 p = strtok_r(flags, ",", &savep);
159 while (p) {
160 /* Look for the flag "p" in the flag list "fl"
161 * If not found, the loop exits with fl[i].name being null.
162 */
163 for (i = 0; fl[i].name; i++) {
164 if (!strncmp(p, fl[i].name, strlen(fl[i].name))) {
165 f |= fl[i].flag;
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700166 if ((fl[i].flag == MF_CRYPT) && flag_vals) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800167 /* The encryptable flag is followed by an = and the
168 * location of the keys. Get it and return it.
169 */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700170 flag_vals->key_loc = strdup(strchr(p, '=') + 1);
171 } else if ((fl[i].flag == MF_LENGTH) && flag_vals) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800172 /* The length flag is followed by an = and the
173 * size of the partition. Get it and return it.
174 */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700175 flag_vals->part_length = strtoll(strchr(p, '=') + 1, NULL, 0);
176 } else if ((fl[i].flag == MF_VOLDMANAGED) && flag_vals) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800177 /* The voldmanaged flag is followed by an = and the
178 * label, a colon and the partition number or the
179 * word "auto", e.g.
180 * voldmanaged=sdcard:3
181 * Get and return them.
182 */
183 char *label_start;
184 char *label_end;
185 char *part_start;
186
187 label_start = strchr(p, '=') + 1;
188 label_end = strchr(p, ':');
189 if (label_end) {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700190 flag_vals->label = strndup(label_start,
191 (int) (label_end - label_start));
Ken Sumrallab6b8522013-02-13 12:58:40 -0800192 part_start = strchr(p, ':') + 1;
193 if (!strcmp(part_start, "auto")) {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700194 flag_vals->partnum = -1;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800195 } else {
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700196 flag_vals->partnum = strtol(part_start, NULL, 0);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800197 }
198 } else {
199 ERROR("Warning: voldmanaged= flag malformed\n");
200 }
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700201 } else if ((fl[i].flag == MF_SWAPPRIO) && flag_vals) {
202 flag_vals->swap_prio = strtoll(strchr(p, '=') + 1, NULL, 0);
203 } else if ((fl[i].flag == MF_ZRAMSIZE) && flag_vals) {
204 flag_vals->zram_size = strtoll(strchr(p, '=') + 1, NULL, 0);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800205 }
206 break;
207 }
208 }
209
210 if (!fl[i].name) {
211 if (fs_options) {
212 /* It's not a known flag, so it must be a filesystem specific
213 * option. Add it to fs_options if it was passed in.
214 */
215 strlcat(fs_options, p, fs_options_len);
216 strlcat(fs_options, ",", fs_options_len);
217 } else {
218 /* fs_options was not passed in, so if the flag is unknown
219 * it's an error.
220 */
221 ERROR("Warning: unknown flag %s\n", p);
222 }
223 }
224 p = strtok_r(NULL, ",", &savep);
225 }
226
227out:
228 if (fs_options && fs_options[0]) {
229 /* remove the last trailing comma from the list of options */
230 fs_options[strlen(fs_options) - 1] = '\0';
231 }
232
233 return f;
234}
235
Ken Sumrallab6b8522013-02-13 12:58:40 -0800236struct fstab *fs_mgr_read_fstab(const char *fstab_path)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800237{
238 FILE *fstab_file;
239 int cnt, entries;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300240 ssize_t len;
241 size_t alloc_len = 0;
242 char *line = NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800243 const char *delim = " \t";
244 char *save_ptr, *p;
Irina Tirdead431b8d2013-09-18 23:00:54 +0300245 struct fstab *fstab = NULL;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800246 struct fstab_rec *recs;
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700247 struct fs_mgr_flag_values flag_vals;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800248#define FS_OPTIONS_LEN 1024
249 char tmp_fs_options[FS_OPTIONS_LEN];
250
251 fstab_file = fopen(fstab_path, "r");
252 if (!fstab_file) {
253 ERROR("Cannot open file %s\n", fstab_path);
254 return 0;
255 }
256
257 entries = 0;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300258 while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800259 /* if the last character is a newline, shorten the string by 1 byte */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800260 if (line[len - 1] == '\n') {
261 line[len - 1] = '\0';
262 }
263 /* Skip any leading whitespace */
264 p = line;
265 while (isspace(*p)) {
266 p++;
267 }
268 /* ignore comments or empty lines */
269 if (*p == '#' || *p == '\0')
270 continue;
271 entries++;
272 }
273
274 if (!entries) {
275 ERROR("No entries found in fstab\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300276 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800277 }
278
Ken Sumrallab6b8522013-02-13 12:58:40 -0800279 /* Allocate and init the fstab structure */
280 fstab = calloc(1, sizeof(struct fstab));
281 fstab->num_entries = entries;
282 fstab->fstab_filename = strdup(fstab_path);
283 fstab->recs = calloc(fstab->num_entries, sizeof(struct fstab_rec));
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800284
285 fseek(fstab_file, 0, SEEK_SET);
286
287 cnt = 0;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300288 while ((len = getline(&line, &alloc_len, fstab_file)) != -1) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800289 /* if the last character is a newline, shorten the string by 1 byte */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800290 if (line[len - 1] == '\n') {
291 line[len - 1] = '\0';
292 }
293
294 /* Skip any leading whitespace */
295 p = line;
296 while (isspace(*p)) {
297 p++;
298 }
299 /* ignore comments or empty lines */
300 if (*p == '#' || *p == '\0')
301 continue;
302
303 /* If a non-comment entry is greater than the size we allocated, give an
304 * error and quit. This can happen in the unlikely case the file changes
305 * between the two reads.
306 */
307 if (cnt >= entries) {
308 ERROR("Tried to process more entries than counted\n");
309 break;
310 }
311
312 if (!(p = strtok_r(line, delim, &save_ptr))) {
313 ERROR("Error parsing mount source\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300314 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800315 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800316 fstab->recs[cnt].blk_device = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800317
318 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800319 ERROR("Error parsing mount_point\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300320 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800321 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800322 fstab->recs[cnt].mount_point = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800323
324 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
325 ERROR("Error parsing fs_type\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300326 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800327 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800328 fstab->recs[cnt].fs_type = strdup(p);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800329
330 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
331 ERROR("Error parsing mount_flags\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300332 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800333 }
334 tmp_fs_options[0] = '\0';
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700335 fstab->recs[cnt].flags = parse_flags(p, mount_flags, NULL,
Ken Sumrallab6b8522013-02-13 12:58:40 -0800336 tmp_fs_options, FS_OPTIONS_LEN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800337
338 /* fs_options are optional */
339 if (tmp_fs_options[0]) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800340 fstab->recs[cnt].fs_options = strdup(tmp_fs_options);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800341 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800342 fstab->recs[cnt].fs_options = NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800343 }
344
345 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
346 ERROR("Error parsing fs_mgr_options\n");
Irina Tirdeae16d7472013-09-06 19:19:44 +0300347 goto err;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800348 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800349 fstab->recs[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags,
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700350 &flag_vals, NULL, 0);
351 fstab->recs[cnt].key_loc = flag_vals.key_loc;
352 fstab->recs[cnt].length = flag_vals.part_length;
353 fstab->recs[cnt].label = flag_vals.label;
354 fstab->recs[cnt].partnum = flag_vals.partnum;
355 fstab->recs[cnt].swap_prio = flag_vals.swap_prio;
356 fstab->recs[cnt].zram_size = flag_vals.zram_size;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800357 cnt++;
358 }
359 fclose(fstab_file);
Irina Tirdeae16d7472013-09-06 19:19:44 +0300360 free(line);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800361 return fstab;
Irina Tirdeae16d7472013-09-06 19:19:44 +0300362
363err:
Irina Tirdead431b8d2013-09-18 23:00:54 +0300364 fclose(fstab_file);
Irina Tirdeae16d7472013-09-06 19:19:44 +0300365 free(line);
Irina Tirdead431b8d2013-09-18 23:00:54 +0300366 if (fstab)
367 fs_mgr_free_fstab(fstab);
Irina Tirdeae16d7472013-09-06 19:19:44 +0300368 return NULL;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800369}
370
Ken Sumrallab6b8522013-02-13 12:58:40 -0800371void fs_mgr_free_fstab(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800372{
Ken Sumrallab6b8522013-02-13 12:58:40 -0800373 int i;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800374
Rom Lemarchand397a3642013-09-24 10:49:21 -0700375 if (!fstab) {
376 return;
377 }
378
Ken Sumrallab6b8522013-02-13 12:58:40 -0800379 for (i = 0; i < fstab->num_entries; i++) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800380 /* Free the pointers return by strdup(3) */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800381 free(fstab->recs[i].blk_device);
382 free(fstab->recs[i].mount_point);
383 free(fstab->recs[i].fs_type);
384 free(fstab->recs[i].fs_options);
385 free(fstab->recs[i].key_loc);
386 free(fstab->recs[i].label);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800387 }
388
Ken Sumrallab6b8522013-02-13 12:58:40 -0800389 /* Free the fstab_recs array created by calloc(3) */
390 free(fstab->recs);
391
392 /* Free the fstab filename */
393 free(fstab->fstab_filename);
394
395 /* Free fstab */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800396 free(fstab);
397}
398
Ken Sumrallab6b8522013-02-13 12:58:40 -0800399static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800400{
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800401 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700402 int ret;
403 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
404 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -0700405 char *e2fsck_argv[] = {
406 E2FSCK_BIN,
407 "-y",
408 blk_device
409 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800410
411 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800412 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700413 /*
414 * First try to mount and unmount the filesystem. We do this because
415 * the kernel is more efficient than e2fsck in running the journal and
416 * processing orphaned inodes, and on at least one device with a
417 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
418 * to do what the kernel does in about a second.
419 *
420 * After mounting and unmounting the filesystem, run e2fsck, and if an
421 * error is recorded in the filesystem superblock, e2fsck will do a full
422 * check. Otherwise, it does nothing. If the kernel cannot mount the
423 * filesytsem due to an error, e2fsck is still run to do a full check
424 * fix the filesystem.
425 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800426 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
427 if (!ret) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700428 umount(target);
429 }
430
Ken Sumrallab6b8522013-02-13 12:58:40 -0800431 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800432
Ken Sumrallbf021b42013-03-19 19:38:44 -0700433 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700434 &status, true, LOG_KLOG | LOG_FILE,
435 true, FSCK_LOG_FILE);
Ken Sumrallbf021b42013-03-19 19:38:44 -0700436
437 if (ret < 0) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800438 /* No need to check for error in fork, we can't really handle it now */
Ken Sumrallbf021b42013-03-19 19:38:44 -0700439 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800440 }
441 }
442
443 return;
444}
445
446static void remove_trailing_slashes(char *n)
447{
448 int len;
449
450 len = strlen(n) - 1;
451 while ((*(n + len) == '/') && len) {
452 *(n + len) = '\0';
453 len--;
454 }
455}
456
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700457/*
458 * Mark the given block device as read-only, using the BLKROSET ioctl.
459 * Return 0 on success, and -1 on error.
460 */
461static void fs_set_blk_ro(const char *blockdev)
462{
463 int fd;
464 int ON = 1;
465
466 fd = open(blockdev, O_RDONLY);
467 if (fd < 0) {
468 // should never happen
469 return;
470 }
471
472 ioctl(fd, BLKROSET, &ON);
473 close(fd);
474}
475
476/*
477 * __mount(): wrapper around the mount() system call which also
478 * sets the underlying block device to read-only if the mount is read-only.
479 * See "man 2 mount" for return values.
480 */
481static int __mount(const char *source, const char *target,
482 const char *filesystemtype, unsigned long mountflags,
483 const void *data)
484{
485 int ret = mount(source, target, filesystemtype, mountflags, data);
486
487 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
488 fs_set_blk_ro(source);
489 }
490
491 return ret;
492}
493
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800494static int fs_match(char *in1, char *in2)
495{
496 char *n1;
497 char *n2;
498 int ret;
499
500 n1 = strdup(in1);
501 n2 = strdup(in2);
502
503 remove_trailing_slashes(n1);
504 remove_trailing_slashes(n2);
505
506 ret = !strcmp(n1, n2);
507
508 free(n1);
509 free(n2);
510
511 return ret;
512}
513
Ken Sumrallab6b8522013-02-13 12:58:40 -0800514int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800515{
516 int i = 0;
517 int encrypted = 0;
518 int ret = -1;
519 int mret;
William Roberts071f28a2014-01-14 14:33:44 -0500520 int mount_errno;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800521
Ken Sumrallab6b8522013-02-13 12:58:40 -0800522 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800523 return ret;
524 }
525
Ken Sumrallab6b8522013-02-13 12:58:40 -0800526 for (i = 0; i < fstab->num_entries; i++) {
527 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800528 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800529 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800530 }
531
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700532 /* Skip swap and raw partition entries such as boot, recovery, etc */
533 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
534 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800535 !strcmp(fstab->recs[i].fs_type, "mtd")) {
536 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800537 }
538
Ken Sumrallab6b8522013-02-13 12:58:40 -0800539 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
540 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
541 }
542
543 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
544 check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
545 fstab->recs[i].mount_point);
546 }
547
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800548 if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) {
549 if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) {
550 ERROR("Could not set up verified partition, skipping!");
551 continue;
552 }
553 }
554
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700555 mret = __mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point,
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800556 fstab->recs[i].fs_type, fstab->recs[i].flags,
557 fstab->recs[i].fs_options);
558
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800559 if (!mret) {
560 /* Success! Go get the next one */
561 continue;
562 }
563
William Roberts071f28a2014-01-14 14:33:44 -0500564 /* back up errno as partition_wipe clobbers the value */
565 mount_errno = errno;
566
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800567 /* mount(2) returned an error, check if it's encrypted and deal with it */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800568 if ((fstab->recs[i].fs_mgr_flags & MF_CRYPT) &&
569 !partition_wiped(fstab->recs[i].blk_device)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800570 /* Need to mount a tmpfs at this mountpoint for now, and set
571 * properties that vold will query later for decrypting
572 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800573 if (mount("tmpfs", fstab->recs[i].mount_point, "tmpfs",
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800574 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
William Roberts071f28a2014-01-14 14:33:44 -0500575 ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s error: %s\n",
576 fstab->recs[i].mount_point, strerror(errno));
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800577 goto out;
578 }
579 encrypted = 1;
580 } else {
William Roberts071f28a2014-01-14 14:33:44 -0500581 ERROR("Failed to mount an un-encryptable or wiped partition on"
582 "%s at %s options: %s error: %s\n",
583 fstab->recs[i].blk_device, fstab->recs[i].mount_point,
584 fstab->recs[i].fs_options, strerror(mount_errno));
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800585 goto out;
586 }
587 }
588
589 if (encrypted) {
590 ret = 1;
591 } else {
592 ret = 0;
593 }
594
595out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800596 return ret;
597}
598
Ken Sumrallab6b8522013-02-13 12:58:40 -0800599/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800600 * tmp mount we do to check the user password
601 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800602int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
603 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800604{
605 int i = 0;
606 int ret = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800607 char *m;
608
Ken Sumrallab6b8522013-02-13 12:58:40 -0800609 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800610 return ret;
611 }
612
Ken Sumrallab6b8522013-02-13 12:58:40 -0800613 for (i = 0; i < fstab->num_entries; i++) {
614 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800615 continue;
616 }
617
618 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700619 /* If this swap or a raw partition, report an error */
620 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
621 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800622 !strcmp(fstab->recs[i].fs_type, "mtd")) {
623 ERROR("Cannot mount filesystem of type %s on %s\n",
624 fstab->recs[i].fs_type, n_blk_device);
625 goto out;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800626 }
627
Ken Sumrallab6b8522013-02-13 12:58:40 -0800628 /* First check the filesystem if requested */
629 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
630 wait_for_file(n_blk_device, WAIT_TIMEOUT);
631 }
632
633 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
634 check_fs(n_blk_device, fstab->recs[i].fs_type,
635 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800636 }
637
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800638 if (fstab->recs[i].fs_mgr_flags & MF_VERIFY) {
639 if (fs_mgr_setup_verity(&fstab->recs[i]) < 0) {
640 ERROR("Could not set up verified partition, skipping!");
641 continue;
642 }
643 }
644
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800645 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800646 if (tmp_mount_point) {
647 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800648 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800649 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800650 }
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700651 if (__mount(n_blk_device, m, fstab->recs[i].fs_type,
652 fstab->recs[i].flags, fstab->recs[i].fs_options)) {
William Roberts071f28a2014-01-14 14:33:44 -0500653 ERROR("Cannot mount filesystem on %s at %s options: %s error: %s\n",
654 n_blk_device, m, fstab->recs[i].fs_options, strerror(errno));
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800655 goto out;
656 } else {
657 ret = 0;
658 goto out;
659 }
660 }
661
662 /* We didn't find a match, say so and return an error */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800663 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800664
665out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800666 return ret;
667}
668
669/*
670 * mount a tmpfs filesystem at the given point.
671 * return 0 on success, non-zero on failure.
672 */
673int fs_mgr_do_tmpfs_mount(char *n_name)
674{
675 int ret;
676
677 ret = mount("tmpfs", n_name, "tmpfs",
678 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
679 if (ret < 0) {
680 ERROR("Cannot mount tmpfs filesystem at %s\n", n_name);
681 return -1;
682 }
683
684 /* Success */
685 return 0;
686}
687
Ken Sumrallab6b8522013-02-13 12:58:40 -0800688int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800689{
690 int i = 0;
691 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800692
Ken Sumrallab6b8522013-02-13 12:58:40 -0800693 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800694 return -1;
695 }
696
Ken Sumrallab6b8522013-02-13 12:58:40 -0800697 while (fstab->recs[i].blk_device) {
698 if (umount(fstab->recs[i].mount_point)) {
699 ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800700 ret = -1;
701 }
702 i++;
703 }
704
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800705 return ret;
706}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700707
708/* This must be called after mount_all, because the mkswap command needs to be
709 * available.
710 */
711int fs_mgr_swapon_all(struct fstab *fstab)
712{
713 int i = 0;
714 int flags = 0;
715 int err = 0;
716 int ret = 0;
717 int status;
718 char *mkswap_argv[2] = {
719 MKSWAP_BIN,
720 NULL
721 };
722
723 if (!fstab) {
724 return -1;
725 }
726
727 for (i = 0; i < fstab->num_entries; i++) {
728 /* Skip non-swap entries */
729 if (strcmp(fstab->recs[i].fs_type, "swap")) {
730 continue;
731 }
732
733 if (fstab->recs[i].zram_size > 0) {
734 /* A zram_size was specified, so we need to configure the
735 * device. There is no point in having multiple zram devices
736 * on a system (all the memory comes from the same pool) so
737 * we can assume the device number is 0.
738 */
739 FILE *zram_fp;
740
741 zram_fp = fopen(ZRAM_CONF_DEV, "r+");
742 if (zram_fp == NULL) {
743 ERROR("Unable to open zram conf device " ZRAM_CONF_DEV);
744 ret = -1;
745 continue;
746 }
747 fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size);
748 fclose(zram_fp);
749 }
750
751 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
752 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
753 }
754
755 /* Initialize the swap area */
756 mkswap_argv[1] = fstab->recs[i].blk_device;
757 err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv,
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700758 &status, true, LOG_KLOG, false, NULL);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700759 if (err) {
760 ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device);
761 ret = -1;
762 continue;
763 }
764
765 /* If -1, then no priority was specified in fstab, so don't set
766 * SWAP_FLAG_PREFER or encode the priority */
767 if (fstab->recs[i].swap_prio >= 0) {
768 flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) &
769 SWAP_FLAG_PRIO_MASK;
770 flags |= SWAP_FLAG_PREFER;
771 } else {
772 flags = 0;
773 }
774 err = swapon(fstab->recs[i].blk_device, flags);
775 if (err) {
776 ERROR("swapon failed for %s\n", fstab->recs[i].blk_device);
777 ret = -1;
778 }
779 }
780
781 return ret;
782}
783
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800784/*
785 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
786 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800787 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800788 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800789int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800790{
791 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800792
Ken Sumrallab6b8522013-02-13 12:58:40 -0800793 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800794 return -1;
795 }
796 /* Initialize return values to null strings */
797 if (key_loc) {
798 *key_loc = '\0';
799 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800800 if (real_blk_device) {
801 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800802 }
803
804 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800805 for (i = 0; i < fstab->num_entries; i++) {
806 /* Don't deal with vold managed enryptable partitions here */
807 if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) {
808 continue;
809 }
810 if (!(fstab->recs[i].fs_mgr_flags & MF_CRYPT)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800811 continue;
812 }
813
814 /* We found a match */
815 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800816 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800817 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800818 if (real_blk_device) {
819 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800820 }
821 break;
822 }
823
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800824 return 0;
825}
826
Ken Sumrallab6b8522013-02-13 12:58:40 -0800827/* Add an entry to the fstab, and return 0 on success or -1 on error */
828int fs_mgr_add_entry(struct fstab *fstab,
829 const char *mount_point, const char *fs_type,
Nick Kralevichbc777672014-01-15 06:18:35 +0000830 const char *blk_device, long long length)
Ken Sumrallab6b8522013-02-13 12:58:40 -0800831{
832 struct fstab_rec *new_fstab_recs;
833 int n = fstab->num_entries;
834
835 new_fstab_recs = (struct fstab_rec *)
836 realloc(fstab->recs, sizeof(struct fstab_rec) * (n + 1));
837
838 if (!new_fstab_recs) {
839 return -1;
840 }
841
842 /* A new entry was added, so initialize it */
843 memset(&new_fstab_recs[n], 0, sizeof(struct fstab_rec));
844 new_fstab_recs[n].mount_point = strdup(mount_point);
845 new_fstab_recs[n].fs_type = strdup(fs_type);
846 new_fstab_recs[n].blk_device = strdup(blk_device);
847 new_fstab_recs[n].length = 0;
848
849 /* Update the fstab struct */
850 fstab->recs = new_fstab_recs;
851 fstab->num_entries++;
852
853 return 0;
854}
855
856struct fstab_rec *fs_mgr_get_entry_for_mount_point(struct fstab *fstab, const char *path)
857{
858 int i;
859
860 if (!fstab) {
861 return NULL;
862 }
863
864 for (i = 0; i < fstab->num_entries; i++) {
865 int len = strlen(fstab->recs[i].mount_point);
866 if (strncmp(path, fstab->recs[i].mount_point, len) == 0 &&
867 (path[len] == '\0' || path[len] == '/')) {
868 return &fstab->recs[i];
869 }
870 }
871
872 return NULL;
873}
874
875int fs_mgr_is_voldmanaged(struct fstab_rec *fstab)
876{
877 return fstab->fs_mgr_flags & MF_VOLDMANAGED;
878}
879
880int fs_mgr_is_nonremovable(struct fstab_rec *fstab)
881{
882 return fstab->fs_mgr_flags & MF_NONREMOVABLE;
883}
884
885int fs_mgr_is_encryptable(struct fstab_rec *fstab)
886{
887 return fstab->fs_mgr_flags & MF_CRYPT;
888}
Ken Sumrall887f2892013-09-11 19:42:51 -0700889
890int fs_mgr_is_noemulatedsd(struct fstab_rec *fstab)
891{
892 return fstab->fs_mgr_flags & MF_NOEMULATEDSD;
893}