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