blob: 5d9ccfe0916a674c45ff06df34180ca947310852 [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>
7#include <stdio.h>
8#include <stdlib.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07009#include <string.h>
10#include <sys/stat.h>
11#include <sys/types.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070012#include <unistd.h>
13
Tao Bao6d881d62016-10-05 17:53:30 -070014#include <ext4_utils/make_ext4fs.h>
Elliott Hughesfc797672015-04-07 20:12:50 -070015#include <sparse/sparse.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070016
Connor O'Brience16a8a2017-02-06 14:39:31 -080017static int generate_ext4_image(int fd, long long partSize, const std::string& initial_dir,
18 unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070019{
Paul Crowley8f7f56e2015-11-27 09:29:37 +000020 if (initial_dir.empty()) {
Connor O'Brience16a8a2017-02-06 14:39:31 -080021 make_ext4fs_sparse_fd_align(fd, partSize, NULL, NULL, eraseBlkSize, logicalBlkSize);
Paul Crowley8f7f56e2015-11-27 09:29:37 +000022 } else {
Connor O'Brience16a8a2017-02-06 14:39:31 -080023 make_ext4fs_sparse_fd_directory_align(fd, partSize, NULL, NULL, initial_dir.c_str(),
24 eraseBlkSize, logicalBlkSize);
Paul Crowley8f7f56e2015-11-27 09:29:37 +000025 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070026 return 0;
27}
28
JP Abgrall6bd72be2014-06-17 23:43:18 -070029#ifdef USE_F2FS
Connor O'Brience16a8a2017-02-06 14:39:31 -080030static int generate_f2fs_image(int fd, long long partSize, const std::string& initial_dir,
31 unsigned /* unused */, unsigned /* unused */)
JP Abgrall12351582014-06-17 17:01:14 -070032{
Paul Crowley8f7f56e2015-11-27 09:29:37 +000033 if (!initial_dir.empty()) {
34 fprintf(stderr, "Unable to set initial directory on F2FS filesystem\n");
35 return -1;
36 }
JP Abgrall6bd72be2014-06-17 23:43:18 -070037 return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
JP Abgrall12351582014-06-17 17:01:14 -070038}
JP Abgrall6bd72be2014-06-17 23:43:18 -070039#endif
JP Abgrall12351582014-06-17 17:01:14 -070040
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070041static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -070042 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +000043
44 //returns 0 or error value
Connor O'Brience16a8a2017-02-06 14:39:31 -080045 int (*generate)(int fd, long long partSize, const std::string& initial_dir,
46 unsigned eraseBlkSize, unsigned logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070047
48} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -070049 { "ext4", generate_ext4_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -070050#ifdef USE_F2FS
JP Abgrall12351582014-06-17 17:01:14 -070051 { "f2fs", generate_f2fs_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -070052#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070053};
54
Elliott Hughes8ab9a322015-11-02 14:05:57 -080055const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -070056 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -080057 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070058 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -070059 }
60 }
61 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070062}
63
Paul Crowley8f7f56e2015-11-27 09:29:37 +000064int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize,
Connor O'Brience16a8a2017-02-06 14:39:31 -080065 const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070066{
Connor O'Brience16a8a2017-02-06 14:39:31 -080067 return gen->generate(tmpFileNo, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070068}