blob: 167499e2cb45ceb23d986b6f57c7fe2be311b6b9 [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
Dees_Troy51a0e822012-09-05 15:24:24 -040027extern "C" {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080028#include <fs_mgr.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include "mtdutils/mtdutils.h"
30#include "mtdutils/mounts.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040031}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include "roots.h"
33#include "common.h"
Doug Zongkerd4208f92010-09-20 12:16:13 -070034#include "make_ext4fs.h"
Doug Zongkerf39989a2013-12-11 15:40:28 -080035extern "C" {
36#include "wipe.h"
37#include "cryptfs.h"
38}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080039
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080040static struct fstab *fstab = NULL;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041
Kenny Root41dda822012-03-30 20:48:34 -070042extern struct selabel_handle *sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050043
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) {
Dees_Troyc51f1f92012-09-20 15:32:13 -040078 if (PartitionManager.Mount_By_Path(path, true))
79 return 0;
80 else
81 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -070082 Volume* v = volume_for_path(path);
83 if (v == NULL) {
84 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 return -1;
86 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -070087 if (strcmp(v->fs_type, "ramdisk") == 0) {
88 // the ramdisk is always mounted.
89 return 0;
90 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070091
92 int result;
93 result = scan_mounted_volumes();
94 if (result < 0) {
95 LOGE("failed to scan mounted volumes\n");
96 return -1;
Doug Zongker23ceeea2010-07-08 17:27:55 -070097 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098
Doug Zongkerd4208f92010-09-20 12:16:13 -070099 const MountedVolume* mv =
100 find_mounted_volume_by_mount_point(v->mount_point);
101 if (mv) {
102 // volume is already mounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 return 0;
104 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700105
106 mkdir(v->mount_point, 0755); // in case it doesn't already exist
107
108 if (strcmp(v->fs_type, "yaffs2") == 0) {
109 // mount an MTD partition as a YAFFS2 filesystem.
110 mtd_scan_partitions();
111 const MtdPartition* partition;
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800112 partition = mtd_find_partition_by_name(v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700113 if (partition == NULL) {
114 LOGE("failed to find \"%s\" partition to mount at \"%s\"\n",
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800115 v->blk_device, v->mount_point);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 return -1;
117 }
118 return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0);
119 } else if (strcmp(v->fs_type, "ext4") == 0 ||
Mohamad Ayyash0ddfa322015-06-29 18:57:14 -0700120 strcmp(v->fs_type, "squashfs") == 0 ||
Doug Zongkerd4208f92010-09-20 12:16:13 -0700121 strcmp(v->fs_type, "vfat") == 0) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800122 result = mount(v->blk_device, v->mount_point, v->fs_type,
Gaelle Nassiete853e962015-03-23 17:51:53 +0100123 v->flags, v->fs_options);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700124 if (result == 0) return 0;
125
Doug Zongkerd4208f92010-09-20 12:16:13 -0700126 LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno));
127 return -1;
128 }
129
130 LOGE("unknown fs_type \"%s\" for %s\n", v->fs_type, v->mount_point);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131 return -1;
132}
133
Doug Zongkerd4208f92010-09-20 12:16:13 -0700134int ensure_path_unmounted(const char* path) {
Dees_Troyc51f1f92012-09-20 15:32:13 -0400135 if (PartitionManager.UnMount_By_Path(path, true))
136 return 0;
137 else
138 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139 Volume* v = volume_for_path(path);
140 if (v == NULL) {
141 LOGE("unknown volume for path [%s]\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142 return -1;
143 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700144 if (strcmp(v->fs_type, "ramdisk") == 0) {
145 // the ramdisk is always mounted; you can't unmount it.
146 return -1;
147 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148
Doug Zongkerd4208f92010-09-20 12:16:13 -0700149 int result;
150 result = scan_mounted_volumes();
151 if (result < 0) {
152 LOGE("failed to scan mounted volumes\n");
153 return -1;
154 }
155
156 const MountedVolume* mv =
157 find_mounted_volume_by_mount_point(v->mount_point);
158 if (mv == NULL) {
159 // volume is already unmounted
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800160 return 0;
161 }
162
Doug Zongkerd4208f92010-09-20 12:16:13 -0700163 return unmount_mounted_volume(mv);
164}
165
JP Abgrall37aedb32014-06-16 19:07:39 -0700166static int exec_cmd(const char* path, char* const argv[]) {
167 int status;
168 pid_t child;
169 if ((child = vfork()) == 0) {
170 execv(path, argv);
171 _exit(-1);
172 }
173 waitpid(child, &status, 0);
174 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
175 LOGE("%s failed with status %d\n", path, WEXITSTATUS(status));
176 }
177 return WEXITSTATUS(status);
178}
179
Doug Zongkerd4208f92010-09-20 12:16:13 -0700180int format_volume(const char* volume) {
Dees_Troyc51f1f92012-09-20 15:32:13 -0400181 if (PartitionManager.Wipe_By_Path(volume))
182 return 0;
183 else
184 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700185 Volume* v = volume_for_path(volume);
186 if (v == NULL) {
187 LOGE("unknown volume \"%s\"\n", volume);
188 return -1;
189 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700190 if (strcmp(v->fs_type, "ramdisk") == 0) {
191 // you can't format the ramdisk.
192 LOGE("can't format_volume \"%s\"", volume);
193 return -1;
194 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700195 if (strcmp(v->mount_point, volume) != 0) {
196 LOGE("can't give path \"%s\" to format_volume\n", volume);
197 return -1;
198 }
199
200 if (ensure_path_unmounted(volume) != 0) {
201 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
202 return -1;
203 }
204
205 if (strcmp(v->fs_type, "yaffs2") == 0 || strcmp(v->fs_type, "mtd") == 0) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206 mtd_scan_partitions();
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800207 const MtdPartition* partition = mtd_find_partition_by_name(v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 if (partition == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800209 LOGE("format_volume: no MTD partition \"%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
Doug Zongkerd4208f92010-09-20 12:16:13 -0700213 MtdWriteContext *write = mtd_write_partition(partition);
214 if (write == NULL) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800215 LOGW("format_volume: can't open MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 return -1;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700217 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800218 LOGW("format_volume: can't erase MTD \"%s\"\n", v->blk_device);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700219 mtd_write_close(write);
220 return -1;
221 } else if (mtd_write_close(write)) {
Ken Sumrallf35d1ce2013-02-13 12:59:35 -0800222 LOGW("format_volume: can't close MTD \"%s\"\n", v->blk_device);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 return -1;
224 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 return 0;
226 }
227
JP Abgrall37aedb32014-06-16 19:07:39 -0700228 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Doug Zongkerf39989a2013-12-11 15:40:28 -0800229 // if there's a key_loc that looks like a path, it should be a
230 // block device for storing encryption metadata. wipe it too.
231 if (v->key_loc != NULL && v->key_loc[0] == '/') {
232 LOGI("wiping %s\n", v->key_loc);
233 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
234 if (fd < 0) {
235 LOGE("format_volume: failed to open %s\n", v->key_loc);
236 return -1;
237 }
238 wipe_block_device(fd, get_file_size(fd));
239 close(fd);
240 }
241
JP Abgrall37aedb32014-06-16 19:07:39 -0700242 ssize_t length = 0;
243 if (v->length != 0) {
244 length = v->length;
245 } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
246 length = -CRYPT_FOOTER_OFFSET;
247 }
248 int result;
249 if (strcmp(v->fs_type, "ext4") == 0) {
250 result = make_ext4fs(v->blk_device, length, volume, sehandle);
251 } else { /* Has to be f2fs because we checked earlier. */
252 if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700253 LOGE("format_volume: crypt footer + negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700254 return -1;
255 }
256 if (length < 0) {
JP Abgrall78d458c2014-08-04 16:44:33 -0700257 LOGE("format_volume: negative length (%zd) not supported on %s\n", length, v->fs_type);
JP Abgrall37aedb32014-06-16 19:07:39 -0700258 return -1;
259 }
260 char *num_sectors;
JP Abgrall78d458c2014-08-04 16:44:33 -0700261 if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700262 LOGE("format_volume: failed to create %s command for %s\n", v->fs_type, v->blk_device);
263 return -1;
264 }
265 const char *f2fs_path = "/sbin/mkfs.f2fs";
266 const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
267
268 result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
269 free(num_sectors);
270 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700271 if (result != 0) {
JP Abgrall37aedb32014-06-16 19:07:39 -0700272 LOGE("format_volume: make %s failed on %s with %d(%s)\n", v->fs_type, v->blk_device, result, strerror(errno));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273 return -1;
274 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700275 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277
278 LOGE("format_volume: fs_type \"%s\" unsupported\n", v->fs_type);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800279 return -1;
280}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700281
282int setup_install_mounts() {
283 if (fstab == NULL) {
284 LOGE("can't set up install mounts: no fstab loaded\n");
285 return -1;
286 }
287 for (int i = 0; i < fstab->num_entries; ++i) {
288 Volume* v = fstab->recs + i;
289
290 if (strcmp(v->mount_point, "/tmp") == 0 ||
291 strcmp(v->mount_point, "/cache") == 0) {
Doug Zongker99916f02014-01-13 14:16:58 -0800292 if (ensure_path_mounted(v->mount_point) != 0) {
293 LOGE("failed to mount %s\n", v->mount_point);
294 return -1;
295 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700296
297 } else {
Doug Zongker99916f02014-01-13 14:16:58 -0800298 if (ensure_path_unmounted(v->mount_point) != 0) {
299 LOGE("failed to unmount %s\n", v->mount_point);
300 return -1;
301 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700302 }
303 }
304 return 0;
305}