Example #1
0
int set_bootloader_to_winusb(int verbose) {
    int opt_silent = verbose;
    struct wdi_device_info *device, list;
    char* inf_name = NULL;
    int r;
    struct wdi_options_prepare_driver driver_opts = {0};

    driver_opts.driver_type = WDI_WINUSB;
    driver_opts.vendor_name = NULL;
    driver_opts.device_guid = NULL;
    driver_opts.disable_cat = FALSE;
    driver_opts.disable_signing = FALSE;
    driver_opts.use_wcid_driver = FALSE;

    list = listDevices(verbose);
    if (&list != NULL) {
        oprintf("set_bootloader_to_winusb - valid list\n");
        for (device = &list; device != NULL; device = device->next) {
            if ((device->vid == BOOTLOADER_VID) && (device->pid == BOOTLOADER_PID)) {
                oprintf("Installing using inf name: %s\n", INF_NAME);
                if ((wdi_prepare_driver(device, DEFAULT_DIR, INF_NAME, &driver_opts) == WDI_SUCCESS) & (device->driver != "WINUSB")) {
                    oprintf("Prepared driver for install\n");
                    r=wdi_install_driver(device, DEFAULT_DIR, INF_NAME, NULL);
                    oprintf("got return code: %d=%s \n", r,wdi_strerror(r));
                }
            }
        }
    }
    return r;
}
Example #2
0
int __cdecl main(int argc, char** argv)
{
    static struct wdi_device_info *ldev, dev = {NULL, VID, PID, FALSE, 0, DESC, NULL, NULL, NULL};
    static struct wdi_options_create_list ocl = { 0 };
    static struct wdi_options_prepare_driver opd = { 0 };
    static struct wdi_options_install_driver oid = { 0 };
    static struct wdi_options_install_cert oic = { 0 };
    static int opt_silent = 0, opt_extract = 0, log_level = WDI_LOG_LEVEL_WARNING;
    static BOOL matching_device_found;
    int c, r;
    char *inf_name = INF_NAME;
    char *ext_dir = DEFAULT_DIR;
    char *cert_name = NULL;

    static struct option long_options[] = {
        {"name", required_argument, 0, 'n'},
        {"inf", required_argument, 0, 'f'},
        {"manufacturer", required_argument, 0, 'm'},
        {"vid", required_argument, 0, 'v'},
        {"pid", required_argument, 0, 'p'},
        {"iid", required_argument, 0, 'i'},
        {"type", required_argument, 0, 't'},
        {"filter", no_argument, 0, 2},
        {"wcid", no_argument, 0, 'w'},
        {"dest", required_argument, 0, 'd'},
        {"cert", required_argument, 0, 'c'},
        {"extract", no_argument, 0, 'x'},
        {"silent", no_argument, 0, 's'},
        {"stealth-cert", no_argument, 0, 1},
        {"progressbar", optional_argument, 0, 'b'},
        {"log", required_argument, 0, 'l'},
        {"timeout", required_argument, 0, 'o'},
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}
    };

    ocl.list_all = TRUE;
    ocl.list_hubs = TRUE;
    ocl.trim_whitespaces = TRUE;
    opd.driver_type = WDI_WINUSB;

    while(1)
    {
        c = getopt_long(argc, argv, "n:f:m:d:c:v:p:i:l:t:o:hxsb", long_options, NULL);
        if (c == -1)
            break;
        switch(c) {
        case 1: // --stealth-cert
            oic.disable_warning = TRUE;
            break;
        case 2: // --filter
            oid.install_filter_driver = TRUE;
            break;
        case 'n':
            dev.desc = optarg;
            break;
        case 'm':
            opd.vendor_name = optarg;
            break;
        case 'f':
            inf_name = optarg;
            break;
        case 'd':
            ext_dir = optarg;
            break;
        case 'c':
            cert_name = optarg;
            break;
        case 'v':
            dev.vid = (unsigned short)strtol(optarg, NULL, 0);
            break;
        case 'o':
            oid.pending_install_timeout = (DWORD)strtoul(optarg, NULL, 0);
            break;
        case 'p':
            dev.pid = (unsigned short)strtol(optarg, NULL, 0);
            break;
        case 'i':
            dev.is_composite = TRUE;
            dev.mi = (unsigned char)strtol(optarg, NULL, 0);
            break;
        case 't':
            opd.driver_type = (int)strtol(optarg, NULL, 0);
            break;
        case 'w':
            opd.use_wcid_driver = TRUE;
            break;
        case 'h':
            usage();
            exit(0);
            break;
        case 'x':
            opt_extract = 1;
            break;
        case 's':
            opt_silent = 1;
            log_level = WDI_LOG_LEVEL_NONE;
            break;
        case 'b':
            oid.hWnd = (optarg)?(HWND)strtol(optarg, NULL, 0):GetConsoleHwnd();
            oic.hWnd = oid.hWnd;
            break;
        case 'l':
            log_level = (int)strtol(optarg, NULL, 0);
            break;
        default:
            usage();
            exit(0);
        }
    }

    wdi_set_log_level(log_level);

    oprintf("Extracting driver files...\n");
    r = wdi_prepare_driver(&dev, ext_dir, inf_name, &opd);
    oprintf("  %s\n", wdi_strerror(r));
    if ((r != WDI_SUCCESS) || (opt_extract))
        return r;

    if (cert_name != NULL) {
        oprintf("Installing certificate '%s' as a Trusted Publisher...\n", cert_name);
        r = wdi_install_trusted_certificate(cert_name, &oic);
        oprintf("  %s\n", wdi_strerror(r));
    }

    oprintf("Installing driver(s)...\n");

    // Try to match against a plugged device to avoid device manager prompts
    matching_device_found = FALSE;
    if (wdi_create_list(&ldev, &ocl) == WDI_SUCCESS) {
        r = WDI_SUCCESS;
        for (; (ldev != NULL) && (r == WDI_SUCCESS); ldev = ldev->next) {
            if ( (ldev->vid == dev.vid) && (ldev->pid == dev.pid) && (ldev->mi == dev.mi) ) {
                dev.hardware_id = ldev->hardware_id;
                dev.device_id = ldev->device_id;
                matching_device_found = TRUE;
                oprintf("  %s: ", dev.hardware_id);
                fflush(stdout);
                r = wdi_install_driver(&dev, ext_dir, inf_name, &oid);
                oprintf("%s\n", wdi_strerror(r));
            }
        }
    }

    // No plugged USB device matches this one -> install driver
    if (!matching_device_found) {
        r = wdi_install_driver(&dev, ext_dir, inf_name, &oid);
        oprintf("  %s\n", wdi_strerror(r));
    }

    return r;
}
Example #3
0
// The following is necessary when compiled from a WDK/DDK environment
int __cdecl main(int argc, char *argv[])
{
	int c;
	struct wdi_device_info *device, *list;
	char* path = "usb_driver";
	static struct wdi_options_create_list cl_options = { 0 };
	static struct wdi_options_prepare_driver pd_options = { 0 };

	static int prompt_flag = 1;
	static unsigned char iface = 0;
	static int vid = 0;
	static unsigned short pid = 0;
	static int verbose_flag = 3;
	static char *desc = NULL;
	static int use_supplied_inf_flag = 0;
	int r, option_index = 0;

	cl_options.trim_whitespaces = TRUE;

	// Parse command-line options
	while(1)
	{
		static struct option long_options[] = {
			// These options set a flag.
			{"noprompt", no_argument, &prompt_flag, 0},
			{"usealldevices", no_argument, &cl_options.list_all, 1},
			{"useinf", no_argument, &use_supplied_inf_flag, 1},
			{"iface", required_argument, 0, 'a'},
			{"vid", required_argument, 0, 'b'},
			{"pid", required_argument, 0, 'c'},
			{"help", no_argument, 0, 'd'},
			{"verbose", no_argument, &verbose_flag, 0},
			{"create", required_argument,0, 'e'},
			{0, 0, 0, 0}
		};

		c = getopt_long(argc, argv, "abc:d:e:",long_options, &option_index);
		//  Detect the end of the options.
		if (c == -1)
			break;

		switch(c)
		{
		case 0:
			// If this option set a flag, do nothing else now.
			if (long_options[option_index].flag != 0) {
				break;
			}
			printf("option %s", long_options[option_index].name);
			if (optarg) {
				printf (" with arg %s", optarg);
			}
			printf("\n");
			break;
		case 'a':
			iface = (unsigned char)atoi(optarg);
			printf("OPT: interface number %d\n", iface);
			break;
		case 'b':
			vid = (unsigned short)strtol(optarg, NULL, 0);
			printf("OPT: VID number %d\n", vid);
			break;
		case 'c':
			pid = (unsigned short)strtol(optarg, NULL, 0);
			printf("OPT: PID number %d\n", pid);
			break;
		case 'd': // getopt_long already printed an error message.
			usage();
			exit(0);
			break;
		case 'e': //create requires a description argument
			desc = optarg;
			break;
		default:
			usage();
			abort ();
		}
	}

	if (desc) {
		// If the device is created from scratch, override the existing device
		if (vid == 0 || pid == 0) {
			printf("Must specify both --vid and --pid with --create\n");
			return 1;
		}
		device = (struct wdi_device_info*)calloc(1, sizeof(struct wdi_device_info));
		if (device == NULL) {
			printf("could not create new device_info struct for installation");
			return 1;
		}
		device->desc = desc;
		device->vid = vid;
		device->pid = pid;
		printf("Creating USB device: device: \"%s\" (%04X:%04X)\n", device->desc, device->vid, device->pid);
		wdi_set_log_level(verbose_flag);
		if (wdi_prepare_driver(device, path, INF_NAME, &pd_options) == WDI_SUCCESS) {
			printf("installing wdi driver with <%s> at <%s>\n", INF_NAME, path);
			r = wdi_install_driver(device, path, INF_NAME, NULL);
			if (r != WDI_SUCCESS) {
				printf("Failed to install driver: %s\n", wdi_strerror(r));
			}
		}
		free(device);
		return r == WDI_SUCCESS ? 0 : 1;
	}

	r = wdi_create_list(&list, &cl_options);
	switch (r) {
	case WDI_SUCCESS:
		break;
	case WDI_ERROR_NO_DEVICE:
		printf("No driverless USB devices were found.\n");
		return 0;
	default:
		printf("wdi_create_list: error %s\n", wdi_strerror(r));
		return 0;
	}

	for (device = list; device != NULL; device = device->next) {
		printf("Found USB device: \"%s\" (%04X:%04X)\n", device->desc, device->vid, device->pid);
		wdi_set_log_level(verbose_flag);
		// If vid and pid have not been initialized
		// prompt user to install driver for each device
		if(vid == 0 || pid == 0) {
			printf("Do you want to install a driver for this device (y/n)?\n");
			c = (char) getchar();
			FLUSHER;
			if ((c!='y') && (c!='Y')) {
				continue;
			}
			// Otherwise a specific vid and pid have been given
			// so drivers will install automatically
		} else {
			// Is VID and PID a match for our device
			if ( (device->vid != vid) || (device->pid != pid)
			  || (device->mi != iface) ) {
				continue;
			}
	}
		// Does the user want to use a supplied .inf
		if (use_supplied_inf_flag == 0) {
			if (wdi_prepare_driver(device, path, INF_NAME, &pd_options) == WDI_SUCCESS) {
				printf("installing wdi driver with <%s> at <%s>\n",INF_NAME, path);
				wdi_install_driver(device, path, INF_NAME, NULL);
			}
		} else {
			printf("installing wdi driver with <%s> at <%s>\n",INF_NAME, path);
			wdi_install_driver(device, path, INF_NAME, NULL);
		}
	}
	wdi_destroy_list(list);

	// This is needed when ran in UAC mode
	if (prompt_flag) {
		printf("\nPress Enter to exit this program\n");
		FLUSHER;
	}
	return 0;
}