Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nickez/mif chip #1188

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions external/asf4-drivers/usb/class/vendor/usb_protocol_vendor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* \file
*
* \brief USB Vendor class protocol definitions.
*
* Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
*
* \asf_license_start
*
* \page License
*
* Subject to your compliance with these terms, you may use Microchip
* software and any derivatives exclusively with Microchip products.
* It is your responsibility to comply with third party license terms applicable
* to your use of third party software (including open source software) that
* may accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
* INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
* AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
* LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
* LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
* SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
* POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
* ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
* RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
*/

#ifndef _USB_PROTOCOL_VENDOR_H_
#define _USB_PROTOCOL_VENDOR_H_

/**
* \ingroup usb_protocol_group
* \defgroup usb_vendor_protocol USB Vendor Class definitions
*
*/

/**
* \name Vendor class values
*/
#define VENDOR_CLASS 0xFF
#define VENDOR_SUBCLASS 0xFF
#define VENDOR_PROTOCOL 0xFF

#endif /* _USB_PROTOCOL_VENDOR_H_ */
20 changes: 20 additions & 0 deletions external/asf4-drivers/usb/device/usbdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*/

#include "usbdc.h"
#include "stdio.h"

#define USBDC_VERSION 0x00000001u

Expand Down Expand Up @@ -201,6 +202,7 @@ static bool usbdc_get_str_desc(const uint8_t ep, struct usb_req *req)
if (NULL == str_desc) {
return false;
}
printf("about to return %u %.*ls\n", str_desc[0], str_desc[0]-2, (wchar_t*)&str_desc[2]);
if (length <= str_desc[0]) {
need_zlp = false;
} else {
Expand Down Expand Up @@ -296,8 +298,10 @@ static bool usbdc_get_desc_req(const uint8_t ep, struct usb_req *req)
uint8_t type = (uint8_t)(req->V.wValue >> 8);
switch (type) {
case USB_DT_DEVICE:
printf("idx: %i len: %u lang: 0x%x Get standard device descriptor\n", req->V.wValue & 0xff, req->L.wLength, req->I.wIndex);
return usbdc_get_dev_desc(ep, req);
case USB_DT_CONFIG:
printf("Get standard config descriptor\n");
return usbdc_get_cfg_desc(ep, req);
#if CONF_USBD_HS_SP
case USB_DT_DEVICE_QUALIFIER:
Expand All @@ -306,6 +310,7 @@ static bool usbdc_get_desc_req(const uint8_t ep, struct usb_req *req)
return usbdc_get_othspdcfg_desc(ep, req);
#endif
case USB_DT_STRING:
printf("idx: %i len: %u lang: 0x%x Get standard string descriptor\n", req->V.wValue & 0xff, req->L.wLength, req->I.wIndex);
return usbdc_get_str_desc(ep, req);
default:
break;
Expand Down Expand Up @@ -721,6 +726,12 @@ static void usbd_sof_cb(void)
*/
static bool usbdc_cb_ctl_req(const uint8_t ep, struct usb_req *req)
{
//printf("ctrl req ep: 0x%u, raw: ", ep);
//for(size_t i=0; i<sizeof(struct usb_req); ++i) {
// printf("%02x", ((uint8_t*)req)[i]);
//}
//printf("\n");
//printf("0x%02x 0x%02x val; %i idx: %i len: %i\n", req->bmRequestType, req->bRequest, swap_u16(req->V.wValue), swap_u16(req->I.wIndex), swap_u16(req->L.wLength));
switch (usbdc_request_handler(ep, req, USB_SETUP_STAGE)) {
case true:
return true;
Expand All @@ -737,6 +748,13 @@ static bool usbdc_cb_ctl_req(const uint8_t ep, struct usb_req *req)
case (USB_REQT_TYPE_STANDARD | USB_REQT_DIR_IN):
return usbdc_get_req(ep, req);
default:
printf("nobody wanted it\n");
printf("ctrl req ep: 0x%u, raw: ", ep);
//for(size_t i=0; i<sizeof(struct usb_req); ++i) {
// printf("%02x", ((uint8_t*)req)[i]);
//}
//printf("\n");
printf("0x%02x 0x%02x val; %i idx: %i len: %i\n", req->bmRequestType, req->bRequest, req->V.wValue, req->I.wIndex, req->L.wLength);
return false;
}
}
Expand All @@ -752,11 +770,13 @@ static void usbdc_ctrl_status_end(const struct usb_req *req)
}
switch (req->bRequest) {
case USB_REQ_SET_CONFIG:
printf("set config\n");
usbdc.cfg_value = req->V.wValue;
usbdc.state = req->V.wValue ? USBD_S_CONFIG : USBD_S_ADDRESS;
usbdc_change_notify(USBDC_C_STATE, usbdc.state);
break;
case USB_REQ_SET_ADDRESS:
//printf("set address %u %x\n", req->V.wValue, req->V.wValue);
usbdc_set_address(req->V.wValue);
usbdc.state = req->V.wValue ? USBD_S_ADDRESS : USBD_S_DEFAULT;
usbdc_change_notify(USBDC_C_STATE, usbdc.state);
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ set(DBB-FIRMWARE-USB-SOURCES
${CMAKE_SOURCE_DIR}/src/usb/usb_frame.c
${CMAKE_SOURCE_DIR}/src/usb/usb_packet.c
${CMAKE_SOURCE_DIR}/src/u2f/u2f_packet.c
${CMAKE_SOURCE_DIR}/src/mfi/mfi.c
${CMAKE_SOURCE_DIR}/src/usb/class/iap2/iap2.c
)
set(DBB-FIRMWARE-USB-SOURCES ${DBB-FIRMWARE-USB-SOURCES} PARENT_SCOPE)

Expand Down
22 changes: 13 additions & 9 deletions src/common_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "memory/memory.h"
#include "memory/mpu.h"
#include "memory/smarteeprom.h"
#include "mfi/mfi.h"
#include "random.h"
#include "screen.h"
#include "securechip/securechip.h"
Expand Down Expand Up @@ -83,16 +84,19 @@ void common_main(void)
/* Enable/configure SmartEEPROM. */
smarteeprom_bb02_config();

init_mfi();

// securechip_setup must come after memory_setup, so the io/auth keys to be
// used are already initialized.
int securechip_result = securechip_setup(&_securechip_interface_functions);
if (securechip_result) {
char errmsg[100] = {0};
snprintf(
errmsg,
sizeof(errmsg),
"Securechip setup failed.\nError code: %i\nPlease contact support.",
securechip_result);
AbortAutoenter(errmsg);
}
(void)securechip_result;
// if (securechip_result) {
// char errmsg[100] = {0};
// snprintf(
// errmsg,
// sizeof(errmsg),
// "Securechip setup failed.\nError code: %i\nPlease contact support.",
// securechip_result);
// AbortAutoenter(errmsg);
// }
}
165 changes: 165 additions & 0 deletions src/mfi/mfi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#include "mfi/mfi.h"
#include "hal_delay.h"
#include "hal_i2c_m_sync.h"
#include "hardfault.h"
#include "util.h"
#include <stdbool.h>

extern struct i2c_m_sync_desc I2C_0;

// Default address is defined in docs. See chapter 73.5.3 Addressing, page 684 of Accessory
// Interface Specification
#define MFI_CHIP_DEFAULT_ADDR 0x10
#define MFI_CHIP_RETRIES 25
#define MFI_CHIP_RETRY_DELAY 10

static bool _send(uint8_t* txdata, int txlen)
{
struct _i2c_m_msg packet;
uint8_t retries = MFI_CHIP_RETRIES;
int32_t r;

packet.addr = MFI_CHIP_DEFAULT_ADDR;
packet.len = (int32_t)txlen;
packet.buffer = txdata;
packet.flags = I2C_M_SEVEN | I2C_M_STOP;

do {
r = i2c_m_sync_transfer(&I2C_0, &packet);
delay_ms(MFI_CHIP_RETRY_DELAY);
} while (retries-- && r != I2C_OK);

return (r == I2C_OK ? true : false);
}

static bool _recv(uint8_t* rxdata, int rxlen)
{
struct _i2c_m_msg packet;
uint8_t retries = MFI_CHIP_RETRIES;
int32_t r;

packet.addr = MFI_CHIP_DEFAULT_ADDR;
packet.len = (int32_t)rxlen;
packet.buffer = rxdata;
packet.flags = I2C_M_SEVEN | I2C_M_RD | I2C_M_STOP;

do {
r = i2c_m_sync_transfer(&I2C_0, &packet);
delay_ms(MFI_CHIP_RETRY_DELAY);
} while (retries-- && r != I2C_OK);

return (r == I2C_OK ? true : false);
}

// See chapter 73.5.7 Registers, page 686 of Accessory Interface Specification
#define MFI_DEVICE_VERSION 0x00
#define MFI_DEVICE_VERSION_LEN 1
#define MFI_AUTHENTICATION_REVISION 0x01
#define MFI_AUTHENTICATION_REVISION_LEN 1
#define MFI_AUTHENTICATION_PROTOCOL_MAJOR_VERSION 0x02
#define MFI_AUTHENTICATION_PROTOCOL_MAJOR_VERSION_LEN 1
#define MFI_AUTHENTICATION_PROTOCOL_MINOR_VERSION 0x03
#define MFI_AUTHENTICATION_PROTOCOL_MINOR_VERSION_LEN 1
#define MFI_DEVICE_ID 0x04
#define MFI_DEVICE_ID_LEN 4
#define MFI_ERROR_CODE 0x05
#define MFI_ERROR_CODE_LEN 1
#define MFI_AUTHENTICATION_CONTROL_AND_STATUS 0x10
#define MFI_AUTHENTICATION_CONTROL_AND_STATUS_LEN 1
#define MFI_CHALLENGE_RESPONSE_DATA_LENGTH 0x11
#define MFI_CHALLENGE_RESPONSE_DATA_LENGTH_LEN 2
#define MFI_CHALLENGE_RESPONSE_DATA 0x12
#define MFI_CHALLENGE_RESPONSE_DATA_LEN 64
#define MFI_CHALLENGE_DATA_LENGTH 0x20
#define MFI_CHALLENGE_DATA_LENGTH_LEN 2
#define MFI_CHALLENGE_DATA 0x21
#define MFI_CHALLENGE_DATA_LEN 32
#define MFI_ACCESSORY_CERTIFICATE_DATA_LENGTH 0x30
#define MFI_ACCESSORY_CERTIFICATE_DATA_LENGTH_LEN 2
#define MFI_ACCESSORY_CERTIFICATE_DATA1 0x31
#define MFI_ACCESSORY_CERTIFICATE_DATA1_LEN 128
#define MFI_ACCESSORY_CERTIFICATE_DATA2 0x32
#define MFI_ACCESSORY_CERTIFICATE_DATA2_LEN 128
#define MFI_ACCESSORY_CERTIFICATE_DATA3 0x33
#define MFI_ACCESSORY_CERTIFICATE_DATA3_LEN 128
#define MFI_ACCESSORY_CERTIFICATE_DATA4 0x34
#define MFI_ACCESSORY_CERTIFICATE_DATA4_LEN 128
#define MFI_ACCESSORY_CERTIFICATE_DATA5 0x35
#define MFI_ACCESSORY_CERTIFICATE_DATA5_LEN 128
#define MFI_SELF_TEST_STATUS 0x40
#define MFI_SELF_TEST_STATUS_LEN 1
#define MFI_DEVICE_CERTIFICATE_SERIAL_NUMBER 0x4E
#define MFI_DEVICE_CERTIFICATE_SERIAL_NUMBER_LEN 32
#define MFI_SLEEP 0x60
#define MFI_SLEEP_LEN 1

static uint8_t _reg_len(uint8_t reg)
{
switch (reg) {
case MFI_DEVICE_VERSION:
return MFI_DEVICE_VERSION_LEN;
case MFI_AUTHENTICATION_REVISION:
return MFI_AUTHENTICATION_REVISION_LEN;
case MFI_AUTHENTICATION_PROTOCOL_MAJOR_VERSION:
return MFI_AUTHENTICATION_PROTOCOL_MAJOR_VERSION_LEN;
case MFI_AUTHENTICATION_PROTOCOL_MINOR_VERSION:
return MFI_AUTHENTICATION_PROTOCOL_MINOR_VERSION_LEN;
case MFI_DEVICE_ID:
return MFI_DEVICE_ID_LEN;
case MFI_ERROR_CODE:
return MFI_ERROR_CODE;
case MFI_AUTHENTICATION_CONTROL_AND_STATUS:
return MFI_AUTHENTICATION_CONTROL_AND_STATUS_LEN;
case MFI_CHALLENGE_RESPONSE_DATA_LENGTH:
return MFI_CHALLENGE_RESPONSE_DATA_LENGTH_LEN;
case MFI_CHALLENGE_RESPONSE_DATA:
return MFI_CHALLENGE_RESPONSE_DATA_LEN;
case MFI_CHALLENGE_DATA_LENGTH:
return MFI_CHALLENGE_DATA_LENGTH_LEN;
case MFI_CHALLENGE_DATA:
return MFI_CHALLENGE_DATA_LEN;
case MFI_ACCESSORY_CERTIFICATE_DATA_LENGTH:
return MFI_ACCESSORY_CERTIFICATE_DATA_LENGTH_LEN;
case MFI_ACCESSORY_CERTIFICATE_DATA1:
return MFI_ACCESSORY_CERTIFICATE_DATA1_LEN;
case MFI_ACCESSORY_CERTIFICATE_DATA2:
return MFI_ACCESSORY_CERTIFICATE_DATA2_LEN;
case MFI_ACCESSORY_CERTIFICATE_DATA3:
return MFI_ACCESSORY_CERTIFICATE_DATA3_LEN;
case MFI_ACCESSORY_CERTIFICATE_DATA4:
return MFI_ACCESSORY_CERTIFICATE_DATA4_LEN;
case MFI_ACCESSORY_CERTIFICATE_DATA5:
return MFI_ACCESSORY_CERTIFICATE_DATA5_LEN;
case MFI_SELF_TEST_STATUS:
return MFI_SELF_TEST_STATUS_LEN;
case MFI_DEVICE_CERTIFICATE_SERIAL_NUMBER:
return MFI_DEVICE_CERTIFICATE_SERIAL_NUMBER_LEN;
default:
return 0;
}
}

// Returns number of bytes read
static uint8_t _read_reg(uint8_t reg, uint8_t* buf, int buflen)
{
uint8_t reg_len = _reg_len(reg);
if (buflen < _reg_len(reg)) {
Abort("buflen to short");
}

_send(&reg, reg_len);
_recv(buf, reg_len);
return reg_len;
}

void init_mfi(void)
{
delay_ms(1000);

uint8_t value;
_read_reg(MFI_DEVICE_VERSION, &value, 1);
traceln("recieved 0x%02x, expecting 0x07", value);

_read_reg(MFI_AUTHENTICATION_REVISION, &value, 1);
traceln("recieved 0x%02x, expecting 0x01", value);
}
7 changes: 7 additions & 0 deletions src/mfi/mfi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _MFI_H_
#define _MFI_H_


void init_mfi(void);

#endif
Loading
Loading