From 662bfa55eccf7cc8b4cc382b79408a995f28267e Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 25 Feb 2019 21:56:45 -0500 Subject: [PATCH] aimeio: Factor out emulated reader assembly app logic --- aimeio/aimeio.c | 96 ++++++++++++++++++++++++++++++++++++++++++++ aimeio/aimeio.def | 8 ++++ aimeio/aimeio.h | 16 ++++++++ aimeio/meson.build | 17 ++++++++ cardhook/_com12.c | 78 ++++++++--------------------------- cardhook/_com12.h | 4 +- cardhook/meson.build | 1 + meson.build | 1 + 8 files changed, 158 insertions(+), 63 deletions(-) create mode 100644 aimeio/aimeio.c create mode 100644 aimeio/aimeio.def create mode 100644 aimeio/aimeio.h create mode 100644 aimeio/meson.build diff --git a/aimeio/aimeio.c b/aimeio/aimeio.c new file mode 100644 index 0000000..ce87102 --- /dev/null +++ b/aimeio/aimeio.c @@ -0,0 +1,96 @@ +#include + +#include +#include +#include + +#include "aimeio/aimeio.h" + +#include "util/crc.h" +#include "util/dprintf.h" + +static const char aime_io_path[] = "DEVICE\\aime.txt"; + +static uint8_t aime_io_luid[10]; + +HRESULT aime_io_init(void) +{ + return S_OK; +} + +void aime_io_fini(void) +{ +} + +HRESULT aime_io_mifare_poll(uint8_t unit_no, uint32_t *uid) +{ + HRESULT hr; + FILE *f; + size_t i; + int byte; + int r; + + if (unit_no != 0) { + return S_FALSE; + } + + hr = S_FALSE; + f = NULL; + + if (!(GetAsyncKeyState(VK_RETURN) & 0x8000)) { + goto end; + } + + f = fopen(aime_io_path, "r"); + + if (f == NULL) { + dprintf("Aime DLL: Failed to open %s\n", aime_io_path); + + goto end; + } + + for (i = 0 ; i < sizeof(aime_io_luid) ; i++) { + r = fscanf(f, "%02x ", &byte); + + if (r != 1) { + dprintf("Aime DLL: fscanf[%i] failed: %i\n", (int) i, r); + + goto end; + } + + aime_io_luid[i] = byte; + } + + /* NOTE: We are just arbitrarily using the CRC32 of the LUID here, real + cards do not work like this! However, neither the application code nor + the network protocol care what the UID is, it just has to be a stable + unique identifier for over-the-air NFC communications. */ + + *uid = crc32(aime_io_luid, sizeof(aime_io_luid), 0); + + hr = S_OK; + +end: + if (f != NULL) { + fclose(f); + } + + return hr; +} + +HRESULT aime_io_mifare_read_luid( + uint8_t unit_no, + uint32_t uid, + uint8_t *luid, + size_t luid_size) +{ + assert(luid != NULL); + assert(luid_size == sizeof(aime_io_luid)); + + memcpy(luid, aime_io_luid, luid_size); + + return S_OK; +} + +void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b) +{} diff --git a/aimeio/aimeio.def b/aimeio/aimeio.def new file mode 100644 index 0000000..a52560d --- /dev/null +++ b/aimeio/aimeio.def @@ -0,0 +1,8 @@ +LIBRARY aimeio + +EXPORTS + aime_io_fini + aime_io_init + aime_io_led_set_color + aime_io_mifare_poll + aime_io_mifare_read_luid diff --git a/aimeio/aimeio.h b/aimeio/aimeio.h new file mode 100644 index 0000000..89247ac --- /dev/null +++ b/aimeio/aimeio.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +#include +#include + +HRESULT aime_io_init(void); +void aime_io_fini(void); +HRESULT aime_io_mifare_poll(uint8_t unit_no, uint32_t *uid); +HRESULT aime_io_mifare_read_luid( + uint8_t unit_no, + uint32_t uid, + uint8_t *luid, + size_t luid_size); +void aime_io_led_set_color(uint8_t unit_no, uint8_t r, uint8_t g, uint8_t b); diff --git a/aimeio/meson.build b/aimeio/meson.build new file mode 100644 index 0000000..7730df6 --- /dev/null +++ b/aimeio/meson.build @@ -0,0 +1,17 @@ +aimeio_dll = shared_library( + 'aimeio', + name_prefix : '', + include_directories: inc, + implicit_include_directories : false, + vs_module_defs : 'aimeio.def', + c_pch : [ + '../precompiled.c', + '../precompiled.h', + ], + link_with : [ + util_lib, + ], + sources : [ + 'aimeio.c', + ], +) diff --git a/cardhook/_com12.c b/cardhook/_com12.c index 56017d6..da46bf0 100644 --- a/cardhook/_com12.c +++ b/cardhook/_com12.c @@ -2,7 +2,8 @@ #include #include -#include + +#include "aimeio/aimeio.h" #include "board/sg-led.h" #include "board/sg-nfc.h" @@ -13,7 +14,6 @@ #include "hooklib/uart.h" -#include "util/crc.h" #include "util/dprintf.h" #include "util/dump.h" @@ -40,16 +40,21 @@ static const struct sg_led_ops com12_led_ops = { static struct sg_nfc com12_nfc; static struct sg_led com12_led; -static const char com12_aime_path[] = "DEVICE\\aime.txt"; - static CRITICAL_SECTION com12_lock; static struct uart com12_uart; static uint8_t com12_written_bytes[520]; static uint8_t com12_readable_bytes[520]; -static uint8_t com12_aime_luid[10]; -void com12_hook_init(void) +HRESULT com12_hook_init(void) { + HRESULT hr; + + hr = aime_io_init(); + + if (FAILED(hr)) { + return hr; + } + sg_nfc_init(&com12_nfc, 0x00, &com12_nfc_ops, NULL); sg_led_init(&com12_led, 0x08, &com12_led_ops, NULL); @@ -61,7 +66,7 @@ void com12_hook_init(void) com12_uart.readable.bytes = com12_readable_bytes; com12_uart.readable.nbytes = sizeof(com12_readable_bytes); - iohook_push_handler(com12_handle_irp); + return iohook_push_handler(com12_handle_irp); } static HRESULT com12_handle_irp(struct irp *irp) @@ -124,70 +129,19 @@ static HRESULT com12_handle_irp_locked(struct irp *irp) static HRESULT com12_mifare_poll(void *ctx, uint32_t *uid) { - HRESULT hr; - FILE *f; - size_t i; - int byte; - int r; - - hr = S_FALSE; - f = NULL; - - if (!(GetAsyncKeyState(VK_RETURN) & 0x8000)) { - goto end; - } - - f = fopen(com12_aime_path, "r"); - - if (f == NULL) { - dprintf("Aime reader: Failed to open %s\n", com12_aime_path); - - goto end; - } - - for (i = 0 ; i < sizeof(com12_aime_luid) ; i++) { - r = fscanf(f, "%02x ", &byte); - - if (r != 1) { - dprintf("Aime reader: fscanf[%i] failed: %i\n", (int) i, r); - - goto end; - } - - com12_aime_luid[i] = byte; - } - - /* NOTE: We are just arbitrarily using the CRC32 of the LUID here, real - cards do not work like this! However, neither the application code nor - the network protocol care what the UID is, it just has to be a stable - unique identifier for over-the-air NFC communications. */ - - *uid = crc32(com12_aime_luid, sizeof(com12_aime_luid), 0); - - hr = S_OK; - -end: - if (f != NULL) { - fclose(f); - } - - return hr; + return aime_io_mifare_poll(0, uid); } static HRESULT com12_mifare_read_luid( void *ctx, uint32_t uid, uint8_t *luid, - size_t nbytes) + size_t luid_size) { - assert(luid != NULL); - assert(nbytes == sizeof(com12_aime_luid)); - - memcpy(luid, com12_aime_luid, nbytes); - - return S_OK; + return aime_io_mifare_read_luid(0, uid, luid, luid_size); } static void com12_led_set_color(void *ctx, uint8_t r, uint8_t g, uint8_t b) { + aime_io_led_set_color(0, r, g, b); } diff --git a/cardhook/_com12.h b/cardhook/_com12.h index 119dd62..9f37744 100644 --- a/cardhook/_com12.h +++ b/cardhook/_com12.h @@ -1,3 +1,5 @@ #pragma once -void com12_hook_init(void); +#include + +HRESULT com12_hook_init(void); diff --git a/cardhook/meson.build b/cardhook/meson.build index 717dbd0..f85dafe 100644 --- a/cardhook/meson.build +++ b/cardhook/meson.build @@ -12,6 +12,7 @@ shared_library( capnhook.get_variable('hooklib_dep'), ], link_with : [ + aimeio_dll, board_lib, util_lib, ], diff --git a/meson.build b/meson.build index 3852044..6d1107f 100644 --- a/meson.build +++ b/meson.build @@ -31,6 +31,7 @@ subdir('jvs') subdir('nu') subdir('util') +subdir('aimeio') subdir('cardhook') subdir('chunihook') subdir('minihook')