From eb0ccdf9903243eed6c0db5b2a3fc7d11cb023e4 Mon Sep 17 00:00:00 2001 From: Felix Date: Sun, 17 Nov 2024 17:59:09 +0100 Subject: [PATCH] adding download --- .vscode/settings.json | 5 +- libfprint/drivers/crfpmoc/crfpmoc.c | 91 ++++++++++++++++++++++++++++- libfprint/drivers/crfpmoc/crfpmoc.h | 40 +++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index cc134d0e..b6a68631 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,8 @@ { "files.associations": { - "cstdlib": "c" + "cstdlib": "c", + "fcntl.h": "c", + "drivers_api.h": "c", + "compare": "cpp" } } \ No newline at end of file diff --git a/libfprint/drivers/crfpmoc/crfpmoc.c b/libfprint/drivers/crfpmoc/crfpmoc.c index bd2f3a32..da73d73f 100644 --- a/libfprint/drivers/crfpmoc/crfpmoc.c +++ b/libfprint/drivers/crfpmoc/crfpmoc.c @@ -295,6 +295,84 @@ crfpmoc_cmd_fp_stats (FpiDeviceCrfpMoc *self, gint8 *template, GError **error) return TRUE; } +static gboolean +crfpmoc_cmd_fp_download_frame (FpiDeviceCrfpMoc *self, const guint16 frame_idx, void *template_buffer, int template_buffer_size, GError **error) +{ + gboolean rv; + + struct crfpmoc_ec_response_fp_info info; + struct crfpmoc_ec_response_get_protocol_info protocol_info; + struct crfpmoc_ec_params_fp_frame p; + guint8 *ptr; + + const int max_attempts = 3; + int num_attempts; + size_t stride, size; + int ec_max_insize; + int template_idx = frame_idx + CRFPMOC_FP_FRAME_INDEX_TEMPLATE; + + + fp_dbg ("Downloading frame %d", template_idx); + + rv = crfpmoc_ec_command(self, CRFPMOC_EC_CMD_GET_PROTOCOL_INFO, 0, NULL, 0, &protocol_info, sizeof(protocol_info), error); + if (!rv) + return rv; + + ec_max_insize = protocol_info.max_response_packet_size - sizeof(struct crfpmoc_ec_host_response); + + rv = crfpmoc_ec_command (self, CRFPMOC_EC_CMD_FP_INFO, 1, NULL, 0, &info, sizeof (info), error); + if (!rv) + return rv; + + fp_dbg ("Fingerprint sensor: vendor %x product %x model %x version %x template size %x", info.vendor_id, info.product_id, info.model_id, info.version, info.template_size); + + if(template_idx < 0 || template_idx >= info.template_max) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Frame index should be between 0 and %d", info.template_max); + return FALSE; + } + + size = info.template_size; + + if(template_buffer_size != size) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Template buffer size should be %ld", size); + return FALSE; + } + + ptr = (guint8 *)(template_buffer); + p.offset = template_idx << CRFPMOC_FP_FRAME_INDEX_SHIFT; + + while (size) { + stride = MIN(ec_max_insize, size); + p.size = stride; + num_attempts = 0; + while (num_attempts < max_attempts) { + num_attempts++; + + rv = crfpmoc_ec_command (self, CRFPMOC_EC_CMD_FP_FRAME, 0, &p, sizeof (p), ptr, stride, error); + + if(!rv) + break; + + + usleep(100000); + } + + // if (!rv) { + // memset(template_buffer, 0, template_buffer_size); + // g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to download frame"); + // return FALSE; + // } + + p.offset += stride; + size -= stride; + ptr += stride; + } + + return TRUE; +} + static void crfpmoc_cmd_wait_event_fingerprint (FpiDeviceCrfpMoc *self) { @@ -340,7 +418,7 @@ crfpmoc_open (FpDevice *device) self->fd = fd; // setting very secure seed - crfpmoc_cmd_fp_seed (self, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &err); + // crfpmoc_cmd_fp_seed (self, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &err); fpi_device_open_complete (device, NULL); } @@ -466,6 +544,17 @@ crfpmoc_enroll_run_state (FpiSsm *ssm, FpDevice *device) user_id = fpi_print_generate_user_id (enroll_print->print); fp_dbg ("New fingerprint ID: %s", user_id); + + // struct crfpmoc_ec_response_fp_info info; + // crfpmoc_ec_command (self, CRFPMOC_EC_CMD_FP_INFO, 1, NULL, 0, &info, sizeof (info), &error); + // char *buffer = g_malloc0 (info.template_size); + // crfpmoc_cmd_fp_download_frame (self, enrolled_templates-1, buffer, info.template_size, &error); + // fp_dbg ("Buffer: %s", buffer); + + // g_free(buffer); + + + g_object_set (enroll_print->print, "description", user_id, NULL); crfpmoc_set_print_data (enroll_print->print, enrolled_templates - 1); diff --git a/libfprint/drivers/crfpmoc/crfpmoc.h b/libfprint/drivers/crfpmoc/crfpmoc.h index cb00e71f..522e0c7c 100644 --- a/libfprint/drivers/crfpmoc/crfpmoc.h +++ b/libfprint/drivers/crfpmoc/crfpmoc.h @@ -77,6 +77,46 @@ G_DECLARE_FINAL_TYPE (FpiDeviceCrfpMoc, fpi_device_crfpmoc, FPI, DEVICE_CRFPMOC, #define CRFPMOC_FP_CONTEXT_ENCRYPTION_SALT_BYTES 16 #define CRFPMOC_FP_CONTEXT_TPM_BYTES 32 +#define CRFPMOC_EC_CMD_FP_FRAME 0x0404 + +/* constants defining the 'offset' field which also contains the frame index */ +#define CRFPMOC_FP_FRAME_INDEX_SHIFT 28 +/* Frame buffer where the captured image is stored */ +#define CRFPMOC_FP_FRAME_INDEX_RAW_IMAGE 0 +/* First frame buffer holding a template */ +#define CRFPMOC_FP_FRAME_INDEX_TEMPLATE 1 +#define CRFPMOC_FP_FRAME_GET_BUFFER_INDEX(offset) ((offset) >> FP_FRAME_INDEX_SHIFT) +#define CRFPMOC_FP_FRAME_OFFSET_MASK 0x0FFFFFFF + + +struct crfpmoc_ec_params_fp_frame { + /* + * The offset contains the template index or FP_FRAME_INDEX_RAW_IMAGE + * in the high nibble, and the real offset within the frame in + * FP_FRAME_OFFSET_MASK. + */ + guint32 offset; + guint32 size; +} __attribute__((packed)); + +#define CRFPMOC_EC_CMD_GET_PROTOCOL_INFO 0x000B +struct crfpmoc_ec_response_get_protocol_info { + /* Fields which exist if at least protocol version 3 supported */ + guint32 protocol_versions; + guint16 max_request_packet_size; + guint16 max_response_packet_size; + guint32 flags; +} __attribute__((packed)); + +struct crfpmoc_ec_host_response { + guint8 struct_version; + guint8 checksum; + guint16 result; + guint16 data_len; + guint16 reserved; +} __attribute__((packed)); + + struct crfpmoc_ec_params_fp_mode {