blob: e41dc23996e5880fe8e619f8290ffa33c172ee58 [file] [log] [blame]
bigbiff32cbabe2020-04-12 19:16:19 -04001#include "twrpApex.hpp"
2
3namespace fs = std::filesystem;
4
5bool 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
30std::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());
bigbiffad58e1b2020-07-06 20:24:34 -040049 std::string path(APEX_BASE);
50 path = path + baseFile;
bigbiff32cbabe2020-04-12 19:16:19 -040051 int fd = open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
52 ret = ExtractEntryToFile(handle, &entry, fd);
53 if (ret != 0) {
54 LOGERR("unable to extract %s\n", path.c_str());
55 close(fd);
56 CloseArchive(handle);
57 return nullptr;
58 }
59
60 close(fd);
61 CloseArchive(handle);
62 return path;
63}
64
65bool twrpApex::createLoopBackDevices(size_t count) {
66 size_t existing_loop_device_count = 0;
67
68 for (const auto& entry : fs::directory_iterator(LOOP_BLOCK_DEVICE_DIR)) {
69 if (entry.is_block_file() && entry.path().string().find("loop") != std::string::npos) {
70 existing_loop_device_count++;
71 }
72 }
73
74 if (existing_loop_device_count < count) {
75 size_t devices_to_create = count - existing_loop_device_count;
76 for (size_t i = existing_loop_device_count; i < (devices_to_create + existing_loop_device_count); ++i) {
77 std::string loop_device = LOOP_BLOCK_DEVICE_DIR;
78 loop_device = loop_device + "loop" + std::to_string(i);
79 int ret = mknod(loop_device.c_str(), S_IFBLK | S_IRUSR | S_IWUSR , makedev(7, i));
80 if (ret != 0) {
81 LOGERR("unable to create loop device: %s\n", loop_device.c_str());
82 return false;
83 }
84 }
85 }
86 return true;
87}
88
89bool twrpApex::loadApexImage(std::string fileToMount, size_t loop_device_number) {
90 struct loop_info64 info;
91
92 int fd = open(fileToMount.c_str(), O_RDONLY);
93 if (fd < 0) {
94 LOGERR("unable to open apex file: %s\n", fileToMount.c_str());
95 return false;
96 }
97
98 std::string loop_device = "/dev/block/loop" + std::to_string(loop_device_number);
99 int loop_fd = open(loop_device.c_str(), O_RDONLY);
100 if (loop_fd < 0) {
101 LOGERR("unable to open loop device: %s\n", loop_device.c_str());
102 close(fd);
103 return false;
104 }
105
106 if (ioctl(loop_fd, LOOP_SET_FD, fd) < 0) {
107 LOGERR("failed to mount %s to loop device %s\n", fileToMount.c_str(), loop_device.c_str());
108 close(fd);
109 close(loop_fd);
110 return false;
111 }
112
113 close(fd);
114
115 memset(&info, 0, sizeof(struct loop_info64));
116 if (ioctl(loop_fd, LOOP_SET_STATUS64, &info)) {
117 LOGERR("failed to mount loop: %s: %s\n", fileToMount.c_str(), strerror(errno));
118 close(loop_fd);
119 return false;
120 }
121 close(loop_fd);
122 std::string bind_mount = fileToMount + "-mount";
123 int ret = mkdir(bind_mount.c_str(), 0666);
124 if (ret != 0) {
125 LOGERR("unable to create mount directory: %s\n", bind_mount.c_str());
126 return false;
127 }
128
129 ret = mount(loop_device.c_str(), bind_mount.c_str(), "ext4", MS_RDONLY, nullptr);
130 if (ret != 0) {
131 LOGERR("unable to mount loop device %s to %s\n", loop_device.c_str(), bind_mount.c_str());
132 return false;
133 }
134
135 return true;
136}