blob: 7dc4ec3d91a187000e4b0698ebbf60e1d1f2a764 [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>
Tom Cherry45e505a2018-11-29 13:33:07 -080021#include <inttypes.h>
Abhishek Arpure4fec8e92017-08-24 15:27:16 +053022#include <stdint.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <stdlib.h>
Tao Baoad774b22017-09-29 10:39:08 -070024#include <string.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/types.h>
JP Abgrall37aedb32014-06-16 19:07:39 -070028#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <unistd.h>
30
Tao Bao3c00fac2017-07-22 16:46:54 -070031#include <algorithm>
32#include <string>
33#include <vector>
34
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070035#include <android-base/logging.h>
Jin Qianded2dac2017-04-21 14:36:12 -070036#include <android-base/properties.h>
37#include <android-base/stringprintf.h>
Jin Qianf3ccad52017-07-24 10:34:35 -070038#include <android-base/unique_fd.h>
Tao Baobb10e582017-07-22 16:30:34 -070039#include <cryptfs.h>
Tao Baode40ba52016-10-05 23:17:01 -070040#include <ext4_utils/wipe.h>
Ken Sumrallf35d1ce2013-02-13 12:59:35 -080041#include <fs_mgr.h>
David Anderson2b2f4232018-10-29 18:48:56 -070042#include <fs_mgr_dm_linear.h>
Tao Baode40ba52016-10-05 23:17:01 -070043
Tao Bao9a319f02018-01-04 13:19:11 -080044#include "otautil/mounts.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080045
Tao Baobb10e582017-07-22 16:30:34 -070046static struct fstab* fstab = nullptr;
David Anderson2b2f4232018-10-29 18:48:56 -070047static bool did_map_logical_partitions = false;
Yifan Hong49327802018-11-26 14:59:09 -080048static constexpr const char* SYSTEM_ROOT = "/system";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080049
Tao Baobb10e582017-07-22 16:30:34 -070050extern struct selabel_handle* sehandle;
Stephen Smalley779701d2012-02-09 14:13:23 -050051
Tao Baobb10e582017-07-22 16:30:34 -070052void load_volume_table() {
53 fstab = fs_mgr_read_fstab_default();
54 if (!fstab) {
55 LOG(ERROR) << "Failed to read default fstab";
56 return;
57 }
Doug Zongker2810ced2011-02-17 15:55:21 -080058
Tao Baobb10e582017-07-22 16:30:34 -070059 int ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
60 if (ret == -1) {
61 LOG(ERROR) << "Failed to add /tmp entry to fstab";
62 fs_mgr_free_fstab(fstab);
63 fstab = nullptr;
64 return;
65 }
Doug Zongkerd4208f92010-09-20 12:16:13 -070066
Tao Baobb10e582017-07-22 16:30:34 -070067 printf("recovery filesystem table\n");
68 printf("=========================\n");
69 for (int i = 0; i < fstab->num_entries; ++i) {
70 const Volume* v = &fstab->recs[i];
Tom Cherry45e505a2018-11-29 13:33:07 -080071 printf(" %d %s %s %s %" PRId64 "\n", i, v->mount_point, v->fs_type, v->blk_device, v->length);
Tao Baobb10e582017-07-22 16:30:34 -070072 }
73 printf("\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -070074}
75
Tao Bao3e18f2b2017-09-29 17:11:13 -070076Volume* volume_for_mount_point(const std::string& mount_point) {
77 return fs_mgr_get_entry_for_mount_point(fstab, mount_point);
78}
79
Tao Bao2dfc1a32017-09-27 13:12:11 -070080// Finds the volume specified by the given path. fs_mgr_get_entry_for_mount_point() does exact match
81// only, so it attempts the prefixes recursively (e.g. "/cache/recovery/last_log",
82// "/cache/recovery", "/cache", "/" for a given path of "/cache/recovery/last_log") and returns the
83// first match or nullptr.
Tao Bao3e18f2b2017-09-29 17:11:13 -070084static Volume* volume_for_path(const char* path) {
Tao Bao2dfc1a32017-09-27 13:12:11 -070085 if (path == nullptr || path[0] == '\0') return nullptr;
86 std::string str(path);
87 while (true) {
Tao Baoad774b22017-09-29 10:39:08 -070088 Volume* result = fs_mgr_get_entry_for_mount_point(fstab, str);
Tao Bao2dfc1a32017-09-27 13:12:11 -070089 if (result != nullptr || str == "/") {
90 return result;
91 }
92 size_t slash = str.find_last_of('/');
93 if (slash == std::string::npos) return nullptr;
94 if (slash == 0) {
95 str = "/";
96 } else {
97 str = str.substr(0, slash);
98 }
99 }
100 return nullptr;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101}
102
Tao Baoabb8f772015-07-30 14:43:27 -0700103// Mount the volume specified by path at the given mount_point.
104int ensure_path_mounted_at(const char* path, const char* mount_point) {
Tao Baobb10e582017-07-22 16:30:34 -0700105 Volume* v = volume_for_path(path);
106 if (v == nullptr) {
107 LOG(ERROR) << "unknown volume for path [" << path << "]";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800108 return -1;
Tao Baobb10e582017-07-22 16:30:34 -0700109 }
110 if (strcmp(v->fs_type, "ramdisk") == 0) {
111 // The ramdisk is always mounted.
112 return 0;
113 }
114
115 if (!scan_mounted_volumes()) {
116 LOG(ERROR) << "Failed to scan mounted volumes";
117 return -1;
118 }
119
120 if (!mount_point) {
121 mount_point = v->mount_point;
122 }
123
David Anderson2b2f4232018-10-29 18:48:56 -0700124 // If we can't acquire the block device for a logical partition, it likely
125 // was never created. In that case we try to create it.
126 if (fs_mgr_is_logical(v) && !fs_mgr_update_logical_partition(v)) {
127 if (did_map_logical_partitions) {
128 LOG(ERROR) << "Failed to find block device for partition";
129 return -1;
130 }
131 std::string super_name = fs_mgr_get_super_partition_name();
132 if (!android::fs_mgr::CreateLogicalPartitions(super_name)) {
133 LOG(ERROR) << "Failed to create logical partitions";
134 return -1;
135 }
136 did_map_logical_partitions = true;
137 if (!fs_mgr_update_logical_partition(v)) {
138 LOG(ERROR) << "Failed to find block device for partition";
139 return -1;
140 }
141 }
142
Tao Baobb10e582017-07-22 16:30:34 -0700143 const MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
144 if (mv != nullptr) {
145 // Volume is already mounted.
146 return 0;
147 }
148
149 mkdir(mount_point, 0755); // in case it doesn't already exist
150
151 if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "squashfs") == 0 ||
Lianjun Huang5d7be6b2018-09-03 18:07:35 +0800152 strcmp(v->fs_type, "vfat") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700153 int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
154 if (result == -1 && fs_mgr_is_formattable(v)) {
155 PLOG(ERROR) << "Failed to mount " << mount_point << "; formatting";
156 bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
157 if (fs_mgr_do_format(v, crypt_footer) == 0) {
158 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
159 } else {
160 PLOG(ERROR) << "Failed to format " << mount_point;
161 return -1;
162 }
163 }
164
165 if (result == -1) {
166 PLOG(ERROR) << "Failed to mount " << mount_point;
167 return -1;
168 }
169 return 0;
170 }
171
172 LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
173 return -1;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800174}
175
Tao Baoabb8f772015-07-30 14:43:27 -0700176int ensure_path_mounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700177 // Mount at the default mount point.
178 return ensure_path_mounted_at(path, nullptr);
Tao Baoabb8f772015-07-30 14:43:27 -0700179}
180
Doug Zongkerd4208f92010-09-20 12:16:13 -0700181int ensure_path_unmounted(const char* path) {
Tao Baobb10e582017-07-22 16:30:34 -0700182 const Volume* v = volume_for_path(path);
183 if (v == nullptr) {
184 LOG(ERROR) << "unknown volume for path [" << path << "]";
185 return -1;
186 }
187 if (strcmp(v->fs_type, "ramdisk") == 0) {
188 // The ramdisk is always mounted; you can't unmount it.
189 return -1;
190 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800191
Tao Baobb10e582017-07-22 16:30:34 -0700192 if (!scan_mounted_volumes()) {
193 LOG(ERROR) << "Failed to scan mounted volumes";
194 return -1;
195 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700196
Tao Baobb10e582017-07-22 16:30:34 -0700197 MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
198 if (mv == nullptr) {
199 // Volume is already unmounted.
200 return 0;
201 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202
Tao Baobb10e582017-07-22 16:30:34 -0700203 return unmount_mounted_volume(mv);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700204}
205
Tao Bao3c00fac2017-07-22 16:46:54 -0700206static int exec_cmd(const std::vector<std::string>& args) {
207 CHECK_NE(static_cast<size_t>(0), args.size());
208
209 std::vector<char*> argv(args.size());
210 std::transform(args.cbegin(), args.cend(), argv.begin(),
211 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
212 argv.push_back(nullptr);
213
214 pid_t child;
George Burgess IV1cfb3612018-02-17 17:48:45 -0800215 if ((child = fork()) == 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700216 execv(argv[0], argv.data());
217 _exit(EXIT_FAILURE);
218 }
219
220 int status;
221 waitpid(child, &status, 0);
222 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
223 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
224 }
225 return WEXITSTATUS(status);
JP Abgrall37aedb32014-06-16 19:07:39 -0700226}
227
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530228static int64_t get_file_size(int fd, uint64_t reserve_len) {
Jin Qianf3ccad52017-07-24 10:34:35 -0700229 struct stat buf;
230 int ret = fstat(fd, &buf);
231 if (ret) return 0;
232
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530233 int64_t computed_size;
Jin Qianf3ccad52017-07-24 10:34:35 -0700234 if (S_ISREG(buf.st_mode)) {
235 computed_size = buf.st_size - reserve_len;
236 } else if (S_ISBLK(buf.st_mode)) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530237 uint64_t block_device_size = get_block_device_size(fd);
238 if (block_device_size < reserve_len ||
239 block_device_size > std::numeric_limits<int64_t>::max()) {
240 computed_size = 0;
241 } else {
242 computed_size = block_device_size - reserve_len;
243 }
Jin Qianf3ccad52017-07-24 10:34:35 -0700244 } else {
245 computed_size = 0;
246 }
247
248 return computed_size;
249}
250
Paul Lawrenced0db3372015-11-05 13:38:40 -0800251int format_volume(const char* volume, const char* directory) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700252 const Volume* v = volume_for_path(volume);
253 if (v == nullptr) {
254 LOG(ERROR) << "unknown volume \"" << volume << "\"";
255 return -1;
256 }
257 if (strcmp(v->fs_type, "ramdisk") == 0) {
258 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
259 return -1;
260 }
261 if (strcmp(v->mount_point, volume) != 0) {
262 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
263 return -1;
264 }
265 if (ensure_path_unmounted(volume) != 0) {
266 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
267 return -1;
268 }
269 if (strcmp(v->fs_type, "ext4") != 0 && strcmp(v->fs_type, "f2fs") != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700270 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800271 return -1;
Tao Bao3c00fac2017-07-22 16:46:54 -0700272 }
273
274 // If there's a key_loc that looks like a path, it should be a block device for storing encryption
275 // metadata. Wipe it too.
276 if (v->key_loc != nullptr && v->key_loc[0] == '/') {
277 LOG(INFO) << "Wiping " << v->key_loc;
278 int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
279 if (fd == -1) {
280 PLOG(ERROR) << "format_volume: Failed to open " << v->key_loc;
281 return -1;
282 }
283 wipe_block_device(fd, get_file_size(fd));
284 close(fd);
285 }
286
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530287 int64_t length = 0;
Jin Qiancc100082017-11-17 23:53:22 -0800288 if (v->length > 0) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700289 length = v->length;
Jin Qiancc100082017-11-17 23:53:22 -0800290 } else if (v->length < 0 ||
291 (v->key_loc != nullptr && strcmp(v->key_loc, "footer") == 0)) {
Tao Bao3c00fac2017-07-22 16:46:54 -0700292 android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
293 if (fd == -1) {
Abhishek Arpure4fec8e92017-08-24 15:27:16 +0530294 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700295 return -1;
296 }
Jin Qiancc100082017-11-17 23:53:22 -0800297 length =
298 get_file_size(fd.get(), v->length ? -v->length : CRYPT_FOOTER_OFFSET);
Tao Bao3c00fac2017-07-22 16:46:54 -0700299 if (length <= 0) {
Jin Qiancc100082017-11-17 23:53:22 -0800300 LOG(ERROR) << "get_file_size: invalid size " << length << " for "
301 << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700302 return -1;
303 }
304 }
305
306 if (strcmp(v->fs_type, "ext4") == 0) {
307 static constexpr int kBlockSize = 4096;
308 std::vector<std::string> mke2fs_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900309 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
Tao Bao3c00fac2017-07-22 16:46:54 -0700310 };
311
312 int raid_stride = v->logical_blk_size / kBlockSize;
313 int raid_stripe_width = v->erase_blk_size / kBlockSize;
314 // stride should be the max of 8KB and logical block size
315 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
316 raid_stride = 8192 / kBlockSize;
317 }
318 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
319 mke2fs_args.push_back("-E");
320 mke2fs_args.push_back(
321 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
322 }
323 mke2fs_args.push_back(v->blk_device);
324 if (length != 0) {
325 mke2fs_args.push_back(std::to_string(length / kBlockSize));
326 }
327
328 int result = exec_cmd(mke2fs_args);
329 if (result == 0 && directory != nullptr) {
330 std::vector<std::string> e2fsdroid_args = {
Jiyong Park69364fe2018-06-20 14:18:18 +0900331 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
Tao Bao3c00fac2017-07-22 16:46:54 -0700332 };
333 result = exec_cmd(e2fsdroid_args);
334 }
335
336 if (result != 0) {
337 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
338 return -1;
339 }
340 return 0;
341 }
342
343 // Has to be f2fs because we checked earlier.
Jaegeuk Kim43582622018-04-02 13:37:35 -0700344 static constexpr int kSectorSize = 4096;
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800345 std::string cmd("/sbin/mkfs.f2fs");
Jaegeuk Kim43582622018-04-02 13:37:35 -0700346 // clang-format off
347 std::vector<std::string> make_f2fs_cmd = {
348 cmd,
Jaegeuk Kim91e631d2018-11-21 11:12:54 -0800349 "-g", "android",
Jaegeuk Kim43582622018-04-02 13:37:35 -0700350 v->blk_device,
351 };
352 // clang-format on
353 if (length >= kSectorSize) {
354 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
Tao Bao3c00fac2017-07-22 16:46:54 -0700355 }
356
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800357 int result = exec_cmd(make_f2fs_cmd);
358 if (result == 0 && directory != nullptr) {
359 cmd = "/sbin/sload.f2fs";
Jaegeuk Kim43582622018-04-02 13:37:35 -0700360 // clang-format off
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800361 std::vector<std::string> sload_f2fs_cmd = {
Jaegeuk Kim43582622018-04-02 13:37:35 -0700362 cmd,
363 "-f", directory,
364 "-t", volume,
365 v->blk_device,
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800366 };
Jaegeuk Kim43582622018-04-02 13:37:35 -0700367 // clang-format on
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800368 result = exec_cmd(sload_f2fs_cmd);
369 }
Tao Bao3c00fac2017-07-22 16:46:54 -0700370 if (result != 0) {
Jaegeuk Kimc1c73112017-11-28 19:48:05 -0800371 PLOG(ERROR) << "format_volume: Failed " << cmd << " on " << v->blk_device;
Tao Bao3c00fac2017-07-22 16:46:54 -0700372 return -1;
373 }
374 return 0;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800375}
Doug Zongker239ac6a2013-08-20 16:03:25 -0700376
Paul Lawrenced0db3372015-11-05 13:38:40 -0800377int format_volume(const char* volume) {
Tao Baobb10e582017-07-22 16:30:34 -0700378 return format_volume(volume, nullptr);
Paul Lawrenced0db3372015-11-05 13:38:40 -0800379}
380
Doug Zongker239ac6a2013-08-20 16:03:25 -0700381int setup_install_mounts() {
Tao Bao57130c42017-05-10 12:11:21 -0700382 if (fstab == nullptr) {
383 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
384 return -1;
385 }
386 for (int i = 0; i < fstab->num_entries; ++i) {
387 const Volume* v = fstab->recs + i;
388
389 // We don't want to do anything with "/".
390 if (strcmp(v->mount_point, "/") == 0) {
391 continue;
392 }
393
394 if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
395 if (ensure_path_mounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700396 LOG(ERROR) << "Failed to mount " << v->mount_point;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700397 return -1;
Tao Bao57130c42017-05-10 12:11:21 -0700398 }
399 } else {
400 if (ensure_path_unmounted(v->mount_point) != 0) {
Tao Baobb10e582017-07-22 16:30:34 -0700401 LOG(ERROR) << "Failed to unmount " << v->mount_point;
Tao Bao57130c42017-05-10 12:11:21 -0700402 return -1;
403 }
Doug Zongker239ac6a2013-08-20 16:03:25 -0700404 }
Tao Bao57130c42017-05-10 12:11:21 -0700405 }
406 return 0;
Doug Zongker239ac6a2013-08-20 16:03:25 -0700407}
David Anderson2b2f4232018-10-29 18:48:56 -0700408
409bool logical_partitions_mapped() {
410 return did_map_logical_partitions;
411}
Yifan Hong49327802018-11-26 14:59:09 -0800412
413std::string get_system_root() {
414 if (volume_for_mount_point(SYSTEM_ROOT) == nullptr) {
415 return "/";
416 } else {
417 return SYSTEM_ROOT;
418 }
419}