blob: ec9469f8a92176173939a2d3fafd2eb5968c9801 [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
39#define CMDLINE_SERIALNO "androidboot.serialno="
40#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
41#define CPUINFO_SERIALNO "Serial"
42#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
43#define CPUINFO_HARDWARE "Hardware"
44#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
45
46char device_id[255];
47int verbose = 0, java = 0;
48
49void get_device_id(void)
50{
51 FILE *fp;
52 char line[2048];
53 char hardware_id[32];
54 char* token;
55
56 // Assign a blank device_id to start with
57 device_id[0] = 0;
58
59 // First, try the cmdline to see if the serial number was supplied
60 fp = fopen("/proc/cmdline", "rt");
61 if (fp != NULL)
62 {
63 // First step, read the line. For cmdline, it's one long line
64 fgets(line, sizeof(line), fp);
65 fclose(fp);
66
67 // Now, let's tokenize the string
68 token = strtok(line, " ");
69
70 // Let's walk through the line, looking for the CMDLINE_SERIALNO token
71 while (token)
72 {
73 // We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
74 if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
75 {
76 // We found the serial number!
77 strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
78 return;
79 }
80 token = strtok(NULL, " ");
81 }
82 }
83
84 // Now we'll try cpuinfo for a serial number
85 fp = fopen("/proc/cpuinfo", "rt");
86 if (fp != NULL)
87 {
88 while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
89 if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
90 {
91 // We found the serial number!
92 token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
93 while (((int)*token > 0 && (int)*token <= 32 ) || (int)*token == ':') token++; // skip over all spaces and the colon
94 if (*token != NULL) {
95 token[30] = 0;
96 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
97 memset(device_id, 0, sizeof(device_id));
98 strncpy(device_id, token, strlen(token) - 1);
99 } else {
100 strcpy(device_id, token);
101 }
102 if (verbose)
103 printf("serial from cpuinfo: '%s'\n", device_id);
104 fclose(fp);
105 return;
106 }
107 } else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
108 // We found the hardware ID
109 token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
110 while (((int)*token > 0 && (int)*token <= 32 ) || (int)*token == ':') token++; // skip over all spaces and the colon
111 if (*token != NULL) {
112 token[30] = 0;
113 if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
114 memset(hardware_id, 0, sizeof(hardware_id));
115 strncpy(hardware_id, token, strlen(token) - 1);
116 } else {
117 strcpy(hardware_id, token);
118 }
119 if (verbose)
120 printf("hardware id from cpuinfo: '%s'\n", hardware_id);
121 }
122 }
123 }
124 fclose(fp);
125 }
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500126
Dees_Troy51a0e822012-09-05 15:24:24 -0400127 if (hardware_id[0] != 0) {
128 if (verbose)
129 printf("using hardware id for device id: '%s'\n", hardware_id);
130 strcpy(device_id, hardware_id);
131 return;
132 }
133
134 strcpy(device_id, "serialno");
135 if (verbose)
136 printf("device id not found, using '%s'.", device_id);
137 return;
138}
139
140void reboot_device() {
141 // Reboot
142 printf("Rebooting!\n");
143 system("reboot system");
144}
145
146void scan_for_ramdisk_data(char *filename, char *ramdisk) {
147 FILE *pFile;
148 unsigned long lSize;
149 unsigned char *buffer;
150 size_t result;
151 int i;
152
153 pFile = fopen(filename, "rb");
154 if(pFile==NULL){
155 printf("Unabled to open image.\nFailed\n");
156 exit(1);
157 }
158
159 fseek (pFile , 0 , SEEK_END);
160 lSize = ftell(pFile);
161 rewind(pFile);
162
163 //printf("\n\nFile is %ld bytes big\n\n", lSize);
164
165 buffer = (unsigned char*)malloc(sizeof(unsigned char) * lSize);
166 if(buffer == NULL){
167 printf("File read error!\nFailed\n");
168 exit(2);
169 }
170
171 result = fread (buffer, 1, lSize, pFile);
172 if (result != lSize) {
173 printf("Error reading file '%s'\nFailed\n", filename);
174 exit(3);
175 }
176
177 unsigned char needle[6] = {0x00, 0x00, 0x00, 0x00, 0x1f, 0x8b};
178 unsigned char *last_needle = NULL;
179 //char *p = memmem(needle, lSize, buffer, sizeof(needle));
180 unsigned char *p = memmem(buffer + 2048, lSize - 2048, needle, sizeof(needle));
181 if (!p) {
182 fclose(pFile);
183 printf("Ramdisk not found in '%s', error!\nFailed\n", filename);
184 exit(4);
185 } else {
186 //printf("Ramdisk found in '%s'!\n", filename);
187 }
188
189 memcpy(ramdisk, p, sizeof(char) * SCAN_SIZE);
190 fclose(pFile);
191 free(buffer);
192}
193
194int compare_ramdisks(char *boot_path, char *recovery_path) {
195 char boot_data[SCAN_SIZE], recovery_data[SCAN_SIZE];
196
197 scan_for_ramdisk_data(boot_path, (char*)&boot_data);
198 scan_for_ramdisk_data(recovery_path, (char*)&recovery_data);
199 if (memcmp(boot_data, recovery_data, sizeof(boot_data)) == 0) {
200 if (verbose)
201 printf("Boot and recovery are the same.\n");
202 return 0;
203 } else {
204 if (verbose)
205 printf("Boot and recovery are NOT the same.\n");
206 return 1;
207 }
208}
209
210void flash_recovery_to_boot(int no_flash, int no_reboot) {
211 char twrp_device_path[255], recovery_path[255], boot_path[255],
212 exec[255], md5recovery[255], md5boot[255],
213 recoveryimg[255], bootimg[255], tempimg[255];
214 int ret_val = 0;
215 FILE *fp;
216 char* token;
217
218 // Create folders
219 if (verbose)
220 printf("Making '/sdcard/TWRP'\n");
221 mkdir("/sdcard/TWRP", 0777);
222 if (verbose)
223 printf("Making folder '/sdcard/TWRP/htcdumlock'\n");
224 mkdir("/sdcard/TWRP/htcdumlock", 0777);
225 strcpy(twrp_device_path, "/sdcard/TWRP/htcdumlock/");
226 strcat(twrp_device_path, device_id);
227 if (verbose)
228 printf("Making folder '%s'\n", twrp_device_path);
229 mkdir(twrp_device_path, 0777);
230 // Make folder for recovery
231 strcpy(recovery_path, twrp_device_path);
232 strcat(recovery_path, "/recovery");
233 if (verbose)
234 printf("Making folder '%s'\n", recovery_path);
235 mkdir(recovery_path, 0777);
236 strcat(recovery_path, "/");
237 // Make folder for boot
238 strcpy(boot_path, twrp_device_path);
239 strcat(boot_path, "/boot");
240 if (verbose)
241 printf("Making folder '%s'\n", boot_path);
242 mkdir(boot_path, 0777);
243 strcat(boot_path, "/");
244
245 // Set up file locations
246 strcpy(recoveryimg, recovery_path);
247 strcat(recoveryimg, "recovery.img");
248 strcpy(bootimg, boot_path);
249 strcat(bootimg, "boot.img");
250 strcpy(tempimg, twrp_device_path);
251 strcat(tempimg, "/temp.img");
252
253 // Dump recovery
254 strcpy(exec, "dump_image recovery ");
255 strcat(exec, recoveryimg);
256 if (verbose)
257 printf("Running command: '%s'\n", exec);
258 ret_val = system(exec);
259 if (ret_val != 0) {
260 printf("Unable to dump recovery.\nFailed\n");
261 return;
262 }
263
264 // Dump boot (kernel)
265 strcpy(exec, "dump_image boot ");
266 strcat(exec, tempimg);
267 if (verbose)
268 printf("Running command: '%s'\n", exec);
269 ret_val = system(exec);
270 if (ret_val != 0) {
271 printf("Unable to dump recovery.\nFailed\n");
272 return;
273 }
274
275 // Compare the ramdisks of the images from boot and recovery to make sure they are different
276 // If they are the same, then recovery is already flashed to boot and we don't want to wipe
277 // out our existing backup of boot
278 if (compare_ramdisks(tempimg, recoveryimg) != 0) {
279 if (verbose)
280 printf("Boot and recovery do not match so recovery is not flashed to boot yet...\n");
281 strcpy(exec, "mv ");
282 strcat(exec, tempimg);
283 strcat(exec, " ");
284 strcat(exec, bootimg);
285 if (verbose)
286 printf("Moving temporary boot.img: '%s'\n", exec);
287 ret_val = system(exec);
288 if (ret_val != 0) {
289 printf("Unable to move temporary boot image.\nFailed\n");
290 return;
291 }
292 } else {
293 if (!java)
294 printf("Ramdisk recovery and boot matches! Recovery is already flashed to boot!\n");
295 if (!no_reboot)
296 reboot_device();
297 return;
298 }
299
300 // Flash recovery to boot
301 strcpy(exec, "flash_image boot ");
302 strcat(exec, recoveryimg);
303 if (no_flash) {
304 if (verbose)
305 printf("NOT flashing recovery to boot due to argument 'noflash', command is '%s'\n", exec);
306 } else {
307 if (verbose)
308 printf("Running command: '%s'\n", exec);
309 ret_val = system(exec);
310 if (ret_val != 0) {
311 printf("Unable to flash recovery to boot.\nFailed\n");
312 return;
313 }
314 }
315
316 if (!no_reboot && !ret_val)
317 reboot_device();
318}
319
320void restore_original_boot(int no_flash) {
321 char boot_path[255], exec[255];
322
323 // Restore original boot partition
324 strcpy(boot_path, "/sdcard/TWRP/htcdumlock/");
325 strcat(boot_path, device_id);
326 strcat(boot_path, "/boot/");
327 strcpy(exec, "flash_image boot ");
328 strcat(exec, boot_path);
329 strcat(exec, "boot.img");
330 if (no_flash) {
331 if (verbose)
332 printf("NOT restoring boot due to argument 'noflash', command is '%s'\n", exec);
333 } else {
334 if (verbose)
335 printf("Running command: '%s'\n", exec);
336 system(exec);
337 }
338}
339
340int main(int argc, char** argv)
341{
342 int recovery = 0, no_flash = 0, restore_boot = 0, arg_error = 0,
343 no_reboot = 0, i;
344
345 // Parse the arguments
346 if (argc < 2)
347 arg_error = 1;
348 else {
349 for (i=1; i<argc; i++) {
350 if (strcmp(argv[i], "recovery") == 0) {
351 // Check to see if restore option is already set
352 // Do not allow user to do recovery and restore at the same time
353 if (restore_boot)
354 arg_error = 1;
355 recovery = 1;
356 } else if (strcmp(argv[i], "restore") == 0) {
357 // Check to see if recovery option is already set
358 // Do not allow user to do recovery and restore at the same time
359 if (recovery)
360 arg_error = 1;
361 restore_boot = 1;
362 } else if (strcmp(argv[i], "noflash") == 0)
363 no_flash = 1;
364 else if (strcmp(argv[i], "noreboot") == 0)
365 no_reboot = 1;
366 else if (strcmp(argv[i], "verbose") == 0)
367 verbose = 1;
368 else if (strcmp(argv[i], "java") == 0)
369 java = 1;
370 else
371 arg_error = 1;
372 }
373 }
374 if (arg_error) {
375 printf("Invalid argument given.\n");
376 printf("Valid arguments are:\n");
377 printf(" recovery -- backs up boot and recovery and flashes recovery to boot\n");
378 printf(" restore -- restores the most recent backup of boot made by this utility\n");
379 printf(" noflash -- same as 'recovery' but does not flash boot or reboot at the end\n");
380 printf(" noreboot -- does not reboot after flashing boot during 'recovery'\n");
381 printf(" verbose -- show extra debug information\n");
382 printf("\nNOTE: You cannot do 'recovery' and 'restore' in the same operation.\nFailed\n");
383 return 0;
384 }
385
386 get_device_id();
387 if (verbose)
388 printf("Device ID is: '%s'\n", device_id);
389 if (strcmp(device_id, "0000000000000000") == 0) {
390 printf("Error, device ID is all zeros!\n");
391 printf("Did you 'su' first? HTC Dumlock requires root access.\nFailed\n");
392 return 0;
393 }
394
395 if (recovery) {
396 if (!java)
397 printf("Flashing recovery to boot, this may take a few minutes . . .\n");
398 flash_recovery_to_boot(no_flash, no_reboot);
399 }
400 if (restore_boot) {
401 printf("Restoring boot, this may take a few minutes . . .\n");
402 restore_original_boot(no_flash);
403 }
404
405 return 0;
406}