blob: fe5cbc3d7d35ad8886dc330c31aab5ac62bff956 [file] [log] [blame]
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001#include "fs.h"
2
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07003#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07004
5#include <errno.h>
Jin Qian4a335822017-04-18 16:23:18 -07006#include <fcntl.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07007#include <stdio.h>
8#include <stdlib.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07009#include <string.h>
10#include <sys/stat.h>
11#include <sys/types.h>
Jin Qian4afba662017-04-18 18:19:12 -070012#ifndef WIN32
13#include <sys/wait.h>
Jin Qian29fc8592017-07-10 16:04:41 -070014#else
15#include <tchar.h>
16#include <windows.h>
Jin Qian4afba662017-04-18 18:19:12 -070017#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070018#include <unistd.h>
Jin Qian4afba662017-04-18 18:19:12 -070019#include <vector>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070020
Jin Qian29fc8592017-07-10 16:04:41 -070021#include <android-base/errors.h>
Jin Qian4afba662017-04-18 18:19:12 -070022#include <android-base/file.h>
23#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
Jin Qian4afba662017-04-18 18:19:12 -070030#ifdef WIN32
Jin Qian03c73e12017-12-04 16:17:46 -080031static int exec_e2fs_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
79 return exit_code != 0;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070080}
Jin Qian4afba662017-04-18 18:19:12 -070081#else
Jin Qian03c73e12017-12-04 16:17:46 -080082static int exec_e2fs_cmd(const char* path, const char** argv, const char** envp) {
Jin Qian4afba662017-04-18 18:19:12 -070083 int status;
84 pid_t child;
85 if ((child = fork()) == 0) {
Jin Qian03c73e12017-12-04 16:17:46 -080086 execvpe(path, const_cast<char**>(argv), const_cast<char**>(envp));
Jin Qian4afba662017-04-18 18:19:12 -070087 _exit(EXIT_FAILURE);
88 }
89 if (child < 0) {
90 fprintf(stderr, "%s failed with fork %s\n", path, strerror(errno));
91 return -1;
92 }
93 if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) {
94 fprintf(stderr, "%s failed with waitpid %s\n", path, strerror(errno));
95 return -1;
96 }
97 int ret = -1;
98 if (WIFEXITED(status)) {
99 ret = WEXITSTATUS(status);
100 if (ret != 0) {
101 fprintf(stderr, "%s failed with status %d\n", path, ret);
102 }
103 }
104 return ret;
105}
Jin Qian29fc8592017-07-10 16:04:41 -0700106#endif
Jin Qian4afba662017-04-18 18:19:12 -0700107
108static int generate_ext4_image(const char* fileName, long long partSize,
109 const std::string& initial_dir, unsigned eraseBlkSize,
110 unsigned logicalBlkSize) {
111 static constexpr int block_size = 4096;
112 const std::string exec_dir = android::base::GetExecutableDirectory();
113
114 const std::string mke2fs_path = exec_dir + "/mke2fs";
115 std::vector<const char*> mke2fs_args = {mke2fs_path.c_str(), "-t", "ext4", "-b"};
116
117 std::string block_size_str = std::to_string(block_size);
118 mke2fs_args.push_back(block_size_str.c_str());
119
120 std::string ext_attr = "android_sparse";
121 if (eraseBlkSize != 0 && logicalBlkSize != 0) {
122 int raid_stride = logicalBlkSize / block_size;
123 int raid_stripe_width = eraseBlkSize / block_size;
124 // stride should be the max of 8kb and logical block size
125 if (logicalBlkSize != 0 && logicalBlkSize < 8192) raid_stride = 8192 / block_size;
Connor O'Brien6ef5c242017-11-01 17:37:32 -0700126 // stripe width should be >= stride
127 if (raid_stripe_width < raid_stride) raid_stripe_width = raid_stride;
Jin Qian4afba662017-04-18 18:19:12 -0700128 ext_attr += StringPrintf(",stride=%d,stripe-width=%d", raid_stride, raid_stripe_width);
129 }
130 mke2fs_args.push_back("-E");
131 mke2fs_args.push_back(ext_attr.c_str());
Jin Qian99e39642017-07-27 16:17:17 -0700132 mke2fs_args.push_back("-O");
133 mke2fs_args.push_back("uninit_bg");
Jin Qian4afba662017-04-18 18:19:12 -0700134 mke2fs_args.push_back(fileName);
135
136 std::string size_str = std::to_string(partSize / block_size);
137 mke2fs_args.push_back(size_str.c_str());
138 mke2fs_args.push_back(nullptr);
139
Jin Qian03c73e12017-12-04 16:17:46 -0800140 const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf";
141 std::vector<const char*> mke2fs_envp = {mke2fs_env.c_str(), nullptr};
142
143 int ret = exec_e2fs_cmd(mke2fs_args[0], mke2fs_args.data(), mke2fs_envp.data());
Jin Qian4afba662017-04-18 18:19:12 -0700144 if (ret != 0) {
145 fprintf(stderr, "mke2fs failed: %d\n", ret);
146 return -1;
147 }
148
149 if (initial_dir.empty()) {
150 return 0;
151 }
152
153 const std::string e2fsdroid_path = exec_dir + "/e2fsdroid";
154 std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(),
155 fileName, nullptr};
156
Jin Qian03c73e12017-12-04 16:17:46 -0800157 ret = exec_e2fs_cmd(e2fsdroid_args[0], e2fsdroid_args.data(), nullptr);
Jin Qian4afba662017-04-18 18:19:12 -0700158 if (ret != 0) {
159 fprintf(stderr, "e2fsdroid failed: %d\n", ret);
160 return -1;
161 }
162
163 return 0;
164}
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700165
Jin Qian4a335822017-04-18 16:23:18 -0700166static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800167 unsigned /* unused */, unsigned /* unused */)
JP Abgrall12351582014-06-17 17:01:14 -0700168{
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800169#ifndef WIN32
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700170 const std::string exec_dir = android::base::GetExecutableDirectory();
171 const std::string mkf2fs_path = exec_dir + "/make_f2fs";
172 std::vector<const char*> mkf2fs_args = {mkf2fs_path.c_str()};
173
174 mkf2fs_args.push_back("-S");
175 std::string size_str = std::to_string(partSize);
176 mkf2fs_args.push_back(size_str.c_str());
177 mkf2fs_args.push_back("-f");
178 mkf2fs_args.push_back("-O");
179 mkf2fs_args.push_back("encrypt");
180 mkf2fs_args.push_back("-O");
181 mkf2fs_args.push_back("quota");
182 mkf2fs_args.push_back(fileName);
183 mkf2fs_args.push_back(nullptr);
184
Jin Qian03c73e12017-12-04 16:17:46 -0800185 int ret = exec_e2fs_cmd(mkf2fs_args[0], mkf2fs_args.data(), nullptr);
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700186 if (ret != 0) {
187 fprintf(stderr, "mkf2fs failed: %d\n", ret);
188 return -1;
189 }
190
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000191 if (!initial_dir.empty()) {
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800192 fprintf(stderr, "sload.f2s not supported yet\n");
193 return 0;
Jin Qian4a335822017-04-18 16:23:18 -0700194 }
Jaegeuk Kimd8721182017-10-02 18:51:48 -0700195 return 0;
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800196#else
Dan Willemsen22088fd2017-11-30 14:59:08 -0800197 UNUSED(fileName, partSize, initial_dir);
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800198 fprintf(stderr, "make_f2fs not supported on Windows\n");
199 return -1;
JP Abgrall6bd72be2014-06-17 23:43:18 -0700200#endif
Jaegeuk Kim8d9b6ee2017-11-15 10:55:07 -0800201}
JP Abgrall12351582014-06-17 17:01:14 -0700202
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700203static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700204 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000205
206 //returns 0 or error value
Jin Qian4a335822017-04-18 16:23:18 -0700207 int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800208 unsigned eraseBlkSize, unsigned logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700209
210} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -0700211 { "ext4", generate_ext4_image},
212 { "f2fs", generate_f2fs_image},
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700213};
214
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800215const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700216 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800217 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700218 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -0700219 }
220 }
221 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700222}
223
Jin Qian4a335822017-04-18 16:23:18 -0700224int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800225 const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700226{
Jin Qian4a335822017-04-18 16:23:18 -0700227 return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700228}