2015-11-27 17:29:37 +08:00
|
|
|
#include "fs.h"
|
|
|
|
|
2014-03-12 09:28:15 +08:00
|
|
|
#include "fastboot.h"
|
2014-06-18 08:01:14 +08:00
|
|
|
#include "make_f2fs.h"
|
2014-03-12 09:28:15 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
2017-04-19 07:23:18 +08:00
|
|
|
#include <fcntl.h>
|
2014-03-12 09:28:15 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2017-04-19 09:19:12 +08:00
|
|
|
#ifndef WIN32
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
2014-03-12 09:28:15 +08:00
|
|
|
#include <unistd.h>
|
2017-04-19 09:19:12 +08:00
|
|
|
#include <vector>
|
2014-03-12 09:28:15 +08:00
|
|
|
|
2017-04-19 09:19:12 +08:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2017-04-19 07:23:18 +08:00
|
|
|
#include <android-base/unique_fd.h>
|
2016-10-06 08:53:30 +08:00
|
|
|
#include <ext4_utils/make_ext4fs.h>
|
2015-04-08 11:12:50 +08:00
|
|
|
#include <sparse/sparse.h>
|
2014-03-12 09:28:15 +08:00
|
|
|
|
2017-04-19 09:19:12 +08:00
|
|
|
using android::base::StringPrintf;
|
2017-04-19 07:23:18 +08:00
|
|
|
using android::base::unique_fd;
|
|
|
|
|
2017-04-19 09:19:12 +08:00
|
|
|
#ifdef WIN32
|
2017-04-19 07:23:18 +08:00
|
|
|
static int generate_ext4_image(const char* fileName, long long partSize, const std::string& initial_dir,
|
2017-02-07 06:39:31 +08:00
|
|
|
unsigned eraseBlkSize, unsigned logicalBlkSize)
|
2014-03-12 09:28:15 +08:00
|
|
|
{
|
2017-04-19 07:23:18 +08:00
|
|
|
unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Unable to open output file for EXT4 filesystem: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-27 17:29:37 +08:00
|
|
|
if (initial_dir.empty()) {
|
2017-02-07 06:39:31 +08:00
|
|
|
make_ext4fs_sparse_fd_align(fd, partSize, NULL, NULL, eraseBlkSize, logicalBlkSize);
|
2015-11-27 17:29:37 +08:00
|
|
|
} else {
|
2017-02-07 06:39:31 +08:00
|
|
|
make_ext4fs_sparse_fd_directory_align(fd, partSize, NULL, NULL, initial_dir.c_str(),
|
|
|
|
eraseBlkSize, logicalBlkSize);
|
2015-11-27 17:29:37 +08:00
|
|
|
}
|
2014-03-12 09:28:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-04-19 09:19:12 +08:00
|
|
|
#else
|
|
|
|
static int exec_e2fs_cmd(const char* path, char* const argv[]) {
|
|
|
|
int status;
|
|
|
|
pid_t child;
|
|
|
|
if ((child = fork()) == 0) {
|
|
|
|
setenv("MKE2FS_CONFIG", "", 1);
|
|
|
|
execvp(path, argv);
|
|
|
|
_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);
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "%s failed with status %d\n", path, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int generate_ext4_image(const char* fileName, long long partSize,
|
|
|
|
const std::string& initial_dir, unsigned eraseBlkSize,
|
|
|
|
unsigned logicalBlkSize) {
|
|
|
|
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;
|
|
|
|
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 07:17:17 +08:00
|
|
|
mke2fs_args.push_back("-O");
|
|
|
|
mke2fs_args.push_back("uninit_bg");
|
2017-04-19 09:19:12 +08: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);
|
|
|
|
|
|
|
|
int ret = exec_e2fs_cmd(mke2fs_args[0], const_cast<char**>(mke2fs_args.data()));
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "mke2fs failed: %d\n", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initial_dir.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string e2fsdroid_path = exec_dir + "/e2fsdroid";
|
|
|
|
std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(),
|
|
|
|
fileName, nullptr};
|
|
|
|
|
|
|
|
ret = exec_e2fs_cmd(e2fsdroid_args[0], const_cast<char**>(e2fsdroid_args.data()));
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(stderr, "e2fsdroid failed: %d\n", ret);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-12 09:28:15 +08:00
|
|
|
|
2014-06-18 14:43:18 +08:00
|
|
|
#ifdef USE_F2FS
|
2017-04-19 07:23:18 +08:00
|
|
|
static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
|
2017-02-07 06:39:31 +08:00
|
|
|
unsigned /* unused */, unsigned /* unused */)
|
2014-06-18 08:01:14 +08:00
|
|
|
{
|
2015-11-27 17:29:37 +08:00
|
|
|
if (!initial_dir.empty()) {
|
2017-04-19 07:23:18 +08:00
|
|
|
fprintf(stderr, "Unable to set initial directory on F2FS filesystem: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Unable to open output file for F2FS filesystem: %s\n", strerror(errno));
|
2015-11-27 17:29:37 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-06-18 14:43:18 +08:00
|
|
|
return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
|
2014-06-18 08:01:14 +08:00
|
|
|
}
|
2014-06-18 14:43:18 +08:00
|
|
|
#endif
|
2014-06-18 08:01:14 +08:00
|
|
|
|
2014-03-12 09:28:15 +08:00
|
|
|
static const struct fs_generator {
|
2015-06-24 11:27:58 +08:00
|
|
|
const char* fs_type; //must match what fastboot reports for partition type
|
2015-11-27 17:29:37 +08:00
|
|
|
|
|
|
|
//returns 0 or error value
|
2017-04-19 07:23:18 +08:00
|
|
|
int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
|
2017-02-07 06:39:31 +08:00
|
|
|
unsigned eraseBlkSize, unsigned logicalBlkSize);
|
2014-03-12 09:28:15 +08:00
|
|
|
|
|
|
|
} generators[] = {
|
2014-06-18 08:01:14 +08:00
|
|
|
{ "ext4", generate_ext4_image},
|
2014-06-18 14:43:18 +08:00
|
|
|
#ifdef USE_F2FS
|
2014-06-18 08:01:14 +08:00
|
|
|
{ "f2fs", generate_f2fs_image},
|
2014-06-18 14:43:18 +08:00
|
|
|
#endif
|
2014-03-12 09:28:15 +08:00
|
|
|
};
|
|
|
|
|
2015-11-03 06:05:57 +08:00
|
|
|
const struct fs_generator* fs_get_generator(const std::string& fs_type) {
|
2015-04-08 11:12:50 +08:00
|
|
|
for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
|
2015-11-03 06:05:57 +08:00
|
|
|
if (fs_type == generators[i].fs_type) {
|
2014-03-12 09:28:15 +08:00
|
|
|
return generators + i;
|
2015-04-08 11:12:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
2014-03-12 09:28:15 +08:00
|
|
|
}
|
|
|
|
|
2017-04-19 07:23:18 +08:00
|
|
|
int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
|
2017-02-07 06:39:31 +08:00
|
|
|
const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
|
2014-03-12 09:28:15 +08:00
|
|
|
{
|
2017-04-19 07:23:18 +08:00
|
|
|
return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
|
2014-03-12 09:28:15 +08:00
|
|
|
}
|