blob: 26602a7104b773d87582b19b5f233f28484132d4 [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>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053021#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080022#include <stdlib.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070026#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <unistd.h>
28
Tao Bao3c00fac2017-07-22 16:46:54 -070029#include <algorithm>
30#include <string>
31#include <vector>
32
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070033#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070034#include <android-base/properties.h>
35#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070036#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070037#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070038#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080039#include <fs_mgr.h>
Tao Baode40ba52016-10-05 23:17:01 -070040
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080041#include "common.h"
Elliott Hughes63a31922016-06-09 17:41:22 -070042#include "mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080043
Tao Baobb10e582017-07-22 16:30:34 -070044static struct fstab* fstab = nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tao Baobb10e582017-07-22 16:30:34 -070046extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050047
Tao Baobb10e582017-07-22 16:30:34 -070048void load_volume_table() {
49 fstab = fs_mgr_read_fstab_default();
50 if (!fstab) {
51 LOG(ERROR) << "Failed to read default fstab";
52 return;
53 }
Doug Zongker2810ced2011-02-17 15:55:21 -080054
Tao Baobb10e582017-07-22 16:30:34 -070055 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
56 if (ret == -1) {
57 LOG(ERROR) << "Failed to add /tmp entry to fstab";
58 fs_mgr_free_fstab(fstab);
59 fstab = nullptr;
60 return;
61 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070062
Tao Baobb10e582017-07-22 16:30:34 -070063 printf("recovery filesystem table\n");
64 printf("=========================\n");
65 for (int i = 0; i < fstab->num_entries; ++i) {
66 const Volume* v = &fstab->recs[i];
67 printf(" %d %s %s %s %lld\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
68 }
69 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070070}
71
72Volume* volume_for_path(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -070073 return fs_mgr_get_entry_for_mount_point(fstab, path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074}
75
Tao Baoabb8f772015-07-30 14:43:27 -070076// Mount the volume specified by path at the given mount_point.
77int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -070078 Volume* v = volume_for_path(path);
79 if (v == nullptr) {
80 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080081 return -1;
Tao Baobb10e582017-07-22 16:30:34 -070082 }
83 if (strcmp(v->fs_type, "ramdisk") == 0) {
84 // The ramdisk is always mounted.
85 return 0;
86 }
87
88 if (!scan_mounted_volumes()) {
89 LOG(ERROR) << "Failed to scan mounted volumes";
90 return -1;
91 }
92
93 if (!mount_point) {
94 mount_point = v->mount_point;
95 }
96
97 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
98 if (mv != nullptr) {
99 // Volume is already mounted.
100 return 0;
101 }
102
103 mkdir(mount_point, 0755); // in case it doesn't already exist
104
105 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
106 strcmp(v->fs_type, "vfat") == 0) {
107 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
108 if (result == -1 && fs_mgr_is_formattable(v)) {
109 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
110 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
111 if (fs_mgr_do_format(v, crypt_footer) == 0) {
112 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
113 } else {
114 PLOG(ERROR) << "Failed to format " << mount_point;
115 return -1;
116 }
117 }
118
119 if (result == -1) {
120 PLOG(ERROR) << "Failed to mount " << mount_point;
121 return -1;
122 }
123 return 0;
124 }
125
126 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
127 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800128}
129
Tao Baoabb8f772015-07-30 14:43:27 -0700130int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700131 // Mount at the default mount point.
132 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700133}
134
Doug Zongkerd4208f92010-09-20 12:16:13 -0700135int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700136 const Volume* v = volume_for_path(path);
137 if (v == nullptr) {
138 LOG(ERROR) << "unknown volume for path [" << path << "]";
139 return -1;
140 }
141 if (strcmp(v->fs_type, "ramdisk") == 0) {
142 // The ramdisk is always mounted; you can't unmount it.
143 return -1;
144 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145
Tao Baobb10e582017-07-22 16:30:34 -0700146 if (!scan_mounted_volumes()) {
147 LOG(ERROR) << "Failed to scan mounted volumes";
148 return -1;
149 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150
Tao Baobb10e582017-07-22 16:30:34 -0700151 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
152 if (mv == nullptr) {
153 // Volume is already unmounted.
154 return 0;
155 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156
Tao Baobb10e582017-07-22 16:30:34 -0700157 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700158}
159
Tao Bao3c00fac2017-07-22 16:46:54 -0700160static int exec_cmd(const std::vector<std::string>& args) {
161 CHECK_NE(static_cast<size_t>(0), args.size());
162
163 std::vector<char*> argv(args.size());
164 std::transform(args.cbegin(), args.cend(), argv.begin(),
165 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
166 argv.push_back(nullptr);
167
168 pid_t child;
169 if ((child = vfork()) == 0) {
170 execv(argv[0], argv.data());
171 _exit(EXIT_FAILURE);
172 }
173
174 int status;
175 waitpid(child, &status, 0);
176 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
177 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
178 }
179 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700180}
181
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530182static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700183 struct stat buf;
184 int ret = fstat(fd, &buf);
185 if (ret) return 0;
186
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530187 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700188 if (S_ISREG(buf.st_mode)) {
189 computed_size = buf.st_size - reserve_len;
190 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530191 uint64_t block_device_size = get_block_device_size(fd);
192 if (block_device_size < reserve_len ||
193 block_device_size > std::numeric_limits<int64_t>::max()) {
194 computed_size = 0;
195 } else {
196 computed_size = block_device_size - reserve_len;
197 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700198 } else {
199 computed_size = 0;
200 }
201
202 return computed_size;
203}
204
Paul Lawrenced0db3372015-11-05 13:38:40 -0800205int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700206 const Volume* v = volume_for_path(volume);
207 if (v == nullptr) {
208 LOG(ERROR) << "unknown volume \"" << volume << "\"";
209 return -1;
210 }
211 if (strcmp(v->fs_type, "ramdisk") == 0) {
212 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
213 return -1;
214 }
215 if (strcmp(v->mount_point, volume) != 0) {
216 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
217 return -1;
218 }
219 if (ensure_path_unmounted(volume) != 0) {
220 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
221 return -1;
222 }
223 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700224 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800225 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700226 }
227
228 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
229 // metadata. Wipe it too.
230 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
231 LOG(INFO) << "Wiping " << v->key_loc;
232 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
233 if (fd == -1) {
234 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
235 return -1;
236 }
237 wipe_block_device(fd, get_file_size(fd));
238 close(fd);
239 }
240
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530241 int64_t length = 0;
Tao Bao3c00fac2017-07-22 16:46:54 -0700242 if (v->length != 0) {
243 length = v->length;
244 } else if (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0) {
245 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
246 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530247 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700248 return -1;
249 }
250 length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
251 if (length <= 0) {
252 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
253 return -1;
254 }
255 }
256
257 if (strcmp(v->fs_type, "ext4") == 0) {
258 static constexpr int kBlockSize = 4096;
259 std::vector<std::string> mke2fs_args = {
260 "/sbin/mke2fs_static", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
261 };
262
263 int raid_stride = v->logical_blk_size / kBlockSize;
264 int raid_stripe_width = v->erase_blk_size / kBlockSize;
265 // stride should be the max of 8KB and logical block size
266 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
267 raid_stride = 8192 / kBlockSize;
268 }
269 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
270 mke2fs_args.push_back("-E");
271 mke2fs_args.push_back(
272 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
273 }
274 mke2fs_args.push_back(v->blk_device);
275 if (length != 0) {
276 mke2fs_args.push_back(std::to_string(length / kBlockSize));
277 }
278
279 int result = exec_cmd(mke2fs_args);
280 if (result == 0 && directory != nullptr) {
281 std::vector<std::string> e2fsdroid_args = {
282 "/sbin/e2fsdroid_static",
283 "-e",
284 "-f",
285 directory,
286 "-a",
287 volume,
288 v->blk_device,
289 };
290 result = exec_cmd(e2fsdroid_args);
291 }
292
293 if (result != 0) {
294 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
295 return -1;
296 }
297 return 0;
298 }
299
300 // Has to be f2fs because we checked earlier.
301 std::vector<std::string> f2fs_args = { "/sbin/mkfs.f2fs", "-t", "-d1", v->blk_device };
302 if (length >= 512) {
303 f2fs_args.push_back(std::to_string(length / 512));
304 }
305
306 int result = exec_cmd(f2fs_args);
307 if (result != 0) {
308 PLOG(ERROR) << "format_volume: Failed to make f2fs on " << v->blk_device;
309 return -1;
310 }
311 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700313
Paul Lawrenced0db3372015-11-05 13:38:40 -0800314int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700315 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800316}
317
Doug Zongker239ac6a2013-08-20 16:03:25 -0700318int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700319 if (fstab == nullptr) {
320 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
321 return -1;
322 }
323 for (int i = 0; i < fstab->num_entries; ++i) {
324 const Volume* v = fstab->recs + i;
325
326 // We don't want to do anything with "/".
327 if (strcmp(v->mount_point, "/") == 0) {
328 continue;
329 }
330
331 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
332 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700333 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700334 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700335 }
336 } else {
337 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700338 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700339 return -1;
340 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700341 }
Tao Bao57130c42017-05-10 12:11:21 -0700342 }
343 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700344}