The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 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 | |
Tao Bao | e3f09a7 | 2019-10-01 11:55:36 -0700 | [diff] [blame] | 17 | #include "recovery_utils/roots.h" |
Elliott Hughes | 63a3192 | 2016-06-09 17:41:22 -0700 | [diff] [blame] | 18 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 20 | #include <stdint.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
Tao Bao | ad774b2 | 2017-09-29 10:39:08 -0700 | [diff] [blame] | 22 | #include <string.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 26 | #include <unistd.h> |
Simon Shields | eb476a0 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 27 | #include <sys/mount.h> |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 28 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 29 | #include <iostream> |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 33 | #include <android-base/logging.h> |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 34 | #include <android-base/properties.h> |
Jin Qian | ded2dac | 2017-04-21 14:36:12 -0700 | [diff] [blame] | 35 | #include <android-base/stringprintf.h> |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 36 | #include <android-base/unique_fd.h> |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 37 | #include <blkid/blkid.h> |
Eric Biggers | a762e14 | 2021-11-08 16:45:49 -0800 | [diff] [blame] | 38 | #include <ext4_utils/ext4_utils.h> |
Jaegeuk Kim | 80a1d8e | 2021-11-30 18:39:31 -0800 | [diff] [blame] | 39 | #include <ext4_utils/wipe.h> |
Ken Sumrall | f35d1ce | 2013-02-13 12:59:35 -0800 | [diff] [blame] | 40 | #include <fs_mgr.h> |
Yifan Hong | 0f339e2 | 2018-12-03 13:44:01 -0800 | [diff] [blame] | 41 | #include <fs_mgr/roots.h> |
Erfan Abdi | f40ff08 | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 42 | #include <fs_mgr_dm_linear.h> |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 43 | #include <fstab/fstab.h> |
Tao Bao | de40ba5 | 2016-10-05 23:17:01 -0700 | [diff] [blame] | 44 | |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 45 | #include "otautil/sysutil.h" |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 46 | |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 47 | using android::fs_mgr::Fstab; |
| 48 | using android::fs_mgr::FstabEntry; |
| 49 | using android::fs_mgr::ReadDefaultFstab; |
aleasto | 157f92d | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 50 | using android::dm::DeviceMapper; |
| 51 | using android::dm::DmDeviceState; |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 52 | |
Steve Kondik | 50ac1c8 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 53 | static void write_fstab_entry(const FstabEntry& entry, FILE* file) { |
| 54 | if (entry.fs_type != "emmc" && !entry.fs_mgr_flags.vold_managed && !entry.blk_device.empty() && |
| 55 | entry.blk_device[0] == '/' && !entry.mount_point.empty() && entry.mount_point[0] == '/') { |
| 56 | fprintf(file, "%s ", entry.blk_device.c_str()); |
| 57 | fprintf(file, "%s ", entry.mount_point.c_str()); |
| 58 | fprintf(file, "%s ", entry.fs_type.c_str()); |
| 59 | fprintf(file, "%s 0 0\n", !entry.fs_options.empty() ? entry.fs_options.c_str() : "defaults"); |
| 60 | } |
| 61 | } |
| 62 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 63 | static Fstab fstab; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 64 | |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 65 | constexpr const char* CACHE_ROOT = "/cache"; |
| 66 | |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 67 | FstabEntry* fstab_entry_for_mount_point_detect_fs(const std::string& path) { |
| 68 | FstabEntry* found = android::fs_mgr::GetEntryForMountPoint(&fstab, path); |
| 69 | if (found == nullptr) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | |
| 73 | if (char* detected_fs_type = blkid_get_tag_value(nullptr, "TYPE", found->blk_device.c_str())) { |
| 74 | for (auto& entry : fstab) { |
| 75 | if (entry.mount_point == path && entry.fs_type == detected_fs_type) { |
| 76 | found = &entry; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | free(detected_fs_type); |
| 81 | } |
| 82 | |
| 83 | return found; |
| 84 | } |
| 85 | |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 86 | void load_volume_table() { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 87 | if (!ReadDefaultFstab(&fstab)) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 88 | LOG(ERROR) << "Failed to read default fstab"; |
| 89 | return; |
| 90 | } |
Doug Zongker | 2810ced | 2011-02-17 15:55:21 -0800 | [diff] [blame] | 91 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 92 | fstab.emplace_back(FstabEntry{ |
Nick Desaulniers | 1cb510d | 2019-10-10 16:33:58 -0700 | [diff] [blame] | 93 | .blk_device = "ramdisk", |
| 94 | .mount_point = "/tmp", |
| 95 | .fs_type = "ramdisk", |
| 96 | .length = 0, |
| 97 | }); |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 98 | |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 99 | Fstab fake_fstab; |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 100 | std::cout << "recovery filesystem table" << std::endl << "=========================" << std::endl; |
| 101 | for (size_t i = 0; i < fstab.size(); ++i) { |
| 102 | const auto& entry = fstab[i]; |
| 103 | std::cout << " " << i << " " << entry.mount_point << " " |
| 104 | << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length |
| 105 | << std::endl; |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 106 | |
| 107 | if (std::find_if(fake_fstab.begin(), fake_fstab.end(), [entry](const FstabEntry& e) { |
| 108 | return entry.mount_point == e.mount_point; |
| 109 | }) == fake_fstab.end()) { |
| 110 | FstabEntry* entry_detectfs = fstab_entry_for_mount_point_detect_fs(entry.mount_point); |
| 111 | if (entry_detectfs == &entry) { |
| 112 | fake_fstab.emplace_back(entry); |
| 113 | } |
Steve Kondik | 50ac1c8 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 114 | } |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 115 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 116 | std::cout << std::endl; |
Steve Kondik | 50ac1c8 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 117 | |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 118 | // Create a boring /etc/fstab so tools like Busybox work |
| 119 | FILE* file = fopen("/etc/fstab", "w"); |
Steve Kondik | 50ac1c8 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 120 | if (file) { |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 121 | for (auto& entry : fake_fstab) { |
| 122 | write_fstab_entry(entry, file); |
| 123 | } |
Steve Kondik | 50ac1c8 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 124 | fclose(file); |
Alessandro Astone | 9062a0c | 2020-03-01 13:33:27 +0100 | [diff] [blame] | 125 | } else { |
| 126 | LOG(ERROR) << "Unable to create /etc/fstab"; |
Steve Kondik | 50ac1c8 | 2013-11-24 21:40:09 -0800 | [diff] [blame] | 127 | } |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 130 | Volume* volume_for_mount_point(const std::string& mount_point) { |
Tom Cherry | 72a114a | 2019-01-30 15:59:53 -0800 | [diff] [blame] | 131 | return android::fs_mgr::GetEntryForMountPoint(&fstab, mount_point); |
Tao Bao | 3e18f2b | 2017-09-29 17:11:13 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 134 | // Mount the volume specified by path at the given mount_point. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 135 | int ensure_path_mounted_at(const std::string& path, const std::string& mount_point) { |
| 136 | return android::fs_mgr::EnsurePathMounted(&fstab, path, mount_point) ? 0 : -1; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 139 | int ensure_path_mounted(const std::string& path) { |
Tao Bao | bb10e58 | 2017-07-22 16:30:34 -0700 | [diff] [blame] | 140 | // Mount at the default mount point. |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 141 | return android::fs_mgr::EnsurePathMounted(&fstab, path) ? 0 : -1; |
Tao Bao | abb8f77 | 2015-07-30 14:43:27 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 144 | int ensure_path_unmounted(const std::string& path) { |
| 145 | return android::fs_mgr::EnsurePathUnmounted(&fstab, path) ? 0 : -1; |
Doug Zongker | d4208f9 | 2010-09-20 12:16:13 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Simon Shields | eb476a0 | 2019-10-02 00:21:45 +1000 | [diff] [blame] | 148 | int ensure_volume_unmounted(const std::string& blk_device) { |
| 149 | android::fs_mgr::Fstab mounted_fstab; |
| 150 | if (!android::fs_mgr::ReadFstabFromFile("/proc/mounts", &mounted_fstab)) { |
| 151 | LOG(ERROR) << "Failed to read /proc/mounts"; |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | /* find any entries with the volume */ |
| 156 | for (auto& entry : mounted_fstab) { |
| 157 | if (entry.blk_device == blk_device) { |
| 158 | int result = umount(entry.mount_point.c_str()); |
| 159 | if (result == -1) { |
| 160 | LOG(ERROR) << "Failed to unmount " << blk_device << " from " << entry.mount_point << ": " |
| 161 | << errno; |
| 162 | return -1; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 169 | static int exec_cmd(const std::vector<std::string>& args) { |
Tao Bao | 3d69f0d | 2018-12-20 09:44:06 -0800 | [diff] [blame] | 170 | CHECK(!args.empty()); |
| 171 | auto argv = StringVectorToNullTerminatedArray(args); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 172 | |
| 173 | pid_t child; |
George Burgess IV | 1cfb361 | 2018-02-17 17:48:45 -0800 | [diff] [blame] | 174 | if ((child = fork()) == 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 175 | execv(argv[0], argv.data()); |
| 176 | _exit(EXIT_FAILURE); |
| 177 | } |
| 178 | |
| 179 | int status; |
| 180 | waitpid(child, &status, 0); |
| 181 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 182 | LOG(ERROR) << args[0] << " failed with status " << WEXITSTATUS(status); |
| 183 | } |
| 184 | return WEXITSTATUS(status); |
JP Abgrall | 37aedb3 | 2014-06-16 19:07:39 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 187 | static int64_t get_file_size(int fd, uint64_t reserve_len) { |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 188 | struct stat buf; |
| 189 | int ret = fstat(fd, &buf); |
| 190 | if (ret) return 0; |
| 191 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 192 | int64_t computed_size; |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 193 | if (S_ISREG(buf.st_mode)) { |
| 194 | computed_size = buf.st_size - reserve_len; |
| 195 | } else if (S_ISBLK(buf.st_mode)) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 196 | uint64_t block_device_size = get_block_device_size(fd); |
| 197 | if (block_device_size < reserve_len || |
| 198 | block_device_size > std::numeric_limits<int64_t>::max()) { |
| 199 | computed_size = 0; |
| 200 | } else { |
| 201 | computed_size = block_device_size - reserve_len; |
| 202 | } |
Jin Qian | f3ccad5 | 2017-07-24 10:34:35 -0700 | [diff] [blame] | 203 | } else { |
| 204 | computed_size = 0; |
| 205 | } |
| 206 | |
| 207 | return computed_size; |
| 208 | } |
| 209 | |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 210 | static FstabEntry* LocateFormattableEntry(const std::vector<FstabEntry*>& entries) { |
| 211 | if (entries.empty()) { |
| 212 | return nullptr; |
| 213 | } |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 214 | FstabEntry* f2fs_entry = nullptr; |
| 215 | for (auto&& entry : entries) { |
| 216 | if (getpagesize() != 4096 && entry->fs_type == "f2fs") { |
| 217 | f2fs_entry = entry; |
| 218 | continue; |
| 219 | } |
| 220 | if (f2fs_entry) { |
| 221 | LOG(INFO) << "Skipping F2FS format for block device " << entry->blk_device << " @ " |
| 222 | << entry->mount_point |
| 223 | << " in non-4K mode for dev option enabled devices, " |
| 224 | "as these devices need to toggle between 4K/16K mode, and F2FS does " |
| 225 | "not support page_size != block_size configuration."; |
| 226 | } |
| 227 | return entry; |
| 228 | } |
| 229 | if (f2fs_entry) { |
| 230 | LOG(INFO) << "Using F2FS for " << f2fs_entry->blk_device << " @ " << f2fs_entry->mount_point |
| 231 | << " even though we are in non-4K mode. Device might require a data wipe after " |
| 232 | "going back to 4K mode, as F2FS does not support page_size != block_size"; |
| 233 | } |
| 234 | return f2fs_entry; |
| 235 | } |
| 236 | |
| 237 | bool WipeBlockDevice(const char* path) { |
| 238 | android::base::unique_fd fd(open(path, O_RDWR)); |
| 239 | if (fd == -1) { |
| 240 | PLOG(ERROR) << "WipeBlockDevice: failed to open " << path; |
| 241 | return false; |
| 242 | } |
| 243 | int64_t device_size = get_file_size(fd.get(), 0); |
| 244 | if (device_size < 0) { |
| 245 | PLOG(ERROR) << "WipeBlockDevice: failed to determine size of " << device_size; |
| 246 | return false; |
| 247 | } |
| 248 | if (device_size == 0) { |
| 249 | PLOG(ERROR) << "WipeBlockDevice: block device " << device_size << " has 0 length, skip wiping"; |
| 250 | return false; |
| 251 | } |
| 252 | if (!wipe_block_device(fd.get(), device_size)) { |
| 253 | return true; |
| 254 | } |
| 255 | PLOG(ERROR) << "Failed to wipe " << path; |
| 256 | return false; |
| 257 | } |
| 258 | |
Kelvin Zhang | 7354994 | 2024-01-04 16:43:53 -0800 | [diff] [blame] | 259 | int format_volume(const std::string& volume, const std::string& directory, |
| 260 | std::string_view new_fstype) { |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 261 | const auto entries = android::fs_mgr::GetEntriesForPath(&fstab, volume); |
| 262 | if (entries.empty()) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 263 | LOG(ERROR) << "unknown volume \"" << volume << "\""; |
| 264 | return -1; |
| 265 | } |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 266 | |
| 267 | const FstabEntry* v = LocateFormattableEntry(entries); |
| 268 | if (v == nullptr) { |
| 269 | LOG(ERROR) << "Unable to find formattable entry for \"" << volume << "\""; |
| 270 | return -1; |
| 271 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 272 | if (v->fs_type == "ramdisk") { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 273 | LOG(ERROR) << "can't format_volume \"" << volume << "\""; |
| 274 | return -1; |
| 275 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 276 | if (v->mount_point != volume) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 277 | LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume"; |
| 278 | return -1; |
| 279 | } |
Alessandro Astone | f32ae70 | 2020-03-30 00:01:21 +0200 | [diff] [blame] | 280 | if (ensure_volume_unmounted(v->blk_device) != 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 281 | LOG(ERROR) << "format_volume: Failed to unmount \"" << v->mount_point << "\""; |
| 282 | return -1; |
| 283 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 284 | if (v->fs_type != "ext4" && v->fs_type != "f2fs") { |
Tianjie Xu | 7b0ad9c | 2016-08-05 18:00:04 -0700 | [diff] [blame] | 285 | LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported"; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 286 | return -1; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 289 | bool needs_casefold = false; |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 290 | |
| 291 | if (volume == "/data") { |
Martijn Coenen | 5a4a7ff | 2020-04-15 11:52:21 +0200 | [diff] [blame] | 292 | needs_casefold = android::base::GetBoolProperty("external_storage.casefold.enabled", false); |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 295 | int64_t length = 0; |
Jin Qian | cc10008 | 2017-11-17 23:53:22 -0800 | [diff] [blame] | 296 | if (v->length > 0) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 297 | length = v->length; |
Eric Biggers | a762e14 | 2021-11-08 16:45:49 -0800 | [diff] [blame] | 298 | } else if (v->length < 0) { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 299 | android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDONLY)); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 300 | if (fd == -1) { |
Abhishek Arpure | 4fec8e9 | 2017-08-24 15:27:16 +0530 | [diff] [blame] | 301 | PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 302 | return -1; |
| 303 | } |
Eric Biggers | a762e14 | 2021-11-08 16:45:49 -0800 | [diff] [blame] | 304 | length = get_file_size(fd.get(), -v->length); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 305 | if (length <= 0) { |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 306 | LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 307 | return -1; |
| 308 | } |
| 309 | } |
| 310 | |
Jaegeuk Kim | 80a1d8e | 2021-11-30 18:39:31 -0800 | [diff] [blame] | 311 | // If the raw disk will be used as a metadata encrypted device mapper target, |
Kelvin Zhang | aa0a88d | 2024-01-13 21:25:15 +0000 | [diff] [blame] | 312 | // next boot will do encrypt_in_place the raw disk. While fs_mgr mounts /data |
| 313 | // as RO to avoid write file operations before encrypt_inplace, this code path |
| 314 | // is not well tested so we would like to avoid it if possible. For safety, |
| 315 | // let vold do the formatting on boot for metadata encrypted devices, except |
| 316 | // when user specified a new fstype. Because init formats /data according |
| 317 | // to fstab, it's difficult to override the fstab in init. |
| 318 | if (!v->metadata_key_dir.empty() && length == 0 && new_fstype.empty()) { |
| 319 | android::base::unique_fd fd(open(v->blk_device.c_str(), O_RDWR)); |
| 320 | if (fd == -1) { |
| 321 | PLOG(ERROR) << "format_volume: failed to open " << v->blk_device; |
| 322 | return -1; |
| 323 | } |
| 324 | int64_t device_size = get_file_size(fd.get(), 0); |
| 325 | if (device_size > 0 && !wipe_block_device(fd.get(), device_size)) { |
| 326 | LOG(INFO) << "format_volume: wipe metadata encrypted " << v->blk_device << " with size " |
| 327 | << device_size; |
| 328 | return 0; |
| 329 | } |
| 330 | } |
Jaegeuk Kim | 80a1d8e | 2021-11-30 18:39:31 -0800 | [diff] [blame] | 331 | |
Dan Shi | c89b4e4 | 2024-01-11 02:46:37 +0000 | [diff] [blame] | 332 | if ((v->fs_type == "ext4" && new_fstype.empty()) || new_fstype == "ext4") { |
Kelvin Zhang | 7354994 | 2024-01-04 16:43:53 -0800 | [diff] [blame] | 333 | LOG(INFO) << "Formatting " << v->blk_device << " as ext4"; |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 334 | static constexpr int kBlockSize = 4096; |
| 335 | std::vector<std::string> mke2fs_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 336 | "/system/bin/mke2fs", "-F", "-t", "ext4", "-b", std::to_string(kBlockSize), |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 337 | }; |
| 338 | |
Shikha Malhotra | d21e516 | 2022-02-02 13:43:39 +0000 | [diff] [blame] | 339 | // Following is added for Project ID's quota as they require wider inodes. |
| 340 | // The Quotas themselves are enabled by tune2fs on boot. |
| 341 | mke2fs_args.push_back("-I"); |
| 342 | mke2fs_args.push_back("512"); |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 343 | |
Jaegeuk Kim | a8d36e1 | 2020-02-11 15:24:54 -0800 | [diff] [blame] | 344 | if (v->fs_mgr_flags.ext_meta_csum) { |
| 345 | mke2fs_args.push_back("-O"); |
| 346 | mke2fs_args.push_back("metadata_csum"); |
| 347 | mke2fs_args.push_back("-O"); |
| 348 | mke2fs_args.push_back("64bit"); |
| 349 | mke2fs_args.push_back("-O"); |
| 350 | mke2fs_args.push_back("extent"); |
| 351 | } |
| 352 | |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 353 | int raid_stride = v->logical_blk_size / kBlockSize; |
| 354 | int raid_stripe_width = v->erase_blk_size / kBlockSize; |
| 355 | // stride should be the max of 8KB and logical block size |
| 356 | if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) { |
| 357 | raid_stride = 8192 / kBlockSize; |
| 358 | } |
| 359 | if (v->erase_blk_size != 0 && v->logical_blk_size != 0) { |
| 360 | mke2fs_args.push_back("-E"); |
| 361 | mke2fs_args.push_back( |
| 362 | android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride, raid_stripe_width)); |
| 363 | } |
| 364 | mke2fs_args.push_back(v->blk_device); |
| 365 | if (length != 0) { |
| 366 | mke2fs_args.push_back(std::to_string(length / kBlockSize)); |
| 367 | } |
| 368 | |
| 369 | int result = exec_cmd(mke2fs_args); |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 370 | if (result == 0 && !directory.empty()) { |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 371 | std::vector<std::string> e2fsdroid_args = { |
Jiyong Park | 69364fe | 2018-06-20 14:18:18 +0900 | [diff] [blame] | 372 | "/system/bin/e2fsdroid", "-e", "-f", directory, "-a", volume, v->blk_device, |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 373 | }; |
| 374 | result = exec_cmd(e2fsdroid_args); |
| 375 | } |
| 376 | |
| 377 | if (result != 0) { |
| 378 | PLOG(ERROR) << "format_volume: Failed to make ext4 on " << v->blk_device; |
| 379 | return -1; |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | // Has to be f2fs because we checked earlier. |
Kelvin Zhang | 7354994 | 2024-01-04 16:43:53 -0800 | [diff] [blame] | 385 | LOG(INFO) << "Formatting " << v->blk_device << " as f2fs"; |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 386 | static constexpr int kSectorSize = 4096; |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 387 | std::vector<std::string> make_f2fs_cmd = { |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 388 | "/system/bin/make_f2fs", |
| 389 | "-g", |
| 390 | "android", |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 391 | }; |
Shikha Malhotra | d21e516 | 2022-02-02 13:43:39 +0000 | [diff] [blame] | 392 | |
| 393 | make_f2fs_cmd.push_back("-O"); |
| 394 | make_f2fs_cmd.push_back("project_quota,extra_attr"); |
| 395 | |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 396 | if (needs_casefold) { |
| 397 | make_f2fs_cmd.push_back("-O"); |
| 398 | make_f2fs_cmd.push_back("casefold"); |
| 399 | make_f2fs_cmd.push_back("-C"); |
| 400 | make_f2fs_cmd.push_back("utf8"); |
| 401 | } |
Jaegeuk Kim | 3210600 | 2020-01-14 11:00:37 -0800 | [diff] [blame] | 402 | if (v->fs_mgr_flags.fs_compress) { |
| 403 | make_f2fs_cmd.push_back("-O"); |
| 404 | make_f2fs_cmd.push_back("compression"); |
| 405 | make_f2fs_cmd.push_back("-O"); |
| 406 | make_f2fs_cmd.push_back("extra_attr"); |
| 407 | } |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 408 | make_f2fs_cmd.push_back("-b"); |
| 409 | make_f2fs_cmd.push_back(std::to_string(getpagesize())); |
Daniel Rosenberg | f25b977 | 2019-12-16 18:32:00 -0800 | [diff] [blame] | 410 | make_f2fs_cmd.push_back(v->blk_device); |
Jaegeuk Kim | 4358262 | 2018-04-02 13:37:35 -0700 | [diff] [blame] | 411 | if (length >= kSectorSize) { |
| 412 | make_f2fs_cmd.push_back(std::to_string(length / kSectorSize)); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 415 | if (exec_cmd(make_f2fs_cmd) != 0) { |
Kelvin Zhang | 4b240f1 | 2024-07-30 11:09:52 -0700 | [diff] [blame] | 416 | PLOG(ERROR) << "format_volume: Failed to make_f2fs on " << v->blk_device |
| 417 | << " wiping the block device to avoid leaving partially formatted data."; |
| 418 | WipeBlockDevice(v->blk_device.c_str()); |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 419 | return -1; |
| 420 | } |
Tao Bao | c674dfb | 2018-12-20 14:25:15 -0800 | [diff] [blame] | 421 | if (!directory.empty()) { |
| 422 | std::vector<std::string> sload_f2fs_cmd = { |
| 423 | "/system/bin/sload_f2fs", "-f", directory, "-t", volume, v->blk_device, |
| 424 | }; |
| 425 | if (exec_cmd(sload_f2fs_cmd) != 0) { |
| 426 | PLOG(ERROR) << "format_volume: Failed to sload_f2fs on " << v->blk_device; |
| 427 | return -1; |
| 428 | } |
| 429 | } |
Tao Bao | 3c00fac | 2017-07-22 16:46:54 -0700 | [diff] [blame] | 430 | return 0; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 431 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 432 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 433 | int format_volume(const std::string& volume) { |
Kelvin Zhang | 7354994 | 2024-01-04 16:43:53 -0800 | [diff] [blame] | 434 | return format_volume(volume, "", ""); |
Paul Lawrence | d0db337 | 2015-11-05 13:38:40 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 437 | int setup_install_mounts() { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 438 | if (fstab.empty()) { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 439 | LOG(ERROR) << "can't set up install mounts: no fstab loaded"; |
| 440 | return -1; |
| 441 | } |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 442 | for (const FstabEntry& entry : fstab) { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 443 | // We don't want to do anything with "/". |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 444 | if (entry.mount_point == "/") { |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 445 | continue; |
| 446 | } |
| 447 | |
Yumi Yukimura | 7ebecd0 | 2024-08-04 00:58:07 +0800 | [diff] [blame] | 448 | // We may load update package from virtiofs mount point. |
| 449 | if (entry.mount_point == "/mnt/vendor/shared") { |
| 450 | continue; |
| 451 | } |
| 452 | |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 453 | if (entry.mount_point == "/tmp" || entry.mount_point == "/cache") { |
| 454 | if (ensure_path_mounted(entry.mount_point) != 0) { |
| 455 | LOG(ERROR) << "Failed to mount " << entry.mount_point; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 456 | return -1; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 457 | } |
| 458 | } else { |
Yifan Hong | d81b8e3 | 2018-12-17 14:29:06 -0800 | [diff] [blame] | 459 | if (ensure_path_unmounted(entry.mount_point) != 0) { |
| 460 | LOG(ERROR) << "Failed to unmount " << entry.mount_point; |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 461 | return -1; |
| 462 | } |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 463 | } |
Tao Bao | 57130c4 | 2017-05-10 12:11:21 -0700 | [diff] [blame] | 464 | } |
| 465 | return 0; |
Doug Zongker | 239ac6a | 2013-08-20 16:03:25 -0700 | [diff] [blame] | 466 | } |
Tianjie Xu | 164c60a | 2019-05-15 13:59:39 -0700 | [diff] [blame] | 467 | |
| 468 | bool HasCache() { |
| 469 | CHECK(!fstab.empty()); |
| 470 | static bool has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr; |
| 471 | return has_cache; |
| 472 | } |
Erfan Abdi | f40ff08 | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 473 | |
aleasto | 157f92d | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 474 | static bool logical_partitions_auto_mapped = false; |
| 475 | |
| 476 | void map_logical_partitions() { |
| 477 | if (android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) && |
| 478 | !logical_partitions_mapped()) { |
| 479 | std::string super_name = fs_mgr_get_super_partition_name(); |
| 480 | if (!android::fs_mgr::CreateLogicalPartitions("/dev/block/by-name/" + super_name)) { |
| 481 | LOG(ERROR) << "Failed to map logical partitions"; |
| 482 | } else { |
| 483 | logical_partitions_auto_mapped = true; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | bool dm_find_system() { |
| 489 | auto rec = GetEntryForPath(&fstab, android::fs_mgr::GetSystemRoot()); |
| 490 | if (!rec->fs_mgr_flags.logical) { |
| 491 | return false; |
| 492 | } |
| 493 | // If the fstab entry for system it's a path instead of a name, then it was already mapped |
| 494 | if (rec->blk_device[0] != '/') { |
| 495 | if (DeviceMapper::Instance().GetState(rec->blk_device) == DmDeviceState::INVALID) { |
| 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | return true; |
| 500 | } |
| 501 | |
Erfan Abdi | f40ff08 | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 502 | bool logical_partitions_mapped() { |
aleasto | 157f92d | 2021-01-17 20:31:22 +0100 | [diff] [blame] | 503 | return android::fs_mgr::LogicalPartitionsMapped() || logical_partitions_auto_mapped || |
| 504 | dm_find_system(); |
Erfan Abdi | f40ff08 | 2020-04-14 23:07:33 +0430 | [diff] [blame] | 505 | } |