blob: 10be2fcfb0229b2d366a409aa323229c843f81dd [file] [log] [blame]
bigbiffad58e1b2020-07-06 20:24:34 -04001/*
2 Copyright 2013 to 2020 TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <string>
20
21#include "data.hpp"
22#include "partitions.hpp"
23#include "twrp-functions.hpp"
24#include "twrpRepacker.hpp"
25#include "twcommon.h"
26#include "variables.h"
27#include "gui/gui.hpp"
28
29bool twrpRepacker::Prepare_Empty_Folder(const std::string& Folder) {
30 if (TWFunc::Path_Exists(Folder))
31 TWFunc::removeDir(Folder, false);
32 return TWFunc::Recursive_Mkdir(Folder);
33}
34
35bool twrpRepacker::Backup_Image_For_Repack(TWPartition* Part, const std::string& Temp_Folder_Destination,
36 const bool Create_Backup, const std::string& Backup_Name) {
37 if (!Part) {
38 LOGERR("Partition was null!\n");
39 return false;
40 }
41 if (!Prepare_Empty_Folder(Temp_Folder_Destination))
42 return false;
43 std::string target_image = Temp_Folder_Destination + "boot.img";
44 PartitionSettings part_settings;
45 part_settings.Part = Part;
46 if (Create_Backup) {
47 if (PartitionManager.Check_Backup_Name(Backup_Name, true, false) != 0)
48 return false;
49 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, part_settings.Backup_Folder);
50 part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + TWFunc::Get_Current_Date() + " " + Backup_Name + "/";
51 if (!TWFunc::Recursive_Mkdir(part_settings.Backup_Folder))
52 return false;
53 } else
54 part_settings.Backup_Folder = Temp_Folder_Destination;
55 part_settings.adbbackup = false;
56 part_settings.generate_digest = false;
57 part_settings.generate_md5 = false;
58 part_settings.PM_Method = PM_BACKUP;
59 part_settings.progress = NULL;
60 pid_t not_a_pid = 0;
61 if (!Part->Backup(&part_settings, &not_a_pid))
62 return false;
63 std::string backed_up_image = part_settings.Backup_Folder;
64 backed_up_image += Part->Get_Backup_FileName();
65 target_image = Temp_Folder_Destination + "boot.img";
66 if (Create_Backup) {
67 std::string source = part_settings.Backup_Folder + Part->Get_Backup_FileName();
68 if (TWFunc::copy_file(source, target_image, 0644) != 0) {
69 LOGERR("Failed to copy backup file '%s' to temp folder target '%s'\n", source.c_str(), target_image.c_str());
70 return false;
71 }
72 } else {
73 if (rename(backed_up_image.c_str(), target_image.c_str()) != 0) {
74 LOGERR("Failed to rename '%s' to '%s'\n", backed_up_image.c_str(), target_image.c_str());
75 return false;
76 }
77 }
78 original_ramdisk_format = Unpack_Image(target_image, Temp_Folder_Destination, false, false);
79 return !original_ramdisk_format.empty();
80}
81
82std::string twrpRepacker::Unpack_Image(const std::string& Source_Path, const std::string& Temp_Folder_Destination,
83 const bool Copy_Source, const bool Create_Destination) {
84 std::string txt_to_find = "RAMDISK_FMT";
85 if (Create_Destination) {
86 if (!Prepare_Empty_Folder(Temp_Folder_Destination))
87 return std::string();
88 }
89 if (Copy_Source) {
90 std::string destination = Temp_Folder_Destination + "/boot.img";
91 if (TWFunc::copy_file(Source_Path, destination, 0644))
92 return std::string();
93 }
94 std::string command = "cd " + Temp_Folder_Destination + " && /system/bin/magiskboot unpack -h ";
95 command = command + "'" + Source_Path +"'";
96
97 std::string magisk_unpack_output;
98 int ret;
99 if ((ret = TWFunc::Exec_Cmd(command, magisk_unpack_output, true)) != 0) {
100 LOGINFO("Error unpacking %s, ret: %d!\n", Source_Path.c_str(), ret);
101 gui_msg(Msg(msg::kError, "unpack_error=Error unpacking image."));
102 return std::string();
103 }
104 size_t pos = magisk_unpack_output.find(txt_to_find) + txt_to_find.size();
105 std::string ramdisk_format = magisk_unpack_output.substr(pos, magisk_unpack_output.size() - 1);
106 ramdisk_format.erase(std::remove(ramdisk_format.begin(), ramdisk_format.end(), '['), ramdisk_format.end());
107 ramdisk_format.erase(std::remove(ramdisk_format.begin(), ramdisk_format.end(), ']'), ramdisk_format.end());
108 ramdisk_format.erase(std::remove(ramdisk_format.begin(), ramdisk_format.end(), ' '), ramdisk_format.end());
109 ramdisk_format.erase(std::remove(ramdisk_format.begin(), ramdisk_format.end(), '\n'), ramdisk_format.end());
110 return ramdisk_format;
111}
112
113bool twrpRepacker::Repack_Image_And_Flash(const std::string& Target_Image, const struct Repack_Options_struct& Repack_Options) {
114 bool recompress = false;
115
116 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
117 LOGERR("Image repacking tool not present in this TWRP build!");
118 return false;
119 }
120 DataManager::SetProgress(0);
nebrassy45f66b22021-07-26 22:34:24 +0200121 PartitionManager.Update_System_Details();
bigbiffad58e1b2020-07-06 20:24:34 -0400122 TWPartition* part = PartitionManager.Find_Partition_By_Path("/boot");
123 if (part)
124 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Get_Display_Name()));
125 else {
126 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/boot"));
127 return false;
128 }
129 if (!Backup_Image_For_Repack(part, REPACK_ORIG_DIR, Repack_Options.Backup_First, gui_lookup("repack", "Repack")))
130 return false;
131 DataManager::SetProgress(.25);
132 gui_msg(Msg("unpacking_image=Unpacking {1}...")(Target_Image));
133 image_ramdisk_format = Unpack_Image(Target_Image, REPACK_NEW_DIR, true);
134 if (image_ramdisk_format.empty())
135 return false;
136 DataManager::SetProgress(.5);
137 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Get_Display_Name()));
138 std::string path = REPACK_NEW_DIR;
139 if (Repack_Options.Type == REPLACE_KERNEL) {
140 // When we replace the kernel, what we really do is copy the boot partition ramdisk into the new image's folder
141 if (TWFunc::copy_file(REPACK_ORIG_DIR "ramdisk.cpio", REPACK_NEW_DIR "ramdisk.cpio", 0644)) {
142 LOGERR("Failed to copy ramdisk\n");
143 return false;
144 }
145 } else if (Repack_Options.Type == REPLACE_RAMDISK) {
146 // Repack the ramdisk
147 if (TWFunc::copy_file(REPACK_NEW_DIR "ramdisk.cpio", REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
148 LOGERR("Failed to copy ramdisk\n");
149 return false;
150 }
151 path = REPACK_ORIG_DIR;
152 } else {
153 LOGERR("Invalid repacking options specified\n");
154 return false;
155 }
156 if (Repack_Options.Disable_Verity)
157 LOGERR("Disabling verity is not implemented yet\n");
158 if (Repack_Options.Disable_Force_Encrypt)
159 LOGERR("Disabling force encrypt is not implemented yet\n");
160 std::string command = "cd " + path + " && /system/bin/magiskboot repack ";
161 if (original_ramdisk_format != image_ramdisk_format) {
bigbiffad58e1b2020-07-06 20:24:34 -0400162 recompress = true;
163 }
164
165 command += path + "boot.img";
166
167 std::string orig_compressed_image(REPACK_ORIG_DIR);
168 orig_compressed_image += "ramdisk.cpio";
169 std::string copy_compressed_image(REPACK_ORIG_DIR);
170 copy_compressed_image += "ramdisk-1.cpio";
171
172 if (recompress) {
nebrassy73782542021-08-07 00:37:30 +0200173 std::string decompress_cmd = "/system/bin/magiskboot decompress " + orig_compressed_image + " " + copy_compressed_image;
174 if (TWFunc::Exec_Cmd(decompress_cmd) != 0) {
bigbiffad58e1b2020-07-06 20:24:34 -0400175 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
176 return false;
177 }
178 std::rename(copy_compressed_image.c_str(), orig_compressed_image.c_str());
179 }
180
181 if (TWFunc::Exec_Cmd(command) != 0) {
182 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
183 return false;
184 }
185
186 DataManager::SetProgress(.75);
187 std::string file = "new-boot.img";
188 DataManager::SetValue("tw_flash_partition", "/boot;");
189 if (!PartitionManager.Flash_Image(path, file)) {
190 LOGINFO("Error flashing new image\n");
191 return false;
192 }
193 DataManager::SetProgress(1);
194 TWFunc::removeDir(REPACK_ORIG_DIR, false);
195 if (part->Is_SlotSelect() && Repack_Options.Type == REPLACE_RAMDISK) {
196 LOGINFO("Switching slots to flash ramdisk to both partitions\n");
197 string Current_Slot = PartitionManager.Get_Active_Slot_Display();
198 if (Current_Slot == "A")
bigbiff74a46272021-07-31 19:02:26 -0400199 PartitionManager.Override_Active_Slot("B");
bigbiffad58e1b2020-07-06 20:24:34 -0400200 else
bigbiff74a46272021-07-31 19:02:26 -0400201 PartitionManager.Override_Active_Slot("A");
bigbiffad58e1b2020-07-06 20:24:34 -0400202 DataManager::SetProgress(.25);
203 if (!Backup_Image_For_Repack(part, REPACK_ORIG_DIR, Repack_Options.Backup_First, gui_lookup("repack", "Repack")))
204 return false;
205 if (TWFunc::copy_file(REPACK_NEW_DIR "ramdisk.cpio", REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
206 LOGERR("Failed to copy ramdisk\n");
207 return false;
208 }
209 path = REPACK_ORIG_DIR;
210 std::string command = "cd " + path + " && /system/bin/magiskboot repack ";
211
212 if (original_ramdisk_format != image_ramdisk_format) {
bigbiffad58e1b2020-07-06 20:24:34 -0400213 recompress = true;
214 }
215 command += path + "boot.img";
216
217 if (recompress) {
nebrassy73782542021-08-07 00:37:30 +0200218 std::string decompress_cmd = "/system/bin/magiskboot decompress " + orig_compressed_image + " " + copy_compressed_image;
219 if (TWFunc::Exec_Cmd(decompress_cmd) != 0) {
bigbiffad58e1b2020-07-06 20:24:34 -0400220 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
221 return false;
222 }
223 std::rename(copy_compressed_image.c_str(), orig_compressed_image.c_str());
224 }
225
226 if (TWFunc::Exec_Cmd(command) != 0) {
227 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
228 return false;
229 }
230
231 if (TWFunc::Exec_Cmd(command) != 0) {
232 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
233 return false;
234 }
235 DataManager::SetProgress(.75);
236 std::string file = "new-boot.img";
237 DataManager::SetValue("tw_flash_partition", "/boot;");
238 if (!PartitionManager.Flash_Image(path, file)) {
239 LOGINFO("Error flashing new image\n");
240 return false;
241 }
242 DataManager::SetProgress(1);
243 TWFunc::removeDir(REPACK_ORIG_DIR, false);
bigbiffad58e1b2020-07-06 20:24:34 -0400244}