Skip to contentSkip to conversation

patch for fxload

PATCH Dirk Jagdmann 1 participant 1 message Sep 5, 2004

lore
Share & tools
lore
#0 lore raw

Hello coders,

I had to debug something connected to fxload and wasn't able (for reasons I don't know yet) to see the error messages from stderr. So I patched fxload to output messages to syslog instead and made this switchable via command line. You are free to include this code with the official versions if you like.

For another reason unknown to me my editor refused to make my changes idented like the rest of the code. Shame on me. Please tell me if you can fix it or if I should try harder (and with a different editor).

Signature · 3 lines
---> doj / cubic
----> http://cubic.org/~doj
-----> http://llg.cubic.org
ezusb.c +40 -41
diff -u -U 5 -r --new-file -x *~ -X /tmp/diffexclude fxload-2002_04_11/ezusb.c fxload-2002_04_11.doj/ezusb.c
@@ -32,10 +32,12 @@
 # include  <linux/usb.h>
 # include  <linux/usbdevice_fs.h>
 
 # include "ezusb.h"
 
+void logerror(char *format, ...);
+
 /*
  * This file contains functions for downloading firmware into Cypress
  * EZ-USB microcontrollers. These chips use control endpoint 0 and vendor
  * specific commands to support writing into the on-chip SRAM. They also
  * support writing into the CPUCS register, which is how we reset the
@@ -121,11 +123,11 @@
     size_t				length
 ) {
     struct usbdevfs_ctrltransfer	ctrl;
 
     if (length > USHRT_MAX) {
-	fputs ("length too big", stderr);
+	logerror("length too big\n");
 	return -EINVAL;
     }
 
     /* 8 bytes SETUP */
     ctrl.bRequestType = requestType;
@@ -168,21 +170,20 @@
     size_t				len
 ) {
     int					status;
 
     if (verbose)
-	fprintf (stderr, "%s, addr 0x%04x len %4d (0x%04x)\n",
-		label, addr, len, len);
+	logerror("%s, addr 0x%04x len %4d (0x%04x)\n", label, addr, len, len);
     status = ctrl_msg (device,
 	USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, opcode,
 	addr, 0,
 	data, len);
     if (status != len) {
 	if (status < 0)
-	    perror (label);
+	    logerror("%s : %s\n", strerror(errno), label);
 	else
-	    fprintf (stderr, "%s ==> %d\n", label, status);
+	    logerror("%s ==> %d\n", label, status);
     }
     return status;
 }
 
 /*
@@ -197,21 +198,20 @@
     size_t				len
 ) {
     int					status;
 
     if (verbose)
-	fprintf (stderr, "%s, addr 0x%04x len %4d (0x%04x)\n",
-		label, addr, len, len);
+	logerror("%s, addr 0x%04x len %4d (0x%04x)\n", label, addr, len, len);
     status = ctrl_msg (device,
 	USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, opcode,
 	addr, 0,
 	(unsigned char *) data, len);
     if (status != len) {
 	if (status < 0)
-	    perror (label);
+	    logerror("%s : %s", strerror(errno), label);
 	else
-	    fprintf (stderr, "%s ==> %d\n", label, status);
+	    logerror("%s ==> %d\n", label, status);
     }
     return status;
 }
 
 /*
@@ -225,22 +225,21 @@
 ) {
     int			status;
     unsigned char	data = doRun ? 0 : 1;
 
     if (verbose)
-	fprintf (stderr, "%s\n", data ? "stop CPU" : "reset CPU");
+	logerror("%s\n", data ? "stop CPU" : "reset CPU");
     status = ctrl_msg (device,
 	USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 	RW_INTERNAL,
 	addr, 0,
 	&data, 1);
     if (status != 1) {
-	char *mesg = "can't modify CPUCS";
 	if (status < 0)
-	    perror (mesg);
+	    logerror("%s : %s", strerror(errno), device);
 	else
-	    fprintf (stderr, "%s\n", mesg);
+	    logerror("can't modify CPUCS\n");
 	return 0;
     } else
 	return 1;
 }
 
@@ -303,28 +302,28 @@
 	size_t		len;
 	unsigned	idx, off;
 
 	cp = fgets(buf, sizeof buf, image);
 	if (cp == 0) {
-	    fprintf (stderr, "EOF without EOF record!\n");
+	    logerror("EOF without EOF record!\n");
 	    break;
 	}
 
 	/* EXTENSION: "# comment-till-end-of-line", for copyrights etc */
 	if (buf[0] == '#')
 	    continue;
 
 	if (buf[0] != ':') {
-	    fprintf (stderr, "not an ihex record: %s", buf);
+	    logerror("not an ihex record: %s", buf);
 	    return -2;
 	}
 
 	if (verbose >= 3) {
 	    cp = strchr (buf, '\n');
 	    if (cp)
 		*cp = 0;
-	    fprintf (stderr, "** LINE: %s\n", buf);
+	    logerror("** LINE: %s\n", buf);
 	}
 
 	/* Read the length field (up to 16 bytes) */
 	tmp = buf[3];
 	buf[3] = 0;
