bigbiff | 32cbabe | 2020-04-12 19:16:19 -0400 | [diff] [blame] | 1 | #include "twrpApex.hpp"
|
| 2 |
|
| 3 | namespace fs = std::filesystem;
|
| 4 |
|
| 5 | bool twrpApex::loadApexImages() {
|
| 6 | std::vector<std::string> apexFiles;
|
| 7 | if (access(APEX_DIR, F_OK) != 0) {
|
| 8 | LOGERR("Unable to open %s\n", APEX_DIR);
|
| 9 | return false;
|
| 10 | }
|
| 11 | for (const auto& entry : fs::directory_iterator(APEX_DIR)) {
|
| 12 | if (entry.is_regular_file()) {
|
| 13 | apexFiles.push_back(entry.path().string());
|
| 14 | }
|
| 15 | }
|
| 16 |
|
| 17 | if (!createLoopBackDevices(apexFiles.size())) {
|
| 18 | LOGERR("unable to create loop devices to mount apex files\n");
|
| 19 | return false;
|
| 20 | }
|
| 21 |
|
| 22 | size_t apexFileCount = 0;
|
| 23 | for (auto&& apexFile : apexFiles) {
|
| 24 | std::string fileToMount = unzipImage(apexFile);
|
| 25 | loadApexImage(fileToMount, apexFileCount++);
|
| 26 | }
|
| 27 | return true;
|
| 28 | }
|
| 29 |
|
| 30 | std::string twrpApex::unzipImage(std::string file) {
|
| 31 | ZipArchiveHandle handle;
|
| 32 | int32_t ret = OpenArchive(file.c_str(), &handle);
|
| 33 | if (ret != 0) {
|
| 34 | LOGERR("unable to open zip archive %s\n", file.c_str());
|
| 35 | CloseArchive(handle);
|
| 36 | return nullptr;
|
| 37 | }
|
| 38 |
|
| 39 | ZipEntry entry;
|
| 40 | ZipString zip_string(APEX_PAYLOAD);
|
| 41 | ret = FindEntry(handle, zip_string, &entry);
|
| 42 | if (ret != 0) {
|
| 43 | LOGERR("unable to find %s in zip\n", APEX_PAYLOAD);
|
| 44 | CloseArchive(handle);
|
| 45 | return nullptr;
|
| 46 | }
|
| 47 |
|
| 48 | std::string baseFile = basename(file.c_str());
|
| 49 | std::string path = "/sbin/" + baseFile;
|
| 50 | int fd = open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
| 51 | ret = ExtractEntryToFile(handle, &entry, fd);
|
| 52 | if (ret != 0) {
|
| 53 | LOGERR("unable to extract %s\n", path.c_str());
|
| 54 | close(fd);
|
| 55 | CloseArchive(handle);
|
| 56 | return nullptr;
|
| 57 | }
|
| 58 |
|
| 59 | close(fd);
|
| 60 | CloseArchive(handle);
|
| 61 | return path;
|
| 62 | }
|
| 63 |
|
| 64 | bool twrpApex::createLoopBackDevices(size_t count) {
|
| 65 | size_t existing_loop_device_count = 0;
|
| 66 |
|
| 67 | for (const auto& entry : fs::directory_iterator(LOOP_BLOCK_DEVICE_DIR)) {
|
| 68 | if (entry.is_block_file() && entry.path().string().find("loop") != std::string::npos) {
|
| 69 | existing_loop_device_count++;
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | if (existing_loop_device_count < count) {
|
| 74 | size_t devices_to_create = count - existing_loop_device_count;
|
| 75 | for (size_t i = existing_loop_device_count; i < (devices_to_create + existing_loop_device_count); ++i) {
|
| 76 | std::string loop_device = LOOP_BLOCK_DEVICE_DIR;
|
| 77 | loop_device = loop_device + "loop" + std::to_string(i);
|
| 78 | int ret = mknod(loop_device.c_str(), S_IFBLK | S_IRUSR | S_IWUSR , makedev(7, i));
|
| 79 | if (ret != 0) {
|
| 80 | LOGERR("unable to create loop device: %s\n", loop_device.c_str());
|
| 81 | return false;
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 | return true;
|
| 86 | }
|
| 87 |
|
| 88 | bool twrpApex::loadApexImage(std::string fileToMount, size_t loop_device_number) {
|
| 89 | struct loop_info64 info;
|
| 90 |
|
| 91 | int fd = open(fileToMount.c_str(), O_RDONLY);
|
| 92 | if (fd < 0) {
|
| 93 | LOGERR("unable to open apex file: %s\n", fileToMount.c_str());
|
| 94 | return false;
|
| 95 | }
|
| 96 |
|
| 97 | std::string loop_device = "/dev/block/loop" + std::to_string(loop_device_number);
|
| 98 | int loop_fd = open(loop_device.c_str(), O_RDONLY);
|
| 99 | if (loop_fd < 0) {
|
| 100 | LOGERR("unable to open loop device: %s\n", loop_device.c_str());
|
| 101 | close(fd);
|
| 102 | return false;
|
| 103 | }
|
| 104 |
|
| 105 | if (ioctl(loop_fd, LOOP_SET_FD, fd) < 0) {
|
| 106 | LOGERR("failed to mount %s to loop device %s\n", fileToMount.c_str(), loop_device.c_str());
|
| 107 | close(fd);
|
| 108 | close(loop_fd);
|
| 109 | return false;
|
| 110 | }
|
| 111 |
|
| 112 | close(fd);
|
| 113 |
|
| 114 | memset(&info, 0, sizeof(struct loop_info64));
|
| 115 | if (ioctl(loop_fd, LOOP_SET_STATUS64, &info)) {
|
| 116 | LOGERR("failed to mount loop: %s: %s\n", fileToMount.c_str(), strerror(errno));
|
| 117 | close(loop_fd);
|
| 118 | return false;
|
| 119 | }
|
| 120 | close(loop_fd);
|
| 121 | std::string bind_mount = fileToMount + "-mount";
|
| 122 | int ret = mkdir(bind_mount.c_str(), 0666);
|
| 123 | if (ret != 0) {
|
| 124 | LOGERR("unable to create mount directory: %s\n", bind_mount.c_str());
|
| 125 | return false;
|
| 126 | }
|
| 127 |
|
| 128 | ret = mount(loop_device.c_str(), bind_mount.c_str(), "ext4", MS_RDONLY, nullptr);
|
| 129 | if (ret != 0) {
|
| 130 | LOGERR("unable to mount loop device %s to %s\n", loop_device.c_str(), bind_mount.c_str());
|
| 131 | return false;
|
| 132 | }
|
| 133 |
|
| 134 | return true;
|
| 135 | }
|