blob: 61798f4957b3aeb0229b29bd21a30fd67d308e12 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 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
17#include <errno.h>
18#include <stdlib.h>
19#include <sys/mount.h>
20#include <sys/stat.h>
21#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070022#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <unistd.h>
Doug Zongkerd4208f92010-09-20 12:16:13 -070024#include <ctype.h>
Doug Zongkerf39989a2013-12-11 15:40:28 -080025#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080027#include <fs_mgr.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080028#include "mtdutils/mtdutils.h"
29#include "mtdutils/mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include "roots.h"
31#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070032#include "make_ext4fs.h"
Doug Zongkerf39989a2013-12-11 15:40:28 -080033extern "C" {
34#include "wipe.h"
35#include "cryptfs.h"
36}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080038static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Kenny Root41dda822012-03-30 20:48:34 -070040extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050041
Andres Moralesee193872014-08-05 19:49:09 -070042static const char* PERSISTENT_PATH = "/persistent";
43
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080044void load_volume_table()
45{
46 int i;
47 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080048
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080049 fstab = fs_mgr_read_fstab("/etc/recovery.fstab");
50 if (!fstab) {
51 LOGE("failed to read /etc/recovery.fstab\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070052 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070054
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070055 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080056 if (ret < 0 ) {
57 LOGE("failed to add /tmp entry to fstab\n");
58 fs_mgr_free_fstab(fstab);
59 fstab = NULL;
60 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070062
Doug Zongkerd4208f92010-09-20 12:16:13 -070063 printf("recovery filesystem table\n");
64 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080065 for (i = 0; i < fstab->num_entries; ++i) {
66 Volume* v = &fstab->recs[i];
67 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
68 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070069 }
70 printf("\n");
71}
72
73Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080074 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075}
76
Doug Zongkerd4208f92010-09-20 12:16:13 -070077int ensure_path_mounted(const char* path) {
78 Volume* v = volume_for_path(path);
79 if (v == NULL) {
80 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 return -1;
82 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070083 if (strcmp(v->fs_type, "ramdisk") == 0) {
84 // the ramdisk is always mounted.
85 return 0;
86 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070087
88 int result;
89 result = scan_mounted_volumes();
90 if (result < 0) {
91 LOGE("failed to scan mounted volumes\n");
92 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070093 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080094
Doug Zongkerd4208f92010-09-20 12:16:13 -070095 const MountedVolume* mv =
96 find_mounted_volume_by_mount_point(v->mount_point);
97 if (mv) {
98 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 return 0;
100 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101
102 mkdir(v->mount_point, 0755); // in case it doesn't already exist
103
104 if (strcmp(v->fs_type, "yaffs2") == 0) {
105 // mount an MTD partition as a YAFFS2 filesystem.
106 mtd_scan_partitions();
107 const MtdPartition* partition;
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800108 partition = mtd_find_partition_by_name(v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700109 if (partition == NULL) {
110 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800111 v->blk_device, v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700112 return -1;
113 }
114 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
115 } else if (strcmp(v->fs_type, "ext4") == 0 ||
116 strcmp(v->fs_type, "vfat") == 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800117 result = mount(v->blk_device, v->mount_point, v->fs_type,
Doug Zongker469243e2011-04-12 09:28:10 -0700118 MS_NOATIME | MS_NODEV | MS_NODIRATIME, "");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700119 if (result == 0) return 0;
120
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
122 return -1;
123 }
124
125 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126 return -1;
127}
128
Doug Zongkerd4208f92010-09-20 12:16:13 -0700129int ensure_path_unmounted(const char* path) {
130 Volume* v = volume_for_path(path);
131 if (v == NULL) {
132 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 return -1;
134 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700135 if (strcmp(v->fs_type, "ramdisk") == 0) {
136 // the ramdisk is always mounted; you can't unmount it.
137 return -1;
138 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800139
Doug Zongkerd4208f92010-09-20 12:16:13 -0700140 int result;
141 result = scan_mounted_volumes();
142 if (result < 0) {
143 LOGE("failed to scan mounted volumes\n");
144 return -1;
145 }
146
147 const MountedVolume* mv =
148 find_mounted_volume_by_mount_point(v->mount_point);
149 if (mv == NULL) {
150 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151 return 0;
152 }
153
Doug Zongkerd4208f92010-09-20 12:16:13 -0700154 return unmount_mounted_volume(mv);
155}
156
JP Abgrall37aedb32014-06-16 19:07:39 -0700157static int exec_cmd(const char* path, char* const argv[]) {
158 int status;
159 pid_t child;
160 if ((child = vfork()) == 0) {
161 execv(path, argv);
162 _exit(-1);
163 }
164 waitpid(child, &status, 0);
165 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
166 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
167 }
168 return WEXITSTATUS(status);
169}
170
Doug Zongkerd4208f92010-09-20 12:16:13 -0700171int format_volume(const char* volume) {
172 Volume* v = volume_for_path(volume);
173 if (v == NULL) {
174 LOGE("unknown volume \"%s\"\n", volume);
175 return -1;
176 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700177 if (strcmp(v->fs_type, "ramdisk") == 0) {
178 // you can't format the ramdisk.
179 LOGE("can't format_volume \"%s\"", volume);
180 return -1;
181 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700182 if (strcmp(v->mount_point, volume) != 0) {
183 LOGE("can't give path \"%s\" to format_volume\n", volume);
184 return -1;
185 }
186
187 if (ensure_path_unmounted(volume) != 0) {
188 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
189 return -1;
190 }
191
192 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800194 const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800195 if (partition == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800196 LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 return -1;
198 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800199
Doug Zongkerd4208f92010-09-20 12:16:13 -0700200 MtdWriteContext *write = mtd_write_partition(partition);
201 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800202 LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700204 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800205 LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700206 mtd_write_close(write);
207 return -1;
208 } else if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800209 LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210 return -1;
211 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 return 0;
213 }
214
JP Abgrall37aedb32014-06-16 19:07:39 -0700215 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800216 // if there's a key_loc that looks like a path, it should be a
217 // block device for storing encryption metadata. wipe it too.
218 if (v->key_loc != NULL && v->key_loc[0] == '/') {
219 LOGI("wiping %s\n", v->key_loc);
220 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
221 if (fd < 0) {
222 LOGE("format_volume: failed to open %s\n", v->key_loc);
223 return -1;
224 }
225 wipe_block_device(fd, get_file_size(fd));
226 close(fd);
227 }
228
JP Abgrall37aedb32014-06-16 19:07:39 -0700229 ssize_t length = 0;
230 if (v->length != 0) {
231 length = v->length;
232 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
233 length = -CRYPT_FOOTER_OFFSET;
234 }
235 int result;
236 if (strcmp(v->fs_type, "ext4") == 0) {
237 result = make_ext4fs(v->blk_device, length, volume, sehandle);
238 } else { /* Has to be f2fs because we checked earlier. */
239 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
240 LOGE("format_volume: crypt footer + negative length (%lld) not supported on %s\n", v->fs_type, length);
241 return -1;
242 }
243 if (length < 0) {
244 LOGE("format_volume: negative length (%ld) not supported on %s\n", length, v->fs_type);
245 return -1;
246 }
247 char *num_sectors;
248 if (asprintf(&num_sectors, "%ld", length / 512) <= 0) {
249 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
250 return -1;
251 }
252 const char *f2fs_path = "/sbin/mkfs.f2fs";
253 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
254
255 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
256 free(num_sectors);
257 }
258 if (result != 0) {
259 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
260 return -1;
261 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700262 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700264
265 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800266 return -1;
267}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700268
Andres Moralesee193872014-08-05 19:49:09 -0700269int erase_persistent_partition() {
270 Volume *v = volume_for_path(PERSISTENT_PATH);
271 if (v == NULL) {
272 // most devices won't have /persistent, so this is not an error.
273 return 0;
274 }
275
276 int fd = open(v->blk_device, O_RDWR);
277 uint64_t size = get_file_size(fd);
278 if (size == 0) {
279 LOGE("failed to stat size of /persistent\n");
280 close(fd);
281 return -1;
282 }
283
284 char oem_unlock_enabled;
285 lseek(fd, size - 1, SEEK_SET);
286 read(fd, &oem_unlock_enabled, 1);
287
288 if (oem_unlock_enabled) {
289 if (wipe_block_device(fd, size)) {
290 LOGE("error wiping /persistent: %s\n", strerror(errno));
291 close(fd);
292 return -1;
293 }
294
295 lseek(fd, size - 1, SEEK_SET);
296 write(fd, &oem_unlock_enabled, 1);
297 }
298
299 close(fd);
300
301 return (int) oem_unlock_enabled;
302}
303
Doug Zongker239ac6a2013-08-20 16:03:25 -0700304int setup_install_mounts() {
305 if (fstab == NULL) {
306 LOGE("can't set up install mounts: no fstab loaded\n");
307 return -1;
308 }
309 for (int i = 0; i < fstab->num_entries; ++i) {
310 Volume* v = fstab->recs + i;
311
312 if (strcmp(v->mount_point, "/tmp") == 0 ||
313 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800314 if (ensure_path_mounted(v->mount_point) != 0) {
315 LOGE("failed to mount %s\n", v->mount_point);
316 return -1;
317 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700318
319 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800320 if (ensure_path_unmounted(v->mount_point) != 0) {
321 LOGE("failed to unmount %s\n", v->mount_point);
322 return -1;
323 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700324 }
325 }
326 return 0;
327}