summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/irecovery.c343
-rw-r--r--src/libirecovery.c672
2 files changed, 0 insertions, 1015 deletions
diff --git a/src/irecovery.c b/src/irecovery.c
deleted file mode 100644
index 0e981cd..0000000
--- a/src/irecovery.c
+++ /dev/null
@@ -1,343 +0,0 @@
1/**
2 * iRecovery - Utility for DFU 2.0, WTF and Recovery Mode
3 * Copyright (C) 2008 - 2009 westbaer
4 *
5 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
17 **/
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <libirecovery.h>
23#include <readline/readline.h>
24#include <readline/history.h>
25
26#define FILE_HISTORY_PATH ".irecovery"
27#define debug(...) if(verbose) fprintf(stderr, __VA_ARGS__)
28
29enum {
30 kResetDevice, kStartShell, kSendCommand, kSendFile, kSendExploit, kSendScript
31};
32
33static unsigned int quit = 0;
34static unsigned int verbose = 0;
35
36void print_progress_bar(double progress);
37int received_cb(irecv_client_t client, const irecv_event_t* event);
38int progress_cb(irecv_client_t client, const irecv_event_t* event);
39int precommand_cb(irecv_client_t client, const irecv_event_t* event);
40int postcommand_cb(irecv_client_t client, const irecv_event_t* event);
41
42void shell_usage() {
43 printf("Usage:\n");
44 printf("\t/upload <file>\tSend file to client.\n");
45 printf("\t/exploit [file]\tSend usb exploit with optional payload\n");
46 printf("\t/help\t\tShow this help.\n");
47 printf("\t/exit\t\tExit interactive shell.\n");
48}
49
50void parse_command(irecv_client_t client, unsigned char* command, unsigned int size) {
51 char* cmd = strdup(command);
52 char* action = strtok(cmd, " ");
53 debug("Executing %s\n", action);
54 if (!strcmp(cmd, "/exit")) {
55 quit = 1;
56 } else
57
58 if (!strcmp(cmd, "/help")) {
59 shell_usage();
60 } else
61
62 if (!strcmp(cmd, "/upload")) {
63 char* filename = strtok(NULL, " ");
64 debug("Uploading files %s\n", filename);
65 if (filename != NULL) {
66 irecv_send_file(client, filename);
67 }
68 } else
69
70 if (!strcmp(cmd, "/exploit")) {
71 char* filename = strtok(NULL, " ");
72 debug("Sending exploit %s\n", filename);
73 if (filename != NULL) {
74 irecv_send_file(client, filename);
75 }
76 irecv_send_exploit(client);
77 } else
78
79 if (!strcmp(cmd, "/execute")) {
80 char* filename = strtok(NULL, " ");
81 debug("Executing script %s\n", filename);
82 if (filename != NULL) {
83 irecv_execute_script(client, filename);
84 }
85 }
86
87
88 free(action);
89}
90
91void load_command_history() {
92 read_history(FILE_HISTORY_PATH);
93}
94
95void append_command_to_history(char* cmd) {
96 add_history(cmd);
97 write_history(FILE_HISTORY_PATH);
98}
99
100void init_shell(irecv_client_t client) {
101 irecv_error_t error = 0;
102 load_command_history();
103 irecv_event_subscribe(client, IRECV_PROGRESS, &progress_cb, NULL);
104 irecv_event_subscribe(client, IRECV_RECEIVED, &received_cb, NULL);
105 irecv_event_subscribe(client, IRECV_PRECOMMAND, &precommand_cb, NULL);
106 irecv_event_subscribe(client, IRECV_POSTCOMMAND, &postcommand_cb, NULL);
107 while (!quit) {
108 error = irecv_receive(client);
109 if (error != IRECV_E_SUCCESS) {
110 debug("%s\n", irecv_strerror(error));
111 break;
112 }
113
114 char* cmd = readline("> ");
115 if (cmd && *cmd) {
116 error = irecv_send_command(client, cmd);
117 if (error != IRECV_E_SUCCESS) {
118 quit = 1;
119 }
120
121 append_command_to_history(cmd);
122 free(cmd);
123 }
124 }
125}
126
127int received_cb(irecv_client_t client, const irecv_event_t* event) {
128 if (event->type == IRECV_RECEIVED) {
129 int i = 0;
130 int size = event->size;
131 char* data = event->data;
132 for (i = 0; i < size; i++) {
133 printf("%c", data[i]);
134 }
135 }
136 return 0;
137}
138
139int precommand_cb(irecv_client_t client, const irecv_event_t* event) {
140 if (event->type == IRECV_PRECOMMAND) {
141 irecv_error_t error = 0;
142 if (event->data[0] == '/') {
143 parse_command(client, event->data, event->size);
144 return -1;
145 }
146 }
147 return 0;
148}
149
150int postcommand_cb(irecv_client_t client, const irecv_event_t* event) {
151 char* value = NULL;
152 char* action = NULL;
153 char* command = NULL;
154 char* argument = NULL;
155 irecv_error_t error = IRECV_E_SUCCESS;
156
157 if (event->type == IRECV_POSTCOMMAND) {
158 command = strdup(event->data);
159 action = strtok(command, " ");
160 if (!strcmp(action, "getenv")) {
161 argument = strtok(NULL, " ");
162 error = irecv_getenv(client, argument, &value);
163 if (error != IRECV_E_SUCCESS) {
164 debug("%s\n", irecv_strerror(error));
165 free(command);
166 return error;
167 }
168 printf("%s\n", value);
169 free(value);
170 }
171
172 if (!strcmp(action, "reboot")) {
173 quit = 1;
174 }
175 }
176
177 if (command) free(command);
178 return 0;
179}
180
181int progress_cb(irecv_client_t client, const irecv_event_t* event) {
182 if (event->type == IRECV_PROGRESS) {
183 print_progress_bar(event->progress);
184 }
185 return 0;
186}
187
188void print_progress_bar(double progress) {
189 int i = 0;
190 if(progress < 0) {
191 return;
192 }
193
194 if(progress > 100) {
195 progress = 100;
196 }
197
198 printf("\r[");
199 for(i = 0; i < 50; i++) {
200 if(i < progress / 2) {
201 printf("=");
202 } else {
203 printf(" ");
204 }
205 }
206
207 printf("] %3.1f%%", progress);
208 fflush(stdout);
209 if(progress == 100) {
210 printf("\n");
211 }
212}
213
214void print_usage() {
215 printf("iRecovery - iDevice Recovery Utility\n");
216 printf("Usage: ./irecovery [args]\n");
217 printf("\t-v\t\tStart irecovery in verbose mode.\n");
218 printf("\t-c <cmd>\tSend command to client.\n");
219 printf("\t-f <file>\tSend file to client.\n");
220 printf("\t-k [payload]\tSend usb exploit to client.\n");
221 printf("\t-h\t\tShow this help.\n");
222 printf("\t-r\t\tReset client.\n");
223 printf("\t-s\t\tStart interactive shell.\n");
224 printf("\t-e <script>\tExecutes recovery shell script.\n");
225 exit(1);
226}
227
228int main(int argc, char** argv) {
229 int i = 0;
230 int opt = 0;
231 int action = 0;
232 char* argument = NULL;
233 irecv_error_t error = 0;
234 if (argc == 1) print_usage();
235 while ((opt = getopt(argc, argv, "vhrsc:f:e:k::")) > 0) {
236 switch (opt) {
237 case 'v':
238 verbose += 1;
239 break;
240
241 case 'h':
242 print_usage();
243 break;
244
245 case 'r':
246 action = kResetDevice;
247 break;
248
249 case 's':
250 action = kStartShell;
251 break;
252
253 case 'f':
254 action = kSendFile;
255 argument = optarg;
256 break;
257
258 case 'c':
259 action = kSendCommand;
260 argument = optarg;
261 break;
262
263 case 'k':
264 action = kSendExploit;
265 argument = optarg;
266 break;
267
268 case 'e':
269 action = kSendScript;
270 argument = optarg;
271 break;
272
273 default:
274 fprintf(stderr, "Unknown argument\n");
275 return -1;
276 }
277 }
278
279 irecv_client_t client = NULL;
280 for (i = 0; i <= 5; i++) {
281 debug("Attempting to connect... \n");
282
283 if (irecv_open(&client) != IRECV_E_SUCCESS)
284 sleep(1);
285 else
286 break;
287
288 if (i == 5) {
289 return -1;
290 }
291 }
292
293 if (verbose) irecv_set_debug_level(verbose);
294
295 switch (action) {
296 case kResetDevice:
297 irecv_reset(client);
298 break;
299
300 case kSendFile:
301 irecv_event_subscribe(client, IRECV_PROGRESS, &progress_cb, NULL);
302 error = irecv_send_file(client, argument);
303 debug("%s\n", irecv_strerror(error));
304 break;
305
306 case kSendCommand:
307 error = irecv_send_command(client, argument);
308 debug("%s\n", irecv_strerror(error));
309 break;
310
311 case kSendExploit:
312 if (argument != NULL) {
313 irecv_event_subscribe(client, IRECV_PROGRESS, &progress_cb, NULL);
314 error = irecv_send_file(client, argument);
315 if (error != IRECV_E_SUCCESS) {
316 debug("%s\n", irecv_strerror(error));
317 break;
318 }
319 }
320 error = irecv_send_exploit(client);
321 debug("%s\n", irecv_strerror(error));
322 break;
323
324 case kStartShell:
325 init_shell(client);
326 break;
327
328 case kSendScript:
329 error = irecv_execute_script(client, argument);
330 if(error != IRECV_E_SUCCESS) {
331 debug("%s\n", irecv_strerror(error));
332 }
333 break;
334
335 default:
336 fprintf(stderr, "Unknown action\n");
337 break;
338 }
339
340 irecv_close(client);
341 return 0;
342}
343
diff --git a/src/libirecovery.c b/src/libirecovery.c
deleted file mode 100644
index dd5c734..0000000
--- a/src/libirecovery.c
+++ /dev/null
@@ -1,672 +0,0 @@
1/**
2 * iRecovery - Utility for DFU 2.0, WTF and Recovery Mode
3 * Copyright (C) 2008 - 2009 westbaer
4 *
5 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
17 **/
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include <libusb-1.0/libusb.h>
24
25#include "libirecovery.h"
26
27#define BUFFER_SIZE 0x1000
28#define debug(...) if(libirecovery_debug) fprintf(stderr, __VA_ARGS__)
29
30static int libirecovery_debug = 0;
31static libusb_context* libirecovery_context = NULL;
32
33int irecv_write_file(const char* filename, const void* data, size_t size);
34int irecv_read_file(const char* filename, char** data, uint32_t* size);
35
36irecv_error_t irecv_open(irecv_client_t* pclient) {
37 int i = 0;
38 char serial[256];
39 struct libusb_device* usb_device = NULL;
40 struct libusb_device** usb_device_list = NULL;
41 struct libusb_device_handle* usb_handle = NULL;
42 struct libusb_device_descriptor usb_descriptor;
43
44 *pclient = NULL;
45 libusb_init(&libirecovery_context);
46 if(libirecovery_debug) {
47 irecv_set_debug_level(libirecovery_debug);
48 }
49
50 irecv_error_t error = IRECV_E_SUCCESS;
51 int usb_device_count = libusb_get_device_list(libirecovery_context, &usb_device_list);
52 for (i = 0; i < usb_device_count; i++) {
53 usb_device = usb_device_list[i];
54 libusb_get_device_descriptor(usb_device, &usb_descriptor);
55 if (usb_descriptor.idVendor == APPLE_VENDOR_ID) {
56 /* verify this device is in a mode we understand */
57 if (usb_descriptor.idProduct == kRecoveryMode1 ||
58 usb_descriptor.idProduct == kRecoveryMode2 ||
59 usb_descriptor.idProduct == kRecoveryMode3 ||
60 usb_descriptor.idProduct == kRecoveryMode4 ||
61 usb_descriptor.idProduct == kDfuMode) {
62
63 libusb_open(usb_device, &usb_handle);
64 if (usb_handle == NULL) {
65 libusb_free_device_list(usb_device_list, 1);
66 libusb_close(usb_handle);
67 libusb_exit(libirecovery_context);
68 return IRECV_E_UNABLE_TO_CONNECT;
69 }
70 libusb_free_device_list(usb_device_list, 1);
71
72 irecv_client_t client = (irecv_client_t) malloc(sizeof(struct irecv_client));
73 if (client == NULL) {
74 libusb_close(usb_handle);
75 libusb_exit(libirecovery_context);
76 return IRECV_E_OUT_OF_MEMORY;
77 }
78
79 memset(client, '\0', sizeof(struct irecv_client));
80 client->interface = 0;
81 client->handle = usb_handle;
82 client->mode = usb_descriptor.idProduct;
83
84 error = irecv_set_configuration(client, 1);
85 if (error != IRECV_E_SUCCESS) {
86 return error;
87 }
88
89 error = irecv_set_interface(client, 1, 1);
90 if (error != IRECV_E_SUCCESS) {
91 return error;
92 }
93
94 *pclient = client;
95 return IRECV_E_SUCCESS;
96 }
97 }
98 }
99
100 return IRECV_E_UNABLE_TO_CONNECT;
101}
102
103irecv_error_t irecv_set_configuration(irecv_client_t client, int configuration) {
104 if (client == NULL || client->handle == NULL) {
105 return IRECV_E_NO_DEVICE;
106 }
107
108 debug("Setting to configuration %d", configuration);
109
110 int current = 0;
111 libusb_get_configuration(client->handle, &current);
112 if (current != configuration) {
113 if (libusb_set_configuration(client->handle, configuration) < 0) {
114 return IRECV_E_USB_CONFIGURATION;
115 }
116 }
117
118 client->config = configuration;
119 return IRECV_E_SUCCESS;
120}
121
122irecv_error_t irecv_set_interface(irecv_client_t client, int interface, int alt_interface) {
123 if (client == NULL || client->handle == NULL) {
124 return IRECV_E_NO_DEVICE;
125 }
126
127 if (client->interface == interface) {
128 return IRECV_E_SUCCESS;
129 }
130
131 debug("Setting to interface %d:%d", interface, alt_interface);
132 if (libusb_claim_interface(client->handle, interface) < 0) {
133 return IRECV_E_USB_INTERFACE;
134 }
135
136 if (libusb_set_interface_alt_setting(client->handle, interface, alt_interface) < 0) {
137 return IRECV_E_USB_INTERFACE;
138 }
139
140 client->interface = interface;
141 client->alt_interface = alt_interface;
142 return IRECV_E_SUCCESS;
143}
144
145irecv_error_t irecv_reset(irecv_client_t client) {
146 if (client == NULL || client->handle == NULL) {
147 return IRECV_E_NO_DEVICE;
148 }
149
150 libusb_reset_device(client->handle);
151
152 return IRECV_E_SUCCESS;
153}
154
155irecv_error_t irecv_event_subscribe(irecv_client_t client, irecv_event_type type, irecv_event_cb_t callback, void* user_data) {
156 switch(type) {
157 case IRECV_RECEIVED:
158 client->received_callback = callback;
159 break;
160
161 case IRECV_PROGRESS:
162 client->progress_callback = callback;
163
164 case IRECV_CONNECTED:
165 client->connected_callback = callback;
166
167 case IRECV_PRECOMMAND:
168 client->precommand_callback = callback;
169 break;
170
171 case IRECV_POSTCOMMAND:
172 client->postcommand_callback = callback;
173 break;
174
175 case IRECV_DISCONNECTED:
176 client->disconnected_callback = callback;
177
178 default:
179 return IRECV_E_UNKNOWN_ERROR;
180 }
181
182 return IRECV_E_SUCCESS;
183}
184
185irecv_error_t irecv_event_unsubscribe(irecv_client_t client, irecv_event_type type) {
186 switch(type) {
187 case IRECV_RECEIVED:
188 client->received_callback = NULL;
189 break;
190
191 case IRECV_PROGRESS:
192 client->progress_callback = NULL;
193
194 case IRECV_CONNECTED:
195 client->connected_callback = NULL;
196
197 case IRECV_PRECOMMAND:
198 client->precommand_callback = NULL;
199 break;
200
201 case IRECV_POSTCOMMAND:
202 client->postcommand_callback = NULL;
203 break;
204
205 case IRECV_DISCONNECTED:
206 client->disconnected_callback = NULL;
207
208 default:
209 return IRECV_E_UNKNOWN_ERROR;
210 }
211
212 return IRECV_E_SUCCESS;
213}
214
215irecv_error_t irecv_close(irecv_client_t client) {
216 if (client != NULL) {
217 if(client->disconnected_callback != NULL) {
218 irecv_event_t event;
219 event.size = 0;
220 event.data = NULL;
221 event.progress = 0;
222 event.type = IRECV_DISCONNECTED;
223 client->disconnected_callback(client, &event);
224 }
225
226 if (client->handle != NULL) {
227 libusb_release_interface(client->handle, client->interface);
228 libusb_close(client->handle);
229 client->handle = NULL;
230 }
231
232 if (libirecovery_context != NULL) {
233 libusb_exit(libirecovery_context);
234 libirecovery_context = NULL;
235 }
236
237 free(client);
238 client = NULL;
239 }
240
241 return IRECV_E_SUCCESS;
242}
243
244void irecv_set_debug_level(int level) {
245 libirecovery_debug = level;
246 if(libirecovery_context) {
247 libusb_set_debug(libirecovery_context, libirecovery_debug);
248 }
249}
250
251irecv_error_t irecv_send_command(irecv_client_t client, unsigned char* command) {
252 if (client == NULL || client->handle == NULL) {
253 return IRECV_E_NO_DEVICE;
254 }
255
256 unsigned int length = strlen(command);
257 if (length >= 0x100) {
258 length = 0xFF;
259 }
260
261 irecv_event_t event;
262 if(client->precommand_callback != NULL) {
263 event.size = length;
264 event.data = command;
265 event.type = IRECV_PRECOMMAND;
266 if(client->precommand_callback(client, &event)) {
267 return IRECV_E_SUCCESS;
268 }
269 }
270
271 if (length > 0) {
272 libusb_control_transfer(client->handle, 0x40, 0, 0, 0, command, length + 1, 100);
273 }
274
275 if(client->postcommand_callback != NULL) {
276 event.size = length;
277 event.data = command;
278 event.type = IRECV_POSTCOMMAND;
279 if(client->postcommand_callback(client, &event)) {
280 return IRECV_E_SUCCESS;
281 }
282 }
283
284 return IRECV_E_SUCCESS;
285}
286
287irecv_error_t irecv_send_file(irecv_client_t client, const char* filename) {
288 if (client == NULL || client->handle == NULL) {
289 return IRECV_E_NO_DEVICE;
290 }
291
292 FILE* file = fopen(filename, "rb");
293 if (file == NULL) {
294 return IRECV_E_FILE_NOT_FOUND;
295 }
296
297 fseek(file, 0, SEEK_END);
298 int length = ftell(file);
299 fseek(file, 0, SEEK_SET);
300
301 unsigned char* buffer = (unsigned char*) malloc(length);
302 if (buffer == NULL) {
303 fclose(file);
304 return IRECV_E_OUT_OF_MEMORY;
305 }
306
307 int bytes = fread(buffer, 1, length, file);
308 fclose(file);
309
310 if (bytes != length) {
311 free(buffer);
312 return IRECV_E_UNKNOWN_ERROR;
313 }
314
315 irecv_error_t error = irecv_send_buffer(client, buffer, length);
316 free(buffer);
317 return error;
318}
319
320irecv_error_t irecv_get_status(irecv_client_t client, unsigned int* status) {
321 if (client == NULL || client->handle == NULL) {
322 *status = 0;
323 return IRECV_E_NO_DEVICE;
324 }
325
326 unsigned char buffer[6];
327 memset(buffer, '\0', 6);
328 if (libusb_control_transfer(client->handle, 0xA1, 3, 0, 0, buffer, 6, 1000) != 6) {
329 *status = 0;
330 return IRECV_E_USB_STATUS;
331 }
332
333 debug("status: %d\n", (unsigned int) buffer[4]);
334 *status = (unsigned int) buffer[4];
335 return IRECV_E_SUCCESS;
336}
337
338irecv_error_t irecv_send_buffer(irecv_client_t client, unsigned char* buffer, unsigned int length) {
339 irecv_error_t error = 0;
340 if (client == NULL || client->handle == NULL) {
341 return IRECV_E_NO_DEVICE;
342 }
343
344 int last = length % 0x800;
345 int packets = length / 0x800;
346 if (last != 0) {
347 packets++;
348 }
349
350 int i = 0;
351 double progress = 0;
352 unsigned int count = 0;
353 unsigned int status = 0;
354 for (i = 0; i < packets; i++) {
355 int size = i + 1 < packets ? 0x800 : last;
356 int bytes = libusb_control_transfer(client->handle, 0x21, 1, 0, 0, &buffer[i * 0x800], size, 1000);
357 if (bytes != size) {
358 return IRECV_E_USB_UPLOAD;
359 }
360
361 error = irecv_get_status(client, &status);
362 if (error != IRECV_E_SUCCESS) {
363 return error;
364 }
365
366 if (status != 5) {
367 return IRECV_E_USB_UPLOAD;
368 }
369
370 count += size;
371 if(client->progress_callback != NULL) {
372 irecv_event_t event;
373 event.progress = ((double) count/ (double) length) * 100.0;
374 event.type = IRECV_PROGRESS;
375 event.data = "Uploading";
376 event.size = count;
377 client->progress_callback(client, &event);
378 } else {
379 debug("Sent: %d bytes - %d of %d\n", bytes, count, length);
380 }
381 }
382
383 libusb_control_transfer(client->handle, 0x21, 1, 0, 0, buffer, 0, 1000);
384 for (i = 0; i < 3; i++) {
385 error = irecv_get_status(client, &status);
386 if (error != IRECV_E_SUCCESS) {
387 return error;
388 }
389 }
390
391 return IRECV_E_SUCCESS;
392}
393
394irecv_error_t irecv_receive(irecv_client_t client) {
395 unsigned char buffer[BUFFER_SIZE];
396 memset(buffer, '\0', BUFFER_SIZE);
397 if (client == NULL || client->handle == NULL) {
398 return IRECV_E_NO_DEVICE;
399 }
400
401 int bytes = 0;
402 while (libusb_bulk_transfer(client->handle, 0x81, buffer, BUFFER_SIZE, &bytes, 100) == 0) {
403 if (bytes > 0) {
404 if (client->received_callback != NULL) {
405 irecv_event_t event;
406 event.size = bytes;
407 event.data = buffer;
408 event.type = IRECV_RECEIVED;
409 if (client->received_callback(client, &event) != 0) {
410 return IRECV_E_SUCCESS;
411 }
412 }
413 } else break;
414 }
415
416 return IRECV_E_SUCCESS;
417}
418
419irecv_error_t irecv_getenv(irecv_client_t client, const char* variable, char** value) {
420 char command[256];
421 if (client == NULL || client->handle == NULL) {
422 return IRECV_E_NO_DEVICE;
423 }
424
425 *value = NULL;
426
427 if(variable == NULL) {
428 return IRECV_E_UNKNOWN_ERROR;
429 }
430
431 memset(command, '\0', sizeof(command));
432 snprintf(command, sizeof(command)-1, "getenv %s", variable);
433 irecv_error_t error = irecv_send_command(client, command);
434 if(error != IRECV_E_SUCCESS) {
435 return error;
436 }
437
438 unsigned char* response = (unsigned char*) malloc(256);
439 if (response == NULL) {
440 return IRECV_E_OUT_OF_MEMORY;
441 }
442
443 memset(response, '\0', 256);
444 int ret = libusb_control_transfer(client->handle, 0xC0, 0, 0, 0, response, 255, 500);
445 if (ret < 0) {
446 return IRECV_E_UNKNOWN_ERROR;
447 }
448
449 *value = response;
450 return IRECV_E_SUCCESS;
451}
452
453irecv_error_t irecv_get_cpid(irecv_client_t client, unsigned int* cpid) {
454 char info[256];
455 memset(info, '\0', 256);
456
457 if (client == NULL || client->handle == NULL) {
458 return IRECV_E_NO_DEVICE;
459 }
460
461 libusb_get_string_descriptor_ascii(client->handle, 3, info, 255);
462
463 unsigned char* cpid_string = strstr(info, "CPID:");
464 if (cpid_string == NULL) {
465 *cpid = 0;
466 return IRECV_E_UNKNOWN_ERROR;
467 }
468 sscanf(cpid_string, "CPID:%d", cpid);
469
470 return IRECV_E_SUCCESS;
471}
472
473irecv_error_t irecv_get_bdid(irecv_client_t client, unsigned int* bdid) {
474 char info[256];
475 memset(info, '\0', 256);
476
477 if (client == NULL || client->handle == NULL) {
478 return IRECV_E_NO_DEVICE;
479 }
480
481 libusb_get_string_descriptor_ascii(client->handle, 3, info, 255);
482
483 unsigned char* bdid_string = strstr(info, "BDID:");
484 if (bdid_string == NULL) {
485 *bdid = 0;
486 return IRECV_E_UNKNOWN_ERROR;
487 }
488 sscanf(bdid_string, "BDID:%d", bdid);
489
490 return IRECV_E_SUCCESS;
491}
492
493irecv_error_t irecv_get_ecid(irecv_client_t client, unsigned long long* ecid) {
494 char info[256];
495 memset(info, '\0', 256);
496
497 if (client == NULL || client->handle == NULL) {
498 return IRECV_E_NO_DEVICE;
499 }
500
501 libusb_get_string_descriptor_ascii(client->handle, 3, info, 255);
502
503 unsigned char* ecid_string = strstr(info, "ECID:");
504 if (ecid_string == NULL) {
505 *ecid = 0;
506 return IRECV_E_UNKNOWN_ERROR;
507 }
508 sscanf(ecid_string, "ECID:%qX", ecid);
509
510 return IRECV_E_SUCCESS;
511}
512
513irecv_error_t irecv_send_exploit(irecv_client_t client) {
514 if (client == NULL || client->handle == NULL) {
515 return IRECV_E_NO_DEVICE;
516 }
517
518 libusb_control_transfer(client->handle, 0x21, 2, 0, 0, NULL, 0, 100);
519 return IRECV_E_SUCCESS;
520}
521
522irecv_error_t irecv_execute_script(irecv_client_t client, const char* filename) {
523 irecv_error_t error = IRECV_E_SUCCESS;
524 if (client == NULL || client->handle == NULL) {
525 return IRECV_E_NO_DEVICE;
526 }
527
528 int file_size = 0;
529 char* file_data = NULL;
530 if(irecv_read_file(filename, &file_data, &file_size) < 0) {
531 return IRECV_E_FILE_NOT_FOUND;
532 }
533
534 char* line = strtok(file_data, "\n");
535 while(line != NULL) {
536 if(line[0] != '#') {
537 error = irecv_send_command(client, line);
538 if(error != IRECV_E_SUCCESS) {
539 return error;
540 }
541
542 error = irecv_receive(client);
543 if(error != IRECV_E_SUCCESS) {
544 return error;
545 }
546 }
547 line = strtok(NULL, "\n");
548 }
549
550 return IRECV_E_SUCCESS;
551}
552
553irecv_error_t irecv_setenv(irecv_client_t client, const char* variable, const char* value) {
554 char command[256];
555 if (client == NULL || client->handle == NULL) {
556 return IRECV_E_NO_DEVICE;
557 }
558
559 if(variable == NULL || value == NULL) {
560 return IRECV_E_UNKNOWN_ERROR;
561 }
562
563 memset(command, '\0', sizeof(command));
564 snprintf(command, sizeof(command)-1, "setenv %s %s", variable, value);
565 irecv_error_t error = irecv_send_command(client, command);
566 if(error != IRECV_E_SUCCESS) {
567 return error;
568 }
569
570 return IRECV_E_SUCCESS;
571}
572
573const char* irecv_strerror(irecv_error_t error) {
574 switch (error) {
575 case IRECV_E_SUCCESS:
576 return "Command completed successfully";
577
578 case IRECV_E_NO_DEVICE:
579 return "Unable to find device";
580
581 case IRECV_E_OUT_OF_MEMORY:
582 return "Out of memory";
583
584 case IRECV_E_UNABLE_TO_CONNECT:
585 return "Unable to connect to device";
586
587 case IRECV_E_INVALID_INPUT:
588 return "Invalid input";
589
590 case IRECV_E_FILE_NOT_FOUND:
591 return "File not found";
592
593 case IRECV_E_USB_UPLOAD:
594 return "Unable to upload data to device";
595
596 case IRECV_E_USB_STATUS:
597 return "Unable to get device status";
598
599 case IRECV_E_USB_INTERFACE:
600 return "Unable to set device interface";
601
602 case IRECV_E_USB_CONFIGURATION:
603 return "Unable to set device configuration";
604
605 default:
606 return "Unknown error";
607 }
608
609 return NULL;
610}
611
612int irecv_write_file(const char* filename, const void* data, size_t size) {
613 size_t bytes = 0;
614 FILE* file = NULL;
615
616 debug("Writing data to %s\n", filename);
617 file = fopen(filename, "wb");
618 if (file == NULL) {
619 error("read_file: Unable to open file %s\n", filename);
620 return -1;
621 }
622
623 bytes = fwrite(data, 1, size, file);
624 fclose(file);
625
626 if (bytes != size) {
627 error("ERROR: Unable to write entire file: %s: %d of %d\n", filename, bytes, size);
628 return -1;
629 }
630
631 return size;
632}
633
634int irecv_read_file(const char* filename, char** data, uint32_t* size) {
635 size_t bytes = 0;
636 size_t length = 0;
637 FILE* file = NULL;
638 char* buffer = NULL;
639 debug("Reading data from %s\n", filename);
640
641 *size = 0;
642 *data = NULL;
643
644 file = fopen(filename, "rb");
645 if (file == NULL) {
646 error("read_file: File %s not found\n", filename);
647 return -1;
648 }
649
650 fseek(file, 0, SEEK_END);
651 length = ftell(file);
652 rewind(file);
653
654 buffer = (char*) malloc(length);
655 if(buffer == NULL) {
656 error("ERROR: Out of memory\n");
657 fclose(file);
658 return -1;
659 }
660 bytes = fread(buffer, 1, length, file);
661 fclose(file);
662
663 if(bytes != length) {
664 error("ERROR: Unable to read entire file\n");
665 free(buffer);
666 return -1;
667 }
668
669 *size = length;
670 *data = buffer;
671 return 0;
672}