bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 1 | #include "twrpApex.hpp" |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 2 | #include "twrp-functions.hpp" |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 3 | #include "common.h" |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 4 | |
| 5 | namespace fs = std::filesystem; |
| 6 | |
| 7 | bool twrpApex::loadApexImages() { |
| 8 | std::vector<std::string> apexFiles; |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 9 | std::vector<std::string> checkApexFlatFiles; |
| 10 | #ifdef TW_ADDITIONAL_APEX_FILES |
| 11 | char* additionalFiles = strdup(EXPAND(TW_ADDITIONAL_APEX_FILES)); |
| 12 | char* additionalApexFiles = std::strtok(additionalFiles, " "); |
| 13 | #endif |
| 14 | |
| 15 | apexFiles.push_back(APEX_DIR "/com.android.apex.cts.shim.apex"); |
| 16 | apexFiles.push_back(APEX_DIR "/com.google.android.tzdata2.apex"); |
| 17 | apexFiles.push_back(APEX_DIR "/com.android.tzdata.apex"); |
| 18 | apexFiles.push_back(APEX_DIR "/com.android.art.release.apex"); |
| 19 | apexFiles.push_back(APEX_DIR "/com.google.android.media.swcodec.apex"); |
| 20 | apexFiles.push_back(APEX_DIR "/com.android.media.swcodec.apex"); |
| 21 | |
| 22 | #ifdef TW_ADDITIONAL_APEX_FILES |
| 23 | while(additionalApexFiles) { |
| 24 | std::stringstream apexFile; |
| 25 | apexFile << APEX_DIR << "/" << additionalApexFiles; |
| 26 | apexFiles.push_back(apexFile.str()); |
| 27 | additionalApexFiles = std::strtok(nullptr, " "); |
| 28 | } |
| 29 | #endif |
| 30 | |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 31 | if (access(APEX_DIR, F_OK) != 0) { |
| 32 | LOGERR("Unable to open %s\n", APEX_DIR); |
| 33 | return false; |
| 34 | } |
| 35 | for (const auto& entry : fs::directory_iterator(APEX_DIR)) { |
| 36 | if (entry.is_regular_file()) { |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 37 | checkApexFlatFiles.push_back(entry.path().string()); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 41 | if (checkApexFlatFiles.size() == 0) { |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 42 | // flattened apex directory |
| 43 | LOGINFO("Bind mounting flattened apex directory\n"); |
| 44 | if (mount(APEX_DIR, APEX_BASE, "", MS_BIND, NULL) < 0) { |
| 45 | LOGERR("Unable to bind mount flattened apex directory\n"); |
| 46 | return false; |
| 47 | } |
bigbiff | ab03661 | 2021-06-24 21:31:40 -0400 | [diff] [blame] | 48 | android::base::SetProperty("twrp.apex.flattened", "true"); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 49 | return true; |
| 50 | } |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 51 | if (!mountApexOnLoopbackDevices(apexFiles)) { |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 52 | LOGERR("Unable to create loop devices to mount apex files\n"); |
| 53 | return false; |
| 54 | } |
| 55 | |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
| 59 | std::string twrpApex::unzipImage(std::string file) { |
| 60 | ZipArchiveHandle handle; |
| 61 | int32_t ret = OpenArchive(file.c_str(), &handle); |
| 62 | if (ret != 0) { |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 63 | LOGINFO("unable to open zip archive %s. Reason: %s\n", file.c_str(), strerror(errno)); |
| 64 | return std::string(); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | ZipEntry entry; |
bigbiff | 673c7ae | 2020-12-02 19:44:56 -0500 | [diff] [blame] | 68 | std::string zip_string(APEX_PAYLOAD); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 69 | ret = FindEntry(handle, zip_string, &entry); |
| 70 | if (ret != 0) { |
| 71 | LOGERR("unable to find %s in zip\n", APEX_PAYLOAD); |
| 72 | CloseArchive(handle); |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 73 | return std::string(); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | std::string baseFile = basename(file.c_str()); |
| 77 | std::string path("/tmp/"); |
| 78 | path = path + baseFile; |
| 79 | int fd = open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 80 | ret = ExtractEntryToFile(handle, &entry, fd); |
| 81 | if (ret != 0) { |
| 82 | LOGERR("unable to extract %s\n", path.c_str()); |
| 83 | close(fd); |
| 84 | CloseArchive(handle); |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 85 | return std::string(); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 86 | } |
| 87 | |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 88 | CloseArchive(handle); |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 89 | close(fd); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 90 | return path; |
| 91 | } |
| 92 | |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 93 | bool twrpApex::mountApexOnLoopbackDevices(std::vector<std::string> apexFiles) { |
| 94 | int fd = open(LOOP_CONTROL, O_RDWR | O_CLOEXEC); |
| 95 | if (fd < 0) { |
| 96 | LOGERR("Unable to open %s device. Reason: %s\n", LOOP_CONTROL, strerror(errno)); |
| 97 | return false; |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 98 | } |
| 99 | |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 100 | size_t device_no = 0; |
| 101 | for (auto&& apexFile:apexFiles) { |
| 102 | int num = ioctl(fd, LOOP_CTL_GET_FREE); |
| 103 | std::string loop_device = LOOP_BLOCK_DEVICE_DIR; |
| 104 | loop_device = loop_device + "loop" + std::to_string(num); |
| 105 | if (!TWFunc::Path_Exists(loop_device)) { |
| 106 | int ret = mknod(loop_device.c_str(), S_IFBLK | S_IRUSR | S_IWUSR , makedev(7, device_no)); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 107 | if (ret != 0) { |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 108 | LOGERR("Unable to create loop device: %s\n", loop_device.c_str()); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | } |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 112 | std::string fileToMount = unzipImage(apexFile); |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 113 | if (fileToMount.empty()) { |
| 114 | LOGINFO("Skipping non-existent apex file: %s\n", apexFile.c_str()); |
| 115 | continue; |
| 116 | } |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 117 | bool load_result = loadApexImage(fileToMount, device_no); |
| 118 | if (!load_result) { |
| 119 | return false; |
| 120 | } |
| 121 | device_no++; |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool twrpApex::loadApexImage(std::string fileToMount, size_t loop_device_number) { |
| 127 | struct loop_info64 info; |
| 128 | |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 129 | int fd = open(fileToMount.c_str(), O_RDONLY | O_CLOEXEC); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 130 | if (fd < 0) { |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 131 | LOGERR("unable to open apex file: %s. Reason: %s\n", fileToMount.c_str(), strerror(errno)); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | |
| 135 | std::string loop_device = "/dev/block/loop" + std::to_string(loop_device_number); |
| 136 | int loop_fd = open(loop_device.c_str(), O_RDONLY); |
| 137 | if (loop_fd < 0) { |
| 138 | LOGERR("unable to open loop device: %s\n", loop_device.c_str()); |
| 139 | close(fd); |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | if (ioctl(loop_fd, LOOP_SET_FD, fd) < 0) { |
bigbiff | 7992430 | 2021-05-02 20:06:09 -0400 | [diff] [blame] | 144 | LOGERR("failed to mount %s to loop device %s. Reason: %s\n", fileToMount.c_str(), loop_device.c_str(), |
| 145 | strerror(errno)); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 146 | close(fd); |
| 147 | close(loop_fd); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | close(fd); |
| 152 | |
| 153 | memset(&info, 0, sizeof(struct loop_info64)); |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 154 | strlcpy((char*)info.lo_crypt_name, "twrpApex", LO_NAME_SIZE); |
| 155 | off_t apex_size = lseek(fd, 0, SEEK_END); |
| 156 | info.lo_sizelimit = apex_size; |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 157 | if (ioctl(loop_fd, LOOP_SET_STATUS64, &info)) { |
| 158 | LOGERR("failed to mount loop: %s: %s\n", fileToMount.c_str(), strerror(errno)); |
| 159 | close(loop_fd); |
| 160 | return false; |
| 161 | } |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 162 | if (ioctl(loop_fd, BLKFLSBUF, 0) == -1) { |
| 163 | LOGERR("Unable to flush loop device buffers\n"); |
| 164 | return false; |
| 165 | } |
| 166 | if (ioctl(loop_fd, LOOP_SET_BLOCK_SIZE, 4096) == -1) { |
| 167 | LOGINFO("Failed to set DIRECT_IO buffer size\n"); |
| 168 | } |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 169 | close(loop_fd); |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 170 | |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 171 | std::string bind_mount(APEX_BASE); |
bigbiff | b5a20a8 | 2021-10-17 20:02:17 -0400 | [diff] [blame] | 172 | std::string apex_cleaned_mount = fileToMount; |
| 173 | apex_cleaned_mount = std::regex_replace(apex_cleaned_mount, std::regex("\\.apex"), ""); |
| 174 | |
| 175 | bind_mount = bind_mount + basename(apex_cleaned_mount.c_str()); |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 176 | |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 177 | int ret = mkdir(bind_mount.c_str(), 0666); |
| 178 | if (ret != 0) { |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 179 | LOGERR("Unable to create bind mount directory: %s\n", bind_mount.c_str()); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
| 183 | ret = mount(loop_device.c_str(), bind_mount.c_str(), "ext4", MS_RDONLY, nullptr); |
| 184 | if (ret != 0) { |
bigbiff | 0e97f7e | 2021-02-25 10:16:59 -0500 | [diff] [blame] | 185 | LOGERR("unable to mount loop device %s to %s. Reason: %s\n", loop_device.c_str(), bind_mount.c_str(), strerror(errno)); |
bigbiff | 6411e08 | 2020-09-14 20:13:12 -0400 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
bigbiff | cfa875c | 2021-06-20 16:20:27 -0400 | [diff] [blame] | 191 | |
| 192 | bool twrpApex::Unmount() { |
| 193 | return (umount2(APEX_BASE, MNT_DETACH) == 0); |
| 194 | } |