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