blob: 2d77dd6eb321041ed93d614470b3aa0718e2be27 [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>
Elliott Hughesfc797672015-04-07 20:12:50 -070026#include <sparse/sparse.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070027
Jin Qian4afba662017-04-18 18:19:12 -070028using android::base::StringPrintf;
Jin Qian4a335822017-04-18 16:23:18 -070029using android::base::unique_fd;
30
Jin Qian4afba662017-04-18 18:19:12 -070031#ifdef WIN32
Jin Qian29fc8592017-07-10 16:04:41 -070032static int exec_e2fs_cmd(const char* path, char* const argv[]) {
33 std::string cmd;
34 int i = 0;
35 while (argv[i] != nullptr) {
36 cmd += argv[i++];
37 cmd += " ";
38 }
39 cmd = cmd.substr(0, cmd.size() - 1);
40
41 STARTUPINFO si;
42 PROCESS_INFORMATION pi;
43 DWORD exit_code = 0;
44
45 ZeroMemory(&si, sizeof(si));
46 si.cb = sizeof(si);
47 ZeroMemory(&pi, sizeof(pi));
48
49 SetEnvironmentVariableA("MKE2FS_CONFIG", "");
50
51 if (!CreateProcessA(nullptr, // No module name (use command line)
52 const_cast<char*>(cmd.c_str()), // Command line
53 nullptr, // Process handle not inheritable
54 nullptr, // Thread handle not inheritable
55 FALSE, // Set handle inheritance to FALSE
56 0, // No creation flags
57 nullptr, // Use parent's environment block
58 nullptr, // Use parent's starting directory
59 &si, // Pointer to STARTUPINFO structure
60 &pi) // Pointer to PROCESS_INFORMATION structure
61 ) {
62 fprintf(stderr, "CreateProcess failed: %s\n",
63 android::base::SystemErrorCodeToString(GetLastError()).c_str());
Jin Qian4a335822017-04-18 16:23:18 -070064 return -1;
65 }
Jin Qian29fc8592017-07-10 16:04:41 -070066
67 WaitForSingleObject(pi.hProcess, INFINITE);
68
69 GetExitCodeProcess(pi.hProcess, &exit_code);
70
71 CloseHandle(pi.hProcess);
72 CloseHandle(pi.hThread);
73
74 return exit_code != 0;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070075}
Jin Qian4afba662017-04-18 18:19:12 -070076#else
77static int exec_e2fs_cmd(const char* path, char* const argv[]) {
78 int status;
79 pid_t child;
80 if ((child = fork()) == 0) {
81 setenv("MKE2FS_CONFIG", "", 1);
82 execvp(path, argv);
83 _exit(EXIT_FAILURE);
84 }
85 if (child < 0) {
86 fprintf(stderr, "%s failed with fork %s\n", path, strerror(errno));
87 return -1;
88 }
89 if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) {
90 fprintf(stderr, "%s failed with waitpid %s\n", path, strerror(errno));
91 return -1;
92 }
93 int ret = -1;
94 if (WIFEXITED(status)) {
95 ret = WEXITSTATUS(status);
96 if (ret != 0) {
97 fprintf(stderr, "%s failed with status %d\n", path, ret);
98 }
99 }
100 return ret;
101}
Jin Qian29fc8592017-07-10 16:04:41 -0700102#endif
Jin Qian4afba662017-04-18 18:19:12 -0700103
104static int generate_ext4_image(const char* fileName, long long partSize,
105 const std::string& initial_dir, unsigned eraseBlkSize,
106 unsigned logicalBlkSize) {
107 static constexpr int block_size = 4096;
108 const std::string exec_dir = android::base::GetExecutableDirectory();
109
110 const std::string mke2fs_path = exec_dir + "/mke2fs";
111 std::vector<const char*> mke2fs_args = {mke2fs_path.c_str(), "-t", "ext4", "-b"};
112
113 std::string block_size_str = std::to_string(block_size);
114 mke2fs_args.push_back(block_size_str.c_str());
115
116 std::string ext_attr = "android_sparse";
117 if (eraseBlkSize != 0 && logicalBlkSize != 0) {
118 int raid_stride = logicalBlkSize / block_size;
119 int raid_stripe_width = eraseBlkSize / block_size;
120 // stride should be the max of 8kb and logical block size
121 if (logicalBlkSize != 0 && logicalBlkSize < 8192) raid_stride = 8192 / block_size;
Connor O'Brien6ef5c242017-11-01 17:37:32 -0700122 // stripe width should be >= stride
123 if (raid_stripe_width < raid_stride) raid_stripe_width = raid_stride;
Jin Qian4afba662017-04-18 18:19:12 -0700124 ext_attr += StringPrintf(",stride=%d,stripe-width=%d", raid_stride, raid_stripe_width);
125 }
126 mke2fs_args.push_back("-E");
127 mke2fs_args.push_back(ext_attr.c_str());
Jin Qian99e39642017-07-27 16:17:17 -0700128 mke2fs_args.push_back("-O");
129 mke2fs_args.push_back("uninit_bg");
Jin Qian4afba662017-04-18 18:19:12 -0700130 mke2fs_args.push_back(fileName);
131
132 std::string size_str = std::to_string(partSize / block_size);
133 mke2fs_args.push_back(size_str.c_str());
134 mke2fs_args.push_back(nullptr);
135
136 int ret = exec_e2fs_cmd(mke2fs_args[0], const_cast<char**>(mke2fs_args.data()));
137 if (ret != 0) {
138 fprintf(stderr, "mke2fs failed: %d\n", ret);
139 return -1;
140 }
141
142 if (initial_dir.empty()) {
143 return 0;
144 }
145
146 const std::string e2fsdroid_path = exec_dir + "/e2fsdroid";
147 std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(),
148 fileName, nullptr};
149
150 ret = exec_e2fs_cmd(e2fsdroid_args[0], const_cast<char**>(e2fsdroid_args.data()));
151 if (ret != 0) {
152 fprintf(stderr, "e2fsdroid failed: %d\n", ret);
153 return -1;
154 }
155
156 return 0;
157}
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700158
JP Abgrall6bd72be2014-06-17 23:43:18 -0700159#ifdef USE_F2FS
Jin Qian4a335822017-04-18 16:23:18 -0700160static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800161 unsigned /* unused */, unsigned /* unused */)
JP Abgrall12351582014-06-17 17:01:14 -0700162{
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000163 if (!initial_dir.empty()) {
Jin Qian4a335822017-04-18 16:23:18 -0700164 fprintf(stderr, "Unable to set initial directory on F2FS filesystem: %s\n", strerror(errno));
165 return -1;
166 }
167 unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
168 if (fd == -1) {
169 fprintf(stderr, "Unable to open output file for F2FS filesystem: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000170 return -1;
171 }
JP Abgrall6bd72be2014-06-17 23:43:18 -0700172 return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
JP Abgrall12351582014-06-17 17:01:14 -0700173}
JP Abgrall6bd72be2014-06-17 23:43:18 -0700174#endif
JP Abgrall12351582014-06-17 17:01:14 -0700175
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700176static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700177 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000178
179 //returns 0 or error value
Jin Qian4a335822017-04-18 16:23:18 -0700180 int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800181 unsigned eraseBlkSize, unsigned logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700182
183} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -0700184 { "ext4", generate_ext4_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -0700185#ifdef USE_F2FS
JP Abgrall12351582014-06-17 17:01:14 -0700186 { "f2fs", generate_f2fs_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -0700187#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700188};
189
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800190const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700191 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800192 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700193 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -0700194 }
195 }
196 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700197}
198
Jin Qian4a335822017-04-18 16:23:18 -0700199int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
Connor O'Brience16a8a2017-02-06 14:39:31 -0800200 const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700201{
Jin Qian4a335822017-04-18 16:23:18 -0700202 return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700203}