-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
802 lines (692 loc) · 25.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
* Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* © Copyright 2001 Apple Computer, Inc. All rights reserved.
*
* IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. (“Apple”) in
* consideration of your agreement to the following terms, and your use, installation,
* modification or redistribution of this Apple software constitutes acceptance of these
* terms. If you do not agree with these terms, please do not use, install, modify or
* redistribute this Apple software.
*
* In consideration of your agreement to abide by the following terms, and subject to these
* terms, Apple grants you a personal, non exclusive license, under Apple’s copyrights in this
* original Apple software (the “Apple Software”), to use, reproduce, modify and redistribute
* the Apple Software, with or without modifications, in source and/or binary forms; provided
* that if you redistribute the Apple Software in its entirety and without modifications, you
* must retain this notice and the following text and disclaimers in all such redistributions
* of the Apple Software. Neither the name, trademarks, service marks or logos of Apple
* Computer, Inc. may be used to endorse or promote products derived from the Apple Software
* without specific prior written permission from Apple. Except as expressly stated in this
* notice, no other rights or licenses, express or implied, are granted by Apple herein,
* including but not limited to any patent rights that may be infringed by your derivative
* works or by other works in which the Apple Software may be incorporated.
*
* The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-
* INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE
* SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
* REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
* WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
* OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// This example works with a Cypress (previously Anchor) EZ-USB device.
// It downloads firmware into the device, then runs a driver for the new device.
//
// Usage: ezload VID PID [PID2|VID2 PID2]
//
// VID is a vendor ID for a USB device, PID is the product ID.
//
// This will look for a device with the VID/PID combination to specified,
// download the firmware, then attempt to load a driver.
//
// The firmware is loaded from a file named with the hex value of VIDPID.HEX
// for example, a device with VID 1452 (0x5AC) and PID 3000 (0xBB8) will try
// to load firmware from a file 05AC0BB8.HEX. This file can be in the current
// directory or in a directory on the PATH environment variable. If an
// environment vaiableHEXPATH is defined, this will instead of the PATH variable.
// The HEX file is in Intel HEX format and is the sort of thing you'll get
// in a Windows oriented developer kit for your device.
//
// Once the firmware is loaded this will look for a device with the downloaded
// VID/PID combination. If just VID and PID are specified, the new device expected
// is VID/PID+1, if PID2 is specified VID/PID2 is expected, if VID2 and PID2 are
// specified VID2/PID2 is expected.
//
// Once the new device is attached this attempts to run a driver for it, this
// is an executable in a directory on the PATH, named similarly to the hex file
// It is executed with the new VID and PID as arguments.
// e.g. if VID=1452(0x5AC) and PID2=3001(0xBB9) this will attempt to execute:
// 05AC0BB9 1452 3001
// This example is based heavily on the USBNotification Example.
#include <unistd.h>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
#include <mach/mach.h>
#include "hex2c.h"
enum CHIPTYPE {
AN21xxSC,
CY7C68013A
};
struct ezloader {
int chiptype;
int intelhex;
int nousb;
int demon;
char *filename;
};
// AN21xxSC
#define k8051_USBCS 0x7f92
// CY7C68013A
#define kFX2_USBCS 0xe600
#define NARRATEIO 0
// globals
static IONotificationPortRef gNotifyPort;
static io_iterator_t gRawAddedIter;
static io_iterator_t gRawRemovedIter;
static io_iterator_t gNewDeviceAddedIter;
static io_iterator_t gNewDeviceRemovedIter;
IOReturn ConfigureAnchorDevice(IOUSBDeviceInterface245 **dev)
{
UInt8 numConf;
IOReturn kr;
IOUSBConfigurationDescriptorPtr confDesc;
kr = (*dev)->GetNumberOfConfigurations(dev, &numConf);
if (!numConf)
return -1;
// get the configuration descriptor for index 0
kr = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &confDesc);
if (kr)
{
printf("\tunable to get config descriptor for index %d (err = %08x)\n", 0, kr);
return -1;
}
kr = (*dev)->SetConfiguration(dev, confDesc->bConfigurationValue);
if (kr)
{
printf("\tunable to set configuration to value %d (err=%08x)\n", 0, kr);
return -1;
}
return kIOReturnSuccess;
}
IOReturn AnchorWrite(IOUSBDeviceInterface245 **dev, UInt16 anchorAddress, UInt16 count, UInt8 writeBuffer[])
{
IOUSBDevRequest request;
request.bmRequestType = USBmakebmRequestType(kUSBOut, kUSBVendor, kUSBDevice);
request.bRequest = 0xa0;
request.wValue = anchorAddress;
request.wIndex = 0;
request.wLength = count;
request.pData = writeBuffer;
return (*dev)->DeviceRequest(dev, &request);
}
#if 0
void runDeviceDriver(UInt16 vendor, UInt16 product)
{
char name[]="12341234",
vendorStr[]="65535",
prodStr[]="65535";
pid_t newPID;
sprintf(name, "%04X%04X", vendor, product);
printf("Command to exec is \"%s\"\n", name);
sprintf(vendorStr, "%d", vendor);
sprintf(prodStr, "%d", product);
printf("Args are %s/%s\n", vendorStr, prodStr);
newPID = fork();
if(newPID == 0)
{
execlp(name, name, vendorStr, prodStr, NULL);
printf("Failed to exec new driver (err = %d)\n", errno);
}
else if(newPID == -1)
{
printf("Fork failed to make new process for driver (err = %d)\n", errno);
}
}
#endif
FILE *openFile(UInt16 vendor, UInt16 product, char *filename)
{
char path[256];
char *pathp = path;
FILE *hexFile;
pathp = getenv("HEXPATH");
if(pathp != nil)
{
strncpy(path, pathp, 256);
}
else
{
pathp = getenv("PATH");
strncpy(path, pathp, 256);
}
// printf("Found path %s\n", path);
printf("File to open is \"%s\"\n", filename);
do{
hexFile = fopen(filename, "r");
if(hexFile != nil)
{
getcwd(path, 255);
printf("From directory %s\n", path);
break;
}
if(pathp == nil)
{
break;
}
if( chdir(strsep(&pathp, ":")) != 0)
{
printf("chdir failed with errno %d\n", errno);
perror(nil);
break;
}
}while(1);
return(hexFile);
}
#if 0
Description of Intel hex records.
Position Description
1 Record Marker: The first character of the line is always a colon (ASCII 0x3A) to identify
the line as an Intel HEX file
2 - 3 Record Length: This field contains the number of data bytes in the register represented
as a 2-digit hexidecimal number. This is the total number of data bytes,
not including the checksum byte nor the first 9 characters of the line.
4 - 7 Address: This field contains the address where the data should be loaded into the chip.
This is a value from 0 to 65,535 represented as a 4-digit hexidecimal value.
8 - 9 Record Type: This field indicates the type of record for this line. The possible values
are: 00=Register contains normal data. 01=End of File. 02=Extended address.
10 - ? Data Bytes: The following bytes are the actual data that will be burned into the EPROM. The
data is represented as 2-digit hexidecimal values.
Last 2 characters Checksum: The last two characters of the line are a checksum for the line. The
checksum value is calculated by taking the twos complement of the sum of all
the preceeding data bytes, excluding the checksum byte itself and the colon
at the beginning of the line.
#endif
IOReturn hexRead(INTEL_HEX_RECORD *record, FILE *hexFile)
{ // Read the next hex record from the file into the structure
// **** Need to impliment checksum checking ****
char c;
UInt16 i;
int n, c1, check, len;
c = getc(hexFile);
if(c != ':')
{
printf("Line does not start with colon (%d)\n", c);
return(kIOReturnNotAligned);
}
n = fscanf(hexFile, "%2lX%4lX%2lX", &record->Length, &record->Address, &record->Type);
if(n != 3)
{
printf("Could not read line preamble %d\n", c);
return(kIOReturnNotAligned);
}
len = record->Length;
if(len > MAX_INTEL_HEX_RECORD_LENGTH)
{
printf("length is more than can fit %d, %d\n", len, MAX_INTEL_HEX_RECORD_LENGTH);
return(kIOReturnNotAligned);
}
for(i = 0; i<len; i++)
{
n = fscanf(hexFile, "%2X", &c1);
if(n != 1)
{
if(i != record->Length)
{
printf("Line finished at wrong time %d, %ld\n", i, record->Length);
return(kIOReturnNotAligned);
}
}
record->Data[i] = c1;
}
n = fscanf(hexFile, "%2X\n", &check);
if(n != 1)
{
printf("Check not found\n");
return(kIOReturnNotAligned);
}
return(kIOReturnSuccess);
}
IOReturn DownloadToAnchorDevice(IOUSBDeviceInterface245 **dev, UInt16 vendor, UInt16 product,
int chiptype, char *filename, int intelhex)
{
UInt8 writeVal;
IOReturn kr;
FILE *hexFile;
INTEL_HEX_RECORD anchorCode;
if(intelhex == 1) {
hexFile = openFile(vendor, product, filename);
} else {
hexFile = fopen(filename, "rb");
}
if(hexFile == nil)
{
printf("File open failed\n");
return(kIOReturnNotOpen);
}
// Assert reset
writeVal = 1;
if(chiptype == AN21xxSC) {
kr = AnchorWrite(dev, k8051_USBCS, 1, &writeVal);
} else {
kr = AnchorWrite(dev, kFX2_USBCS, 1, &writeVal);
}
if (kIOReturnSuccess != kr)
{
printf("AnchorWrite reset returned err 0x%x!\n", kr);
// Don't do this, the calling function does this on error.
// (*dev)->USBDeviceClose(dev);
// (*dev)->Release(dev);
return kr;
}
int address = 0;
// Download code
while (1)
{
if(intelhex == 1) {
kr = hexRead(&anchorCode, hexFile);
if(anchorCode.Type != 0)
{
break;
}
} else {
anchorCode.Length = fread(&anchorCode.Data, 1, sizeof(anchorCode.Data), hexFile);
if(anchorCode.Length == 0) {
break;
}
anchorCode.Address = address;
address += anchorCode.Length;
kr = kIOReturnSuccess;
}
if(kr == kIOReturnSuccess)
{
kr = AnchorWrite(dev, anchorCode.Address, anchorCode.Length, anchorCode.Data);
}
if (kIOReturnSuccess != kr)
{
printf("AnchorWrite download %lx returned err 0x%x!\n", anchorCode.Address, kr);
// Don't do this, the calling function does this on error.
// (*dev)->USBDeviceClose(dev);
// (*dev)->Release(dev);
return kr;
}
#if NARRATEDOWNLOAD
printf("%04lx ",anchorCode.Address);
#endif
}
#if NARRATEDOWNLOAD
printf("\n");
#endif
// De-assert reset
writeVal = 0;
if(chiptype == AN21xxSC) {
kr = AnchorWrite(dev, k8051_USBCS, 1, &writeVal);
} else {
kr = AnchorWrite(dev, kFX2_USBCS, 1, &writeVal);
}
if (kIOReturnSuccess != kr)
{
printf("AnchorWrite run returned err 0x%x!\n", kr);
}
return kr;
}
void RawDeviceAdded(void *refCon, io_iterator_t iterator)
{
kern_return_t kr;
io_service_t usbDevice;
IOCFPlugInInterface **plugInInterface=NULL;
IOUSBDeviceInterface245 **dev=NULL;
HRESULT res;
SInt32 score;
UInt16 vendor;
UInt16 product;
UInt16 release;
int exclusiveErr = 0;
struct ezloader *param = (struct ezloader *)refCon;
while ( (usbDevice = IOIteratorNext(iterator)) )
{
printf("Raw device added.\n");
kr = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
kr = IOObjectRelease(usbDevice); // done with the device object now that I have the plugin
if ((kIOReturnSuccess != kr) || !plugInInterface)
{
printf("unable to create a plugin (%08x)\n", kr);
continue;
}
// I have the device plugin, I need the device interface
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245), (LPVOID)&dev);
IODestroyPlugInInterface(plugInInterface); // done with this
if (res || !dev)
{
printf("couldn't create a device interface (%08x)\n", (int) res);
continue;
}
// technically should check these kr values
kr = (*dev)->GetDeviceVendor(dev, &vendor);
kr = (*dev)->GetDeviceProduct(dev, &product);
kr = (*dev)->GetDeviceReleaseNumber(dev, &release);
// need to open the device in order to change its state
do{
kr = (*dev)->USBDeviceOpen(dev);
if(kIOReturnExclusiveAccess == kr)
{
exclusiveErr++;
printf("Exclusive access err, sleeping on it %d\n", exclusiveErr);
sleep(1);
}
}while( (kIOReturnExclusiveAccess == kr) && (exclusiveErr < 5) );
if (kIOReturnSuccess != kr)
{
printf("unable to open device: %08x\n", kr);
(void) (*dev)->Release(dev);
continue;
}
kr = ConfigureAnchorDevice(dev);
if (kIOReturnSuccess != kr)
{
printf("unable to configure device: %08x\n", kr);
(void) (*dev)->USBDeviceClose(dev);
(void) (*dev)->Release(dev);
continue;
}
kr = DownloadToAnchorDevice(dev, vendor, product, param->chiptype, param->filename, param->intelhex);
if (kIOReturnSuccess != kr)
{
printf("unable to download to device: %08x\n", kr);
(void) (*dev)->USBDeviceClose(dev);
(void) (*dev)->Release(dev);
continue;
}
kr = (*dev)->USBDeviceClose(dev);
kr = (*dev)->Release(dev);
}
}
void RawDeviceRemoved(void *refCon, io_iterator_t iterator)
{
kern_return_t kr;
io_service_t obj;
struct ezloader *param = (struct ezloader *)refCon;
while ( (obj = IOIteratorNext(iterator)) )
{
printf("Raw device removed.\n");
kr = IOObjectRelease(obj);
if(param->demon == 0 && param->nousb == 1) {
exit(0);
}
}
}
void NewDeviceAdded(void *refCon, io_iterator_t iterator)
{
kern_return_t kr;
io_service_t usbDevice;
IOCFPlugInInterface **plugInInterface=NULL;
IOUSBDeviceInterface245 **dev=NULL;
HRESULT res;
SInt32 score;
UInt16 vendor;
UInt16 product;
while ( (usbDevice = IOIteratorNext(iterator)) )
{
printf("New device added.\n");
kr = IOCreatePlugInInterfaceForService(usbDevice, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
kr = IOObjectRelease(usbDevice); // done with the device object now that I have the plugin
if ((kIOReturnSuccess != kr) || !plugInInterface)
{
printf("unable to create a plugin (%08x)\n", kr);
continue;
}
// I have the device plugin, I need the device interface
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID245), (LPVOID)&dev);
IODestroyPlugInInterface(plugInInterface); // done with this
if (res || !dev)
{
printf("couldn't create a device interface (%08x)\n", (int) res);
continue;
}
// technically should check these kr values
kr = (*dev)->GetDeviceVendor(dev, &vendor);
kr = (*dev)->GetDeviceProduct(dev, &product);
// runDeviceDriver(vendor, product);
exit(1);
}
}
void NewDeviceRemoved(void *refCon, io_iterator_t iterator)
{
kern_return_t kr;
io_service_t obj;
while ( (obj = IOIteratorNext(iterator)) )
{
printf("New device removed.\n");
kr = IOObjectRelease(obj);
}
}
void SignalHandler(int sigraised)
{
printf("\nInterrupted\n");
// Clean up here
IONotificationPortDestroy(gNotifyPort);
if (gRawAddedIter)
{
IOObjectRelease(gRawAddedIter);
gRawAddedIter = 0;
}
if (gRawRemovedIter)
{
IOObjectRelease(gRawRemovedIter);
gRawRemovedIter = 0;
}
if (gNewDeviceAddedIter)
{
IOObjectRelease(gNewDeviceAddedIter);
gNewDeviceAddedIter = 0;
}
if (gNewDeviceRemovedIter)
{
IOObjectRelease(gNewDeviceRemovedIter);
gNewDeviceRemovedIter = 0;
}
// exit(0) should not be called from a signal handler. Use _exit(0) instead
//
_exit(0);
}
int main (int argc, const char *argv[])
{
mach_port_t masterPort;
CFMutableDictionaryRef matchingDict;
CFRunLoopSourceRef runLoopSource;
kern_return_t kr;
SInt32 usbVendor = 0xdead;//kOurVendorID;
SInt32 usbProduct = 0xbeef;// kOurProductID;
SInt32 usbVendor2;
SInt32 usbProduct2;
sig_t oldHandler;
int ch;
char name[]="12341234.hex";
struct ezloader param;
usbVendor2 = 0;
usbProduct2 = 0;
param.chiptype = AN21xxSC;
param.intelhex = 1;
param.filename = nil;
param.nousb = 0;
param.demon = 0;
while ((ch = getopt(argc,argv,"?v:p:V:P:f:Frnd")) != -1) {
switch (ch) {
case 'v':
if(strncmp(optarg, "0x", 2) == 0)
sscanf(optarg+2, "%x", &usbVendor);
else
usbVendor = atoi(optarg);
break;
case 'p':
if(strncmp(optarg, "0x", 2) == 0)
sscanf(optarg+2, "%x", &usbProduct);
else
usbProduct = atoi(optarg);
break;
case 'V':
if(strncmp(optarg, "0x", 2) == 0)
sscanf(optarg+2, "%x", &usbVendor2);
else
usbVendor2 = atoi(optarg);
break;
case 'P':
if(strncmp(optarg, "0x", 2) == 0)
sscanf(optarg+2, "%x", &usbProduct2);
else
usbProduct2 = atoi(optarg);
break;
case 'r':
param.intelhex = 0;
break;
case 'n':
param.nousb = 1;
break;
case 'd':
param.demon = 1;
break;
case 'f':
param.filename = optarg;
break;
case 'F':
param.chiptype = CY7C68013A;
break;
case '?':
printf("Usage: loadez -v VID -p PID -V NEWVID -P NEWPID -f filename\n");
exit(0);
break;
}
}
if(param.chiptype == CY7C68013A && usbVendor == 0xdead && usbProduct == 0xbeef) {
usbVendor = 0x04b4;
usbProduct = 0x8613;
}
if(usbVendor2 == 0)
usbVendor2 = usbVendor;
if(usbProduct2 == 0)
usbProduct2 = usbProduct+1;
if(param.filename == nil) {
sprintf(name, "%04X%04X.HEX", usbVendor, usbProduct);
param.filename = name;
}
// Set up a signal handler so we can clean up when we're interrupted from the command line
// Otherwise we stay in our run loop forever.
oldHandler = signal(SIGINT, SignalHandler);
if (oldHandler == SIG_ERR)
printf("Could not establish new signal handler");
// first create a master_port for my task
kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (kr || !masterPort)
{
printf("ERR: Couldn't create a master IOKit Port(%08x)\n", kr);
return -1;
}
printf("\nLooking for devices matching vendor ID=0x%04x and product ID=0x%04x\n", usbVendor, usbProduct);
// Set up the matching criteria for the devices we're interested in
matchingDict = IOServiceMatching(kIOUSBDeviceClassName); // Interested in instances of class IOUSBDevice and its subclasses
if (!matchingDict)
{
printf("Can't create a USB matching dictionary\n");
mach_port_deallocate(mach_task_self(), masterPort);
return -1;
}
// Add our vendor and product IDs to the matching criteria
CFDictionarySetValue(
matchingDict,
CFSTR(kUSBVendorID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(
matchingDict,
CFSTR(kUSBProductID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbProduct));
// Create a notification port and add its run loop event source to our run loop
// This is how async notifications get set up.
gNotifyPort = IONotificationPortCreate(masterPort);
runLoopSource = IONotificationPortGetRunLoopSource(gNotifyPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
// Retain additional references because we use this same dictionary with four calls to
// IOServiceAddMatchingNotification, each of which consumes one reference.
matchingDict = (CFMutableDictionaryRef) CFRetain( matchingDict );
matchingDict = (CFMutableDictionaryRef) CFRetain( matchingDict );
matchingDict = (CFMutableDictionaryRef) CFRetain( matchingDict );
// Now set up two notifications, one to be called when a raw device is first matched by I/O Kit, and the other to be
// called when the device is terminated.
kr = IOServiceAddMatchingNotification( gNotifyPort,
kIOFirstMatchNotification,
matchingDict,
RawDeviceAdded,
¶m,
&gRawAddedIter );
RawDeviceAdded(¶m, gRawAddedIter); // Iterate once to get already-present devices and
// arm the notification
kr = IOServiceAddMatchingNotification( gNotifyPort,
kIOTerminatedNotification,
matchingDict,
RawDeviceRemoved,
¶m,
&gRawRemovedIter );
RawDeviceRemoved(¶m, gRawRemovedIter); // Iterate once to arm the notification
if(param.nousb == 0 || param.demon == 0) {
// Change the USB product ID in our matching dictionary to the one the device will have once the
// firmware has been downloaded.
printf("Downloaded devices should match vendor ID=0x%04x and product ID=0x%04x\n", usbVendor2, usbProduct2);
CFDictionarySetValue(
matchingDict,
CFSTR(kUSBVendorID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbVendor2));
CFDictionarySetValue(
matchingDict,
CFSTR(kUSBProductID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &usbProduct2));
// Now set up two more notifications, one to be called when a new test device is first matched by I/O Kit, and the other to be
// called when the device is terminated.
kr = IOServiceAddMatchingNotification( gNotifyPort,
kIOFirstMatchNotification,
matchingDict,
NewDeviceAdded,
NULL,
&gNewDeviceAddedIter );
NewDeviceAdded(NULL, gNewDeviceAddedIter); // Iterate once to get already-present devices and
// arm the notification
kr = IOServiceAddMatchingNotification( gNotifyPort,
kIOTerminatedNotification,
matchingDict,
NewDeviceRemoved,
NULL,
&gNewDeviceRemovedIter );
NewDeviceRemoved(NULL, gNewDeviceRemovedIter); // Iterate once to arm the notification
}
// Now done with the master_port
mach_port_deallocate(mach_task_self(), masterPort);
masterPort = 0;
// Start the run loop. Now we'll receive notifications.
CFRunLoopRun();
// We should never get here
return 0;
}