diff options
Diffstat (limited to 'reader_yhy.c')
-rw-r--r-- | reader_yhy.c | 85 |
1 files changed, 71 insertions, 14 deletions
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); |