2015-11-27 10:29:37 +01:00
|
|
|
#include "fs.h"
|
|
|
|
|
2014-03-12 02:28:15 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
2017-04-19 01:23:18 +02:00
|
|
|
#include <fcntl.h>
|
2014-03-12 02:28:15 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-08-23 19:42:02 +02:00
|
|
|
#ifndef _WIN32
|
2017-04-19 03:19:12 +02:00
|
|
|
#include <sys/wait.h>
|
2017-07-11 01:04:41 +02:00
|
|
|
#else
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <windows.h>
|
2017-04-19 03:19:12 +02:00
|
|
|
#endif
|
2014-03-12 02:28:15 +01:00
|
|
|
#include <unistd.h>
|
2017-04-19 03:19:12 +02:00
|
|
|
#include <vector>
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2017-07-11 01:04:41 +02:00
|
|
|
#include <android-base/errors.h>
|
2017-04-19 03:19:12 +02:00
|
|
|
#include <android-base/file.h>
|
2018-04-11 03:01:08 +02:00
|
|
|
#include <android-base/macros.h>
|
2017-04-19 03:19:12 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-04-19 01:23:18 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2017-12-05 01:17:46 +01:00
|
|
|
using android::base::GetExecutableDirectory;
|
2017-04-19 03:19:12 +02:00
|
|
|
using android::base::StringPrintf;
|
2017-04-19 01:23:18 +02:00
|
|
|
using android::base::unique_fd;
|
|
|
|
|
2018-08-23 19:42:02 +02:00
|
|
|
#ifdef _WIN32
|
2017-11-29 04:26:34 +01:00
|
|
|
static int exec_cmd(const char* path, const char** argv, const char** envp) {
|
2017-07-11 01:04:41 +02:00
|
|
|
std::string cmd;
|
|
|
|
int i = 0;
|
|
|
|
while (argv[i] != nullptr) {
|
|
|
|
cmd += argv[i++];
|
|
|
|
cmd += " ";
|
2017-04-19 01:23:18 +02:00
|
|
|
}
|
2017-07-11 01:04:41 +02:00
|
|
|
cmd = cmd.substr(0, cmd.size() - 1);
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
DWORD exit_code = 0;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
2017-12-05 01:17:46 +01:00
|
|
|
std::string env_str;
|
|
|
|
if (envp != nullptr) {
|
|
|
|
while (*envp != nullptr) {
|
|
|
|
env_str += std::string(*envp) + std::string("\0", 1);
|
|
|
|
envp++;
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 01:04:41 +02:00
|
|
|
|
|
|
|
if (!CreateProcessA(nullptr, // No module name (use command line)
|
|
|
|
const_cast<char*>(cmd.c_str()), // Command line
|
|
|
|
nullptr, // Process handle not inheritable
|
|
|
|
nullptr, // Thread handle not inheritable
|
|
|
|
FALSE, // Set handle inheritance to FALSE
|
|
|
|
0, // No creation flags
|
2017-12-05 01:17:46 +01:00
|
|
|
env_str.empty() ? nullptr : LPSTR(env_str.c_str()),
|
|
|
|
nullptr, // Use parent's starting directory
|
|
|
|
&si, // Pointer to STARTUPINFO structure
|
|
|
|
&pi) // Pointer to PROCESS_INFORMATION structure
|
2017-07-11 01:04:41 +02:00
|
|
|
) {
|
|
|
|
fprintf(stderr, "CreateProcess failed: %s\n",
|
|
|
|
android::base::SystemErrorCodeToString(GetLastError()).c_str());
|
|
|
|
return -1;
|
2015-11-27 10:29:37 +01:00
|
|
|
}
|
2017-07-11 01:04:41 +02:00
|
|
|
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
|
|
|
|
GetExitCodeProcess(pi.hProcess, &exit_code);
|
|
|
|
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
2017-11-29 04:26:34 +01:00
|
|
|
if (exit_code != 0) {
|
|
|
|
fprintf(stderr, "%s failed: %lu\n", path, exit_code);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
2017-04-19 03:19:12 +02:00
|
|
|
#else
|
2017-11-29 04:26:34 +01:00
|
|
|
static int exec_cmd(const char* path, const char** argv, const char** envp) {
|
2017-04-19 03:19:12 +02:00
|
|
|
int status;
|
|
|
|
pid_t child;
|
|
|
|
if ((child = fork()) == 0) {
|
2017-12-06 06:39:40 +01:00
|
|
|
execve(path, const_cast<char**>(argv), const_cast<char**>(envp));
|
2017-04-19 03:19:12 +02:00
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (child < 0) {
|
|
|
|
fprintf(stderr, "%s failed with fork %s\n", path, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) {
|
|
|
|
fprintf(stderr, "%s failed with waitpid %s\n", path, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int ret = -1;
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
ret = WEXITSTATUS(status);
|
|
|
|
}
|
2017-11-29 04:26:34 +01:00
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "%s failed with status %d\n", path, ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-04-19 03:19:12 +02:00
|
|
|
}
|
2017-07-11 01:04:41 +02:00
|
|
|
#endif
|
2017-04-19 03:19:12 +02:00
|
|
|
|
2022-07-21 06:43:20 +02:00
|
|
|
static int generate_ext4_image(const char* fileName, long long partSize, unsigned eraseBlkSize,
|
2020-11-09 17:54:13 +01:00
|
|
|
unsigned logicalBlkSize, const unsigned fsOptions) {
|
2017-04-19 03:19:12 +02:00
|
|
|
static constexpr int block_size = 4096;
|
|
|
|
const std::string exec_dir = android::base::GetExecutableDirectory();
|
|
|
|
|
|
|
|
const std::string mke2fs_path = exec_dir + "/mke2fs";
|
|
|
|
std::vector<const char*> mke2fs_args = {mke2fs_path.c_str(), "-t", "ext4", "-b"};
|
|
|
|
|
|
|
|
std::string block_size_str = std::to_string(block_size);
|
|
|
|
mke2fs_args.push_back(block_size_str.c_str());
|
|
|
|
|
|
|
|
std::string ext_attr = "android_sparse";
|
|
|
|
if (eraseBlkSize != 0 && logicalBlkSize != 0) {
|
|
|
|
int raid_stride = logicalBlkSize / block_size;
|
|
|
|
int raid_stripe_width = eraseBlkSize / block_size;
|
|
|
|
// stride should be the max of 8kb and logical block size
|
|
|
|
if (logicalBlkSize != 0 && logicalBlkSize < 8192) raid_stride = 8192 / block_size;
|
2017-11-02 01:37:32 +01:00
|
|
|
// stripe width should be >= stride
|
|
|
|
if (raid_stripe_width < raid_stride) raid_stripe_width = raid_stride;
|
2017-04-19 03:19:12 +02:00
|
|
|
ext_attr += StringPrintf(",stride=%d,stripe-width=%d", raid_stride, raid_stripe_width);
|
|
|
|
}
|
|
|
|
mke2fs_args.push_back("-E");
|
|
|
|
mke2fs_args.push_back(ext_attr.c_str());
|
2017-07-28 01:17:17 +02:00
|
|
|
mke2fs_args.push_back("-O");
|
|
|
|
mke2fs_args.push_back("uninit_bg");
|
2020-11-09 17:54:13 +01:00
|
|
|
|
|
|
|
if (fsOptions & (1 << FS_OPT_PROJID)) {
|
|
|
|
mke2fs_args.push_back("-I");
|
|
|
|
mke2fs_args.push_back("512");
|
|
|
|
}
|
|
|
|
|
2021-07-14 02:31:19 +02:00
|
|
|
if (fsOptions & (1 << FS_OPT_CASEFOLD)) {
|
|
|
|
mke2fs_args.push_back("-O");
|
|
|
|
mke2fs_args.push_back("casefold");
|
|
|
|
mke2fs_args.push_back("-E");
|
|
|
|
mke2fs_args.push_back("encoding=utf8");
|
|
|
|
}
|
|
|
|
|
2017-04-19 03:19:12 +02:00
|
|
|
mke2fs_args.push_back(fileName);
|
|
|
|
|
|
|
|
std::string size_str = std::to_string(partSize / block_size);
|
|
|
|
mke2fs_args.push_back(size_str.c_str());
|
|
|
|
mke2fs_args.push_back(nullptr);
|
|
|
|
|
2017-12-05 01:17:46 +01:00
|
|
|
const std::string mke2fs_env = "MKE2FS_CONFIG=" + GetExecutableDirectory() + "/mke2fs.conf";
|
|
|
|
std::vector<const char*> mke2fs_envp = {mke2fs_env.c_str(), nullptr};
|
|
|
|
|
2017-11-29 04:26:34 +01:00
|
|
|
int ret = exec_cmd(mke2fs_args[0], mke2fs_args.data(), mke2fs_envp.data());
|
2017-04-19 03:19:12 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-07-21 06:43:20 +02:00
|
|
|
return 0;
|
2017-04-19 03:19:12 +02:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2020-12-29 20:55:18 +01:00
|
|
|
enum {
|
|
|
|
// clang-format off
|
|
|
|
FSCK_SUCCESS = 0,
|
|
|
|
FSCK_ERROR_CORRECTED = 1 << 0,
|
|
|
|
FSCK_SYSTEM_SHOULD_REBOOT = 1 << 1,
|
|
|
|
FSCK_ERRORS_LEFT_UNCORRECTED = 1 << 2,
|
|
|
|
FSCK_OPERATIONAL_ERROR = 1 << 3,
|
|
|
|
FSCK_USAGE_OR_SYNTAX_ERROR = 1 << 4,
|
|
|
|
FSCK_USER_CANCELLED = 1 << 5,
|
|
|
|
FSCK_SHARED_LIB_ERROR = 1 << 7,
|
|
|
|
// clang-format on
|
|
|
|
};
|
|
|
|
|
2022-07-21 06:43:20 +02:00
|
|
|
static int generate_f2fs_image(const char* fileName, long long partSize, unsigned /* unused */,
|
2020-11-09 17:54:13 +01:00
|
|
|
unsigned /* unused */, const unsigned fsOptions) {
|
2017-10-03 03:51:48 +02:00
|
|
|
const std::string exec_dir = android::base::GetExecutableDirectory();
|
|
|
|
const std::string mkf2fs_path = exec_dir + "/make_f2fs";
|
|
|
|
std::vector<const char*> mkf2fs_args = {mkf2fs_path.c_str()};
|
|
|
|
|
|
|
|
mkf2fs_args.push_back("-S");
|
|
|
|
std::string size_str = std::to_string(partSize);
|
|
|
|
mkf2fs_args.push_back(size_str.c_str());
|
2018-11-21 21:55:19 +01:00
|
|
|
mkf2fs_args.push_back("-g");
|
|
|
|
mkf2fs_args.push_back("android");
|
2020-11-09 17:54:13 +01:00
|
|
|
|
|
|
|
if (fsOptions & (1 << FS_OPT_PROJID)) {
|
|
|
|
mkf2fs_args.push_back("-O");
|
|
|
|
mkf2fs_args.push_back("project_quota,extra_attr");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fsOptions & (1 << FS_OPT_CASEFOLD)) {
|
|
|
|
mkf2fs_args.push_back("-O");
|
|
|
|
mkf2fs_args.push_back("casefold");
|
|
|
|
mkf2fs_args.push_back("-C");
|
|
|
|
mkf2fs_args.push_back("utf8");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fsOptions & (1 << FS_OPT_COMPRESS)) {
|
|
|
|
mkf2fs_args.push_back("-O");
|
|
|
|
mkf2fs_args.push_back("compression");
|
|
|
|
mkf2fs_args.push_back("-O");
|
|
|
|
mkf2fs_args.push_back("extra_attr");
|
|
|
|
}
|
|
|
|
|
2017-10-03 03:51:48 +02:00
|
|
|
mkf2fs_args.push_back(fileName);
|
|
|
|
mkf2fs_args.push_back(nullptr);
|
|
|
|
|
2017-11-29 04:26:34 +01:00
|
|
|
int ret = exec_cmd(mkf2fs_args[0], mkf2fs_args.data(), nullptr);
|
2017-10-03 03:51:48 +02:00
|
|
|
if (ret != 0) {
|
2017-04-19 01:23:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-29 20:55:18 +01:00
|
|
|
return 0;
|
2017-11-15 19:55:07 +01:00
|
|
|
}
|
2014-06-18 02:01:14 +02:00
|
|
|
|
2014-03-12 02:28:15 +01:00
|
|
|
static const struct fs_generator {
|
2015-06-24 05:27:58 +02:00
|
|
|
const char* fs_type; //must match what fastboot reports for partition type
|
2015-11-27 10:29:37 +01:00
|
|
|
|
|
|
|
//returns 0 or error value
|
2022-07-21 06:43:20 +02:00
|
|
|
int (*generate)(const char* fileName, long long partSize, unsigned eraseBlkSize,
|
|
|
|
unsigned logicalBlkSize, const unsigned fsOptions);
|
2014-03-12 02:28:15 +01:00
|
|
|
|
|
|
|
} generators[] = {
|
2014-06-18 02:01:14 +02:00
|
|
|
{ "ext4", generate_ext4_image},
|
|
|
|
{ "f2fs", generate_f2fs_image},
|
2014-03-12 02:28:15 +01:00
|
|
|
};
|
|
|
|
|
2015-11-02 23:05:57 +01:00
|
|
|
const struct fs_generator* fs_get_generator(const std::string& fs_type) {
|
2015-04-08 05:12:50 +02:00
|
|
|
for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
|
2015-11-02 23:05:57 +01:00
|
|
|
if (fs_type == generators[i].fs_type) {
|
2014-03-12 02:28:15 +01:00
|
|
|
return generators + i;
|
2015-04-08 05:12:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 01:23:18 +02:00
|
|
|
int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
|
2022-07-21 06:43:20 +02:00
|
|
|
unsigned eraseBlkSize, unsigned logicalBlkSize,
|
|
|
|
const unsigned fsOptions) {
|
|
|
|
return gen->generate(fileName, partSize, eraseBlkSize, logicalBlkSize, fsOptions);
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|