Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1 | #include "fs.h" |
| 2 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 3 | |
| 4 | #include <errno.h> |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 5 | #include <fcntl.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 8 | #include <string.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/types.h> |
David Anderson | c3ade7f | 2018-08-23 10:42:02 -0700 | [diff] [blame] | 11 | #ifndef _WIN32 |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 12 | #include <sys/wait.h> |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 13 | #else |
| 14 | #include <tchar.h> |
| 15 | #include <windows.h> |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 16 | #endif |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 17 | #include <unistd.h> |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 18 | #include <vector> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 19 | |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 20 | #include <android-base/errors.h> |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 21 | #include <android-base/file.h> |
Elliott Hughes | 7678ffd | 2018-04-10 18:01:08 -0700 | [diff] [blame] | 22 | #include <android-base/macros.h> |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 24 | #include <android-base/unique_fd.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 25 | |
Jin Qian | 03c73e1 | 2017-12-04 16:17:46 -0800 | [diff] [blame] | 26 | using android::base::GetExecutableDirectory; |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 27 | using android::base::StringPrintf; |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 28 | using android::base::unique_fd; |
| 29 | |
David Anderson | c3ade7f | 2018-08-23 10:42:02 -0700 | [diff] [blame] | 30 | #ifdef _WIN32 |
Jaegeuk Kim | 899ad55 | 2017-11-28 19:26:34 -0800 | [diff] [blame] | 31 | static int exec_cmd(const char* path, const char** argv, const char** envp) { |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 32 | std::string cmd; |
| 33 | int i = 0; |
| 34 | while (argv[i] != nullptr) { |
| 35 | cmd += argv[i++]; |
| 36 | cmd += " "; |
| 37 | } |
| 38 | cmd = cmd.substr(0, cmd.size() - 1); |
| 39 | |
| 40 | STARTUPINFO si; |
| 41 | PROCESS_INFORMATION pi; |
| 42 | DWORD exit_code = 0; |
| 43 | |
| 44 | ZeroMemory(&si, sizeof(si)); |
| 45 | si.cb = sizeof(si); |
| 46 | ZeroMemory(&pi, sizeof(pi)); |
| 47 | |
Jin Qian | 03c73e1 | 2017-12-04 16:17:46 -0800 | [diff] [blame] | 48 | std::string env_str; |
| 49 | if (envp != nullptr) { |
| 50 | while (*envp != nullptr) { |
| 51 | env_str += std::string(*envp) + std::string("\0", 1); |
| 52 | envp++; |
| 53 | } |
| 54 | } |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 55 | |
| 56 | if (!CreateProcessA(nullptr, // No module name (use command line) |
| 57 | const_cast<char*>(cmd.c_str()), // Command line |
| 58 | nullptr, // Process handle not inheritable |
| 59 | nullptr, // Thread handle not inheritable |
| 60 | FALSE, // Set handle inheritance to FALSE |
| 61 | 0, // No creation flags |
Jin Qian | 03c73e1 | 2017-12-04 16:17:46 -0800 | [diff] [blame] | 62 | env_str.empty() ? nullptr : LPSTR(env_str.c_str()), |
| 63 | nullptr, // Use parent's starting directory |
| 64 | &si, // Pointer to STARTUPINFO structure |
| 65 | &pi) // Pointer to PROCESS_INFORMATION structure |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 66 | ) { |
| 67 | fprintf(stderr, "CreateProcess failed: %s\n", |
| 68 | android::base::SystemErrorCodeToString(GetLastError()).c_str()); |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 69 | return -1; |
| 70 | } |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 71 | |
| 72 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 73 | |
| 74 | GetExitCodeProcess(pi.hProcess, &exit_code); |
| 75 | |
| 76 | CloseHandle(pi.hProcess); |
| 77 | CloseHandle(pi.hThread); |
| 78 | |
Jaegeuk Kim | 899ad55 | 2017-11-28 19:26:34 -0800 | [diff] [blame] | 79 | if (exit_code != 0) { |
| 80 | fprintf(stderr, "%s failed: %lu\n", path, exit_code); |
| 81 | return -1; |
| 82 | } |
| 83 | return 0; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 84 | } |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 85 | #else |
Jaegeuk Kim | 899ad55 | 2017-11-28 19:26:34 -0800 | [diff] [blame] | 86 | static int exec_cmd(const char* path, const char** argv, const char** envp) { |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 87 | int status; |
| 88 | pid_t child; |
| 89 | if ((child = fork()) == 0) { |
Jin Qian | 0c04f78 | 2017-12-05 21:39:40 -0800 | [diff] [blame] | 90 | execve(path, const_cast<char**>(argv), const_cast<char**>(envp)); |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 91 | _exit(EXIT_FAILURE); |
| 92 | } |
| 93 | if (child < 0) { |
| 94 | fprintf(stderr, "%s failed with fork %s\n", path, strerror(errno)); |
| 95 | return -1; |
| 96 | } |
| 97 | if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) { |
| 98 | fprintf(stderr, "%s failed with waitpid %s\n", path, strerror(errno)); |
| 99 | return -1; |
| 100 | } |
| 101 | int ret = -1; |
| 102 | if (WIFEXITED(status)) { |
| 103 | ret = WEXITSTATUS(status); |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 104 | } |
Jaegeuk Kim | 899ad55 | 2017-11-28 19:26:34 -0800 | [diff] [blame] | 105 | |
| 106 | if (ret != 0) { |
| 107 | fprintf(stderr, "%s failed with status %d\n", path, ret); |
| 108 | return -1; |
| 109 | } |
| 110 | return 0; |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 111 | } |
Jin Qian | 29fc859 | 2017-07-10 16:04:41 -0700 | [diff] [blame] | 112 | #endif |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 113 | |
Thiébaud Weksteen | 5d72d6c | 2022-07-21 14:43:20 +1000 | [diff] [blame] | 114 | static int generate_ext4_image(const char* fileName, long long partSize, unsigned eraseBlkSize, |
Jaegeuk Kim | 638d05e | 2020-11-09 08:54:13 -0800 | [diff] [blame] | 115 | unsigned logicalBlkSize, const unsigned fsOptions) { |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 116 | static constexpr int block_size = 4096; |
| 117 | const std::string exec_dir = android::base::GetExecutableDirectory(); |
| 118 | |
| 119 | const std::string mke2fs_path = exec_dir + "/mke2fs"; |
| 120 | std::vector<const char*> mke2fs_args = {mke2fs_path.c_str(), "-t", "ext4", "-b"}; |
| 121 | |
| 122 | std::string block_size_str = std::to_string(block_size); |
| 123 | mke2fs_args.push_back(block_size_str.c_str()); |
| 124 | |
| 125 | std::string ext_attr = "android_sparse"; |
| 126 | if (eraseBlkSize != 0 && logicalBlkSize != 0) { |
| 127 | int raid_stride = logicalBlkSize / block_size; |
| 128 | int raid_stripe_width = eraseBlkSize / block_size; |
| 129 | // stride should be the max of 8kb and logical block size |
| 130 | if (logicalBlkSize != 0 && logicalBlkSize < 8192) raid_stride = 8192 / block_size; |
Connor O'Brien | 6ef5c24 | 2017-11-01 17:37:32 -0700 | [diff] [blame] | 131 | // stripe width should be >= stride |
| 132 | if (raid_stripe_width < raid_stride) raid_stripe_width = raid_stride; |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 133 | ext_attr += StringPrintf(",stride=%d,stripe-width=%d", raid_stride, raid_stripe_width); |
| 134 | } |
| 135 | mke2fs_args.push_back("-E"); |
| 136 | mke2fs_args.push_back(ext_attr.c_str()); |
Jin Qian | 99e3964 | 2017-07-27 16:17:17 -0700 | [diff] [blame] | 137 | mke2fs_args.push_back("-O"); |
| 138 | mke2fs_args.push_back("uninit_bg"); |
Jaegeuk Kim | 638d05e | 2020-11-09 08:54:13 -0800 | [diff] [blame] | 139 | |
| 140 | if (fsOptions & (1 << FS_OPT_PROJID)) { |
| 141 | mke2fs_args.push_back("-I"); |
| 142 | mke2fs_args.push_back("512"); |
| 143 | } |
| 144 | |
David Anderson | 6f70cc6 | 2021-07-13 17:31:19 -0700 | [diff] [blame] | 145 | if (fsOptions & (1 << FS_OPT_CASEFOLD)) { |
| 146 | mke2fs_args.push_back("-O"); |
| 147 | mke2fs_args.push_back("casefold"); |
| 148 | mke2fs_args.push_back("-E"); |
| 149 | mke2fs_args.push_back("encoding=utf8"); |
| 150 | } |
| 151 | |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 152 | mke2fs_args.push_back(fileName); |
| 153 | |
| 154 | std::string size_str = std::to_string(partSize / block_size); |
| 155 | mke2fs_args.push_back(size_str.c_str()); |
| 156 | mke2fs_args.push_back(nullptr); |
| 157 | |
Jin Qian | 03c73e1 | 2017-12-04 16:17:46 -0800 | [diff] [blame] | 158 | const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf"; |
| 159 | std::vector<const char*> mke2fs_envp = {mke2fs_env.c_str(), nullptr}; |
| 160 | |
Jaegeuk Kim | 899ad55 | 2017-11-28 19:26:34 -0800 | [diff] [blame] | 161 | int ret = exec_cmd(mke2fs_args[0], mke2fs_args.data(), mke2fs_envp.data()); |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 162 | if (ret != 0) { |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 163 | return -1; |
| 164 | } |
Thiébaud Weksteen | 5d72d6c | 2022-07-21 14:43:20 +1000 | [diff] [blame] | 165 | return 0; |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame] | 166 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 167 | |
Jaegeuk Kim | 58d10c2 | 2020-12-29 11:55:18 -0800 | [diff] [blame] | 168 | enum { |
| 169 | // clang-format off |
| 170 | FSCK_SUCCESS = 0, |
| 171 | FSCK_ERROR_CORRECTED = 1 << 0, |
| 172 | FSCK_SYSTEM_SHOULD_REBOOT = 1 << 1, |
| 173 | FSCK_ERRORS_LEFT_UNCORRECTED = 1 << 2, |
| 174 | FSCK_OPERATIONAL_ERROR = 1 << 3, |
| 175 | FSCK_USAGE_OR_SYNTAX_ERROR = 1 << 4, |
| 176 | FSCK_USER_CANCELLED = 1 << 5, |
| 177 | FSCK_SHARED_LIB_ERROR = 1 << 7, |
| 178 | // clang-format on |
| 179 | }; |
| 180 | |
Thiébaud Weksteen | 5d72d6c | 2022-07-21 14:43:20 +1000 | [diff] [blame] | 181 | static int generate_f2fs_image(const char* fileName, long long partSize, unsigned /* unused */, |
Jaegeuk Kim | 638d05e | 2020-11-09 08:54:13 -0800 | [diff] [blame] | 182 | unsigned /* unused */, const unsigned fsOptions) { |
Jaegeuk Kim | d872118 | 2017-10-02 18:51:48 -0700 | [diff] [blame] | 183 | const std::string exec_dir = android::base::GetExecutableDirectory(); |
| 184 | const std::string mkf2fs_path = exec_dir + "/make_f2fs"; |
| 185 | std::vector<const char*> mkf2fs_args = {mkf2fs_path.c_str()}; |
| 186 | |
| 187 | mkf2fs_args.push_back("-S"); |
| 188 | std::string size_str = std::to_string(partSize); |
| 189 | mkf2fs_args.push_back(size_str.c_str()); |
Jaegeuk Kim | 46542f9 | 2018-11-21 12:55:19 -0800 | [diff] [blame] | 190 | mkf2fs_args.push_back("-g"); |
| 191 | mkf2fs_args.push_back("android"); |
Jaegeuk Kim | 638d05e | 2020-11-09 08:54:13 -0800 | [diff] [blame] | 192 | |
| 193 | if (fsOptions & (1 << FS_OPT_PROJID)) { |
| 194 | mkf2fs_args.push_back("-O"); |
| 195 | mkf2fs_args.push_back("project_quota,extra_attr"); |
| 196 | } |
| 197 | |
| 198 | if (fsOptions & (1 << FS_OPT_CASEFOLD)) { |
| 199 | mkf2fs_args.push_back("-O"); |
| 200 | mkf2fs_args.push_back("casefold"); |
| 201 | mkf2fs_args.push_back("-C"); |
| 202 | mkf2fs_args.push_back("utf8"); |
| 203 | } |
| 204 | |
| 205 | if (fsOptions & (1 << FS_OPT_COMPRESS)) { |
| 206 | mkf2fs_args.push_back("-O"); |
| 207 | mkf2fs_args.push_back("compression"); |
| 208 | mkf2fs_args.push_back("-O"); |
| 209 | mkf2fs_args.push_back("extra_attr"); |
| 210 | } |
| 211 | |
Jaegeuk Kim | d872118 | 2017-10-02 18:51:48 -0700 | [diff] [blame] | 212 | mkf2fs_args.push_back(fileName); |
| 213 | mkf2fs_args.push_back(nullptr); |
| 214 | |
Jaegeuk Kim | 899ad55 | 2017-11-28 19:26:34 -0800 | [diff] [blame] | 215 | int ret = exec_cmd(mkf2fs_args[0], mkf2fs_args.data(), nullptr); |
Jaegeuk Kim | d872118 | 2017-10-02 18:51:48 -0700 | [diff] [blame] | 216 | if (ret != 0) { |
Jaegeuk Kim | d872118 | 2017-10-02 18:51:48 -0700 | [diff] [blame] | 217 | return -1; |
| 218 | } |
Jaegeuk Kim | 58d10c2 | 2020-12-29 11:55:18 -0800 | [diff] [blame] | 219 | return 0; |
Jaegeuk Kim | 8d9b6ee | 2017-11-15 10:55:07 -0800 | [diff] [blame] | 220 | } |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 221 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 222 | static const struct fs_generator { |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 223 | const char* fs_type; //must match what fastboot reports for partition type |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 224 | |
| 225 | //returns 0 or error value |
Thiébaud Weksteen | 5d72d6c | 2022-07-21 14:43:20 +1000 | [diff] [blame] | 226 | int (*generate)(const char* fileName, long long partSize, unsigned eraseBlkSize, |
| 227 | unsigned logicalBlkSize, const unsigned fsOptions); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 228 | |
| 229 | } generators[] = { |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 230 | { "ext4", generate_ext4_image}, |
| 231 | { "f2fs", generate_f2fs_image}, |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 232 | }; |
| 233 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 234 | const struct fs_generator* fs_get_generator(const std::string& fs_type) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 235 | for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 236 | if (fs_type == generators[i].fs_type) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 237 | return generators + i; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | return nullptr; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 243 | int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize, |
Thiébaud Weksteen | 5d72d6c | 2022-07-21 14:43:20 +1000 | [diff] [blame] | 244 | unsigned eraseBlkSize, unsigned logicalBlkSize, |
| 245 | const unsigned fsOptions) { |
| 246 | return gen->generate(fileName, partSize, eraseBlkSize, logicalBlkSize, fsOptions); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 247 | } |