Skip to content

Commit 29592eb

Browse files
committed
Initialize KIS device in device callback
Also, print the detailed mode in irecovery -q and -m output.
1 parent 15fdc6a commit 29592eb

3 files changed

Lines changed: 64 additions & 15 deletions

File tree

include/libirecovery.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct irecv_device_info {
110110
unsigned int ap_nonce_size;
111111
unsigned char* sep_nonce;
112112
unsigned int sep_nonce_size;
113+
uint16_t pid;
113114
};
114115

115116
typedef enum {

src/libirecovery.c

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,11 @@ static void irecv_load_device_info_from_iboot_string(irecv_client_t client, cons
777777
}
778778
client->device_info.srtg = strdup(tmp);
779779
}
780+
781+
client->device_info.pid = client->mode;
782+
if (client->isKIS) {
783+
client->device_info.pid = KIS_PRODUCT_ID;
784+
}
780785
}
781786

782787
static void irecv_copy_nonce_with_tag_from_buffer(const char* tag, unsigned char** nonce, unsigned int* nonce_size, const char *buf)
@@ -2185,6 +2190,8 @@ static void* _irecv_handle_device_add(void *userdata)
21852190
char serial_str[256];
21862191
uint32_t location = 0;
21872192
uint16_t product_id = 0;
2193+
irecv_error_t error = 0;
2194+
irecv_client_t client = NULL;
21882195

21892196
memset(serial_str, 0, 256);
21902197
#ifdef WIN32
@@ -2263,18 +2270,14 @@ static void* _irecv_handle_device_add(void *userdata)
22632270

22642271
if (product_id == KIS_PRODUCT_ID) {
22652272
IOObjectRetain(device);
2266-
irecv_client_t client;
22672273

2268-
irecv_error_t error = iokit_usb_open_service(&client, device);
2274+
error = iokit_usb_open_service(&client, device);
22692275
if (error != IRECV_E_SUCCESS) {
22702276
debug("%s: ERROR: could not open KIS device!\n", __func__);
22712277
return NULL;
22722278
}
22732279

2274-
strcpy(serial_str, client->device_info.serial_string);
22752280
product_id = client->mode;
2276-
2277-
irecv_close(client);
22782281
} else {
22792282
CFStringRef serialString = (CFStringRef)IORegistryEntryCreateCFProperty(device, CFSTR(kUSBSerialNumberString), kCFAllocatorDefault, 0);
22802283
if (serialString) {
@@ -2307,17 +2310,13 @@ static void* _irecv_handle_device_add(void *userdata)
23072310
}
23082311

23092312
if (product_id == KIS_PRODUCT_ID) {
2310-
irecv_client_t client;
2311-
irecv_error_t error = libusb_usb_open_handle_with_descriptor_and_ecid(&client, usb_handle, &devdesc, 0);
2313+
error = libusb_usb_open_handle_with_descriptor_and_ecid(&client, usb_handle, &devdesc, 0);
23122314
if (error != IRECV_E_SUCCESS) {
23132315
debug("%s: ERROR: could not open KIS device!\n", __func__);
23142316
return NULL;
23152317
}
23162318

2317-
strcpy(serial_str, client->device_info.serial_string);
23182319
product_id = client->mode;
2319-
2320-
irecv_close(client);
23212320
} else {
23222321
libusb_error = libusb_get_string_descriptor_ascii(usb_handle, devdesc.iSerialNumber, (unsigned char*)serial_str, 255);
23232322
if (libusb_error < 0) {
@@ -2329,8 +2328,45 @@ static void* _irecv_handle_device_add(void *userdata)
23292328
#endif /* !HAVE_IOKIT */
23302329
#endif /* !WIN32 */
23312330
memset(&client_loc, '\0', sizeof(client_loc));
2332-
irecv_load_device_info_from_iboot_string(&client_loc, serial_str);
2331+
if (product_id == KIS_PRODUCT_ID) {
2332+
error = irecv_usb_set_configuration(client, 1);
2333+
if (error != IRECV_E_SUCCESS) {
2334+
debug("Failed to set configuration, error %d\n", error);
2335+
irecv_close(client);
2336+
return NULL;
2337+
}
2338+
2339+
error = irecv_usb_set_interface(client, 0, 0);
2340+
if (error != IRECV_E_SUCCESS) {
2341+
debug("Failed to set interface, error %d\n", error);
2342+
irecv_close(client);
2343+
return NULL;
2344+
}
2345+
2346+
error = irecv_kis_init(client);
2347+
if (error != IRECV_E_SUCCESS) {
2348+
debug("irecv_kis_init failed, error %d\n", error);
2349+
irecv_close(client);
2350+
return NULL;
2351+
}
2352+
2353+
error = irecv_kis_load_device_info(client);
2354+
if (error != IRECV_E_SUCCESS) {
2355+
debug("irecv_kis_load_device_info failed, error %d\n", error);
2356+
irecv_close(client);
2357+
return NULL;
2358+
}
2359+
debug("found device with ECID %016" PRIx64 "\n", (uint64_t)client->device_info.ecid);
2360+
strncpy(serial_str, client->device_info.serial_string, 256);
2361+
product_id = client->mode;
2362+
client_loc.isKIS = 1;
2363+
}
2364+
if (client) {
2365+
irecv_close(client);
2366+
}
2367+
23332368
client_loc.mode = product_id;
2369+
irecv_load_device_info_from_iboot_string(&client_loc, serial_str);
23342370

23352371
struct irecv_usb_device_info *usb_dev_info = (struct irecv_usb_device_info*)malloc(sizeof(struct irecv_usb_device_info));
23362372
memcpy(&(usb_dev_info->device_info), &(client_loc.device_info), sizeof(struct irecv_device_info));

tools/irecovery.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,14 @@ static void print_device_info(irecv_client_t client)
179179

180180
ret = irecv_get_mode(client, &mode);
181181
if (ret == IRECV_E_SUCCESS) {
182-
printf("MODE: %s\n", mode_to_str(mode));
182+
switch (devinfo->pid) {
183+
case 0x1881:
184+
printf("MODE: DFU via Debug USB (KIS)\n");
185+
break;
186+
default:
187+
printf("MODE: %s\n", mode_to_str(mode));
188+
break;
189+
}
183190
}
184191

185192
irecv_devices_get_device_by_client(client, &device);
@@ -630,11 +637,16 @@ int main(int argc, char* argv[])
630637
}
631638
break;
632639

633-
case kShowMode:
640+
case kShowMode: {
641+
const struct irecv_device_info *devinfo = irecv_get_device_info(client);
634642
irecv_get_mode(client, &mode);
635-
printf("%s Mode\n", mode_to_str(mode));
643+
printf("%s Mode", mode_to_str(mode));
644+
if (devinfo->pid == 0x1881) {
645+
printf(" via Debug USB (KIS)");
646+
}
647+
printf("\n");
636648
break;
637-
649+
}
638650
case kRebootToNormalMode:
639651
error = irecv_setenv(client, "auto-boot", "true");
640652
if (error != IRECV_E_SUCCESS) {

0 commit comments

Comments
 (0)