mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-27 08:35:12 +02:00
Refactor PHY to support more flexible and scalable architecture.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -528,54 +528,3 @@ int delete_file(file_t *ef) {
|
||||
low_flash_available();
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
int parse_phy_data(const uint8_t *data, uint8_t len) {
|
||||
file_t *ef_phy = search_by_fid(EF_PHY, NULL, SPECIFY_EF);
|
||||
if (!ef_phy) {
|
||||
return CCID_ERR_FILE_NOT_FOUND;
|
||||
}
|
||||
uint8_t tmp[PHY_MAX_SIZE];
|
||||
memset(tmp, 0, sizeof(tmp));
|
||||
uint16_t opts = 0;
|
||||
if (file_has_data(ef_phy)) {
|
||||
memcpy(tmp, file_get_data(ef_phy), MIN(sizeof(tmp), file_get_size(ef_phy)));
|
||||
if (file_get_size(ef_phy) >= 8) {
|
||||
opts = (tmp[PHY_OPTS] << 8) | tmp[PHY_OPTS + 1];
|
||||
}
|
||||
}
|
||||
const uint8_t *p = data;
|
||||
while (p < data + len) {
|
||||
uint8_t tag = *p++;
|
||||
switch (tag) {
|
||||
case PHY_VID:
|
||||
memcpy(tmp + PHY_VID, p, 2);
|
||||
opts |= PHY_OPT_VPID;
|
||||
p += 2;
|
||||
break;
|
||||
case PHY_PID:
|
||||
memcpy(tmp + PHY_PID, p, 2);
|
||||
opts |= PHY_OPT_VPID;
|
||||
p += 2;
|
||||
break;
|
||||
case PHY_LED_GPIO:
|
||||
tmp[PHY_LED_GPIO] = *p++;
|
||||
opts |= PHY_OPT_GPIO;
|
||||
break;
|
||||
case PHY_LED_BTNESS:
|
||||
tmp[PHY_LED_BTNESS] = *p++;
|
||||
opts |= PHY_OPT_BTNESS;
|
||||
break;
|
||||
case PHY_OPTS:
|
||||
opts = (opts & ~PHY_OPT_MASK) | (((*p << 8) | *(p + 1)) & PHY_OPT_MASK);
|
||||
p += 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
tmp[PHY_OPTS] = opts >> 8;
|
||||
tmp[PHY_OPTS + 1] = opts & 0xff;
|
||||
file_put_data(ef_phy, tmp, sizeof(tmp));
|
||||
low_flash_available();
|
||||
return CCID_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _FILE_H_
|
||||
#define _FILE_H_
|
||||
|
||||
@@ -27,6 +26,7 @@
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
#include "compat.h"
|
||||
#include "phy.h"
|
||||
|
||||
#define FILE_TYPE_NOT_KNOWN 0x00
|
||||
#define FILE_TYPE_DF 0x04
|
||||
@@ -67,28 +67,6 @@
|
||||
#define EF_SKDFS 0x6045
|
||||
#define EF_META 0xE010
|
||||
|
||||
#define EF_PHY 0xE020
|
||||
|
||||
#define PHY_VID 0x0
|
||||
#define PHY_PID 0x2
|
||||
#define PHY_LED_GPIO 0x4
|
||||
#define PHY_LED_BTNESS 0x5
|
||||
#define PHY_OPTS 0x6
|
||||
|
||||
#define PHY_OPT_WCID 0x1
|
||||
#define PHY_OPT_VPID 0x2
|
||||
#define PHY_OPT_GPIO 0x4
|
||||
#define PHY_OPT_BTNESS 0x8
|
||||
#define PHY_OPT_DIMM 0x10
|
||||
|
||||
#define PHY_OPT_MASK (PHY_OPT_DIMM | PHY_OPT_WCID)
|
||||
|
||||
#define PHY_MAX_SIZE 8
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
extern int parse_phy_data(const uint8_t *data, uint8_t len);
|
||||
#endif
|
||||
|
||||
#define MAX_DEPTH 4
|
||||
|
||||
typedef struct file {
|
||||
|
||||
99
src/fs/phy.c
Normal file
99
src/fs/phy.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "pico_keys.h"
|
||||
#include "file.h"
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
|
||||
phy_data_t phy_data;
|
||||
|
||||
int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) {
|
||||
if (!phy || !data || !len) {
|
||||
return CCID_ERR_NULL_PARAM;
|
||||
}
|
||||
uint8_t *p = data;
|
||||
if (phy->vidpid_present) {
|
||||
*p++ = PHY_VIDPID;
|
||||
*p++ = phy->vidpid[1];
|
||||
*p++ = phy->vidpid[0];
|
||||
*p++ = phy->vidpid[3];
|
||||
*p++ = phy->vidpid[2];
|
||||
}
|
||||
if (phy->led_gpio_present) {
|
||||
*p++ = PHY_LED_GPIO;
|
||||
*p++ = phy->led_gpio;
|
||||
}
|
||||
if (phy->led_brightness_present) {
|
||||
*p++ = PHY_LED_BTNESS;
|
||||
*p++ = phy->led_brightness;
|
||||
}
|
||||
*p++ = PHY_OPTS;
|
||||
*p++ = phy->opts >> 8;
|
||||
*p++ = phy->opts & 0xff;
|
||||
*len = p - data;
|
||||
return CCID_OK;
|
||||
}
|
||||
#include <stdio.h>
|
||||
int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy) {
|
||||
if (!phy || !data || !len) {
|
||||
return CCID_ERR_NULL_PARAM;
|
||||
}
|
||||
memset(phy, 0, sizeof(phy_data_t));
|
||||
const uint8_t *p = data;
|
||||
while (p < data + len) {
|
||||
printf("TAG %x\n",*p);
|
||||
switch (*p++) {
|
||||
case PHY_VIDPID:
|
||||
memcpy(phy->vidpid, p, 4);
|
||||
phy->vidpid[1] = *p++;
|
||||
phy->vidpid[0] = *p++;
|
||||
phy->vidpid[3] = *p++;
|
||||
phy->vidpid[2] = *p++;
|
||||
phy->vidpid_present = true;
|
||||
break;
|
||||
case PHY_LED_GPIO:
|
||||
phy->led_gpio = *p++;
|
||||
phy->led_gpio_present = true;
|
||||
break;
|
||||
case PHY_LED_BTNESS:
|
||||
phy->led_brightness = *p++;
|
||||
phy->led_brightness_present = true;
|
||||
break;
|
||||
case PHY_OPTS:
|
||||
phy->opts = (*p << 8) | *(p + 1);
|
||||
p += 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return CCID_OK;
|
||||
}
|
||||
|
||||
int phy_init() {
|
||||
memset(&phy_data, 0, sizeof(phy_data_t));
|
||||
if (file_has_data(ef_phy)) {
|
||||
const uint8_t *data = file_get_data(ef_phy);
|
||||
DEBUG_DATA(data, file_get_size(ef_phy));
|
||||
int ret = phy_unserialize_data(data, file_get_size(ef_phy), &phy_data);
|
||||
if (ret != CCID_OK) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return CCID_OK;
|
||||
}
|
||||
#endif
|
||||
60
src/fs/phy.h
Normal file
60
src/fs/phy.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* This file is part of the Pico Keys SDK distribution (https://github.com/polhenarejos/pico-keys-sdk).
|
||||
* Copyright (c) 2022 Pol Henarejos.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _PHY_H_
|
||||
#define _PHY_H_
|
||||
|
||||
#define EF_PHY 0xE020
|
||||
|
||||
#define PHY_VIDPID 0x0
|
||||
#define PHY_LED_GPIO 0x4
|
||||
#define PHY_LED_BTNESS 0x5
|
||||
#define PHY_OPTS 0x6
|
||||
|
||||
#define PHY_OPT_WCID 0x1
|
||||
#define PHY_OPT_DIMM 0x2
|
||||
#define PHY_OPT_SECURE_BOOT 0x4
|
||||
#define PHY_OPT_SECURE_LOCK 0x8
|
||||
|
||||
typedef struct phy_data {
|
||||
union {
|
||||
struct {
|
||||
uint16_t vid;
|
||||
uint16_t pid;
|
||||
};
|
||||
uint8_t vidpid[4];
|
||||
};
|
||||
uint8_t led_gpio;
|
||||
uint8_t led_brightness;
|
||||
uint16_t opts;
|
||||
bool vidpid_present;
|
||||
bool led_gpio_present;
|
||||
bool led_brightness_present;
|
||||
} phy_data_t;
|
||||
|
||||
#define PHY_OPT_MASK (PHY_OPT_SECURE_LOCK | PHY_OPT_SECURE_BOOT | PHY_OPT_DIMM | PHY_OPT_WCID)
|
||||
|
||||
#define PHY_MAX_SIZE 8
|
||||
|
||||
#ifndef ENABLE_EMULATION
|
||||
extern int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len);
|
||||
extern int phy_unserialize_data(const uint8_t *data, uint16_t len, phy_data_t *phy);
|
||||
extern int phy_init();
|
||||
extern phy_data_t phy_data;
|
||||
#endif
|
||||
|
||||
#endif // _PHY_H_
|
||||
Reference in New Issue
Block a user