2018-11-21 02:26:11 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2019-10-19 21:51:10 +02:00
|
|
|
#include <stdlib.h>
|
2018-11-21 02:26:11 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "board/sg-cmd.h"
|
|
|
|
#include "board/sg-nfc.h"
|
|
|
|
#include "board/sg-nfc-cmd.h"
|
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
#include "iccard/aime.h"
|
|
|
|
#include "iccard/felica.h"
|
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
#include "util/dprintf.h"
|
2019-10-19 21:51:10 +02:00
|
|
|
#include "util/dump.h"
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_dispatch(
|
|
|
|
void *ctx,
|
|
|
|
const void *v_req,
|
2019-10-27 00:25:04 +02:00
|
|
|
void *v_res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_reset(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_res_header *res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_fw_version(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_get_fw_version *res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_hw_version(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_get_hw_version *res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
static HRESULT sg_nfc_cmd_poll(
|
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_poll *res);
|
2019-10-19 21:51:10 +02:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_aime(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_mifare *mifare);
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_felica(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_felica *felica);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_mifare_read_block(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_nfc_req_mifare_read_block *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_mifare_read_block *res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
static HRESULT sg_nfc_cmd_felica_encap(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
const struct sg_nfc_req_felica_encap *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_felica_encap *res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2024-08-21 15:13:09 +02:00
|
|
|
static HRESULT sg_nfc_cmd_send_hex_data(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
const struct sg_req_header *req,
|
|
|
|
struct sg_res_header *res);
|
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
static HRESULT sg_nfc_cmd_dummy(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_res_header *res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2024-03-17 14:19:56 +01:00
|
|
|
static const struct version_info hw_version[] = {
|
|
|
|
{"TN32MSEC003S H/W Ver3.0", 23},
|
|
|
|
{"837-15286", 9},
|
|
|
|
{"837-15396", 9}
|
2023-12-03 18:45:42 +01:00
|
|
|
};
|
|
|
|
|
2024-03-17 14:19:56 +01:00
|
|
|
static const struct version_info fw_version[] = {
|
|
|
|
{"TN32MSEC003S F/W Ver1.2", 23},
|
|
|
|
{"\x94", 1},
|
|
|
|
{"\x94", 1}
|
2023-12-03 18:45:42 +01:00
|
|
|
};
|
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
void sg_nfc_init(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
uint8_t addr,
|
|
|
|
const struct sg_nfc_ops *ops,
|
2023-12-03 18:45:42 +01:00
|
|
|
unsigned int gen,
|
2018-11-21 02:26:11 +01:00
|
|
|
void *ops_ctx)
|
|
|
|
{
|
|
|
|
assert(nfc != NULL);
|
|
|
|
assert(ops != NULL);
|
|
|
|
|
|
|
|
nfc->ops = ops;
|
|
|
|
nfc->ops_ctx = ops_ctx;
|
|
|
|
nfc->addr = addr;
|
2023-12-03 18:45:42 +01:00
|
|
|
nfc->gen = gen;
|
2018-11-21 02:26:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define sg_nfc_dprintfv(nfc, fmt, ap)
|
|
|
|
#define sg_nfc_dprintf(nfc, fmt, ...)
|
|
|
|
#else
|
|
|
|
static void sg_nfc_dprintfv(
|
|
|
|
const struct sg_nfc *nfc,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
dprintf("NFC %02x: ", nfc->addr);
|
|
|
|
dprintfv(fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sg_nfc_dprintf(
|
|
|
|
const struct sg_nfc *nfc,
|
|
|
|
const char *fmt,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
sg_nfc_dprintfv(nfc, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void sg_nfc_transact(
|
|
|
|
struct sg_nfc *nfc,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct iobuf *res_frame,
|
2018-11-21 02:26:11 +01:00
|
|
|
const void *req_bytes,
|
|
|
|
size_t req_nbytes)
|
|
|
|
{
|
|
|
|
assert(nfc != NULL);
|
2019-10-27 00:25:04 +02:00
|
|
|
assert(res_frame != NULL);
|
2018-11-21 02:26:11 +01:00
|
|
|
assert(req_bytes != NULL);
|
|
|
|
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_req_transact(res_frame, req_bytes, req_nbytes, sg_nfc_dispatch, nfc);
|
2018-11-21 02:26:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_dispatch(
|
|
|
|
void *ctx,
|
|
|
|
const void *v_req,
|
2019-10-27 00:25:04 +02:00
|
|
|
void *v_res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc;
|
2018-11-21 02:26:11 +01:00
|
|
|
const union sg_nfc_req_any *req;
|
2019-10-27 00:25:04 +02:00
|
|
|
union sg_nfc_res_any *res;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
nfc = ctx;
|
|
|
|
req = v_req;
|
2019-10-27 00:25:04 +02:00
|
|
|
res = v_res;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
if (req->simple.hdr.addr != nfc->addr) {
|
|
|
|
/* Not addressed to us, don't send a response */
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (req->simple.hdr.cmd) {
|
|
|
|
case SG_NFC_CMD_RESET:
|
2019-10-27 00:25:04 +02:00
|
|
|
return sg_nfc_cmd_reset(nfc, &req->simple, &res->simple);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
case SG_NFC_CMD_GET_FW_VERSION:
|
|
|
|
return sg_nfc_cmd_get_fw_version(
|
|
|
|
nfc,
|
|
|
|
&req->simple,
|
2019-10-27 00:25:04 +02:00
|
|
|
&res->get_fw_version);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
case SG_NFC_CMD_GET_HW_VERSION:
|
|
|
|
return sg_nfc_cmd_get_hw_version(
|
|
|
|
nfc,
|
|
|
|
&req->simple,
|
2019-10-27 00:25:04 +02:00
|
|
|
&res->get_hw_version);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
case SG_NFC_CMD_POLL:
|
|
|
|
return sg_nfc_cmd_poll(
|
2018-11-21 02:26:11 +01:00
|
|
|
nfc,
|
|
|
|
&req->simple,
|
2019-10-27 00:25:04 +02:00
|
|
|
&res->poll);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
case SG_NFC_CMD_MIFARE_READ_BLOCK:
|
|
|
|
return sg_nfc_cmd_mifare_read_block(
|
|
|
|
nfc,
|
|
|
|
&req->mifare_read_block,
|
2019-10-27 00:25:04 +02:00
|
|
|
&res->mifare_read_block);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
case SG_NFC_CMD_FELICA_ENCAP:
|
|
|
|
return sg_nfc_cmd_felica_encap(
|
|
|
|
nfc,
|
|
|
|
&req->felica_encap,
|
2019-10-27 00:25:04 +02:00
|
|
|
&res->felica_encap);
|
2019-10-19 21:51:10 +02:00
|
|
|
|
2024-09-11 13:31:23 +02:00
|
|
|
case SG_NFC_CMD_MIFARE_AUTHENTICATE_A:
|
2024-09-20 11:09:21 +02:00
|
|
|
case SG_NFC_CMD_MIFARE_AUTHENTICATE_B:
|
2024-08-21 15:13:09 +02:00
|
|
|
case SG_NFC_CMD_SEND_HEX_DATA:
|
|
|
|
return sg_nfc_cmd_send_hex_data(nfc, &req->simple, &res->simple);
|
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
case SG_NFC_CMD_MIFARE_SELECT_TAG:
|
2019-10-19 21:51:10 +02:00
|
|
|
case SG_NFC_CMD_MIFARE_SET_KEY_AIME:
|
|
|
|
case SG_NFC_CMD_MIFARE_SET_KEY_BANA:
|
|
|
|
case SG_NFC_CMD_RADIO_ON:
|
|
|
|
case SG_NFC_CMD_RADIO_OFF:
|
2024-08-21 15:13:09 +02:00
|
|
|
case SG_NFC_CMD_TO_UPDATE_MODE:
|
2019-10-27 00:25:04 +02:00
|
|
|
return sg_nfc_cmd_dummy(nfc, &req->simple, &res->simple);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
sg_nfc_dprintf(nfc, "Unimpl command %02x\n", req->simple.hdr.cmd);
|
|
|
|
|
|
|
|
return E_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_reset(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_res_header *res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
|
|
|
sg_nfc_dprintf(nfc, "Reset\n");
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(res, req, 0);
|
|
|
|
res->status = 3;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_fw_version(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_get_fw_version *res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
2024-03-17 14:19:56 +01:00
|
|
|
const struct version_info *fw = &fw_version[nfc->gen - 1];
|
2023-12-03 18:45:42 +01:00
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
/* Dest version is not NUL terminated, this is intentional */
|
2024-03-17 14:19:56 +01:00
|
|
|
sg_res_init(&res->res, req, fw->length);
|
|
|
|
memcpy(res->version, fw->version, fw->length);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_get_hw_version(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_get_hw_version *res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
2024-03-17 14:19:56 +01:00
|
|
|
const struct version_info *hw = &hw_version[nfc->gen - 1];
|
2023-12-03 18:45:42 +01:00
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
/* Dest version is not NUL terminated, this is intentional */
|
2024-03-17 14:19:56 +01:00
|
|
|
sg_res_init(&res->res, req, hw->length);
|
|
|
|
memcpy(res->version, hw->version, hw->length);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
static HRESULT sg_nfc_cmd_poll(
|
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_poll *res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc_poll_mifare mifare;
|
|
|
|
struct sg_nfc_poll_felica felica;
|
2018-11-21 02:26:11 +01:00
|
|
|
HRESULT hr;
|
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
hr = nfc->ops->poll(nfc->ops_ctx);
|
2018-12-17 23:09:56 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
2018-12-17 23:09:56 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
hr = sg_nfc_poll_felica(nfc, &felica);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
if (SUCCEEDED(hr) && hr != S_FALSE) {
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(&res->res, req, 1 + sizeof(felica));
|
|
|
|
memcpy(res->payload, &felica, sizeof(felica));
|
|
|
|
res->count = 1;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
2019-10-19 21:51:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = sg_nfc_poll_aime(nfc, &mifare);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr) && hr != S_FALSE) {
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(&res->res, req, 1 + sizeof(mifare));
|
|
|
|
memcpy(res->payload, &mifare, sizeof(mifare));
|
|
|
|
res->count = 1;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
2019-10-19 21:51:10 +02:00
|
|
|
}
|
|
|
|
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(&res->res, req, 1);
|
|
|
|
res->count = 0;
|
2019-10-19 21:51:10 +02:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_aime(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_mifare *mifare)
|
|
|
|
{
|
|
|
|
uint8_t luid[10];
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
/* Call backend */
|
|
|
|
|
|
|
|
if (nfc->ops->get_aime_id != NULL) {
|
|
|
|
hr = nfc->ops->get_aime_id(nfc->ops_ctx, luid, sizeof(luid));
|
|
|
|
} else {
|
|
|
|
hr = S_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr) || hr == S_FALSE) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
sg_nfc_dprintf(nfc, "AiMe card is present\n");
|
|
|
|
|
|
|
|
/* Construct response (use an arbitrary UID) */
|
|
|
|
|
|
|
|
mifare->type = 0x10;
|
|
|
|
mifare->id_len = sizeof(mifare->uid);
|
|
|
|
mifare->uid = _byteswap_ulong(0x01020304);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
/* Initialize MIFARE IC emulator */
|
|
|
|
|
|
|
|
hr = aime_card_populate(&nfc->mifare, luid, sizeof(luid));
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2018-11-21 02:26:11 +01:00
|
|
|
return hr;
|
2019-10-19 21:51:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_poll_felica(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
struct sg_nfc_poll_felica *felica)
|
|
|
|
{
|
|
|
|
uint64_t IDm;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
/* Call backend */
|
|
|
|
|
|
|
|
if (nfc->ops->get_felica_id != NULL) {
|
|
|
|
hr = nfc->ops->get_felica_id(nfc->ops_ctx, &IDm);
|
2018-11-21 02:26:11 +01:00
|
|
|
} else {
|
2019-10-19 21:51:10 +02:00
|
|
|
hr = S_FALSE;
|
|
|
|
}
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
if (FAILED(hr) || hr == S_FALSE) {
|
|
|
|
return hr;
|
2018-11-21 02:26:11 +01:00
|
|
|
}
|
2019-10-19 21:51:10 +02:00
|
|
|
|
|
|
|
sg_nfc_dprintf(nfc, "FeliCa card is present\n");
|
|
|
|
|
|
|
|
/* Construct poll response */
|
|
|
|
|
|
|
|
felica->type = 0x20;
|
|
|
|
felica->id_len = sizeof(felica->IDm) + sizeof(felica->PMm);
|
|
|
|
felica->IDm = _byteswap_uint64(IDm);
|
|
|
|
felica->PMm = _byteswap_uint64(felica_get_generic_PMm());
|
|
|
|
|
|
|
|
/* Initialize FeliCa IC emulator */
|
|
|
|
|
|
|
|
nfc->felica.IDm = IDm;
|
|
|
|
nfc->felica.PMm = felica_get_generic_PMm();
|
|
|
|
nfc->felica.system_code = 0x0000;
|
|
|
|
|
|
|
|
return S_OK;
|
2018-11-21 02:26:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT sg_nfc_cmd_mifare_read_block(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_nfc_req_mifare_read_block *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_mifare_read_block *res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
2019-02-25 23:23:04 +01:00
|
|
|
uint32_t uid;
|
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
if (req->req.payload_len != sizeof(req->payload)) {
|
|
|
|
sg_nfc_dprintf(nfc, "%s: Payload size is incorrect\n", __func__);
|
|
|
|
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
uid = _byteswap_ulong(req->payload.uid);
|
2019-02-25 23:23:04 +01:00
|
|
|
|
|
|
|
sg_nfc_dprintf(nfc, "Read uid %08x block %i\n", uid, req->payload.block_no);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
if (req->payload.block_no > 3) {
|
|
|
|
sg_nfc_dprintf(nfc, "MIFARE block number out of range\n");
|
|
|
|
|
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(&res->res, &req->req, sizeof(res->block));
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-27 00:25:04 +02:00
|
|
|
memcpy( res->block,
|
2019-10-19 21:51:10 +02:00
|
|
|
nfc->mifare.sectors[0].blocks[req->payload.block_no].bytes,
|
2019-10-27 00:25:04 +02:00
|
|
|
sizeof(res->block));
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
return S_OK;
|
|
|
|
}
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
static HRESULT sg_nfc_cmd_felica_encap(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
const struct sg_nfc_req_felica_encap *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_nfc_res_felica_encap *res)
|
2019-10-19 21:51:10 +02:00
|
|
|
{
|
|
|
|
struct const_iobuf f_req;
|
|
|
|
struct iobuf f_res;
|
|
|
|
HRESULT hr;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
/* First byte of encapsulated request and response is a length byte
|
|
|
|
(inclusive of itself). The FeliCa emulator expects its caller to handle
|
|
|
|
that length byte on its behalf (we adopt the convention that the length
|
|
|
|
byte is part of the FeliCa protocol's framing layer). */
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
if (req->req.payload_len != 8 + req->payload[0]) {
|
2018-11-21 02:26:11 +01:00
|
|
|
sg_nfc_dprintf(
|
|
|
|
nfc,
|
2019-10-19 21:51:10 +02:00
|
|
|
"FeliCa encap payload length mismatch: sg %i != felica %i + 8",
|
|
|
|
req->req.payload_len,
|
|
|
|
req->payload[0]);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
return E_FAIL;
|
2018-11-21 02:26:11 +01:00
|
|
|
}
|
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
f_req.bytes = req->payload;
|
|
|
|
f_req.nbytes = req->payload[0];
|
|
|
|
f_req.pos = 1;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-27 00:25:04 +02:00
|
|
|
f_res.bytes = res->payload;
|
|
|
|
f_res.nbytes = sizeof(res->payload);
|
2019-10-19 21:51:10 +02:00
|
|
|
f_res.pos = 1;
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2024-06-20 02:22:01 +02:00
|
|
|
#if defined(LOG_NFC)
|
2019-10-19 21:51:10 +02:00
|
|
|
dprintf("FELICA OUTBOUND:\n");
|
|
|
|
dump_const_iobuf(&f_req);
|
|
|
|
#endif
|
2018-11-21 02:26:11 +01:00
|
|
|
|
2019-10-19 21:51:10 +02:00
|
|
|
hr = felica_transact(&nfc->felica, &f_req, &f_res);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(&res->res, &req->req, f_res.pos);
|
|
|
|
res->payload[0] = f_res.pos;
|
2019-10-19 21:51:10 +02:00
|
|
|
|
2024-06-20 02:22:01 +02:00
|
|
|
#if defined(LOG_NFC)
|
2019-10-19 21:51:10 +02:00
|
|
|
dprintf("FELICA INBOUND:\n");
|
|
|
|
dump_iobuf(&f_res);
|
|
|
|
#endif
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2024-08-21 15:13:09 +02:00
|
|
|
static HRESULT sg_nfc_cmd_send_hex_data(
|
|
|
|
struct sg_nfc *nfc,
|
|
|
|
const struct sg_req_header *req,
|
|
|
|
struct sg_res_header *res)
|
|
|
|
{
|
|
|
|
sg_res_init(res, req, 0);
|
|
|
|
|
|
|
|
/* Firmware checksum length? */
|
|
|
|
if (req->payload_len == 0x2b) {
|
|
|
|
/* The firmware is identical flag? */
|
|
|
|
res->status = 0x20;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2018-11-21 02:26:11 +01:00
|
|
|
static HRESULT sg_nfc_cmd_dummy(
|
2019-10-19 21:51:10 +02:00
|
|
|
struct sg_nfc *nfc,
|
2018-11-21 02:26:11 +01:00
|
|
|
const struct sg_req_header *req,
|
2019-10-27 00:25:04 +02:00
|
|
|
struct sg_res_header *res)
|
2018-11-21 02:26:11 +01:00
|
|
|
{
|
2019-10-27 00:25:04 +02:00
|
|
|
sg_res_init(res, req, 0);
|
2018-11-21 02:26:11 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|