blob: 709f061bd6e32ad3a444223b5f7568372b2fd6bc [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"
JP Abgrall12351582014-06-17 17:01:14 -07004#include "make_f2fs.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07005
6#include <errno.h>
Jin Qian4a335822017-04-18 16:23:18 -07007#include <fcntl.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07008#include <stdio.h>
9#include <stdlib.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070010#include <string.h>
11#include <sys/stat.h>
12#include <sys/types.h>
Jin Qian4afba662017-04-18 18:19:12 -070013#ifndef WIN32
14#include <sys/wait.h>
Jin Qian29fc8592017-07-10 16:04:41 -070015#else
16#include <tchar.h>
17#include <windows.h>
Jin Qian4afba662017-04-18 18:19:12 -070018#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070019#include <unistd.h>
Jin Qian4afba662017-04-18 18:19:12 -070020#include <vector>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070021
Jin Qian29fc8592017-07-10 16:04:41 -070022#include <android-base/errors.h>
Jin Qian4afba662017-04-18 18:19:12 -070023#include <android-base/file.h>
24#include <android-base/stringprintf.h>
Jin Qian4a335822017-04-18 16:23:18 -070025#include <android-base/unique_fd.h>
Tao Bao6d881d62016-10-05 17:53:30 -070026#include <ext4_utils/make_ext4fs.h>
Elliott Hughesfc797672015-04-07 20:12:50 -070027#include <sparse/sparse.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070028
Jin Qian4afba662017-04-18 18:19:12 -070029using android::base::StringPrintf;
Jin Qian4a335822017-04-18 16:23:18 -070030using android::base::unique_fd;
31
Jin Qian4afba662017-04-18 18:19:12 -070032#ifdef WIN32
Jin Qian29fc8592017-07-10 16:04:41 -070033static 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 Qian4a335822017-04-18 16:23:18 -070065 return -1;
66 }
Jin Qian29fc8592017-07-10 16:04:41 -070067
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 Grinberge6f3e9b2014-03-11 18:28:15 -070076}
Jin Qian4afba662017-04-18 18:19:12 -070077#else
78static 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 Qian29fc8592017-07-10 16:04:41 -0700103#endif
Jin Qian4afba662017-04-18 18:19:12 -0700104
105static 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 Qian99e39642017-07-27 16:17:17 -0700127 mke2fs_args.push_back("-O");
128 mke2fs_args.push_back("uninit_bg");
Jin Qian4afba662017-04-18 18:19:12 -0700129 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 Grinberge6f3e9b2014-03-11 18:28:15 -0700157
JP Abgrall6bd72be2014-06-17 23:43:18 -0700158#ifdef USE_F2FS
Jin Qian4a335822017-04-18 16:23:18 -0700159static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800160 unsigned /* unused */, unsigned /* unused */)
JP Abgrall12351582014-06-17 17:01:14 -0700161{
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000162 if (!initial_dir.empty()) {
Jin Qian4a335822017-04-18 16:23:18 -0700163 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 Crowley8f7f56e2015-11-27 09:29:37 +0000169 return -1;
170 }
JP Abgrall6bd72be2014-06-17 23:43:18 -0700171 return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
JP Abgrall12351582014-06-17 17:01:14 -0700172}
JP Abgrall6bd72be2014-06-17 23:43:18 -0700173#endif
JP Abgrall12351582014-06-17 17:01:14 -0700174
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700175static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700176 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000177
178 //returns 0 or error value
Jin Qian4a335822017-04-18 16:23:18 -0700179 int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800180 unsigned eraseBlkSize, unsigned logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700181
182} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -0700183 { "ext4", generate_ext4_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -0700184#ifdef USE_F2FS
JP Abgrall12351582014-06-17 17:01:14 -0700185 { "f2fs", generate_f2fs_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -0700186#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700187};
188
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800189const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700190 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800191 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700192 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -0700193 }
194 }
195 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700196}
197
Jin Qian4a335822017-04-18 16:23:18 -0700198int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800199 const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700200{
Jin Qian4a335822017-04-18 16:23:18 -0700201 return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700202}