Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 1 | #include "fs.h" |
| 2 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 3 | #include "fastboot.h" |
| 4 | #include "make_ext4fs.h" |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 5 | #include "make_f2fs.h" |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 6 | |
| 7 | #include <errno.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 10 | #include <string.h> |
| 11 | #include <sys/stat.h> |
| 12 | #include <sys/types.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 15 | #include <sparse/sparse.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 16 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 17 | static int generate_ext4_image(int fd, long long partSize, const std::string& initial_dir) |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 18 | { |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 19 | 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 Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 24 | return 0; |
| 25 | } |
| 26 | |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 27 | #ifdef USE_F2FS |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 28 | static int generate_f2fs_image(int fd, long long partSize, const std::string& initial_dir) |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 29 | { |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 30 | if (!initial_dir.empty()) { |
| 31 | fprintf(stderr, "Unable to set initial directory on F2FS filesystem\n"); |
| 32 | return -1; |
| 33 | } |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 34 | return make_f2fs_sparse_fd(fd, partSize, NULL, NULL); |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 35 | } |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 36 | #endif |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 37 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 38 | static const struct fs_generator { |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 39 | const char* fs_type; //must match what fastboot reports for partition type |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 40 | |
| 41 | //returns 0 or error value |
| 42 | int (*generate)(int fd, long long partSize, const std::string& initial_dir); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 43 | |
| 44 | } generators[] = { |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 45 | { "ext4", generate_ext4_image}, |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 46 | #ifdef USE_F2FS |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 47 | { "f2fs", generate_f2fs_image}, |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 48 | #endif |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 51 | const struct fs_generator* fs_get_generator(const std::string& fs_type) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 52 | for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 53 | if (fs_type == generators[i].fs_type) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 54 | return generators + i; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | return nullptr; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 60 | int fs_generator_generate(const struct fs_generator* gen, int tmpFileNo, long long partSize, |
| 61 | const std::string& initial_dir) |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 62 | { |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 63 | return gen->generate(tmpFileNo, partSize, initial_dir); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 64 | } |