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