Commit 0885e9ac authored by Zygmunt Krynicki's avatar Zygmunt Krynicki
Browse files

lm4flash.c: Rename old main to flasher_flash()

This function, given all arguments, simply flashes the image
parent 78a94f34
...@@ -606,42 +606,63 @@ flasher_usage() ...@@ -606,42 +606,63 @@ flasher_usage()
printf("\t-s SERIAL - Flash device with the following serial\n"); printf("\t-s SERIAL - Flash device with the following serial\n");
} }
int main(int argc, char *argv[])
static
int
flasher_flash(
int do_verify,
const char *serial,
const char *rom_name
)
{ {
libusb_context *ctx = NULL; libusb_context *ctx = NULL;
libusb_device *device = NULL;
libusb_device_handle *handle = NULL; libusb_device_handle *handle = NULL;
int retval = 1; int retval;
FILE *f = NULL; FILE *f = NULL;
if (argc < 2) { retval = libusb_init(&ctx);
printf("usage: %s [-v] <binary-file>\n", argv[0]);
printf("\t-v : enables verification after write\n"); if (retval != 0) {
fprintf(stderr, "Error initializing libusb: %s\n",
libusb_error_name(retval));
goto done; goto done;
} }
if ((argc == 3) && (strncmp(argv[1], "-v", strlen("-v")) == 0)) switch (flasher_find_matching_device(
do_verify = 1; ctx, &device, &retval, ICDI_VID, ICDI_PID, serial)) {
case FLASHER_SUCCESS:
if (libusb_init(&ctx) != 0) { break;
fprintf(stderr, "Error initializing libusb\n"); case FLASHER_ERR_LIBUSB_FAILURE:
fprintf(stderr, "Error while matching ICDI devices: %s\n",
libusb_error_name(retval));
goto done;
case FLASHER_ERR_NO_DEVICES:
fprintf(stderr, "Unable to find any ICDI devices\n");
goto done;
case FLASHER_ERR_MULTIPLE_DEVICES:
if (serial == NULL)
fprintf(stderr, "Found multiple ICDI devices\n");
else
fprintf(stderr, "Found ICDI serial number collision!\n");
goto done; goto done;
} }
/* FIXME: should not be using this function call! */ retval = libusb_open(device, &handle);
handle = libusb_open_device_with_vid_pid(ctx, ICDI_VID, ICDI_PID); if (retval != 0) {
if (!handle) { fprintf(stderr, "Error opening selected device: %s\n",
fprintf(stderr, "No ICDI device with USB VID:PID %04x:%04x found!\n", libusb_error_name(retval));
ICDI_VID, ICDI_PID);
goto done; goto done;
} }
retval = libusb_claim_interface(handle, INTERFACE_NR); retval = libusb_claim_interface(handle, INTERFACE_NR);
if (retval != 0) { if (retval != 0) {
printf("Error claiming interface %d\n", retval); fprintf(stderr, "Error claiming interface: %s\n",
libusb_error_name(retval));
goto done; goto done;
} }
f = fopen(argv[argc - 1], "rb"); f = fopen(rom_name, "rb");
if (!f) { if (!f) {
perror("fopen"); perror("fopen");
retval = 1; retval = 1;
...@@ -655,8 +676,11 @@ done: ...@@ -655,8 +676,11 @@ done:
fclose(f); fclose(f);
if (handle) if (handle)
libusb_close(handle); libusb_close(handle);
if (device)
libusb_unref_device(device);
if (ctx) if (ctx)
libusb_exit(ctx); libusb_exit(ctx);
return retval; return retval;
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment