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