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> |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 7 | #include <fcntl.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 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> |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame^] | 13 | #ifndef WIN32 |
| 14 | #include <sys/wait.h> |
| 15 | #endif |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 16 | #include <unistd.h> |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame^] | 17 | #include <vector> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 18 | |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame^] | 19 | #include <android-base/file.h> |
| 20 | #include <android-base/stringprintf.h> |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 21 | #include <android-base/unique_fd.h> |
Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 22 | #include <ext4_utils/make_ext4fs.h> |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 23 | #include <sparse/sparse.h> |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 24 | |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame^] | 25 | using android::base::StringPrintf; |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 26 | using android::base::unique_fd; |
| 27 | |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame^] | 28 | #ifdef WIN32 |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 29 | static int generate_ext4_image(const char* fileName, long long partSize, const std::string& initial_dir, |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 30 | unsigned eraseBlkSize, unsigned logicalBlkSize) |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 31 | { |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 32 | unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)); |
| 33 | if (fd == -1) { |
| 34 | fprintf(stderr, "Unable to open output file for EXT4 filesystem: %s\n", strerror(errno)); |
| 35 | return -1; |
| 36 | } |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 37 | if (initial_dir.empty()) { |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 38 | make_ext4fs_sparse_fd_align(fd, partSize, NULL, NULL, eraseBlkSize, logicalBlkSize); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 39 | } else { |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 40 | make_ext4fs_sparse_fd_directory_align(fd, partSize, NULL, NULL, initial_dir.c_str(), |
| 41 | eraseBlkSize, logicalBlkSize); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 42 | } |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 43 | return 0; |
| 44 | } |
Jin Qian | 4afba66 | 2017-04-18 18:19:12 -0700 | [diff] [blame^] | 45 | #else |
| 46 | static int exec_e2fs_cmd(const char* path, char* const argv[]) { |
| 47 | int status; |
| 48 | pid_t child; |
| 49 | if ((child = fork()) == 0) { |
| 50 | setenv("MKE2FS_CONFIG", "", 1); |
| 51 | execvp(path, argv); |
| 52 | _exit(EXIT_FAILURE); |
| 53 | } |
| 54 | if (child < 0) { |
| 55 | fprintf(stderr, "%s failed with fork %s\n", path, strerror(errno)); |
| 56 | return -1; |
| 57 | } |
| 58 | if (TEMP_FAILURE_RETRY(waitpid(child, &status, 0)) == -1) { |
| 59 | fprintf(stderr, "%s failed with waitpid %s\n", path, strerror(errno)); |
| 60 | return -1; |
| 61 | } |
| 62 | int ret = -1; |
| 63 | if (WIFEXITED(status)) { |
| 64 | ret = WEXITSTATUS(status); |
| 65 | if (ret != 0) { |
| 66 | fprintf(stderr, "%s failed with status %d\n", path, ret); |
| 67 | } |
| 68 | } |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int generate_ext4_image(const char* fileName, long long partSize, |
| 73 | const std::string& initial_dir, unsigned eraseBlkSize, |
| 74 | unsigned logicalBlkSize) { |
| 75 | static constexpr int block_size = 4096; |
| 76 | const std::string exec_dir = android::base::GetExecutableDirectory(); |
| 77 | |
| 78 | const std::string mke2fs_path = exec_dir + "/mke2fs"; |
| 79 | std::vector<const char*> mke2fs_args = {mke2fs_path.c_str(), "-t", "ext4", "-b"}; |
| 80 | |
| 81 | std::string block_size_str = std::to_string(block_size); |
| 82 | mke2fs_args.push_back(block_size_str.c_str()); |
| 83 | |
| 84 | std::string ext_attr = "android_sparse"; |
| 85 | if (eraseBlkSize != 0 && logicalBlkSize != 0) { |
| 86 | int raid_stride = logicalBlkSize / block_size; |
| 87 | int raid_stripe_width = eraseBlkSize / block_size; |
| 88 | // stride should be the max of 8kb and logical block size |
| 89 | if (logicalBlkSize != 0 && logicalBlkSize < 8192) raid_stride = 8192 / block_size; |
| 90 | ext_attr += StringPrintf(",stride=%d,stripe-width=%d", raid_stride, raid_stripe_width); |
| 91 | } |
| 92 | mke2fs_args.push_back("-E"); |
| 93 | mke2fs_args.push_back(ext_attr.c_str()); |
| 94 | mke2fs_args.push_back(fileName); |
| 95 | |
| 96 | std::string size_str = std::to_string(partSize / block_size); |
| 97 | mke2fs_args.push_back(size_str.c_str()); |
| 98 | mke2fs_args.push_back(nullptr); |
| 99 | |
| 100 | int ret = exec_e2fs_cmd(mke2fs_args[0], const_cast<char**>(mke2fs_args.data())); |
| 101 | if (ret != 0) { |
| 102 | fprintf(stderr, "mke2fs failed: %d\n", ret); |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | if (initial_dir.empty()) { |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | const std::string e2fsdroid_path = exec_dir + "/e2fsdroid"; |
| 111 | std::vector<const char*> e2fsdroid_args = {e2fsdroid_path.c_str(), "-f", initial_dir.c_str(), |
| 112 | fileName, nullptr}; |
| 113 | |
| 114 | ret = exec_e2fs_cmd(e2fsdroid_args[0], const_cast<char**>(e2fsdroid_args.data())); |
| 115 | if (ret != 0) { |
| 116 | fprintf(stderr, "e2fsdroid failed: %d\n", ret); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | #endif |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 123 | |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 124 | #ifdef USE_F2FS |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 125 | static int generate_f2fs_image(const char* fileName, long long partSize, const std::string& initial_dir, |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 126 | unsigned /* unused */, unsigned /* unused */) |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 127 | { |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 128 | if (!initial_dir.empty()) { |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 129 | fprintf(stderr, "Unable to set initial directory on F2FS filesystem: %s\n", strerror(errno)); |
| 130 | return -1; |
| 131 | } |
| 132 | unique_fd fd(open(fileName, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)); |
| 133 | if (fd == -1) { |
| 134 | fprintf(stderr, "Unable to open output file for F2FS filesystem: %s\n", strerror(errno)); |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 135 | return -1; |
| 136 | } |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 137 | return make_f2fs_sparse_fd(fd, partSize, NULL, NULL); |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 138 | } |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 139 | #endif |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 140 | |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 141 | static const struct fs_generator { |
Elliott Hughes | b3748de | 2015-06-23 20:27:58 -0700 | [diff] [blame] | 142 | const char* fs_type; //must match what fastboot reports for partition type |
Paul Crowley | 8f7f56e | 2015-11-27 09:29:37 +0000 | [diff] [blame] | 143 | |
| 144 | //returns 0 or error value |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 145 | int (*generate)(const char* fileName, long long partSize, const std::string& initial_dir, |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 146 | unsigned eraseBlkSize, unsigned logicalBlkSize); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 147 | |
| 148 | } generators[] = { |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 149 | { "ext4", generate_ext4_image}, |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 150 | #ifdef USE_F2FS |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 151 | { "f2fs", generate_f2fs_image}, |
JP Abgrall | 6bd72be | 2014-06-17 23:43:18 -0700 | [diff] [blame] | 152 | #endif |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 155 | const struct fs_generator* fs_get_generator(const std::string& fs_type) { |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 156 | for (size_t i = 0; i < sizeof(generators) / sizeof(*generators); i++) { |
Elliott Hughes | 8ab9a32 | 2015-11-02 14:05:57 -0800 | [diff] [blame] | 157 | if (fs_type == generators[i].fs_type) { |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 158 | return generators + i; |
Elliott Hughes | fc79767 | 2015-04-07 20:12:50 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | return nullptr; |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 164 | int fs_generator_generate(const struct fs_generator* gen, const char* fileName, long long partSize, |
Connor O'Brien | ce16a8a | 2017-02-06 14:39:31 -0800 | [diff] [blame] | 165 | const std::string& initial_dir, unsigned eraseBlkSize, unsigned logicalBlkSize) |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 166 | { |
Jin Qian | 4a33582 | 2017-04-18 16:23:18 -0700 | [diff] [blame] | 167 | return gen->generate(fileName, partSize, initial_dir, eraseBlkSize, logicalBlkSize); |
Dmitry Grinberg | e6f3e9b | 2014-03-11 18:28:15 -0700 | [diff] [blame] | 168 | } |