blob: 9288177e7fb4230ce67678e7f4fde131532b8bfd [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 -080033#include "wipe.h"
34#include "cryptfs.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080036static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037
Kenny Root41dda822012-03-30 20:48:34 -070038extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050039
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040void load_volume_table()
41{
42 int i;
43 int ret;
Doug Zongker2810ced2011-02-17 15:55:21 -080044
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080045 fstab = fs_mgr_read_fstab("/etc/recovery.fstab");
46 if (!fstab) {
47 LOGE("failed to read /etc/recovery.fstab\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070048 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070050
Sasha Levitskiy85ef47d2014-04-10 17:11:34 -070051 ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080052 if (ret < 0 ) {
53 LOGE("failed to add /tmp entry to fstab\n");
54 fs_mgr_free_fstab(fstab);
55 fstab = NULL;
56 return;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080057 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070058
Doug Zongkerd4208f92010-09-20 12:16:13 -070059 printf("recovery filesystem table\n");
60 printf("=========================\n");
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080061 for (i = 0; i < fstab->num_entries; ++i) {
62 Volume* v = &fstab->recs[i];
63 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
64 v->blk_device, v->length);
Doug Zongkerd4208f92010-09-20 12:16:13 -070065 }
66 printf("\n");
67}
68
69Volume* volume_for_path(const char* path) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080070 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071}
72
Doug Zongkerd4208f92010-09-20 12:16:13 -070073int ensure_path_mounted(const char* path) {
74 Volume* v = volume_for_path(path);
75 if (v == NULL) {
76 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077 return -1;
78 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070079 if (strcmp(v->fs_type, "ramdisk") == 0) {
80 // the ramdisk is always mounted.
81 return 0;
82 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070083
84 int result;
85 result = scan_mounted_volumes();
86 if (result < 0) {
87 LOGE("failed to scan mounted volumes\n");
88 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070089 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090
Doug Zongkerd4208f92010-09-20 12:16:13 -070091 const MountedVolume* mv =
92 find_mounted_volume_by_mount_point(v->mount_point);
93 if (mv) {
94 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 return 0;
96 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070097
98 mkdir(v->mount_point, 0755); // in case it doesn't already exist
99
100 if (strcmp(v->fs_type, "yaffs2") == 0) {
101 // mount an MTD partition as a YAFFS2 filesystem.
102 mtd_scan_partitions();
103 const MtdPartition* partition;
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800104 partition = mtd_find_partition_by_name(v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700105 if (partition == NULL) {
106 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800107 v->blk_device, v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700108 return -1;
109 }
110 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
111 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash0ddfa322015-06-29 18:57:14 -0700112 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 strcmp(v->fs_type, "vfat") == 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800114 result = mount(v->blk_device, v->mount_point, v->fs_type,
Gaelle Nassiete853e962015-03-23 17:51:53 +0100115 v->flags, v->fs_options);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 if (result == 0) return 0;
117
Doug Zongkerd4208f92010-09-20 12:16:13 -0700118 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
119 return -1;
120 }
121
122 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800123 return -1;
124}
125
Doug Zongkerd4208f92010-09-20 12:16:13 -0700126int ensure_path_unmounted(const char* path) {
127 Volume* v = volume_for_path(path);
128 if (v == NULL) {
129 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 return -1;
131 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700132 if (strcmp(v->fs_type, "ramdisk") == 0) {
133 // the ramdisk is always mounted; you can't unmount it.
134 return -1;
135 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136
Doug Zongkerd4208f92010-09-20 12:16:13 -0700137 int result;
138 result = scan_mounted_volumes();
139 if (result < 0) {
140 LOGE("failed to scan mounted volumes\n");
141 return -1;
142 }
143
144 const MountedVolume* mv =
145 find_mounted_volume_by_mount_point(v->mount_point);
146 if (mv == NULL) {
147 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 return 0;
149 }
150
Doug Zongkerd4208f92010-09-20 12:16:13 -0700151 return unmount_mounted_volume(mv);
152}
153
JP Abgrall37aedb32014-06-16 19:07:39 -0700154static int exec_cmd(const char* path, char* const argv[]) {
155 int status;
156 pid_t child;
157 if ((child = vfork()) == 0) {
158 execv(path, argv);
159 _exit(-1);
160 }
161 waitpid(child, &status, 0);
162 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
163 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
164 }
165 return WEXITSTATUS(status);
166}
167
Doug Zongkerd4208f92010-09-20 12:16:13 -0700168int format_volume(const char* volume) {
169 Volume* v = volume_for_path(volume);
170 if (v == NULL) {
171 LOGE("unknown volume \"%s\"\n", volume);
172 return -1;
173 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700174 if (strcmp(v->fs_type, "ramdisk") == 0) {
175 // you can't format the ramdisk.
176 LOGE("can't format_volume \"%s\"", volume);
177 return -1;
178 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700179 if (strcmp(v->mount_point, volume) != 0) {
180 LOGE("can't give path \"%s\" to format_volume\n", volume);
181 return -1;
182 }
183
184 if (ensure_path_unmounted(volume) != 0) {
185 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
186 return -1;
187 }
188
189 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800191 const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800192 if (partition == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800193 LOGE("format_volume: no MTD partition \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194 return -1;
195 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800196
Doug Zongkerd4208f92010-09-20 12:16:13 -0700197 MtdWriteContext *write = mtd_write_partition(partition);
198 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800199 LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700201 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800202 LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700203 mtd_write_close(write);
204 return -1;
205 } else if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800206 LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800207 return -1;
208 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800209 return 0;
210 }
211
JP Abgrall37aedb32014-06-16 19:07:39 -0700212 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800213 // if there's a key_loc that looks like a path, it should be a
214 // block device for storing encryption metadata. wipe it too.
215 if (v->key_loc != NULL && v->key_loc[0] == '/') {
216 LOGI("wiping %s\n", v->key_loc);
217 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
218 if (fd < 0) {
219 LOGE("format_volume: failed to open %s\n", v->key_loc);
220 return -1;
221 }
222 wipe_block_device(fd, get_file_size(fd));
223 close(fd);
224 }
225
JP Abgrall37aedb32014-06-16 19:07:39 -0700226 ssize_t length = 0;
227 if (v->length != 0) {
228 length = v->length;
229 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
230 length = -CRYPT_FOOTER_OFFSET;
231 }
232 int result;
233 if (strcmp(v->fs_type, "ext4") == 0) {
234 result = make_ext4fs(v->blk_device, length, volume, sehandle);
235 } else { /* Has to be f2fs because we checked earlier. */
236 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700237 LOGE("format_volume: crypt footer + negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700238 return -1;
239 }
240 if (length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700241 LOGE("format_volume: negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700242 return -1;
243 }
244 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700245 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700246 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
247 return -1;
248 }
249 const char *f2fs_path = "/sbin/mkfs.f2fs";
250 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
251
252 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
253 free(num_sectors);
254 }
255 if (result != 0) {
256 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
257 return -1;
258 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700259 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800260 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700261
262 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800263 return -1;
264}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700265
266int setup_install_mounts() {
267 if (fstab == NULL) {
268 LOGE("can't set up install mounts: no fstab loaded\n");
269 return -1;
270 }
271 for (int i = 0; i < fstab->num_entries; ++i) {
272 Volume* v = fstab->recs + i;
273
274 if (strcmp(v->mount_point, "/tmp") == 0 ||
275 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800276 if (ensure_path_mounted(v->mount_point) != 0) {
277 LOGE("failed to mount %s\n", v->mount_point);
278 return -1;
279 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700280
281 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800282 if (ensure_path_unmounted(v->mount_point) != 0) {
283 LOGE("failed to unmount %s\n", v->mount_point);
284 return -1;
285 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700286 }
287 }
288 return 0;
289}