1 #include "fs.h"
2 
3 #include "fastboot.h"
4 #include "make_f2fs.h"
5 
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 
14 #include <ext4_utils/make_ext4fs.h>
15 #include <sparse/sparse.h>
16 
generate_ext4_image(int fd,long long partSize,const std::string & initial_dir,unsigned eraseBlkSize,unsigned logicalBlkSize)17 static int generate_ext4_image(int fd, long long partSize, const std::string& initial_dir,
18                                        unsigned eraseBlkSize, unsigned logicalBlkSize)
19 {
20     if (initial_dir.empty()) {
21         make_ext4fs_sparse_fd_align(fd, partSize, NULL, NULL, eraseBlkSize, logicalBlkSize);
22     } else {
23         make_ext4fs_sparse_fd_directory_align(fd, partSize, NULL, NULL, initial_dir.c_str(),
24                                               eraseBlkSize, logicalBlkSize);
25     }
26     return 0;
27 }
28 
29 #ifdef USE_F2FS
generate_f2fs_image(int fd,long long partSize,const std::string & initial_dir,unsigned,unsigned)30 static int generate_f2fs_image(int fd, long long partSize, const std::string& initial_dir,
31                                unsigned /* unused */, unsigned /* unused */)
32 {
33     if (!initial_dir.empty()) {
34         fprintf(stderr, "Unable to set initial directory on F2FS filesystem\n");
35         return -1;
36     }
37     return make_f2fs_sparse_fd(fd, partSize, NULL, NULL);
38 }
39 #endif
40 
41 static const struct fs_generator {
42     const char* fs_type;  //must match what fastboot reports for partition type
43 
44     //returns 0 or error value
45     int (*generate)(int fd, long long partSize, const std::string& initial_dir,
46                     unsigned eraseBlkSize, unsigned logicalBlkSize);
47 
48 } generators[] = {
49     { "ext4", generate_ext4_image},
50 #ifdef USE_F2FS
51     { "f2fs", generate_f2fs_image},
52 #endif
53 };
54 
fs_get_generator(const std::string & fs_type)55 const struct fs_generator* fs_get_generator(const std::string& fs_type) {
56     for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) {
57         if (fs_type == generators[i].fs_type) {
58             return generators + i;
59         }
60     }
61     return nullptr;
62 }
63 
fs_generator_generate(const struct fs_generator * gen,int tmpFileNo,long long partSize,const std::string & initial_dir,unsigned eraseBlkSize,unsigned logicalBlkSize)64 int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize,
65     const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize)
66 {
67     return gen->generate(tmpFileNo, partSize, initial_dir, eraseBlkSize, logicalBlkSize);
68 }
69