Example #1
0
void USB::dumpDescriptors() {
	uint8_t i;
	for (i = 0; i < N_DESCRIPTORS; i++) {
		if (descriptors[i] == (usbdesc_base *) 0) {
			iprintf("--- FIN at %d\n", i);
			return;
		}
		iprintf("[%d:+%d]", i, descriptors[i]->bLength);
		switch (descriptors[i]->bDescType) {
			case DT_DEVICE: {
				dumpDevice((usbdesc_device *) descriptors[i]);
				break;
			}
			case DT_CONFIGURATION: {
				dumpConfiguration((usbdesc_configuration *) descriptors[i]);
				break;
			}
			case DT_INTERFACE: {
				dumpInterface((usbdesc_interface *) descriptors[i]);
				break;
			}
			case DT_ENDPOINT: {
				dumpEndpoint((usbdesc_endpoint *) descriptors[i]);
				break;
			}
			case DT_STRING: {
				dumpString((usbdesc_string *) descriptors[i]);
				break;
			}
			case DT_CDC_DESCRIPTOR: {
				dumpCDC((uint8_t *) descriptors[i]);
				break;
			}
		}
	}
}
int main(int argc, char **argv)
{
    io_registry_entry_t    service;
    io_connect_t           connect;
    kern_return_t          status;

    service = IOServiceGetMatchingService(kIOMasterPortDefault, 
                                            IOServiceMatching("AppleACPIPlatformExpert"));
    assert(service);
    if (service) 
    {
        status = IOServiceOpen(service, mach_task_self(), 0, &connect);
        IOObjectRelease(service);
        assert(kIOReturnSuccess == status);
    }

    uint32_t count = 0;
    uint32_t segment = 0;
    uint32_t maxBus = 0;
    uint32_t bus, device, fn, maxFn;
    uint32_t vendProd;

    if (argc > 3)
    {
        bus    = strtoul(argv[1], NULL, 0);
        device = strtoul(argv[2], NULL, 0);
        fn     = strtoul(argv[3], NULL, 0);
		if (argc == 4)
		{
            dumpDevice(connect, segment, bus, device, fn, NULL, NULL);
    	    count++;
		}
        if (argc > 5)
        {
            uint32_t offs;
            uint32_t data;
            offs    = strtoul(argv[4], NULL, 0);
            data = strtoul(argv[5], NULL, 0);
            configWrite32(connect, segment, bus, device, fn, offs, data);
            printf("wrote 0x%08x to [%d, %d, %d]:0x%X\n", data, bus, device, fn, offs);
        }
        else if (argc > 4)
        {
            uint32_t offs;
            uint32_t data;
            offs    = strtoul(argv[4], NULL, 0);
            data = configRead32(connect, segment, bus, device, fn, offs);
            printf("read 0x%08x from [%d, %d, %d]:0x%X\n", data, bus, device, fn, offs);
        }
    }
    else if (argc > 2)
    {
        uint64_t offs;
        uint32_t data;
        offs = strtoull(argv[1], NULL, 0);
        data = strtoul(argv[2], NULL, 0);
        physWrite32(connect, offs, data);
        printf("wrote 0x%08x to 0x%llX\n", data, offs);
    }
    else if (argc > 1)
    {
        uint64_t offs;
        uint32_t data;
        offs = strtoull(argv[1], NULL, 0);
		if (true || (offs > 0x10000ULL))
		{
			data = physRead32(connect, offs);
			printf("read 0x%08x from mem 0x%llX\n", data, offs);
		}
		else
		{
			data = ioRead32(connect, offs);
			printf("read 0x%08x from i/o 0x%llX\n", data, offs);
		}
    }
    else for (bus = 0; bus <= maxBus; bus++)
    {
        for (device = 0; device < 32; device++)
        {
            maxFn = 0;
            for (fn = 0; fn <= maxFn; fn++)
            {
                vendProd = configRead32(connect, segment, bus, device, fn, kIOPCIConfigVendorID);
                if ((0xFFFFFFFF == vendProd) || !vendProd)
                    continue;
                count++;
                dumpDevice(connect, segment, bus, device, fn, &maxBus, &maxFn);
            }
        }
    }

    printf("total: %d\n", count);
    exit(0);    
}