From cd534f500edd5c04fc8e68e72eae3264dfef34fd Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 16 Aug 2010 13:05:13 +0800 Subject: easytool: ad getopt option parser --- easytool/easytool.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 4 deletions(-) (limited to 'easytool') diff --git a/easytool/easytool.c b/easytool/easytool.c index 67157c8..75cec83 100644 --- a/easytool/easytool.c +++ b/easytool/easytool.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -125,20 +126,64 @@ static void print_help(void) { } +enum mode { + MODE_DUMP_MFACC, + MODE_DUMP, + MODE_RECHARGE, + MODE_PURCHASE, +}; + int main(int argc, char **argv) { + enum mode mode = MODE_DUMP; + int delta = 0; + int option_index = 0; struct stat st; printf(COPYRIGHT); - if (argc < 2) { + while (1) { + int c; + static struct option long_options[] = { + { "dump-access-bits", 0, 0, 'a' }, + { "alter-recharge", 1, 0, 'r' }, + { "alter-purchase", 1, 0, 'p' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "r:p:ha", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 'a': + mode = MODE_DUMP_MFACC; + break; + case 'r': + mode = MODE_RECHARGE; + delta = atoi(optarg); + break; + case 'p': + mode = MODE_PURCHASE; + delta = atoi(optarg); + break; + case 'h': + print_help(); + exit(0); + break; + } + }; + + if (argc <= optind) { fprintf(stderr, "ERROR: You must specify the file name of " "a mifare dump file (.mfd)\n"); print_help(); exit(2); } - global.fd = open(argv[1], O_RDONLY); + global.fd = open(argv[optind], O_RDONLY); if (global.fd < 0) { perror("Error opening the MFD file"); exit(1); @@ -155,8 +200,18 @@ int main(int argc, char **argv) exit(1); } - //dump_mfcl(global.mft); - dump_easycard(global.mft); + switch (mode) { + case MODE_DUMP_MFACC: + dump_mfcl(global.mft); + break; + case MODE_DUMP: + dump_easycard(global.mft); + break; + case MODE_RECHARGE: + break; + case MODE_PURCHASE: + break; + } munmap(global.mft, global.size); close(global.fd); -- cgit v1.2.3 From d3c282f9179de48d66b850444ea688bfd02c40d7 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 16 Aug 2010 13:09:04 +0800 Subject: actually call functions to modify the dump file --- easytool/easytool.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'easytool') diff --git a/easytool/easytool.c b/easytool/easytool.c index 75cec83..2e7ada3 100644 --- a/easytool/easytool.c +++ b/easytool/easytool.c @@ -42,16 +42,24 @@ /* Easycard specific includes */ #include "easycard.h" -#define VERSION "0.03" +#define VERSION "0.04" #define COPYRIGHT \ "EasyTool "VERSION" (C) 2010 by Harald Welte \n" \ "This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n" \ "Use of this software is authorized for RESEARCH PURPOSE ONLY!\n\n" +enum mode { + MODE_DUMP_MFACC, + MODE_DUMP, + MODE_RECHARGE, + MODE_PURCHASE, +}; + struct { int fd; unsigned long size; mifare_tag *mft; + enum mode mode; } global; static void dump_acc_bits(const struct acc_bits_parsed *abp) @@ -126,19 +134,14 @@ static void print_help(void) { } -enum mode { - MODE_DUMP_MFACC, - MODE_DUMP, - MODE_RECHARGE, - MODE_PURCHASE, -}; - int main(int argc, char **argv) { - enum mode mode = MODE_DUMP; + struct stat st; int delta = 0; int option_index = 0; - struct stat st; + int rc; + + global.mode = MODE_DUMP; printf(COPYRIGHT); @@ -159,14 +162,14 @@ int main(int argc, char **argv) switch (c) { case 'a': - mode = MODE_DUMP_MFACC; + global.mode = MODE_DUMP_MFACC; break; case 'r': - mode = MODE_RECHARGE; + global.mode = MODE_RECHARGE; delta = atoi(optarg); break; case 'p': - mode = MODE_PURCHASE; + global.mode = MODE_PURCHASE; delta = atoi(optarg); break; case 'h': @@ -200,7 +203,7 @@ int main(int argc, char **argv) exit(1); } - switch (mode) { + switch (global.mode) { case MODE_DUMP_MFACC: dump_mfcl(global.mft); break; @@ -208,8 +211,10 @@ int main(int argc, char **argv) dump_easycard(global.mft); break; case MODE_RECHARGE: + rc = easy_alter_last_recharge(global.mft, delta); break; case MODE_PURCHASE: + rc = easy_alter_last_purchase(global.mft, delta); break; } -- cgit v1.2.3 From 6ed439ecb8c09465ad4bdce53aaca59e935cbbd9 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 17 Aug 2010 10:00:27 +0800 Subject: easytool: open file read/write in case we want to modify content --- easytool/easytool.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'easytool') diff --git a/easytool/easytool.c b/easytool/easytool.c index 2e7ada3..c504003 100644 --- a/easytool/easytool.c +++ b/easytool/easytool.c @@ -140,6 +140,7 @@ int main(int argc, char **argv) int delta = 0; int option_index = 0; int rc; + int prot, flags = O_RDONLY; global.mode = MODE_DUMP; @@ -166,10 +167,12 @@ int main(int argc, char **argv) break; case 'r': global.mode = MODE_RECHARGE; + flags = O_RDWR; delta = atoi(optarg); break; case 'p': global.mode = MODE_PURCHASE; + flags = O_RDWR; delta = atoi(optarg); break; case 'h': @@ -186,7 +189,7 @@ int main(int argc, char **argv) exit(2); } - global.fd = open(argv[optind], O_RDONLY); + global.fd = open(argv[optind], flags); if (global.fd < 0) { perror("Error opening the MFD file"); exit(1); @@ -196,8 +199,12 @@ int main(int argc, char **argv) exit(1); } global.size = st.st_size; - global.mft = mmap(NULL, global.size, PROT_READ, MAP_SHARED, - global.fd, 0); + + prot = PROT_READ; + if (flags == O_RDWR) + prot |= PROT_WRITE; + + global.mft = mmap(NULL, global.size, prot, MAP_SHARED, global.fd, 0); if (!global.mft) { perror("Error mmap()ing the MFD file"); exit(1); -- cgit v1.2.3 From aa0de9c6ae42de1e8528f73af9d99568e7a6aef0 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 17 Aug 2010 10:00:47 +0800 Subject: easytool: Add BUS transaction type (we don't know more details) --- easytool/data.c | 1 + easytool/easycard.h | 1 + 2 files changed, 2 insertions(+) (limited to 'easytool') diff --git a/easytool/data.c b/easytool/data.c index 9b20930..425f0f2 100644 --- a/easytool/data.c +++ b/easytool/data.c @@ -118,6 +118,7 @@ const struct value_string taipei_mrt_stn_id[] = { /* Easycard Transaction Type names */ const struct value_string easy_tt_names[] = { { EASY_TT_MRT_ENTER, "Enter MRT" }, + { EASY_TT_BUS, "Bus ride" }, { EASY_TT_MRT_REENTER, "ReEnter MRT" }, { EASY_TT_MRT_EXIT, "Leave MRT" }, { EASY_TT_PURCHASE, "Shop Purchase" }, diff --git a/easytool/easycard.h b/easytool/easycard.h index 8979524..1631c60 100644 --- a/easytool/easycard.h +++ b/easytool/easycard.h @@ -6,6 +6,7 @@ #include "utils.h" #define EASY_TT_MRT_ENTER 0x00 +#define EASY_TT_BUS 0x01 #define EASY_TT_MRT_REENTER 0x80 #define EASY_TT_MRT_EXIT 0x11 #define EASY_TT_PURCHASE 0x20 -- cgit v1.2.3 From e268c5b72e40da936cdea7cb846adde6ceca522e Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Tue, 17 Aug 2010 10:02:51 +0800 Subject: easytool: fix calculation of 'remaining' when modifying log record --- easytool/easycard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'easytool') diff --git a/easytool/easycard.c b/easytool/easycard.c index 980735d..a98c280 100644 --- a/easytool/easycard.c +++ b/easytool/easycard.c @@ -52,7 +52,7 @@ time_t easy_timestamp2time(const uint8_t *easy_ts) int easy_update_log_rec(struct easy_log_rec *elr, int16_t delta) { int32_t sum = elr->amount + delta; - int32_t remaining = elr->remaining = delta; + int32_t remaining = elr->remaining + delta; if ((sum < 0 || sum > 0xffff) || (remaining < 0 || remaining > 0xffff)) -- cgit v1.2.3