blob: 6ac2ce9026bf4199d8a7d8b64348e79e84e06c7d [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 }
nebrassyafb12ee2021-09-02 09:27:54 -040094 std::string command = "cd " + Temp_Folder_Destination + " && /system/bin/magiskboot unpack -h -n ";
bigbiffad58e1b2020-07-06 20:24:34 -040095 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);
nebrassyac29e692021-05-20 13:03:30 +0200132 if (Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
133 if (!Prepare_Empty_Folder(REPACK_NEW_DIR))
134 return false;
135 image_ramdisk_format = "gzip";
136 } else {
137 gui_msg(Msg("unpacking_image=Unpacking {1}...")(Target_Image));
138 image_ramdisk_format = Unpack_Image(Target_Image, REPACK_NEW_DIR, true);
139 }
bigbiffad58e1b2020-07-06 20:24:34 -0400140 if (image_ramdisk_format.empty())
141 return false;
142 DataManager::SetProgress(.5);
143 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Get_Display_Name()));
144 std::string path = REPACK_NEW_DIR;
145 if (Repack_Options.Type == REPLACE_KERNEL) {
146 // When we replace the kernel, what we really do is copy the boot partition ramdisk into the new image's folder
147 if (TWFunc::copy_file(REPACK_ORIG_DIR "ramdisk.cpio", REPACK_NEW_DIR "ramdisk.cpio", 0644)) {
148 LOGERR("Failed to copy ramdisk\n");
149 return false;
150 }
nebrassyac29e692021-05-20 13:03:30 +0200151 } else if (Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
152 if (TWFunc::copy_file(Target_Image, REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
153 LOGERR("Failed to copy ramdisk\n");
154 return false;
155 }
156 if (TWFunc::copy_file(Target_Image, REPACK_NEW_DIR "ramdisk.cpio", 0644)) {
157 LOGERR("Failed to copy ramdisk\n");
158 return false;
159 }
160 path = REPACK_ORIG_DIR;
bigbiffad58e1b2020-07-06 20:24:34 -0400161 } else if (Repack_Options.Type == REPLACE_RAMDISK) {
162 // Repack the ramdisk
163 if (TWFunc::copy_file(REPACK_NEW_DIR "ramdisk.cpio", REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
164 LOGERR("Failed to copy ramdisk\n");
165 return false;
166 }
167 path = REPACK_ORIG_DIR;
168 } else {
169 LOGERR("Invalid repacking options specified\n");
170 return false;
171 }
172 if (Repack_Options.Disable_Verity)
173 LOGERR("Disabling verity is not implemented yet\n");
174 if (Repack_Options.Disable_Force_Encrypt)
175 LOGERR("Disabling force encrypt is not implemented yet\n");
176 std::string command = "cd " + path + " && /system/bin/magiskboot repack ";
177 if (original_ramdisk_format != image_ramdisk_format) {
bigbiffad58e1b2020-07-06 20:24:34 -0400178 recompress = true;
179 }
180
181 command += path + "boot.img";
182
183 std::string orig_compressed_image(REPACK_ORIG_DIR);
184 orig_compressed_image += "ramdisk.cpio";
185 std::string copy_compressed_image(REPACK_ORIG_DIR);
186 copy_compressed_image += "ramdisk-1.cpio";
187
188 if (recompress) {
nebrassy73782542021-08-07 00:37:30 +0200189 std::string decompress_cmd = "/system/bin/magiskboot decompress " + orig_compressed_image + " " + copy_compressed_image;
190 if (TWFunc::Exec_Cmd(decompress_cmd) != 0) {
bigbiffad58e1b2020-07-06 20:24:34 -0400191 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
192 return false;
193 }
194 std::rename(copy_compressed_image.c_str(), orig_compressed_image.c_str());
195 }
196
197 if (TWFunc::Exec_Cmd(command) != 0) {
198 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
199 return false;
200 }
201
202 DataManager::SetProgress(.75);
203 std::string file = "new-boot.img";
204 DataManager::SetValue("tw_flash_partition", "/boot;");
205 if (!PartitionManager.Flash_Image(path, file)) {
206 LOGINFO("Error flashing new image\n");
207 return false;
208 }
209 DataManager::SetProgress(1);
210 TWFunc::removeDir(REPACK_ORIG_DIR, false);
Darth97ce944d2022-02-03 22:17:49 +0000211 if (part->Is_SlotSelect()) {
212 if (Repack_Options.Type == REPLACE_RAMDISK || Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
213 LOGINFO("Switching slots to flash ramdisk to both partitions\n");
214 string Current_Slot = PartitionManager.Get_Active_Slot_Display();
215 if (Current_Slot == "A")
216 PartitionManager.Override_Active_Slot("B");
217 else
218 PartitionManager.Override_Active_Slot("A");
219 DataManager::SetProgress(.25);
220 if (!Backup_Image_For_Repack(part, REPACK_ORIG_DIR, Repack_Options.Backup_First, gui_lookup("repack", "Repack")))
221 return false;
222 if (TWFunc::copy_file(REPACK_NEW_DIR "ramdisk.cpio", REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
223 LOGERR("Failed to copy ramdisk\n");
224 return false;
225 }
226 path = REPACK_ORIG_DIR;
227 std::string command = "cd " + path + " && /system/bin/magiskboot repack ";
bigbiffad58e1b2020-07-06 20:24:34 -0400228
Darth97ce944d2022-02-03 22:17:49 +0000229 if (original_ramdisk_format != image_ramdisk_format) {
230 recompress = true;
231 }
232 command += path + "boot.img";
bigbiffad58e1b2020-07-06 20:24:34 -0400233
Darth97ce944d2022-02-03 22:17:49 +0000234 if (recompress) {
235 std::string decompress_cmd = "/system/bin/magiskboot decompress " + orig_compressed_image + " " + copy_compressed_image;
236 if (TWFunc::Exec_Cmd(decompress_cmd) != 0) {
237 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
238 return false;
239 }
240 std::rename(copy_compressed_image.c_str(), orig_compressed_image.c_str());
241 }
242
243 if (TWFunc::Exec_Cmd(command) != 0) {
bigbiffad58e1b2020-07-06 20:24:34 -0400244 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
245 return false;
246 }
Darth97ce944d2022-02-03 22:17:49 +0000247 DataManager::SetProgress(.75);
248 std::string file = "new-boot.img";
249 DataManager::SetValue("tw_flash_partition", "/boot;");
250 if (!PartitionManager.Flash_Image(path, file)) {
251 LOGINFO("Error flashing new image\n");
252 return false;
253 }
254 DataManager::SetProgress(1);
255 TWFunc::removeDir(REPACK_ORIG_DIR, false);
bigbiffad58e1b2020-07-06 20:24:34 -0400256 }
Darth97ce944d2022-02-03 22:17:49 +0000257 }
bigbiffd1e556a2021-08-07 14:34:06 -0400258 TWFunc::removeDir(REPACK_NEW_DIR, false);
Captain Throwback210ebe32021-10-03 01:30:44 -0400259 gui_msg(Msg(msg::kWarning, "repack_overwrite_warning=If device was previously rooted, then root has been overwritten and will need to be reinstalled."));
nebrassy0fc1ee22021-11-21 03:32:47 +0100260 string Current_Slot = PartitionManager.Get_Active_Slot_Display();
261 if (Current_Slot == "A")
262 PartitionManager.Override_Active_Slot("B");
263 else
264 PartitionManager.Override_Active_Slot("A");
bigbiffd1e556a2021-08-07 14:34:06 -0400265 return true;
bigbiffad58e1b2020-07-06 20:24:34 -0400266}
nebrassyac29e692021-05-20 13:03:30 +0200267
268bool twrpRepacker::Flash_Current_Twrp() {
269if (!TWFunc::Path_Exists("/ramdisk-files.txt")) {
270 LOGERR("can not find ramdisk-files.txt");
271 return false;
272 }
273 Repack_Options_struct Repack_Options;
274 Repack_Options.Disable_Verity = false;
275 Repack_Options.Disable_Force_Encrypt = false;
276 Repack_Options.Type = REPLACE_RAMDISK_UNPACKED;
277 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
278 std::string verifyfiles = "cd / && sha256sum --status -c ramdisk-files.sha256sum";
279 if (TWFunc::Exec_Cmd(verifyfiles) != 0) {
280 gui_msg(Msg(msg::kError, "modified_ramdisk_error=ramdisk files have been modified, unable to create ramdisk to flash, fastboot boot twrp and try this option again or use the Install Recovery Ramdisk option."));
281 return false;
282 }
283 std::string command = "cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/currentramdisk.cpio && /system/bin/gzip -f /tmp/currentramdisk.cpio";
284 if (TWFunc::Exec_Cmd(command) != 0) {
285 gui_msg(Msg(msg::kError, "create_ramdisk_error=failed to create ramdisk to flash."));
286 return false;
287 }
288 if (!Repack_Image_And_Flash("/tmp/currentramdisk.cpio.gz", Repack_Options))
289 return false;
290 else
291 return true;
292}