blob: 2bd457efe366f5163c471204ae1e8a2f74d49b50 [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
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080042void load_volume_table()
43{
44 int i;
45 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080046
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080047 fstab = fs_mgr_read_fstab("/etc/recovery.fstab");
48 if (!fstab) {
49 LOGE("failed to read /etc/recovery.fstab\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070050 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070052
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070053 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080054 if (ret < 0 ) {
55 LOGE("failed to add /tmp entry to fstab\n");
56 fs_mgr_free_fstab(fstab);
57 fstab = NULL;
58 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070060
Doug Zongkerd4208f92010-09-20 12:16:13 -070061 printf("recovery filesystem table\n");
62 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080063 for (i = 0; i < fstab->num_entries; ++i) {
64 Volume* v = &fstab->recs[i];
65 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
66 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070067 }
68 printf("\n");
69}
70
71Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080072 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073}
74
Doug Zongkerd4208f92010-09-20 12:16:13 -070075int ensure_path_mounted(const char* path) {
76 Volume* v = volume_for_path(path);
77 if (v == NULL) {
78 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079 return -1;
80 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070081 if (strcmp(v->fs_type, "ramdisk") == 0) {
82 // the ramdisk is always mounted.
83 return 0;
84 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070085
86 int result;
87 result = scan_mounted_volumes();
88 if (result < 0) {
89 LOGE("failed to scan mounted volumes\n");
90 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070091 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092
Doug Zongkerd4208f92010-09-20 12:16:13 -070093 const MountedVolume* mv =
94 find_mounted_volume_by_mount_point(v->mount_point);
95 if (mv) {
96 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 return 0;
98 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070099
100 mkdir(v->mount_point, 0755); // in case it doesn't already exist
101
102 if (strcmp(v->fs_type, "yaffs2") == 0) {
103 // mount an MTD partition as a YAFFS2 filesystem.
104 mtd_scan_partitions();
105 const MtdPartition* partition;
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800106 partition = mtd_find_partition_by_name(v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700107 if (partition == NULL) {
108 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800109 v->blk_device, v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700110 return -1;
111 }
112 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
113 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash0ddfa322015-06-29 18:57:14 -0700114 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700115 strcmp(v->fs_type, "vfat") == 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800116 result = mount(v->blk_device, v->mount_point, v->fs_type,
Gaelle Nassiete853e962015-03-23 17:51:53 +0100117 v->flags, v->fs_options);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700118 if (result == 0) return 0;
119
Doug Zongkerd4208f92010-09-20 12:16:13 -0700120 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
121 return -1;
122 }
123
124 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 return -1;
126}
127
Doug Zongkerd4208f92010-09-20 12:16:13 -0700128int ensure_path_unmounted(const char* path) {
129 Volume* v = volume_for_path(path);
130 if (v == NULL) {
131 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800132 return -1;
133 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700134 if (strcmp(v->fs_type, "ramdisk") == 0) {
135 // the ramdisk is always mounted; you can't unmount it.
136 return -1;
137 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139 int result;
140 result = scan_mounted_volumes();
141 if (result < 0) {
142 LOGE("failed to scan mounted volumes\n");
143 return -1;
144 }
145
146 const MountedVolume* mv =
147 find_mounted_volume_by_mount_point(v->mount_point);
148 if (mv == NULL) {
149 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150 return 0;
151 }
152
Doug Zongkerd4208f92010-09-20 12:16:13 -0700153 return unmount_mounted_volume(mv);
154}
155
JP Abgrall37aedb32014-06-16 19:07:39 -0700156static int exec_cmd(const char* path, char* const argv[]) {
157 int status;
158 pid_t child;
159 if ((child = vfork()) == 0) {
160 execv(path, argv);
161 _exit(-1);
162 }
163 waitpid(child, &status, 0);
164 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
165 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
166 }
167 return WEXITSTATUS(status);
168}
169
Doug Zongkerd4208f92010-09-20 12:16:13 -0700170int format_volume(const char* volume) {
171 Volume* v = volume_for_path(volume);
172 if (v == NULL) {
173 LOGE("unknown volume \"%s\"\n", volume);
174 return -1;
175 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700176 if (strcmp(v->fs_type, "ramdisk") == 0) {
177 // you can't format the ramdisk.
178 LOGE("can't format_volume \"%s\"", volume);
179 return -1;
180 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181 if (strcmp(v->mount_point, volume) != 0) {
182 LOGE("can't give path \"%s\" to format_volume\n", volume);
183 return -1;
184 }
185
186 if (ensure_path_unmounted(volume) != 0) {
187 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
188 return -1;
189 }
190
191 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800193 const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194 if (partition == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800195 LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800196 return -1;
197 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198
Doug Zongkerd4208f92010-09-20 12:16:13 -0700199 MtdWriteContext *write = mtd_write_partition(partition);
200 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800201 LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700203 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800204 LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700205 mtd_write_close(write);
206 return -1;
207 } else if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800208 LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 return -1;
210 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800211 return 0;
212 }
213
JP Abgrall37aedb32014-06-16 19:07:39 -0700214 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800215 // if there's a key_loc that looks like a path, it should be a
216 // block device for storing encryption metadata. wipe it too.
217 if (v->key_loc != NULL && v->key_loc[0] == '/') {
218 LOGI("wiping %s\n", v->key_loc);
219 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
220 if (fd < 0) {
221 LOGE("format_volume: failed to open %s\n", v->key_loc);
222 return -1;
223 }
224 wipe_block_device(fd, get_file_size(fd));
225 close(fd);
226 }
227
JP Abgrall37aedb32014-06-16 19:07:39 -0700228 ssize_t length = 0;
229 if (v->length != 0) {
230 length = v->length;
231 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
232 length = -CRYPT_FOOTER_OFFSET;
233 }
234 int result;
235 if (strcmp(v->fs_type, "ext4") == 0) {
236 result = make_ext4fs(v->blk_device, length, volume, sehandle);
237 } else { /* Has to be f2fs because we checked earlier. */
238 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700239 LOGE("format_volume: crypt footer + negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700240 return -1;
241 }
242 if (length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700243 LOGE("format_volume: negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700244 return -1;
245 }
246 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700247 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700248 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
249 return -1;
250 }
251 const char *f2fs_path = "/sbin/mkfs.f2fs";
252 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
253
254 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
255 free(num_sectors);
256 }
257 if (result != 0) {
258 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
259 return -1;
260 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700261 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800262 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700263
264 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800265 return -1;
266}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700267
268int setup_install_mounts() {
269 if (fstab == NULL) {
270 LOGE("can't set up install mounts: no fstab loaded\n");
271 return -1;
272 }
273 for (int i = 0; i < fstab->num_entries; ++i) {
274 Volume* v = fstab->recs + i;
275
276 if (strcmp(v->mount_point, "/tmp") == 0 ||
277 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800278 if (ensure_path_mounted(v->mount_point) != 0) {
279 LOGE("failed to mount %s\n", v->mount_point);
280 return -1;
281 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700282
283 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800284 if (ensure_path_unmounted(v->mount_point) != 0) {
285 LOGE("failed to unmount %s\n", v->mount_point);
286 return -1;
287 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700288 }
289 }
290 return 0;
291}