1 #include "fs.h"
2
3
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #ifndef _WIN32
12 #include <sys/wait.h>
13 #else
14 #include <tchar.h>
15 #include <windows.h>
16 #endif
17 #include <unistd.h>
18 #include <vector>
19
20 #include <android-base/errors.h>
21 #include <android-base/file.h>
22 #include <android-base/macros.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/unique_fd.h>
25
26 using android::base::GetExecutableDirectory;
27 using android::base::StringPrintf;
28 using android::base::unique_fd;
29
30 #ifdef _WIN32
exec_cmd(const char * path,const char ** argv,const char ** envp)31 static int exec_cmd(const char* path, const char** argv, const char** envp) {
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
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 }
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
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
66 ) {
67 fprintf(stderr, "CreateProcess failed: %s\n",
68 android::base::SystemErrorCodeToString(GetLastError()).c_str());
69 return -1;
70 }
71
72 WaitForSingleObject(pi.hProcess, INFINITE);
73
74 GetExitCodeProcess(pi.hProcess, &exit_code);
75
76 CloseHandle(pi.hProcess);
77 CloseHandle(pi.hThread);
78
79 if (exit_code != 0) {
80 fprintf(stderr, "%s failed: %lu\n", path, exit_code);
81 return -1;
82 }
83 return 0;
84 }
85 #else
exec_cmd(const char * path,const char ** argv,const char ** envp)86 static int exec_cmd(const char* path, const char** argv, const char** envp) {
87 int status;
88 pid_t child;
89 if ((child = fork()) == 0) {
90 execve(path, const_cast<char**>(argv), const_cast<char**>(envp));
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);
104 }
105
106 if (ret != 0) {
107 fprintf(stderr, "%s failed with status %d\n", path, ret);
108 return -1;
109 }
110 return 0;
111 }
112 #endif
113
generate_ext4_image(const char * fileName,long long partSize,const std::string & initial_dir,unsigned eraseBlkSize,unsigned logicalBlkSize,const unsigned fsOptions)114 static int generate_ext4_image(const char* fileName, long long partSize,
115 const std::string& initial_dir, unsigned eraseBlkSize,
116 unsigned logicalBlkSize, const unsigned fsOptions) {
117 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;
132 // stripe width should be >= stride
133 if (raid_stripe_width < raid_stride) raid_stripe_width = raid_stride;
134 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());
138 mke2fs_args.push_back("-O");
139 mke2fs_args.push_back("uninit_bg");
140
141 if (fsOptions & (1 << FS_OPT_PROJID)) {
142 mke2fs_args.push_back("-I");
143 mke2fs_args.push_back("512");
144 }
145
146 mke2fs_args.push_back(fileName);
147
148 std::string size_str = std::to_string(partSize / block_size);
149 mke2fs_args.push_back(size_str.c_str());
150 mke2fs_args.push_back(nullptr);
151
152 const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf";
153 std::vector<const char*> mke2fs_envp = {mke2fs_env.c_str(), nullptr};
154
155 int ret = exec_cmd(mke2fs_args[0], mke2fs_args.data(), mke2fs_envp.data());
156 if (ret != 0) {
157 return -1;
158 }
159
160 if (initial_dir.empty()) {
161 return 0;
162 }
163
164 const std::string e2fsdroid_path = exec_dir + "/e2fsdroid";
165 std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(),
166 fileName, nullptr};
167
168 return exec_cmd(e2fsdroid_args[0], e2fsdroid_args.data(), nullptr);
169 }
170
171 enum {
172 // clang-format off
173 FSCK_SUCCESS = 0,
174 FSCK_ERROR_CORRECTED = 1 << 0,
175 FSCK_SYSTEM_SHOULD_REBOOT = 1 << 1,
176 FSCK_ERRORS_LEFT_UNCORRECTED = 1 << 2,
177 FSCK_OPERATIONAL_ERROR = 1 << 3,
178 FSCK_USAGE_OR_SYNTAX_ERROR = 1 << 4,
179 FSCK_USER_CANCELLED = 1 << 5,
180 FSCK_SHARED_LIB_ERROR = 1 << 7,
181 // clang-format on
182 };
183
generate_f2fs_image(const char * fileName,long long partSize,const std::string & initial_dir,unsigned,unsigned,const unsigned fsOptions)184 static int generate_f2fs_image(const char* fileName, long long partSize,
185 const std::string& initial_dir, unsigned /* unused */,
186 unsigned /* unused */, const unsigned fsOptions) {
187 const std::string exec_dir = android::base::GetExecutableDirectory();
188 const std::string mkf2fs_path = exec_dir + "/make_f2fs";
189 std::vector<const char*> mkf2fs_args = {mkf2fs_path.c_str()};
190
191 mkf2fs_args.push_back("-S");
192 std::string size_str = std::to_string(partSize);
193 mkf2fs_args.push_back(size_str.c_str());
194 mkf2fs_args.push_back("-g");
195 mkf2fs_args.push_back("android");
196
197 if (fsOptions & (1 << FS_OPT_PROJID)) {
198 mkf2fs_args.push_back("-O");
199 mkf2fs_args.push_back("project_quota,extra_attr");
200 }
201
202 if (fsOptions & (1 << FS_OPT_CASEFOLD)) {
203 mkf2fs_args.push_back("-O");
204 mkf2fs_args.push_back("casefold");
205 mkf2fs_args.push_back("-C");
206 mkf2fs_args.push_back("utf8");
207 }
208
209 if (fsOptions & (1 << FS_OPT_COMPRESS)) {
210 mkf2fs_args.push_back("-O");
211 mkf2fs_args.push_back("compression");
212 mkf2fs_args.push_back("-O");
213 mkf2fs_args.push_back("extra_attr");
214 }
215
216 mkf2fs_args.push_back(fileName);
217 mkf2fs_args.push_back(nullptr);
218
219 int ret = exec_cmd(mkf2fs_args[0], mkf2fs_args.data(), nullptr);
220 if (ret != 0) {
221 return -1;
222 }
223
224 if (initial_dir.empty()) {
225 return 0;
226 }
227
228 const std::string sload_path = exec_dir + "/sload_f2fs";
229 std::vector<const char*> sload_args = {sload_path.c_str(), "-S",
230 "-f", initial_dir.c_str(), fileName, nullptr};
231
232 ret = exec_cmd(sload_args[0], sload_args.data(), nullptr);
233 if (ret != 0 && ret != FSCK_ERROR_CORRECTED) {
234 return -1;
235 }
236 return 0;
237 }
238
239 static const struct fs_generator {
240 const char* fs_type; //must match what fastboot reports for partition type
241
242 //returns 0 or error value
243 int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
244 unsigned eraseBlkSize, unsigned logicalBlkSize, const unsigned fsOptions);
245
246 } generators[] = {
247 { "ext4", generate_ext4_image},
248 { "f2fs", generate_f2fs_image},
249 };
250
fs_get_generator(const std::string & fs_type)251 const struct fs_generator* fs_get_generator(const std::string& fs_type) {
252 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
253 if (fs_type == generators[i].fs_type) {
254 return generators + i;
255 }
256 }
257 return nullptr;
258 }
259
fs_generator_generate(const struct fs_generator * gen,const char * fileName,long long partSize,const std::string & initial_dir,unsigned eraseBlkSize,unsigned logicalBlkSize,const unsigned fsOptions)260 int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
261 const std::string& initial_dir, unsigned eraseBlkSize,
262 unsigned logicalBlkSize, const unsigned fsOptions) {
263 return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize, fsOptions);
264 }
265