blob: d268a502c31fadc9a2d81bccbbcc751ac263139b [file] [log] [blame]
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001#include "fs.h"
2
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07003
4#include <errno.h>
Jin Qian4a335822017-04-18 16:23:18 -07005#include <fcntl.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07006#include <stdio.h>
7#include <stdlib.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07008#include <string.h>
9#include <sys/stat.h>
10#include <sys/types.h>
David Andersonc3ade7f2018-08-23 10:42:02 -070011#ifndef _WIN32
Jin Qian4afba662017-04-18 18:19:12 -070012#include <sys/wait.h>
Jin Qian29fc8592017-07-10 16:04:41 -070013#else
14#include <tchar.h>
15#include <windows.h>
Jin Qian4afba662017-04-18 18:19:12 -070016#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070017#include <unistd.h>
Jin Qian4afba662017-04-18 18:19:12 -070018#include <vector>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070019
Jin Qian29fc8592017-07-10 16:04:41 -070020#include <android-base/errors.h>
Jin Qian4afba662017-04-18 18:19:12 -070021#include <android-base/file.h>
Elliott Hughes7678ffd2018-04-10 18:01:08 -070022#include <android-base/macros.h>
Jin Qian4afba662017-04-18 18:19:12 -070023#include <android-base/stringprintf.h>
Jin Qian4a335822017-04-18 16:23:18 -070024#include <android-base/unique_fd.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070025
Jin Qian03c73e12017-12-04 16:17:46 -080026using android::base::GetExecutableDirectory;
Jin Qian4afba662017-04-18 18:19:12 -070027using android::base::StringPrintf;
Jin Qian4a335822017-04-18 16:23:18 -070028using android::base::unique_fd;
29
David Andersonc3ade7f2018-08-23 10:42:02 -070030#ifdef _WIN32
Jaegeuk Kim899ad552017-11-28 19:26:34 -080031static int exec_cmd(const char* path, const char** argv, const char** envp) {
Jin Qian29fc8592017-07-10 16:04:41 -070032 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 Qian03c73e12017-12-04 16:17:46 -080048 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 Qian29fc8592017-07-10 16:04:41 -070055
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 Qian03c73e12017-12-04 16:17:46 -080062 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 Qian29fc8592017-07-10 16:04:41 -070066 ) {
67 fprintf(stderr, "CreateProcess failed: %s\n",
68 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Jin Qian4a335822017-04-18 16:23:18 -070069 return -1;
70 }
Jin Qian29fc8592017-07-10 16:04:41 -070071
72 WaitForSingleObject(pi.hProcess, INFINITE);
73
74 GetExitCodeProcess(pi.hProcess, &exit_code);
75
76 CloseHandle(pi.hProcess);
77 CloseHandle(pi.hThread);
78
Jaegeuk Kim899ad552017-11-28 19:26:34 -080079 if (exit_code != 0) {
80 fprintf(stderr, "%s failed: %lu\n", path, exit_code);
81 return -1;
82 }
83 return 0;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070084}
Jin Qian4afba662017-04-18 18:19:12 -070085#else
Jaegeuk Kim899ad552017-11-28 19:26:34 -080086static int exec_cmd(const char* path, const char** argv, const char** envp) {
Jin Qian4afba662017-04-18 18:19:12 -070087 int status;
88 pid_t child;
89 if ((child = fork()) == 0) {
Jin Qian0c04f782017-12-05 21:39:40 -080090 execve(path, const_cast<char**>(argv), const_cast<char**>(envp));
Jin Qian4afba662017-04-18 18:19:12 -070091 _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 Qian4afba662017-04-18 18:19:12 -0700104 }
Jaegeuk Kim899ad552017-11-28 19:26:34 -0800105
106 if (ret != 0) {
107 fprintf(stderr, "%s failed with status %d\n", path, ret);
108 return -1;
109 }
110 return 0;
Jin Qian4afba662017-04-18 18:19:12 -0700111}
Jin Qian29fc8592017-07-10 16:04:41 -0700112#endif
Jin Qian4afba662017-04-18 18:19:12 -0700113
114static int generate_ext4_image(const char* fileName, long long partSize,
115 const std::string& initial_dir, unsigned eraseBlkSize,
Jaegeuk Kim638d05e2020-11-09 08:54:13 -0800116 unsigned logicalBlkSize, const unsigned fsOptions) {
Jin Qian4afba662017-04-18 18:19:12 -0700117 static constexpr int block_size = 4096;
118 const std::string exec_dir = android::base::GetExecutableDirectory();
119
120 const std::string mke2fs_path = exec_dir + "/mke2fs";
121 std::vector<const char*> mke2fs_args = {mke2fs_path.c_str(), "-t", "ext4", "-b"};
122
123 std::string block_size_str = std::to_string(block_size);
124 mke2fs_args.push_back(block_size_str.c_str());
125
126 std::string ext_attr = "android_sparse";
127 if (eraseBlkSize != 0 && logicalBlkSize != 0) {
128 int raid_stride = logicalBlkSize / block_size;
129 int raid_stripe_width = eraseBlkSize / block_size;
130 // stride should be the max of 8kb and logical block size
131 if (logicalBlkSize != 0 && logicalBlkSize < 8192) raid_stride = 8192 / block_size;
Connor O'Brien6ef5c242017-11-01 17:37:32 -0700132 // stripe width should be >= stride
133 if (raid_stripe_width < raid_stride) raid_stripe_width = raid_stride;
Jin Qian4afba662017-04-18 18:19:12 -0700134 ext_attr += StringPrintf(",stride=%d,stripe-width=%d", raid_stride, raid_stripe_width);
135 }
136 mke2fs_args.push_back("-E");
137 mke2fs_args.push_back(ext_attr.c_str());
Jin Qian99e39642017-07-27 16:17:17 -0700138 mke2fs_args.push_back("-O");
139 mke2fs_args.push_back("uninit_bg");
Jaegeuk Kim638d05e2020-11-09 08:54:13 -0800140
141 if (fsOptions & (1 << FS_OPT_PROJID)) {
142 mke2fs_args.push_back("-I");
143 mke2fs_args.push_back("512");
144 }
145
David Anderson6f70cc62021-07-13 17:31:19 -0700146 if (fsOptions & (1 << FS_OPT_CASEFOLD)) {
147 mke2fs_args.push_back("-O");
148 mke2fs_args.push_back("casefold");
149 mke2fs_args.push_back("-E");
150 mke2fs_args.push_back("encoding=utf8");
151 }
152
Jin Qian4afba662017-04-18 18:19:12 -0700153 mke2fs_args.push_back(fileName);
154
155 std::string size_str = std::to_string(partSize / block_size);
156 mke2fs_args.push_back(size_str.c_str());
157 mke2fs_args.push_back(nullptr);
158
Jin Qian03c73e12017-12-04 16:17:46 -0800159 const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf";
160 std::vector<const char*> mke2fs_envp = {mke2fs_env.c_str(), nullptr};
161
Jaegeuk Kim899ad552017-11-28 19:26:34 -0800162 int ret = exec_cmd(mke2fs_args[0], mke2fs_args.data(), mke2fs_envp.data());
Jin Qian4afba662017-04-18 18:19:12 -0700163 if (ret != 0) {
Jin Qian4afba662017-04-18 18:19:12 -0700164 return -1;
165 }
166
167 if (initial_dir.empty()) {
168 return 0;
169 }
170
171 const std::string e2fsdroid_path = exec_dir + "/e2fsdroid";
172 std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(),
173 fileName, nullptr};
174
Jaegeuk Kim899ad552017-11-28 19:26:34 -0800175 return exec_cmd(e2fsdroid_args[0], e2fsdroid_args.data(), nullptr);
Jin Qian4afba662017-04-18 18:19:12 -0700176}
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700177
Jaegeuk Kim58d10c22020-12-29 11:55:18 -0800178enum {
179 // clang-format off
180 FSCK_SUCCESS = 0,
181 FSCK_ERROR_CORRECTED = 1 << 0,
182 FSCK_SYSTEM_SHOULD_REBOOT = 1 << 1,
183 FSCK_ERRORS_LEFT_UNCORRECTED = 1 << 2,
184 FSCK_OPERATIONAL_ERROR = 1 << 3,
185 FSCK_USAGE_OR_SYNTAX_ERROR = 1 << 4,
186 FSCK_USER_CANCELLED = 1 << 5,
187 FSCK_SHARED_LIB_ERROR = 1 << 7,
188 // clang-format on
189};
190
Jaegeuk Kim638d05e2020-11-09 08:54:13 -0800191static int generate_f2fs_image(const char* fileName, long long partSize,
192 const std::string& initial_dir, unsigned /* unused */,
193 unsigned /* unused */, const unsigned fsOptions) {
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700194 const std::string exec_dir = android::base::GetExecutableDirectory();
195 const std::string mkf2fs_path = exec_dir + "/make_f2fs";
196 std::vector<const char*> mkf2fs_args = {mkf2fs_path.c_str()};
197
198 mkf2fs_args.push_back("-S");
199 std::string size_str = std::to_string(partSize);
200 mkf2fs_args.push_back(size_str.c_str());
Jaegeuk Kim46542f92018-11-21 12:55:19 -0800201 mkf2fs_args.push_back("-g");
202 mkf2fs_args.push_back("android");
Jaegeuk Kim638d05e2020-11-09 08:54:13 -0800203
204 if (fsOptions & (1 << FS_OPT_PROJID)) {
205 mkf2fs_args.push_back("-O");
206 mkf2fs_args.push_back("project_quota,extra_attr");
207 }
208
209 if (fsOptions & (1 << FS_OPT_CASEFOLD)) {
210 mkf2fs_args.push_back("-O");
211 mkf2fs_args.push_back("casefold");
212 mkf2fs_args.push_back("-C");
213 mkf2fs_args.push_back("utf8");
214 }
215
216 if (fsOptions & (1 << FS_OPT_COMPRESS)) {
217 mkf2fs_args.push_back("-O");
218 mkf2fs_args.push_back("compression");
219 mkf2fs_args.push_back("-O");
220 mkf2fs_args.push_back("extra_attr");
221 }
222
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700223 mkf2fs_args.push_back(fileName);
224 mkf2fs_args.push_back(nullptr);
225
Jaegeuk Kim899ad552017-11-28 19:26:34 -0800226 int ret = exec_cmd(mkf2fs_args[0], mkf2fs_args.data(), nullptr);
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700227 if (ret != 0) {
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700228 return -1;
229 }
230
Jaegeuk Kim899ad552017-11-28 19:26:34 -0800231 if (initial_dir.empty()) {
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800232 return 0;
Jin Qian4a335822017-04-18 16:23:18 -0700233 }
Jaegeuk Kim899ad552017-11-28 19:26:34 -0800234
235 const std::string sload_path = exec_dir + "/sload_f2fs";
236 std::vector<const char*> sload_args = {sload_path.c_str(), "-S",
237 "-f", initial_dir.c_str(), fileName, nullptr};
238
Jaegeuk Kim58d10c22020-12-29 11:55:18 -0800239 ret = exec_cmd(sload_args[0], sload_args.data(), nullptr);
240 if (ret != 0 && ret != FSCK_ERROR_CORRECTED) {
241 return -1;
242 }
243 return 0;
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800244}
JP Abgrall12351582014-06-17 17:01:14 -0700245
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700246static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700247 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000248
249 //returns 0 or error value
Jin Qian4a335822017-04-18 16:23:18 -0700250 int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
Jaegeuk Kim638d05e2020-11-09 08:54:13 -0800251 unsigned eraseBlkSize, unsigned logicalBlkSize, const unsigned fsOptions);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700252
253} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -0700254 { "ext4", generate_ext4_image},
255 { "f2fs", generate_f2fs_image},
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700256};
257
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800258const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700259 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800260 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700261 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -0700262 }
263 }
264 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700265}
266
Jin Qian4a335822017-04-18 16:23:18 -0700267int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
Jaegeuk Kim638d05e2020-11-09 08:54:13 -0800268 const std::string& initial_dir, unsigned eraseBlkSize,
269 unsigned logicalBlkSize, const unsigned fsOptions) {
270 return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize, fsOptions);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700271}