1 /*
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 "recovery_utils/roots.h"
18
19 #include <fcntl.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27
28 #include <iostream>
29 #include <string>
30 #include <vector>
31
32 #include <android-base/logging.h>
33 #include <android-base/properties.h>
34 #include <android-base/stringprintf.h>
35 #include <android-base/unique_fd.h>
36 #include <ext4_utils/ext4_utils.h>
37 #include <ext4_utils/wipe.h>
38 #include <fs_mgr.h>
39 #include <fs_mgr/roots.h>
40
41 #include "otautil/sysutil.h"
42
43 using android::fs_mgr::Fstab;
44 using android::fs_mgr::FstabEntry;
45 using android::fs_mgr::ReadDefaultFstab;
46
47 static Fstab fstab;
48
49 constexpr const char* CACHE_ROOT = "/cache";
50
load_volume_table()51 void load_volume_table() {
52 if (!ReadDefaultFstab(&fstab)) {
53 LOG(ERROR) << "Failed to read default fstab";
54 return;
55 }
56
57 fstab.emplace_back(FstabEntry{
58 .blk_device = "ramdisk",
59 .mount_point = "/tmp",
60 .fs_type = "ramdisk",
61 .length = 0,
62 });
63
64 std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl;
65 for (size_t i = 0; i < fstab.size(); ++i) {
66 const auto& entry = fstab[i];
67 std::cout << " " << i << " " << entry.mount_point << " "
68 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
69 << std::endl;
70 }
71 std::cout << std::endl;
72 }
73
volume_for_mount_point(const std::string & mount_point)74 Volume* volume_for_mount_point(const std::string& mount_point) {
75 return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point);
76 }
77
78 // Mount the volume specified by path at the given mount_point.
ensure_path_mounted_at(const std::string & path,const std::string & mount_point)79 int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) {
80 return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1;
81 }
82
ensure_path_mounted(const std::string & path)83 int ensure_path_mounted(const std::string& path) {
84 // Mount at the default mount point.
85 return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1;
86 }
87
ensure_path_unmounted(const std::string & path)88 int ensure_path_unmounted(const std::string& path) {
89 return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1;
90 }
91
exec_cmd(const std::vector<std::string> & args)92 static int exec_cmd(const std::vector<std::string>& args) {
93 CHECK(!args.empty());
94 auto argv = StringVectorToNullTerminatedArray(args);
95
96 pid_t child;
97 if ((child = fork()) == 0) {
98 execv(argv[0], argv.data());
99 _exit(EXIT_FAILURE);
100 }
101
102 int status;
103 waitpid(child, &status, 0);
104 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
105 LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status);
106 }
107 return WEXITSTATUS(status);
108 }
109
get_file_size(int fd,uint64_t reserve_len)110 static int64_t get_file_size(int fd, uint64_t reserve_len) {
111 struct stat buf;
112 int ret = fstat(fd, &buf);
113 if (ret) return 0;
114
115 int64_t computed_size;
116 if (S_ISREG(buf.st_mode)) {
117 computed_size = buf.st_size - reserve_len;
118 } else if (S_ISBLK(buf.st_mode)) {
119 uint64_t block_device_size = get_block_device_size(fd);
120 if (block_device_size < reserve_len ||
121 block_device_size > std::numeric_limits<int64_t>::max()) {
122 computed_size = 0;
123 } else {
124 computed_size = block_device_size - reserve_len;
125 }
126 } else {
127 computed_size = 0;
128 }
129
130 return computed_size;
131 }
132
format_volume(const std::string & volume,const std::string & directory,std::string_view new_fstype)133 int format_volume(const std::string& volume, const std::string& directory,
134 std::string_view new_fstype) {
135 const FstabEntry* v = android::fs_mgr::GetEntryForPath(&fstab, volume);
136 if (v == nullptr) {
137 LOG(ERROR) << "unknown volume \"" << volume << "\"";
138 return -1;
139 }
140 if (v->fs_type == "ramdisk") {
141 LOG(ERROR) << "can't format_volume \"" << volume << "\"";
142 return -1;
143 }
144 if (v->mount_point != volume) {
145 LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
146 return -1;
147 }
148 if (ensure_path_unmounted(volume) != 0) {
149 LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\"";
150 return -1;
151 }
152 if (v->fs_type != "ext4" && v->fs_type != "f2fs") {
153 LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
154 return -1;
155 }
156
157 bool needs_casefold = false;
158
159 if (volume == "/data") {
160 needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false);
161 }
162
163 int64_t length = 0;
164 if (v->length > 0) {
165 length = v->length;
166 } else if (v->length < 0) {
167 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY));
168 if (fd == -1) {
169 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
170 return -1;
171 }
172 length = get_file_size(fd.get(), -v->length);
173 if (length <= 0) {
174 LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
175 return -1;
176 }
177 }
178
179 // If the raw disk will be used as a metadata encrypted device mapper target,
180 // next boot will do encrypt_in_place the raw disk. While fs_mgr mounts /data
181 // as RO to avoid write file operations before encrypt_inplace, this code path
182 // is not well tested so we would like to avoid it if possible. For safety,
183 // let vold do the formatting on boot for metadata encrypted devices, except
184 // when user specified a new fstype. Because init formats /data according
185 // to fstab, it's difficult to override the fstab in init.
186 if (!v->metadata_key_dir.empty() && length == 0 && new_fstype.empty()) {
187 android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDWR));
188 if (fd == -1) {
189 PLOG(ERROR) << "format_volume: failed to open " << v->blk_device;
190 return -1;
191 }
192 int64_t device_size = get_file_size(fd.get(), 0);
193 if (device_size > 0 && !wipe_block_device(fd.get(), device_size)) {
194 LOG(INFO) << "format_volume: wipe metadata encrypted " << v->blk_device << " with size "
195 << device_size;
196 return 0;
197 }
198 }
199
200 if ((v->fs_type == "ext4" && new_fstype.empty()) || new_fstype == "ext4") {
201 LOG(INFO) << "Formatting " << v->blk_device << " as ext4";
202 static constexpr int kBlockSize = 4096;
203 std::vector<std::string> mke2fs_args = {
204 "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize),
205 };
206
207 // Following is added for Project ID's quota as they require wider inodes.
208 // The Quotas themselves are enabled by tune2fs on boot.
209 mke2fs_args.push_back("-I");
210 mke2fs_args.push_back("512");
211
212 if (v->fs_mgr_flags.ext_meta_csum) {
213 mke2fs_args.push_back("-O");
214 mke2fs_args.push_back("metadata_csum");
215 mke2fs_args.push_back("-O");
216 mke2fs_args.push_back("64bit");
217 mke2fs_args.push_back("-O");
218 mke2fs_args.push_back("extent");
219 }
220
221 int raid_stride = v->logical_blk_size / kBlockSize;
222 int raid_stripe_width = v->erase_blk_size / kBlockSize;
223 // stride should be the max of 8KB and logical block size
224 if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
225 raid_stride = 8192 / kBlockSize;
226 }
227 if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
228 mke2fs_args.push_back("-E");
229 mke2fs_args.push_back(
230 android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width));
231 }
232 mke2fs_args.push_back(v->blk_device);
233 if (length != 0) {
234 mke2fs_args.push_back(std::to_string(length / kBlockSize));
235 }
236
237 int result = exec_cmd(mke2fs_args);
238 if (result == 0 && !directory.empty()) {
239 std::vector<std::string> e2fsdroid_args = {
240 "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device,
241 };
242 result = exec_cmd(e2fsdroid_args);
243 }
244
245 if (result != 0) {
246 PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device;
247 return -1;
248 }
249 return 0;
250 }
251
252 // Has to be f2fs because we checked earlier.
253 LOG(INFO) << "Formatting " << v->blk_device << " as f2fs";
254 static constexpr int kSectorSize = 4096;
255 std::vector<std::string> make_f2fs_cmd = {
256 "/system/bin/make_f2fs",
257 "-g",
258 "android",
259 };
260
261 make_f2fs_cmd.push_back("-O");
262 make_f2fs_cmd.push_back("project_quota,extra_attr");
263
264 if (needs_casefold) {
265 make_f2fs_cmd.push_back("-O");
266 make_f2fs_cmd.push_back("casefold");
267 make_f2fs_cmd.push_back("-C");
268 make_f2fs_cmd.push_back("utf8");
269 }
270 if (v->fs_mgr_flags.fs_compress) {
271 make_f2fs_cmd.push_back("-O");
272 make_f2fs_cmd.push_back("compression");
273 make_f2fs_cmd.push_back("-O");
274 make_f2fs_cmd.push_back("extra_attr");
275 }
276 make_f2fs_cmd.push_back(v->blk_device);
277 if (length >= kSectorSize) {
278 make_f2fs_cmd.push_back(std::to_string(length / kSectorSize));
279 }
280
281 if (exec_cmd(make_f2fs_cmd) != 0) {
282 PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device;
283 return -1;
284 }
285 if (!directory.empty()) {
286 std::vector<std::string> sload_f2fs_cmd = {
287 "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device,
288 };
289 if (exec_cmd(sload_f2fs_cmd) != 0) {
290 PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device;
291 return -1;
292 }
293 }
294 return 0;
295 }
296
format_volume(const std::string & volume)297 int format_volume(const std::string& volume) {
298 return format_volume(volume, "", "");
299 }
300
setup_install_mounts()301 int setup_install_mounts() {
302 if (fstab.empty()) {
303 LOG(ERROR) << "can't set up install mounts: no fstab loaded";
304 return -1;
305 }
306 for (const FstabEntry& entry : fstab) {
307 // We don't want to do anything with "/".
308 if (entry.mount_point == "/") {
309 continue;
310 }
311
312 if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") {
313 if (ensure_path_mounted(entry.mount_point) != 0) {
314 LOG(ERROR) << "Failed to mount " << entry.mount_point;
315 return -1;
316 }
317 } else {
318 if (ensure_path_unmounted(entry.mount_point) != 0) {
319 LOG(ERROR) << "Failed to unmount " << entry.mount_point;
320 return -1;
321 }
322 }
323 }
324 return 0;
325 }
326
HasCache()327 bool HasCache() {
328 CHECK(!fstab.empty());
329 static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
330 return has_cache;
331 }
332