blob: 8539e2382589009fd6599d539b7d17c44eb6374a [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"
4#include "make_ext4fs.h"
JP Abgrall12351582014-06-17 17:01:14 -07005#include "make_f2fs.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07006
7#include <errno.h>
8#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>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070013#include <unistd.h>
14
Elliott Hughesfc797672015-04-07 20:12:50 -070015#include <sparse/sparse.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070016
Paul Crowley8f7f56e2015-11-27 09:29:37 +000017static int generate_ext4_image(int fd, long long partSize, const std::string& initial_dir)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070018{
Paul Crowley8f7f56e2015-11-27 09:29:37 +000019 if (initial_dir.empty()) {
20 make_ext4fs_sparse_fd(fd, partSize, NULL, NULL);
21 } else {
22 make_ext4fs_sparse_fd_directory(fd, partSize, NULL, NULL, initial_dir.c_str());
23 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070024 return 0;
25}
26
JP Abgrall6bd72be2014-06-17 23:43:18 -070027#ifdef USE_F2FS
Paul Crowley8f7f56e2015-11-27 09:29:37 +000028static int generate_f2fs_image(int fd, long long partSize, const std::string& initial_dir)
JP Abgrall12351582014-06-17 17:01:14 -070029{
Paul Crowley8f7f56e2015-11-27 09:29:37 +000030 if (!initial_dir.empty()) {
31 fprintf(stderr, "Unable to set initial directory on F2FS filesystem\n");
32 return -1;
33 }
JP Abgrall6bd72be2014-06-17 23:43:18 -070034 return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
JP Abgrall12351582014-06-17 17:01:14 -070035}
JP Abgrall6bd72be2014-06-17 23:43:18 -070036#endif
JP Abgrall12351582014-06-17 17:01:14 -070037
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070038static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -070039 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +000040
41 //returns 0 or error value
42 int (*generate)(int fd, long long partSize, const std::string& initial_dir);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070043
44} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -070045 { "ext4", generate_ext4_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -070046#ifdef USE_F2FS
JP Abgrall12351582014-06-17 17:01:14 -070047 { "f2fs", generate_f2fs_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -070048#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070049};
50
Elliott Hughes8ab9a322015-11-02 14:05:57 -080051const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -070052 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -080053 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070054 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -070055 }
56 }
57 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070058}
59
Paul Crowley8f7f56e2015-11-27 09:29:37 +000060int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize,
61 const std::string& initial_dir)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070062{
Paul Crowley8f7f56e2015-11-27 09:29:37 +000063 return gen->generate(tmpFileNo, partSize, initial_dir);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064}