2015-11-27 10:29:37 +01:00
|
|
|
#include "fs.h"
|
|
|
|
|
2014-03-12 02:28:15 +01:00
|
|
|
#include "fastboot.h"
|
2014-06-18 02:01:14 +02:00
|
|
|
#include "make_f2fs.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>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-04-19 01:23:18 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2016-10-06 02:53:30 +02:00
|
|
|
#include <ext4_utils/make_ext4fs.h>
|
2015-04-08 05:12:50 +02:00
|
|
|
#include <sparse/sparse.h>
|
2014-03-12 02:28:15 +01:00
|
|
|
|
2017-04-19 01:23:18 +02:00
|
|
|
using android::base::unique_fd;
|
|
|
|
|
|
|
|
static int generate_ext4_image(const char* fileName, long long partSize, const std::string& initial_dir,
|
2017-02-06 23:39:31 +01:00
|
|
|
unsigned eraseBlkSize, unsigned logicalBlkSize)
|
2014-03-12 02:28:15 +01:00
|
|
|
{
|
2017-04-19 01:23:18 +02: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 10:29:37 +01:00
|
|
|
if (initial_dir.empty()) {
|
2017-02-06 23:39:31 +01:00
|
|
|
make_ext4fs_sparse_fd_align(fd, partSize, NULL, NULL, eraseBlkSize, logicalBlkSize);
|
2015-11-27 10:29:37 +01:00
|
|
|
} else {
|
2017-02-06 23:39:31 +01:00
|
|
|
make_ext4fs_sparse_fd_directory_align(fd, partSize, NULL, NULL, initial_dir.c_str(),
|
|
|
|
eraseBlkSize, logicalBlkSize);
|
2015-11-27 10:29:37 +01:00
|
|
|
}
|
2014-03-12 02:28:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-18 08:43:18 +02:00
|
|
|
#ifdef USE_F2FS
|
2017-04-19 01:23:18 +02:00
|
|
|
static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
|
2017-02-06 23:39:31 +01:00
|
|
|
unsigned /* unused */, unsigned /* unused */)
|
2014-06-18 02:01:14 +02:00
|
|
|
{
|
2015-11-27 10:29:37 +01:00
|
|
|
if (!initial_dir.empty()) {
|
2017-04-19 01:23:18 +02: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 10:29:37 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-06-18 08:43:18 +02:00
|
|
|
return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
|
2014-06-18 02:01:14 +02:00
|
|
|
}
|
2014-06-18 08:43:18 +02:00
|
|
|
#endif
|
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
|
2017-04-19 01:23:18 +02:00
|
|
|
int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
|
2017-02-06 23:39:31 +01:00
|
|
|
unsigned eraseBlkSize, unsigned logicalBlkSize);
|
2014-03-12 02:28:15 +01:00
|
|
|
|
|
|
|
} generators[] = {
|
2014-06-18 02:01:14 +02:00
|
|
|
{ "ext4", generate_ext4_image},
|
2014-06-18 08:43:18 +02:00
|
|
|
#ifdef USE_F2FS
|
2014-06-18 02:01:14 +02:00
|
|
|
{ "f2fs", generate_f2fs_image},
|
2014-06-18 08:43:18 +02:00
|
|
|
#endif
|
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,
|
2017-02-06 23:39:31 +01:00
|
|
|
const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
|
2014-03-12 02:28:15 +01:00
|
|
|
{
|
2017-04-19 01:23:18 +02:00
|
|
|
return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
|
2014-03-12 02:28:15 +01:00
|
|
|
}
|