@@ -350,21 +349,21 @@
 	buf[9] = tmp;
 
 	/* If this is an EOF record, then make it so. */
 	if (type == 1) {
 	    if (verbose >= 2)
-		fprintf (stderr, "EOF on hexfile\n");
+		logerror("EOF on hexfile\n");
 	    break;
 	}
 
 	if (type != 0) {
-	    fprintf (stderr, "unsupported record type: %u\n", type);
+	    logerror("unsupported record type: %u\n", type);
 	    return -3;
 	}
 
 	if ((len * 2) + 11 >= strlen(buf)) {
-	    fprintf (stderr, "record too short?\n");
+	    logerror("record too short?\n");
 	    return -4;
 	}
 
 	// FIXME check for _physically_ contiguous not just virtually
 	// e.g. on FX2 0x1f00-0x2100 includes both on-chip and external
@@ -442,35 +441,35 @@
     unsigned		retry = 0;
 
     switch (ctx->mode) {
     case internal_only:		/* CPU should be stopped */
 	if (external) {
-	    fprintf (stderr, "can't write %d bytes external memory at 0x%04x\n",
+	    logerror("can't write %d bytes external memory at 0x%04x\n",
 		len, addr);
 	    return -EINVAL;
 	}
 	break;
     case skip_internal:		/* CPU must be running */
 	if (!external) {
 	    if (verbose >= 2) {
-		fprintf (stderr, "SKIP on-chip RAM, %d bytes at 0x%04x\n",
+		logerror("SKIP on-chip RAM, %d bytes at 0x%04x\n",
 		    len, addr);
 	    }
 	    return 0;
 	}
 	break;
     case skip_external:		/* CPU should be stopped */
 	if (external) {
 	    if (verbose >= 2) {
-		fprintf (stderr, "SKIP external RAM, %d bytes at 0x%04x\n",
+		logerror("SKIP external RAM, %d bytes at 0x%04x\n",
 		    len, addr);
 	    }
 	    return 0;
 	}
 	break;
     default:
-	fprintf (stderr, "bug\n");
+	logerror("bug\n");
 	return -EDOM;
     }
 
     ctx->total += len;
     ctx->count++;
@@ -512,14 +511,14 @@
     struct ram_poke_context	ctx;
     int				status;
 
     image = fopen (path, "r");
     if (image == 0) {
-	fprintf (stderr, "%s: unable to open for input.\n", path);
+	logerror("%s: unable to open for input.\n", path);
 	return -2;
     } else if (verbose)
-	fprintf (stderr, "open RAM hexfile image %s\n", path);
+	logerror("open RAM hexfile image %s\n", path);
 
     /* EZ-USB original/FX and FX2 devices differ, apart from the 8051 core */
     if (fx2) {
 	cpucs_addr = 0xe600;
 	is_external = fx2_is_external;
@@ -540,19 +539,19 @@
     } else {
 	ctx.mode = skip_internal;
 
 	/* let CPU run; overwrite the 2nd stage loader later */
 	if (verbose)
-	    fprintf (stderr, "2nd stage:  write external memory\n");
+	    logerror("2nd stage:  write external memory\n");
     }
     
     /* scan the image, first (maybe only) time */
     ctx.device = fd;
     ctx.total = ctx.count = 0;
     status = parse_ihex (image, &ctx, is_external, ram_poke);
     if (status < 0) {
-	fprintf (stderr, "unable to download %s\n", path);
+	logerror("unable to download %s\n", path);
 	return status;
     }
 
     /* second part of 2nd stage: rescan */
     if (stage) {
@@ -563,20 +562,20 @@
 	    return -1;
 
 	/* at least write the interrupt vectors (at 0x0000) for reset! */
 	rewind (image);
 	if (verbose)
-	    fprintf (stderr, "2nd stage:  write on-chip memory\n");
+	    logerror("2nd stage:  write on-chip memory\n");
 	status = parse_ihex (image, &ctx, is_external, ram_poke);
 	if (status < 0) {
-	    fprintf (stderr, "unable to completely download %s\n", path);
+	    logerror("unable to completely download %s\n", path);
 	    return status;
 	}
     }
 
     if (verbose)
-	fprintf (stderr, "... WROTE: %d bytes, %d segments, avg %d\n",
+	logerror("... WROTE: %d bytes, %d segments, avg %d\n",
 	    ctx.total, ctx.count, ctx.total / ctx.count);
 	
     /* now reset the CPU so it runs what we just downloaded */
     if (!ezusb_cpucs (fd, cpucs_addr, 1))
 	return -1;
@@ -605,18 +604,18 @@
     struct eeprom_poke_context	*ctx = context;
     int			rc;
     unsigned char	header [4];
 
     if (external) {
-	fprintf (stderr,
+      logerror(
 	    "EEPROM can't init %d bytes external memory at 0x%04x\n",
 	    len, addr);
 	return -EINVAL;
     }
 
     if (len > 1023) {
-	fprintf (stderr, "not fragmenting %d bytes\n", len);
+	logerror("not fragmenting %d bytes\n", len);
 	return -EDOM;
     }
 
     /* NOTE:  No retries here.  They don't seem to be needed;
      * could be added if that changes.
@@ -668,26 +667,26 @@
 	return -1;
     }
 
     image = fopen (path, "r");
     if (image == 0) {
-	fprintf (stderr, "%s: unable to open for input.\n", path);
+	logerror("%s: unable to open for input.\n", path);
 	return -2;
     } else if (verbose)
-	fprintf (stderr, "open EEPROM hexfile image %s\n", path);
+	logerror("open EEPROM hexfile image %s\n", path);
 
     if (verbose)
-	fprintf (stderr, "2nd stage:  write boot EEPROM\n");
+	logerror("2nd stage:  write boot EEPROM\n");
 
     /* EZ-USB family devices differ, apart from the 8051 core */
     if (strcmp ("fx2", type) == 0) {
 	first_byte = 0xC2;
 	cpucs_addr = 0xe600;
 	is_external = fx2_is_external;
 	ctx.ee_addr = 8;
 	config &= 0x4f;
-	fprintf (stderr,
+	logerror(
 	    "FX2:  config = 0x%02x, %sconnected, I2C = %d KHz\n",
 	    config,
 	    (config & 0x40) ? "dis" : "",
 		// NOTE:  old chiprevs let CPU clock speed be set
 		// or cycle inverted here.  You shouldn't use those.
@@ -699,11 +698,11 @@
 	first_byte = 0xB6;
 	cpucs_addr = 0x7f92;
 	is_external = fx_is_external;
 	ctx.ee_addr = 9;
 	config &= 0x07;
-	fprintf (stderr,
+	logerror(
 	    "FX:  config = 0x%02x, %d MHz%s, I2C = %d KHz\n",
 	    config,
 	    ((config & 0x04) ? 48 : 24),
 	    (config & 0x02) ? " inverted" : "",
 	    (config & 0x01) ? 400 : 100
@@ -713,14 +712,14 @@
 	first_byte = 0xB2;
 	cpucs_addr = 0x7f92;
 	is_external = fx_is_external;
 	ctx.ee_addr = 7;
 	config = 0;
-	fprintf (stderr, "AN21xx:  no EEPROM config byte\n");
+	logerror("AN21xx:  no EEPROM config byte\n");
 
     } else {
-	fprintf (stderr, "?? Unrecognized microcontroller type %s ??\n", type);
+	logerror("?? Unrecognized microcontroller type %s ??\n", type);
 	return -1;
     }
 
     /* make sure the EEPROM won't be used for booting,
      * in case of problems writing it
@@ -734,20 +733,20 @@
     /* scan the image, write to EEPROM */
     ctx.device = dev;
     ctx.last = 0;
     status = parse_ihex (image, &ctx, is_external, eeprom_poke);
     if (status < 0) {
-	fprintf (stderr, "unable to write EEPROM %s\n", path);
+	logerror("unable to write EEPROM %s\n", path);
 	return status;
     }
 
     /* append a reset command */
     value = 0;
     ctx.last = 1;
     status = eeprom_poke (&ctx, cpucs_addr, 0, &value, sizeof value);
     if (status < 0) {
-	fprintf (stderr, "unable to append reset to EEPROM %s\n", path);
+	logerror("unable to append reset to EEPROM %s\n", path);
 	return status;
     }
 
     /* write the config byte for FX, FX2 */
     if (strcmp ("an21", type) != 0) {
fxload.8 +4 -0
diff -u -U 5 -r --new-file -x *~ -X /tmp/diffexclude fxload-2002_04_11/fxload.8 fxload-2002_04_11.doj/fxload.8
@@ -26,10 +26,11 @@
 .SH "NAME"
 fxload \- Firmware download to EZ-USB devices
 .SH "SYNOPSIS"
 .B fxload
 .BI "[ \-v ]"
+.BI "[ \-l ]"
 .BI "[ \-D " devpath " ]"
 .BI "[ \-I " hexfile " ]"
 .BI "[ \-t " type " ]"
 .BI "[ \-c " config " ]"
 .BI "[ \-s " loader " ]"
@@ -170,10 +171,13 @@
 .B "\-v"
 Prints some diagnostics, such as download addresses and sizes,
 to standard error.  Repeat the flag
 .RB ( -vv ", " -vvv )
 to get more diagnostics.
+.TP
+.B "\-l"
+print error and verbose messages to syslog.
 .TP
 .BI "\-D " devpath
 Specifies the "usbfs" path name for the device in question,
 such as
 .IR /proc/bus/usb/004/080 .
main.c +35 -23

187 lines not on this page · raw message