From e9e0469be8fc2fbeb1123ba33ba5b341dc695719 Mon Sep 17 00:00:00 2001 From: Rutger Hofman Date: Thu, 7 May 2009 22:27:21 +0000 Subject: [PATCH] Convert src/tap/*.c and src/tap/parport, src/tap/usbconn/ to use urj_log() and urj_error() i.s.o. printf. Introduce urj_warning that prints __FILE__ __func__ __LINE__ git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1579 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/configure.ac | 2 +- urjtag/include/urjtag/bus_driver.h | 1 + urjtag/include/urjtag/cable.h | 21 ++ urjtag/include/urjtag/chain.h | 30 ++- urjtag/include/urjtag/error.h | 23 +- urjtag/include/urjtag/flash.h | 58 ++---- urjtag/include/urjtag/jtag.h | 2 +- urjtag/include/urjtag/log.h | 18 +- urjtag/include/urjtag/parport.h | 6 + urjtag/include/urjtag/tap.h | 14 +- urjtag/include/urjtag/tap_register.h | 1 + urjtag/include/urjtag/usbconn.h | 4 + urjtag/src/apps/jtag/jtag.c | 8 +- urjtag/src/bsdl/bsdl.c | 7 +- urjtag/src/bsdl/bsdl_flex.l | 15 ++ urjtag/src/bsdl/vhdl_flex.l | 15 ++ urjtag/src/cmd/cmd_addpart.c | 2 + urjtag/src/cmd/cmd_detectflash.c | 8 +- urjtag/src/cmd/cmd_eraseflash.c | 7 +- urjtag/src/cmd/cmd_flashmem.c | 17 +- urjtag/src/cmd/cmd_idcode.c | 1 + urjtag/src/cmd/cmd_scan.c | 2 + urjtag/src/cmd/cmd_shift.c | 2 + urjtag/src/flash/amd.c | 187 +++++++++-------- urjtag/src/flash/amd.h | 48 +++++ urjtag/src/flash/amd_flash.c | 126 ++++++----- urjtag/src/flash/cfi.c | 110 +++++++--- urjtag/src/flash/cfi.h | 8 + urjtag/src/flash/detectflash.c | 301 ++++++++++++++------------- urjtag/src/flash/flash.c | 257 +++++++++++++---------- urjtag/src/flash/flash.h | 60 ++++++ urjtag/src/flash/intel.c | 133 ++++++------ urjtag/src/flash/intel.h | 5 + urjtag/src/flash/jedec.c | 70 +++++-- urjtag/src/flash/jedec.h | 4 +- urjtag/src/flash/jedec_exp.c | 70 ++++--- urjtag/src/global/parse.c | 5 +- urjtag/src/global/urjtag.c | 28 ++- urjtag/src/part/data_register.c | 3 +- urjtag/src/part/instruction.c | 3 +- urjtag/src/svf/svf.c | 47 ++--- urjtag/src/svf/svf_bison.y | 10 +- urjtag/src/svf/svf_flex.l | 17 +- urjtag/src/tap/cable.c | 135 ++++++------ urjtag/src/tap/chain.c | 70 +++++-- urjtag/src/tap/detect.c | 97 +++++---- urjtag/src/tap/discovery.c | 29 +-- urjtag/src/tap/idcode.c | 16 +- urjtag/src/tap/parport.c | 1 + urjtag/src/tap/parport/direct.c | 76 ++++--- urjtag/src/tap/parport/ppdev.c | 76 +++++-- urjtag/src/tap/parport/ppi.c | 54 ++++- urjtag/src/tap/register.c | 16 ++ urjtag/src/tap/tap.c | 20 +- urjtag/src/tap/usbconn.c | 1 + urjtag/src/tap/usbconn/libftd2xx.c | 148 ++++++------- urjtag/src/tap/usbconn/libftdi.c | 149 +++++++------ urjtag/src/tap/usbconn/libusb.c | 35 +++- 58 files changed, 1641 insertions(+), 1038 deletions(-) create mode 100644 urjtag/src/flash/amd.h create mode 100644 urjtag/src/flash/flash.h diff --git a/urjtag/configure.ac b/urjtag/configure.ac index 770bc617..2e402711 100644 --- a/urjtag/configure.ac +++ b/urjtag/configure.ac @@ -266,7 +266,7 @@ AS_IF([test "x$with_inpout32" = xyes], [ ]) -CFLAGS="$CFLAGS -Wall -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith" +CFLAGS="$CFLAGS -Werror -Wall -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith" CPPFLAGS="-I\$(top_srcdir) -I\$(top_srcdir)/include $CPPFLAGS" diff --git a/urjtag/include/urjtag/bus_driver.h b/urjtag/include/urjtag/bus_driver.h index dd91fdd6..242ef58e 100644 --- a/urjtag/include/urjtag/bus_driver.h +++ b/urjtag/include/urjtag/bus_driver.h @@ -57,6 +57,7 @@ struct urj_bus_driver void (*free_bus) (urj_bus_t *bus); void (*printinfo) (urj_bus_t *bus); void (*prepare) (urj_bus_t *bus); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*area) (urj_bus_t *bus, uint32_t adr, urj_bus_area_t *area); void (*read_start) (urj_bus_t *bus, uint32_t adr); uint32_t (*read_next) (urj_bus_t *bus, uint32_t adr); diff --git a/urjtag/include/urjtag/cable.h b/urjtag/include/urjtag/cable.h index e6f11f5c..d3344015 100644 --- a/urjtag/include/urjtag/cable.h +++ b/urjtag/include/urjtag/cable.h @@ -46,16 +46,22 @@ struct urj_cable_driver { const char *name; const char *description; + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int (*connect) (char *params[], urj_cable_t *cable); void (*disconnect) (urj_cable_t *cable); void (*cable_free) (urj_cable_t *cable); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int (*init) (urj_cable_t *); void (*done) (urj_cable_t *); void (*set_frequency) (urj_cable_t *, uint32_t freq); void (*clock) (urj_cable_t *, int, int, int); + /** @return 0 or 1 on success; -1 on failure */ int (*get_tdo) (urj_cable_t *); + /** @return the number of transferred bits on success; -1 on failure */ int (*transfer) (urj_cable_t *, int, char *, char *); + /** @return 0 or 1 on success; -1 on failure */ int (*set_signal) (urj_cable_t *, int, int); + /** @return 0 or 1 on success; -1 on failure */ int (*get_signal) (urj_cable_t *, urj_pod_sigsel_t); void (*flush) (urj_cable_t *, urj_cable_flush_amount_t); void (*help) (const char *); @@ -130,22 +136,35 @@ struct urj_cable }; void urj_tap_cable_free (urj_cable_t *cable); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int urj_tap_cable_init (urj_cable_t *cable); void urj_tap_cable_done (urj_cable_t *cable); void urj_tap_cable_flush (urj_cable_t *cable, urj_cable_flush_amount_t); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ void urj_tap_cable_clock (urj_cable_t *cable, int tms, int tdi, int n); int urj_tap_cable_defer_clock (urj_cable_t *cable, int tms, int tdi, int n); +/** @return 0 or 1 on success; -1 on failure */ int urj_tap_cable_get_tdo (urj_cable_t *cable); +/** @return 0 or 1 on success; -1 on failure */ int urj_tap_cable_get_tdo_late (urj_cable_t *cable); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int urj_tap_cable_defer_get_tdo (urj_cable_t *cable); +/** @return @see (*set_signal)() */ int urj_tap_cable_set_signal (urj_cable_t *cable, int mask, int val); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int urj_tap_cable_defer_set_signal (urj_cable_t *cable, int mask, int val); +/** @return @see (*get_signal)() */ int urj_tap_cable_get_signal (urj_cable_t *cable, urj_pod_sigsel_t sig); +/** @return @see (*get_signal)() */ int urj_tap_cable_get_signal_late (urj_cable_t *cable, urj_pod_sigsel_t sig); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int urj_tap_cable_defer_get_signal (urj_cable_t *cable, urj_pod_sigsel_t sig); +/** @return the number of transferred bits on success; -1 on failure */ int urj_tap_cable_transfer (urj_cable_t *cable, int len, char *in, char *out); +/** @return the number of transferred bits on success; -1 on failure */ int urj_tap_cable_transfer_late (urj_cable_t *cable, char *out); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on failure */ int urj_tap_cable_defer_transfer (urj_cable_t *cable, int len, char *in, char *out); @@ -153,8 +172,10 @@ void urj_tap_cable_set_frequency (urj_cable_t *cable, uint32_t frequency); uint32_t urj_tap_cable_get_frequency (urj_cable_t *cable); void urj_tap_cable_wait (urj_cable_t *cable); void urj_tap_cable_purge_queue (urj_cable_queue_info_t *q, int io); +/** @return queue item number on success; -1 on failure */ int urj_tap_cable_add_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q); +/** @return queue item number on success; -1 on failure */ int urj_tap_cable_get_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q); diff --git a/urjtag/include/urjtag/chain.h b/urjtag/include/urjtag/chain.h index bac6158e..d7658454 100644 --- a/urjtag/include/urjtag/chain.h +++ b/urjtag/include/urjtag/chain.h @@ -49,19 +49,27 @@ struct urj_chain urj_chain_t *urj_tap_chain_alloc (void); void urj_tap_chain_free (urj_chain_t *chain); void urj_tap_chain_disconnect (urj_chain_t *chain); -void urj_tap_chain_clock (urj_chain_t *chain, int tms, int tdi, int n); -void urj_tap_chain_defer_clock (urj_chain_t *chain, int tms, int tdi, int n); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_chain_clock (urj_chain_t *chain, int tms, int tdi, int n); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_chain_defer_clock (urj_chain_t *chain, int tms, int tdi, int n); +/** @return trst = 0 or 1 on success; -1 on error */ int urj_tap_chain_set_trst (urj_chain_t *chain, int trst); +/** @return 0 or 1 on success; -1 on error */ int urj_tap_chain_get_trst (urj_chain_t *chain); -void urj_tap_chain_shift_instructions (urj_chain_t *chain); -void urj_tap_chain_shift_instructions_mode (urj_chain_t *chain, - int capture_output, int capture, - int chain_exit); -void urj_tap_chain_shift_data_registers (urj_chain_t *chain, - int capture_output); -void urj_tap_chain_shift_data_registers_mode (urj_chain_t *chain, - int capture_output, int capture, - int chain_exit); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_chain_shift_instructions (urj_chain_t *chain); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_chain_shift_instructions_mode (urj_chain_t *chain, + int capture_output, int capture, + int chain_exit); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_chain_shift_data_registers (urj_chain_t *chain, + int capture_output); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_chain_shift_data_registers_mode (urj_chain_t *chain, + int capture_output, int capture, + int chain_exit); void urj_tap_chain_flush (urj_chain_t *chain); int urj_tap_chain_set_pod_signal (urj_chain_t *chain, int mask, int val); int urj_tap_chain_get_pod_signal (urj_chain_t *chain, urj_pod_sigsel_t sig); diff --git a/urjtag/include/urjtag/error.h b/urjtag/include/urjtag/error.h index bb7b7c5a..2bfd4a68 100644 --- a/urjtag/include/urjtag/error.h +++ b/urjtag/include/urjtag/error.h @@ -36,13 +36,24 @@ typedef enum urj_error { URJ_ERROR_OUT_OF_MEMORY, URJ_ERROR_NO_CHAIN, URJ_ERROR_NO_ACTIVE_PART, + URJ_ERROR_NO_ACTIVE_INSTRUCTION, + URJ_ERROR_NO_DATA_REGISTER, URJ_ERROR_INVALID, URJ_ERROR_NOTFOUND, - URJ_ERROR_IO, /**< I/O error from OS */ URJ_ERROR_NO_BUS_DRIVER, URJ_ERROR_BUFFER_EXHAUSTED, URJ_ERROR_ILLEGAL_STATE, URJ_ERROR_OUT_OF_BOUNDS, + URJ_ERROR_UNSUPPORTED, + URJ_ERROR_SYNTAX, + + URJ_ERROR_IO, /**< I/O error from OS */ + + URJ_ERROR_FLASH, + URJ_ERROR_FLASH_DETECT, + URJ_ERROR_FLASH_PROGRAM, + URJ_ERROR_FLASH_ERASE, + URJ_ERROR_FLASH_UNLOCK, } urj_error_t; /** Max length of message string that can be recorded. */ @@ -67,7 +78,7 @@ extern urj_error_state_t urj_error_state; extern const char *urj_error_string(urj_error_t error); /** - * Set error state. If the logging level is not SILENT, also logs the error. + * Set error state. * * @param e urj_error_t value * @param ... consists of a printf argument set. It needs to start with a @@ -81,12 +92,12 @@ extern const char *urj_error_string(urj_error_t error); urj_error_state.line = __LINE__; \ snprintf (urj_error_state.msg, sizeof urj_error_state.msg, \ __VA_ARGS__); \ - if (urj_log_state.level < URJ_LOG_LEVEL_SILENT) \ + if (0 && urj_log_state.level < URJ_LOG_LEVEL_SILENT) \ { \ - urj_log(URJ_LOG_LEVEL_ERRORS, "%s:%d %s() %s: ", __FILE__, \ + urj_log(URJ_LOG_LEVEL_ERROR, "%s:%d %s() %s: ", __FILE__, \ __LINE__, __func__, urj_error_string(e)); \ - urj_log(URJ_LOG_LEVEL_ERRORS, __VA_ARGS__); \ - urj_log(URJ_LOG_LEVEL_ERRORS, "\n"); \ + urj_log(URJ_LOG_LEVEL_ERROR, __VA_ARGS__); \ + urj_log(URJ_LOG_LEVEL_ERROR, "\n"); \ } \ } while (0) diff --git a/urjtag/include/urjtag/flash.h b/urjtag/include/urjtag/flash.h index 72da4fb8..663727a1 100644 --- a/urjtag/include/urjtag/flash.h +++ b/urjtag/include/urjtag/flash.h @@ -38,56 +38,42 @@ #include #include "types.h" +#include "log.h" -typedef struct urj_flash_cfi_chip urj_flash_cfi_chip_t; - -typedef struct -{ - urj_bus_t *bus; - uint32_t address; - int bus_width; /* in cfi_chips, e.g. 4 for 32 bits */ - urj_flash_cfi_chip_t **cfi_chips; -} urj_flash_cfi_array_t; - -void urj_flash_cfi_array_free (urj_flash_cfi_array_t *urj_flash_cfi_array); -int urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, - urj_flash_cfi_array_t **urj_flash_cfi_array); - -/* End of brux/cfi.h */ +typedef struct urj_flash_cfi_array urj_flash_cfi_array_t; typedef struct { unsigned int bus_width; /* 1 for 8 bits, 2 for 16 bits, 4 for 32 bits, etc. */ const char *name; const char *description; - int (*autodetect) (urj_flash_cfi_array_t *urj_flash_cfi_array); - void (*print_info) (urj_flash_cfi_array_t *urj_flash_cfi_array); - int (*erase_block) (urj_flash_cfi_array_t *urj_flash_cfi_array, - uint32_t adr); - int (*unlock_block) (urj_flash_cfi_array_t *urj_flash_cfi_array, - uint32_t adr); - int (*program) (urj_flash_cfi_array_t *urj_flash_cfi_array, uint32_t adr, + /** @return 1 if autodetected, 0 otherwise */ + int (*autodetect) (urj_flash_cfi_array_t *cfi_array); + void (*print_info) (urj_log_level_t ll, urj_flash_cfi_array_t *cfi_array); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ + int (*erase_block) (urj_flash_cfi_array_t *cfi_array, uint32_t adr); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ + int (*unlock_block) (urj_flash_cfi_array_t *cfi_array, uint32_t adr); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ + int (*program) (urj_flash_cfi_array_t *cfi_array, uint32_t adr, uint32_t *buffer, int count); - void (*readarray) (urj_flash_cfi_array_t *urj_flash_cfi_array); + void (*readarray) (urj_flash_cfi_array_t *cfi_array); } urj_flash_driver_t; -#define URJ_FLASH_ERROR_NOERROR 0 -#define URJ_FLASH_ERROR_INVALID_COMMAND_SEQUENCE 1 -#define URJ_FLASH_ERROR_LOW_VPEN 2 -#define URJ_FLASH_ERROR_BLOCK_LOCKED 3 -#define URJ_FLASH_ERROR_UNKNOWN 99 - -extern urj_flash_cfi_array_t *urj_flash_cfi_array; +extern urj_flash_driver_t *urj_flash_flash_drivers[]; -void urj_flash_detectflash (urj_bus_t *bus, uint32_t adr); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_flash_detectflash (urj_log_level_t ll, urj_bus_t *bus, uint32_t adr); +void urj_flash_cleanup (void); -void urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int); -void urj_flashmsbin (urj_bus_t *bus, FILE *f, int); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_flashmsbin (urj_bus_t *bus, FILE *f, int); -void urj_flasherase (urj_bus_t *bus, uint32_t addr, int number); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_flasherase (urj_bus_t *bus, uint32_t addr, int number); /* end of original brux/flash.h */ -extern urj_flash_driver_t *urj_flash_flash_drivers[]; - #endif /* URJ_FLASH_H */ diff --git a/urjtag/include/urjtag/jtag.h b/urjtag/include/urjtag/jtag.h index 71264d7d..d1c98850 100644 --- a/urjtag/include/urjtag/jtag.h +++ b/urjtag/include/urjtag/jtag.h @@ -30,7 +30,7 @@ extern int urj_big_endian; extern int urj_debug_mode; -/** Register the application name with cmd_jtag. +/** Register the application name with global/data_dir. * @param argv0 is remembered as a pointer, it is not strdup()'ed. */ void urj_set_argv0(const char *argv0); diff --git a/urjtag/include/urjtag/log.h b/urjtag/include/urjtag/log.h index e31b078c..92c6d5bb 100644 --- a/urjtag/include/urjtag/log.h +++ b/urjtag/include/urjtag/log.h @@ -36,8 +36,8 @@ typedef enum urj_log_level { URJ_LOG_LEVEL_DEBUG, /**< more details of interest for developers */ URJ_LOG_LEVEL_DETAIL, /**< verbose output */ URJ_LOG_LEVEL_NORMAL, /**< just noteworthy info */ - URJ_LOG_LEVEL_WARNINGS, /**< unmissable warnings */ - URJ_LOG_LEVEL_ERRORS, /**< only fatal errors */ + URJ_LOG_LEVEL_WARNING, /**< unmissable warnings */ + URJ_LOG_LEVEL_ERROR, /**< only fatal errors */ URJ_LOG_LEVEL_SILENT, /**< suppress logging output */ } urj_log_level_t; @@ -58,4 +58,18 @@ int urj_log (urj_log_level_t level, const char *fmt, ...) #endif ; +/** + * Print warning unless logging level is > URJ_LOG_LEVEL_WARNING + * + * @param e urj_error_t value + * @param ... consists of a printf argument set. It needs to start with a + * const char *fmt, followed by arguments used by fmt. + */ +#define urj_warning(...) \ + do { \ + urj_log (URJ_LOG_LEVEL_WARNING, "%s:%d %s() Warning: ", \ + __FILE__, __LINE__, __func__); \ + urj_log (URJ_LOG_LEVEL_WARNING, __VA_ARGS__); \ + } while (0) + #endif /* URJ_LOG_H */ diff --git a/urjtag/include/urjtag/parport.h b/urjtag/include/urjtag/parport.h index 244d0b6d..c9cd05ad 100644 --- a/urjtag/include/urjtag/parport.h +++ b/urjtag/include/urjtag/parport.h @@ -35,11 +35,17 @@ typedef struct const char *type; urj_parport_t *(*connect) (const char **, int); void (*parport_free) (urj_parport_t *); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*open) (urj_parport_t *); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*close) (urj_parport_t *); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*set_data) (urj_parport_t *, uint8_t); + /** @return data on success; -1 on error */ int (*get_data) (urj_parport_t *); + /** @return status on success; -1 on error */ int (*get_status) (urj_parport_t *); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*set_control) (urj_parport_t *, uint8_t); } urj_parport_driver_t; diff --git a/urjtag/include/urjtag/tap.h b/urjtag/include/urjtag/tap.h index 35400a34..0957e1d2 100644 --- a/urjtag/include/urjtag/tap.h +++ b/urjtag/include/urjtag/tap.h @@ -28,7 +28,8 @@ #include "types.h" void urj_tap_reset (urj_chain_t *chain); -void urj_tap_reset_bypass (urj_chain_t *chain); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_reset_bypass (urj_chain_t *chain); void urj_tap_capture_dr (urj_chain_t *chain); void urj_tap_capture_ir (urj_chain_t *chain); void urj_tap_defer_shift_register (urj_chain_t *chain, @@ -42,16 +43,21 @@ void urj_tap_shift_register (urj_chain_t *chain, urj_tap_register_t *out, int tap_exit); /** API functions */ +/** @return number of detected parts on success; -1 on error */ int urj_tap_detect_parts (urj_chain_t *chain, const char *db_path); +/** @return chain length on success; -1 on error */ int urj_tap_manual_add (urj_chain_t *chain, int instr_len); +/** @return register size on success; -1 on error */ int urj_tap_detect_register_size (urj_chain_t *chain); -void urj_tap_discovery (urj_chain_t *chain); -void urj_tap_idcode (urj_chain_t *chain, unsigned int bytes); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_discovery (urj_chain_t *chain); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_tap_idcode (urj_chain_t *chain, unsigned int bytes); /** * Convenience function that detects the parts, initialises them to BYPASS, * and initialises the bus drivers. * - * @return URJ_STATUS_OK on success; URJ_STATUS_FAILURE on error + * @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int urj_tap_detect (urj_chain_t *chain); diff --git a/urjtag/include/urjtag/tap_register.h b/urjtag/include/urjtag/tap_register.h index e6056c86..cf11f46f 100644 --- a/urjtag/include/urjtag/tap_register.h +++ b/urjtag/include/urjtag/tap_register.h @@ -39,6 +39,7 @@ urj_tap_register_t *urj_tap_register_duplicate (const urj_tap_register_t *tr); void urj_tap_register_free (urj_tap_register_t *tr); urj_tap_register_t *urj_tap_register_fill (urj_tap_register_t *tr, int val); const char *urj_tap_register_get_string (const urj_tap_register_t *tr); +/** @return 0 or 1 on success; -1 on error */ int urj_tap_register_all_bits_same_value (const urj_tap_register_t *tr); urj_tap_register_t *urj_tap_register_init (urj_tap_register_t *tr, const char *value); diff --git a/urjtag/include/urjtag/usbconn.h b/urjtag/include/urjtag/usbconn.h index 52698ccd..96f36b25 100644 --- a/urjtag/include/urjtag/usbconn.h +++ b/urjtag/include/urjtag/usbconn.h @@ -44,9 +44,13 @@ typedef struct const char *type; urj_usbconn_t *(*connect) (const char **, int, urj_usbconn_cable_t *); void (*free) (urj_usbconn_t *); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*open) (urj_usbconn_t *); + /** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ int (*close) (urj_usbconn_t *); + /** @return bytes read on success; -1 on error */ int (*read) (urj_usbconn_t *, uint8_t *, int); + /** @return bytes written on success; -1 on error */ int (*write) (urj_usbconn_t *, uint8_t *, int, int); } urj_usbconn_driver_t; diff --git a/urjtag/src/apps/jtag/jtag.c b/urjtag/src/apps/jtag/jtag.c index 67fce4a1..7d78ad9c 100644 --- a/urjtag/src/apps/jtag/jtag.c +++ b/urjtag/src/apps/jtag/jtag.c @@ -284,8 +284,7 @@ jtag_parse_rc (urj_chain_t *chain) static void cleanup (urj_chain_t *chain) { - urj_flash_cfi_array_free (urj_flash_cfi_array); - urj_flash_cfi_array = NULL; + urj_flash_cleanup (); if (urj_bus) { @@ -477,8 +476,9 @@ main (int argc, char *const argv[]) if (!quiet) { - printf (_("WARNING: %s may damage your hardware!\n"), PACKAGE_NAME); - printf (_("Type \"quit\" to exit, \"help\" for help.\n\n")); + urj_warning (_("%s may damage your hardware!\n"), PACKAGE_NAME); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Type \"quit\" to exit, \"help\" for help.\n\n")); } /* Create ~/.jtag */ diff --git a/urjtag/src/bsdl/bsdl.c b/urjtag/src/bsdl/bsdl.c index 8973b505..4e5ad794 100644 --- a/urjtag/src/bsdl/bsdl.c +++ b/urjtag/src/bsdl/bsdl.c @@ -326,7 +326,7 @@ urj_bsdl_scan_files (urj_chain_t *chain, const char *idcode, int proc_mode) { char *name; - /* @@@@ ToDo handle malloc error result RFHH */ + /* @@@@ RFHH ToDo handle malloc error result */ name = malloc (strlen (globs->path_list[idx]) + strlen (elem->d_name) + 1 + 1); if (name) @@ -341,9 +341,8 @@ urj_bsdl_scan_files (urj_chain_t *chain, const char *idcode, int proc_mode) { if (buf.st_mode & S_IFREG) { - result = - urj_bsdl_read_file (chain, name, proc_mode, - idcode); + result = urj_bsdl_read_file (chain, name, proc_mode, + idcode); if (result == 1) printf (_(" Filename: %s\n"), name); } diff --git a/urjtag/src/bsdl/bsdl_flex.l b/urjtag/src/bsdl/bsdl_flex.l index 30b3431c..a8d6d2d4 100644 --- a/urjtag/src/bsdl/bsdl_flex.l +++ b/urjtag/src/bsdl/bsdl_flex.l @@ -140,6 +140,21 @@ LEGAL NOTICES: #define YY_EXTRA_TYPE urj_bsdl_scan_extra_t * +// @@@@ RFHH: take these out after the refactor +// Prototypes that flex should have added, functions that flex should have +// called or not declared: +int urj_bsdl_get_column (yyscan_t yyscanner); +void urj_bsdl_set_column (int column_no , yyscan_t yyscanner); + +static void yyunput (int c, register char * yy_bp , yyscan_t yyscanner); + +void urj_bsdl_use_yyunput (yyscan_t yyscanner); +void urj_bsdl_use_yyunput (yyscan_t yyscanner) +{ + yyunput (0, NULL, yyscanner); +} +// @@@@ RFHH: take these out after the refactor + static char *new_string (urj_bsdl_scan_extra_t *, const char *); #define BINARY 0 diff --git a/urjtag/src/bsdl/vhdl_flex.l b/urjtag/src/bsdl/vhdl_flex.l index fc2c3733..e8bc76b9 100644 --- a/urjtag/src/bsdl/vhdl_flex.l +++ b/urjtag/src/bsdl/vhdl_flex.l @@ -142,6 +142,21 @@ LEGAL NOTICES: #define YY_EXTRA_TYPE urj_bsdl_scan_extra_t * +// @@@@ RFHH: take these out after the refactor +// Prototypes that flex should have added, functions that flex should have +// called or not declared: +int urj_vhdl_get_column (yyscan_t yyscanner); +void urj_vhdl_set_column (int column_no , yyscan_t yyscanner); + +static void yyunput (int c, register char * yy_bp , yyscan_t yyscanner); + +void urj_vhdl_use_yyunput (yyscan_t yyscanner); +void urj_vhdl_use_yyunput (yyscan_t yyscanner) +{ + yyunput (0, NULL, yyscanner); +} +// @@@@ RFHH: take these out after the refactor + static char *new_string (urj_bsdl_scan_extra_t *, const char *); #define BINARY 0 diff --git a/urjtag/src/cmd/cmd_addpart.c b/urjtag/src/cmd/cmd_addpart.c index 6f0af9c2..d38d94cb 100644 --- a/urjtag/src/cmd/cmd_addpart.c +++ b/urjtag/src/cmd/cmd_addpart.c @@ -47,6 +47,7 @@ cmd_addpart_run (urj_chain_t *chain, char *params[]) if (!urj_cmd_test_cable (chain)) return 1; + // @@@@ RFHH check result urj_tap_manual_add (chain, len); if (chain->parts == NULL) @@ -59,6 +60,7 @@ cmd_addpart_run (urj_chain_t *chain, char *params[]) } urj_part_parts_set_instruction (chain->parts, "BYPASS"); + /* @@@@ RFHH check result */ urj_tap_chain_shift_instructions (chain); return 1; diff --git a/urjtag/src/cmd/cmd_detectflash.c b/urjtag/src/cmd/cmd_detectflash.c index 03a0fa14..770c735a 100644 --- a/urjtag/src/cmd/cmd_detectflash.c +++ b/urjtag/src/cmd/cmd_detectflash.c @@ -26,6 +26,8 @@ #include +#include +#include #include #include #include @@ -49,7 +51,11 @@ cmd_detectflash_run (urj_chain_t *chain, char *params[]) if (urj_cmd_get_number (params[1], &adr)) return -1; - urj_flash_detectflash (urj_bus, adr); + if (urj_flash_detectflash (URJ_LOG_LEVEL_NORMAL, urj_bus, adr) != URJ_STATUS_OK) + { + printf("detect flash error: %s\n", urj_error_describe()); + urj_error_reset(); + } return 1; } diff --git a/urjtag/src/cmd/cmd_eraseflash.c b/urjtag/src/cmd/cmd_eraseflash.c index 1025271f..62e54b3e 100644 --- a/urjtag/src/cmd/cmd_eraseflash.c +++ b/urjtag/src/cmd/cmd_eraseflash.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -54,7 +55,11 @@ cmd_eraseflash_run (urj_chain_t *chain, char *params[]) return -1; if (urj_cmd_get_number (params[2], &number)) return -1; - urj_flasherase (urj_bus, adr, number); + if (urj_flasherase (urj_bus, adr, number) != URJ_STATUS_OK) + { + printf ("error: %s\n", urj_error_describe()); + urj_error_reset(); + } return 1; } diff --git a/urjtag/src/cmd/cmd_flashmem.c b/urjtag/src/cmd/cmd_flashmem.c index 628d5c09..5dd48b68 100644 --- a/urjtag/src/cmd/cmd_flashmem.c +++ b/urjtag/src/cmd/cmd_flashmem.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -69,9 +70,21 @@ cmd_flashmem_run (urj_chain_t *chain, char *params[]) return 1; } if (msbin) - urj_flashmsbin (urj_bus, f, noverify); + { + if (urj_flashmsbin (urj_bus, f, noverify) != URJ_STATUS_OK) + { + printf ("error: %s\n", urj_error_describe()); + urj_error_reset(); + } + } else - urj_flashmem (urj_bus, f, adr, noverify); + { + if (urj_flashmem (urj_bus, f, adr, noverify) != URJ_STATUS_OK) + { + printf ("error: %s\n", urj_error_describe()); + urj_error_reset(); + } + } fclose (f); return 1; diff --git a/urjtag/src/cmd/cmd_idcode.c b/urjtag/src/cmd/cmd_idcode.c index 129ada3d..64c17d25 100644 --- a/urjtag/src/cmd/cmd_idcode.c +++ b/urjtag/src/cmd/cmd_idcode.c @@ -51,6 +51,7 @@ cmd_idcode_run (urj_chain_t *chain, char *params[]) return 1; printf (_("Reading %d bytes if idcode\n"), bytes); + // @@@@ RFHH check return value urj_tap_idcode (chain, bytes); return 1; } diff --git a/urjtag/src/cmd/cmd_scan.c b/urjtag/src/cmd/cmd_scan.c index 346aec57..65492697 100644 --- a/urjtag/src/cmd/cmd_scan.c +++ b/urjtag/src/cmd/cmd_scan.c @@ -81,6 +81,7 @@ cmd_scan_run (urj_chain_t *chain, char *params[]) return 1; } + /* @@@@ RFHH check result */ urj_tap_chain_shift_instructions (chain); obsr = urj_tap_register_alloc (bsr->out->len); @@ -96,6 +97,7 @@ cmd_scan_run (urj_chain_t *chain, char *params[]) urj_tap_register_init (obsr, urj_tap_register_get_string (bsr->out)); // copy + /* @@@@ RFHH check result */ urj_tap_chain_shift_data_registers (chain, 1); for (s = part->signals; s; s = s->next) diff --git a/urjtag/src/cmd/cmd_shift.c b/urjtag/src/cmd/cmd_shift.c index 6f0dcffa..2e278347 100644 --- a/urjtag/src/cmd/cmd_shift.c +++ b/urjtag/src/cmd/cmd_shift.c @@ -44,11 +44,13 @@ cmd_shift_run (urj_chain_t *chain, char *params[]) if (strcasecmp (params[1], "ir") == 0) { + /* @@@@ RFHH check result */ urj_tap_chain_shift_instructions (chain); return 1; } if (strcasecmp (params[1], "dr") == 0) { + /* @@@@ RFHH check result */ urj_tap_chain_shift_data_registers (chain, 1); return 1; } diff --git a/urjtag/src/flash/amd.c b/urjtag/src/flash/amd.c index 2841cc36..6704958e 100644 --- a/urjtag/src/flash/amd.c +++ b/urjtag/src/flash/amd.c @@ -40,13 +40,15 @@ #include #include /* usleep */ +#include +#include #include #include -#include "intel.h" +#include "flash.h" #include "cfi.h" +#include "amd.h" -static int dbg = 0; /* The code below assumes a connection of the flash chip address LSB (A0) * to A0, A1 or A2 of the byte-addressed CPU bus dependent on the bus width. @@ -123,8 +125,8 @@ amd_flash_autodetect8 (urj_flash_cfi_array_t *cfi_array) /* * check device status - * 1/true PASS - * 0/false FAIL + * URJ_STATUS_OK PASS + * URJ_STATUS_FAIL FAIL */ /* * first implementation: see [1], page 29 @@ -146,11 +148,10 @@ amdstatus29 (urj_flash_cfi_array_t *cfi_array, uint32_t adr, { data1 = URJ_BUS_READ (bus, adr << o); data1 = URJ_BUS_READ (bus, adr << o); - if (dbg) - printf ("amdstatus %d: %04X (%04X) = %04X\n", timeout, data1, - (data1 & dq7mask), bit7); + urj_log (URJ_LOG_LEVEL_DEBUG, "amdstatus %d: %04X (%04X) = %04X\n", + timeout, data1, (data1 & dq7mask), bit7); if (((data1 & dq7mask) == dq7mask) == bit7) /* FIXME: This looks non-portable */ - return 1; + return URJ_STATUS_OK; if ((data1 & dq5mask) == dq5mask) break; @@ -159,9 +160,10 @@ amdstatus29 (urj_flash_cfi_array_t *cfi_array, uint32_t adr, data1 = URJ_BUS_READ (bus, adr << o); if (((data1 & dq7mask) == dq7mask) == bit7) /* FIXME: This looks non-portable */ - return 1; + return URJ_STATUS_OK; - return 0; + urj_error_set (URJ_ERROR_FLASH, "hardware failure"); + return URJ_STATUS_FAIL; } #endif /* 0 */ @@ -184,18 +186,21 @@ amdstatus (urj_flash_cfi_array_t *cfi_array, uint32_t adr, int data) uint32_t data1 = URJ_BUS_READ (bus, adr); uint32_t data2 = URJ_BUS_READ (bus, adr); - /*printf("amdstatus %d: %04X/%04X %04X/%04X \n", */ - /* timeout, data1, data2, (data1 & togglemask), (data2 & togglemask)); */ + urj_log (URJ_LOG_LEVEL_DEBUG, "amdstatus %d: %04X/%04X %04X/%04X \n", + timeout, data1, data2, (data1 & togglemask), + (data2 & togglemask)); if ((data1 & togglemask) == (data2 & togglemask)) - return 1; + return URJ_STATUS_OK; /* if ( (data1 & dq5mask) != 0 ) TODO */ - /* return 0; */ - if (dbg) - printf ("amdstatus %d: %04X/%04X\n", timeout, data1, data2); + /* return URJ_STATUS_OK; */ + urj_log (URJ_LOG_LEVEL_DEBUG, "amdstatus %d: %04X/%04X\n", + timeout, data1, data2); usleep (100); } - return 0; + + urj_error_set (URJ_ERROR_FLASH, "hardware failure"); + return URJ_STATUS_FAIL; } #else /* 1 */ @@ -223,8 +228,10 @@ amdstatus (urj_flash_cfi_array_t *cfi_array, uint32_t adr, int data) data2 = URJ_BUS_READ (bus, adr); - /*printf("amdstatus %d: %04X/%04X %04X/%04X \n", */ - /* timeout, data1, data2, (data1 & togglemask), (data2 & togglemask)); */ + urj_log (URJ_LOG_LEVEL_DEBUG, + "amdstatus %d: %04X/%04X %04X/%04X \n", + timeout, data1, data2, (data1 & togglemask), + (data2 & togglemask)); /* Work around an issue with RTL8181: toggle bits don't toggle when reading the same flash address repeatedly without any other memory access in between. Other @@ -235,18 +242,21 @@ amdstatus (urj_flash_cfi_array_t *cfi_array, uint32_t adr, int data) So, check for the correct data read twice instead. */ /*if ( (data1 & togglemask) == (data2 & togglemask)) */ if ((data1 == data) && (data2 == data)) - return 1; + return URJ_STATUS_OK; /* if ( (data1 & dq5mask) != 0 ) TODO */ - /* return 0; */ - if (dbg) - printf ("amdstatus %d: %04X/%04X\n", timeout, data1, data2); + /* return URJ_STATUS_OK; */ + if (urj_log_status.level <= URJ_LOG_LEVEL_DEBUG) + urj_log (URJ_LOG_LEVEL_DEBUG, "amdstatus %d: %04X/%04X\n", + timeout, data1, data2); else - printf ("."); + urj_log (URJ_LOG_LEVEL_NORMAL, "."); usleep (100); data1 = data2; } - return 0; + + urj_error_set (URJ_ERROR_FLASH, "hardware failure"); + return URJ_STATUS_FAIL; } #endif /* 0 */ @@ -281,7 +291,7 @@ amdisprotected (parts * ps, urj_flash_cfi_array_t *cfi_array, #endif /* 0 */ static void -amd_flash_print_info (urj_flash_cfi_array_t *cfi_array) +amd_flash_print_info (urj_log_level_t ll, urj_flash_cfi_array_t *cfi_array) { int mid, cid, prot; urj_bus_t *bus = cfi_array->bus; @@ -294,95 +304,95 @@ amd_flash_print_info (urj_flash_cfi_array_t *cfi_array) cid = URJ_BUS_READ (bus, cfi_array->address + (0x01 << o)) & 0xFFFF; prot = URJ_BUS_READ (bus, cfi_array->address + (0x02 << o)) & 0xFF; amd_flash_read_array (cfi_array); /* AMD reset */ - printf (_("Chip: AMD Flash\n\tManufacturer: ")); + urj_log (ll, _("Chip: AMD Flash\n\tManufacturer: ")); switch (mid) { case 0x0001: - printf ("AMD"); - printf (_("\n\tChip: ")); + urj_log (ll, "AMD"); + urj_log (ll, _("\n\tChip: ")); switch (cid) { case 0x0049: - printf ("AM29LV160DB"); + urj_log (ll, "AM29LV160DB"); break; case 0x0093: - printf ("Am29LV065D"); + urj_log (ll, "Am29LV065D"); break; case 0x004F: - printf ("Am29LV040B"); + urj_log (ll, "Am29LV040B"); break; case 0x22D7: - printf ("Am29LV640D/Am29LV641D/Am29LV642D"); + urj_log (ll, "Am29LV640D/Am29LV641D/Am29LV642D"); break; case 0x225B: - printf ("Am29LV800B"); + urj_log (ll, "Am29LV800B"); break; case 0x227E: /* 16-bit mode */ case 0x007E: /* 8-bit mode */ - printf ("S29GLxxxN"); + urj_log (ll, "S92GLxxxN"); break; default: - printf (_("Unknown (ID 0x%04x)"), cid); + urj_log (ll, _("Unknown (ID 0x%04x)"), cid); break; } break; case 0x001f: - printf ("Atmel"); - printf (_("\n\tChip: ")); + urj_log (ll, "Atmel"); + urj_log (ll, _("\n\tChip: ")); switch (cid) { case 0x01d2: - printf ("AT49BW642DT"); + urj_log (ll, "AT49BW642DT"); break; case 0x01d6: - printf ("AT49BW642D"); + urj_log (ll, "AT49BW642D"); break; default: - printf (_("Unknown (ID 0x%04x)"), cid); + urj_log (ll, _("Unknown (ID 0x%04x)"), cid); break; } break; case 0x0020: - printf ("ST/Samsung"); - printf (_("\n\tChip: ")); + urj_log (ll, "ST/Samsung"); + urj_log (ll, _("\n\tChip: ")); switch (cid) { case 0x00ca: - printf ("M29W320DT"); + urj_log (ll, "M29W320DT"); break; case 0x00cb: - printf ("M29W320DB"); + urj_log (ll, "M29W320DB"); break; case 0x22ed: - printf ("M29W640DT"); + urj_log (ll, "M29W640DT"); break; default: - printf (_("Unknown (ID 0x%04x)"), cid); + urj_log (ll, _("Unknown (ID 0x%04x)"), cid); break; } break; case 0x00C2: - printf ("Macronix"); - printf (_("\n\tChip: ")); + urj_log (ll, "Macronix"); + urj_log (ll, _("\n\tChip: ")); switch (cid) { case 0x2249: - printf ("MX29LV160B"); + urj_log (ll, "MX29LV160B"); break; case 0x22CB: - printf ("MX29LV640B"); + urj_log (ll, "MX29LV640B"); break; default: - printf (_("Unknown (ID 0x%04x)"), cid); + urj_log (ll, _("Unknown (ID 0x%04x)"), cid); break; } break; default: - printf (_("Unknown manufacturer (ID 0x%04x) Chip (ID 0x%04x)"), mid, - cid); + urj_log (ll, _("Unknown manufacturer (ID 0x%04x) Chip (ID 0x%04x)"), + mid, cid); break; } - printf (_("\n\tProtected: %04x\n"), prot); + urj_log (ll, _("\n\tProtected: %04x\n"), prot); /* Read Array */ URJ_BUS_WRITE (bus, cfi_array->address + (0x0000 << o), 0x00ff00ff); @@ -394,9 +404,9 @@ amd_flash_erase_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) urj_bus_t *bus = cfi_array->bus; int o = amd_flash_address_shift (cfi_array); - printf ("flash_erase_block 0x%08X\n", adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_erase_block 0x%08X\n", adr); - /* printf("protected: %d\n", amdisprotected(ps, cfi_array, adr)); */ + /* urj_log (URJ_LOG_LEVEL_NORMAL, "protected: %d\n", amdisprotected(ps, cfi_array, adr)); */ URJ_BUS_WRITE (bus, cfi_array->address + (0x0555 << o), 0x00aa00aa); /* autoselect p29, sector erase */ URJ_BUS_WRITE (bus, cfi_array->address + (0x02aa << o), 0x00550055); @@ -405,24 +415,25 @@ amd_flash_erase_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) URJ_BUS_WRITE (bus, cfi_array->address + (0x02aa << o), 0x00550055); URJ_BUS_WRITE (bus, adr, 0x00300030); - if (amdstatus (cfi_array, adr, 0xffff)) + if (amdstatus (cfi_array, adr, 0xffff) == URJ_STATUS_OK) { - printf ("flash_erase_block 0x%08X DONE\n", adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_erase_block 0x%08X DONE\n", adr); amd_flash_read_array (cfi_array); /* AMD reset */ - return 0; + return URJ_STATUS_OK; } - printf ("flash_erase_block 0x%08X FAILED\n", adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_erase_block 0x%08X FAILED\n", adr); /* Read Array */ amd_flash_read_array (cfi_array); /* AMD reset */ - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_ERASE, "unknown erase error"); + return URJ_STATUS_FAIL; } static int amd_flash_unlock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) { - printf ("flash_unlock_block 0x%08X IGNORE\n", adr); - return 0; + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_unlock_block 0x%08X IGNORE\n", adr); + return URJ_STATUS_OK; } static int @@ -433,8 +444,8 @@ amd_flash_program_single (urj_flash_cfi_array_t *cfi_array, uint32_t adr, urj_bus_t *bus = cfi_array->bus; int o = amd_flash_address_shift (cfi_array); - if (dbg) - printf ("\nflash_program 0x%08X = 0x%08X\n", adr, data); + urj_log (URJ_LOG_LEVEL_DEBUG, "\nflash_program 0x%08X = 0x%08X\n", + adr, data); URJ_BUS_WRITE (bus, cfi_array->address + (0x0555 << o), 0x00aa00aa); /* autoselect p29, program */ URJ_BUS_WRITE (bus, cfi_array->address + (0x02aa << o), 0x00550055); @@ -444,7 +455,7 @@ amd_flash_program_single (urj_flash_cfi_array_t *cfi_array, uint32_t adr, status = amdstatus (cfi_array, adr, data); /* amd_flash_read_array(ps); */ - return !status; + return status; } static int @@ -464,11 +475,11 @@ amd_program_buffer_status (urj_flash_cfi_array_t *cfi_array, uint32_t adr, for (timeout = 0; timeout < 7000; timeout++) { data1 = URJ_BUS_READ (bus, adr); - if (dbg) - printf ("amd_program_buffer_status %d: %04X (%04X) = %04X\n", - timeout, data1, (data1 & dq7mask), bit7); + urj_log (URJ_LOG_LEVEL_DEBUG, + "amd_program_buffer_status %d: %04X (%04X) = %04X\n", + timeout, data1, (data1 & dq7mask), bit7); if ((data1 & dq7mask) == bit7) - return 1; + return URJ_STATUS_OK; if ((data1 & dq5mask) == dq5mask) break; @@ -477,9 +488,9 @@ amd_program_buffer_status (urj_flash_cfi_array_t *cfi_array, uint32_t adr, data1 = URJ_BUS_READ (bus, adr); if ((data1 & dq7mask) == bit7) - return 1; + return URJ_STATUS_OK; - return 0; + return URJ_STATUS_FAIL; } static int @@ -495,8 +506,8 @@ amd_flash_program_buffer (urj_flash_cfi_array_t *cfi_array, uint32_t adr, int chip_width = cfi_chip->width; int offset = 0; - if (dbg) - printf ("\nflash_program_buffer 0x%08X, count 0x%08X\n", adr, count); + urj_log (URJ_LOG_LEVEL_DEBUG, + "\nflash_program_buffer 0x%08X, count 0x%08X\n", adr, count); while (count > 0) { @@ -525,17 +536,20 @@ amd_flash_program_buffer (urj_flash_cfi_array_t *cfi_array, uint32_t adr, /* program buffer to flash */ URJ_BUS_WRITE (bus, sa, 0x00290029); - status = - amd_program_buffer_status (cfi_array, adr - cfi_array->bus_width, - buffer[offset - 1]); + status = amd_program_buffer_status (cfi_array, + adr - cfi_array->bus_width, + buffer[offset - 1]); /* amd_flash_read_array(ps); */ - if (status != 1) - return 1; + if (status != URJ_STATUS_OK) + { + urj_error_set (URJ_ERROR_FLASH_PROGRAM, "status fails after write"); + return URJ_STATUS_FAIL; + } count -= wcount; } - return 0; + return URJ_STATUS_OK; } static int @@ -561,13 +575,13 @@ amd_flash_program (urj_flash_cfi_array_t *cfi_array, uint32_t adr, for (idx = 0; idx < count; idx++) { int status = amd_flash_program_single (cfi_array, adr, buffer[idx]); - if (status) + if (status != URJ_STATUS_OK) return status; adr += cfi_array->bus_width; } } - return 0; + return URJ_STATUS_OK; } static int @@ -584,14 +598,13 @@ amd_flash_program32 (urj_flash_cfi_array_t *cfi_array, uint32_t adr, /* unroll buffer to single writes */ for (idx = 0; idx < count; idx++) { - int status = - amd_flash_program_single (cfi_array, adr, buffer[idx]); - if (status) + int status = amd_flash_program_single (cfi_array, adr, buffer[idx]); + if (status != URJ_STATUS_OK) return status; adr += cfi_array->bus_width; } - return 0; + return URJ_STATUS_OK; } urj_flash_driver_t urj_flash_amd_32_flash_driver = { diff --git a/urjtag/src/flash/amd.h b/urjtag/src/flash/amd.h new file mode 100644 index 00000000..9f79b855 --- /dev/null +++ b/urjtag/src/flash/amd.h @@ -0,0 +1,48 @@ +/* + * $Id: flash.h 1554 2009-05-03 16:06:22Z rfhh $ + * + * Copyright (C) 2003 ETC s.r.o. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the ETC s.r.o. nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Written by Marcel Telka , 2003. + * + */ + +#ifndef URJ_SRC_AMD_H +#define URJ_SRC_AMD_H + +#include +#include + +int urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, + urj_flash_cfi_array_t **urj_flash_cfi_array); + +extern urj_flash_driver_t urj_flash_amd_32_flash_driver; +extern urj_flash_driver_t urj_flash_amd_16_flash_driver; +extern urj_flash_driver_t urj_flash_amd_8_flash_driver; +extern urj_flash_driver_t urj_flash_amd_29xx040_flash_driver; //20/09/2006 + +#endif /* URJ_FLASH_H */ diff --git a/urjtag/src/flash/amd_flash.c b/urjtag/src/flash/amd_flash.c index b7f96136..c4d102a1 100644 --- a/urjtag/src/flash/amd_flash.c +++ b/urjtag/src/flash/amd_flash.c @@ -34,12 +34,14 @@ #include #include +#include +#include #include #include -#include "jedec.h" +#include "flash.h" +#include "amd.h" #include "cfi.h" -#include "intel.h" //write specific #define AMD_SECTOR_PROTECTED @@ -86,11 +88,18 @@ urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, urj_flash_cfi_query_structure_t *cfi; if (!cfi_array || !bus) - return -1; /* invalid parameters */ + { + urj_error_set (URJ_ERROR_INVALID, "cfi_array or bus"); + return URJ_STATUS_FAIL; + } *cfi_array = calloc (1, sizeof (urj_flash_cfi_array_t)); if (!*cfi_array) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) failed", + 1, sizeof (urj_flash_cfi_array_t)); + return URJ_STATUS_FAIL; + } URJ_BUS_WRITE (bus, adr + 0x0, 0xf0); URJ_BUS_WRITE (bus, adr + 0x555, 0xaa); @@ -100,9 +109,12 @@ urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, did = URJ_BUS_READ (bus, adr + 0x1); URJ_BUS_WRITE (bus, adr + 0x0, 0xf0); - printf ("%s: mid %x, did %x\n", __FUNCTION__, mid, did); + urj_log (URJ_LOG_LEVEL_NORMAL, "%s: mid %x, did %x\n", __func__, mid, did); if (mid != 0x01) - return -1; + { + urj_error_set (URJ_ERROR_FLASH, "mid != 0x01"); + return URJ_STATUS_FAIL; + } switch (did) { @@ -120,22 +132,33 @@ urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, (*cfi_array)->bus = bus; (*cfi_array)->address = 0; if (URJ_BUS_AREA (bus, adr + 0, &area) != URJ_STATUS_OK) - return -8; /* bus width detection failed */ + // retain error status + return URJ_STATUS_FAIL; /* bus width detection failed */ unsigned int bw = area.width; int ba, i; if (bw != 8 && bw != 16 && bw != 32) - return -3; /* invalid bus width */ + { + urj_error_set (URJ_ERROR_INVALID, "bus width = %d", bw); + return URJ_STATUS_FAIL; + } + (*cfi_array)->bus_width = ba = bw / 8; - (*cfi_array)->cfi_chips = - calloc (ba, sizeof (urj_flash_cfi_chip_t *)); + (*cfi_array)->cfi_chips = calloc (ba, sizeof (urj_flash_cfi_chip_t *)); if (!(*cfi_array)->cfi_chips) - return -2; + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + ba, sizeof (urj_flash_cfi_chip_t *)); + return URJ_STATUS_FAIL; + } for (i = 0; i < ba; i++) { - (*cfi_array)->cfi_chips[i] = - calloc (1, sizeof (urj_flash_cfi_chip_t)); + (*cfi_array)->cfi_chips[i] = calloc (1, sizeof (urj_flash_cfi_chip_t)); if (!(*cfi_array)->cfi_chips[i]) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_chip_t)); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[i]->width = 1; //ba; cfi = &(*cfi_array)->cfi_chips[i]->cfi; @@ -152,7 +175,11 @@ urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, malloc (cfi->device_geometry.number_of_erase_regions * sizeof (urj_flash_cfi_erase_block_region_t)); if (!cfi->device_geometry.erase_block_regions) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + sizeof (urj_flash_cfi_erase_block_region_t)); + return URJ_STATUS_FAIL; + } cfi->device_geometry.erase_block_regions[i].erase_block_size = 64 * 1024; @@ -160,7 +187,8 @@ urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, 8; //Add other details for info } - return 0; + + return URJ_STATUS_OK; } @@ -185,35 +213,35 @@ amd_29xx040_status (urj_bus_t *bus, uint32_t adr, unsigned short data) { data1 = (unsigned short) (URJ_BUS_READ (bus, adr) & 0xFF); if ((data1 & dq7mask) == dq7bit) - return 1; //Success + return URJ_STATUS_OK; //Success if ((data1 & dq5mask) == dq5mask) { data1 = (unsigned short) (URJ_BUS_READ (bus, adr) & 0xFF); if ((data1 & dq7mask) == dq7bit) - { - return 1; //Success - } + return URJ_STATUS_OK; //Success else { - return 0; //Failure - Needs a reset command to return back to read array data + urj_error_set (URJ_ERROR_FLASH, + "status failure: needs a reset command to return back to read array data"); + return URJ_STATUS_FAIL; } } usleep (50); } - return 0; //hardware failure + urj_error_set (URJ_ERROR_FLASH, "hardware failure"); + return URJ_STATUS_FAIL; } static void -amd_29xx040_print_info (urj_flash_cfi_array_t *cfi_array) +amd_29xx040_print_info (urj_log_level_t ll, urj_flash_cfi_array_t *cfi_array) { int mid, did, prot; urj_bus_t *bus = cfi_array->bus; - URJ_BUS_WRITE (bus, cfi_array->address + 0x0, 0xf0); URJ_BUS_WRITE (bus, cfi_array->address + 0x555, 0xaa); URJ_BUS_WRITE (bus, cfi_array->address + 0x2AA, 0x55); @@ -223,34 +251,34 @@ amd_29xx040_print_info (urj_flash_cfi_array_t *cfi_array) prot = URJ_BUS_READ (bus, cfi_array->address + 0x2); URJ_BUS_WRITE (bus, cfi_array->address + 0x0, 0xf0); - printf ("%s: mid %x, did %x\n", __FUNCTION__, mid, did); + urj_log (ll, "%s: mid %x, did %x\n", __FUNCTION__, mid, did); // amd_29xx040_read_array( cfi_array ); /* AMD reset */ switch (mid) { case 0x01: - printf (_("Chip: AMD Flash\n\tPartNumber: ")); + urj_log (ll, _("Chip: AMD Flash\n\tPartNumber: ")); break; default: - printf (_("Unknown manufacturer (ID 0x%04x)"), mid); + urj_log (ll, _("Unknown manufacturer (ID 0x%04x)"), mid); break; } - printf (_("\n\tChip: ")); + urj_log (ll, _("\n\tChip: ")); switch (did) { case 0xA4: - printf ("Am29C040B\t-\t"); - printf (_("5V Flash\n")); + urj_log (ll, "Am29C040B\t-\t"); + urj_log (ll, _("5V Flash\n")); break; case 0x4F: - printf ("Am29LV040B\t-\t"); - printf (_("3V Flash\n")); + urj_log (ll, "Am29LV040B\t-\t"); + urj_log (ll, _("3V Flash\n")); break; default: - printf (_("Unknown (ID 0x%04x)"), did); + urj_log (ll, _("Unknown (ID 0x%04x)"), did); break; } - printf (_("\n\tProtected: %04x\n"), prot); + urj_log (ll, _("\n\tProtected: %04x\n"), prot); } static void @@ -278,9 +306,9 @@ amd_29xx040_erase_block (urj_flash_cfi_array_t *cfi_array, { urj_bus_t *bus = cfi_array->bus; - printf ("flash_erase_block 0x%08X\n", adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_erase_block 0x%08X\n", adr); - /* printf("protected: %d\n", amdisprotected(ps, adr)); */ + /* urj_log (URJ_LOG_LEVEL_NORMAL, "protected: %d\n", amdisprotected(ps, adr)); */ if (var_forced_detection.unlock_bypass == AMD_BYPASS_UNLOCK_MODE) { @@ -300,17 +328,18 @@ amd_29xx040_erase_block (urj_flash_cfi_array_t *cfi_array, URJ_BUS_WRITE (bus, adr, 0x30); //Sector erase - if (amd_29xx040_status (bus, adr, 0xff)) + if (amd_29xx040_status (bus, adr, 0xff) == URJ_STATUS_OK) { - printf ("flash_erase_block 0x%08X DONE\n", adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_erase_block 0x%08X DONE\n", adr); amd_29xx040_read_array (cfi_array); /* AMD reset */ - return ERASE_FLASH_SUCCESS; + return URJ_STATUS_OK; } - printf ("flash_erase_block 0x%08X FAILED\n", adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_erase_block 0x%08X FAILED\n", adr); /* Read Array */ amd_29xx040_read_array (cfi_array); /* AMD reset */ - return FLASH_ERASE_ERROR; + urj_error_set (URJ_ERROR_FLASH_ERASE, "erase block"); + return URJ_STATUS_FAIL; } static int @@ -320,8 +349,9 @@ amd_29xx040_program_single (urj_flash_cfi_array_t *cfi_array, int status; urj_bus_t *bus = cfi_array->bus; - if (0) - printf ("\nflash_program 0x%08X = 0x%08X\n", adr, data); + urj_log (URJ_LOG_LEVEL_DETAIL, "\nflash_program 0x%08X = 0x%08X\n", + adr, data); + if (var_forced_detection.algorithm == AMD_BYPASS_UNLOCK_ALGORITHM) { if (var_forced_detection.unlock_bypass != AMD_BYPASS_UNLOCK_MODE) @@ -344,7 +374,7 @@ amd_29xx040_program_single (urj_flash_cfi_array_t *cfi_array, status = amd_29xx040_status (bus, adr, data); /* amd_29xx040_read_array(cfi_array); */ - return !status; + return status; } static int @@ -358,20 +388,20 @@ amd_29xx040_program (urj_flash_cfi_array_t *cfi_array, { int status = amd_29xx040_program_single (cfi_array, adr, buffer[idx]); - if (status) + if (status != URJ_STATUS_OK) return status; adr += cfi_array->bus_width; } - return 0; + return URJ_STATUS_OK; } static int amd_29xx040_unlock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) { - printf ("flash_unlock_block 0x%08X IGNORE\n", adr); - return 0; + urj_log (URJ_LOG_LEVEL_NORMAL, "flash_unlock_block 0x%08X IGNORE\n", adr); + return URJ_STATUS_OK; } diff --git a/urjtag/src/flash/cfi.c b/urjtag/src/flash/cfi.c index 34a263b6..75f7f740 100644 --- a/urjtag/src/flash/cfi.c +++ b/urjtag/src/flash/cfi.c @@ -33,9 +33,13 @@ #include #include +#include +#include #include #include +#include "flash.h" + #include "cfi.h" void @@ -73,29 +77,43 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, { unsigned int bw; /* bus width */ unsigned int d; /* data offset */ - int ba; /* bus width address multiplier */ + size_t ba; /* bus width address multiplier */ int ma; /* flash mode address multiplier */ urj_bus_area_t area; if (!cfi_array || !bus) - return -1; /* invalid parameters */ + { + urj_error_set (URJ_ERROR_INVALID, "cfi_array or bus"); + return URJ_STATUS_FAIL; + } *cfi_array = calloc (1, sizeof (urj_flash_cfi_array_t)); if (!*cfi_array) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_array_t)); + return URJ_STATUS_FAIL; + } (*cfi_array)->bus = bus; (*cfi_array)->address = adr; if (URJ_BUS_AREA (bus, adr, &area) != URJ_STATUS_OK) - return -8; /* bus width detection failed */ + // retain error state + return URJ_STATUS_FAIL; bw = area.width; if (bw != 8 && bw != 16 && bw != 32) - return -3; /* invalid bus width */ + { + urj_error_set (URJ_ERROR_INVALID, "bus width = %d", bw); + return URJ_STATUS_FAIL; + } (*cfi_array)->bus_width = ba = bw / 8; - (*cfi_array)->cfi_chips = - calloc (ba, sizeof (urj_flash_cfi_chip_t *)); + (*cfi_array)->cfi_chips = calloc (ba, sizeof (urj_flash_cfi_chip_t *)); if (!(*cfi_array)->cfi_chips) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + ba, sizeof (urj_flash_cfi_chip_t *)); + return URJ_STATUS_FAIL; + } for (d = 0; d < bw; d += 8) { @@ -127,12 +145,19 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, } if (ma > 4) - return ret; /* CFI not detected (Q or R) */ + { + if (ret == -4) + urj_error_set (URJ_ERROR_FLASH, "CFI not detected (Q)"); + else + urj_error_set (URJ_ERROR_FLASH, "CFI not detected (R)"); + return URJ_STATUS_FAIL; + } if (read1 (CFI_QUERY_ID_OFFSET + 2) != 'Y') { write1 (0, CFI_CMD_READ_ARRAY1); - return -6; /* CFI not detected (Y) */ + urj_error_set (URJ_ERROR_FLASH, "CFI not detected (Y)"); + return URJ_STATUS_FAIL; } (*cfi_array)->cfi_chips[d / 8] = @@ -140,7 +165,9 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, if (!(*cfi_array)->cfi_chips[d / 8]) { write1 (0, CFI_CMD_READ_ARRAY1); - return -2; /* out of memory */ + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_chip_t)); + return URJ_STATUS_FAIL; } cfi = &(*cfi_array)->cfi_chips[d / 8]->cfi; @@ -220,7 +247,9 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, if (!cfi->device_geometry.erase_block_regions) { write1 (0, CFI_CMD_READ_ARRAY1); - return -2; /* out of memory */ + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + tmp * sizeof (urj_flash_cfi_erase_block_region_t)); + return URJ_STATUS_FAIL; } { @@ -257,7 +286,9 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, if (read1 (0) != 'P' || read1 (1) != 'R' || read1 (2) != 'I') { write1 (0, CFI_CMD_READ_ARRAY1); - return -9; /* CFI primary vendor table not detected */ + urj_error_set (URJ_ERROR_FLASH, + "CFI primary vendor table not detected"); + return URJ_STATUS_FAIL; } major_version = read1 (MAJOR_VERSION_OFFSET); @@ -273,7 +304,10 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, if (!pri_vendor_tbl) { write1 (0, CFI_CMD_READ_ARRAY1); - return -2; /* out of memory */ + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_amd_pri_extened_query_structure_t) + + num_of_banks * sizeof (uint8_t)); + return URJ_STATUS_FAIL; } if (major_version > '1' @@ -284,8 +318,7 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, pri_vendor_tbl->address_sensitive_unlock = read1 (ADDRESS_SENSITIVE_UNLOCK_OFFSET); pri_vendor_tbl->erase_suspend = read1 (ERASE_SUSPEND_OFFSET); - pri_vendor_tbl->sector_protect = - read1 (SECTOR_PROTECT_OFFSET); + pri_vendor_tbl->sector_protect = read1 (SECTOR_PROTECT_OFFSET); pri_vendor_tbl->sector_temporary_unprotect = read1 (SECTOR_TEMPORARY_UNPROTECT_OFFSET); pri_vendor_tbl->sector_protect_scheme = @@ -346,8 +379,7 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, tmp ? (1 << tmp) : 0; } - cfi->identification_string.pri_vendor_tbl = - (void *) pri_vendor_tbl; + cfi->identification_string.pri_vendor_tbl = (void *) pri_vendor_tbl; #undef A #define A(off) (adr + (off) * ba * ma) @@ -368,19 +400,15 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, number_of_erase_blocks; cfi->device_geometry.erase_block_regions[i]. erase_block_size = - cfi->device_geometry.erase_block_regions[n - i - - 1]. + cfi->device_geometry.erase_block_regions[n - i - 1]. erase_block_size; cfi->device_geometry.erase_block_regions[i]. number_of_erase_blocks = - cfi->device_geometry.erase_block_regions[n - i - - 1]. + cfi->device_geometry.erase_block_regions[n - i - 1]. number_of_erase_blocks; - cfi->device_geometry.erase_block_regions[n - i - - 1]. + cfi->device_geometry.erase_block_regions[n - i - 1]. erase_block_size = z; - cfi->device_geometry.erase_block_regions[n - i - - 1]. + cfi->device_geometry.erase_block_regions[n - i - 1]. number_of_erase_blocks = y; } } @@ -402,31 +430,46 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, { case CFI_INTERFACE_X8: if (ma != 1) - return -7; /* error in device detection */ + { + urj_error_set (URJ_ERROR_FLASH_DETECT, "device detection"); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[d / 8]->width = 1; break; case CFI_INTERFACE_X16: if (ma != 1) - return -7; /* error in device detection */ + { + urj_error_set (URJ_ERROR_FLASH_DETECT, "device detection"); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[d / 8]->width = 2; d += 8; break; case CFI_INTERFACE_X8_X16: if (ma != 1 && ma != 2) - return -7; /* error in device detection */ + { + urj_error_set (URJ_ERROR_FLASH_DETECT, "device detection"); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[d / 8]->width = 2 / ma; if (ma == 1) d += 8; break; case CFI_INTERFACE_X32: if (ma != 1) - return -7; /* error in device detection */ + { + urj_error_set (URJ_ERROR_FLASH_DETECT, "device detection"); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[d / 8]->width = 4; d += 24; break; case CFI_INTERFACE_X16_X32: if (ma != 1 && ma != 2) - return -7; /* error in device detection */ + { + urj_error_set (URJ_ERROR_FLASH_DETECT, "device detection"); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[d / 8]->width = 4 / ma; if (ma == 1) d += 24; @@ -434,9 +477,10 @@ urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, d += 8; break; default: - return -7; /* error in device detection */ + urj_error_set (URJ_ERROR_FLASH_DETECT, "device detection"); + return URJ_STATUS_FAIL; } } - return 0; + return URJ_STATUS_OK; } diff --git a/urjtag/src/flash/cfi.h b/urjtag/src/flash/cfi.h index 4bf63d53..7ef26ce0 100644 --- a/urjtag/src/flash/cfi.h +++ b/urjtag/src/flash/cfi.h @@ -47,6 +47,9 @@ #ifndef __ASSEMBLY__ #include + +#include +#include #endif /* CFI commands - see Table 1 in [1] */ @@ -220,6 +223,11 @@ typedef struct amd_pri_extened_query_structure uint8_t bank_organization; /* in us */ uint8_t bank_region_info[0]; } urj_flash_cfi_amd_pri_extened_query_structure_t; + +void urj_flash_cfi_array_free (urj_flash_cfi_array_t *cfi_array); +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_flash_cfi_detect (urj_bus_t *bus, uint32_t adr, + urj_flash_cfi_array_t **cfi_array); #endif /* __ASSEMBLY__ */ #endif /* FLASH_CFI_H */ diff --git a/urjtag/src/flash/detectflash.c b/urjtag/src/flash/detectflash.c index 917e157d..c2f5aeb1 100644 --- a/urjtag/src/flash/detectflash.c +++ b/urjtag/src/flash/detectflash.c @@ -34,51 +34,58 @@ #include #include +#include #include #include +#include "flash.h" + #include "jedec.h" +#include "amd.h" #include "cfi.h" #include "intel.h" -#include "mic.h" urj_flash_cfi_array_t *urj_flash_cfi_array = NULL; void -urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) +urj_flash_cleanup (void) +{ + urj_flash_cfi_array_free (urj_flash_cfi_array); + urj_flash_cfi_array = NULL; +} + +int +urj_flash_detectflash (urj_log_level_t ll, urj_bus_t *bus, uint32_t adr) { urj_flash_cfi_query_structure_t *cfi; const char *s; if (!bus) { - printf (_("Error: Missing bus driver!\n")); - return; + urj_error_set (URJ_ERROR_INVALID, _("bus driver")); + return URJ_STATUS_FAIL; } - urj_flash_cfi_array_free (urj_flash_cfi_array); - urj_flash_cfi_array = NULL; + urj_flash_cleanup(); URJ_BUS_PREPARE (bus); - if (urj_flash_cfi_detect (bus, adr, &urj_flash_cfi_array)) + if (urj_flash_cfi_detect (bus, adr, &urj_flash_cfi_array) != URJ_STATUS_OK) { - urj_flash_cfi_array_free (urj_flash_cfi_array); - urj_flash_cfi_array = NULL; - if (urj_flash_jedec_detect (bus, adr, &urj_flash_cfi_array) != 0) + urj_flash_cleanup(); + if (urj_flash_jedec_detect (bus, adr, &urj_flash_cfi_array) + != URJ_STATUS_OK) { - urj_flash_cfi_array_free (urj_flash_cfi_array); - urj_flash_cfi_array = NULL; - if (urj_flash_amd_detect (bus, adr, &urj_flash_cfi_array) != 0) + urj_flash_cleanup(); + if (urj_flash_amd_detect (bus, adr, &urj_flash_cfi_array) + != URJ_STATUS_OK) { - urj_flash_cfi_array_free (urj_flash_cfi_array); - urj_flash_cfi_array = NULL; + urj_flash_cleanup(); #ifdef JEDEC_EXP - if (urj_flash_jedec_exp_detect - (bus, adr, &urj_flash_cfi_array)) + if (urj_flash_jedec_exp_detect (bus, adr, &urj_flash_cfi_array) + != URJ_STATUS_OK) { - urj_flash_cfi_array_free (urj_flash_cfi_array); - urj_flash_cfi_array = NULL; + urj_flash_cleanup(); } #endif } @@ -87,8 +94,8 @@ urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) if (urj_flash_cfi_array == NULL) { - printf (_("Flash not found!\n")); - return; + urj_error_set (URJ_ERROR_NOTFOUND, _("Flash not found!")); + return URJ_STATUS_FAIL; } cfi = &urj_flash_cfi_array->cfi_chips[0]->cfi; @@ -96,7 +103,7 @@ urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) /* detect CFI capable devices */ /* TODO: Low chip only */ /* see 4.3.2 in [1] */ - printf (_("Query identification string:\n")); + urj_log (ll, _("Query identification string:\n")); /* see section 2 in [2] */ switch (cfi->identification_string.pri_id_code) { @@ -128,9 +135,8 @@ urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) s = N_("unknown!!!"); break; } - printf (_ - ("\tPrimary Algorithm Command Set and Control Interface ID Code: 0x%04X (%s)\n"), - cfi->identification_string.pri_id_code, _(s)); + urj_log (ll, _("\tPrimary Algorithm Command Set and Control Interface ID Code: 0x%04X (%s)\n"), + cfi->identification_string.pri_id_code, _(s)); switch (cfi->identification_string.alt_id_code) { case CFI_VENDOR_NULL: @@ -161,48 +167,42 @@ urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) s = N_("unknown!!!"); break; } - printf (_ - ("\tAlternate Algorithm Command Set and Control Interface ID Code: 0x%04X (%s)\n"), - cfi->identification_string.alt_id_code, _(s)); + urj_log (ll, _("\tAlternate Algorithm Command Set and Control Interface ID Code: 0x%04X (%s)\n"), + cfi->identification_string.alt_id_code, _(s)); /* see 4.3.3 in [1] */ - printf (_("Query system interface information:\n")); - printf (_ - ("\tVcc Logic Supply Minimum Write/Erase or Write voltage: %d mV\n"), - cfi->system_interface_info.vcc_min_wev); - printf (_ - ("\tVcc Logic Supply Maximum Write/Erase or Write voltage: %d mV\n"), - cfi->system_interface_info.vcc_max_wev); - printf (_ - ("\tVpp [Programming] Supply Minimum Write/Erase voltage: %d mV\n"), - cfi->system_interface_info.vpp_min_wev); - printf (_ - ("\tVpp [Programming] Supply Maximum Write/Erase voltage: %d mV\n"), - cfi->system_interface_info.vpp_max_wev); - printf (_("\tTypical timeout per single byte/word program: %d us\n"), - cfi->system_interface_info.typ_single_write_timeout); - printf (_ - ("\tTypical timeout for maximum-size multi-byte program: %d us\n"), - cfi->system_interface_info.typ_buffer_write_timeout); - printf (_("\tTypical timeout per individual block erase: %d ms\n"), - cfi->system_interface_info.typ_block_erase_timeout); - printf (_("\tTypical timeout for full chip erase: %d ms\n"), - cfi->system_interface_info.typ_chip_erase_timeout); - printf (_("\tMaximum timeout for byte/word program: %d us\n"), - cfi->system_interface_info.max_single_write_timeout); - printf (_("\tMaximum timeout for multi-byte program: %d us\n"), - cfi->system_interface_info.max_buffer_write_timeout); - printf (_("\tMaximum timeout per individual block erase: %d ms\n"), - cfi->system_interface_info.max_block_erase_timeout); - printf (_("\tMaximum timeout for chip erase: %d ms\n"), - cfi->system_interface_info.max_chip_erase_timeout); + urj_log (ll, _("Query system interface information:\n")); + urj_log (ll, _("\tVcc Logic Supply Minimum Write/Erase or Write voltage: %d mV\n"), + cfi->system_interface_info.vcc_min_wev); + urj_log (ll, _("\tVcc Logic Supply Maximum Write/Erase or Write voltage: %d mV\n"), + cfi->system_interface_info.vcc_max_wev); + urj_log (ll, _("\tVpp [Programming] Supply Minimum Write/Erase voltage: %d mV\n"), + cfi->system_interface_info.vpp_min_wev); + urj_log (ll, _("\tVpp [Programming] Supply Maximum Write/Erase voltage: %d mV\n"), + cfi->system_interface_info.vpp_max_wev); + urj_log (ll, _("\tTypical timeout per single byte/word program: %d us\n"), + cfi->system_interface_info.typ_single_write_timeout); + urj_log (ll, _("\tTypical timeout for maximum-size multi-byte program: %d us\n"), + cfi->system_interface_info.typ_buffer_write_timeout); + urj_log (ll, _("\tTypical timeout per individual block erase: %d ms\n"), + cfi->system_interface_info.typ_block_erase_timeout); + urj_log (ll, _("\tTypical timeout for full chip erase: %d ms\n"), + cfi->system_interface_info.typ_chip_erase_timeout); + urj_log (ll, _("\tMaximum timeout for byte/word program: %d us\n"), + cfi->system_interface_info.max_single_write_timeout); + urj_log (ll, _("\tMaximum timeout for multi-byte program: %d us\n"), + cfi->system_interface_info.max_buffer_write_timeout); + urj_log (ll, _("\tMaximum timeout per individual block erase: %d ms\n"), + cfi->system_interface_info.max_block_erase_timeout); + urj_log (ll, _("\tMaximum timeout for chip erase: %d ms\n"), + cfi->system_interface_info.max_chip_erase_timeout); /* see 4.3.4 in [1] */ - printf (_("Device geometry definition:\n")); - printf (_("\tDevice Size: %d B (%d KiB, %d MiB)\n"), - cfi->device_geometry.device_size, - cfi->device_geometry.device_size / 1024, - cfi->device_geometry.device_size / (1024 * 1024)); + urj_log (ll, _("Device geometry definition:\n")); + urj_log (ll, _("\tDevice Size: %d B (%d KiB, %d MiB)\n"), + cfi->device_geometry.device_size, + cfi->device_geometry.device_size / 1024, + cfi->device_geometry.device_size / (1024 * 1024)); /* see section 4 in [2] */ switch (cfi->device_geometry.device_interface) { @@ -225,27 +225,27 @@ urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) s = N_("unknown!!!"); break; } - printf (_("\tFlash Device Interface Code description: 0x%04X (%s)\n"), - cfi->device_geometry.device_interface, _(s)); - printf (_("\tMaximum number of bytes in multi-byte program: %d\n"), - cfi->device_geometry.max_bytes_write); - printf (_("\tNumber of Erase Block Regions within device: %d\n"), - cfi->device_geometry.number_of_erase_regions); - printf (_("\tErase Block Region Information:\n")); + urj_log (ll, _("\tFlash Device Interface Code description: 0x%04X (%s)\n"), + cfi->device_geometry.device_interface, _(s)); + urj_log (ll, _("\tMaximum number of bytes in multi-byte program: %d\n"), + cfi->device_geometry.max_bytes_write); + urj_log (ll, _("\tNumber of Erase Block Regions within device: %d\n"), + cfi->device_geometry.number_of_erase_regions); + urj_log (ll, _("\tErase Block Region Information:\n")); { int i; for (i = 0; i < cfi->device_geometry.number_of_erase_regions; i++) { - printf (_("\t\tRegion %d:\n"), i); - printf (_("\t\t\tErase Block Size: %d B (%d KiB)\n"), - cfi->device_geometry.erase_block_regions[i]. - erase_block_size, - cfi->device_geometry.erase_block_regions[i]. - erase_block_size / 1024); - printf (_("\t\t\tNumber of Erase Blocks: %d\n"), - cfi->device_geometry.erase_block_regions[i]. - number_of_erase_blocks); + urj_log (ll, _("\t\tRegion %d:\n"), i); + urj_log (ll, _("\t\t\tErase Block Size: %d B (%d KiB)\n"), + cfi->device_geometry.erase_block_regions[i]. + erase_block_size, + cfi->device_geometry.erase_block_regions[i]. + erase_block_size / 1024); + urj_log (ll, _("\t\t\tNumber of Erase Blocks: %d\n"), + cfi->device_geometry.erase_block_regions[i]. + number_of_erase_blocks); } } @@ -303,137 +303,142 @@ urj_flash_detectflash (urj_bus_t *bus, uint32_t adr) major_version = pri_vendor_tbl->major_version; minor_version = pri_vendor_tbl->minor_version; - printf (_("Primary Vendor-Specific Extended Query:\n")); - printf (_("\tMajor version number: %c\n"), - pri_vendor_tbl->major_version); - printf (_("\tMinor version number: %c\n"), - pri_vendor_tbl->minor_version); + urj_log (ll, _("Primary Vendor-Specific Extended Query:\n")); + urj_log (ll, _("\tMajor version number: %c\n"), + pri_vendor_tbl->major_version); + urj_log (ll, _("\tMinor version number: %c\n"), + pri_vendor_tbl->minor_version); if (major_version > '1' || (major_version == '1' && minor_version >= '0')) { if ((pri_vendor_tbl->address_sensitive_unlock & 0x3) < ARRAY_SIZE (required_or_not)) - printf (_("\tAddress Sensitive Unlock: %s\n"), - required_or_not[pri_vendor_tbl-> - address_sensitive_unlock & 0x3]); + urj_log (ll, _("\tAddress Sensitive Unlock: %s\n"), + required_or_not[pri_vendor_tbl-> + address_sensitive_unlock & 0x3]); else - printf (_("\tAddress Sensitive Unlock: %s\n"), bad_value); + urj_log (ll, _("\tAddress Sensitive Unlock: %s\n"), bad_value); if (major_version > '1' || (major_version == '1' && minor_version >= '4')) { if ((pri_vendor_tbl->address_sensitive_unlock >> 2) < ARRAY_SIZE (process_technology)) - printf (_("\tProcess Technology: %s\n"), - process_technology[pri_vendor_tbl-> - address_sensitive_unlock >> - 2]); + urj_log (ll, _("\tProcess Technology: %s\n"), + process_technology[pri_vendor_tbl-> + address_sensitive_unlock >> + 2]); else - printf (_("\tProcess Technology: %s\n"), bad_value); + urj_log (ll, _("\tProcess Technology: %s\n"), bad_value); } else if (major_version == '1' && minor_version == '3') { if ((pri_vendor_tbl->address_sensitive_unlock >> 2) < ARRAY_SIZE (process_technology_13)) - printf (_("\tProcess Technology: %s\n"), - process_technology_13[pri_vendor_tbl-> - address_sensitive_unlock >> - 2]); + urj_log (ll, _("\tProcess Technology: %s\n"), + process_technology_13[pri_vendor_tbl-> + address_sensitive_unlock >> + 2]); else - printf (_("\tProcess Technology: %s\n"), bad_value); + urj_log (ll, _("\tProcess Technology: %s\n"), bad_value); } if (pri_vendor_tbl->erase_suspend < ARRAY_SIZE (erase_suspend)) - printf (_("\tErase Suspend: %s\n"), - erase_suspend[pri_vendor_tbl->erase_suspend]); + urj_log (ll, _("\tErase Suspend: %s\n"), + erase_suspend[pri_vendor_tbl->erase_suspend]); if (pri_vendor_tbl->sector_protect == 0) - printf (_("\tSector Protect: Not supported\n")); + urj_log (ll, _("\tSector Protect: Not supported\n")); else - printf (_("\tSector Protect: %d sectors per group\n"), - pri_vendor_tbl->sector_protect); + urj_log (ll, _("\tSector Protect: %d sectors per group\n"), + pri_vendor_tbl->sector_protect); if (pri_vendor_tbl->sector_temporary_unprotect < ARRAY_SIZE (supported_or_not)) - printf (_("\tSector Temporary Unprotect: %s\n"), - supported_or_not[pri_vendor_tbl-> - sector_temporary_unprotect]); + urj_log (ll, _("\tSector Temporary Unprotect: %s\n"), + supported_or_not[pri_vendor_tbl-> + sector_temporary_unprotect]); else - printf (_("\tSector Temporary Unprotect: %s\n"), bad_value); + urj_log (ll, _("\tSector Temporary Unprotect: %s\n"), + bad_value); if (pri_vendor_tbl->sector_protect_scheme < ARRAY_SIZE (sector_protect_scheme)) - printf (_("\tSector Protect/Unprotect Scheme: %s\n"), - sector_protect_scheme[pri_vendor_tbl-> - sector_protect_scheme]); + urj_log (ll, _("\tSector Protect/Unprotect Scheme: %s\n"), + sector_protect_scheme[pri_vendor_tbl-> + sector_protect_scheme]); else - printf (_("\tSector Protect/Unprotect Scheme: %s\n"), - bad_value); + urj_log (ll, _("\tSector Protect/Unprotect Scheme: %s\n"), + bad_value); if (pri_vendor_tbl->simultaneous_operation == 0) - printf (_("\tSimultaneous Operation: Not supported\n")); + urj_log (ll, _("\tSimultaneous Operation: Not supported\n")); else - printf (_("\tSimultaneous Operation: %d sectors\n"), - pri_vendor_tbl->simultaneous_operation); + urj_log (ll, _("\tSimultaneous Operation: %d sectors\n"), + pri_vendor_tbl->simultaneous_operation); if (pri_vendor_tbl->burst_mode_type < ARRAY_SIZE (supported_or_not)) - printf (_("\tBurst Mode Type: %s\n"), - supported_or_not[pri_vendor_tbl->burst_mode_type]); + urj_log (ll, _("\tBurst Mode Type: %s\n"), + supported_or_not[pri_vendor_tbl->burst_mode_type]); else - printf (_("\tBurst Mode Type: %s\n"), bad_value); + urj_log (ll, _("\tBurst Mode Type: %s\n"), + bad_value); if (pri_vendor_tbl->page_mode_type < ARRAY_SIZE (page_mode_type)) - printf (_("\tPage Mode Type: %s\n"), - page_mode_type[pri_vendor_tbl->page_mode_type]); + urj_log (ll, _("\tPage Mode Type: %s\n"), + page_mode_type[pri_vendor_tbl->page_mode_type]); else - printf (_("\tPage Mode Type: %s\n"), bad_value); + urj_log (ll, _("\tPage Mode Type: %s\n"), + bad_value); } if (major_version > '1' || (major_version == '1' && minor_version >= '1')) { - printf (_("\tACC (Acceleration) Supply Minimum: %d mV\n"), - pri_vendor_tbl->acc_min); - printf (_("\tACC (Acceleration) Supply Maximum: %d mV\n"), - pri_vendor_tbl->acc_max); + urj_log (ll, _("\tACC (Acceleration) Supply Minimum: %d mV\n"), + pri_vendor_tbl->acc_min); + urj_log (ll, _("\tACC (Acceleration) Supply Maximum: %d mV\n"), + pri_vendor_tbl->acc_max); if (pri_vendor_tbl->top_bottom_sector_flag < ARRAY_SIZE (top_bottom)) - printf (_("\tTop/Bottom Sector Flag: %s\n"), - top_bottom[pri_vendor_tbl->top_bottom_sector_flag]); + urj_log (ll, _("\tTop/Bottom Sector Flag: %s\n"), + top_bottom[pri_vendor_tbl->top_bottom_sector_flag]); else - printf (_("\tTop/Bottom Sector Flag: %s\n"), bad_value); + urj_log (ll, _("\tTop/Bottom Sector Flag: %s\n"), bad_value); } if (major_version > '1' || (major_version == '1' && minor_version >= '2')) { if (pri_vendor_tbl->program_suspend < ARRAY_SIZE (supported_or_not)) - printf (_("\tProgram Suspend: %s\n"), - supported_or_not[pri_vendor_tbl->program_suspend]); + urj_log (ll, _("\tProgram Suspend: %s\n"), + supported_or_not[pri_vendor_tbl->program_suspend]); else - printf (_("\tProgram Suspend: %s\n"), bad_value); + urj_log (ll, _("\tProgram Suspend: %s\n"), + bad_value); } if (major_version > '1' || (major_version == '1' && minor_version >= '4')) { if (pri_vendor_tbl->unlock_bypass < ARRAY_SIZE (supported_or_not)) - printf (_("\tUnlock Bypass: %s\n"), - supported_or_not[pri_vendor_tbl->unlock_bypass]); + urj_log (ll, _("\tUnlock Bypass: %s\n"), + supported_or_not[pri_vendor_tbl->unlock_bypass]); else - printf (_("\tUnlock Bypass: %s\n"), bad_value); - printf (_("\tSecSi Sector (Customer OTP Area) Size: %d bytes\n"), - pri_vendor_tbl->secsi_sector_size); - printf (_("\tEmbedded Hardware Reset Timeout Maximum: %d ns\n"), - pri_vendor_tbl->embedded_hwrst_timeout_max); - printf (_ - ("\tNon-Embedded Hardware Reset Timeout Maximum: %d ns\n"), - pri_vendor_tbl->non_embedded_hwrst_timeout_max); - printf (_("\tErase Suspend Timeout Maximum: %d us\n"), - pri_vendor_tbl->erase_suspend_timeout_max); - printf (_("\tProgram Suspend Timeout Maximum: %d us\n"), - pri_vendor_tbl->program_suspend_timeout_max); + urj_log (ll, _("\tUnlock Bypass: %s\n"), bad_value); + urj_log (ll, _("\tSecSi Sector (Customer OTP Area) Size: %d bytes\n"), + pri_vendor_tbl->secsi_sector_size); + urj_log (ll, _("\tEmbedded Hardware Reset Timeout Maximum: %d ns\n"), + pri_vendor_tbl->embedded_hwrst_timeout_max); + urj_log (ll, _("\tNon-Embedded Hardware Reset Timeout Maximum: %d ns\n"), + pri_vendor_tbl->non_embedded_hwrst_timeout_max); + urj_log (ll, _("\tErase Suspend Timeout Maximum: %d us\n"), + pri_vendor_tbl->erase_suspend_timeout_max); + urj_log (ll, _("\tProgram Suspend Timeout Maximum: %d us\n"), + pri_vendor_tbl->program_suspend_timeout_max); } if ((major_version > '1' || (major_version == '1' && minor_version >= '3')) && pri_vendor_tbl->bank_organization) { - printf (_("\tBank Organization:\n")); + urj_log (ll, _("\tBank Organization:\n")); for (i = 0; i < pri_vendor_tbl->bank_organization; i++) - printf (_("\t\tBank%d: %d sectors\n"), i + 1, - pri_vendor_tbl->bank_region_info[i]); + urj_log (ll, _("\t\tBank%d: %d sectors\n"), + i + 1, pri_vendor_tbl->bank_region_info[i]); } } + + return URJ_STATUS_OK; } diff --git a/urjtag/src/flash/flash.c b/urjtag/src/flash/flash.c index 4fff8511..026cdffa 100644 --- a/urjtag/src/flash/flash.c +++ b/urjtag/src/flash/flash.c @@ -39,21 +39,16 @@ #include #include -#include "cfi.h" -#include "intel.h" - +#include +#include #include #include #include -/* @@@@ RFHH Put these into a local .h file, so the implementation can check */ -extern urj_flash_driver_t urj_flash_amd_32_flash_driver; -extern urj_flash_driver_t urj_flash_amd_16_flash_driver; -extern urj_flash_driver_t urj_flash_amd_8_flash_driver; -extern urj_flash_driver_t urj_flash_intel_32_flash_driver; -extern urj_flash_driver_t urj_flash_intel_16_flash_driver; -extern urj_flash_driver_t urj_flash_intel_8_flash_driver; -extern urj_flash_driver_t urj_flash_amd_29xx040_flash_driver; //20/09/2006 +#include "flash.h" +#include "cfi.h" +#include "intel.h" +#include "amd.h" urj_flash_driver_t *urj_flash_flash_drivers[] = { &urj_flash_amd_32_flash_driver, @@ -68,7 +63,7 @@ urj_flash_driver_t *urj_flash_flash_drivers[] = { static urj_flash_driver_t *flash_driver = NULL; -static void +static int set_flash_driver (void) { int i; @@ -76,25 +71,33 @@ set_flash_driver (void) flash_driver = NULL; if (urj_flash_cfi_array == NULL) - return; + { + urj_error_set (URJ_ERROR_NOTFOUND, "global cfi_array not set"); + return URJ_STATUS_FAIL; + } + cfi = &urj_flash_cfi_array->cfi_chips[0]->cfi; for (i = 0; urj_flash_flash_drivers[i] != NULL; i++) if (urj_flash_flash_drivers[i]->autodetect (urj_flash_cfi_array)) { flash_driver = urj_flash_flash_drivers[i]; - flash_driver->print_info (urj_flash_cfi_array); - return; + flash_driver->print_info (URJ_LOG_LEVEL_NORMAL, + urj_flash_cfi_array); + return URJ_STATUS_OK; } - printf (_("unknown flash - vendor id: %d (0x%04x)\n"), - cfi->identification_string.pri_id_code, - cfi->identification_string.pri_id_code); + urj_log (URJ_LOG_LEVEL_ERROR, + _("unknown flash - vendor id: %d (0x%04x)\n"), + cfi->identification_string.pri_id_code, + cfi->identification_string.pri_id_code); - printf (_("Flash not supported!\n")); + urj_error_set (URJ_ERROR_UNSUPPORTED, _("Flash not supported")); + + return URJ_STATUS_FAIL; } -void +int urj_flashmsbin (urj_bus_t *bus, FILE *f, int noverify) { uint32_t adr; @@ -103,20 +106,22 @@ urj_flashmsbin (urj_bus_t *bus, FILE *f, int noverify) set_flash_driver (); if (!urj_flash_cfi_array || !flash_driver) { - printf (_("no flash driver found\n")); - return; + urj_error_set (URJ_ERROR_NOTFOUND, _("no flash driver found\n")); + return URJ_STATUS_FAIL; } + cfi = &urj_flash_cfi_array->cfi_chips[0]->cfi; /* test sync bytes */ { char sync[8]; + // @@@@ RFHH check error state? fread (&sync, sizeof (char), 7, f); sync[7] = '\0'; if (strcmp ("B000FF\n", sync) != 0) { - printf (_("Invalid sync sequence!\n")); - return; + urj_error_set (URJ_ERROR_INVALID, _("Invalid sync sequence!\n")); + return URJ_STATUS_FAIL; } } @@ -128,21 +133,28 @@ urj_flashmsbin (urj_bus_t *bus, FILE *f, int noverify) uint32_t block_size = cfi->device_geometry.erase_block_regions[0].erase_block_size; + // @@@@ RFHH check error state? fread (&start, sizeof start, 1, f); + // @@@@ RFHH check error state? fread (&len, sizeof len, 1, f); first = start / (block_size * 2); last = (start + len - 1) / (block_size * 2); for (; first <= last; first++) { + int r; + adr = first * block_size * 2; - flash_driver->unlock_block (urj_flash_cfi_array, adr); - printf (_("block %d unlocked\n"), first); - printf (_("erasing block %d: %d\n"), first, - flash_driver->erase_block (urj_flash_cfi_array, adr)); + // @@@@ RFHH what about returning on error? + (void) flash_driver->unlock_block (urj_flash_cfi_array, adr); + urj_log (URJ_LOG_LEVEL_NORMAL, _("block %d unlocked\n"), first); + // @@@@ RFHH what about returning on error? + r = flash_driver->erase_block (urj_flash_cfi_array, adr); + urj_log (URJ_LOG_LEVEL_NORMAL, _("erasing block %d: %d\n"), + first, r); } } - printf (_("program:\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("program:\n")); for (;;) { uint32_t a, l, c; @@ -152,49 +164,48 @@ urj_flashmsbin (urj_bus_t *bus, FILE *f, int noverify) fread (&c, sizeof c, 1, f); if (feof (f)) { - printf (_("Error: premature end of file\n")); - return; + urj_error_set (URJ_ERROR_IO, _("premature end of file")); + return URJ_STATUS_FAIL; } - printf (_ - ("record: start = 0x%08X, len = 0x%08X, checksum = 0x%08X\n"), - a, l, c); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("record: start = 0x%08X, len = 0x%08X, checksum = 0x%08X\n"), + a, l, c); if ((a == 0) && (c == 0)) break; if (l & 3) { - printf (_("Error: Invalid record length!\n")); - return; + urj_error_set (URJ_ERROR_INVALID, _("Invalid record length")); + return URJ_STATUS_FAIL; } while (l) { uint32_t data; - printf (_("addr: 0x%08X"), a); - printf ("\r"); - fflush (stdout); + urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08X"), a); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + // @@@@ RFHH check error state fread (&data, sizeof data, 1, f); - if (flash_driver->program (urj_flash_cfi_array, a, &data, 1)) - { - printf (_("\nflash error\n")); - return; - } + if (flash_driver->program (urj_flash_cfi_array, a, &data, 1) + != URJ_STATUS_OK) + // retain error state + return URJ_STATUS_FAIL; a += 4; l -= 4; } } - printf ("\n"); + urj_log (URJ_LOG_LEVEL_NORMAL, "\n"); flash_driver->readarray (urj_flash_cfi_array); if (noverify) { - printf (_("verify skipped\n")); - return; + urj_log (URJ_LOG_LEVEL_NORMAL, _("verify skipped\n")); + return URJ_STATUS_OK; } fseek (f, 15, SEEK_SET); - printf (_("verify:\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("verify:\n")); for (;;) { @@ -205,41 +216,44 @@ urj_flashmsbin (urj_bus_t *bus, FILE *f, int noverify) fread (&c, sizeof c, 1, f); if (feof (f)) { - printf (_("Error: premature end of file\n")); - return; + urj_error_set (URJ_ERROR_IO, _("premature end of file")); + return URJ_STATUS_FAIL; } - printf (_ + urj_log (URJ_LOG_LEVEL_NORMAL, _ ("record: start = 0x%08X, len = 0x%08X, checksum = 0x%08X\n"), a, l, c); if ((a == 0) && (c == 0)) break; if (l & 3) { - printf (_("Error: Invalid record length!\n")); - return; + urj_error_set (URJ_ERROR_INVALID, _("Invalid record length!")); + return URJ_STATUS_FAIL; } while (l) { uint32_t data, readed; - printf (_("addr: 0x%08X"), a); - printf ("\r"); - fflush (stdout); + urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08X"), a); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + // @@@@ RFHH check error state? fread (&data, sizeof data, 1, f); readed = URJ_BUS_READ (bus, a); if (data != readed) { - printf (_("\nverify error: 0x%08X vs. 0x%08X at addr %08X\n"), - readed, data, a); - return; + urj_error_set (URJ_ERROR_FLASH_PROGRAM, + _("\nverify error: 0x%08X vs. 0x%08X at addr %08X\n"), + readed, data, a); + return URJ_STATUS_FAIL; } a += 4; l -= 4; } } - printf (_("\nDone.\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("\nDone.\n")); + + return URJ_STATUS_OK; } static int @@ -272,7 +286,7 @@ find_block (urj_flash_cfi_query_structure_t *cfi, int adr, int bus_width, return -1; } -void +int urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) { uint32_t adr; @@ -290,8 +304,8 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) set_flash_driver (); if (!urj_flash_cfi_array || !flash_driver) { - printf (_("no flash driver found\n")); - return; + urj_error_set (URJ_ERROR_NOTFOUND, _("no flash driver found")); + return URJ_STATUS_FAIL; } cfi = &urj_flash_cfi_array->cfi_chips[0]->cfi; @@ -306,13 +320,14 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) erased = malloc (neb * sizeof *erased); if (!erased) { - printf (_("Out of memory!\n")); - return; + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, _("malloc(%zd) failed"), + neb * sizeof *erased); + return URJ_STATUS_FAIL; } for (i = 0; i < neb; i++) erased[i] = 0; - printf (_("program:\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("program:\n")); adr = addr; while (!feof (f)) { @@ -327,14 +342,21 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) if (btr > BSIZE) btr = BSIZE; + // @@@@ RFHH check error state? bn = fread (b, 1, btr, f); if (bn > 0 && !erased[block_no]) { - flash_driver->unlock_block (urj_flash_cfi_array, adr); - printf (_("\nblock %d unlocked\n"), block_no); - printf (_("erasing block %d: %d\n"), block_no, - flash_driver->erase_block (urj_flash_cfi_array, adr)); + int r; + + // @@@@ RFHH what about returning on error? + (void) flash_driver->unlock_block (urj_flash_cfi_array, adr); + urj_log (URJ_LOG_LEVEL_NORMAL, _("\nblock %d unlocked\n"), + block_no); + // @@@@ RFHH what about returning on error? + r = flash_driver->erase_block (urj_flash_cfi_array, adr); + urj_log (URJ_LOG_LEVEL_NORMAL, _("erasing block %d: %d\n"), + block_no, r); erased[block_no] = 1; } @@ -343,9 +365,8 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) int j; if ((adr & (BSIZE - 1)) == 0) { - printf (_("addr: 0x%08X"), adr); - printf ("\r"); - fflush (stdout); + urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08X"), adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); } data = 0; @@ -365,26 +386,26 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) if (flash_driver->program (urj_flash_cfi_array, write_buffer_adr, write_buffer, write_buffer_count)) { - printf (_("\nflash error\n")); - return; + // retain error state + return URJ_STATUS_FAIL; } } free (erased); - printf (_("addr: 0x%08X\n"), adr - flash_driver->bus_width); + urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08X\n"), + adr - flash_driver->bus_width); flash_driver->readarray (urj_flash_cfi_array); if (noverify) { - printf (_("verify skipped\n")); - return; + urj_log (URJ_LOG_LEVEL_NORMAL, _("verify skipped\n")); + return URJ_STATUS_OK; } fseek (f, 0, SEEK_SET); - printf (_("verify:\n")); - fflush (stdout); + urj_log (URJ_LOG_LEVEL_NORMAL, _("verify:\n")); adr = addr; while (!feof (f)) { @@ -392,6 +413,7 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) uint8_t b[BSIZE]; int bc = 0, bn = 0, btr = BSIZE; + // @@@@ RFHH check error state? bn = fread (b, 1, btr, f); for (bc = 0; bc < bn; bc += flash_driver->bus_width) @@ -399,9 +421,8 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) int j; if ((adr & 0xFF) == 0) { - printf (_("addr: 0x%08X"), adr); - printf ("\r"); - fflush (stdout); + urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08X"), adr); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); } data = 0; @@ -414,87 +435,97 @@ urj_flashmem (urj_bus_t *bus, FILE *f, uint32_t addr, int noverify) readed = URJ_BUS_READ (bus, adr); if (data != readed) { - printf (_("addr: 0x%08X\n"), adr); - printf (_("verify error:\nread: 0x%08X\nexpected: 0x%08X\n"), - readed, data); - return; + urj_error_set (URJ_ERROR_FLASH_PROGRAM, + _("addr: 0x%08X\n verify error:\nread: 0x%08X\nexpected: 0x%08X\n"), + adr, readed, data); + return URJ_STATUS_FAIL; } adr += flash_driver->bus_width; } } - printf (_("addr: 0x%08X\nDone.\n"), adr - flash_driver->bus_width); + urj_log (URJ_LOG_LEVEL_NORMAL, _("addr: 0x%08X\nDone.\n"), + adr - flash_driver->bus_width); + + return URJ_STATUS_OK; } -void +int urj_flasherase (urj_bus_t *bus, uint32_t addr, int number) { urj_flash_cfi_query_structure_t *cfi; int i; - int status = 0; + int status = URJ_STATUS_OK; int bus_width; int chip_width; set_flash_driver (); if (!urj_flash_cfi_array || !flash_driver) { - printf (_("no flash driver found\n")); - return; + urj_error_set (URJ_ERROR_NOTFOUND, _("no flash driver found\n")); + return URJ_STATUS_FAIL; } cfi = &urj_flash_cfi_array->cfi_chips[0]->cfi; bus_width = urj_flash_cfi_array->bus_width; chip_width = urj_flash_cfi_array->cfi_chips[0]->width; - printf (_("\nErasing %d Flash block%s from address 0x%x\n"), number, - number > 1 ? "s" : "", addr); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("\nErasing %d Flash block%s from address 0x%x\n"), number, + number > 1 ? "s" : "", addr); for (i = 1; i <= number; i++) { + int r; int btr = 0; int block_no = find_block (cfi, addr - urj_flash_cfi_array->address, bus_width, chip_width, &btr); if (block_no < 0) { - status = URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_ERASE, "Cannot find block"); + status = URJ_STATUS_FAIL; break; } - printf (_("(%d%% Completed) FLASH Block %d : Unlocking ... "), + urj_log (URJ_LOG_LEVEL_NORMAL, + _("(%d%% Completed) FLASH Block %d : Unlocking ... "), i * 100 / number, block_no); - fflush (stdout); flash_driver->unlock_block (urj_flash_cfi_array, addr); - printf (_("Erasing ... ")); - fflush (stdout); - status = flash_driver->erase_block (urj_flash_cfi_array, addr); - if (status == 0) + urj_log (URJ_LOG_LEVEL_NORMAL, _("Erasing ... ")); + r = flash_driver->erase_block (urj_flash_cfi_array, addr); + if (r == URJ_STATUS_OK) { if (i == number) { - printf ("\r"); - printf (_ - ("(100%% Completed) FLASH Block %d : Unlocking ... Erasing ... Ok.\n"), - block_no); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("(100%% Completed) FLASH Block %d : Unlocking ... Erasing ... Ok.\n"), + block_no); } else { - printf (_("Ok.")); - printf ("\r"); - printf (_("%78s"), ""); - printf ("\r"); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Ok.")); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); + urj_log (URJ_LOG_LEVEL_NORMAL, _("%78s"), ""); + urj_log (URJ_LOG_LEVEL_NORMAL, "\r"); } } else - printf (_("ERROR.\n")); + { + urj_log (URJ_LOG_LEVEL_NORMAL, _("ERROR.\n")); + status = r; + } addr += btr; } - if (status == 0) - printf (_("\nErasing Completed.\n")); + if (status == URJ_STATUS_OK) + urj_log (URJ_LOG_LEVEL_NORMAL, _("\nErasing Completed.\n")); else - printf (_("\nErasing Failed.\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("\nErasing (partially) Failed.\n")); /* BYPASS */ // urj_part_parts_set_instruction( ps, "BYPASS" ); // urj_tap_chain_shift_instructions( chain ); + + return status; } diff --git a/urjtag/src/flash/flash.h b/urjtag/src/flash/flash.h new file mode 100644 index 00000000..33183231 --- /dev/null +++ b/urjtag/src/flash/flash.h @@ -0,0 +1,60 @@ +/* + * $Id: flash.h 1554 2009-05-03 16:06:22Z rfhh $ + * + * Copyright (C) 2003 ETC s.r.o. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the ETC s.r.o. nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Written by Marcel Telka , 2003. + * + */ + +#ifndef URJ_SRC_FLASH_H +#define URJ_SRC_FLASH_H + +#include + +#include +#include + +#include +#include + +typedef struct urj_flash_cfi_chip urj_flash_cfi_chip_t; + +struct urj_flash_cfi_array +{ + urj_bus_t *bus; + uint32_t address; + int bus_width; /* in cfi_chips, e.g. 4 for 32 bits */ + urj_flash_cfi_chip_t **cfi_chips; +}; + +/** @return URJ_STATUS_OK on success; URJ_STATUS_FAIL on error */ +int urj_flash_detectflash (urj_log_level_t ll, urj_bus_t *bus, uint32_t adr); + +extern urj_flash_cfi_array_t *urj_flash_cfi_array; + +#endif /* URJ_FLASH_H */ diff --git a/urjtag/src/flash/intel.c b/urjtag/src/flash/intel.c index ca106e84..1ca61296 100644 --- a/urjtag/src/flash/intel.c +++ b/urjtag/src/flash/intel.c @@ -41,9 +41,13 @@ #include #include +#include +#include #include #include +#include "flash.h" + #include "cfi.h" #include "intel.h" #include "mic.h" @@ -110,7 +114,8 @@ intel_flash_autodetect8 (urj_flash_cfi_array_t *cfi_array) } static void -_intel_flash_print_info (urj_flash_cfi_array_t *cfi_array, int o) +_intel_flash_print_info (urj_log_level_t ll, urj_flash_cfi_array_t *cfi_array, + int o) { uint32_t mid, cid; urj_bus_t *bus = cfi_array->bus; @@ -119,73 +124,73 @@ _intel_flash_print_info (urj_flash_cfi_array_t *cfi_array, int o) switch (mid) { case STD_MIC_INTEL: - printf (_("Manufacturer: %s\n"), STD_MICN_INTEL); + urj_log (ll, _("Manufacturer: %s\n"), STD_MICN_INTEL); break; case STD_MIC_MITSUBISHI: - printf (_("Manufacturer: %s\n"), STD_MICN_MITSUBISHI); + urj_log (ll, _("Manufacturer: %s\n"), STD_MICN_MITSUBISHI); break; case STD_MIC_MICRON_TECHNOLOGY: - printf (_("Manufacturer: %s\n"), STD_MICN_MICRON_TECHNOLOGY); + urj_log (ll, _("Manufacturer: %s\n"), STD_MICN_MICRON_TECHNOLOGY); break; default: - printf (_("Unknown manufacturer (0x%04X)!\n"), mid); + urj_log (ll, _("Unknown manufacturer (0x%04X)!\n"), mid); break; } - printf (_("Chip: ")); + urj_log (ll, _("Chip: ")); cid = (URJ_BUS_READ (bus, cfi_array->address + (0x01 << o)) & 0xFFFF); switch (cid) { case 0x0016: - printf ("28F320J3A\n"); + urj_log (ll, "28F320J3A\n"); break; case 0x0017: - printf ("28F640J3A\n"); + urj_log (ll, "28F640J3A\n"); break; case 0x0018: - printf ("28F128J3A\n"); + urj_log (ll, "28F128J3A\n"); break; case 0x001D: - printf ("28F256J3A\n"); + urj_log (ll, "28F256J3A\n"); break; case 0x8801: - printf ("28F640K3\n"); + urj_log (ll, "28F640K3\n"); break; case 0x8802: - printf ("28F128K3\n"); + urj_log (ll, "28F128K3\n"); break; case 0x8803: - printf ("28F256K3\n"); + urj_log (ll, "28F256K3\n"); break; case 0x8805: - printf ("28F640K18\n"); + urj_log (ll, "28F640K18\n"); break; case 0x8806: - printf ("28F128K18\n"); + urj_log (ll, "28F128K18\n"); break; case 0x8807: - printf ("28F256K18\n"); + urj_log (ll, "28F256K18\n"); break; case 0x880B: - printf ("GE28F640L18T\n"); + urj_log (ll, "GE28F640L18T\n"); break; case 0x880C: - printf ("GE28F128L18T\n"); + urj_log (ll, "GE28F128L18T\n"); break; case 0x880D: - printf ("GE28F256L18T\n"); + urj_log (ll, "GE28F256L18T\n"); break; case 0x880E: - printf ("GE28F640L18B\n"); + urj_log (ll, "GE28F640L18B\n"); break; case 0x880F: - printf ("GE28F128L18B\n"); + urj_log (ll, "GE28F128L18B\n"); break; case 0x8810: - printf ("GE28F256L18B\n"); + urj_log (ll, "GE28F256L18B\n"); break; default: - printf (_("Unknown (0x%02X)!\n"), cid); + urj_log (ll, _("Unknown (0x%02X)!\n"), cid); break; } @@ -194,7 +199,7 @@ _intel_flash_print_info (urj_flash_cfi_array_t *cfi_array, int o) } static void -intel_flash_print_info (urj_flash_cfi_array_t *cfi_array) +intel_flash_print_info (urj_log_level_t ll, urj_flash_cfi_array_t *cfi_array) { int o = 1; urj_bus_t *bus = cfi_array->bus; @@ -208,11 +213,11 @@ intel_flash_print_info (urj_flash_cfi_array_t *cfi_array) /* Read Identifier Command */ URJ_BUS_WRITE (bus, cfi_array->address + (0 << 0), 0x0090); - _intel_flash_print_info (cfi_array, o); + _intel_flash_print_info (ll, cfi_array, o); } static void -intel_flash_print_info32 (urj_flash_cfi_array_t *cfi_array) +intel_flash_print_info32 (urj_log_level_t ll, urj_flash_cfi_array_t *cfi_array) { int o = 2; urj_bus_t *bus = cfi_array->bus; @@ -225,7 +230,7 @@ intel_flash_print_info32 (urj_flash_cfi_array_t *cfi_array) /* Read Identifier Command */ URJ_BUS_WRITE (bus, cfi_array->address + (0 << 0), 0x00900090); - _intel_flash_print_info (cfi_array, o); + _intel_flash_print_info (ll, cfi_array, o); } static int @@ -244,21 +249,22 @@ intel_flash_erase_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) switch (sr & ~CFI_INTEL_SR_READY) { case 0: - return 0; + return URJ_STATUS_OK; case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_PROGRAM_ERROR: - printf (_("flash: invalid command seq\n")); - return URJ_FLASH_ERROR_INVALID_COMMAND_SEQUENCE; + urj_error_set (URJ_ERROR_FLASH_ERASE, _("invalid command seq\n")); + return URJ_STATUS_FAIL; case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_VPEN_ERROR: - printf (_("flash: low vpen\n")); - return URJ_FLASH_ERROR_LOW_VPEN; + urj_error_set (URJ_ERROR_FLASH_ERASE, _("low vpen\n")); + return URJ_STATUS_FAIL; case CFI_INTEL_SR_ERASE_ERROR | CFI_INTEL_SR_BLOCK_LOCKED: - printf (_("flash: block locked\n")); - return URJ_FLASH_ERROR_BLOCK_LOCKED; + urj_error_set (URJ_ERROR_FLASH_ERASE, _("block locked\n")); + return URJ_STATUS_FAIL; default: break; } - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH, "unknown error"); + return URJ_STATUS_FAIL; } static int @@ -276,11 +282,12 @@ intel_flash_unlock_block (urj_flash_cfi_array_t *cfi_array, uint32_t adr) if (sr != CFI_INTEL_SR_READY) { - printf (_("flash: unknown error while unblocking\n")); - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_UNLOCK, + _("unknown error while unlocking block\n")); + return URJ_STATUS_FAIL; } - else - return 0; + + return URJ_STATUS_OK; } static int @@ -299,11 +306,12 @@ intel_flash_program_single (urj_flash_cfi_array_t *cfi_array, if (sr != CFI_INTEL_SR_READY) { - printf (_("flash: unknown error while programming\n")); - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_PROGRAM, + _("unknown error while programming\n")); + return URJ_STATUS_FAIL; } - else - return 0; + + return URJ_STATUS_OK; } static int @@ -357,11 +365,12 @@ intel_flash_program_buffer (urj_flash_cfi_array_t *cfi_array, while (!((sr = URJ_BUS_READ (bus, cfi_array->address) & 0xFE) & CFI_INTEL_SR_READY)); /* TODO: add timeout */ if (sr != CFI_INTEL_SR_READY) { - printf (_("flash: unknown error while programming\n")); - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_PROGRAM, + _("unknown error while programming\n")); + return URJ_STATUS_FAIL; } - return 0; + return URJ_STATUS_OK; } static int @@ -388,13 +397,13 @@ intel_flash_program (urj_flash_cfi_array_t *cfi_array, { int status = intel_flash_program_single (cfi_array, adr, buffer[idx]); - if (status) + if (status != URJ_STATUS_OK) return status; adr += cfi_array->bus_width; } } - return 0; + return URJ_STATUS_OK; } static int @@ -416,11 +425,11 @@ intel_flash_erase_block32 (urj_flash_cfi_array_t *cfi_array, uint32_t adr) if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { - printf ("\nsr = 0x%08X\n", sr); - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_ERASE, "sr = 0x%08X", sr); + return URJ_STATUS_FAIL; } - else - return 0; + + return URJ_STATUS_OK; } static int @@ -444,11 +453,11 @@ intel_flash_unlock_block32 (urj_flash_cfi_array_t *cfi_array, if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { - printf ("\nsr = 0x%08X\n", sr); - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_UNLOCK, "sr = 0x%08X", sr); + return URJ_STATUS_FAIL; } - else - return 0; + + return URJ_STATUS_OK; } static int @@ -469,11 +478,11 @@ intel_flash_program32_single (urj_flash_cfi_array_t *cfi_array, if (sr != ((CFI_INTEL_SR_READY << 16) | CFI_INTEL_SR_READY)) { - printf ("\nsr = 0x%08X\n", sr); - return URJ_FLASH_ERROR_UNKNOWN; + urj_error_set (URJ_ERROR_FLASH_PROGRAM, "\nsr = 0x%08X\n", sr); + return URJ_STATUS_FAIL; } - else - return 0; + + return URJ_STATUS_OK; } static int @@ -490,12 +499,12 @@ intel_flash_program32 (urj_flash_cfi_array_t *cfi_array, for (idx = 0; idx < count; idx++) { int status = intel_flash_program32_single (cfi_array, adr, buffer[idx]); - if (status) + if (status != URJ_STATUS_OK) return status; adr += cfi_array->bus_width; } - return 0; + return URJ_STATUS_OK; } static void diff --git a/urjtag/src/flash/intel.h b/urjtag/src/flash/intel.h index f5fcc9d4..906c3a0e 100644 --- a/urjtag/src/flash/intel.h +++ b/urjtag/src/flash/intel.h @@ -41,6 +41,7 @@ #define FLASH_INTEL_H #include +#include /* Intel CFI commands - see Table 4. in [1] and Table 3. in [2] */ @@ -97,4 +98,8 @@ #define CFI_CHIP_INTEL_28F256K18 0x8807 #define CFI_CHIPN_INTEL_28F256K18 "28F256K18" +extern urj_flash_driver_t urj_flash_intel_32_flash_driver; +extern urj_flash_driver_t urj_flash_intel_16_flash_driver; +extern urj_flash_driver_t urj_flash_intel_8_flash_driver; + #endif /* FLASH_INTEL_H */ diff --git a/urjtag/src/flash/jedec.c b/urjtag/src/flash/jedec.c index f4d5152f..12f7d707 100644 --- a/urjtag/src/flash/jedec.c +++ b/urjtag/src/flash/jedec.c @@ -28,9 +28,13 @@ #include #include +#include +#include #include #include +#include "flash.h" + #include "cfi.h" #include "intel.h" #include "jedec.h" @@ -408,25 +412,40 @@ urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, *cfi_array = calloc (1, sizeof (urj_flash_cfi_array_t)); if (!*cfi_array) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_array_t)); + return URJ_STATUS_FAIL; + } (*cfi_array)->bus = bus; (*cfi_array)->address = adr; if (URJ_BUS_AREA (bus, adr, &area) != URJ_STATUS_OK) - return -8; /* bus width detection failed */ + // retain error state + return URJ_STATUS_FAIL; bw = area.width; if (bw != 8 && bw != 16 && bw != 32) - return -3; /* invalid bus width */ + { + urj_error_set (URJ_ERROR_INVALID, "bus width %d", bw); + return URJ_STATUS_FAIL; + } (*cfi_array)->bus_width = ba = bw / 8; - (*cfi_array)->cfi_chips = - calloc (1, sizeof (urj_flash_cfi_chip_t *) * ba); + (*cfi_array)->cfi_chips = calloc (1, sizeof (urj_flash_cfi_chip_t *) * ba); if (!(*cfi_array)->cfi_chips) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_chip_t *) * ba); + return URJ_STATUS_FAIL; + } (*cfi_array)->cfi_chips[0] = calloc (1, sizeof (urj_flash_cfi_chip_t)); if (!(*cfi_array)->cfi_chips[0]) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_chip_t)); + return URJ_STATUS_FAIL; + } /* probe device with Autoselect method 1 */ URJ_BUS_WRITE (bus, adr, 0xf0); @@ -457,10 +476,13 @@ urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, if (manid == table[i].mfr_id && devid == table[i].dev_id) break; } - fprintf (stderr, "dev ID=%04x man ID=%04x\n", devid, manid); + urj_log (URJ_LOG_LEVEL_NORMAL, "dev ID=%04x man ID=%04x\n", devid, manid); if (i == sizeof (table) / sizeof (struct amd_flash_info)) - return -4; + { + urj_error_set (URJ_ERROR_NOTFOUND, "amd_flash_info table"); + return URJ_STATUS_FAIL; + } cfi = &(*cfi_array)->cfi_chips[0]->cfi; @@ -481,9 +503,8 @@ urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, (*cfi_array)->cfi_chips[0]->width = 2; break; case CFI_INTERFACE_X8_X16: - fprintf (stderr, - "Warning: Unsupported interface geometry %s, falling back to %s\n", - "CFI_INTERFACE_X8_X16", "CFI_INTERFACE_X16"); + urj_warning ("Unsupported interface geometry %s, falling back to %s\n", + "CFI_INTERFACE_X8_X16", "CFI_INTERFACE_X16"); (*cfi_array)->cfi_chips[0]->width = 2; cfi->device_geometry.device_interface = CFI_INTERFACE_X16; break; @@ -491,21 +512,18 @@ urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, (*cfi_array)->cfi_chips[0]->width = 4; break; case CFI_INTERFACE_X16_X32: - fprintf (stderr, - "Warning: Unsupported interface geometry %s, falling back to %s\n", - "CFI_INTERFACE_X16_X32", "CFI_INTERFACE_X32"); + urj_warning ("Unsupported interface geometry %s, falling back to %s\n", + "CFI_INTERFACE_X16_X32", "CFI_INTERFACE_X32"); (*cfi_array)->cfi_chips[0]->width = 4; cfi->device_geometry.device_interface = CFI_INTERFACE_X32; break; default: /* unsupported interface geometry */ - fprintf (stderr, - "Error: Unsupported interface geometry %d, bailing out\n", - table[i].interface_width); (*cfi_array)->cfi_chips[0]->width = 1; cfi->device_geometry.device_interface = CFI_INTERFACE_X8; - return -5; - break; + urj_error_set (URJ_ERROR_UNSUPPORTED, + "interface geometry %d", table[i].interface_width); + return URJ_STATUS_FAIL; } cfi->device_geometry.number_of_erase_regions = table[i].numeraseregions; @@ -514,7 +532,12 @@ urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, malloc (cfi->device_geometry.number_of_erase_regions * sizeof (urj_flash_cfi_erase_block_region_t)); if (!cfi->device_geometry.erase_block_regions) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + cfi->device_geometry.number_of_erase_regions + * sizeof (urj_flash_cfi_erase_block_region_t)); + return URJ_STATUS_FAIL; + } for (j = 0; j < cfi->device_geometry.number_of_erase_regions; j++) { @@ -524,8 +547,9 @@ urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, table[i].regions[j].numblocks; } - fprintf (stderr, "Found %s flash, size = %li bytes.\n", table[i].name, + urj_log (URJ_LOG_LEVEL_NORMAL, + "Found %s flash, size = %li bytes.\n", table[i].name, table[i].size); - return 0; + return URJ_STATUS_OK; } diff --git a/urjtag/src/flash/jedec.h b/urjtag/src/flash/jedec.h index 32caa8c5..ec0f19f2 100644 --- a/urjtag/src/flash/jedec.h +++ b/urjtag/src/flash/jedec.h @@ -25,7 +25,7 @@ #ifndef URJ_FLASH_JEDEC_H #define URJ_FLASH_JEDEC_H -#include +#include #include int urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, @@ -34,7 +34,5 @@ int urj_flash_jedec_detect (urj_bus_t *bus, uint32_t adr, int urj_flash_jedec_exp_detect (urj_bus_t *bus, uint32_t adr, urj_flash_cfi_array_t **urj_flash_cfi_array); #endif -int urj_flash_amd_detect (urj_bus_t *bus, uint32_t adr, - urj_flash_cfi_array_t **urj_flash_cfi_array); #endif /* ndef URJ_FLASH_JEDEC_H */ diff --git a/urjtag/src/flash/jedec_exp.c b/urjtag/src/flash/jedec_exp.c index cf5cbe61..a8d9379c 100644 --- a/urjtag/src/flash/jedec_exp.c +++ b/urjtag/src/flash/jedec_exp.c @@ -28,10 +28,14 @@ #include #include +#include +#include #include #include #include +#include "flash.h" + #include "jedec.h" #include "cfi.h" #include "intel.h" @@ -45,19 +49,20 @@ urj_flash_jedec_exp_read_id (urj_bus_t *bus, uint32_t adr, uint32_t dmask, int locofs; det_addrpat <<= det_addroffset; - printf (" trying with address pattern base %08x:", det_addrpat); + urj_log (URJ_LOG_LEVEL_NORMAL, + " trying with address pattern base %08x:", det_addrpat); URJ_BUS_WRITE (bus, adr + det_addrpat, pata); URJ_BUS_WRITE (bus, adr + (det_addrpat >> 1), patb); URJ_BUS_WRITE (bus, adr + det_addrpat, dcmd); for (locofs = 0; locofs <= 2; locofs++) { - printf (" %08x", - (dmask & URJ_BUS_READ (bus, adr + (locofs << det_addroffset))) - >> det_dataoffset); + urj_log (URJ_LOG_LEVEL_NORMAL, " %08x", + (dmask & URJ_BUS_READ (bus, adr + (locofs << det_addroffset))) + >> det_dataoffset); } - printf ("\n"); + urj_log (URJ_LOG_LEVEL_NORMAL, "\n"); } int @@ -72,42 +77,58 @@ urj_flash_jedec_exp_detect (urj_bus_t *bus, uint32_t adr, *cfi_array = calloc (1, sizeof (urj_flash_cfi_array_t)); if (!*cfi_array) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_array_t)); + return URJ_STATUS_FAIL; + } (*cfi_array)->bus = bus; (*cfi_array)->address = adr; if (URJ_BUS_AREA (bus, adr, &area) != URJ_STATUS_OK) - return -8; /* bus width detection failed */ + // retain error state + return URJ_STATUS_FAIL; bw = area.width; if (bw == 0) bw = 32; // autodetection! if (bw != 8 && bw != 16 && bw != 32) - return -3; /* invalid bus width */ + { + urj_error_set (URJ_ERROR_INVALID, "bus width %d", bw); + return URJ_STATUS_FAIL; + } (*cfi_array)->bus_width = ba = bw / 8; - (*cfi_array)->cfi_chips = - calloc (1, sizeof (urj_flash_cfi_chip_t *)); + (*cfi_array)->cfi_chips = calloc (1, sizeof (urj_flash_cfi_chip_t *)); if (!(*cfi_array)->cfi_chips) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_chip_t *)); + return URJ_STATUS_FAIL; + } - (*cfi_array)->cfi_chips[0] = - calloc (1, sizeof (urj_flash_cfi_chip_t)); + (*cfi_array)->cfi_chips[0] = calloc (1, sizeof (urj_flash_cfi_chip_t)); if (!(*cfi_array)->cfi_chips[0]) - return -2; /* out of memory */ + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + 1, sizeof (urj_flash_cfi_chip_t)); + return URJ_STATUS_FAIL; + } - printf - ("=== experimental extensive JEDEC brute-force autodetection ===\n"); + urj_log (URJ_LOG_LEVEL_NORMAL, + "=== experimental extensive JEDEC brute-force autodetection ===\n"); for (det_buswidth = bw; det_buswidth >= 8; det_buswidth >>= 1) { int det_datawidth; - printf ("- trying with cpu buswidth %d\n", det_buswidth); + urj_log (URJ_LOG_LEVEL_NORMAL, "- trying with cpu buswidth %d\n", + det_buswidth); for (det_datawidth = det_buswidth; det_datawidth >= 8; det_datawidth >>= 1) { int det_dataoffset; - printf ("-- trying with flash datawidth %d\n", det_datawidth); + urj_log (URJ_LOG_LEVEL_NORMAL, + "-- trying with flash datawidth %d\n", det_datawidth); for (det_dataoffset = 0; det_dataoffset + det_datawidth <= det_buswidth; det_dataoffset += 8) @@ -120,9 +141,10 @@ urj_flash_jedec_exp_detect (urj_bus_t *bus, uint32_t adr, uint32_t patb = ~dmask | (0x55 << det_dataoffset); uint32_t dcmd = ~dmask | (0x90 << det_dataoffset); - printf ("--- trying with flash dataoffset %d", - det_dataoffset); - printf (" (using %08X, %08X and %08X)\n", pata, patb, dcmd); + urj_log (URJ_LOG_LEVEL_NORMAL, + "--- trying with flash dataoffset %d", det_dataoffset); + urj_log (URJ_LOG_LEVEL_NORMAL, " (using %08X, %08X and %08X)\n", + pata, patb, dcmd); for (det_addroffset = 0; det_addroffset <= 2; det_addroffset++) @@ -137,8 +159,8 @@ urj_flash_jedec_exp_detect (urj_bus_t *bus, uint32_t adr, } } } - printf - ("=== end of experimental extensive JEDEC brute-force autodetection ===\n"); + urj_log (URJ_LOG_LEVEL_NORMAL, + "=== end of experimental extensive JEDEC brute-force autodetection ===\n"); - return 1; + return URJ_STATUS_OK; } diff --git a/urjtag/src/global/parse.c b/urjtag/src/global/parse.c index 6dc5507c..68ea580b 100644 --- a/urjtag/src/global/parse.c +++ b/urjtag/src/global/parse.c @@ -153,9 +153,8 @@ urj_parse_stream (urj_chain_t *chain, FILE *f) inputline[i] = '\0'; lnr++; if (clip && !found_comment) - urj_log (URJ_LOG_LEVEL_WARNINGS, - "line %d exceeds %zd characters, clipped\n", lnr, - sizeof (inputline) - 1); + urj_warning ("line %d exceeds %zd characters, clipped\n", lnr, + sizeof (inputline) - 1); go = urj_parse_line (chain, inputline); urj_tap_chain_flush (chain); } diff --git a/urjtag/src/global/urjtag.c b/urjtag/src/global/urjtag.c index 3c30d91e..ff8a16a3 100644 --- a/urjtag/src/global/urjtag.c +++ b/urjtag/src/global/urjtag.c @@ -34,11 +34,12 @@ int urj_debug_mode = 0; int urj_big_endian = 0; static int stderr_vprintf (const char *fmt, va_list ap); +static int stdout_vprintf (const char *fmt, va_list ap); urj_log_state_t urj_log_state = { .level = URJ_LOG_LEVEL_NORMAL, - .out_vprintf = vprintf, + .out_vprintf = stdout_vprintf, .err_vprintf = stderr_vprintf, }; @@ -48,6 +49,16 @@ stderr_vprintf(const char *fmt, va_list ap) return vfprintf (stderr, fmt, ap); } +static int +stdout_vprintf(const char *fmt, va_list ap) +{ + int r = vfprintf (stdout, fmt, ap); + + fflush (stdout); + + return r; +} + int urj_log (urj_log_level_t level, const char *fmt, ...) { @@ -58,7 +69,7 @@ urj_log (urj_log_level_t level, const char *fmt, ...) return 0; va_start (ap, fmt); - if (level < URJ_LOG_LEVEL_WARNINGS) + if (level < URJ_LOG_LEVEL_WARNING) r = urj_log_state.out_vprintf (fmt, ap); else r = urj_log_state.err_vprintf (fmt, ap); @@ -95,13 +106,24 @@ urj_error_string (urj_error_t err) case URJ_ERROR_OUT_OF_MEMORY: return "out of memory"; case URJ_ERROR_NO_CHAIN: return "no chain"; case URJ_ERROR_NO_ACTIVE_PART: return "no active part"; + case URJ_ERROR_NO_ACTIVE_INSTRUCTION: return "no active instruction"; + case URJ_ERROR_NO_DATA_REGISTER: return "no data register"; case URJ_ERROR_INVALID: return "invalid parameter"; case URJ_ERROR_NOTFOUND: return "not found"; - case URJ_ERROR_IO: return "I/O error from OS"; case URJ_ERROR_NO_BUS_DRIVER: return "no bus driver"; case URJ_ERROR_BUFFER_EXHAUSTED: return "buffer exhausted"; case URJ_ERROR_ILLEGAL_STATE: return "illegal state transition"; case URJ_ERROR_OUT_OF_BOUNDS: return "out of bounds"; + case URJ_ERROR_UNSUPPORTED: return "unsupported"; + case URJ_ERROR_SYNTAX: return "syntax"; + + case URJ_ERROR_IO: return "I/O error from OS"; + + case URJ_ERROR_FLASH: return "flash"; + case URJ_ERROR_FLASH_DETECT: return "flash detect"; + case URJ_ERROR_FLASH_PROGRAM: return "flash program"; + case URJ_ERROR_FLASH_ERASE: return "flash erase"; + case URJ_ERROR_FLASH_UNLOCK: return "flash unlock"; } return "UNDEFINED ERROR"; diff --git a/urjtag/src/part/data_register.c b/urjtag/src/part/data_register.c index dd6fb39b..103cb431 100644 --- a/urjtag/src/part/data_register.c +++ b/urjtag/src/part/data_register.c @@ -50,8 +50,7 @@ urj_part_data_register_alloc (const char *name, int len) } if (strlen (name) > URJ_DATA_REGISTER_MAXLEN) - urj_log (URJ_LOG_LEVEL_WARNINGS, - _("Warning: Data register name too long\n")); + urj_warning (_("Data register name too long\n")); strncpy (dr->name, name, URJ_DATA_REGISTER_MAXLEN); dr->name[URJ_DATA_REGISTER_MAXLEN] = '\0'; diff --git a/urjtag/src/part/instruction.c b/urjtag/src/part/instruction.c index 805d0bcd..7a61d18d 100644 --- a/urjtag/src/part/instruction.c +++ b/urjtag/src/part/instruction.c @@ -48,8 +48,7 @@ urj_part_instruction_alloc (const char *name, int len, const char *val) } if (strlen (name) > URJ_INSTRUCTION_MAXLEN_INSTRUCTION) - urj_log (URJ_LOG_LEVEL_WARNINGS, - _("Warning: Instruction name too long\n")); + urj_warning (_("Instruction name too long\n")); strncpy (i->name, name, URJ_INSTRUCTION_MAXLEN_INSTRUCTION); i->name[URJ_INSTRUCTION_MAXLEN_INSTRUCTION] = '\0'; diff --git a/urjtag/src/svf/svf.c b/urjtag/src/svf/svf.c index cab2a904..634a4d0d 100644 --- a/urjtag/src/svf/svf.c +++ b/urjtag/src/svf/svf.c @@ -630,9 +630,8 @@ int urj_svf_hxr (enum generic_irdr_coding ir_dr, struct ths_params *params) { if (params->number != 0.0) - urj_log (URJ_LOG_LEVEL_WARNINGS, - _("Warning %s: command %s not implemented\n"), "svf", - ir_dr == generic_ir ? "HIR" : "HDR"); + urj_warning ( _("command %s not implemented\n"), + ir_dr == generic_ir ? "HIR" : "HDR"); return (1); } @@ -682,11 +681,9 @@ urj_svf_runtest (urj_chain_t *chain, urj_svf_parser_priv_t *priv, if (params->max_time > 0.0) if (!priv->issued_runtest_maxtime) { - urj_log (URJ_LOG_LEVEL_WARNINGS, - _("Warning %s: maximum time for RUNTEST not guaranteed.\n"), - "svf"); - urj_log (URJ_LOG_LEVEL_WARNINGS, - _(" This message is only displayed once.\n")); + urj_warning ("%s%s", + _("maximum time for RUNTEST not guaranteed.\n"), + _(" This message is only displayed once.\n")); priv->issued_runtest_maxtime = 1; } @@ -721,7 +718,7 @@ urj_svf_runtest (urj_chain_t *chain, urj_svf_parser_priv_t *priv, urj_error_set (URJ_ERROR_OUT_OF_BOUNDS, _("Error %s: Maximum cable clock frequency required for RUNTEST.\n"), "svf"); - urj_log (URJ_LOG_LEVEL_ERRORS, + urj_log (URJ_LOG_LEVEL_ERROR, _(" Set the cable frequency with 'FREQUENCY '.\n")); return 0; } @@ -886,7 +883,7 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, { if (!params->tdi) { - urj_log (URJ_LOG_LEVEL_ERRORS, + urj_log (URJ_LOG_LEVEL_ERROR, _("Error %s: first %s command after length change must have a TDI value.\n"), "svf", ir_dr == generic_ir ? "SIR" : "SDR"); result = 0; @@ -914,11 +911,11 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, /* is SIR large enough? */ if (priv->ir->value->len != len) { - urj_log (URJ_LOG_LEVEL_ERRORS, + urj_log (URJ_LOG_LEVEL_ERROR, _("Error %s: SIR command length inconsistent.\n"), "svf"); if (loc != NULL) { - urj_log (URJ_LOG_LEVEL_ERRORS, + urj_log (URJ_LOG_LEVEL_ERROR, _(" in input file between line %d col %d and line %d col %d\n"), loc->first_line + 1, loc->first_column + 1, loc->last_line + 1, loc->last_column + 1); @@ -960,20 +957,21 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, { case generic_ir: urj_svf_goto_state (chain, URJ_TAP_STATE_SHIFT_IR); + /* @@@@ RFHH check result */ urj_tap_chain_shift_instructions_mode (chain, sxr_params->params.tdo ? 1 : 0, 0, URJ_CHAIN_EXITMODE_EXIT1); urj_svf_goto_state (chain, priv->endir); if (sxr_params->params.tdo) - result = - urj_svf_compare_tdo (priv, sxr_params->params.tdo, - sxr_params->params.mask, priv->ir->out, - loc); + result = urj_svf_compare_tdo (priv, sxr_params->params.tdo, + sxr_params->params.mask, + priv->ir->out, loc); break; case generic_dr: urj_svf_goto_state (chain, URJ_TAP_STATE_SHIFT_DR); + /* @@@@ RFHH check result */ urj_tap_chain_shift_data_registers_mode (chain, sxr_params->params. tdo ? 1 : 0, 0, @@ -981,10 +979,9 @@ urj_svf_sxr (urj_chain_t *chain, urj_svf_parser_priv_t *priv, urj_svf_goto_state (chain, priv->enddr); if (sxr_params->params.tdo) - result = - urj_svf_compare_tdo (priv, sxr_params->params.tdo, - sxr_params->params.mask, priv->dr->out, - loc); + result = urj_svf_compare_tdo (priv, sxr_params->params.tdo, + sxr_params->params.mask, + priv->dr->out, loc); break; } @@ -1063,9 +1060,8 @@ urj_svf_trst (urj_chain_t *chain, urj_svf_parser_priv_t *priv, int trst_mode) } if (trst_cable < 0) - urj_log (URJ_LOG_LEVEL_WARNINGS, - _("Warning %s: unimplemented mode '%s' for TRST\n"), "svf", - unimplemented_mode); + urj_warning (_("unimplemented mode '%s' for TRST\n"), + unimplemented_mode); else urj_tap_cable_set_signal (chain->cable, URJ_POD_CS_TRST, trst_cable ? URJ_POD_CS_TRST : 0); @@ -1094,9 +1090,8 @@ int urj_svf_txr (enum generic_irdr_coding ir_dr, struct ths_params *params) { if (params->number != 0.0) - urj_log (URJ_LOG_LEVEL_WARNINGS, - _("Warning %s: command %s not implemented\n"), "svf", - ir_dr == generic_ir ? "TIR" : "TDR"); + urj_warning (_("command %s not implemented\n"), + ir_dr == generic_ir ? "TIR" : "TDR"); return (1); } diff --git a/urjtag/src/svf/svf_bison.y b/urjtag/src/svf/svf_bison.y index e251dc48..dbb7f3e0 100644 --- a/urjtag/src/svf/svf_bison.y +++ b/urjtag/src/svf/svf_bison.y @@ -43,6 +43,8 @@ #define YYLEX_PARAM priv_data->scanner int yylex (YYSTYPE *, YYLTYPE *, void *); +// @@@@ RFHH need to define YYPRINTF in terms of urj_log() + #define YYERROR_VERBOSE @@ -138,7 +140,7 @@ svf_statement | PIOMAP '(' direction IDENTIFIER piomap_rec ')' ';' { - urj_log (URJ_LOG_LEVEL_ERRORS, "PIOMAP not implemented\n"); + urj_log (URJ_LOG_LEVEL_ERROR, "PIOMAP not implemented\n"); yyerror(&@$, priv_data, chain, "PIOMAP"); YYERROR; } @@ -146,7 +148,7 @@ svf_statement | PIO VECTOR_STRING ';' { free($2); - urj_log (URJ_LOG_LEVEL_ERRORS, "PIO not implemented\n"); + urj_log (URJ_LOG_LEVEL_ERROR, "PIO not implemented\n"); yyerror(&@$, priv_data, chain, "PIO"); YYERROR; } @@ -384,7 +386,7 @@ path_states ps->states[ps->num_states] = $2; ps->num_states++; } else - urj_log (URJ_LOG_LEVEL_ERRORS, "Error %s: maximum number of %d path states reached.\n", + urj_log (URJ_LOG_LEVEL_ERROR, "Error %s: maximum number of %d path states reached.\n", "svf", MAX_PATH_STATES); } ; @@ -411,7 +413,7 @@ direction yyerror (YYLTYPE *locp, urj_svf_parser_priv_t *priv_data, urj_chain_t *chain, const char *error_string) { - urj_log (URJ_LOG_LEVEL_ERRORS, "Error occurred for SVF command %s.\n", + urj_log (URJ_LOG_LEVEL_ERROR, "Error occurred for SVF command %s.\n", error_string); } diff --git a/urjtag/src/svf/svf_flex.l b/urjtag/src/svf/svf_flex.l index af9f7ecf..dac8db03 100644 --- a/urjtag/src/svf/svf_flex.l +++ b/urjtag/src/svf/svf_flex.l @@ -55,11 +55,26 @@ static void fix_yylloc(YYLTYPE *, char *); static void fix_yylloc_nl(YYLTYPE *, char *, YY_EXTRA_TYPE); static void progress_nl(YYLTYPE *, YY_EXTRA_TYPE); +// @@@@ RFHH: take these out after the refactor +// Prototypes that flex should have added, functions that flex should have +// called or not declared: +int urj_svf_get_column (yyscan_t yyscanner); +void urj_svf_set_column (int column_no , yyscan_t yyscanner); + +static void yyunput (int c, register char * yy_bp , yyscan_t yyscanner); + +void urj_svf_use_yyunput (yyscan_t yyscanner); +void urj_svf_use_yyunput (yyscan_t yyscanner) +{ + yyunput (0, NULL, yyscanner); +} + int yywrap(yyscan_t scanner); int yywrap(yyscan_t scanner) { return(1); } +// @@@@ RFHH: take these out after the refactor #define YY_USER_INIT \ do { \ @@ -207,7 +222,7 @@ COMMENT (!.*)|("//".*)[^\n] . { /* print token if interactive parsing enabled and yyin != stdin */ - urj_log (URJ_LOG_LEVEL_ERRORS, + urj_log (URJ_LOG_LEVEL_ERROR, "Error: \"%s\" is not a legal SVF language token\n", yytext); } /* end of any other character */ diff --git a/urjtag/src/tap/cable.c b/urjtag/src/tap/cable.c index 15022b1a..0a2b9e18 100644 --- a/urjtag/src/tap/cable.c +++ b/urjtag/src/tap/cable.c @@ -35,10 +35,13 @@ #include #include +#include +#include #include #undef VERBOSE +/* @@@@ RFHH stick these into a (or many) .h files */ extern urj_cable_driver_t urj_tap_cable_arcom_driver; extern urj_cable_driver_t urj_tap_cable_byteblaster_driver; extern urj_cable_driver_t urj_tap_cable_usbblaster_driver; @@ -174,12 +177,15 @@ urj_tap_cable_init (urj_cable_t *cable) if (cable->todo.data == NULL || cable->done.data == NULL) { - printf (_("Failed to allocate memory for cable activity queue.\n")); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + _("malloc(%zd) or malloc(%zd) fails"), + cable->todo.max_items * sizeof (urj_cable_queue_t), + cable->done.max_items * sizeof (urj_cable_queue_t)); if (cable->todo.data != NULL) free (cable->todo.data); if (cable->done.data != NULL) free (cable->done.data); - return 1; + return URJ_STATUS_FAIL; } return cable->driver->init (cable); @@ -200,7 +206,7 @@ urj_tap_cable_done (urj_cable_t *cable) free (cable->todo.data); free (cable->done.data); } - return cable->driver->done (cable); + cable->driver->done (cable); } int @@ -212,26 +218,22 @@ urj_tap_cable_add_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q) int new_max_items; urj_cable_queue_t *resized; -#ifdef VERBOSE - printf - ("Queue %p needs resizing; n(%d) >= max(%d); free=%d, next=%d\n", + urj_log (URJ_LOG_LEVEL_DETAIL, + "Queue %p needs resizing; n(%d) >= max(%d); free=%d, next=%d\n", q, q->num_items, q->max_items, q->next_free, q->next_item); -#endif new_max_items = q->max_items + 128; - resized = - realloc (q->data, new_max_items * sizeof (urj_cable_queue_t)); + resized = realloc (q->data, new_max_items * sizeof (urj_cable_queue_t)); if (resized == NULL) { - printf (_ - ("Out of memory: couldn't resize activity queue to %d\n"), - new_max_items); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "realloc(%s,%zd) fails", + "q->data", + new_max_items * sizeof (urj_cable_queue_t)); return -1; /* report failure */ } -#ifdef VERBOSE - printf (_("(Resized JTAG activity queue to hold max %d items)\n"), - new_max_items); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, + _("(Resized JTAG activity queue to hold max %d items)\n"), + new_max_items); q->data = resized; /* The queue was full. Except for the special case when next_item is 0, @@ -248,17 +250,15 @@ urj_tap_cable_add_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q) #ifdef CHOOSE_SMALLEST_AREA_TO_MOVE if (num_to_move <= q->next_free) -#endif +#endif /* def CHOOSE_SMALLEST_AREA_TO_MOVE */ { /* Move queue items at end of old array * towards end of new array: 345612__ -> 3456__12 */ int dest = new_max_items - num_to_move; -#ifdef VERBOSE - printf - ("Resize: Move %d items towards end of queue memory (%d > %d)\n", - num_to_move, q->next_item, dest); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, + "Resize: Move %d items towards end of queue memory (%d > %d)\n", + num_to_move, q->next_item, dest); memmove (&(q->data[dest]), &(q->data[q->next_item]), num_to_move * sizeof (urj_cable_queue_t)); @@ -272,10 +272,9 @@ urj_tap_cable_add_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q) /* Relocate queue items at beginning of old array * to end of new array: 561234__ -> __123456 */ -#ifdef VERBOSE - printf ("Resize: Move %d items from start to end\n", - q->next_free); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, + "Resize: Move %d items from start to end\n", + q->next_free); memcpy (&(q->data[q->max_items]), &(q->data[0]), q->next_free * sizeof (urj_cable_queue_t)); @@ -287,39 +286,34 @@ urj_tap_cable_add_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q) /* Step 1: 456123__ -> __612345 */ -#ifdef VERBOSE - printf ("Resize.A: Move %d items from start to end\n", + urj_log (URJ_LOG_LEVEL_DETAIL, + "Resize.A: Move %d items from start to end\n", added_space); -#endif memcpy (&(q->data[q->max_items]), &(q->data[0]), added_space * sizeof (urj_cable_queue_t)); /* Step 2: __612345 -> 6__12345 */ -#ifdef VERBOSE - printf - ("Resize.B: Move %d items towards start (offset %d)\n", + urj_log (URJ_LOG_LEVEL_DETAIL, + "Resize.B: Move %d items towards start (offset %d)\n", (q->next_free - added_space), added_space); -#endif memmove (&(q->data[0]), &(q->data[added_space]), (q->next_free - added_space) * sizeof (urj_cable_queue_t)); } } -#endif +#endif /* def CHOOSE_SMALLEST_AREA_TO_MOVE */ } q->max_items = new_max_items; q->next_free = q->next_item + q->num_items; if (q->next_free >= new_max_items) q->next_free -= new_max_items; -#ifdef VERBOSE - printf - ("Queue %p after resizing; n(%d) >= max(%d); free=%d, next=%d\n", + urj_log (URJ_LOG_LEVEL_DETAIL, + "Queue %p after resizing; n(%d) >= max(%d); free=%d, next=%d\n", q, q->num_items, q->max_items, q->next_free, q->next_item); -#endif } i = q->next_free; @@ -329,7 +323,7 @@ urj_tap_cable_add_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q) q->next_free = j; q->num_items++; - // printf("add_queue_item to %p: %d\n", q, i); + // urj_log (URJ_LOG_LEVEL_DEBUG, "add_queue_item to %p: %d\n", q, i); return i; } @@ -344,11 +338,12 @@ urj_tap_cable_get_queue_item (urj_cable_t *cable, urj_cable_queue_info_t *q) j = 0; q->next_item = j; q->num_items--; - // printf("get_queue_item from %p: %d\n", q, i); + // urj_log (URJ_LOG_LEVEL_DEBUG, "get_queue_item from %p: %d\n", q, i); return i; } - // printf("get_queue_item from %p: %d\n", q, -1); + urj_error_set (URJ_ERROR_NOTFOUND, "queue is empty"); + // urj_log (URJ_LOG_LEVEL_DEBUG, "get_queue_item from %p: %d\n", q, -1); return -1; } @@ -397,13 +392,13 @@ urj_tap_cable_defer_clock (urj_cable_t *cable, int tms, int tdi, int n) { int i = urj_tap_cable_add_queue_item (cable, &(cable->todo)); if (i < 0) - return 1; /* report failure */ + return URJ_STATUS_FAIL; /* report failure */ cable->todo.data[i].action = URJ_TAP_CABLE_CLOCK; cable->todo.data[i].arg.clock.tms = tms; cable->todo.data[i].arg.clock.tdi = tdi; cable->todo.data[i].arg.clock.n = n; urj_tap_cable_flush (cable, URJ_TAP_CABLE_OPTIONALLY); - return 0; /* success */ + return URJ_STATUS_OK; /* success */ } int @@ -423,9 +418,9 @@ urj_tap_cable_get_tdo_late (urj_cable_t *cable) { if (cable->done.data[i].action != URJ_TAP_CABLE_GET_TDO) { - printf (_ - ("Internal error: Got wrong type of result from queue (%d? %p.%d)\n"), - cable->done.data[i].action, &(cable->done), i); + urj_warning ( + _("Internal error: Got wrong type of result from queue (%d? %p.%d)\n"), + cable->done.data[i].action, &(cable->done), i); urj_tap_cable_purge_queue (&(cable->done), 1); } else @@ -441,10 +436,10 @@ urj_tap_cable_defer_get_tdo (urj_cable_t *cable) { int i = urj_tap_cable_add_queue_item (cable, &(cable->todo)); if (i < 0) - return 1; /* report failure */ + return URJ_STATUS_FAIL; /* report failure */ cable->todo.data[i].action = URJ_TAP_CABLE_GET_TDO; urj_tap_cable_flush (cable, URJ_TAP_CABLE_OPTIONALLY); - return 0; /* success */ + return URJ_STATUS_OK; /* success */ } int @@ -459,12 +454,12 @@ urj_tap_cable_defer_set_signal (urj_cable_t *cable, int mask, int val) { int i = urj_tap_cable_add_queue_item (cable, &(cable->todo)); if (i < 0) - return 1; /* report failure */ + return URJ_STATUS_FAIL; /* report failure */ cable->todo.data[i].action = URJ_TAP_CABLE_SET_SIGNAL; cable->todo.data[i].arg.value.mask = mask; cable->todo.data[i].arg.value.val = val; urj_tap_cable_flush (cable, URJ_TAP_CABLE_OPTIONALLY); - return 0; /* success */ + return URJ_STATUS_OK; /* success */ } int @@ -484,16 +479,16 @@ urj_tap_cable_get_signal_late (urj_cable_t *cable, urj_pod_sigsel_t sig) { if (cable->done.data[i].action != URJ_TAP_CABLE_GET_SIGNAL) { - printf (_ - ("Internal error: Got wrong type of result from queue (%d? %p.%d)\n"), - cable->done.data[i].action, &(cable->done), i); + urj_warning ( + _("Internal error: Got wrong type of result from queue (%d? %p.%d)\n"), + cable->done.data[i].action, &(cable->done), i); urj_tap_cable_purge_queue (&(cable->done), 1); } else if (cable->done.data[i].arg.value.sig != sig) { - printf (_ - ("Internal error: Got wrong signal's value from queue (%d? %p.%d)\n"), - cable->done.data[i].action, &(cable->done), i); + urj_warning ( + _("Internal error: Got wrong signal's value from queue (%d? %p.%d)\n"), + cable->done.data[i].action, &(cable->done), i); urj_tap_cable_purge_queue (&(cable->done), 1); } else @@ -509,11 +504,11 @@ urj_tap_cable_defer_get_signal (urj_cable_t *cable, urj_pod_sigsel_t sig) { int i = urj_tap_cable_add_queue_item (cable, &(cable->todo)); if (i < 0) - return 1; /* report failure */ + return URJ_STATUS_FAIL; /* report failure */ cable->todo.data[i].action = URJ_TAP_CABLE_GET_SIGNAL; cable->todo.data[i].arg.value.sig = sig; urj_tap_cable_flush (cable, URJ_TAP_CABLE_OPTIONALLY); - return 0; /* success */ + return URJ_STATUS_OK; /* success */ } int @@ -533,7 +528,7 @@ urj_tap_cable_transfer_late (urj_cable_t *cable, char *out) if (i >= 0 && cable->done.data[i].action == URJ_TAP_CABLE_TRANSFER) { #if 0 - printf ("Got queue item (%p.%d) len=%d out=%p\n", + urj_log (URJ_LOG_LEVEL_DEBUG, "Got queue item (%p.%d) len=%d out=%p\n", &(cable->done), i, cable->done.data[i].arg.xferred.len, cable->done.data[i].arg.xferred.out); @@ -548,15 +543,15 @@ urj_tap_cable_transfer_late (urj_cable_t *cable, char *out) if (cable->done.data[i].action != URJ_TAP_CABLE_TRANSFER) { - printf (_ - ("Internal error: Got wrong type of result from queue (#%d %p.%d)\n"), - cable->done.data[i].action, &(cable->done), i); + urj_warning ( + _("Internal error: Got wrong type of result from queue (#%d %p.%d)\n"), + cable->done.data[i].action, &(cable->done), i); urj_tap_cable_purge_queue (&(cable->done), 1); } else { - printf (_ - ("Internal error: Wanted transfer result but none was queued\n")); + urj_warning ( + _("Internal error: Wanted transfer result but none was queued\n")); } return 0; } @@ -570,7 +565,10 @@ urj_tap_cable_defer_transfer (urj_cable_t *cable, int len, char *in, ibuf = malloc (len); if (ibuf == NULL) - return 1; + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", len); + return URJ_STATUS_FAIL; + } if (out) { @@ -578,7 +576,8 @@ urj_tap_cable_defer_transfer (urj_cable_t *cable, int len, char *in, if (obuf == NULL) { free (ibuf); - return 1; + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", len); + return URJ_STATUS_FAIL; } } @@ -588,7 +587,7 @@ urj_tap_cable_defer_transfer (urj_cable_t *cable, int len, char *in, free (ibuf); if (obuf) free (obuf); - return 1; /* report failure */ + return URJ_STATUS_FAIL; /* report failure */ } cable->todo.data[i].action = URJ_TAP_CABLE_TRANSFER; @@ -598,7 +597,7 @@ urj_tap_cable_defer_transfer (urj_cable_t *cable, int len, char *in, cable->todo.data[i].arg.transfer.in = ibuf; cable->todo.data[i].arg.transfer.out = obuf; urj_tap_cable_flush (cable, URJ_TAP_CABLE_OPTIONALLY); - return 0; /* success */ + return URJ_STATUS_OK; /* success */ } void diff --git a/urjtag/src/tap/chain.c b/urjtag/src/tap/chain.c index f973fa5a..d2847f41 100644 --- a/urjtag/src/tap/chain.c +++ b/urjtag/src/tap/chain.c @@ -43,7 +43,11 @@ urj_tap_chain_alloc (void) { urj_chain_t *chain = malloc (sizeof (urj_chain_t)); if (!chain) + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + sizeof (urj_chain_t)); return NULL; + } chain->cable = NULL; chain->parts = NULL; @@ -79,32 +83,42 @@ urj_tap_chain_disconnect (urj_chain_t *chain) chain->cable = NULL; } -void +int urj_tap_chain_clock (urj_chain_t *chain, int tms, int tdi, int n) { int i; if (!chain || !chain->cable) - return; + { + urj_error_set (URJ_ERROR_NO_CHAIN, "no chain or no part"); + return URJ_STATUS_FAIL; + } urj_tap_cable_clock (chain->cable, tms, tdi, n); for (i = 0; i < n; i++) urj_tap_state_clock (chain, tms); + + return URJ_STATUS_OK; } -void +int urj_tap_chain_defer_clock (urj_chain_t *chain, int tms, int tdi, int n) { int i; if (!chain || !chain->cable) - return; + { + urj_error_set (URJ_ERROR_NO_CHAIN, "no chain or no part"); + return URJ_STATUS_FAIL; + } urj_tap_cable_defer_clock (chain->cable, tms, tdi, n); for (i = 0; i < n; i++) urj_tap_state_clock (chain, tms); + + return URJ_STATUS_OK; } int @@ -144,7 +158,7 @@ urj_tap_chain_get_pod_signal (urj_chain_t *chain, urj_pod_sigsel_t sig) return urj_tap_cable_get_signal (chain->cable, sig); } -void +int urj_tap_chain_shift_instructions_mode (urj_chain_t *chain, int capture_output, int capture, int chain_exit) @@ -153,7 +167,10 @@ urj_tap_chain_shift_instructions_mode (urj_chain_t *chain, urj_parts_t *ps; if (!chain || !chain->parts) - return; + { + urj_error_set (URJ_ERROR_NO_CHAIN, "no chain or no part"); + return URJ_STATUS_FAIL; + } ps = chain->parts; @@ -161,9 +178,9 @@ urj_tap_chain_shift_instructions_mode (urj_chain_t *chain, { if (ps->parts[i]->active_instruction == NULL) { - printf (_("%s(%d) Part %d without active instruction\n"), - __FILE__, __LINE__, i); - return; + urj_error_set (URJ_ERROR_NO_ACTIVE_INSTRUCTION, + _("Part %d without active instruction"), i); + return URJ_STATUS_FAIL; } } @@ -198,16 +215,18 @@ urj_tap_chain_shift_instructions_mode (urj_chain_t *chain, /* give the cable driver a chance to flush if it's considered useful */ urj_tap_cable_flush (chain->cable, URJ_TAP_CABLE_TO_OUTPUT); } + + return URJ_STATUS_OK; } -void +int urj_tap_chain_shift_instructions (urj_chain_t *chain) { - urj_tap_chain_shift_instructions_mode (chain, 0, 1, - URJ_CHAIN_EXITMODE_IDLE); + return urj_tap_chain_shift_instructions_mode (chain, 0, 1, + URJ_CHAIN_EXITMODE_IDLE); } -void +int urj_tap_chain_shift_data_registers_mode (urj_chain_t *chain, int capture_output, int capture, int chain_exit) @@ -216,7 +235,10 @@ urj_tap_chain_shift_data_registers_mode (urj_chain_t *chain, urj_parts_t *ps; if (!chain || !chain->parts) - return; + { + urj_error_set (URJ_ERROR_NO_CHAIN, "no chain or no part"); + return URJ_STATUS_FAIL; + } ps = chain->parts; @@ -224,15 +246,15 @@ urj_tap_chain_shift_data_registers_mode (urj_chain_t *chain, { if (ps->parts[i]->active_instruction == NULL) { - printf (_("%s(%d) Part %d without active instruction\n"), - __FILE__, __LINE__, i); - return; + urj_error_set (URJ_ERROR_NO_ACTIVE_INSTRUCTION, + _("Part %d without active instruction"), i); + return URJ_STATUS_FAIL; } if (ps->parts[i]->active_instruction->data_register == NULL) { - printf (_("%s(%d) Part %d without data register\n"), __FILE__, - __LINE__, i); - return; + urj_error_set (URJ_ERROR_NO_DATA_REGISTER, + _("Part %d without data register\n"), i); + return URJ_STATUS_FAIL; } } @@ -267,13 +289,15 @@ urj_tap_chain_shift_data_registers_mode (urj_chain_t *chain, /* give the cable driver a chance to flush if it's considered useful */ urj_tap_cable_flush (chain->cable, URJ_TAP_CABLE_TO_OUTPUT); } + + return URJ_STATUS_OK; } -void +int urj_tap_chain_shift_data_registers (urj_chain_t *chain, int capture_output) { - urj_tap_chain_shift_data_registers_mode (chain, capture_output, 1, - URJ_CHAIN_EXITMODE_IDLE); + return urj_tap_chain_shift_data_registers_mode (chain, capture_output, 1, + URJ_CHAIN_EXITMODE_IDLE); } void diff --git a/urjtag/src/tap/detect.c b/urjtag/src/tap/detect.c index eb557b81..8c8f6655 100644 --- a/urjtag/src/tap/detect.c +++ b/urjtag/src/tap/detect.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -60,7 +61,9 @@ find_record (char *filename, urj_tap_register_t *key, struct id_record *idr) file = fopen (filename, "r"); if (!file) { - printf (_("Cannot open %s\n"), filename); + urj_error_set (URJ_ERROR_IO, "Cannot open '%s': %s", filename, + strerror(errno)); + errno = 0; return 0; } @@ -186,7 +189,6 @@ bits_to_uint64 (urj_tap_register_t *t) return l; } - int urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) { @@ -211,18 +213,16 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) urj_tap_capture_ir (chain); irlen = urj_tap_detect_register_size (chain); if (irlen < 1) - return 0; + // retain error state + return -1; - printf (_("IR length: %d\n"), irlen); + urj_log (URJ_LOG_LEVEL_NORMAL, _("IR length: %d\n"), irlen); chain->total_instr_len = irlen; /* Allocate IR */ ir = urj_tap_register_fill (urj_tap_register_alloc (irlen), 1); if (ir == NULL) - { - printf (_("out of memory\n")); - return 0; - } + return -1; urj_tap_shift_register (chain, ir, NULL, URJ_CHAIN_EXITMODE_IDLE); urj_tap_register_free (ir); @@ -232,10 +232,12 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) chlen = urj_tap_detect_register_size (chain); if (chlen < 1) { - printf (_("Unable to detect JTAG chain length\n")); - return 0; + // retain error state + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Unable to detect JTAG chain length\n")); + return -1; } - printf (_("Chain length: %d\n"), chlen); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Chain length: %d\n"), chlen); /* Allocate registers and parts */ one = urj_tap_register_fill (urj_tap_register_alloc (1), 1); @@ -245,14 +247,13 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) ps = urj_part_parts_alloc (); if (!one || !ones || !br || !id || !ps) { - printf (_("out of memory\n")); - urj_tap_register_free (one); urj_tap_register_free (ones); urj_tap_register_free (br); urj_tap_register_free (id); urj_part_parts_free (ps); - return 0; + // retain error state + return -1; } chain->parts = ps; chain->active_part = 0; @@ -280,15 +281,12 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) did = id; } - printf (_("Device Id: %s (0x%016" PRIX64 ")\n"), - urj_tap_register_get_string (did), bits_to_uint64 (did)); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Device Id: %s (0x%016" PRIX64 ")\n"), + urj_tap_register_get_string (did), bits_to_uint64 (did)); part = urj_part_alloc (did); if (part == NULL) - { - printf (_("Out of memory\n")); break; - } urj_part_parts_add_part (ps, part); if (did == br) @@ -313,15 +311,16 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) memcpy (key->data, &id->data[1], key->len); if (!find_record (data_path, key, &idr)) { - printf (_(" Unknown manufacturer!\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Unknown manufacturer!\n")); urj_tap_register_free (key); continue; } urj_tap_register_free (key); - printf (_(" Manufacturer: %s\n"), idr.fullname); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Manufacturer: %s\n"), + idr.fullname); if (strlen (idr.fullname) > URJ_PART_MANUFACTURER_MAXLEN) - printf (_("Warning: Manufacturer too long\n")); + urj_warning (_("Manufacturer too long\n")); strncpy (manufacturer, idr.fullname, URJ_PART_MANUFACTURER_MAXLEN); manufacturer[URJ_PART_MANUFACTURER_MAXLEN] = '\0'; @@ -339,16 +338,16 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) memcpy (key->data, &id->data[12], key->len); if (!find_record (data_path, key, &idr)) { - printf (_(" Unknown part!\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Unknown part!\n")); urj_tap_register_free (key); continue; } urj_tap_register_free (key); - printf (_(" Part(%d): %s\n"), chain->active_part, - idr.fullname); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Part(%d): %s\n"), + chain->active_part, idr.fullname); if (strlen (idr.fullname) > URJ_PART_PART_MAXLEN) - printf (_("Warning: Part too long\n")); + urj_warning (_("Part too long\n")); strncpy (partname, idr.fullname, URJ_PART_PART_MAXLEN); partname[URJ_PART_PART_MAXLEN] = '\0'; @@ -365,15 +364,16 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) memcpy (key->data, &id->data[28], key->len); if (!find_record (data_path, key, &idr)) { - printf (_(" Unknown stepping!\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Unknown stepping!\n")); urj_tap_register_free (key); continue; } urj_tap_register_free (key); - printf (_(" Stepping: %s\n"), idr.fullname); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Stepping: %s\n"), + idr.fullname); if (strlen (idr.fullname) > URJ_PART_STEPPING_MAXLEN) - printf (_("Warning: Stepping too long\n")); + urj_warning (_("Stepping too long\n")); strncpy (stepping, idr.fullname, URJ_PART_STEPPING_MAXLEN); stepping[URJ_PART_STEPPING_MAXLEN] = '\0'; @@ -385,7 +385,8 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) data_path[0] = '\0'; strcat (data_path, idr.name); - printf (_(" Filename: %s\n"), data_path); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" Filename: %s\n"), + data_path); /* run JTAG declarations */ strcpy (part->manufacturer, manufacturer); @@ -406,7 +407,8 @@ urj_tap_detect_parts (urj_chain_t *chain, const char *db_path) urj_tap_shift_register (chain, one, br, URJ_CHAIN_EXITMODE_SHIFT); if (urj_tap_register_compare (one, br) != 0) { - printf (_("Error: Unable to detect JTAG chain end!\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Error: Unable to detect JTAG chain end!\n")); break; } } @@ -434,28 +436,19 @@ urj_tap_manual_add (urj_chain_t *chain, int instr_len) id = urj_tap_register_alloc (1); if (id == NULL) - { - printf (_("Error: Unable to allocate a register!\n")); - return 0; - } + return -1; /* if there are no parts, create the parts list */ if (chain->parts == NULL) { chain->parts = urj_part_parts_alloc (); if (chain->parts == NULL) - { - printf (_("Error: Unable to allocate space for parts!\n")); - return 0; - } + return -1; } part = urj_part_alloc (id); if (part == NULL) - { - printf (_("Error: Unable to allocate space for a part!\n")); - return 0; - } + return -1; strncpy (part->part, "unknown", URJ_PART_PART_MAXLEN); part->instruction_length = instr_len; @@ -466,8 +459,8 @@ urj_tap_manual_add (urj_chain_t *chain, int instr_len) /* make the BR register available */ if (urj_part_data_register_define (part, "BR", 1) != URJ_STATUS_OK) { - printf (_("Error: could not set BR register")); - return 0; + urj_log (URJ_LOG_LEVEL_NORMAL, _("Error: could not set BR register")); + return -1; } /* create a string of 1's for BYPASS instruction */ @@ -478,8 +471,9 @@ urj_tap_manual_add (urj_chain_t *chain, int instr_len) str = calloc (instr_len + 1, sizeof (char)); if (str == NULL) { - printf (_("Out of memory!\n")); - return 0; + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "calloc(%zd,%zd) fails", + instr_len + 1, sizeof (char)); + return -1; } memset (str, '1', instr_len); @@ -490,8 +484,9 @@ urj_tap_manual_add (urj_chain_t *chain, int instr_len) if (result < 1) { - printf (_("Error: could not set BYPASS instruction")); - return 0; + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Error: could not set BYPASS instruction")); + return -1; } /* update total instruction register length of chain */ @@ -509,7 +504,9 @@ urj_tap_detect (urj_chain_t *chain) urj_bus_buses_free (); urj_part_parts_free (chain->parts); chain->parts = NULL; - urj_tap_detect_parts (chain, urj_get_data_dir ()); + if (urj_tap_detect_parts (chain, urj_get_data_dir ()) != URJ_STATUS_OK) + // retain error state + return URJ_STATUS_FAIL; if (!chain->parts) { urj_error_set (URJ_ERROR_INVALID, "chain has no parts"); diff --git a/urjtag/src/tap/discovery.c b/urjtag/src/tap/discovery.c index 03e7b29c..fa451220 100644 --- a/urjtag/src/tap/discovery.c +++ b/urjtag/src/tap/discovery.c @@ -78,8 +78,8 @@ urj_tap_detect_register_size (urj_chain_t *chain) urj_tap_shift_register (chain, rpat, rout, 0); #ifdef VERY_LOW_LEVEL_DEBUG - printf (">>> %s\n", urj_tap_register_get_string (rz)); - printf (" + %s\n", urj_tap_register_get_string (rpat)); + urj_log (URJ_LOG_LEVEL_ALL, ">>> %s\n", urj_tap_register_get_string (rz)); + urj_log (URJ_LOG_LEVEL_ALL, " + %s\n", urj_tap_register_get_string (rpat)); #endif tdo = urj_tap_register_all_bits_same_value (rout); if (tdo_stuck == -2) @@ -91,7 +91,7 @@ urj_tap_detect_register_size (urj_chain_t *chain) if (urj_tap_register_compare (rpat, rout) == 0) ok++; #ifdef VERY_LOW_LEVEL_DEBUG - printf (" = %s => %d\n", urj_tap_register_get_string (rout), + urj_log (URJ_LOG_LEVEL_ALL, " = %s => %d\n", urj_tap_register_get_string (rout), ok); #endif } @@ -114,7 +114,7 @@ urj_tap_detect_register_size (urj_chain_t *chain) if (tdo_stuck >= 0) { - printf (_("Warning: TDO seems to be stuck at %d\n"), tdo_stuck); + urj_warning (_("TDO seems to be stuck at %d\n"), tdo_stuck); } return -1; @@ -129,7 +129,7 @@ jtag_reset (urj_chain_t *chain) urj_tap_reset (chain); } -void +int urj_tap_discovery (urj_chain_t *chain) { int irlen; @@ -139,18 +139,19 @@ urj_tap_discovery (urj_chain_t *chain) /* detecting IR size */ jtag_reset (chain); - printf (_("Detecting IR length ... ")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Detecting IR length ... ")); fflush (stdout); urj_tap_capture_ir (chain); irlen = urj_tap_detect_register_size (chain); - printf (_("%d\n"), irlen); + urj_log (URJ_LOG_LEVEL_NORMAL, _("%d\n"), irlen); if (irlen < 1) { - printf (_("Error: Invalid IR length!\n")); - return; + // retain error state + urj_log (URJ_LOG_LEVEL_NORMAL, _("Error: Invalid IR length!\n")); + return URJ_STATUS_FAIL; } /* all 1 is BYPASS in all parts, so DR length gives number of parts */ @@ -161,8 +162,7 @@ urj_tap_discovery (urj_chain_t *chain) { urj_tap_register_free (ir); urj_tap_register_free (irz); - printf (_("Error: Out of memory!\n")); - return; + return URJ_STATUS_FAIL; } for (;;) @@ -174,14 +174,15 @@ urj_tap_discovery (urj_chain_t *chain) urj_tap_capture_ir (chain); urj_tap_shift_register (chain, ir, NULL, 1); - printf (_("Detecting DR length for IR %s ... "), + urj_log (URJ_LOG_LEVEL_NORMAL, _("Detecting DR length for IR %s ... "), urj_tap_register_get_string (ir)); fflush (stdout); urj_tap_capture_dr (chain); + // @@@@ RFHH check result rs = urj_tap_detect_register_size (chain); - printf (_("%d\n"), rs); + urj_log (URJ_LOG_LEVEL_NORMAL, _("%d\n"), rs); urj_tap_register_inc (ir); if (urj_tap_register_compare (ir, irz) == 0) @@ -189,4 +190,6 @@ urj_tap_discovery (urj_chain_t *chain) } urj_tap_register_free (ir); urj_tap_register_free (irz); + + return URJ_STATUS_OK; } diff --git a/urjtag/src/tap/idcode.c b/urjtag/src/tap/idcode.c index 03398e21..4867f07d 100644 --- a/urjtag/src/tap/idcode.c +++ b/urjtag/src/tap/idcode.c @@ -31,7 +31,7 @@ #include -void +int urj_tap_idcode (urj_chain_t *chain, unsigned int bytes) { int i; @@ -52,14 +52,14 @@ urj_tap_idcode (urj_chain_t *chain, unsigned int bytes) rout = urj_tap_register_alloc (8); if (!rz || !rout || !rnull) - { - printf (_("Allocation failed\n")); - } - printf (_("Read")); + return URJ_STATUS_FAIL; + + urj_log (URJ_LOG_LEVEL_NORMAL, _("Read")); for (i = 0; i < ((bytes) ? bytes : 1000); i++) { urj_tap_shift_register (chain, rz, rout, 0); - printf (_(" %s"), urj_tap_register_get_string (rout)); + urj_log (URJ_LOG_LEVEL_NORMAL, _(" %s"), + urj_tap_register_get_string (rout)); if (!bytes) { /* Abort Reading when a null IDCODE has been read */ @@ -74,5 +74,7 @@ urj_tap_idcode (urj_chain_t *chain, unsigned int bytes) urj_tap_register_free (rz); urj_tap_register_free (rnull); urj_tap_register_free (rout); - printf (_("\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("\n")); + + return URJ_STATUS_OK; } diff --git a/urjtag/src/tap/parport.c b/urjtag/src/tap/parport.c index a1667d5e..21d19e48 100644 --- a/urjtag/src/tap/parport.c +++ b/urjtag/src/tap/parport.c @@ -28,6 +28,7 @@ #include +/* @@@@ RFHH move these to some .h file */ extern urj_parport_driver_t urj_tap_parport_direct_parport_driver; extern urj_parport_driver_t urj_tap_parport_ppdev_parport_driver; extern urj_parport_driver_t urj_tap_parport_ppi_parport_driver; diff --git a/urjtag/src/tap/parport/direct.c b/urjtag/src/tap/parport/direct.c index 55936c53..ac261de4 100644 --- a/urjtag/src/tap/parport/direct.c +++ b/urjtag/src/tap/parport/direct.c @@ -31,7 +31,10 @@ #include #include #include +#include +#include +#include #include #include @@ -132,6 +135,17 @@ direct_parport_alloc (unsigned int port) urj_parport_t *parport = malloc (sizeof *parport); port_node_t *node = malloc (sizeof *node); + if (!node || !parport || !params) + { + free (node); + free (parport); + free (params); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + "malloc(%zd)/malloc(%zd)/malloc(%zd) fails", + sizeof *params, sizeof *parport, sizeof *node); + return NULL; + } + #if defined(HAVE_INPOUTXX) if (inpout32_dll_handle == NULL) { @@ -139,26 +153,14 @@ direct_parport_alloc (unsigned int port) } if (inpout32_dll_handle == NULL) { - fprintf (stderr, - _("Couldn't load InpOut32.dll; maybe not installed?\n")); - } - else - { - Inp32 = (inpfuncPtr) GetProcAddress (inpout32_dll_handle, "Inp32"); - Out32 = (outfuncPtr) GetProcAddress (inpout32_dll_handle, "Out32"); + urj_error_set (URJ_ERROR_IO, + _("Couldn't load InpOut32.dll; maybe not installed?\n")); + return NULL; } - if (!node || !parport || !params || !inpout32_dll_handle) -#else - if (!node || !parport || !params) + Inp32 = (inpfuncPtr) GetProcAddress (inpout32_dll_handle, "Inp32"); + Out32 = (outfuncPtr) GetProcAddress (inpout32_dll_handle, "Out32"); #endif - { - - free (node); - free (parport); - free (params); - return NULL; - } params->port = port; @@ -209,7 +211,7 @@ direct_connect (const char **par, int parnum) if (parnum != 1) { - printf (_("Syntax error!\n")); + urj_error_set (URJ_ERROR_SYNTAX, "#params != 1"); return NULL; } @@ -217,7 +219,7 @@ direct_connect (const char **par, int parnum) if (port_scan_val < 0 || (port_scan_val + 3) > 0xffff) { - printf (_("Invalid port address!\n")); + urj_error_set (URJ_ERROR_INVALID, _("Invalid port address")); return NULL; } @@ -231,19 +233,20 @@ direct_connect (const char **par, int parnum) aport = ((direct_params_t *) pn->port->params)->port; if (abs (aport - port) < 3) { - printf (_("Disconnecting %s from parallel port at 0x%x\n"), - _(pn->port->cable->driver->description), aport); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Disconnecting %s from parallel port at 0x%x\n"), + _(pn->port->cable->driver->description), aport); pn->port->cable->driver->disconnect (pn->port->cable); break; } } - printf (_("Initializing parallel port at 0x%x\n"), port); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Initializing parallel port at 0x%x\n"), + port); parport = direct_parport_alloc (port); if (!parport) { - printf (_("%s(%d) Out of memory.\n"), __FILE__, __LINE__); return NULL; } @@ -254,11 +257,17 @@ static int direct_open (urj_parport_t *parport) { #ifdef HAVE_INPOUTXX - return 0; + return URJ_STATUS_OK; #else unsigned int port = ((direct_params_t *) parport->params)->port; - return ((port + 3 <= 0x400) && ioperm (port, 3, 1)) || ((port + 3 > 0x400) - && iopl (3)); + if (((port + 3 <= 0x400) ? ioperm (port, 3, 1) : iopl (3)) == -1) + { + urj_error_set (URJ_ERROR_IO, "ioperm(3,1) or iopl(3) fails: %s", + strerror(errno)); + errno = 0; + return URJ_STATUS_FAIL; + } + return URJ_STATUS_OK; #endif } @@ -266,10 +275,17 @@ static int direct_close (urj_parport_t *parport) { #if defined(HAVE_INPOUTXX) - return 0; + return URJ_STATUS_OK; #else unsigned int port = ((direct_params_t *) parport->params)->port; - return (port + 3 <= 0x400) ? ioperm (port, 3, 0) : iopl (0); + if (((port + 3 <= 0x400) ? ioperm (port, 3, 0) : iopl (0)) == -1) + { + urj_error_set (URJ_ERROR_IO, "ioperm(3,0) or iopl(0) fails: %s", + strerror(errno)); + errno = 0; + return URJ_STATUS_FAIL; + } + return URJ_STATUS_OK; #endif } @@ -278,7 +294,7 @@ direct_set_data (urj_parport_t *parport, uint8_t data) { unsigned int port = ((direct_params_t *) parport->params)->port; outb (data, port); - return 0; + return URJ_STATUS_OK; } static int @@ -300,7 +316,7 @@ direct_set_control (urj_parport_t *parport, uint8_t data) { unsigned int port = ((direct_params_t *) parport->params)->port; outb (data ^ 0x0B, port + 2); /* SELECT, AUTOFD, and STROBE are inverted */ - return 0; + return URJ_STATUS_OK; } urj_parport_driver_t urj_tap_parport_direct_parport_driver = { diff --git a/urjtag/src/tap/parport/ppdev.c b/urjtag/src/tap/parport/ppdev.c index 9acb4b33..2ea9de1b 100644 --- a/urjtag/src/tap/parport/ppdev.c +++ b/urjtag/src/tap/parport/ppdev.c @@ -40,6 +40,8 @@ #include #include +#include +#include #include #include @@ -75,6 +77,9 @@ ppdev_parport_alloc (const char *port) free (parport); free (params); free (portname); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + "malloc(%zd)/strdup(%s)/malloc(%zd)/malloc(%zd) fails", + sizeof *params, port, sizeof *parport, sizeof *node); return NULL; } @@ -122,27 +127,25 @@ ppdev_connect (const char **par, int parnum) if (parnum != 1) { - printf (_("Syntax error!\n")); + urj_error_set (URJ_ERROR_SYNTAX, "#param != 1"); return NULL; } for (pn = ports; pn; pn = pn->next) if (strcmp (pn->port->params, par[0]) == 0) { - printf (_("Disconnecting %s from ppdev port %s\n"), - _(pn->port->cable->driver->description), par[0]); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("Disconnecting %s from ppdev port %s\n"), + _(pn->port->cable->driver->description), par[0]); pn->port->cable->driver->disconnect (pn->port->cable); break; } - printf (_("Initializing ppdev port %s\n"), par[0]); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Initializing ppdev port %s\n"), par[0]); parport = ppdev_parport_alloc (par[0]); if (!parport) - { - printf (_("%s(%d) Out of memory.\n"), __FILE__, __LINE__); return NULL; - } return parport; } @@ -155,34 +158,47 @@ ppdev_open (urj_parport_t *parport) p->fd = open (p->portname, O_RDWR); if (p->fd < 0) { - printf (_("Could not open port %s: %s\n"), p->portname, - strerror (errno)); - return -1; + urj_error_set (URJ_ERROR_IO, _("Could not open port %s: %s\n"), + p->portname, strerror (errno)); + errno = 0; + return URJ_STATUS_FAIL; } if ( /*(ioctl( p->fd, PPEXCL ) == -1) || */ (ioctl (p->fd, PPCLAIM) == -1)) { - printf (_("Could not claim ppdev device: %s\n"), strerror (errno)); + urj_error_set (URJ_ERROR_IO, _("Could not claim ppdev device: %s\n"), + strerror (errno)); + errno = 0; close (p->fd); p->fd = -1; - return -1; + return URJ_STATUS_FAIL; } - return 0; + return URJ_STATUS_OK; } static int ppdev_close (urj_parport_t *parport) { - int r = 0; + int r = URJ_STATUS_OK; ppdev_params_t *p = parport->params; if (ioctl (p->fd, PPRELEASE) == -1) - r = -1; + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPRELEASE) fails: %s", + strerror (errno)); + errno = 0; + r = URJ_STATUS_FAIL; + } if (close (p->fd) != 0) - return -1; + { + urj_error_set (URJ_ERROR_IO, "Cannot close(%d): %s", p->fd, + strerror (errno)); + errno = 0; + return URJ_STATUS_FAIL; + } p->fd = -1; return r; @@ -194,9 +210,14 @@ ppdev_set_data (urj_parport_t *parport, uint8_t data) ppdev_params_t *p = parport->params; if (ioctl (p->fd, PPWDATA, &data) == -1) - return -1; + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPWDATA) fails: %s", + strerror (errno)); + errno = 0; + return URJ_STATUS_FAIL; + } - return 0; + return URJ_STATUS_OK; } static int @@ -206,7 +227,12 @@ ppdev_get_data (urj_parport_t *parport) ppdev_params_t *p = parport->params; if (ioctl (p->fd, PPRDATA, &d) == -1) + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPRSTATUS) fails: %s", + strerror (errno)); + errno = 0; return -1; + } return d; } @@ -218,7 +244,12 @@ ppdev_get_status (urj_parport_t *parport) ppdev_params_t *p = parport->params; if (ioctl (p->fd, PPRSTATUS, &d) == -1) + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPRSTATUS) fails: %s", + strerror (errno)); + errno = 0; return -1; + } return d ^ 0x80; /* BUSY is inverted */ } @@ -231,9 +262,14 @@ ppdev_set_control (urj_parport_t *parport, uint8_t data) data ^= 0x0B; /* SELECT, AUTOFD, and STROBE are inverted */ if (ioctl (p->fd, PPWCONTROL, &data) == -1) - return -1; + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPWDATA) fails: %s", + strerror (errno)); + errno = 0; + return URJ_STATUS_FAIL; + } - return 0; + return URJ_STATUS_OK; } urj_parport_driver_t urj_tap_parport_ppdev_parport_driver = { diff --git a/urjtag/src/tap/parport/ppi.c b/urjtag/src/tap/parport/ppi.c index 904e3311..34a376e1 100644 --- a/urjtag/src/tap/parport/ppi.c +++ b/urjtag/src/tap/parport/ppi.c @@ -27,6 +27,7 @@ #ifdef HAVE_DEV_PPBUS_PPI_H +#include #include #include #include @@ -35,6 +36,8 @@ #include #include +#include +#include #include #include @@ -70,6 +73,9 @@ ppi_parport_alloc (const char *port) free (parport); free (params); free (portname); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + "malloc(%zd)/strdup(%s)/malloc(%zd)/malloc(%zd) fails", + sizeof *params, port, sizeof *parport, sizeof *node); return NULL; } @@ -117,7 +123,7 @@ ppi_connect (const char **par, int parnum) if (parnum != 1) { - printf (_("Syntax error!\n")); + urj_error_set (URJ_ERROR_SYNTAX, "#params != 1"); return NULL; } @@ -135,7 +141,6 @@ ppi_connect (const char **par, int parnum) parport = ppi_parport_alloc (par[0]); if (!parport) { - printf (_("%s(%d) Out of memory.\n"), __FILE__, __LINE__); return NULL; } @@ -149,19 +154,29 @@ ppi_open (urj_parport_t *parport) p->fd = open (p->portname, O_RDWR); if (p->fd < 0) - return -1; + { + urj_error_set (URJ_ERROR_IO, "Cannot open(%s): %s", p->portname, + strerror(errno)); + errno = 0; + return URJ_STATUS_FAIL; + } - return 0; + return URJ_STATUS_OK; } static int ppi_close (urj_parport_t *parport) { - int r = 0; + int r = URJ_STATUS_OK; ppi_params_t *p = parport->params; if (close (p->fd) != 0) - return -1; + { + urj_error_set (URJ_ERROR_IO, "Cannot close(%d): %s", p->fd, + strerror(errno)); + errno = 0; + return URJ_STATUS_FAIL; + } p->fd = -1; return r; @@ -173,9 +188,14 @@ ppi_set_data (urj_parport_t *parport, uint8_t data) ppi_params_t *p = parport->params; if (ioctl (p->fd, PPISDATA, &data) == -1) - return -1; + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPISDATA) fails: %s", + strerror (errno)); + errno = 0; + return URJ_STATUS_FAIL; + } - return 0; + return URJ_STATUS_OK; } static int @@ -185,7 +205,12 @@ ppi_get_data (urj_parport_t *parport) ppi_params_t *p = parport->params; if (ioctl (p->fd, PPIGDATA, &d) == -1) + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPIGDATA) fails: %s", + strerror (errno)); + errno = 0; return -1; + } return d; } @@ -197,7 +222,12 @@ ppi_get_status (urj_parport_t *parport) ppi_params_t *p = parport->params; if (ioctl (p->fd, PPIGSTATUS, &d) == -1) + { + urj_error_set (URJ_ERROR_IO, "ioctl(PPIGSTATUS) fails: %s", + strerror (errno)); + errno = 0; return -1; + } return d ^ 0x80; /* BUSY is inverted */ } @@ -210,9 +240,13 @@ ppi_set_control (urj_parport_t *parport, uint8_t data) data ^= 0x0B; /* SELECT, AUTOFD, and STROBE are inverted */ if (ioctl (p->fd, PPIGCTRL, &data) == -1) - return -1; + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + sizeof (urj_chain_t)); + return URJ_STATUS_FAIL; + } - return 0; + return URJ_STATUS_OK; } urj_parport_driver_t urj_tap_parport_ppi_parport_driver = { diff --git a/urjtag/src/tap/register.c b/urjtag/src/tap/register.c index b968ec44..a7a40b1a 100644 --- a/urjtag/src/tap/register.c +++ b/urjtag/src/tap/register.c @@ -25,6 +25,8 @@ #include #include +#include +#include #include urj_tap_register_t * @@ -33,16 +35,25 @@ urj_tap_register_alloc (int len) urj_tap_register_t *tr; if (len < 1) + { + urj_error_set (URJ_ERROR_INVALID, "len < 1"); return NULL; + } tr = malloc (sizeof (urj_tap_register_t)); if (!tr) + { + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + sizeof (urj_tap_register_t)); return NULL; + } tr->data = malloc (len); if (!tr->data) { free (tr); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + (size_t) len); return NULL; } @@ -53,6 +64,8 @@ urj_tap_register_alloc (int len) { free (tr->data); free (tr); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, "malloc(%zd) fails", + (size_t) (len + 1)); return NULL; } @@ -66,7 +79,10 @@ urj_tap_register_t * urj_tap_register_duplicate (const urj_tap_register_t *tr) { if (!tr) + { + urj_error_set (URJ_ERROR_INVALID, "tr == NULL"); return NULL; + } return urj_tap_register_init (urj_tap_register_alloc (tr->len), urj_tap_register_get_string (tr)); diff --git a/urjtag/src/tap/tap.c b/urjtag/src/tap/tap.c index cd6bfc25..019c8ac4 100644 --- a/urjtag/src/tap/tap.c +++ b/urjtag/src/tap/tap.c @@ -26,6 +26,7 @@ #include +#include #include #include #include @@ -42,7 +43,7 @@ urj_tap_reset (urj_chain_t *chain) urj_tap_chain_clock (chain, 0, 0, 1); /* Run-Test/Idle */ } -void +int urj_tap_reset_bypass (urj_chain_t *chain) { urj_tap_reset (chain); @@ -54,10 +55,7 @@ urj_tap_reset_bypass (urj_chain_t *chain) urj_tap_register_t *ir = urj_tap_register_fill ( urj_tap_register_alloc (chain->total_instr_len), 1); if (!ir) - { - printf (_("out of memory\n")); - return; - } + return URJ_STATUS_FAIL; urj_tap_capture_ir (chain); urj_tap_shift_register (chain, ir, NULL, URJ_CHAIN_EXITMODE_IDLE); @@ -65,6 +63,8 @@ urj_tap_reset_bypass (urj_chain_t *chain) urj_part_parts_set_instruction (chain->parts, "BYPASS"); } + + return URJ_STATUS_OK; } void @@ -75,7 +75,7 @@ urj_tap_defer_shift_register (urj_chain_t *chain, int i; if (!(urj_tap_state (chain) & URJ_TAP_STATE_SHIFT)) - printf (_("%s: Invalid state: %2X\n"), "urj_tap_shift_register", + urj_log (URJ_LOG_LEVEL_NORMAL, _("%s: Invalid state: %2X\n"), __func__, urj_tap_state (chain)); /* Capture-DR, Capture-IR, Shift-DR, Shift-IR, Exit2-DR or Exit2-IR state */ @@ -147,8 +147,8 @@ urj_tap_capture_dr (urj_chain_t *chain) { if ((urj_tap_state (chain) & (URJ_TAP_STATE_RESET | URJ_TAP_STATE_IDLE)) != URJ_TAP_STATE_IDLE) - printf (_("%s: Invalid state: %2X\n"), "urj_tap_capture_dr", - urj_tap_state (chain)); + urj_log (URJ_LOG_LEVEL_NORMAL, _("%s: Invalid state: %2X\n"), __func__, + urj_tap_state (chain)); /* Run-Test/Idle or Update-DR or Update-IR state */ urj_tap_chain_defer_clock (chain, 1, 0, 1); /* Select-DR-Scan */ @@ -160,8 +160,8 @@ urj_tap_capture_ir (urj_chain_t *chain) { if ((urj_tap_state (chain) & (URJ_TAP_STATE_RESET | URJ_TAP_STATE_IDLE)) != URJ_TAP_STATE_IDLE) - printf (_("%s: Invalid state: %2X\n"), "urj_tap_capture_ir", - urj_tap_state (chain)); + urj_log (URJ_LOG_LEVEL_NORMAL, _("%s: Invalid state: %2X\n"), __func__, + urj_tap_state (chain)); /* Run-Test/Idle or Update-DR or Update-IR state */ urj_tap_chain_defer_clock (chain, 1, 0, 2); /* Select-DR-Scan, then Select-IR-Scan */ diff --git a/urjtag/src/tap/usbconn.c b/urjtag/src/tap/usbconn.c index e29af5f2..c1b9bf1c 100644 --- a/urjtag/src/tap/usbconn.c +++ b/urjtag/src/tap/usbconn.c @@ -29,6 +29,7 @@ #include +/* @@@@ RFHH stick these into a .h file */ #ifdef HAVE_LIBUSB extern urj_usbconn_driver_t urj_tap_usbconn_libusb_driver; #endif /* HAVE_LIBUSB */ diff --git a/urjtag/src/tap/usbconn/libftd2xx.c b/urjtag/src/tap/usbconn/libftd2xx.c index e5e41e97..91598169 100644 --- a/urjtag/src/tap/usbconn/libftd2xx.c +++ b/urjtag/src/tap/usbconn/libftd2xx.c @@ -39,16 +39,14 @@ #include -#include +#include +#include #include #include /* enables debug output */ -#undef DEBUG -#ifdef DEBUG static const char *module = "usbconn_ftd2xx_"; -#endif typedef struct @@ -72,11 +70,12 @@ typedef struct urj_usbconn_driver_t urj_tap_usbconn_ftd2xx_driver; urj_usbconn_driver_t urj_tap_usbconn_ftd2xx_mpsse_driver; -static int usbconn_ftd2xx_common_open (urj_usbconn_t *conn, int printerr); +static int usbconn_ftd2xx_common_open (urj_usbconn_t *conn, urj_log_level_t ll); static void usbconn_ftd2xx_free (urj_usbconn_t *conn); /* ---------------------------------------------------------------------- */ +/** @return number of flushed bytes on success; -1 on error */ static int usbconn_ftd2xx_flush (ftd2xx_param_t *p) { @@ -87,26 +86,25 @@ usbconn_ftd2xx_flush (ftd2xx_param_t *p) if (!p->fc) return -1; -#ifdef DEBUG - printf ("%sflush begin:\n", module); - printf (" send_buf_len %d, send_buffered %d\n", p->send_buf_len, - p->send_buffered); - printf (" recv_buf_len %d, to_recv %d\n", p->recv_buf_len, p->to_recv); - printf (" recv_write_idx %d, recv_read_idx %d\n", p->recv_write_idx, - p->recv_read_idx); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, "%sflush begin:\n", module); + urj_log (URJ_LOG_LEVEL_DETAIL, " send_buf_len %d, send_buffered %d\n", + p->send_buf_len, p->send_buffered); + urj_log (URJ_LOG_LEVEL_DETAIL, " recv_buf_len %d, to_recv %d\n", + p->recv_buf_len, p->to_recv); + urj_log (URJ_LOG_LEVEL_DETAIL, " recv_write_idx %d, recv_read_idx %d\n", + p->recv_write_idx, p->recv_read_idx); if (p->send_buffered == 0) return 0; - if ((status = - FT_Write (p->fc, p->send_buf, p->send_buffered, &xferred)) != FT_OK) - printf (_("%s(): FT_Write() failed.\n"), __FUNCTION__); + if ((status = FT_Write (p->fc, p->send_buf, p->send_buffered, + &xferred)) != FT_OK) + urj_error_set (URJ_ERROR_IO, _("FT_Write() failed")); if (xferred < p->send_buffered) { - printf (_("%s(): Written fewer bytes than requested.\n"), - __FUNCTION__); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Written fewer bytes than requested")); return -1; } @@ -125,29 +123,30 @@ usbconn_ftd2xx_flush (ftd2xx_param_t *p) if (!p->recv_buf) { - printf (_("%s(): Receive buffer does not exist.\n"), - __FUNCTION__); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Receive buffer does not exist")); return -1; } while (recvd == 0) if ((status = FT_Read (p->fc, &(p->recv_buf[p->recv_write_idx]), p->to_recv, &recvd)) != FT_OK) - printf (_("%s(): Error from FT_Read(): %d\n"), - __FUNCTION__, (int) status); + urj_error_set (URJ_ERROR_IO, _("Error from FT_Read(): %d"), + (int) status); if (recvd < p->to_recv) - printf (_("%s(): Received less bytes than requested.\n"), - __FUNCTION__); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("%s(): Received fewer bytes than requested.\n"), + __func__); p->to_recv -= recvd; p->recv_write_idx += recvd; } -#ifdef DEBUG - printf ("%sflush end: status %ld, xferred %ld, recvd %ld\n", module, + urj_log (URJ_LOG_LEVEL_DETAIL, + "%sflush end: status %ld, xferred %ld, recvd %ld\n", module, status, xferred, recvd); -#endif + return status != FT_OK ? -1 : xferred; } @@ -161,9 +160,7 @@ usbconn_ftd2xx_read (urj_usbconn_t *conn, uint8_t *buf, int len) FT_STATUS status = FT_OK; DWORD recvd = 0; -#ifdef DEBUG - printf ("%sread begin: len %d\n", module, len); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, "%sread begin: len %d\n", module, len); if (!p->fc) return -1; @@ -196,14 +193,13 @@ usbconn_ftd2xx_read (urj_usbconn_t *conn, uint8_t *buf, int len) while (recvd == 0) if ((status = FT_Read (p->fc, &(buf[cpy_len]), len, &recvd)) != FT_OK) - printf (_("%s(): Error from FT_Read(): %d\n"), __FUNCTION__, - (int) status); + urj_error_set (URJ_ERROR_IO, _("Error from FT_Read(): %d"), + (int) status); } -#ifdef DEBUG - printf ("%sread end : status %ld, length %d\n", module, status, - cpy_len + len); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, "%sread end : status %ld, length %d\n", + module, status, cpy_len + len); + return status != FT_OK ? -1 : cpy_len + len; } @@ -218,9 +214,8 @@ usbconn_ftd2xx_write (urj_usbconn_t *conn, uint8_t *buf, int len, int recv) if (!p->fc) return -1; -#ifdef DEBUG - printf ("%swrite begin: len %d, recv %d\n", module, len, recv); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, "%swrite begin: len %d, recv %d\n", module, + len, recv); /* this write function will try to buffer write data buffering will be ceased and a flush triggered in two cases. */ @@ -257,14 +252,15 @@ usbconn_ftd2xx_write (urj_usbconn_t *conn, uint8_t *buf, int len, int recv) xferred = usbconn_ftd2xx_flush (p); } -#ifdef DEBUG - printf ("%swrite end: xferred %d\n", module, xferred); -#endif + urj_log (URJ_LOG_LEVEL_DETAIL, "%swrite end: xferred %d\n", module, + xferred); + return xferred < 0 ? -1 : len; } else { - printf (_("%s(): Send buffer does not exist.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Send buffer does not exist")); return -1; } } @@ -292,7 +288,6 @@ usbconn_ftd2xx_connect (const char **param, int paramc, if (!p || !c || !p->send_buf || !p->recv_buf) { - printf (_("Out of memory\n")); if (p->send_buf) free (p->send_buf); if (p->recv_buf) @@ -301,6 +296,10 @@ usbconn_ftd2xx_connect (const char **param, int paramc, free (p); if (c) free (c); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + "malloc(%zd)/malloc(%zd)/malloc(%s) failed", + sizeof (urj_usbconn_t), sizeof (ftd2xx_param_t), + "p->send_buf_len"); return NULL; } @@ -316,14 +315,14 @@ usbconn_ftd2xx_connect (const char **param, int paramc, /* do a test open with the specified cable paramters, there's no other way to detect the presence of the specified USB device */ - if (usbconn_ftd2xx_common_open (c, 0) != 0) + if (usbconn_ftd2xx_common_open (c, URJ_LOG_LEVEL_DETAIL) != URJ_STATUS_OK) { usbconn_ftd2xx_free (c); return NULL; } FT_Close (p->fc); - printf (_("Connected to libftd2xx driver.\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Connected to libftd2xx driver.\n")); return c; } @@ -345,7 +344,7 @@ usbconn_ftd2xx_mpsse_connect (const char **param, int paramc, /* ---------------------------------------------------------------------- */ static int -usbconn_ftd2xx_common_open (urj_usbconn_t *conn, int printerr) +usbconn_ftd2xx_common_open (urj_usbconn_t *conn, urj_log_level_t ll) { ftd2xx_param_t *p = conn->params; FT_STATUS status; @@ -353,7 +352,7 @@ usbconn_ftd2xx_common_open (urj_usbconn_t *conn, int printerr) #if !__CYGWIN__ && !__MINGW32__ /* Add non-standard Vid/Pid to the linux driver */ if ((status = FT_SetVIDPID (p->vid, p->pid)) != FT_OK) - fprintf (stderr, "Warning: couldn't add %4.4x:%4.4x", p->vid, p->pid); + urj_warning ("couldn't add %4.4x:%4.4x", p->vid, p->pid); #endif /* try various methods to open a FTDI device */ @@ -374,14 +373,14 @@ usbconn_ftd2xx_common_open (urj_usbconn_t *conn, int printerr) if (status != FT_OK) { - if (printerr) - printf ("Unable to open FTDI device.\n"); + urj_error_set (URJ_ERROR_IO, "Unable to open TFDI device"); + urj_log (ll, "Unable to open FTDI device.\n"); /* mark ftd2xx layer as not initialized */ p->fc = NULL; - return -1; + return URJ_STATUS_FAIL; } - return 0; + return URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -393,24 +392,25 @@ usbconn_ftd2xx_open (urj_usbconn_t *conn) FT_HANDLE fc; FT_STATUS status; - if (usbconn_ftd2xx_common_open (conn, 1) < 0) - return -1; + if (usbconn_ftd2xx_common_open (conn, URJ_LOG_LEVEL_NORMAL) + != URJ_STATUS_OK) + return URJ_STATUS_FAIL; fc = p->fc; if ((status = FT_ResetDevice (fc)) != FT_OK) - printf (_("%s(): Can't reset device.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't reset device")); if (status == FT_OK) if ((status = FT_Purge (fc, FT_PURGE_RX)) != FT_OK) - printf (_("%s(): Can't purge RX buffer.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't purge RX buffer")); if (status == FT_OK) if ((status = FT_SetLatencyTimer (fc, 2)) != FT_OK) - printf (_("%s(): Can't set latency timer.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't set latency timer")); if (status == FT_OK) if ((status = FT_SetBaudRate (fc, 3E6)) != FT_OK) - printf (_("%s(): Can't set baudrate.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't set baudrate")); if (status != FT_OK) { @@ -419,7 +419,7 @@ usbconn_ftd2xx_open (urj_usbconn_t *conn) p->fc = NULL; } - return status != FT_OK ? -1 : 0; + return status != FT_OK ? URJ_STATUS_FAIL : URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -431,8 +431,9 @@ usbconn_ftd2xx_mpsse_open (urj_usbconn_t *conn) FT_HANDLE fc; FT_STATUS status; - if (usbconn_ftd2xx_common_open (conn, 1) < 0) - return -1; + if (usbconn_ftd2xx_common_open (conn, URJ_LOG_LEVEL_NORMAL) + != URJ_STATUS_OK) + return URJ_STATUS_FAIL; fc = p->fc; @@ -441,40 +442,39 @@ usbconn_ftd2xx_mpsse_open (urj_usbconn_t *conn) Ref. FTCJTAGPG10.pdf Intermittent problems will occur when certain steps are skipped. */ if ((status = FT_ResetDevice (fc)) != FT_OK) - printf (_("%s(): Can't reset device.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't reset device")); if (status == FT_OK) if ((status = FT_Purge (fc, FT_PURGE_RX)) != FT_OK) - printf (_("%s(): Can't purge RX buffer.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("s(): Can't purge RX buffer")); if (status == FT_OK) if ((status = FT_SetUSBParameters (fc, URJ_USBCONN_FTDX_MAXSEND_MPSSE, URJ_USBCONN_FTDX_MAXSEND_MPSSE)) != FT_OK) - printf (_("%s(): Can't set USB parameters.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't set USB parameters")); if (status == FT_OK) if ((status = FT_SetChars (fc, 0, 0, 0, 0)) != FT_OK) - printf (_("%s(): Can't set special characters.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't set special characters")); /* set a reasonable latency timer value if this value is too low then the chip will send intermediate result data in short packets (suboptimal performance) */ if (status == FT_OK) if ((status = FT_SetLatencyTimer (fc, 16)) != FT_OK) - printf (_("%s(): Can't set target latency timer.\n"), - __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't set target latency timer")); if (status == FT_OK) if ((status = FT_SetBitMode (fc, 0x0b, 0x02 /* BITMODE_MPSSE */ )) != FT_OK) - printf (_("%s(): Can't set MPSSE bitmode.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't set MPSSE bitmode")); if (status == FT_OK) if ((status = FT_ResetDevice (fc)) != FT_OK) - printf (_("%s(): Can't reset device.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't reset device")); if (status == FT_OK) if ((status = FT_Purge (fc, FT_PURGE_RX)) != FT_OK) - printf (_("%s(): Can't purge RX buffer.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't purge RX buffer")); /* set TCK Divisor */ if (status == FT_OK) @@ -496,10 +496,10 @@ usbconn_ftd2xx_mpsse_open (urj_usbconn_t *conn) if (status == FT_OK) if ((status = FT_ResetDevice (fc)) != FT_OK) - printf (_("%s(): Can't reset device.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't reset device")); if (status == FT_OK) if ((status = FT_Purge (fc, FT_PURGE_RX)) != FT_OK) - printf (_("%s(): Can't purge RX buffer.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_IO, _("Can't purge RX buffer")); if (status != FT_OK) { @@ -508,7 +508,7 @@ usbconn_ftd2xx_mpsse_open (urj_usbconn_t *conn) p->fc = NULL; } - return status != FT_OK ? -1 : 0; + return status != FT_OK ? URJ_STATUS_FAIL : URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -524,7 +524,7 @@ usbconn_ftd2xx_close (urj_usbconn_t *conn) p->fc = NULL; } - return 0; + return URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ diff --git a/urjtag/src/tap/usbconn/libftdi.c b/urjtag/src/tap/usbconn/libftdi.c index 06d2c03c..3b042181 100644 --- a/urjtag/src/tap/usbconn/libftdi.c +++ b/urjtag/src/tap/usbconn/libftdi.c @@ -39,7 +39,8 @@ #include -#include +#include +#include #include #include @@ -64,11 +65,12 @@ typedef struct urj_usbconn_driver_t urj_tap_usbconn_ftdi_driver; urj_usbconn_driver_t urj_tap_usbconn_ftdi_mpsse_driver; -static int usbconn_ftdi_common_open (urj_usbconn_t *conn, int printerr); +static int usbconn_ftdi_common_open (urj_usbconn_t *conn, urj_log_level_t ll); static void usbconn_ftdi_free (urj_usbconn_t *conn); /* ---------------------------------------------------------------------- */ +/** @return number of bytes flushed; -1 on error */ static int usbconn_ftdi_flush (ftdi_param_t *p) { @@ -81,15 +83,14 @@ usbconn_ftdi_flush (ftdi_param_t *p) if (p->send_buffered == 0) return 0; - if ((xferred = - ftdi_write_data (p->fc, p->send_buf, p->send_buffered)) < 0) - printf (_("%s(): ftdi_write_data() failed: %s\n"), __FUNCTION__, - ftdi_get_error_string (p->fc)); + if ((xferred = ftdi_write_data (p->fc, p->send_buf, p->send_buffered)) < 0) + urj_error_set (URJ_ERROR_IO, _("ftdi_write_data() failed: %s"), + ftdi_get_error_string (p->fc)); if (xferred < p->send_buffered) { - printf (_("%s(): Written fewer bytes than requested.\n"), - __FUNCTION__); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Written fewer bytes than requested")); return -1; } @@ -108,21 +109,23 @@ usbconn_ftdi_flush (ftdi_param_t *p) if (!p->recv_buf) { - printf (_("%s(): Receive buffer does not exist.\n"), - __FUNCTION__); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Receive buffer does not exist")); return -1; } while (recvd == 0) - if ((recvd = - ftdi_read_data (p->fc, &(p->recv_buf[p->recv_write_idx]), - p->to_recv)) < 0) - printf (_("%s(): Error from ftdi_read_data(): %s\n"), - __FUNCTION__, ftdi_get_error_string (p->fc)); + if ((recvd = ftdi_read_data (p->fc, + &(p->recv_buf[p->recv_write_idx]), + p->to_recv)) < 0) + urj_error_set (URJ_ERROR_IO, + _("Error from ftdi_read_data(): %s"), + ftdi_get_error_string (p->fc)); if (recvd < p->to_recv) - printf (_("%s(): Received less bytes than requested.\n"), - __FUNCTION__); + urj_log (URJ_LOG_LEVEL_NORMAL, + _("%s(): Received fewer bytes than requested.\n"), + __func__); p->to_recv -= recvd; p->recv_write_idx += recvd; @@ -133,6 +136,7 @@ usbconn_ftdi_flush (ftdi_param_t *p) /* ---------------------------------------------------------------------- */ +/** @return number of bytes read; -1 on error */ static int usbconn_ftdi_read (urj_usbconn_t *conn, uint8_t *buf, int len) { @@ -170,8 +174,9 @@ usbconn_ftdi_read (urj_usbconn_t *conn, uint8_t *buf, int len) /* need to get more data directly from the device */ while (recvd == 0) if ((recvd = ftdi_read_data (p->fc, &(buf[cpy_len]), len)) < 0) - printf (_("%s(): Error from ftdi_read_data(): %s\n"), - __FUNCTION__, ftdi_get_error_string (p->fc)); + urj_error_set (URJ_ERROR_IO, + _("Error from ftdi_read_data(): %s"), + ftdi_get_error_string (p->fc)); } return recvd < 0 ? -1 : cpy_len + len; @@ -179,6 +184,7 @@ usbconn_ftdi_read (urj_usbconn_t *conn, uint8_t *buf, int len) /* ---------------------------------------------------------------------- */ +/** @return number of bytes written; -1 on error */ static int usbconn_ftdi_write (urj_usbconn_t *conn, uint8_t *buf, int len, int recv) { @@ -227,7 +233,8 @@ usbconn_ftdi_write (urj_usbconn_t *conn, uint8_t *buf, int len, int recv) } else { - printf (_("%s(): Send buffer does not exist.\n"), __FUNCTION__); + urj_error_set (URJ_ERROR_ILLEGAL_STATE, + _("Send buffer does not exist")); return -1; } } @@ -256,7 +263,6 @@ usbconn_ftdi_connect (const char **param, int paramc, if (!p || !c || !fc || !p->send_buf || !p->recv_buf) { - printf (_("Out of memory\n")); if (p->send_buf) free (p->send_buf); if (p->recv_buf) @@ -267,6 +273,11 @@ usbconn_ftdi_connect (const char **param, int paramc, free (c); if (fc) free (fc); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + "malloc(%zd)/malloc(%zd)/malloc(%zd)/malloc(%s)/malloc(%s) failed", + sizeof (urj_usbconn_t), sizeof (ftdi_param_t), + sizeof (struct ftdi_context), + "p->send_buf_len", "p->recv_buf_len"); return NULL; } @@ -283,14 +294,14 @@ usbconn_ftdi_connect (const char **param, int paramc, /* do a test open with the specified cable paramters, alternatively we could use libusb to detect the presence of the specified USB device */ - if (usbconn_ftdi_common_open (c, 0) != 0) + if (usbconn_ftdi_common_open (c, URJ_LOG_LEVEL_DETAIL) != 0) { usbconn_ftdi_free (c); return NULL; } ftdi_usb_close (fc); - printf (_("Connected to libftdi driver.\n")); + urj_log (URJ_LOG_LEVEL_NORMAL, _("Connected to libftdi driver.\n")); return c; } @@ -312,7 +323,7 @@ usbconn_ftdi_mpsse_connect (const char **param, int paramc, /* ---------------------------------------------------------------------- */ static int -usbconn_ftdi_common_open (urj_usbconn_t *conn, int printerr) +usbconn_ftdi_common_open (urj_usbconn_t *conn, urj_log_level_t ll) { ftdi_param_t *p = conn->params; struct ftdi_context *fc = p->fc; @@ -328,18 +339,20 @@ usbconn_ftdi_common_open (urj_usbconn_t *conn, int printerr) if (status < 0) { /* device not found == -3 */ - if (printerr || (status != -3)) - printf (_("%s() failed: %s\n"), __FUNCTION__, - ftdi_get_error_string (fc)); + if (status != -3) + urj_log (ll, _("%s(): ftdi_usb_open_desc() failed: %s"), + __func__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_usb_open_desc() failed: %s"), + ftdi_get_error_string (fc)); ftdi_deinit (fc); /* mark ftdi layer as not initialized */ p->fc = NULL; /* TODO: disconnect? */ - return -1; + return URJ_STATUS_FAIL; } - return 0; + return URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -354,12 +367,12 @@ seq_purge (struct ftdi_context *fc, int purge_rx, int purge_tx) #ifndef LIBFTDI_UNIMPLEMENTED if ((r = ftdi_usb_purge_buffers (fc)) < 0) - printf (_("%s(): ftdi_usb_purge_buffers() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_usb_purge_buffers() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) if ((r = ftdi_read_data (fc, &buf, 1)) < 0) - printf (_("%s(): ftdi_read_data() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_read_data() failed: %s"), + ftdi_get_error_string (fc)); #else /* not yet available */ { int rx_loop; @@ -367,24 +380,24 @@ seq_purge (struct ftdi_context *fc, int purge_rx, int purge_tx) if (purge_rx) for (rx_loop = 0; (rx_loop < 6) && (r >= 0); rx_loop++) if ((r = ftdi_usb_purge_rx_buffer (fc)) < 0) - printf (_ - ("%s(): ftdi_usb_purge_rx_buffer() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, + _("ftdi_usb_purge_rx_buffer() failed: %s"), + ftdi_get_error_string (fc)); if (purge_tx) if (r >= 0) if ((r = ftdi_usb_purge_tx_buffer (fc)) < 0) - printf (_ - ("%s(): ftdi_usb_purge_tx_buffer() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, + _("ftdi_usb_purge_tx_buffer() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) if ((r = ftdi_read_data (fc, &buf, 1)) < 0) - printf (_("%s(): ftdi_read_data() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_read_data() failed: %s"), + ftdi_get_error_string (fc)); } #endif - return r < 0 ? -1 : 0; + return r < 0 ? URJ_STATUS_FAIL : URJ_STATUS_OK; } static int @@ -396,17 +409,17 @@ seq_reset (struct ftdi_context *fc) { unsigned short status; if ((r = ftdi_poll_status (fc, &status)) < 0) - printf (_("%s(): ftdi_poll_status() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_poll_status() failed: %s"), + ftdi_get_error_string (fc)); } #endif if ((r = ftdi_usb_reset (fc)) < 0) - printf (_("%s(): ftdi_usb_reset() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_usb_reset() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) r = seq_purge (fc, 1, 1); - return r < 0 ? -1 : 0; + return r < 0 ? URJ_STATUS_FAIL : URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -418,8 +431,8 @@ usbconn_ftdi_open (urj_usbconn_t *conn) struct ftdi_context *fc = p->fc; int r; - if (usbconn_ftdi_common_open (conn, 1) < 0) - return -1; + if (usbconn_ftdi_common_open (conn, URJ_LOG_LEVEL_NORMAL) < 0) + return URJ_STATUS_FAIL; r = seq_reset (fc); if (r >= 0) @@ -427,8 +440,9 @@ usbconn_ftdi_open (urj_usbconn_t *conn) if (r >= 0) if ((r = ftdi_set_latency_timer (fc, 2)) < 0) - printf (_("%s(): ftdi_set_latency_timer() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, + _("ftdi_set_latency_timer() failed: %s"), + ftdi_get_error_string (fc)); #if 0 /* libftdi 0.6 doesn't allow high baudrates, so we send the control @@ -437,14 +451,14 @@ usbconn_ftdi_open (urj_usbconn_t *conn) if (usb_control_msg (fc->usb_dev, 0x40, 3, 1, 0, NULL, 0, fc->usb_write_timeout) != 0) { - printf ("Can't set max baud rate.\n"); + urj_error_set (URJ_ERROR_IO, "Can't set max baud rate"); r = -1; } #else if (r >= 0) if ((r = ftdi_set_baudrate (fc, 3E6)) < 0) - printf (_("%s(): ftdi_set_baudrate() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_set_baudrate() failed: %s"), + ftdi_get_error_string (fc)); #endif if (r < 0) @@ -455,7 +469,7 @@ usbconn_ftdi_open (urj_usbconn_t *conn) p->fc = NULL; } - return r < 0 ? -1 : 0; + return r < 0 ? URJ_STATUS_FAIL : URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -467,8 +481,8 @@ usbconn_ftdi_mpsse_open (urj_usbconn_t *conn) struct ftdi_context *fc = p->fc; int r; - if (usbconn_ftdi_common_open (conn, 1) < 0) - return -1; + if (usbconn_ftdi_common_open (conn, URJ_LOG_LEVEL_NORMAL) < 0) + return URJ_STATUS_FAIL; /* This sequence might seem weird and containing superfluous stuff. However, it's built after the description of JTAG_InitDevice @@ -505,18 +519,19 @@ usbconn_ftdi_mpsse_open (urj_usbconn_t *conn) in short packets (suboptimal performance) */ if (r >= 0) if ((r = ftdi_set_latency_timer (fc, 16)) < 0) - printf (_("%s(): ftdi_set_latency_timer() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, + _("ftdi_set_latency_timer() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) if ((r = ftdi_set_bitmode (fc, 0x0b, BITMODE_MPSSE)) < 0) - printf (_("%s(): ftdi_set_bitmode() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_set_bitmode() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) if ((r = ftdi_usb_reset (fc)) < 0) - printf (_("%s(): ftdi_usb_reset() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_usb_reset() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) r = seq_purge (fc, 1, 0); @@ -537,8 +552,8 @@ usbconn_ftdi_mpsse_open (urj_usbconn_t *conn) if (r >= 0) if ((r = ftdi_usb_reset (fc)) < 0) - printf (_("%s(): ftdi_usb_reset() failed: %s\n"), - __FUNCTION__, ftdi_get_error_string (fc)); + urj_error_set (URJ_ERROR_IO, _("ftdi_usb_reset() failed: %s"), + ftdi_get_error_string (fc)); if (r >= 0) r = seq_purge (fc, 1, 0); @@ -550,7 +565,7 @@ usbconn_ftdi_mpsse_open (urj_usbconn_t *conn) p->fc = NULL; } - return r < 0 ? -1 : 0; + return r < 0 ? URJ_STATUS_FAIL : URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -567,7 +582,7 @@ usbconn_ftdi_close (urj_usbconn_t *conn) p->fc = NULL; } - return 0; + return URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ diff --git a/urjtag/src/tap/usbconn/libusb.c b/urjtag/src/tap/usbconn/libusb.c index 7d817a8e..a65d524c 100644 --- a/urjtag/src/tap/usbconn/libusb.c +++ b/urjtag/src/tap/usbconn/libusb.c @@ -40,10 +40,11 @@ #include #include #include +#include #include -#include -#include +#include +#include #include typedef struct @@ -69,7 +70,8 @@ libusb_match_desc (struct usb_device *dev, char *desc) handle = usb_open (dev); if (handle == NULL) { - perror ("libusb: usb_open() failed"); + urj_error_set (URJ_ERROR_IO, "usb_open() failed: %s", strerror(errno)); + errno = 0; return 0; } if (dev->descriptor.iManufacturer) @@ -121,12 +123,16 @@ usbconn_libusb_connect (const char **param, int paramc, usb_init (); if (usb_find_busses () < 0) { - perror ("libusb: usb_find_busses failed"); + urj_error_set (URJ_ERROR_IO, "usb_find_busses() failed: %s", + strerror(errno)); + errno = 0; return NULL; } if (usb_find_devices () < 0) { - perror ("libusb: usb_find_devices failed"); + urj_error_set (URJ_ERROR_IO, "usb_find_devices() failed: %s", + strerror(errno)); + errno = 0; return NULL; } @@ -151,6 +157,7 @@ usbconn_libusb_connect (const char **param, int paramc, if (!found_dev) { + urj_error_set (URJ_ERROR_NOTFOUND, "no USB connections"); return NULL; } @@ -158,7 +165,10 @@ usbconn_libusb_connect (const char **param, int paramc, libusb_params = malloc (sizeof (urj_usbconn_libusb_param_t)); if (libusb_params == NULL || libusb_conn == NULL) { - printf (_("Out of memory\n")); + urj_error_set (URJ_ERROR_OUT_OF_MEMORY, + _("malloc(%zd)/malloc(%zd) fails"), + sizeof (urj_usbconn_t), + sizeof (urj_usbconn_libusb_param_t)); if (libusb_params) free (libusb_params); if (libusb_conn) @@ -186,7 +196,8 @@ usbconn_libusb_open (urj_usbconn_t *conn) p->handle = usb_open (p->dev); if (p->handle == NULL) { - perror ("libusb: usb_open() failed"); + urj_error_set (URJ_ERROR_IO, "usb_open() failed: %s", strerror(errno)); + errno = 0; } else { @@ -196,8 +207,10 @@ usbconn_libusb_open (urj_usbconn_t *conn) #endif if (usb_claim_interface (p->handle, 0) != 0) { - perror ("libusb: usb_claim_interface failed"); usb_close (p->handle); + urj_error_set (URJ_ERROR_IO, "usb_claim_interface failed: %s", + strerror(errno)); + errno = 0; p->handle = NULL; } #if 1 @@ -211,10 +224,10 @@ usbconn_libusb_open (urj_usbconn_t *conn) if (p->handle == NULL) { /* TODO: disconnect? */ - return -1; + return URJ_STATUS_FAIL; } - return 0; + return URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */ @@ -229,7 +242,7 @@ usbconn_libusb_close (urj_usbconn_t *conn) usb_close (p->handle); } p->handle = NULL; - return 0; + return URJ_STATUS_OK; } /* ---------------------------------------------------------------------- */