blob: 99ca7dd81e504213aa087d28a21f883b3369dd8f [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>
Jin Qian4a335822017-04-18 16:23:18 -07007#include <fcntl.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07008#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
Jin Qian4a335822017-04-18 16:23:18 -070015#include <android-base/unique_fd.h>
Tao Bao6d881d62016-10-05 17:53:30 -070016#include <ext4_utils/make_ext4fs.h>
Elliott Hughesfc797672015-04-07 20:12:50 -070017#include <sparse/sparse.h>
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070018
Jin Qian4a335822017-04-18 16:23:18 -070019using android::base::unique_fd;
20
21static int generate_ext4_image(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -080022 unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070023{
Jin Qian4a335822017-04-18 16:23:18 -070024 unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
25 if (fd == -1) {
26 fprintf(stderr, "Unable to open output file for EXT4 filesystem: %s\n", strerror(errno));
27 return -1;
28 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +000029 if (initial_dir.empty()) {
Connor O'Brience16a8a2017-02-06 14:39:31 -080030 make_ext4fs_sparse_fd_align(fd, partSize, NULL, NULL, eraseBlkSize, logicalBlkSize);
Paul Crowley8f7f56e2015-11-27 09:29:37 +000031 } else {
Connor O'Brience16a8a2017-02-06 14:39:31 -080032 make_ext4fs_sparse_fd_directory_align(fd, partSize, NULL, NULL, initial_dir.c_str(),
33 eraseBlkSize, logicalBlkSize);
Paul Crowley8f7f56e2015-11-27 09:29:37 +000034 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070035 return 0;
36}
37
JP Abgrall6bd72be2014-06-17 23:43:18 -070038#ifdef USE_F2FS
Jin Qian4a335822017-04-18 16:23:18 -070039static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -080040 unsigned /* unused */, unsigned /* unused */)
JP Abgrall12351582014-06-17 17:01:14 -070041{
Paul Crowley8f7f56e2015-11-27 09:29:37 +000042 if (!initial_dir.empty()) {
Jin Qian4a335822017-04-18 16:23:18 -070043 fprintf(stderr, "Unable to set initial directory on F2FS filesystem: %s\n", strerror(errno));
44 return -1;
45 }
46 unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR));
47 if (fd == -1) {
48 fprintf(stderr, "Unable to open output file for F2FS filesystem: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +000049 return -1;
50 }
JP Abgrall6bd72be2014-06-17 23:43:18 -070051 return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
JP Abgrall12351582014-06-17 17:01:14 -070052}
JP Abgrall6bd72be2014-06-17 23:43:18 -070053#endif
JP Abgrall12351582014-06-17 17:01:14 -070054
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070055static const struct fs_generator {
Elliott Hughesb3748de2015-06-23 20:27:58 -070056 const char* fs_type; //must match what fastboot reports for partition type
Paul Crowley8f7f56e2015-11-27 09:29:37 +000057
58 //returns 0 or error value
Jin Qian4a335822017-04-18 16:23:18 -070059 int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir,
Connor O'Brience16a8a2017-02-06 14:39:31 -080060 unsigned eraseBlkSize, unsigned logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070061
62} generators[] = {
JP Abgrall12351582014-06-17 17:01:14 -070063 { "ext4", generate_ext4_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -070064#ifdef USE_F2FS
JP Abgrall12351582014-06-17 17:01:14 -070065 { "f2fs", generate_f2fs_image},
JP Abgrall6bd72be2014-06-17 23:43:18 -070066#endif
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070067};
68
Elliott Hughes8ab9a322015-11-02 14:05:57 -080069const struct fs_generator* fs_get_generator(const std::string& fs_type) {
Elliott Hughesfc797672015-04-07 20:12:50 -070070 for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -080071 if (fs_type == generators[i].fs_type) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070072 return generators + i;
Elliott Hughesfc797672015-04-07 20:12:50 -070073 }
74 }
75 return nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070076}
77
Jin Qian4a335822017-04-18 16:23:18 -070078int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize,
Connor O'Brience16a8a2017-02-06 14:39:31 -080079 const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070080{
Jin Qian4a335822017-04-18 16:23:18 -070081 return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070082}