blob: 687295c91edfbe2070c026011450d79f4d7533d5 [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
Darth98af7fe22024-01-18 20:00:44 +0000113static bool is_AB_for_repacker() {
114 std::string slot = android::base::GetProperty("ro.boot.slot_suffix", "");
115 if (slot.empty())
116 slot = android::base::GetProperty("ro.boot.slot", "");
117 return !slot.empty();
118}
bigbiffad58e1b2020-07-06 20:24:34 -0400119
Darth98af7fe22024-01-18 20:00:44 +0000120bool twrpRepacker::Repack_Image_And_Flash(const std::string& Target_Image, const struct Repack_Options_struct& Repack_Options) {
bigbiffad58e1b2020-07-06 20:24:34 -0400121 if (!TWFunc::Path_Exists("/system/bin/magiskboot")) {
122 LOGERR("Image repacking tool not present in this TWRP build!");
123 return false;
124 }
Darth98af7fe22024-01-18 20:00:44 +0000125
126 bool recompress = false;
127 bool is_vendor_boot = false;
128 bool is_vendor_boot_v4 = false;
129 std::string dest_partition = "/boot";
130 std::string ramdisk_cpio = "ramdisk.cpio";
131
132 #ifdef BOARD_MOVE_RECOVERY_RESOURCES_TO_VENDOR_BOOT
133 dest_partition = "/vendor_boot";
134 is_vendor_boot = true;
135 if (DataManager::GetIntValue("tw_boot_header_version") == 4) {
136 is_vendor_boot_v4 = true;
137 ramdisk_cpio = "vendor_ramdisk_recovery.cpio";
138 LOGINFO("Vendor_boot with v4 header\n");
139 } else {
140 LOGINFO("Vendor_boot with v3 header\n");
141 }
142 #else
143 // we shouldn't reach here, because of the code in twrpRepacker::Flash_Current_Twrp(); but if we do, then handle it
144 if (PartitionManager.Find_Partition_By_Path("/recovery") && is_AB_for_repacker()) {
145 dest_partition = "/recovery";
146 }
147 #endif
148
149 if (is_vendor_boot || is_vendor_boot_v4) {
150 // placeholder for any specific vendor_boot stuff;
151 // in the meantime, stop the compiler's complaints about unused variables
152 }
153
bigbiffad58e1b2020-07-06 20:24:34 -0400154 DataManager::SetProgress(0);
nebrassy45f66b22021-07-26 22:34:24 +0200155 PartitionManager.Update_System_Details();
Darth98af7fe22024-01-18 20:00:44 +0000156 TWPartition* part = PartitionManager.Find_Partition_By_Path(dest_partition);
bigbiffad58e1b2020-07-06 20:24:34 -0400157 if (part)
158 gui_msg(Msg("unpacking_image=Unpacking {1}...")(part->Get_Display_Name()));
159 else {
Darth98af7fe22024-01-18 20:00:44 +0000160 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(dest_partition.c_str()));
bigbiffad58e1b2020-07-06 20:24:34 -0400161 return false;
162 }
163 if (!Backup_Image_For_Repack(part, REPACK_ORIG_DIR, Repack_Options.Backup_First, gui_lookup("repack", "Repack")))
164 return false;
165 DataManager::SetProgress(.25);
nebrassyac29e692021-05-20 13:03:30 +0200166 if (Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
167 if (!Prepare_Empty_Folder(REPACK_NEW_DIR))
168 return false;
169 image_ramdisk_format = "gzip";
170 } else {
171 gui_msg(Msg("unpacking_image=Unpacking {1}...")(Target_Image));
172 image_ramdisk_format = Unpack_Image(Target_Image, REPACK_NEW_DIR, true);
173 }
bigbiffad58e1b2020-07-06 20:24:34 -0400174 if (image_ramdisk_format.empty())
175 return false;
176 DataManager::SetProgress(.5);
177 gui_msg(Msg("repacking_image=Repacking {1}...")(part->Get_Display_Name()));
178 std::string path = REPACK_NEW_DIR;
179 if (Repack_Options.Type == REPLACE_KERNEL) {
180 // When we replace the kernel, what we really do is copy the boot partition ramdisk into the new image's folder
Darth98af7fe22024-01-18 20:00:44 +0000181 if (TWFunc::copy_file(REPACK_ORIG_DIR + ramdisk_cpio, REPACK_NEW_DIR + ramdisk_cpio, 0644)) {
bigbiffad58e1b2020-07-06 20:24:34 -0400182 LOGERR("Failed to copy ramdisk\n");
183 return false;
184 }
nebrassyac29e692021-05-20 13:03:30 +0200185 } else if (Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
Darth98af7fe22024-01-18 20:00:44 +0000186 if (TWFunc::copy_file(Target_Image, REPACK_ORIG_DIR + ramdisk_cpio, 0644)) {
nebrassyac29e692021-05-20 13:03:30 +0200187 LOGERR("Failed to copy ramdisk\n");
188 return false;
189 }
Darth98af7fe22024-01-18 20:00:44 +0000190 if (TWFunc::copy_file(Target_Image, REPACK_NEW_DIR + ramdisk_cpio, 0644)) {
nebrassyac29e692021-05-20 13:03:30 +0200191 LOGERR("Failed to copy ramdisk\n");
192 return false;
193 }
194 path = REPACK_ORIG_DIR;
bigbiffad58e1b2020-07-06 20:24:34 -0400195 } else if (Repack_Options.Type == REPLACE_RAMDISK) {
196 // Repack the ramdisk
Darth98af7fe22024-01-18 20:00:44 +0000197 if (TWFunc::copy_file(REPACK_NEW_DIR + ramdisk_cpio, REPACK_ORIG_DIR + ramdisk_cpio, 0644)) {
bigbiffad58e1b2020-07-06 20:24:34 -0400198 LOGERR("Failed to copy ramdisk\n");
199 return false;
200 }
201 path = REPACK_ORIG_DIR;
202 } else {
203 LOGERR("Invalid repacking options specified\n");
204 return false;
205 }
206 if (Repack_Options.Disable_Verity)
207 LOGERR("Disabling verity is not implemented yet\n");
208 if (Repack_Options.Disable_Force_Encrypt)
209 LOGERR("Disabling force encrypt is not implemented yet\n");
210 std::string command = "cd " + path + " && /system/bin/magiskboot repack ";
211 if (original_ramdisk_format != image_ramdisk_format) {
bigbiffad58e1b2020-07-06 20:24:34 -0400212 recompress = true;
213 }
214
215 command += path + "boot.img";
216
217 std::string orig_compressed_image(REPACK_ORIG_DIR);
Darth98af7fe22024-01-18 20:00:44 +0000218 orig_compressed_image += ramdisk_cpio;
bigbiffad58e1b2020-07-06 20:24:34 -0400219 std::string copy_compressed_image(REPACK_ORIG_DIR);
220 copy_compressed_image += "ramdisk-1.cpio";
221
222 if (recompress) {
nebrassy73782542021-08-07 00:37:30 +0200223 std::string decompress_cmd = "/system/bin/magiskboot decompress " + orig_compressed_image + " " + copy_compressed_image;
224 if (TWFunc::Exec_Cmd(decompress_cmd) != 0) {
bigbiffad58e1b2020-07-06 20:24:34 -0400225 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
226 return false;
227 }
228 std::rename(copy_compressed_image.c_str(), orig_compressed_image.c_str());
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
236 DataManager::SetProgress(.75);
237 std::string file = "new-boot.img";
Darth98af7fe22024-01-18 20:00:44 +0000238 DataManager::SetValue("tw_flash_partition", dest_partition + ";");
bigbiffad58e1b2020-07-06 20:24:34 -0400239 if (!PartitionManager.Flash_Image(path, file)) {
240 LOGINFO("Error flashing new image\n");
241 return false;
242 }
243 DataManager::SetProgress(1);
244 TWFunc::removeDir(REPACK_ORIG_DIR, false);
Darth9c9cc4ac2022-02-03 22:17:49 +0000245 if (part->Is_SlotSelect()) {
246 if (Repack_Options.Type == REPLACE_RAMDISK || Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
247 LOGINFO("Switching slots to flash ramdisk to both partitions\n");
248 string Current_Slot = PartitionManager.Get_Active_Slot_Display();
249 if (Current_Slot == "A")
250 PartitionManager.Override_Active_Slot("B");
251 else
252 PartitionManager.Override_Active_Slot("A");
253 DataManager::SetProgress(.25);
254 if (!Backup_Image_For_Repack(part, REPACK_ORIG_DIR, Repack_Options.Backup_First, gui_lookup("repack", "Repack")))
255 return false;
Darth98af7fe22024-01-18 20:00:44 +0000256 if (TWFunc::copy_file(REPACK_NEW_DIR + ramdisk_cpio, REPACK_ORIG_DIR + ramdisk_cpio, 0644)) {
Darth9c9cc4ac2022-02-03 22:17:49 +0000257 LOGERR("Failed to copy ramdisk\n");
258 return false;
259 }
260 path = REPACK_ORIG_DIR;
261 std::string command = "cd " + path + " && /system/bin/magiskboot repack ";
bigbiffad58e1b2020-07-06 20:24:34 -0400262
Darth9c9cc4ac2022-02-03 22:17:49 +0000263 if (original_ramdisk_format != image_ramdisk_format) {
264 recompress = true;
265 }
266 command += path + "boot.img";
bigbiffad58e1b2020-07-06 20:24:34 -0400267
Darth9c9cc4ac2022-02-03 22:17:49 +0000268 if (recompress) {
269 std::string decompress_cmd = "/system/bin/magiskboot decompress " + orig_compressed_image + " " + copy_compressed_image;
270 if (TWFunc::Exec_Cmd(decompress_cmd) != 0) {
271 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
272 return false;
273 }
274 std::rename(copy_compressed_image.c_str(), orig_compressed_image.c_str());
275 }
276
277 if (TWFunc::Exec_Cmd(command) != 0) {
bigbiffad58e1b2020-07-06 20:24:34 -0400278 gui_msg(Msg(msg::kError, "repack_error=Error repacking image."));
279 return false;
280 }
Darth9c9cc4ac2022-02-03 22:17:49 +0000281 DataManager::SetProgress(.75);
282 std::string file = "new-boot.img";
Darth98af7fe22024-01-18 20:00:44 +0000283 DataManager::SetValue("tw_flash_partition", dest_partition + ";");
Darth9c9cc4ac2022-02-03 22:17:49 +0000284 if (!PartitionManager.Flash_Image(path, file)) {
285 LOGINFO("Error flashing new image\n");
286 return false;
287 }
288 DataManager::SetProgress(1);
289 TWFunc::removeDir(REPACK_ORIG_DIR, false);
bigbiffad58e1b2020-07-06 20:24:34 -0400290 }
Darth9c9cc4ac2022-02-03 22:17:49 +0000291 }
bigbiffd1e556a2021-08-07 14:34:06 -0400292 TWFunc::removeDir(REPACK_NEW_DIR, false);
Darth98af7fe22024-01-18 20:00:44 +0000293 if (dest_partition == "/boot")
294 gui_msg(Msg(msg::kWarning, "repack_overwrite_warning=If device was previously rooted, then root has been overwritten and will need to be reinstalled."));
nebrassy07c4b902021-11-21 03:32:47 +0100295 string Current_Slot = PartitionManager.Get_Active_Slot_Display();
Darth98af7fe22024-01-18 20:00:44 +0000296 if (Current_Slot == "A")
297 PartitionManager.Override_Active_Slot("B");
298 else
299 PartitionManager.Override_Active_Slot("A");
bigbiffd1e556a2021-08-07 14:34:06 -0400300 return true;
bigbiffad58e1b2020-07-06 20:24:34 -0400301}
nebrassyac29e692021-05-20 13:03:30 +0200302
303bool twrpRepacker::Flash_Current_Twrp() {
Darth98af7fe22024-01-18 20:00:44 +0000304 // A/B with dedicated recovery partition
305 std::string slot = android::base::GetProperty("ro.boot.slot_suffix", "");
306 if (slot.empty())
307 slot = android::base::GetProperty("ro.boot.slot", "");
308 if (!slot.empty() && PartitionManager.Find_Partition_By_Path("/recovery")) {
309 std::string root,src, dest;
310 std::string dest_partition = "/recovery";
311 root = "/dev/block/bootdevice/by-name" + dest_partition;
312 if (slot == "_a" || slot == "a") {
313 src = root + "_a";
314 dest= root + "_b";
315 }
316 else {
317 src = root + "_b";
318 dest= root + "_a";
319 }
320 PartitionManager.Unlock_Block_Partitions();
321
322 // only copy the relevant active slot to the inactive slot, on the basis that the recovery currently running
323 // in the active slot can simply be copied over to the inactive slot, so that both have the same recovery image
324 std::string command = "dd bs=1048576 if=" + src + " of=" + dest;
325 LOGINFO("Command=%s\n", command.c_str());
326
327 if (TWFunc::Exec_Cmd(command) != 0) {
328 LOGERR("Failed to flash the %s image\n", dest_partition.c_str());
329 return false;
330 }
331 else {
332 gui_print("Finished flashing the %s image\n", dest_partition.c_str());
333 return true;
334 }
335 // if we reach here, something is awry - bale out
336 return false;
337 }
338
339 if (!TWFunc::Path_Exists("/ramdisk-files.txt")) {
nebrassyac29e692021-05-20 13:03:30 +0200340 LOGERR("can not find ramdisk-files.txt");
341 return false;
342 }
343 Repack_Options_struct Repack_Options;
344 Repack_Options.Disable_Verity = false;
345 Repack_Options.Disable_Force_Encrypt = false;
346 Repack_Options.Type = REPLACE_RAMDISK_UNPACKED;
347 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
348 std::string verifyfiles = "cd / && sha256sum --status -c ramdisk-files.sha256sum";
349 if (TWFunc::Exec_Cmd(verifyfiles) != 0) {
350 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."));
351 return false;
352 }
353 std::string command = "cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/currentramdisk.cpio && /system/bin/gzip -f /tmp/currentramdisk.cpio";
354 if (TWFunc::Exec_Cmd(command) != 0) {
355 gui_msg(Msg(msg::kError, "create_ramdisk_error=failed to create ramdisk to flash."));
356 return false;
357 }
358 if (!Repack_Image_And_Flash("/tmp/currentramdisk.cpio.gz", Repack_Options))
359 return false;
Darth98af7fe22024-01-18 20:00:44 +0000360 else
361 return true;
nebrassyac29e692021-05-20 13:03:30 +0200362}