blob: fdcbfe84485148307a158709148893acf46e71e0 [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
Elliott Hughes63a31922016-06-09 17:41:22 -070017#include "roots.h"
18
Tao Baobb10e582017-07-22 16:30:34 -070019#include <ctype.h>
20#include <fcntl.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <stdlib.h>
22#include <sys/mount.h>
23#include <sys/stat.h>
24#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070025#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080026#include <unistd.h>
27
Tao Bao3c00fac2017-07-22 16:46:54 -070028#include <algorithm>
29#include <string>
30#include <vector>
31
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070032#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070033#include <android-base/properties.h>
34#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070035#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070036#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070037#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080038#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070039
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070041#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080042
Tao Baobb10e582017-07-22 16:30:34 -070043static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080044
Tao Baobb10e582017-07-22 16:30:34 -070045extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050046
Tao Baobb10e582017-07-22 16:30:34 -070047void load_volume_table() {
48 fstab = fs_mgr_read_fstab_default();
49 if (!fstab) {
50 LOG(ERROR) << "Failed to read default fstab";
51 return;
52 }
Doug Zongker2810ced2011-02-17 15:55:21 -080053
Tao Baobb10e582017-07-22 16:30:34 -070054 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
55 if (ret == -1) {
56 LOG(ERROR) << "Failed to add /tmp entry to fstab";
57 fs_mgr_free_fstab(fstab);
58 fstab = nullptr;
59 return;
60 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070061
Tao Baobb10e582017-07-22 16:30:34 -070062 printf("recovery filesystem table\n");
63 printf("=========================\n");
64 for (int i = 0; i < fstab->num_entries; ++i) {
65 const Volume* v = &fstab->recs[i];
66 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
67 }
68 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070069}
70
71Volume* volume_for_path(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -070072 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073}
74
Tao Baoabb8f772015-07-30 14:43:27 -070075// Mount the volume specified by path at the given mount_point.
76int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -070077 Volume* v = volume_for_path(path);
78 if (v == nullptr) {
79 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080 return -1;
Tao Baobb10e582017-07-22 16:30:34 -070081 }
82 if (strcmp(v->fs_type, "ramdisk") == 0) {
83 // The ramdisk is always mounted.
84 return 0;
85 }
86
87 if (!scan_mounted_volumes()) {
88 LOG(ERROR) << "Failed to scan mounted volumes";
89 return -1;
90 }
91
92 if (!mount_point) {
93 mount_point = v->mount_point;
94 }
95
96 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
97 if (mv != nullptr) {
98 // Volume is already mounted.
99 return 0;
100 }
101
102 mkdir(mount_point, 0755); // in case it doesn't already exist
103
104 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
105 strcmp(v->fs_type, "vfat") == 0) {
106 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
107 if (result == -1 && fs_mgr_is_formattable(v)) {
108 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
109 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
110 if (fs_mgr_do_format(v, crypt_footer) == 0) {
111 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
112 } else {
113 PLOG(ERROR) << "Failed to format " << mount_point;
114 return -1;
115 }
116 }
117
118 if (result == -1) {
119 PLOG(ERROR) << "Failed to mount " << mount_point;
120 return -1;
121 }
122 return 0;
123 }
124
125 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
126 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800127}
128
Tao Baoabb8f772015-07-30 14:43:27 -0700129int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700130 // Mount at the default mount point.
131 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700132}
133
Doug Zongkerd4208f92010-09-20 12:16:13 -0700134int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700135 const Volume* v = volume_for_path(path);
136 if (v == nullptr) {
137 LOG(ERROR) << "unknown volume for path [" << path << "]";
138 return -1;
139 }
140 if (strcmp(v->fs_type, "ramdisk") == 0) {
141 // The ramdisk is always mounted; you can't unmount it.
142 return -1;
143 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144
Tao Baobb10e582017-07-22 16:30:34 -0700145 if (!scan_mounted_volumes()) {
146 LOG(ERROR) << "Failed to scan mounted volumes";
147 return -1;
148 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700149
Tao Baobb10e582017-07-22 16:30:34 -0700150 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
151 if (mv == nullptr) {
152 // Volume is already unmounted.
153 return 0;
154 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155
Tao Baobb10e582017-07-22 16:30:34 -0700156 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700157}
158
Tao Bao3c00fac2017-07-22 16:46:54 -0700159static int exec_cmd(const std::vector<std::string>& args) {
160 CHECK_NE(static_cast<size_t>(0), args.size());
161
162 std::vector<char*> argv(args.size());
163 std::transform(args.cbegin(), args.cend(), argv.begin(),
164 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
165 argv.push_back(nullptr);
166
167 pid_t child;
168 if ((child = vfork()) == 0) {
169 execv(argv[0], argv.data());
170 _exit(EXIT_FAILURE);
171 }
172
173 int status;
174 waitpid(child, &status, 0);
175 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
176 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
177 }
178 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700179}
180
Jin Qianf3ccad52017-07-24 10:34:35 -0700181static ssize_t get_file_size(int fd, uint64_t reserve_len) {
182 struct stat buf;
183 int ret = fstat(fd, &buf);
184 if (ret) return 0;
185
186 ssize_t computed_size;
187 if (S_ISREG(buf.st_mode)) {
188 computed_size = buf.st_size - reserve_len;
189 } else if (S_ISBLK(buf.st_mode)) {
190 computed_size = get_block_device_size(fd) - reserve_len;
191 } else {
192 computed_size = 0;
193 }
194
195 return computed_size;
196}
197
Paul Lawrenced0db3372015-11-05 13:38:40 -0800198int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700199 const Volume* v = volume_for_path(volume);
200 if (v == nullptr) {
201 LOG(ERROR) << "unknown volume \"" << volume << "\"";
202 return -1;
203 }
204 if (strcmp(v->fs_type, "ramdisk") == 0) {
205 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
206 return -1;
207 }
208 if (strcmp(v->mount_point, volume) != 0) {
209 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
210 return -1;
211 }
212 if (ensure_path_unmounted(volume) != 0) {
213 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
214 return -1;
215 }
216 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700217 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700219 }
220
221 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
222 // metadata. Wipe it too.
223 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
224 LOG(INFO) << "Wiping " << v->key_loc;
225 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
226 if (fd == -1) {
227 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
228 return -1;
229 }
230 wipe_block_device(fd, get_file_size(fd));
231 close(fd);
232 }
233
234 ssize_t length = 0;
235 if (v->length != 0) {
236 length = v->length;
237 } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) {
238 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
239 if (fd == -1) {
240 PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device;
241 return -1;
242 }
243 length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
244 if (length <= 0) {
245 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
246 return -1;
247 }
248 }
249
250 if (strcmp(v->fs_type, "ext4") == 0) {
251 static constexpr int kBlockSize = 4096;
252 std::vector<std::string> mke2fs_args = {
253 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
254 };
255
256 int raid_stride = v->logical_blk_size / kBlockSize;
257 int raid_stripe_width = v->erase_blk_size / kBlockSize;
258 // stride should be the max of 8KB and logical block size
259 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
260 raid_stride = 8192 / kBlockSize;
261 }
262 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
263 mke2fs_args.push_back("-E");
264 mke2fs_args.push_back(
265 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
266 }
267 mke2fs_args.push_back(v->blk_device);
268 if (length != 0) {
269 mke2fs_args.push_back(std::to_string(length / kBlockSize));
270 }
271
272 int result = exec_cmd(mke2fs_args);
273 if (result == 0 && directory != nullptr) {
274 std::vector<std::string> e2fsdroid_args = {
275 "/sbin/e2fsdroid_static",
276 "-e",
277 "-f",
278 directory,
279 "-a",
280 volume,
281 v->blk_device,
282 };
283 result = exec_cmd(e2fsdroid_args);
284 }
285
286 if (result != 0) {
287 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
288 return -1;
289 }
290 return 0;
291 }
292
293 // Has to be f2fs because we checked earlier.
294 std::vector<std::string> f2fs_args = { "/sbin/mkfs.f2fs", "-t", "-d1", v->blk_device };
295 if (length >= 512) {
296 f2fs_args.push_back(std::to_string(length / 512));
297 }
298
299 int result = exec_cmd(f2fs_args);
300 if (result != 0) {
301 PLOG(ERROR) << "format_volume: Failed to make f2fs on " << v->blk_device;
302 return -1;
303 }
304 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700306
Paul Lawrenced0db3372015-11-05 13:38:40 -0800307int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700308 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800309}
310
Doug Zongker239ac6a2013-08-20 16:03:25 -0700311int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700312 if (fstab == nullptr) {
313 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
314 return -1;
315 }
316 for (int i = 0; i < fstab->num_entries; ++i) {
317 const Volume* v = fstab->recs + i;
318
319 // We don't want to do anything with "/".
320 if (strcmp(v->mount_point, "/") == 0) {
321 continue;
322 }
323
324 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
325 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700326 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700327 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700328 }
329 } else {
330 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700331 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700332 return -1;
333 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700334 }
Tao Bao57130c42017-05-10 12:11:21 -0700335 }
336 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700337}