blob: 9119612926425e71cd73c4a2b089d923baccb662 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * This binary is a workaround for HTC's unlock method that doesn't let
3 * you flash boot while booted to recovery. It is designed to dump
4 * recovery and boot to the sdcard then flash recovery to boot. When
5 * used with a supported recovery, you can reflash the dumped copy of
6 * boot once you enter the recovery.
7 *
8 * This program is free software; you can redistribute it and/or modify
Dees Troy3be70a82013-10-22 14:25:12 +00009 * it under the terms of the GNU General Public License version 3 and
Dees_Troy51a0e822012-09-05 15:24:24 -040010 * only version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA.
21 *
22 * The code was written from scratch by Dees_Troy dees_troy at
23 * yahoo
24 *
25 * Copyright (c) 2012
26 *
27 * Note that this all could probably be done as a shell script, but
28 * I am much better at C than I am at scripting. :)
29 */
30#include <stdio.h>
31#include <string.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <sys/stat.h>
35
36// Number of bytes in the ramdisks to compare
37#define SCAN_SIZE 60
38
Matt Mowere9260742015-02-22 20:20:18 -060039#define DEVID_MAX 64
40
Dees_Troy51a0e822012-09-05 15:24:24 -040041#define CMDLINE_SERIALNO "androidboot.serialno="
42#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
43#define CPUINFO_SERIALNO "Serial"
44#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
45#define CPUINFO_HARDWARE "Hardware"
46#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
47
Matt Mowere9260742015-02-22 20:20:18 -060048char device_id[DEVID_MAX] = { 0 };
Dees_Troy51a0e822012-09-05 15:24:24 -040049int verbose = 0, java = 0;
50
Matt Mowere9260742015-02-22 20:20:18 -060051void sanitize_device_id(void) {
52 const char* whitelist ="-._";
53 char str[DEVID_MAX];
54 char* c = str;
55
56 snprintf(str, DEVID_MAX, "%s", device_id);
57 memset(device_id, 0, strlen(device_id));
58 while (*c) {
59 if (isalnum(*c) || strchr(whitelist, *c))
60 strncat(device_id, c, 1);
61 c++;
62 }
63 return;
64}
65
66/* Recent HTC devices that still take advantage of dumlock
67 can safely rely on cmdline device_id retrieval */
Dees_Troy51a0e822012-09-05 15:24:24 -040068void get_device_id(void)
69{
70 FILE *fp;
Matt Mowere9260742015-02-22 20:20:18 -060071 char line[2048];
Dees_Troy51a0e822012-09-05 15:24:24 -040072 char* token;
73
Matt Mowere9260742015-02-22 20:20:18 -060074 // Check the cmdline to see if the serial number was supplied
Dees_Troy51a0e822012-09-05 15:24:24 -040075 fp = fopen("/proc/cmdline", "rt");
Matt Mowere9260742015-02-22 20:20:18 -060076 if (fp != NULL) {
77 fgets(line, sizeof(line), fp);
78 fclose(fp); // cmdline is only one line long
Dees_Troy51a0e822012-09-05 15:24:24 -040079
Matt Mowere9260742015-02-22 20:20:18 -060080 token = strtok(line, " ");
81 while (token) {
82 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0) {
83 token += CMDLINE_SERIALNO_LEN;
84 snprintf(device_id, DEVID_MAX, "%s", token);
85 sanitize_device_id(); // also removes newlines
86 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040087 }
Matt Mowere9260742015-02-22 20:20:18 -060088 token = strtok(NULL, " ");
Dees_Troy51a0e822012-09-05 15:24:24 -040089 }
Dees_Troy51a0e822012-09-05 15:24:24 -040090 }
91
Matt Mowere9260742015-02-22 20:20:18 -060092 strcpy(device_id, "serialno");
Dees_Troy51a0e822012-09-05 15:24:24 -040093 if (verbose)
94 printf("device id not found, using '%s'.", device_id);
Matt Mowere9260742015-02-22 20:20:18 -060095 return;
Dees_Troy51a0e822012-09-05 15:24:24 -040096}
97
98void reboot_device() {
99 // Reboot
100 printf("Rebooting!\n");
101 system("reboot system");
102}
103
104void scan_for_ramdisk_data(char *filename, char *ramdisk) {
105 FILE *pFile;
106 unsigned long lSize;
107 unsigned char *buffer;
108 size_t result;
109 int i;
110
111 pFile = fopen(filename, "rb");
112 if(pFile==NULL){
113 printf("Unabled to open image.\nFailed\n");
114 exit(1);
115 }
116
117 fseek (pFile , 0 , SEEK_END);
118 lSize = ftell(pFile);
119 rewind(pFile);
120
121 //printf("\n\nFile is %ld bytes big\n\n", lSize);
122
123 buffer = (unsigned char*)malloc(sizeof(unsigned char) * lSize);
124 if(buffer == NULL){
125 printf("File read error!\nFailed\n");
126 exit(2);
127 }
128
129 result = fread (buffer, 1, lSize, pFile);
130 if (result != lSize) {
131 printf("Error reading file '%s'\nFailed\n", filename);
132 exit(3);
133 }
134
135 unsigned char needle[6] = {0x00, 0x00, 0x00, 0x00, 0x1f, 0x8b};
136 unsigned char *last_needle = NULL;
137 //char *p = memmem(needle, lSize, buffer, sizeof(needle));
138 unsigned char *p = memmem(buffer + 2048, lSize - 2048, needle, sizeof(needle));
139 if (!p) {
140 fclose(pFile);
141 printf("Ramdisk not found in '%s', error!\nFailed\n", filename);
142 exit(4);
143 } else {
144 //printf("Ramdisk found in '%s'!\n", filename);
145 }
146
147 memcpy(ramdisk, p, sizeof(char) * SCAN_SIZE);
148 fclose(pFile);
149 free(buffer);
150}
151
152int compare_ramdisks(char *boot_path, char *recovery_path) {
153 char boot_data[SCAN_SIZE], recovery_data[SCAN_SIZE];
154
155 scan_for_ramdisk_data(boot_path, (char*)&boot_data);
156 scan_for_ramdisk_data(recovery_path, (char*)&recovery_data);
157 if (memcmp(boot_data, recovery_data, sizeof(boot_data)) == 0) {
158 if (verbose)
159 printf("Boot and recovery are the same.\n");
160 return 0;
161 } else {
162 if (verbose)
163 printf("Boot and recovery are NOT the same.\n");
164 return 1;
165 }
166}
167
168void flash_recovery_to_boot(int no_flash, int no_reboot) {
169 char twrp_device_path[255], recovery_path[255], boot_path[255],
170 exec[255], md5recovery[255], md5boot[255],
171 recoveryimg[255], bootimg[255], tempimg[255];
172 int ret_val = 0;
173 FILE *fp;
174 char* token;
175
176 // Create folders
177 if (verbose)
178 printf("Making '/sdcard/TWRP'\n");
179 mkdir("/sdcard/TWRP", 0777);
180 if (verbose)
181 printf("Making folder '/sdcard/TWRP/htcdumlock'\n");
182 mkdir("/sdcard/TWRP/htcdumlock", 0777);
183 strcpy(twrp_device_path, "/sdcard/TWRP/htcdumlock/");
184 strcat(twrp_device_path, device_id);
185 if (verbose)
186 printf("Making folder '%s'\n", twrp_device_path);
187 mkdir(twrp_device_path, 0777);
188 // Make folder for recovery
189 strcpy(recovery_path, twrp_device_path);
190 strcat(recovery_path, "/recovery");
191 if (verbose)
192 printf("Making folder '%s'\n", recovery_path);
193 mkdir(recovery_path, 0777);
194 strcat(recovery_path, "/");
195 // Make folder for boot
196 strcpy(boot_path, twrp_device_path);
197 strcat(boot_path, "/boot");
198 if (verbose)
199 printf("Making folder '%s'\n", boot_path);
200 mkdir(boot_path, 0777);
201 strcat(boot_path, "/");
202
203 // Set up file locations
204 strcpy(recoveryimg, recovery_path);
205 strcat(recoveryimg, "recovery.img");
206 strcpy(bootimg, boot_path);
207 strcat(bootimg, "boot.img");
208 strcpy(tempimg, twrp_device_path);
209 strcat(tempimg, "/temp.img");
210
211 // Dump recovery
212 strcpy(exec, "dump_image recovery ");
213 strcat(exec, recoveryimg);
214 if (verbose)
215 printf("Running command: '%s'\n", exec);
216 ret_val = system(exec);
217 if (ret_val != 0) {
218 printf("Unable to dump recovery.\nFailed\n");
219 return;
220 }
221
222 // Dump boot (kernel)
223 strcpy(exec, "dump_image boot ");
224 strcat(exec, tempimg);
225 if (verbose)
226 printf("Running command: '%s'\n", exec);
227 ret_val = system(exec);
228 if (ret_val != 0) {
229 printf("Unable to dump recovery.\nFailed\n");
230 return;
231 }
232
233 // Compare the ramdisks of the images from boot and recovery to make sure they are different
234 // If they are the same, then recovery is already flashed to boot and we don't want to wipe
235 // out our existing backup of boot
236 if (compare_ramdisks(tempimg, recoveryimg) != 0) {
237 if (verbose)
238 printf("Boot and recovery do not match so recovery is not flashed to boot yet...\n");
239 strcpy(exec, "mv ");
240 strcat(exec, tempimg);
241 strcat(exec, " ");
242 strcat(exec, bootimg);
243 if (verbose)
244 printf("Moving temporary boot.img: '%s'\n", exec);
245 ret_val = system(exec);
246 if (ret_val != 0) {
247 printf("Unable to move temporary boot image.\nFailed\n");
248 return;
249 }
250 } else {
251 if (!java)
252 printf("Ramdisk recovery and boot matches! Recovery is already flashed to boot!\n");
253 if (!no_reboot)
254 reboot_device();
255 return;
256 }
257
258 // Flash recovery to boot
259 strcpy(exec, "flash_image boot ");
260 strcat(exec, recoveryimg);
261 if (no_flash) {
262 if (verbose)
263 printf("NOT flashing recovery to boot due to argument 'noflash', command is '%s'\n", exec);
264 } else {
265 if (verbose)
266 printf("Running command: '%s'\n", exec);
267 ret_val = system(exec);
268 if (ret_val != 0) {
269 printf("Unable to flash recovery to boot.\nFailed\n");
270 return;
271 }
272 }
273
274 if (!no_reboot && !ret_val)
275 reboot_device();
276}
277
278void restore_original_boot(int no_flash) {
279 char boot_path[255], exec[255];
280
281 // Restore original boot partition
282 strcpy(boot_path, "/sdcard/TWRP/htcdumlock/");
283 strcat(boot_path, device_id);
284 strcat(boot_path, "/boot/");
285 strcpy(exec, "flash_image boot ");
286 strcat(exec, boot_path);
287 strcat(exec, "boot.img");
288 if (no_flash) {
289 if (verbose)
290 printf("NOT restoring boot due to argument 'noflash', command is '%s'\n", exec);
291 } else {
292 if (verbose)
293 printf("Running command: '%s'\n", exec);
294 system(exec);
295 }
296}
297
298int main(int argc, char** argv)
299{
300 int recovery = 0, no_flash = 0, restore_boot = 0, arg_error = 0,
301 no_reboot = 0, i;
302
303 // Parse the arguments
304 if (argc < 2)
305 arg_error = 1;
306 else {
307 for (i=1; i<argc; i++) {
308 if (strcmp(argv[i], "recovery") == 0) {
309 // Check to see if restore option is already set
310 // Do not allow user to do recovery and restore at the same time
311 if (restore_boot)
312 arg_error = 1;
313 recovery = 1;
314 } else if (strcmp(argv[i], "restore") == 0) {
315 // Check to see if recovery option is already set
316 // Do not allow user to do recovery and restore at the same time
317 if (recovery)
318 arg_error = 1;
319 restore_boot = 1;
320 } else if (strcmp(argv[i], "noflash") == 0)
321 no_flash = 1;
322 else if (strcmp(argv[i], "noreboot") == 0)
323 no_reboot = 1;
324 else if (strcmp(argv[i], "verbose") == 0)
325 verbose = 1;
326 else if (strcmp(argv[i], "java") == 0)
327 java = 1;
328 else
329 arg_error = 1;
330 }
331 }
332 if (arg_error) {
333 printf("Invalid argument given.\n");
334 printf("Valid arguments are:\n");
335 printf(" recovery -- backs up boot and recovery and flashes recovery to boot\n");
336 printf(" restore -- restores the most recent backup of boot made by this utility\n");
337 printf(" noflash -- same as 'recovery' but does not flash boot or reboot at the end\n");
338 printf(" noreboot -- does not reboot after flashing boot during 'recovery'\n");
339 printf(" verbose -- show extra debug information\n");
340 printf("\nNOTE: You cannot do 'recovery' and 'restore' in the same operation.\nFailed\n");
341 return 0;
342 }
343
344 get_device_id();
345 if (verbose)
346 printf("Device ID is: '%s'\n", device_id);
Matt Mowere9260742015-02-22 20:20:18 -0600347 if (strcmp(device_id, "serialno") == 0) {
348 printf("Error, dummy device ID detected!\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400349 printf("Did you 'su' first? HTC Dumlock requires root access.\nFailed\n");
350 return 0;
351 }
352
353 if (recovery) {
354 if (!java)
355 printf("Flashing recovery to boot, this may take a few minutes . . .\n");
356 flash_recovery_to_boot(no_flash, no_reboot);
357 }
358 if (restore_boot) {
359 printf("Restoring boot, this may take a few minutes . . .\n");
360 restore_original_boot(no_flash);
361 }
362
363 return 0;
364}