From 3dd58e872ae0a555b9dad21787548b9ed8a812af Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Thu, 21 Feb 2013 23:47:27 +0100 Subject: [PATCH 01/28] Missing release date in ChangeLog --- ChangeLog | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index bcc6f2d..402e560 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ -Feb TBD, 2013 - 1.7.0-rc5 (release candidate) +Feb 16, 2013 - 1.7.0-rc5 (release candidate) +-------------------------------------------- Fixes: - Add missing sample configuration file in archive From e070decd3cbd930507c714c7214526c4ac6baa46 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 22 Feb 2013 00:00:07 +0100 Subject: [PATCH 02/28] Updates ChangeLog with recent fixes --- ChangeLog | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ChangeLog b/ChangeLog index 402e560..00ec8eb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +TDB, 2013 - 1.7.0-rc6 (release candidate) +----------------------------------------- + +Fixes: + - Fix minor memory leak with nfc_drivers + - Fix stack smash while displaying long data transmission (LOG_HEX) + - pn53x-tamashell: allow larger commands up to full extended frame + Feb 16, 2013 - 1.7.0-rc5 (release candidate) -------------------------------------------- From 3e87abe88a6e90cf285b473d104e140e08e52967 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 1 Mar 2013 16:42:48 +0100 Subject: [PATCH 03/28] Uses new email for bugs, reports, etc. --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 2fa04ef..d6c6ec1 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ # General init # /!\ Don't forget to update 'CMakeLists.txt' too /!\ -AC_INIT([libnfc],[1.7.0-rc5],[info@libnfc.org]) +AC_INIT([libnfc],[1.7.0-rc5],[nfc-tools@googlegroups.com]) AC_CONFIG_MACRO_DIR([m4]) From 2506ee35f62355495d010d847d1bc9e7ebd93fa0 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 1 Mar 2013 16:43:49 +0100 Subject: [PATCH 04/28] Fixes invalid example in nfc-scan-device manpage --- utils/nfc-scan-device.1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/nfc-scan-device.1 b/utils/nfc-scan-device.1 index cccb28b..347674e 100644 --- a/utils/nfc-scan-device.1 +++ b/utils/nfc-scan-device.1 @@ -28,10 +28,10 @@ to allow intrusive scan (eg. serial ports scan). This is equivalent to set envir .SH EXAMPLE For a SCL3711 device (in verbose mode): -- ACS / ACR122U PICC Interface: - acr122_usb:002:005 - chip: PN532 v1.4 - initator mode modulations: ISO/IEC 14443A (106 kbps), FeliCa (424 kbps, 212 kbps), ISO/IEC 14443-4B (106 kbps), Innovision Jewel (106 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps) + - SCM Micro / SCL3711-NFC&RW: + pn53x_usb:002:017 + chip: PN533 v2.7 + initator mode modulations: ISO/IEC 14443A (106 kbps), FeliCa (424 kbps, 212 kbps), ISO/IEC 14443-4B (847 kbps, 424 kbps, 212 kbps, 106 kbps), Innovision Jewel (106 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps) target mode modulations: ISO/IEC 14443A (106 kbps), FeliCa (424 kbps, 212 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps) .SH BUGS From 59227c3dd3421d086a6d03cdd7b4fab033e3ff55 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Fri, 1 Mar 2013 17:05:21 +0100 Subject: [PATCH 05/28] Adds missing version.rc.in file in tarball (for Windows users) --- ChangeLog | 5 +++-- contrib/win32/Makefile.am | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 00ec8eb..e4b8536 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,11 @@ -TDB, 2013 - 1.7.0-rc6 (release candidate) ------------------------------------------ +TBD dd, 2013 - 1.7.0-rc6 (release candidate) +-------------------------------------------- Fixes: - Fix minor memory leak with nfc_drivers - Fix stack smash while displaying long data transmission (LOG_HEX) - pn53x-tamashell: allow larger commands up to full extended frame + - Add missing windows file in archive (version.rc.in) Feb 16, 2013 - 1.7.0-rc5 (release candidate) -------------------------------------------- diff --git a/contrib/win32/Makefile.am b/contrib/win32/Makefile.am index 9bcd866..e007de8 100644 --- a/contrib/win32/Makefile.am +++ b/contrib/win32/Makefile.am @@ -4,5 +4,5 @@ EXTRA_DIST = \ err.h \ nfc.def \ stdlib.c \ - unistd.h + unistd.h \ version.rc.in From 9dcf7378b6d0def9e02c9512afb80a4be8f52b43 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 01:08:07 +0100 Subject: [PATCH 06/28] Fix mem leak with libusb by introducing buses/usbbus.c Now call only once usb_init(); usb_find_busses(); usb_find_devices() instead of multiple calls in several scan() then open() This fixes the following leaks: ==1159== 8 bytes in 1 blocks are definitely lost in loss record 9 of 102 ==1159== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==1159== by 0x53D9931: usb_parse_configuration (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53DB8B1: usb_os_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53D8FDC: usb_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x4E41D79: pn53x_usb_scan (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==1159== ==1159== 8 bytes in 1 blocks are definitely lost in loss record 10 of 102 ==1159== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==1159== by 0x53D9931: usb_parse_configuration (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53DB8B1: usb_os_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x53D8FDC: usb_find_devices (in /lib/x86_64-linux-gnu/libusb-0.1.so.4.4.4) ==1159== by 0x4E42CC7: pn53x_usb_open (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==1159== by 0x4E351E6: nfc_open (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) --- libnfc/buses/Makefile.am | 2 +- libnfc/buses/usbbus.c | 61 +++++++++++++++++++++++++++++++++++++ libnfc/buses/usbbus.h | 49 +++++++++++++++++++++++++++++ libnfc/drivers/acr122_usb.c | 51 +++---------------------------- libnfc/drivers/pn53x_usb.c | 54 +++----------------------------- 5 files changed, 120 insertions(+), 97 deletions(-) create mode 100644 libnfc/buses/usbbus.c create mode 100644 libnfc/buses/usbbus.h diff --git a/libnfc/buses/Makefile.am b/libnfc/buses/Makefile.am index 2ce0720..23cf46f 100644 --- a/libnfc/buses/Makefile.am +++ b/libnfc/buses/Makefile.am @@ -3,7 +3,7 @@ AM_CPPFLAGS = $(all_includes) $(LIBNFC_CFLAGS) noinst_LTLIBRARIES = libnfcbuses.la -libnfcbuses_la_SOURCES = uart.c uart.h +libnfcbuses_la_SOURCES = uart.c uart.h usbbus.c usbbus.h libnfcbuses_la_CFLAGS = -I$(top_srcdir)/libnfc EXTRA_DIST = uart_posix.c uart_win32.c diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c new file mode 100644 index 0000000..fcf4233 --- /dev/null +++ b/libnfc/buses/usbbus.c @@ -0,0 +1,61 @@ +/*- + * Public platform independent Near Field Communication (NFC) library + * + * Copyright (C) 2013, Romuald Conty + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/** + * @file usbbus.c + * @brief libusb 0.1 driver wrapper + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#include "usbbus.h" +#include "log.h" +#define LOG_CATEGORY "libnfc.buses.usbbus" +#define LOG_GROUP NFC_LOG_GROUP_DRIVER + +// Global flag to know if usb_init() has already been called or not +bool usb_initialized=false; + +int usb_prepare(void) { + if (usb_initialized) + return 0; + usb_init(); + usb_initialized = true; + + int res; + // usb_find_busses will find all of the busses on the system. Returns the + // number of changes since previous call to this function (total of new + // busses and busses removed). + if ((res = usb_find_busses()) < 0) { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); + return -1; + } + // usb_find_devices will find all of the devices on each bus. This should be + // called after usb_find_busses. Returns the number of changes since the + // previous call to this function (total of new device and devices removed). + if ((res = usb_find_devices()) < 0) { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); + return -1; + } + return 0; +} + diff --git a/libnfc/buses/usbbus.h b/libnfc/buses/usbbus.h new file mode 100644 index 0000000..5c08137 --- /dev/null +++ b/libnfc/buses/usbbus.h @@ -0,0 +1,49 @@ +/*- + * Public platform independent Near Field Communication (NFC) library + * + * Copyright (C) 2013, Romuald Conty + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + * + */ + +/** + * @file usbbus.h + * @brief libusb 0.1 driver header + */ + +#ifndef __NFC_BUS_USB_H__ +# define __NFC_BUS_USB_H__ + +#ifndef _WIN32 +// Under POSIX system, we use libusb (>= 0.1.12) +#include +#define USB_TIMEDOUT ETIMEDOUT +#define _usb_strerror( X ) strerror(-X) +#else +// Under Windows we use libusb-win32 (>= 1.2.5) +#include +#define USB_TIMEDOUT 116 +#define _usb_strerror( X ) usb_strerror() +#endif + +#include +#include + +// Global flag to know if usb_init() has already been called or not +extern bool usb_initialized; + +int usb_prepare(void); + +#endif // __NFC_BUS_USB_H__ diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index da14a60..5f35234 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -54,24 +54,12 @@ Thanks to d18c7db and Okko for example code #include #include #include - -#ifndef _WIN32 -// Under POSIX system, we use libusb (>= 0.1.12) -#include -#define USB_TIMEDOUT ETIMEDOUT -#define _usb_strerror( X ) strerror(-X) -#else -// Under Windows we use libusb-win32 (>= 1.2.5) -#include -#define USB_TIMEDOUT 116 -#define _usb_strerror( X ) usb_strerror() -#endif - #include #include #include "nfc-internal.h" +#include "buses/usbbus.h" #include "chips/pn53x.h" #include "chips/pn53x-internal.h" #include "drivers/acr122_usb.h" @@ -309,23 +297,8 @@ static size_t acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void)context; - usb_init(); - int res; - // usb_find_busses will find all of the busses on the system. Returns the - // number of changes since previous call to this function (total of new - // busses and busses removed). - if ((res = usb_find_busses()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - return 0; - } - // usb_find_devices will find all of the devices on each bus. This should be - // called after usb_find_busses. Returns the number of changes since the - // previous call to this function (total of new device and devices removed). - if ((res = usb_find_devices()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - return 0; - } + usb_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; @@ -446,23 +419,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) struct usb_bus *bus; struct usb_device *dev; - usb_init(); - - int res; - // usb_find_busses will find all of the busses on the system. Returns the - // number of changes since previous call to this function (total of new - // busses and busses removed). - if ((res = usb_find_busses()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - goto free_mem; - } - // usb_find_devices will find all of the devices on each bus. This should be - // called after usb_find_busses. Returns the number of changes since the - // previous call to this function (total of new device and devices removed). - if ((res = usb_find_devices()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - goto free_mem; - } + usb_prepare(); for (bus = usb_get_busses(); bus; bus = bus->next) { if (connstring_decode_level > 1) { @@ -483,7 +440,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) // Retrieve end points acr122_usb_get_end_points(dev, &data); // Claim interface - res = usb_claim_interface(data.pudh, 0); + int res = usb_claim_interface(data.pudh, 0); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror(res)); usb_close(data.pudh); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 6340cb7..905a951 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -37,24 +37,12 @@ Thanks to d18c7db and Okko for example code #include #include #include - -#ifndef _WIN32 -// Under POSIX system, we use libusb (>= 0.1.12) -#include -#define USB_TIMEDOUT ETIMEDOUT -#define _usb_strerror( X ) strerror(-X) -#else -// Under Windows we use libusb-win32 (>= 1.2.5) -#include -#define USB_TIMEDOUT 116 -#define _usb_strerror( X ) usb_strerror() -#endif - #include #include #include "nfc-internal.h" +#include "buses/usbbus.h" #include "chips/pn53x.h" #include "chips/pn53x-internal.h" #include "drivers/pn53x_usb.h" @@ -186,23 +174,7 @@ static size_t pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void)context; - usb_init(); - int res; - // usb_find_busses will find all of the busses on the system. Returns the - // number of changes since previous call to this function (total of new - // busses and busses removed). - if ((res = usb_find_busses()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - return 0; - } - // usb_find_devices will find all of the devices on each bus. This should be - // called after usb_find_busses. Returns the number of changes since the - // previous call to this function (total of new device and devices removed). - if ((res = usb_find_devices()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - return 0; - } - + usb_prepare(); size_t device_found = 0; uint32_t uiBusIndex = 0; struct usb_bus *bus; @@ -227,7 +199,7 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s usb_dev_handle *udev = usb_open(dev); // Set configuration - res = usb_set_configuration(udev, 1); + int res = usb_set_configuration(udev, 1); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); usb_close(udev); @@ -330,23 +302,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) struct usb_bus *bus; struct usb_device *dev; - usb_init(); - - int res; - // usb_find_busses will find all of the busses on the system. Returns the - // number of changes since previous call to this function (total of new - // busses and busses removed). - if ((res = usb_find_busses()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror(res)); - goto free_mem; - } - // usb_find_devices will find all of the devices on each bus. This should be - // called after usb_find_busses. Returns the number of changes since the - // previous call to this function (total of new device and devices removed). - if ((res = usb_find_devices()) < 0) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror(res)); - goto free_mem; - } + usb_prepare(); for (bus = usb_get_busses(); bus; bus = bus->next) { if (connstring_decode_level > 1) { @@ -365,7 +321,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) // Retrieve end points pn53x_usb_get_end_points(dev, &data); // Set configuration - res = usb_set_configuration(data.pudh, 1); + int res = usb_set_configuration(data.pudh, 1); if (res < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror(res)); if (EPERM == -res) { From 3d9ebb5044a9fb77ad253a1e20758811013fce0d Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 01:09:47 +0100 Subject: [PATCH 07/28] Fix mem leak in config file parser This fixes the following leaks: ==18690== 256 bytes in 1 blocks are definitely lost in loss record 75 of 100 ==18690== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==18690== by 0x511613F: regcomp (regcomp.c:487) ==18690== by 0x4E34313: conf_parse_file (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== by 0x4E349F5: conf_devices_load.constprop.1 (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== by 0x4E362C0: nfc_context_new (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== by 0x4E34D08: nfc_init (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== ==18690== 32,068 (224 direct, 31,844 indirect) bytes in 1 blocks are definitely lost in loss record 98 of 100 ==18690== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==18690== by 0x4C28D6F: realloc (vg_replace_malloc.c:632) ==18690== by 0x5115DF3: re_compile_internal (regcomp.c:760) ==18690== by 0x51161AB: regcomp (regcomp.c:506) ==18690== by 0x4E34313: conf_parse_file (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== by 0x4E349F5: conf_devices_load.constprop.1 (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== by 0x4E362C0: nfc_context_new (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==18690== by 0x4E34D08: nfc_init (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) --- libnfc/conf.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libnfc/conf.c b/libnfc/conf.c index 87aec26..ca5f801 100644 --- a/libnfc/conf.c +++ b/libnfc/conf.c @@ -66,6 +66,7 @@ conf_parse_file(const char *filename, void (*conf_keyvalue)(void *data, const ch regmatch_t *pmatch = malloc(sizeof(*pmatch) * nmatch); if (!pmatch) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Not enough memory: malloc failed."); + regfree(&preg); return false; } @@ -98,6 +99,7 @@ conf_parse_file(const char *filename, void (*conf_keyvalue)(void *data, const ch } free(pmatch); + regfree(&preg); return false; } From d7e7abb0619a03d63aa41e7fc385345258481cf3 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 01:21:13 +0100 Subject: [PATCH 08/28] Fix mem leak in config file parser (opendir) This fixes the following leaks: ==30517== 32,808 bytes in 1 blocks are definitely lost in loss record 11 of 12 ==30517== at 0x4C28BED: malloc (vg_replace_malloc.c:263) ==30517== by 0x50F4B2A: __alloc_dir (opendir.c:186) ==30517== by 0x4E34998: conf_devices_load.constprop.1 (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==30517== by 0x4E36350: nfc_context_new (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) ==30517== by 0x4E34D98: nfc_init (in /usr/lib/x86_64-linux-gnu/libnfc.so.4.0.0) --- libnfc/conf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/libnfc/conf.c b/libnfc/conf.c index ca5f801..4e19911 100644 --- a/libnfc/conf.c +++ b/libnfc/conf.c @@ -183,6 +183,7 @@ conf_devices_load(const char *dirname, nfc_context *context) } } } + closedir(d); } void From 35b08dc5cc1113e28f12c8d8a4ad4e5a8d434a4b Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 01:39:59 +0100 Subject: [PATCH 09/28] Add new symbols from usbbus.c --- debian/libnfc4.symbols | 2 ++ 1 file changed, 2 insertions(+) diff --git a/debian/libnfc4.symbols b/debian/libnfc4.symbols index df9dc8e..48a856d 100644 --- a/debian/libnfc4.symbols +++ b/debian/libnfc4.symbols @@ -54,3 +54,5 @@ libnfc.so.4 libnfc4 #MINVER# str_nfc_baud_rate@Base 1.7.0~rc2 str_nfc_modulation_type@Base 1.7.0~rc2 str_nfc_target@Base 1.7.0~rc2 + usb_initialized@Base 1.7.0~rc6-0 + usb_prepare@Base 1.7.0~rc6-0 From 07082792151eabbf4670d8e00c5ea149634b40a1 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 02:25:13 +0100 Subject: [PATCH 10/28] malloc/free: some cleaning & checking malloc errors --- libnfc/drivers/acr122_pcsc.c | 14 ++++++++++++-- libnfc/drivers/acr122_usb.c | 19 +++++++++++++++++++ libnfc/drivers/acr122s.c | 10 ++++++++++ libnfc/drivers/arygon.c | 8 ++++++++ libnfc/drivers/pn532_uart.c | 8 ++++++++ libnfc/drivers/pn53x_usb.c | 19 +++++++++++++++++++ 6 files changed, 76 insertions(+), 2 deletions(-) diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 105a392..1bb9b8e 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -261,9 +261,15 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1) return NULL; nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); - size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1); - if (szDeviceFound < index + 1) + if (!ncs) { + perror("malloc"); return NULL; + } + size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1); + if (szDeviceFound < index + 1) { + free(ncs); + return NULL; + } strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring)); free(ncs); connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); @@ -275,6 +281,10 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) char *pcFirmware; nfc_device *pnd = nfc_device_new(context, fullconnstring); pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data)); + if (!pnd->driver_data) { + perror("malloc"); + goto error; + } // Alloc and init chip's data pn53x_data_new(pnd, &acr122_pcsc_io); diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 5f35234..6240fd3 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -351,8 +351,23 @@ acr122_usb_connstring_decode(const nfc_connstring connstring, struct acr122_usb_ { int n = strlen(connstring) + 1; char *driver_name = malloc(n); + if (!driver_name) { + perror("malloc"); + return 0; + } char *dirname = malloc(n); + if (!dirname) { + perror("malloc"); + free(driver_name); + return 0; + } char *filename = malloc(n); + if (!filename) { + perror("malloc"); + free(driver_name); + free(dirname); + return 0; + } driver_name[0] = '\0'; @@ -462,6 +477,10 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) acr122_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct acr122_usb_data)); + if (!pnd->driver_data) { + perror("malloc"); + goto error; + } *DRIVER_DATA(pnd) = data; // Alloc and init chip's data diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 485c1dc..3043268 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -473,6 +473,10 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz pnd->driver = &acr122s_driver; pnd->driver_data = malloc(sizeof(struct acr122s_data)); + if (!pnd->driver_data) { + perror("malloc"); + return -1; + } DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->seq = 0; @@ -574,6 +578,12 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) strcpy(pnd->name, ACR122S_DRIVER_NAME); pnd->driver_data = malloc(sizeof(struct acr122s_data)); + if (!pnd->driver_data) { + perror("malloc"); + acr122s_close(pnd); + return NULL; + } + DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->seq = 0; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index c7784d1..ee0f149 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -117,6 +117,10 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); + if (!pnd->driver_data) { + perror("malloc"); + return 0; + } DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data @@ -262,6 +266,10 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct arygon_data)); + if (!pnd->driver_data) { + perror("malloc"); + return NULL; + } DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 0409ca5..48ffc4b 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -90,6 +90,10 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const nfc_device *pnd = nfc_device_new(context, connstring); pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); + if (!pnd->driver_data) { + perror("malloc"); + return 0; + } DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data @@ -239,6 +243,10 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); + if (!pnd->driver_data) { + perror("malloc"); + return NULL; + } DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 905a951..859c93e 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -234,8 +234,23 @@ pn53x_usb_connstring_decode(const nfc_connstring connstring, struct pn53x_usb_de { int n = strlen(connstring) + 1; char *driver_name = malloc(n); + if (!driver_name) { + perror("malloc"); + return 0; + } char *dirname = malloc(n); + if (!dirname) { + perror("malloc"); + free(driver_name); + return 0; + } char *filename = malloc(n); + if (!filename) { + perror("malloc"); + free(driver_name); + free(dirname); + return 0; + } driver_name[0] = '\0'; @@ -345,6 +360,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); + if (!pnd->driver_data) { + perror("malloc"); + goto error; + } *DRIVER_DATA(pnd) = data; // Alloc and init chip's data From a6c405a5d57c3f729e0bf8dbbb317aff217b9693 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 02:50:33 +0100 Subject: [PATCH 11/28] malloc/free: some more cleaning & checking malloc errors --- libnfc/buses/uart_posix.c | 4 ++++ libnfc/buses/uart_win32.c | 7 +++++++ libnfc/chips/pn53x.c | 33 +++++++++++++++++++++++++++++++++ libnfc/nfc.c | 2 ++ 4 files changed, 46 insertions(+) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 7b687c1..84bf5b4 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -79,16 +79,19 @@ uart_open(const char *pcPortName) sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NONBLOCK); if (sp->fd == -1) { uart_close_ext(sp, false); + free(sp); return INVALID_SERIAL_PORT; } if (tcgetattr(sp->fd, &sp->termios_backup) == -1) { uart_close_ext(sp, false); + free(sp); return INVALID_SERIAL_PORT; } // Make sure the port is not claimed already if (sp->termios_backup.c_iflag & CCLAIMED) { uart_close_ext(sp, false); + free(sp); return CLAIMED_SERIAL_PORT; } // Copy the old terminal info struct @@ -104,6 +107,7 @@ uart_open(const char *pcPortName) if (tcsetattr(sp->fd, TCSANOW, &sp->termios_new) == -1) { uart_close_ext(sp, true); + free(sp); return INVALID_SERIAL_PORT; } return sp; diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 0a47342..82c92b1 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -45,6 +45,9 @@ uart_open(const char *pcPortName) char acPortName[255]; struct serial_port_windows *sp = malloc(sizeof(struct serial_port_windows)); + if (sp == 0) + return INVALID_SERIAL_PORT; + // Copy the input "com?" to "\\.\COM?" format sprintf(acPortName, "\\\\.\\%s", pcPortName); _strupr(acPortName); @@ -53,6 +56,7 @@ uart_open(const char *pcPortName) sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (sp->hPort == INVALID_HANDLE_VALUE) { uart_close(sp); + free(sp); return INVALID_SERIAL_PORT; } // Prepare the device control @@ -60,11 +64,13 @@ uart_open(const char *pcPortName) sp->dcb.DCBlength = sizeof(DCB); if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1", &sp->dcb)) { uart_close(sp); + free(sp); return INVALID_SERIAL_PORT; } // Update the active serial port if (!SetCommState(sp->hPort, &sp->dcb)) { uart_close(sp); + free(sp); return INVALID_SERIAL_PORT; } @@ -76,6 +82,7 @@ uart_open(const char *pcPortName) if (!SetCommTimeouts(sp->hPort, &sp->ct)) { uart_close(sp); + free(sp); return INVALID_SERIAL_PORT; } diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 0d61fdd..8ccccca 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -78,6 +78,8 @@ pn53x_init(struct nfc_device *pnd) if (!CHIP_DATA(pnd)->supported_modulation_as_initiator) { CHIP_DATA(pnd)->supported_modulation_as_initiator = malloc(sizeof(nfc_modulation) * 9); + if (! CHIP_DATA(pnd)->supported_modulation_as_initiator) + return NFC_ESOFT; int nbSupportedModulation = 0; if ((pnd->btSupportByte & SUPPORT_ISO14443A)) { CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_ISO14443A; @@ -1649,6 +1651,8 @@ pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pb // We've to compute CRC ourselves to know last byte actually sent uint8_t *pbtTxRaw; pbtTxRaw = (uint8_t *) malloc(szTx + 2); + if (!pbtTxRaw) + return NFC_ESOFT; memcpy(pbtTxRaw, pbtTx, szTx); iso14443a_crc_append(pbtTxRaw, szTx); *cycles = __pn53x_get_timer(pnd, pbtTxRaw[szTx + 1]); @@ -2929,124 +2933,153 @@ pn53x_get_information_about(nfc_device *pnd, char **pbuf) { size_t buflen = 2048; *pbuf = malloc(buflen); + if (! *pbuf) { + return NFC_ESOFT; + } char *buf = *pbuf; int res; if ((res = snprintf(buf, buflen, "chip: %s\n", CHIP_DATA(pnd)->firmware_text)) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; if ((res = snprintf(buf, buflen, "initator mode modulations: ")) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; const nfc_modulation_type *nmt; if ((res = nfc_device_get_supported_modulation(pnd, N_INITIATOR, &nmt)) < 0) { + free(*pbuf); return res; } for (int i = 0; nmt[i]; i++) { if ((res = snprintf(buf, buflen, "%s%s (", (i == 0) ? "" : ", ", str_nfc_modulation_type(nmt[i]))) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; const nfc_baud_rate *nbr; if ((res = nfc_device_get_supported_baud_rate(pnd, nmt[i], &nbr)) < 0) { + free(*pbuf); return res; } for (int j = 0; nbr[j]; j++) { if ((res = snprintf(buf, buflen, "%s%s", (j == 0) ? "" : ", ", str_nfc_baud_rate(nbr[j]))) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; } if ((res = snprintf(buf, buflen, ")")) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; } if ((res = snprintf(buf, buflen, "\n")) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; if ((res = snprintf(buf, buflen, "target mode modulations: ")) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; if ((res = nfc_device_get_supported_modulation(pnd, N_TARGET, &nmt)) < 0) { + free(*pbuf); return res; } for (int i = 0; nmt[i]; i++) { if ((res = snprintf(buf, buflen, "%s%s (", (i == 0) ? "" : ", ", str_nfc_modulation_type(nmt[i]))) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; const nfc_baud_rate *nbr; if ((res = nfc_device_get_supported_baud_rate(pnd, nmt[i], &nbr)) < 0) { + free(*pbuf); return res; } for (int j = 0; nbr[j]; j++) { if ((res = snprintf(buf, buflen, "%s%s", (j == 0) ? "" : ", ", str_nfc_baud_rate(nbr[j]))) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; } if ((res = snprintf(buf, buflen, ")")) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; } if ((res = snprintf(buf, buflen, "\n")) < 0) { + free(*pbuf); return NFC_ESOFT; } buf += res; if (buflen <= (size_t)res) { + free(*pbuf); return NFC_EOVFLOW; } buflen -= res; diff --git a/libnfc/nfc.c b/libnfc/nfc.c index b3ce252..4c0368d 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -1287,6 +1287,8 @@ int str_nfc_target(char **buf, const nfc_target nt, bool verbose) { *buf = malloc(4096); + if (! *buf) + return NFC_ESOFT; (*buf)[0] = '\0'; sprint_nfc_target(*buf, nt, verbose); return strlen(*buf); From b2b1d2a2a27ce52092672e9a0eb9fb035ae5cb93 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 02:52:07 +0100 Subject: [PATCH 12/28] make style --- libnfc/buses/usbbus.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c index fcf4233..a2294da 100644 --- a/libnfc/buses/usbbus.c +++ b/libnfc/buses/usbbus.c @@ -33,9 +33,10 @@ #define LOG_GROUP NFC_LOG_GROUP_DRIVER // Global flag to know if usb_init() has already been called or not -bool usb_initialized=false; +bool usb_initialized = false; -int usb_prepare(void) { +int usb_prepare(void) +{ if (usb_initialized) return 0; usb_init(); From 3aadb459887b8337dc22fc3e8fea92a3b6473b52 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Sat, 2 Mar 2013 10:59:37 +0100 Subject: [PATCH 13/28] Ease diff between both libusb based drivers: acr122_usb and pn53x_usb --- libnfc/drivers/acr122_usb.c | 3 ++- libnfc/drivers/pn53x_usb.c | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 6240fd3..976f6e0 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -160,6 +160,7 @@ struct acr122_usb_apdu_frame { }; #pragma pack() +// Internal data struct struct acr122_usb_data { usb_dev_handle *pudh; acr122_usb_model model; @@ -191,7 +192,7 @@ const uint8_t acr122_usb_frame_template[] = { // APDUs instructions #define APDU_GetAdditionnalData 0xc0 -// Internal data struct +// Internal io struct const struct pn53x_io acr122_usb_io; // Prototypes diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 859c93e..747c6ec 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -65,7 +65,7 @@ typedef enum { SONY_RCS360 } pn53x_usb_model; -// Internal data structs +// Internal data struct struct pn53x_usb_data { usb_dev_handle *pudh; pn53x_usb_model model; @@ -75,6 +75,7 @@ struct pn53x_usb_data { volatile bool abort_flag; }; +// Internal io struct const struct pn53x_io pn53x_usb_io; // Prototypes @@ -174,7 +175,9 @@ static size_t pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { (void)context; + usb_prepare(); + size_t device_found = 0; uint32_t uiBusIndex = 0; struct usb_bus *bus; @@ -736,6 +739,7 @@ const struct pn53x_io pn53x_usb_io = { const struct nfc_driver pn53x_usb_driver = { .name = PN53X_USB_DRIVER_NAME, + .scan_type = NOT_INTRUSIVE, .scan = pn53x_usb_scan, .open = pn53x_usb_open, .close = pn53x_usb_close, From e7290de83beb3f32557d55132695f021d2526eb4 Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Sat, 2 Mar 2013 11:30:24 +0100 Subject: [PATCH 14/28] Fix compiler warning nfc.c:121: warning: function declaration isn't a prototype nfc.c: In function 'nfc_drivers_init': nfc.c:121: warning: old-style function definition --- libnfc/nfc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 4c0368d..20ed75d 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -117,7 +117,7 @@ struct nfc_driver_list { const struct nfc_driver_list *nfc_drivers = NULL; static void -nfc_drivers_init() +nfc_drivers_init(void) { #if defined (DRIVER_PN53X_USB_ENABLED) nfc_register_driver(&pn53x_usb_driver); From 1f0b0e5b81e5895c6dcfda8a685293511b895a90 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 12:26:24 +0100 Subject: [PATCH 15/28] Fix zealous double free --- libnfc/buses/uart_posix.c | 4 ---- libnfc/buses/uart_win32.c | 4 ---- 2 files changed, 8 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 84bf5b4..7b687c1 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -79,19 +79,16 @@ uart_open(const char *pcPortName) sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NONBLOCK); if (sp->fd == -1) { uart_close_ext(sp, false); - free(sp); return INVALID_SERIAL_PORT; } if (tcgetattr(sp->fd, &sp->termios_backup) == -1) { uart_close_ext(sp, false); - free(sp); return INVALID_SERIAL_PORT; } // Make sure the port is not claimed already if (sp->termios_backup.c_iflag & CCLAIMED) { uart_close_ext(sp, false); - free(sp); return CLAIMED_SERIAL_PORT; } // Copy the old terminal info struct @@ -107,7 +104,6 @@ uart_open(const char *pcPortName) if (tcsetattr(sp->fd, TCSANOW, &sp->termios_new) == -1) { uart_close_ext(sp, true); - free(sp); return INVALID_SERIAL_PORT; } return sp; diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index 82c92b1..f14e28d 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -56,7 +56,6 @@ uart_open(const char *pcPortName) sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (sp->hPort == INVALID_HANDLE_VALUE) { uart_close(sp); - free(sp); return INVALID_SERIAL_PORT; } // Prepare the device control @@ -64,13 +63,11 @@ uart_open(const char *pcPortName) sp->dcb.DCBlength = sizeof(DCB); if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1", &sp->dcb)) { uart_close(sp); - free(sp); return INVALID_SERIAL_PORT; } // Update the active serial port if (!SetCommState(sp->hPort, &sp->dcb)) { uart_close(sp); - free(sp); return INVALID_SERIAL_PORT; } @@ -82,7 +79,6 @@ uart_open(const char *pcPortName) if (!SetCommTimeouts(sp->hPort, &sp->ct)) { uart_close(sp); - free(sp); return INVALID_SERIAL_PORT; } From ae571941c5396c01acc0e0c97fe8fddc17ab8bb4 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 12:43:15 +0100 Subject: [PATCH 16/28] usbbus: make usb_initialized static --- debian/libnfc4.symbols | 1 - libnfc/buses/usbbus.c | 7 +++---- libnfc/buses/usbbus.h | 3 --- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/debian/libnfc4.symbols b/debian/libnfc4.symbols index 48a856d..2437b36 100644 --- a/debian/libnfc4.symbols +++ b/debian/libnfc4.symbols @@ -54,5 +54,4 @@ libnfc.so.4 libnfc4 #MINVER# str_nfc_baud_rate@Base 1.7.0~rc2 str_nfc_modulation_type@Base 1.7.0~rc2 str_nfc_target@Base 1.7.0~rc2 - usb_initialized@Base 1.7.0~rc6-0 usb_prepare@Base 1.7.0~rc6-0 diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c index a2294da..6c0abdd 100644 --- a/libnfc/buses/usbbus.c +++ b/libnfc/buses/usbbus.c @@ -32,13 +32,12 @@ #define LOG_CATEGORY "libnfc.buses.usbbus" #define LOG_GROUP NFC_LOG_GROUP_DRIVER -// Global flag to know if usb_init() has already been called or not -bool usb_initialized = false; - int usb_prepare(void) { - if (usb_initialized) + static bool usb_initialized = false; + if (usb_initialized) { return 0; + } usb_init(); usb_initialized = true; diff --git a/libnfc/buses/usbbus.h b/libnfc/buses/usbbus.h index 5c08137..329160a 100644 --- a/libnfc/buses/usbbus.h +++ b/libnfc/buses/usbbus.h @@ -41,9 +41,6 @@ #include #include -// Global flag to know if usb_init() has already been called or not -extern bool usb_initialized; - int usb_prepare(void); #endif // __NFC_BUS_USB_H__ From abf54c759803151523a2f42cdb5783d53c6f5fc3 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 12:55:43 +0100 Subject: [PATCH 17/28] Debian: remove non-exported symbol --- debian/libnfc4.symbols | 1 - 1 file changed, 1 deletion(-) diff --git a/debian/libnfc4.symbols b/debian/libnfc4.symbols index 2437b36..df9dc8e 100644 --- a/debian/libnfc4.symbols +++ b/debian/libnfc4.symbols @@ -54,4 +54,3 @@ libnfc.so.4 libnfc4 #MINVER# str_nfc_baud_rate@Base 1.7.0~rc2 str_nfc_modulation_type@Base 1.7.0~rc2 str_nfc_target@Base 1.7.0~rc2 - usb_prepare@Base 1.7.0~rc6-0 From aee56b92556f3ac82c0033e631ee2cdc77c87a2f Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Sat, 2 Mar 2013 13:08:03 +0100 Subject: [PATCH 18/28] Initialize variables Fix compiler warnings and potential bug pn53x.c:1526:19: warning: The right operand of '<' is a garbage value for (i = 0; i < sz; i++) { ^ ~~ pn53x.c:1621:19: warning: The right operand of '<' is a garbage value for (i = 0; i < sz; i++) { ^ ~~ --- libnfc/chips/pn53x.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 8ccccca..5e18e7a 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1456,7 +1456,7 @@ pn53x_initiator_transceive_bits_timed(struct nfc_device *pnd, const uint8_t *pbt (void) pbtTxPar; (void) pbtRxPar; uint16_t i; - uint8_t sz; + uint8_t sz = 0; int res = 0; size_t szRxBits = 0; @@ -1555,7 +1555,7 @@ int pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles) { uint16_t i; - uint8_t sz; + uint8_t sz = 0; int res = 0; // We can not just send bytes without parity while the PN53X expects we handled them From 999631588da61b95f98e3355a733e1177078a3c1 Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Sat, 2 Mar 2013 13:11:30 +0100 Subject: [PATCH 19/28] closedir() only if opendir() succeeds Fix compiler warning: conf.c:186:3: warning: Null pointer passed as an argument to a 'nonnull' parameter closedir(d); ^ ~ --- libnfc/conf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/conf.c b/libnfc/conf.c index 4e19911..f28dd72 100644 --- a/libnfc/conf.c +++ b/libnfc/conf.c @@ -182,8 +182,8 @@ conf_devices_load(const char *dirname, nfc_context *context) } } } + closedir(d); } - closedir(d); } void From f07d784b8b4ce460ee827fcf2aed46a23f3c106c Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 13:17:57 +0100 Subject: [PATCH 20/28] make style --- libnfc/conf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/conf.c b/libnfc/conf.c index f28dd72..7647a52 100644 --- a/libnfc/conf.c +++ b/libnfc/conf.c @@ -182,7 +182,7 @@ conf_devices_load(const char *dirname, nfc_context *context) } } } - closedir(d); + closedir(d); } } From bd8a9fe96eee54a42f0df682520379e1ae755d9c Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Sat, 2 Mar 2013 13:22:32 +0100 Subject: [PATCH 21/28] Remove dead code The local variable bFailure is set before a return. nfc-mfclassic.c:319:11: warning: Value stored to 'bFailure' is never read bFailure = true; ^ ~~~~ --- utils/nfc-mfclassic.c | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 4547df9..91b9a9e 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -316,7 +316,6 @@ read_card(int read_unlocked) if (nfc_initiator_mifare_cmd(pnd, MC_READ, iBlock, &mp)) { memcpy(mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16); } else { - bFailure = true; printf("!\nError: unable to read block 0x%02x\n", iBlock); return false; } From 70b0dffca750129c67d162231668d668dc0afb49 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 19:20:04 +0100 Subject: [PATCH 22/28] acr122_usb: fix compiler warnings & check return value acr122_usb.c:570:3: warning: Null pointer passed as an argument to a 'nonnull' parameter memcpy(DRIVER_DATA(pnd)->apdu_frame.apdu_payload, data, data_len); ^ ~~~~ acr122_usb.c:738:3: warning: Value stored to 'offset' is never read offset += len; ^ ~~~ acr122_usb.c:753:3: warning: Value stored to 'res' is never read res = acr122_usb_bulk_write(DRIVER_DATA(pnd), (unsigned char *) & (DRIVER_DATA(pnd)->tama_frame), res, 1000); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- libnfc/drivers/acr122_usb.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 976f6e0..91b47c0 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -561,13 +561,21 @@ acr122_build_frame_from_apdu(nfc_device *pnd, const uint8_t ins, const uint8_t p { if (data_len > sizeof(DRIVER_DATA(pnd)->apdu_frame.apdu_payload)) return NFC_EINVARG; + if ((data == NULL) && (data_len != 0)) + return NFC_EINVARG; DRIVER_DATA(pnd)->apdu_frame.ccid_header.dwLength = htole32(data_len + sizeof(struct apdu_header)); DRIVER_DATA(pnd)->apdu_frame.apdu_header.bIns = ins; DRIVER_DATA(pnd)->apdu_frame.apdu_header.bP1 = p1; DRIVER_DATA(pnd)->apdu_frame.apdu_header.bP2 = p2; - DRIVER_DATA(pnd)->apdu_frame.apdu_header.bLen = (data ? data_len : le); // XXX This line is a bit tricky ^^: bLen is Lc when data != NULL... otherwise its Le. - memcpy(DRIVER_DATA(pnd)->apdu_frame.apdu_payload, data, data_len); + if (data) { + // bLen is Lc when data != NULL + DRIVER_DATA(pnd)->apdu_frame.apdu_header.bLen = data_len; + memcpy(DRIVER_DATA(pnd)->apdu_frame.apdu_payload, data, data_len); + } else { + // bLen is Le when no data. + DRIVER_DATA(pnd)->apdu_frame.apdu_header.bLen = le; + } return (sizeof(struct ccid_header) + sizeof(struct apdu_header) + data_len); } @@ -735,7 +743,6 @@ read: offset += 1; memcpy(pbtData, abtRxBuf + offset, len); - offset += len; return len; } @@ -749,8 +756,8 @@ acr122_usb_ack(nfc_device *pnd) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "ACR122 Abort"); if ((res = acr122_build_frame_from_tama(pnd, acr122_ack_frame, sizeof(acr122_ack_frame))) < 0) return res; - - res = acr122_usb_bulk_write(DRIVER_DATA(pnd), (unsigned char *) & (DRIVER_DATA(pnd)->tama_frame), res, 1000); + if ((res = acr122_usb_bulk_write(DRIVER_DATA(pnd), (unsigned char *) & (DRIVER_DATA(pnd)->tama_frame), res, 1000)) < 0) + return res; uint8_t abtRxBuf[255 + sizeof(struct ccid_header)]; res = acr122_usb_bulk_read(DRIVER_DATA(pnd), abtRxBuf, sizeof(abtRxBuf), 1000); return res; From 2334adc8bdfecc91365348a7774c9809092f9df0 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 19:39:20 +0100 Subject: [PATCH 23/28] Fix compilation warnings: values never read Fixes: pn53x.c:523:11: warning: Value stored to 'pbtRawData' is never read pbtRawData += szRawData - 8; ^ ~~~~~~~~~~~~~ pn53x.c:544:7: warning: Value stored to 'pbtRawData' is never read pbtRawData += 2; ^ ~ pn53x.c:532:7: warning: Value stored to 'pbtRawData' is never read pbtRawData += 8; ^ ~ pn53x.c:3085:3: warning: Value stored to 'buflen' is never read buflen -= res; ^ ~~~ pn53x.c:3080:3: warning: Value stored to 'buf' is never read buf += res; ^ ~~~ target-subr.c:466:7: warning: Value stored to 'dst' is never read dst += sprintf(dst, "* Unknown card, sorry\n"); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ target-subr.c:480:3: warning: Value stored to 'dst' is never read dst += sprint_hex(dst, nfi.abtSysCode, 2); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ target-subr.c:490:3: warning: Value stored to 'dst' is never read dst += sprint_hex(dst, nji.btId, 4); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ target-subr.c:546:7: warning: Value stored to 'dst' is never read dst += sprintf(dst, "\n"); ^ ~~~~~~~~~~~~~~~~~~ target-subr.c:571:5: warning: Value stored to 'dst' is never read dst += sprint_hex(dst, nii.abtAtr, nii.szAtrLen); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ target-subr.c:580:3: warning: Value stored to 'dst' is never read dst += sprint_hex(dst, nsi.abtUID, 8); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ target-subr.c:593:3: warning: Value stored to 'dst' is never read dst += sprintf(dst, " Fab Code: %02X\n", nci.btFabCode); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ target-subr.c:608:5: warning: Value stored to 'dst' is never read dst += sprint_hex(dst, ndi.abtGB, ndi.szGB); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- libnfc/chips/pn53x.c | 7 ++----- libnfc/target-subr.c | 16 ++++++++-------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 5e18e7a..362cb4a 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -520,7 +520,6 @@ pn53x_decode_target_data(const uint8_t *pbtRawData, size_t szRawData, pn53x_type pnti->nii.btConfig = *(pbtRawData++); if (pnti->nii.btConfig & 0x40) { memcpy(pnti->nii.abtAtr, pbtRawData, szRawData - 8); - pbtRawData += szRawData - 8; pnti->nii.szAtrLen = szRawData - 8; } } @@ -529,7 +528,6 @@ pn53x_decode_target_data(const uint8_t *pbtRawData, size_t szRawData, pn53x_type case NMT_ISO14443B2SR: // Store the UID memcpy(pnti->nsi.abtUID, pbtRawData, 8); - pbtRawData += 8; break; case NMT_ISO14443B2CT: @@ -541,7 +539,6 @@ pn53x_decode_target_data(const uint8_t *pbtRawData, size_t szRawData, pn53x_type pnti->nci.btFabCode = *(pbtRawData++); // Store UID MSB memcpy(pnti->nci.abtUID + 2, pbtRawData, 2); - pbtRawData += 2; break; case NMT_FELICA: @@ -3077,12 +3074,12 @@ pn53x_get_information_about(nfc_device *pnd, char **pbuf) free(*pbuf); return NFC_ESOFT; } - buf += res; + //buf += res; if (buflen <= (size_t)res) { free(*pbuf); return NFC_EOVFLOW; } - buflen -= res; + //buflen -= res; return NFC_SUCCESS; } diff --git a/libnfc/target-subr.c b/libnfc/target-subr.c index 0c9dd63..9b3640f 100644 --- a/libnfc/target-subr.c +++ b/libnfc/target-subr.c @@ -463,7 +463,7 @@ sprint_nfc_iso14443a_info(char *dst, const nfc_iso14443a_info nai, bool verbose) break; } if (! found_possible_match) { - dst += sprintf(dst, "* Unknown card, sorry\n"); + sprintf(dst, "* Unknown card, sorry\n"); } } } @@ -477,7 +477,7 @@ sprint_nfc_felica_info(char *dst, const nfc_felica_info nfi, bool verbose) dst += sprintf(dst, " Parameter (PAD): "); dst += sprint_hex(dst, nfi.abtPad, 8); dst += sprintf(dst, " System Code (SC): "); - dst += sprint_hex(dst, nfi.abtSysCode, 2); + sprint_hex(dst, nfi.abtSysCode, 2); } void @@ -487,7 +487,7 @@ sprint_nfc_jewel_info(char *dst, const nfc_jewel_info nji, bool verbose) dst += sprintf(dst, " ATQA (SENS_RES): "); dst += sprint_hex(dst, nji.btSensRes, 2); dst += sprintf(dst, " 4-LSB JEWELID: "); - dst += sprint_hex(dst, nji.btId, 4); + sprint_hex(dst, nji.btId, 4); } #define PI_ISO14443_4_SUPPORTED 0x01 @@ -543,7 +543,7 @@ sprint_nfc_iso14443b_info(char *dst, const nfc_iso14443b_info nbi, bool verbose) dst += sprintf(dst, "* Frame options supported: "); if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) dst += sprintf(dst, "NAD "); if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) dst += sprintf(dst, "CID "); - dst += sprintf(dst, "\n"); + sprintf(dst, "\n"); } } } @@ -568,7 +568,7 @@ sprint_nfc_iso14443bi_info(char *dst, const nfc_iso14443bi_info nii, bool verbos } if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x40)) { dst += sprintf(dst, " ATS: "); - dst += sprint_hex(dst, nii.abtAtr, nii.szAtrLen); + sprint_hex(dst, nii.abtAtr, nii.szAtrLen); } } @@ -577,7 +577,7 @@ sprint_nfc_iso14443b2sr_info(char *dst, const nfc_iso14443b2sr_info nsi, bool ve { (void) verbose; dst += sprintf(dst, " UID: "); - dst += sprint_hex(dst, nsi.abtUID, 8); + sprint_hex(dst, nsi.abtUID, 8); } void @@ -590,7 +590,7 @@ sprint_nfc_iso14443b2ct_info(char *dst, const nfc_iso14443b2ct_info nci, bool ve dst += sprint_hex(dst, nci.abtUID, sizeof(nci.abtUID)); dst += sprintf(dst, " UID (decimal): %010u\n", uid); dst += sprintf(dst, " Product Code: %02X\n", nci.btProdCode); - dst += sprintf(dst, " Fab Code: %02X\n", nci.btFabCode); + sprintf(dst, " Fab Code: %02X\n", nci.btFabCode); } void @@ -605,7 +605,7 @@ sprint_nfc_dep_info(char *dst, const nfc_dep_info ndi, bool verbose) dst += sprintf(dst, " PP: %02x\n", ndi.btPP); if (ndi.szGB) { dst += sprintf(dst, "General Bytes: "); - dst += sprint_hex(dst, ndi.abtGB, ndi.szGB); + sprint_hex(dst, ndi.abtGB, ndi.szGB); } } From ddd7cabda82ccc83f3201839ca779377d954164f Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 19:46:17 +0100 Subject: [PATCH 24/28] Edit HACKING & introduce clang (thanks Ludovic!) --- HACKING | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/HACKING b/HACKING index f7e65ff..4a02615 100644 --- a/HACKING +++ b/HACKING @@ -30,7 +30,12 @@ Here are some directions to get you started: -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition \ -Wbad-function-cast -Wnested-externs -Wmissing-declarations" $ ./configure + $ make clean $ make + You can chase even more issues by using clang: + $ scan-build ./configure + $ make clean + $ scan-build make 2.3 When Debianizing $ lintian *deb From b1448f678584f46add265cbf54631098e623a033 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 22:29:10 +0100 Subject: [PATCH 25/28] nfc-mfclassic: add option to tolerate RW errors & other enhancements Reconciliate read & write operations. Add option to tolerate or not RW failures. Print success/failure for each block as ACL is per block. --- utils/nfc-mfclassic.1 | 40 +++++++++++++++++++++++++++++++++++----- utils/nfc-mfclassic.c | 33 +++++++++++++++------------------ 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/utils/nfc-mfclassic.1 b/utils/nfc-mfclassic.1 index a36ddaa..d1f9117 100644 --- a/utils/nfc-mfclassic.1 +++ b/utils/nfc-mfclassic.1 @@ -4,7 +4,7 @@ nfc-mfclassic \- MIFARE Classic command line tool .SH SYNOPSIS .B nfc-mfclassic .RI \fR\fBr\fR|\fR\fBR\fR|\fBw\fR\fR|\fBW\fR -.RI \fR\fBa\fR|\fBb\fR +.RI \fR\fBa\fR|\fR\fBA\fR|\fBb\fR\fR|\fBB\fR .IR DUMP .IR [KEYS] @@ -27,16 +27,36 @@ to store the keys and data for all sectors. Be cautious that some parts of a MIFARE Classic memory are used for r/w access of the rest of the memory, so please read the tag documentation before experimenting too much! -The 'W' option allows writing of special MIFARE cards that can be 'unlocked' to allow block 0 +The +.B W +option allows writing of special MIFARE cards that can be 'unlocked' to allow block 0 to be overwritten. This includes UID and manufacturer data. Take care when amending UIDs to set the correct BCC (UID checksum). Currently only 4 byte UIDs are supported. -Similarly, the 'R' option allows an 'unlocked' read. This bypasses authentication and allows +Similarly, the +.B R +option allows an 'unlocked' read. This bypasses authentication and allows reading of the Key A and Key B data regardless of ACLs. -*** Note that 'W' and 'R' options only work on special versions of MIFARE 1K cards (Chinese clones). +R/W errors on some blocks can be either considered as critical or ignored. +To halt on first error, specify keys with lowercase ( +.B a +or +.B b +). To ignore such errors, use uppercase ( +.B A +or +.B B +). + +*** Note that +.B W +and +.B R +options only work on special versions of MIFARE 1K cards (Chinese clones). .SH OPTIONS +.TP .BR r " | " R " | " w " | " W Perform read from ( .B r @@ -48,8 +68,18 @@ Perform read from ( .B W ) card. .TP -.BR a " | " b +.BR a " | " A " | " b " | " B Use A or B MIFARE keys. +Halt on errors ( +.B a +| +.B b +) or tolerate errors ( +.B A +| +.B +B +). .TP .IR DUMP MiFare Dump (MFD) used to write (card to MFD) or (MFD to card) diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 91b9a9e..764b4ca 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -61,6 +61,7 @@ static mifare_classic_tag mtKeys; static mifare_classic_tag mtDump; static bool bUseKeyA; static bool bUseKeyFile; +static bool bTolerateFailures; static uint8_t uiBlocks; static uint8_t keys[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -268,18 +269,11 @@ read_card(int read_unlocked) if (!unlock_card()) return false; - printf("Reading out %d blocks |", uiBlocks + 1); - // Read the card from end to begin for (iBlock = uiBlocks; iBlock >= 0; iBlock--) { // Authenticate everytime we reach a trailer block if (is_trailer_block(iBlock)) { - // Skip this the first time, bFailure it means nothing (yet) - if (iBlock != uiBlocks) - print_success_or_failure(bFailure, &uiReadBlocks); - - // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { @@ -307,7 +301,8 @@ read_card(int read_unlocked) memcpy(mtDump.amb[iBlock].mbt.abtKeyB, mtKeys.amb[iBlock].mbt.abtKeyB, 6); } } else { - printf("!\nError: unable to read trailer block 0x%02x\n", iBlock); + printf("!\nfailed to read trailer block 0x%02x\n", iBlock); + bFailure = true; } } else { // Make sure a earlier readout did not fail @@ -317,12 +312,15 @@ read_card(int read_unlocked) memcpy(mtDump.amb[iBlock].mbd.abtData, mp.mpd.abtData, 16); } else { printf("!\nError: unable to read block 0x%02x\n", iBlock); - return false; + bFailure = true; } } } + // Show if the readout went well for each block + print_success_or_failure(bFailure, &uiReadBlocks); + if ((! bTolerateFailures) && bFailure) + return false; } - print_success_or_failure(bFailure, &uiReadBlocks); printf("|\n"); printf("Done, %d of %d blocks read.\n", uiReadBlocks, uiBlocks + 1); fflush(stdout); @@ -337,7 +335,6 @@ write_card(int write_block_zero) bool bFailure = false; uint32_t uiWriteBlocks = 0; - if (write_block_zero) if (!unlock_card()) return false; @@ -347,11 +344,6 @@ write_card(int write_block_zero) for (uiBlock = 0; uiBlock <= uiBlocks; uiBlock++) { // Authenticate everytime we reach the first sector of a new block if (is_first_block(uiBlock)) { - // Skip this the first time, bFailure it means nothing (yet) - if (uiBlock != 0) - print_success_or_failure(bFailure, &uiWriteBlocks); - - // Show if the readout went well if (bFailure) { // When a failure occured we need to redo the anti-collision if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { @@ -402,8 +394,11 @@ write_card(int write_block_zero) bFailure = true; } } + // Show if the write went well for each block + print_success_or_failure(bFailure, &uiWriteBlocks); + if ((! bTolerateFailures) && bFailure) + return false; } - print_success_or_failure(bFailure, &uiWriteBlocks); printf("|\n"); printf("Done, %d of %d blocks written.\n", uiWriteBlocks, uiBlocks + 1); fflush(stdout); @@ -426,7 +421,7 @@ print_usage(const char *pcProgramName) printf(" *** note that unlocked write will attempt to overwrite block 0 including UID\n"); printf(" *** unlocked read does not require authentication and will reveal A and B keys\n"); printf(" *** unlocking only works with special Mifare 1K cards (Chinese clones)\n"); - printf(" a|b - Use A or B keys for action\n"); + printf(" a|A|b|B - Use A or B keys for action; Halt on errors (a|b) or tolerate errors (A|B)\n"); printf(" - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n"); printf(" - MiFare Dump (MFD) that contain the keys (optional)\n"); } @@ -455,6 +450,7 @@ main(int argc, const char *argv[]) if (strcmp(command, "R") == 0) unlock = 1; bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a'; + bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2])); bUseKeyFile = (argc > 4); } else if (strcmp(command, "w") == 0 || strcmp(command, "W") == 0) { if (argc < 4) { @@ -465,6 +461,7 @@ main(int argc, const char *argv[]) if (strcmp(command, "W") == 0) unlock = 1; bUseKeyA = tolower((int)((unsigned char) * (argv[2]))) == 'a'; + bTolerateFailures = tolower((int)((unsigned char) * (argv[2]))) != (int)((unsigned char) * (argv[2])); bUseKeyFile = (argc > 4); } From b68a37b8356d003efd045d4ca44428ab66dd8f73 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 2 Mar 2013 23:41:43 +0100 Subject: [PATCH 26/28] Replace usb_set_debug() in utils by new group in LIBNFC_LOG_LEVEL To use it: (NFC_LOG_PRIORITY_DEBUG * 2 ^ NFC_LOG_GROUP_LIBUSB) LIBUSB_LOG_LEVEL=12288 --- libnfc/buses/usbbus.c | 10 ++++++++++ libnfc/log.h | 1 + utils/nfc-list.c | 12 ------------ utils/nfc-scan-device.c | 12 ------------ 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/libnfc/buses/usbbus.c b/libnfc/buses/usbbus.c index 6c0abdd..a3e2f99 100644 --- a/libnfc/buses/usbbus.c +++ b/libnfc/buses/usbbus.c @@ -38,6 +38,16 @@ int usb_prepare(void) if (usb_initialized) { return 0; } + +#ifdef ENVVARS + char *env_log_level = getenv("LIBNFC_LOG_LEVEL"); + // Set libusb debug only if asked explicitely: + // LIBUSB_LOG_LEVEL=12288 (= NFC_LOG_PRIORITY_DEBUG * 2 ^ NFC_LOG_GROUP_LIBUSB) + if (env_log_level && (((atoi(env_log_level) >> (NFC_LOG_GROUP_LIBUSB * 2)) & 0x00000003) >= NFC_LOG_PRIORITY_DEBUG)) { + setenv("USB_DEBUG", "255", 1); + } +#endif + usb_init(); usb_initialized = true; diff --git a/libnfc/log.h b/libnfc/log.h index e5ab1ca..d3604fa 100644 --- a/libnfc/log.h +++ b/libnfc/log.h @@ -35,6 +35,7 @@ #define NFC_LOG_GROUP_CHIP 3 #define NFC_LOG_GROUP_DRIVER 4 #define NFC_LOG_GROUP_COM 5 +#define NFC_LOG_GROUP_LIBUSB 6 /* To enable log only for one (or more) group, you can use this formula: diff --git a/utils/nfc-list.c b/utils/nfc-list.c index ca49be9..380120d 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -38,12 +38,6 @@ # include "config.h" #endif // HAVE_CONFIG_H -#ifdef HAVE_LIBUSB -# ifdef DEBUG -# include -# endif -#endif - #include #include #include @@ -90,12 +84,6 @@ main(int argc, const char *argv[]) } } -#ifdef HAVE_LIBUSB -# ifdef DEBUG - usb_set_debug(4); -# endif -#endif - /* Lazy way to open an NFC device */ #if 0 pnd = nfc_open(context, NULL); diff --git a/utils/nfc-scan-device.c b/utils/nfc-scan-device.c index 12693b1..04eae0d 100644 --- a/utils/nfc-scan-device.c +++ b/utils/nfc-scan-device.c @@ -38,12 +38,6 @@ # include "config.h" #endif // HAVE_CONFIG_H -#ifdef HAVE_LIBUSB -# ifdef DEBUG -# include -# endif -#endif - #include #include #include @@ -101,12 +95,6 @@ main(int argc, const char *argv[]) acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); -#ifdef HAVE_LIBUSB -# ifdef DEBUG - usb_set_debug(4); -# endif -#endif - nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); From d143eba7ad0873088d72725312e285dbb8ae4d67 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 3 Mar 2013 01:04:01 +0100 Subject: [PATCH 27/28] Edit ChangeLog --- ChangeLog | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index e4b8536..bf736a1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,10 +2,21 @@ TBD dd, 2013 - 1.7.0-rc6 (release candidate) -------------------------------------------- Fixes: - - Fix minor memory leak with nfc_drivers + - Fix several memory leaks (nfc_drivers, libusb, config parser) - Fix stack smash while displaying long data transmission (LOG_HEX) - pn53x-tamashell: allow larger commands up to full extended frame - Add missing windows file in archive (version.rc.in) + - Fix compilation warnings & potential bugs + - Fix documentation + - Fix missing malloc() calls checks + - Fix missing free() calls in some error handlers + +Improvements: + - Allow to disable conffils & envvar supports on embedded platforms + - Add option to nfc-mfclassic to tolerate RW errors + +Changes: + - Replace usb_set_debug() in applications by LIBNFC_LOG_LEVEL libusb group Feb 16, 2013 - 1.7.0-rc5 (release candidate) -------------------------------------------- From 9a20430bfc3b87e05d7271d7c72b696a742f10e1 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 3 Mar 2013 15:34:35 +0100 Subject: [PATCH 28/28] Prepare release 1.7.0 RC6 --- ChangeLog | 4 ++-- configure.ac | 2 +- debian/changelog | 48 +++++++++++++++++++++++++++--------------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index bf736a1..ea24f75 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -TBD dd, 2013 - 1.7.0-rc6 (release candidate) +Mar 03, 2013 - 1.7.0-rc6 (release candidate) -------------------------------------------- Fixes: @@ -16,7 +16,7 @@ Improvements: - Add option to nfc-mfclassic to tolerate RW errors Changes: - - Replace usb_set_debug() in applications by LIBNFC_LOG_LEVEL libusb group + - Replace usb_set_debug() in applications by LIBNFC_LOG_LEVEL libusb group Feb 16, 2013 - 1.7.0-rc5 (release candidate) -------------------------------------------- diff --git a/configure.ac b/configure.ac index d6c6ec1..eb3bb86 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ # General init # /!\ Don't forget to update 'CMakeLists.txt' too /!\ -AC_INIT([libnfc],[1.7.0-rc5],[nfc-tools@googlegroups.com]) +AC_INIT([libnfc],[1.7.0-rc6],[nfc-tools@googlegroups.com]) AC_CONFIG_MACRO_DIR([m4]) diff --git a/debian/changelog b/debian/changelog index 2b26261..924900e 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +libnfc (1.7.0~rc6-0) unstable; urgency=low + + * New upstream release + + -- yobibe Sun, 3 Mar 2013 12:42:42 +0100 + libnfc (1.7.0~rc5-0) unstable; urgency=low * New upstream release @@ -17,31 +23,31 @@ libnfc (1.7.0~rc3-1) unstable; urgency=low -- Nobuhiro Iwamatsu Fri, 01 Feb 2013 09:01:53 +0900 libnfc (1.7.0~rc2-0) unstable; urgency=low - + * New upstream release -- Romuald Conty Sun, 20 Jan 2013 12:42:42 +0100 libnfc (1.7.0~rc1-0) unstable; urgency=low - + * New upstream release -- Romuald Conty Thu, 9 Dec 2012 12:42:42 +0100 libnfc (1.6.0rc1-0) unstable; urgency=low - + * New upstream release -- Romuald Conty Mon, 27 Feb 2012 12:42:42 +0100 libnfc (1.5.1pre2.1-0) unstable; urgency=low - + * udev rules file renamed accordingly to udev's readme file -- Romuald Conty Fri, 9 Dec 2011 15:42:42 +0100 libnfc (1.5.1-0) unstable; urgency=low - + * Move nfc-emulate-nfcforum-tag2, nfc-emulate-nfcforum-tag4 and nfc-relay-picc from libnfc-examples to libnfc-bin package. * Add nfc-read-forum-tag3 to libnfc-bin package. @@ -49,26 +55,26 @@ libnfc (1.5.1-0) unstable; urgency=low -- Romuald Conty Mon, 10 Oct 2011 02:42:42 +0100 libnfc (1.5.0pre1.4-0) unstable; urgency=low - + * All drivers are now enabled under Debian; * Add nfc-emulate-nfcforum-tag2 and nfc-mfsetuid to libnfc-examples. -- Romuald Conty Tue, 13 May 2011 15:42:42 +0100 libnfc (1.5.0pre1.3-0) unstable; urgency=low - + * Update debian/copyright. -- Romuald Conty Wed, 18 May 2011 12:42:42 +0100 libnfc (1.5.0pre1.2-0) unstable; urgency=low - + * Change name of lib to libnfc2 to match SONAME major version 2. -- Thomas Hood Fri, 13 May 2011 12:00:00 +0100 libnfc (1.5.0pre1.1-0) unstable; urgency=low - + * Change name of lib to libnfc1 to match SONAME major version 1. * Change section and priority. * Don't depend on automake which doesn't get run on build. @@ -83,73 +89,73 @@ libnfc (1.5.0pre1.1-0) unstable; urgency=low -- Thomas Hood Mon, 10 May 2011 12:00:00 +0100 libnfc (1.5.0-0) unstable; urgency=low - + * Experimental released version (r1019). -- Romuald Conty Mon, 29 Apr 2011 11:42:42 +0100 libnfc (1.4.2-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Tue, 21 Feb 2011 11:42:42 +0100 libnfc (1.4.1-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Tue, 1 Feb 2011 10:42:42 +0100 libnfc (1.4.0-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Tue, 26 Oct 2010 16:42:42 +0100 libnfc (1.3.9-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Tue, 31 Aug 2010 14:42:42 +0100 libnfc (1.3.4-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Wed, 31 Mar 2010 12:42:42 +0100 libnfc (1.3.3-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Thu, 17 Feb 2010 15:42:42 +0100 libnfc (1.3.2-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Fri, 29 Jan 2010 15:42:42 +0100 libnfc (1.3.1-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Mon, 25 Jan 2010 11:42:42 +0100 libnfc (1.3.0-0) unstable; urgency=low - + * New upstream release. -- Romuald Conty Thu, 14 Jan 2010 16:42:42 +0100 libnfc (1.2.1-4) unstable; urgency=low - + * Add udev rules for USB PN53x. -- Romuald Conty Fri, 20 Nov 2009 16:42:42 +0100 libnfc (1.2.1-3) unstable; urgency=low - + * Don't use CDBS anymore, should now cross compile. -- Romuald Conty Thu, 29 Sep 2009 9:42:42 +0100