summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcbdev <cb@cbcdn.com>2023-06-24 23:38:03 +0200
committercbdev <cb@cbcdn.com>2023-06-24 23:38:03 +0200
commitd17413ffe8186f82977d0d1e98d5955b4d51b50d (patch)
tree4b723c97fa0e04b6bd193222f0ad4ecc6ec6e92d
parentbf9f1fd058c5ca324b2df5223c582ef1ece7a698 (diff)
downloadnfcommander-d17413ffe8186f82977d0d1e98d5955b4d51b50d.tar.gz
nfcommander-d17413ffe8186f82977d0d1e98d5955b4d51b50d.tar.bz2
nfcommander-d17413ffe8186f82977d0d1e98d5955b4d51b50d.zip
Implement YHY NTAG reading
-rw-r--r--nfcommander.h1
-rw-r--r--reader.c8
-rw-r--r--reader_yhy.c85
3 files changed, 78 insertions, 16 deletions
diff --git a/nfcommander.h b/nfcommander.h
index 00f0fb8..0c24236 100644
--- a/nfcommander.h
+++ b/nfcommander.h
@@ -5,6 +5,7 @@
#ifndef NFCOMMANDER_VERSION
#define NFCOMMANDER_VERSION "v0.1-dev"
#endif
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
typedef enum {
tag_unset, /*unknown tag type*/
diff --git a/reader.c b/reader.c
index 5f1316b..f90ea52 100644
--- a/reader.c
+++ b/reader.c
@@ -97,9 +97,12 @@ static void reader_print_tag(nfc_tag_info_t tag){
case tag_desfire: type = "DESF"; break;
}
- printf("[%s:%lu", type, tag.bytes_available);
+ printf("[%s:%lu ", type, tag.bytes_available);
+ if(tag.static_length){
+ printf("D:%lu:%lu ", tag.static_length, tag.dynamic_length);
+ }
for(n = 0; n < tag.uid_length; n++){
- printf(" %02X", tag.uid[n]);
+ printf("%02X", tag.uid[n]);
}
printf("]");
}
@@ -145,6 +148,7 @@ static int reader_process(){
printf("Slot %2lu: ", n);
reader_print_tag(tags[n].info);
printf(" valid\n");
+ printf("%s\n", tags[n].info.static_data);
//TODO
return 0;
}
diff --git a/reader_yhy.c b/reader_yhy.c
index 92d5080..704a053 100644
--- a/reader_yhy.c
+++ b/reader_yhy.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/timerfd.h>
+#include <string.h>
#include "nfcommander.h"
#include "config.h"
@@ -42,8 +43,8 @@ int handle(int fd){
}
uint8_t read_ntag(nfc_tag_info_t* tag){
- uint8_t data[16];
- size_t bytes;
+ uint8_t data[16] = "";
+ size_t bytes = 0, n = 0;
//read 16 bytes starting at block 2
//[0-3] UID, lock bytes
@@ -86,13 +87,64 @@ uint8_t read_ntag(nfc_tag_info_t* tag){
return FLAG_TAG_UNPROGRAMMED;
}
-
- return 0;
+ //sanity check length data
+ if(data[12] < 6 || !data[13]){
+ return FLAG_TAG_UNPROGRAMMED;
+ }
+
+ //read data
+ tag->static_length = data[13] * tag->granularity;
+ tag->static_data = calloc(tag->static_length, sizeof(uint8_t));
+ if(!tag->static_data){
+ printf("Failed to allocate memory\n");
+ return FLAG_TAG_LOCKED;
+ }
+
+ do{
+ //each read fetches 4 blocks, but due to a probable firmware bug we cant read blocks 0 or 10
+ //so we overwrite 2 bytes per pass and skip the read for 10.
+ uint8_t block = data[12] + n * 2;
+ if(block != 10){
+ bytes = yhy_sync_read(reader_fd, block,
+ tag->static_data + n * 2 * 4,
+ tag->static_length - (n * 2 * 4));
+ }
+ if(bytes <= 0){
+ return FLAG_TAG_LOCKED;
+ }
+ n++;
+ } while(n <= data[13] / 2);
+
+ //read dynamic data if present
+ tag->dynamic_length = data[15] * tag->granularity;
+ if(tag->dynamic_length){
+ tag->dynamic_data = calloc(tag->dynamic_length, sizeof(uint8_t));
+ if(!tag->dynamic_data){
+ printf("Failed to allocate memory\n");
+ return FLAG_TAG_LOCKED;
+ }
+
+ do{
+ uint8_t block = data[14] + n * 2;
+ if(block != 10){
+ bytes = yhy_sync_read(reader_fd, block,
+ tag->dynamic_data + n * 2 * 4,
+ tag->dynamic_length - (n * 2 * 4));
+ }
+ if(bytes <= 0){
+ return FLAG_TAG_LOCKED;
+ }
+ n++;
+ } while(n <= data[15] / 2);
+
+ }
+
+ return FLAG_TAG_DATA_VALID;
}
uint8_t read_mifare(nfc_tag_info_t* tag){
//TODO
- return 0;
+ return FLAG_TAG_LOCKED;
}
int scan(){
@@ -137,15 +189,20 @@ int scan(){
}
}
- if(reader_tag_present(0, &card) == TAG_READ_REQUESTED){
- //read card data
- if(card.type == tag_ntag){
- flags = read_ntag(&card);
- }
- else if(card.type == tag_mifare1){
- flags = read_mifare(&card);
- }
- reader_tag_present(flags, &card);
+ switch(reader_tag_present(0, &card)){
+ case TAG_READ_REQUESTED:
+ //read card data
+ if(card.type == tag_ntag){
+ flags = read_ntag(&card);
+ }
+ else if(card.type == tag_mifare1){
+ flags = read_mifare(&card);
+ }
+ reader_tag_present(flags, &card);
+ break;
+ case TAG_WRITE_REQUESTED:
+ //TODO write card data
+ break;
}
yhy_sync_hlta(reader_fd);