summaryrefslogtreecommitdiffstats
path: root/src/AFC.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/AFC.c')
-rw-r--r--src/AFC.c67
1 files changed, 30 insertions, 37 deletions
diff --git a/src/AFC.c b/src/AFC.c
index cd120fc..8cc88f1 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -39,11 +39,7 @@ const int MAXIMUM_PACKET_SIZE = (2 << 15);
39 */ 39 */
40static void afc_lock(afc_client_t client) 40static void afc_lock(afc_client_t client)
41{ 41{
42 log_debug_msg("Locked\n"); 42 log_debug_msg("%s: Locked\n", __func__);
43 /*while (client->lock) {
44 usleep(500); // they say it's obsolete, but whatever
45 }
46 client->lock = 1; */
47 g_mutex_lock(client->mutex); 43 g_mutex_lock(client->mutex);
48} 44}
49 45
@@ -52,9 +48,8 @@ static void afc_lock(afc_client_t client)
52 * @param client The AFC 48 * @param client The AFC
53 */ 49 */
54static void afc_unlock(afc_client_t client) 50static void afc_unlock(afc_client_t client)
55{ // just to be pretty 51{
56 log_debug_msg("Unlocked\n"); 52 log_debug_msg("%s: Unlocked\n", __func__);
57 //client->lock = 0;
58 g_mutex_unlock(client->mutex); 53 g_mutex_unlock(client->mutex);
59} 54}
60 55
@@ -245,12 +240,12 @@ static int dispatch_AFC_packet(afc_client_t client, const char *data, uint64_t l
245 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); 240 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
246 offset = client->afc_packet->this_length - sizeof(AFCPacket); 241 offset = client->afc_packet->this_length - sizeof(AFCPacket);
247 242
248 log_debug_msg("dispatch_AFC_packet: Offset: %i\n", offset); 243 log_debug_msg("%s: Offset: %i\n", __func__, offset);
249 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { 244 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) {
250 log_debug_msg("dispatch_AFC_packet: Length did not resemble what it was supposed"); 245 log_debug_msg("%s: Length did not resemble what it was supposed", __func__);
251 log_debug_msg("to based on the packet.\n"); 246 log_debug_msg("to based on the packet.\n");
252 log_debug_msg("length minus offset: %i\n", length - offset); 247 log_debug_msg("%s: length minus offset: %i\n", __func__, length - offset);
253 log_debug_msg("rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); 248 log_debug_msg("%s: rest of packet: %i\n", __func__, client->afc_packet->entire_length - client->afc_packet->this_length);
254 free(buffer); 249 free(buffer);
255 return -1; 250 return -1;
256 } 251 }
@@ -261,19 +256,19 @@ static int dispatch_AFC_packet(afc_client_t client, const char *data, uint64_t l
261 return bytes; 256 return bytes;
262 } 257 }
263 258
264 log_debug_msg("dispatch_AFC_packet: sent the first now go with the second\n"); 259 log_debug_msg("%s: sent the first now go with the second\n", __func__);
265 log_debug_msg("Length: %i\n", length - offset); 260 log_debug_msg("%s: Length: %i\n", __func__, length - offset);
266 log_debug_msg("Buffer: \n"); 261 log_debug_msg("%s: Buffer: \n", __func__);
267 log_debug_buffer(data + offset, length - offset); 262 log_debug_buffer(data + offset, length - offset);
268 263
269 usbmuxd_send(client->sfd, data + offset, length - offset, (uint32_t*)&bytes); 264 usbmuxd_send(client->sfd, data + offset, length - offset, (uint32_t*)&bytes);
270 return bytes; 265 return bytes;
271 } else { 266 } else {
272 log_debug_msg("dispatch_AFC_packet doin things the old way\n"); 267 log_debug_msg("%s: doin things the old way\n", __func__);
273 buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length); 268 buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length);
274 log_debug_msg("dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); 269 log_debug_msg("%s: packet length = %i\n", __func__, client->afc_packet->this_length);
275 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket)); 270 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
276 log_debug_msg("dispatch_AFC_packet packet data follows\n"); 271 log_debug_msg("%s: packet data follows\n", __func__);
277 if (length > 0) { 272 if (length > 0) {
278 memcpy(buffer + sizeof(AFCPacket), data, length); 273 memcpy(buffer + sizeof(AFCPacket), data, length);
279 } 274 }
@@ -354,7 +349,7 @@ static int receive_AFC_data(afc_client_t client, char **dump_here)
354 } 349 }
355 } 350 }
356 351
357 log_debug_msg("%s: received AFC packet, full len=%lld, this len=%lld, operation=%lld\n", __func__, header.entire_length, header.this_length, header.operation); 352 log_debug_msg("%s: received AFC packet, full len=%lld, this len=%lld, operation=0x%llx\n", __func__, header.entire_length, header.this_length, header.operation);
358 353
359 entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); 354 entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket);
360 this_len = (uint32_t)header.this_length - sizeof(AFCPacket); 355 this_len = (uint32_t)header.this_length - sizeof(AFCPacket);
@@ -758,7 +753,7 @@ afc_open_file(afc_client_t client, const char *filename,
758 free(data); 753 free(data);
759 754
760 if (bytes <= 0) { 755 if (bytes <= 0) {
761 log_debug_msg("afc_open_file: Didn't receive a response to the command\n"); 756 log_debug_msg("%s: Didn't receive a response to the command\n", __func__);
762 afc_unlock(client); 757 afc_unlock(client);
763 return IPHONE_E_NOT_ENOUGH_DATA; 758 return IPHONE_E_NOT_ENOUGH_DATA;
764 } 759 }
@@ -800,14 +795,14 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint
800 795
801 if (!client || !client->afc_packet || client->sfd < 0 || handle == 0) 796 if (!client || !client->afc_packet || client->sfd < 0 || handle == 0)
802 return IPHONE_E_INVALID_ARG; 797 return IPHONE_E_INVALID_ARG;
803 log_debug_msg("afc_read_file called for length %i\n", length); 798 log_debug_msg("%s: called for length %i\n", __func__, length);
804 799
805 afc_lock(client); 800 afc_lock(client);
806 801
807 // Looping here to get around the maximum amount of data that 802 // Looping here to get around the maximum amount of data that
808 // recieve_AFC_data can handle 803 // recieve_AFC_data can handle
809 while (current_count < length) { 804 while (current_count < length) {
810 log_debug_msg("afc_read_file: current count is %i but length is %i\n", current_count, length); 805 log_debug_msg("%s: current count is %i but length is %i\n", __func__, current_count, length);
811 806
812 // Send the read command 807 // Send the read command
813 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); 808 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket));
@@ -824,7 +819,7 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint
824 } 819 }
825 // Receive the data 820 // Receive the data
826 bytes_loc = receive_AFC_data(client, &input); 821 bytes_loc = receive_AFC_data(client, &input);
827 log_debug_msg("afc_read_file: bytes returned: %i\n", bytes_loc); 822 log_debug_msg("%s: bytes returned: %i\n", __func__, bytes_loc);
828 if (bytes_loc < 0) { 823 if (bytes_loc < 0) {
829 afc_unlock(client); 824 afc_unlock(client);
830 return IPHONE_E_AFC_ERROR; 825 return IPHONE_E_AFC_ERROR;
@@ -837,7 +832,7 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint
837 // success 832 // success
838 } else { 833 } else {
839 if (input) { 834 if (input) {
840 log_debug_msg("afc_read_file: %d\n", bytes_loc); 835 log_debug_msg("%s: %d\n", __func__, bytes_loc);
841 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); 836 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc);
842 free(input); 837 free(input);
843 input = NULL; 838 input = NULL;
@@ -845,7 +840,7 @@ afc_read_file(afc_client_t client, uint64_t handle, char *data, int length, uint
845 } 840 }
846 } 841 }
847 } 842 }
848 log_debug_msg("afc_read_file: returning current_count as %i\n", current_count); 843 log_debug_msg("%s: returning current_count as %i\n", __func__, current_count);
849 844
850 afc_unlock(client); 845 afc_unlock(client);
851 *bytes = current_count; 846 *bytes = current_count;
@@ -878,7 +873,7 @@ afc_write_file(afc_client_t client, uint64_t handle,
878 873
879 afc_lock(client); 874 afc_lock(client);
880 875
881 log_debug_msg("afc_write_file: Write length: %i\n", length); 876 log_debug_msg("%s: Write length: %i\n", __func__, length);
882 877
883 // Divide the file into segments. 878 // Divide the file into segments.
884 for (i = 0; i < segments; i++) { 879 for (i = 0; i < segments; i++) {
@@ -939,7 +934,7 @@ afc_write_file(afc_client_t client, uint64_t handle,
939 bytes_loc = receive_AFC_data(client, &acknowledgement); 934 bytes_loc = receive_AFC_data(client, &acknowledgement);
940 afc_unlock(client); 935 afc_unlock(client);
941 if (bytes_loc < 0) { 936 if (bytes_loc < 0) {
942 log_debug_msg("afc_write_file: uh oh?\n"); 937 log_debug_msg("%s: uh oh?\n", __func__);
943 } else { 938 } else {
944 free(acknowledgement); 939 free(acknowledgement);
945 } 940 }
@@ -961,7 +956,7 @@ iphone_error_t afc_close_file(afc_client_t client, uint64_t handle)
961 956
962 afc_lock(client); 957 afc_lock(client);
963 958
964 log_debug_msg("afc_close_file: File handle %i\n", handle); 959 log_debug_msg("%s: File handle %i\n", __func__, handle);
965 960
966 // Send command 961 // Send command
967 memcpy(buffer, &handle, sizeof(uint64_t)); 962 memcpy(buffer, &handle, sizeof(uint64_t));
@@ -971,18 +966,16 @@ iphone_error_t afc_close_file(afc_client_t client, uint64_t handle)
971 free(buffer); 966 free(buffer);
972 buffer = NULL; 967 buffer = NULL;
973 968
974 // FIXME: Is this necesary?
975 // client->afc_packet->entire_length = client->afc_packet->this_length
976 // = 0;
977
978 if (bytes <= 0) { 969 if (bytes <= 0) {
979 afc_unlock(client); 970 afc_unlock(client);
980 return IPHONE_E_UNKNOWN_ERROR; 971 return IPHONE_E_UNKNOWN_ERROR;
981 } 972 }
973
982 // Receive the response 974 // Receive the response
983 bytes = receive_AFC_data(client, &buffer); 975 bytes = receive_AFC_data(client, &buffer);
984 if (buffer) 976 if (buffer)
985 free(buffer); 977 free(buffer);
978
986 afc_unlock(client); 979 afc_unlock(client);
987 return IPHONE_E_SUCCESS; 980 return IPHONE_E_SUCCESS;
988} 981}
@@ -1008,7 +1001,7 @@ iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, afc_lock_op_t
1008 1001
1009 afc_lock(client); 1002 afc_lock(client);
1010 1003
1011 log_debug_msg("afc_lock_file: File handle %i\n", handle); 1004 log_debug_msg("%s: file handle %i\n", __func__, handle);
1012 1005
1013 // Send command 1006 // Send command
1014 memcpy(buffer, &handle, sizeof(uint64_t)); 1007 memcpy(buffer, &handle, sizeof(uint64_t));
@@ -1022,7 +1015,7 @@ iphone_error_t afc_lock_file(afc_client_t client, uint64_t handle, afc_lock_op_t
1022 1015
1023 if (bytes <= 0) { 1016 if (bytes <= 0) {
1024 afc_unlock(client); 1017 afc_unlock(client);
1025 log_debug_msg("Could not send lock command\n"); 1018 log_debug_msg("%s: could not send lock command\n", __func__);
1026 return IPHONE_E_UNKNOWN_ERROR; 1019 return IPHONE_E_UNKNOWN_ERROR;
1027 } 1020 }
1028 // Receive the response 1021 // Receive the response
@@ -1194,9 +1187,9 @@ iphone_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, cons
1194 1187
1195 afc_lock(client); 1188 afc_lock(client);
1196 1189
1197 log_debug_msg("link type: %lld\n", type); 1190 log_debug_msg("%s: link type: %lld\n", __func__, type);
1198 log_debug_msg("target: %s, length:%d\n", target, strlen(target)); 1191 log_debug_msg("%s: target: %s, length:%d\n", __func__, target, strlen(target));
1199 log_debug_msg("linkname: %s, length:%d\n", linkname, strlen(linkname)); 1192 log_debug_msg("%s: linkname: %s, length:%d\n", __func__, linkname, strlen(linkname));
1200 1193
1201 // Send command 1194 // Send command
1202 memcpy(send, &type, 8); 1195 memcpy(send, &type, 8);