mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-20 13:23:25 -07:00
Finally, rewrote bootrom and flasher program, much faster now
This commit is contained in:
parent
79a73ab2d1
commit
28fdb04fd8
38 changed files with 311 additions and 1032 deletions
|
@ -8,15 +8,15 @@
|
|||
|
||||
# DO NOT use thumb mode in the phase 1 bootloader since that generates a section with glue code
|
||||
ARMSRC =
|
||||
THUMBSRC = usb_hid.c bootrom.c
|
||||
THUMBSRC = cmd.c usb_cdc.c bootrom.c
|
||||
ASMSRC = ram-reset.s flash-reset.s
|
||||
|
||||
## There is a strange bug with the linker: Sometimes it will not emit the glue to call
|
||||
## BootROM from ARM mode. The symbol is emitted, but the section will be filled with
|
||||
## zeroes. As a temporary workaround, do not use thumb for the phase 2 bootloader
|
||||
## -- Henryk Plötz <henryk@ploetzli.ch> 2009-09-01
|
||||
ARMSRC := $(ARMSRC) $(THUMBSRC)
|
||||
THUMBSRC :=
|
||||
# ARMSRC := $(ARMSRC) $(THUMBSRC)
|
||||
# THUMBSRC :=
|
||||
|
||||
# stdint.h provided locally until GCC 4.5 becomes C99 compliant
|
||||
APP_CFLAGS = -I.
|
||||
|
|
|
@ -7,7 +7,17 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <proxmark3.h>
|
||||
#include "usb_hid.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "cmd.h"
|
||||
//#include "usb_hid.h"
|
||||
|
||||
void DbpString(char *str) {
|
||||
byte_t len = 0;
|
||||
while (str[len] != 0x00) {
|
||||
len++;
|
||||
}
|
||||
cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len);
|
||||
}
|
||||
|
||||
struct common_area common_area __attribute__((section(".commonarea")));
|
||||
unsigned int start_addr, end_addr, bootrom_unlocked;
|
||||
|
@ -76,110 +86,130 @@ static void ConfigClocks(void)
|
|||
|
||||
static void Fatal(void)
|
||||
{
|
||||
for(;;);
|
||||
LED_D_OFF();
|
||||
LED_C_OFF();
|
||||
LED_B_OFF();
|
||||
LED_A_OFF();
|
||||
for(;;);
|
||||
}
|
||||
|
||||
void UsbPacketReceived(uint8_t *packet, int len)
|
||||
{
|
||||
int i, dont_ack=0;
|
||||
UsbCommand *c = (UsbCommand *)packet;
|
||||
volatile uint32_t *p;
|
||||
|
||||
if(len != sizeof(*c)) {
|
||||
Fatal();
|
||||
}
|
||||
|
||||
switch(c->cmd) {
|
||||
case CMD_DEVICE_INFO:
|
||||
dont_ack = 1;
|
||||
c->cmd = CMD_DEVICE_INFO;
|
||||
c->arg[0] = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
|
||||
DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
|
||||
if(common_area.flags.osimage_present) c->arg[0] |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
|
||||
UsbSendPacket(packet, len);
|
||||
break;
|
||||
|
||||
case CMD_SETUP_WRITE:
|
||||
/* The temporary write buffer of the embedded flash controller is mapped to the
|
||||
* whole memory region, only the last 8 bits are decoded.
|
||||
*/
|
||||
p = (volatile uint32_t *)&_flash_start;
|
||||
for(i = 0; i < 12; i++) {
|
||||
p[i+c->arg[0]] = c->d.asDwords[i];
|
||||
}
|
||||
break;
|
||||
|
||||
case CMD_FINISH_WRITE:
|
||||
p = (volatile uint32_t *)&_flash_start;
|
||||
for(i = 0; i < 4; i++) {
|
||||
p[i+60] = c->d.asDwords[i];
|
||||
}
|
||||
|
||||
/* Check that the address that we are supposed to write to is within our allowed region */
|
||||
if( ((c->arg[0]+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (c->arg[0] < start_addr) ) {
|
||||
/* Disallow write */
|
||||
dont_ack = 1;
|
||||
c->cmd = CMD_NACK;
|
||||
UsbSendPacket(packet, len);
|
||||
} else {
|
||||
/* Translate address to flash page and do flash, update here for the 512k part */
|
||||
AT91C_BASE_EFC0->EFC_FCR = MC_FLASH_COMMAND_KEY |
|
||||
MC_FLASH_COMMAND_PAGEN((c->arg[0]-(int)&_flash_start)/AT91C_IFLASH_PAGE_SIZE) |
|
||||
AT91C_MC_FCMD_START_PROG;
|
||||
}
|
||||
|
||||
uint32_t sr;
|
||||
|
||||
while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY))
|
||||
;
|
||||
if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
|
||||
dont_ack = 1;
|
||||
c->cmd = CMD_NACK;
|
||||
UsbSendPacket(packet, len);
|
||||
}
|
||||
break;
|
||||
|
||||
case CMD_HARDWARE_RESET:
|
||||
USB_D_PLUS_PULLUP_OFF();
|
||||
AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
|
||||
break;
|
||||
|
||||
case CMD_START_FLASH:
|
||||
if(c->arg[2] == START_FLASH_MAGIC) bootrom_unlocked = 1;
|
||||
else bootrom_unlocked = 0;
|
||||
{
|
||||
int prot_start = (int)&_bootrom_start;
|
||||
int prot_end = (int)&_bootrom_end;
|
||||
int allow_start = (int)&_flash_start;
|
||||
int allow_end = (int)&_flash_end;
|
||||
int cmd_start = c->arg[0];
|
||||
int cmd_end = c->arg[1];
|
||||
|
||||
/* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
|
||||
* bootrom area. In any case they must be within the flash area.
|
||||
*/
|
||||
if( (bootrom_unlocked || ((cmd_start >= prot_end) || (cmd_end < prot_start)))
|
||||
&& (cmd_start >= allow_start) && (cmd_end <= allow_end) ) {
|
||||
start_addr = cmd_start;
|
||||
end_addr = cmd_end;
|
||||
} else {
|
||||
start_addr = end_addr = 0;
|
||||
dont_ack = 1;
|
||||
c->cmd = CMD_NACK;
|
||||
UsbSendPacket(packet, len);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
Fatal();
|
||||
break;
|
||||
}
|
||||
|
||||
if(!dont_ack) {
|
||||
c->cmd = CMD_ACK;
|
||||
UsbSendPacket(packet, len);
|
||||
}
|
||||
void UsbPacketReceived(uint8_t *packet, int len) {
|
||||
int i, dont_ack=0;
|
||||
UsbCommand* c = (UsbCommand *)packet;
|
||||
volatile uint32_t *p;
|
||||
|
||||
if(len != sizeof(UsbCommand)) {
|
||||
Fatal();
|
||||
}
|
||||
|
||||
uint32_t arg0 = (uint32_t)c->arg[0];
|
||||
|
||||
switch(c->cmd) {
|
||||
case CMD_DEVICE_INFO: {
|
||||
dont_ack = 1;
|
||||
// c->cmd = CMD_DEVICE_INFO;
|
||||
arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
|
||||
DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
|
||||
if(common_area.flags.osimage_present) {
|
||||
arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
|
||||
}
|
||||
// UsbSendPacket(packet, len);
|
||||
cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0);
|
||||
} break;
|
||||
|
||||
case CMD_SETUP_WRITE: {
|
||||
/* The temporary write buffer of the embedded flash controller is mapped to the
|
||||
* whole memory region, only the last 8 bits are decoded.
|
||||
*/
|
||||
p = (volatile uint32_t *)&_flash_start;
|
||||
for(i = 0; i < 12; i++) {
|
||||
p[i+arg0] = c->d.asDwords[i];
|
||||
}
|
||||
} break;
|
||||
|
||||
case CMD_FINISH_WRITE: {
|
||||
uint32_t* flash_mem = (uint32_t*)(&_flash_start);
|
||||
// p = (volatile uint32_t *)&_flash_start;
|
||||
for (size_t j=0; j<2; j++) {
|
||||
for(i = 0+(64*j); i < 64+(64*j); i++) {
|
||||
//p[i+60] = c->d.asDwords[i];
|
||||
flash_mem[i] = c->d.asDwords[i];
|
||||
}
|
||||
|
||||
uint32_t flash_address = arg0 + (0x100*j);
|
||||
|
||||
/* Check that the address that we are supposed to write to is within our allowed region */
|
||||
if( ((flash_address+AT91C_IFLASH_PAGE_SIZE-1) >= end_addr) || (flash_address < start_addr) ) {
|
||||
/* Disallow write */
|
||||
dont_ack = 1;
|
||||
// c->cmd = CMD_NACK;
|
||||
// UsbSendPacket(packet, len);
|
||||
cmd_send(CMD_NACK,0,0,0,0,0);
|
||||
} else {
|
||||
uint32_t page_n = (flash_address - ((uint32_t)flash_mem)) / AT91C_IFLASH_PAGE_SIZE;
|
||||
/* Translate address to flash page and do flash, update here for the 512k part */
|
||||
AT91C_BASE_EFC0->EFC_FCR = MC_FLASH_COMMAND_KEY |
|
||||
MC_FLASH_COMMAND_PAGEN(page_n) |
|
||||
AT91C_MC_FCMD_START_PROG;
|
||||
// arg0 = (address - ((uint32_t)flash_s));
|
||||
}
|
||||
|
||||
// Wait until flashing of page finishes
|
||||
uint32_t sr;
|
||||
while(!((sr = AT91C_BASE_EFC0->EFC_FSR) & AT91C_MC_FRDY));
|
||||
if(sr & (AT91C_MC_LOCKE | AT91C_MC_PROGE)) {
|
||||
dont_ack = 1;
|
||||
// c->cmd = CMD_NACK;
|
||||
cmd_send(CMD_NACK,0,0,0,0,0);
|
||||
// UsbSendPacket(packet, len);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
case CMD_HARDWARE_RESET: {
|
||||
// USB_D_PLUS_PULLUP_OFF();
|
||||
usb_disable();
|
||||
AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
|
||||
} break;
|
||||
|
||||
case CMD_START_FLASH: {
|
||||
if(c->arg[2] == START_FLASH_MAGIC) bootrom_unlocked = 1;
|
||||
else bootrom_unlocked = 0;
|
||||
{
|
||||
int prot_start = (int)&_bootrom_start;
|
||||
int prot_end = (int)&_bootrom_end;
|
||||
int allow_start = (int)&_flash_start;
|
||||
int allow_end = (int)&_flash_end;
|
||||
int cmd_start = c->arg[0];
|
||||
int cmd_end = c->arg[1];
|
||||
|
||||
/* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
|
||||
* bootrom area. In any case they must be within the flash area.
|
||||
*/
|
||||
if( (bootrom_unlocked || ((cmd_start >= prot_end) || (cmd_end < prot_start)))
|
||||
&& (cmd_start >= allow_start) && (cmd_end <= allow_end) ) {
|
||||
start_addr = cmd_start;
|
||||
end_addr = cmd_end;
|
||||
} else {
|
||||
start_addr = end_addr = 0;
|
||||
dont_ack = 1;
|
||||
// c->cmd = CMD_NACK;
|
||||
// UsbSendPacket(packet, len);
|
||||
cmd_send(CMD_NACK,0,0,0,0,0);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
default: {
|
||||
Fatal();
|
||||
} break;
|
||||
}
|
||||
|
||||
if(!dont_ack) {
|
||||
// c->cmd = CMD_ACK;
|
||||
// UsbSendPacket(packet, len);
|
||||
cmd_send(CMD_ACK,arg0,0,0,0,0);
|
||||
}
|
||||
}
|
||||
|
||||
static void flash_mode(int externally_entered)
|
||||
|
@ -187,16 +217,34 @@ static void flash_mode(int externally_entered)
|
|||
start_addr = 0;
|
||||
end_addr = 0;
|
||||
bootrom_unlocked = 0;
|
||||
byte_t rx[sizeof(UsbCommand)];
|
||||
size_t rx_len;
|
||||
|
||||
UsbStart();
|
||||
usb_enable();
|
||||
for (volatile size_t i=0; i<0x100000; i++);
|
||||
LED_D_ON();
|
||||
LED_C_ON();
|
||||
LED_B_ON();
|
||||
LED_A_ON();
|
||||
|
||||
// UsbStart();
|
||||
for(;;) {
|
||||
WDT_HIT();
|
||||
|
||||
UsbPoll(TRUE);
|
||||
if (usb_poll()) {
|
||||
rx_len = usb_read(rx,sizeof(UsbCommand));
|
||||
if (rx_len) {
|
||||
// DbpString("starting to flash");
|
||||
UsbPacketReceived(rx,rx_len);
|
||||
}
|
||||
}
|
||||
|
||||
// UsbPoll(TRUE);
|
||||
|
||||
if(!externally_entered && !BUTTON_PRESS()) {
|
||||
/* Perform a reset to leave flash mode */
|
||||
USB_D_PLUS_PULLUP_OFF();
|
||||
// USB_D_PLUS_PULLUP_OFF();
|
||||
usb_disable();
|
||||
LED_B_ON();
|
||||
AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
|
||||
for(;;);
|
||||
|
@ -208,7 +256,7 @@ static void flash_mode(int externally_entered)
|
|||
}
|
||||
}
|
||||
|
||||
extern char _osimage_entry;
|
||||
extern uint32_t _osimage_entry;
|
||||
void BootROM(void)
|
||||
{
|
||||
//------------
|
||||
|
@ -252,7 +300,8 @@ void BootROM(void)
|
|||
GPIO_LED_C |
|
||||
GPIO_LED_D;
|
||||
|
||||
USB_D_PLUS_PULLUP_OFF();
|
||||
// USB_D_PLUS_PULLUP_OFF();
|
||||
usb_disable();
|
||||
LED_D_OFF();
|
||||
LED_C_ON();
|
||||
LED_B_OFF();
|
||||
|
@ -297,7 +346,7 @@ void BootROM(void)
|
|||
flash_mode(1);
|
||||
} else if(BUTTON_PRESS()) {
|
||||
flash_mode(0);
|
||||
} else if(*(uint32_t*)&_osimage_entry == 0xffffffffU) {
|
||||
} else if(_osimage_entry == 0xffffffffU) {
|
||||
flash_mode(1);
|
||||
} else {
|
||||
// jump to Flash address of the osimage entry point (LSBit set for thumb mode)
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (C) 2010 Hector Martin "marcan" <marcan@marcansoft.com>
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// Replacement stdint.h because GCC doesn't come with it yet (C99)
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef __STDINT_H
|
||||
#define __STDINT_H
|
||||
|
||||
typedef signed char int8_t;
|
||||
typedef short int int16_t;
|
||||
typedef int int32_t;
|
||||
typedef long long int int64_t;
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short int uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef unsigned long long int uint64_t;
|
||||
|
||||
typedef int intptr_t;
|
||||
typedef unsigned int uintptr_t;
|
||||
|
||||
#endif /* __STDINT_H */
|
|
@ -1,524 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Jonathan Westhues, split Aug 14 2005
|
||||
//
|
||||
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
||||
// at your option, any later version. See the LICENSE.txt file for the text of
|
||||
// the license.
|
||||
//-----------------------------------------------------------------------------
|
||||
// The common USB driver used for both the bootloader and the application.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "proxmark3.h"
|
||||
#include "usb_hid.h"
|
||||
|
||||
#define min(a, b) (((a) > (b)) ? (b) : (a))
|
||||
|
||||
#define USB_REPORT_PACKET_SIZE 64
|
||||
|
||||
typedef struct PACKED {
|
||||
uint8_t bmRequestType;
|
||||
uint8_t bRequest;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
} UsbSetupData;
|
||||
|
||||
#define USB_REQUEST_GET_STATUS 0
|
||||
#define USB_REQUEST_CLEAR_FEATURE 1
|
||||
#define USB_REQUEST_SET_FEATURE 3
|
||||
#define USB_REQUEST_SET_ADDRESS 5
|
||||
#define USB_REQUEST_GET_DESCRIPTOR 6
|
||||
#define USB_REQUEST_SET_DESCRIPTOR 7
|
||||
#define USB_REQUEST_GET_CONFIGURATION 8
|
||||
#define USB_REQUEST_SET_CONFIGURATION 9
|
||||
#define USB_REQUEST_GET_INTERFACE 10
|
||||
#define USB_REQUEST_SET_INTERFACE 11
|
||||
#define USB_REQUEST_SYNC_FRAME 12
|
||||
|
||||
#define USB_DESCRIPTOR_TYPE_DEVICE 1
|
||||
#define USB_DESCRIPTOR_TYPE_CONFIGURATION 2
|
||||
#define USB_DESCRIPTOR_TYPE_STRING 3
|
||||
#define USB_DESCRIPTOR_TYPE_INTERFACE 4
|
||||
#define USB_DESCRIPTOR_TYPE_ENDPOINT 5
|
||||
#define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 6
|
||||
#define USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONF 7
|
||||
#define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 8
|
||||
#define USB_DESCRIPTOR_TYPE_HID 0x21
|
||||
#define USB_DESCRIPTOR_TYPE_HID_REPORT 0x22
|
||||
|
||||
#define USB_DEVICE_CLASS_HID 0x03
|
||||
|
||||
static const uint8_t HidReportDescriptor[] = {
|
||||
0x06,0xA0,0xFF, // Usage Page (vendor defined) FFA0
|
||||
0x09,0x01, // Usage (vendor defined)
|
||||
0xA1,0x01, // Collection (Application)
|
||||
0x09,0x02, // Usage (vendor defined)
|
||||
0xA1,0x00, // Collection (Physical)
|
||||
0x06,0xA1,0xFF, // Usage Page (vendor defined)
|
||||
|
||||
//The,input report
|
||||
0x09,0x03, // usage - vendor defined
|
||||
0x09,0x04, // usage - vendor defined
|
||||
0x15,0x80, // Logical Minimum (-128)
|
||||
0x25,0x7F, // Logical Maximum (127)
|
||||
0x35,0x00, // Physical Minimum (0)
|
||||
0x45,0xFF, // Physical Maximum (255)
|
||||
0x75,0x08, // Report Size (8) (bits)
|
||||
0x95,0x40, // Report Count (64) (fields)
|
||||
0x81,0x02, // Input (Data,Variable,Absolute)
|
||||
|
||||
//The,output report
|
||||
0x09,0x05, // usage - vendor defined
|
||||
0x09,0x06, // usage - vendor defined
|
||||
0x15,0x80, // Logical Minimum (-128)
|
||||
0x25,0x7F, // Logical Maximum (127)
|
||||
0x35,0x00, // Physical Minimum (0)
|
||||
0x45,0xFF, // Physical Maximum (255)
|
||||
0x75,0x08, // Report Size (8) (bits)
|
||||
0x95,0x40, // Report Count (64) (fields)
|
||||
0x91,0x02, // Output (Data,Variable,Absolute)
|
||||
|
||||
0xC0, // End Collection
|
||||
|
||||
0xC0, // End Collection
|
||||
};
|
||||
|
||||
static const uint8_t DeviceDescriptor[] = {
|
||||
0x12, // Descriptor length (18 bytes)
|
||||
0x01, // Descriptor type (Device)
|
||||
0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10)
|
||||
0x00, // Class code (0)
|
||||
0x00, // Subclass code (0)
|
||||
0x00, // Protocol (No specific protocol)
|
||||
0x08, // Maximum packet size for Endpoint 0 (8 bytes)
|
||||
0xc4,0x9a, // Vendor ID (random numbers)
|
||||
0x8f,0x4b, // Product ID (random numbers)
|
||||
0x01,0x00, // Device release number (0001)
|
||||
0x01, // Manufacturer string descriptor index
|
||||
0x02, // Product string descriptor index
|
||||
0x03, // Serial Number string descriptor index
|
||||
0x01, // Number of possible configurations (1)
|
||||
};
|
||||
|
||||
static const uint8_t ConfigurationDescriptor[] = {
|
||||
0x09, // Descriptor length (9 bytes)
|
||||
0x02, // Descriptor type (Configuration)
|
||||
0x29,0x00, // Total data length (41 bytes)
|
||||
0x01, // Interface supported (1)
|
||||
0x01, // Configuration value (1)
|
||||
0x00, // Index of string descriptor (None)
|
||||
0x80, // Configuration (Bus powered)
|
||||
250, // Maximum power consumption (500mA)
|
||||
|
||||
//interface
|
||||
0x09, // Descriptor length (9 bytes)
|
||||
0x04, // Descriptor type (Interface)
|
||||
0x00, // Number of interface (0)
|
||||
0x00, // Alternate setting (0)
|
||||
0x02, // Number of interface endpoint (2)
|
||||
0x03, // Class code (HID)
|
||||
0x00, // Subclass code ()
|
||||
0x00, // Protocol code ()
|
||||
0x00, // Index of string()
|
||||
|
||||
// class
|
||||
0x09, // Descriptor length (9 bytes)
|
||||
0x21, // Descriptor type (HID)
|
||||
0x00,0x01, // HID class release number (1.00)
|
||||
0x00, // Localized country code (None)
|
||||
0x01, // # of HID class dscrptr to follow (1)
|
||||
0x22, // Report descriptor type (HID)
|
||||
// Total length of report descriptor
|
||||
sizeof(HidReportDescriptor),0x00,
|
||||
|
||||
// endpoint 1
|
||||
0x07, // Descriptor length (7 bytes)
|
||||
0x05, // Descriptor type (Endpoint)
|
||||
0x01, // Encoded address (Respond to OUT)
|
||||
0x03, // Endpoint attribute (Interrupt transfer)
|
||||
0x08,0x00, // Maximum packet size (8 bytes)
|
||||
0x01, // Polling interval (1 ms)
|
||||
|
||||
// endpoint 2
|
||||
0x07, // Descriptor length (7 bytes)
|
||||
0x05, // Descriptor type (Endpoint)
|
||||
0x82, // Encoded address (Respond to IN)
|
||||
0x03, // Endpoint attribute (Interrupt transfer)
|
||||
0x08,0x00, // Maximum packet size (8 bytes)
|
||||
0x01, // Polling interval (1 ms)
|
||||
};
|
||||
|
||||
static const uint8_t StringDescriptor0[] = {
|
||||
0x04, // Length
|
||||
0x03, // Type is string
|
||||
0x09, // English
|
||||
0x04, // US
|
||||
};
|
||||
|
||||
static const uint8_t StringDescriptor1[] = {
|
||||
24, // Length
|
||||
0x03, // Type is string
|
||||
'J', 0x00,
|
||||
'.', 0x00,
|
||||
' ', 0x00,
|
||||
'W', 0x00,
|
||||
'e', 0x00,
|
||||
's', 0x00,
|
||||
't', 0x00,
|
||||
'h', 0x00,
|
||||
'u', 0x00,
|
||||
'e', 0x00,
|
||||
's', 0x00,
|
||||
};
|
||||
|
||||
static const uint8_t StringDescriptor2[] = {
|
||||
54, // Length
|
||||
0x03, // Type is string
|
||||
'P', 0x00,
|
||||
'r', 0x00,
|
||||
'o', 0x00,
|
||||
'x', 0x00,
|
||||
'M', 0x00,
|
||||
'a', 0x00,
|
||||
'r', 0x00,
|
||||
'k', 0x00,
|
||||
'-', 0x00,
|
||||
'3', 0x00,
|
||||
' ', 0x00,
|
||||
'R', 0x00,
|
||||
'F', 0x00,
|
||||
'I', 0x00,
|
||||
'D', 0x00,
|
||||
' ', 0x00,
|
||||
'I', 0x00,
|
||||
'n', 0x00,
|
||||
's', 0x00,
|
||||
't', 0x00,
|
||||
'r', 0x00,
|
||||
'u', 0x00,
|
||||
'm', 0x00,
|
||||
'e', 0x00,
|
||||
'n', 0x00,
|
||||
't', 0x00,
|
||||
};
|
||||
|
||||
// Serial Number
|
||||
// TODO: Pick yours! Don't forget to modify the length, if needed.
|
||||
static const uint8_t StringDescriptor3[] = {
|
||||
18, // Length
|
||||
0x03, // Type is string
|
||||
'C', 0x00,
|
||||
'h', 0x00,
|
||||
'a', 0x00,
|
||||
'n', 0x00,
|
||||
'g', 0x00,
|
||||
'e', 0x00,
|
||||
'M', 0x00,
|
||||
'e', 0x00,
|
||||
};
|
||||
|
||||
static const uint8_t * const StringDescriptors[] = {
|
||||
StringDescriptor0,
|
||||
StringDescriptor1,
|
||||
StringDescriptor2,
|
||||
StringDescriptor3,
|
||||
};
|
||||
|
||||
|
||||
static uint8_t UsbBuffer[64];
|
||||
static int UsbSoFarCount;
|
||||
|
||||
static uint8_t CurrentConfiguration;
|
||||
|
||||
static void UsbSendEp0(const uint8_t *data, int len)
|
||||
{
|
||||
int thisTime, i;
|
||||
|
||||
do {
|
||||
thisTime = min(len, 8);
|
||||
len -= thisTime;
|
||||
|
||||
for(i = 0; i < thisTime; i++) {
|
||||
AT91C_BASE_UDP->UDP_FDR[0] = *data;
|
||||
data++;
|
||||
}
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP) {
|
||||
AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;
|
||||
while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)
|
||||
;
|
||||
}
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;
|
||||
|
||||
do {
|
||||
if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RX_DATA_BK0) {
|
||||
// This means that the host is trying to write to us, so
|
||||
// abandon our write to them.
|
||||
AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_RX_DATA_BK0;
|
||||
return;
|
||||
}
|
||||
} while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP));
|
||||
} while(len > 0);
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP) {
|
||||
AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;
|
||||
while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
static void UsbSendZeroLength(void)
|
||||
{
|
||||
AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_TXPKTRDY;
|
||||
|
||||
while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP))
|
||||
;
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_TXCOMP;
|
||||
|
||||
while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_TXCOMP)
|
||||
;
|
||||
}
|
||||
|
||||
static void UsbSendStall(void)
|
||||
{
|
||||
AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_FORCESTALL;
|
||||
|
||||
while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_STALLSENT))
|
||||
;
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_STALLSENT;
|
||||
|
||||
while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_STALLSENT)
|
||||
;
|
||||
}
|
||||
|
||||
static void HandleRxdSetupData(void)
|
||||
{
|
||||
int i;
|
||||
UsbSetupData usd;
|
||||
|
||||
for(i = 0; i < sizeof(usd); i++) {
|
||||
((uint8_t *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0];
|
||||
}
|
||||
|
||||
if(usd.bmRequestType & 0x80) {
|
||||
AT91C_BASE_UDP->UDP_CSR[0] |= AT91C_UDP_DIR;
|
||||
while(!(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_DIR))
|
||||
;
|
||||
}
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[0] &= ~AT91C_UDP_RXSETUP;
|
||||
while(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RXSETUP)
|
||||
;
|
||||
|
||||
switch(usd.bRequest) {
|
||||
case USB_REQUEST_GET_DESCRIPTOR:
|
||||
if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_DEVICE) {
|
||||
UsbSendEp0((uint8_t *)&DeviceDescriptor,
|
||||
min(sizeof(DeviceDescriptor), usd.wLength));
|
||||
} else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_CONFIGURATION) {
|
||||
UsbSendEp0((uint8_t *)&ConfigurationDescriptor,
|
||||
min(sizeof(ConfigurationDescriptor), usd.wLength));
|
||||
} else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_STRING) {
|
||||
const uint8_t *s = StringDescriptors[usd.wValue & 0xff];
|
||||
UsbSendEp0(s, min(s[0], usd.wLength));
|
||||
} else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_HID_REPORT) {
|
||||
UsbSendEp0((uint8_t *)&HidReportDescriptor,
|
||||
min(sizeof(HidReportDescriptor), usd.wLength));
|
||||
} else {
|
||||
*((uint32_t *)0x00200000) = usd.wValue;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQUEST_SET_ADDRESS:
|
||||
UsbSendZeroLength();
|
||||
AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN | usd.wValue ;
|
||||
if(usd.wValue != 0) {
|
||||
AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
|
||||
} else {
|
||||
AT91C_BASE_UDP->UDP_GLBSTATE = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case USB_REQUEST_GET_CONFIGURATION:
|
||||
UsbSendEp0(&CurrentConfiguration, sizeof(CurrentConfiguration));
|
||||
break;
|
||||
|
||||
case USB_REQUEST_GET_STATUS: {
|
||||
if(usd.bmRequestType & 0x80) {
|
||||
uint16_t w = 0;
|
||||
UsbSendEp0((uint8_t *)&w, sizeof(w));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case USB_REQUEST_SET_CONFIGURATION:
|
||||
CurrentConfiguration = usd.wValue;
|
||||
if(CurrentConfiguration) {
|
||||
AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
|
||||
AT91C_BASE_UDP->UDP_CSR[1] = AT91C_UDP_EPEDS |
|
||||
AT91C_UDP_EPTYPE_INT_OUT;
|
||||
AT91C_BASE_UDP->UDP_CSR[2] = AT91C_UDP_EPEDS |
|
||||
AT91C_UDP_EPTYPE_INT_IN;
|
||||
} else {
|
||||
AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
|
||||
AT91C_BASE_UDP->UDP_CSR[1] = 0;
|
||||
AT91C_BASE_UDP->UDP_CSR[2] = 0;
|
||||
}
|
||||
UsbSendZeroLength();
|
||||
break;
|
||||
|
||||
case USB_REQUEST_GET_INTERFACE: {
|
||||
uint8_t b = 0;
|
||||
UsbSendEp0(&b, sizeof(b));
|
||||
break;
|
||||
}
|
||||
|
||||
case USB_REQUEST_SET_INTERFACE:
|
||||
UsbSendZeroLength();
|
||||
break;
|
||||
|
||||
case USB_REQUEST_CLEAR_FEATURE:
|
||||
case USB_REQUEST_SET_FEATURE:
|
||||
UsbSendStall();
|
||||
break;
|
||||
case USB_REQUEST_SET_DESCRIPTOR:
|
||||
case USB_REQUEST_SYNC_FRAME:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UsbSendPacket(uint8_t *packet, int len)
|
||||
{
|
||||
int i, thisTime;
|
||||
|
||||
while(len > 0) {
|
||||
thisTime = min(len, 8);
|
||||
|
||||
for(i = 0; i < thisTime; i++) {
|
||||
AT91C_BASE_UDP->UDP_FDR[2] = packet[i];
|
||||
}
|
||||
AT91C_BASE_UDP->UDP_CSR[2] |= AT91C_UDP_TXPKTRDY;
|
||||
|
||||
while(!(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP)) {
|
||||
WDT_HIT();
|
||||
}
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[2] &= ~AT91C_UDP_TXCOMP;
|
||||
|
||||
while(AT91C_BASE_UDP->UDP_CSR[2] & AT91C_UDP_TXCOMP) {
|
||||
WDT_HIT();
|
||||
}
|
||||
|
||||
len -= thisTime;
|
||||
packet += thisTime;
|
||||
}
|
||||
}
|
||||
|
||||
static void HandleRxdData(void)
|
||||
{
|
||||
int i, len;
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0) {
|
||||
len = UDP_CSR_BYTES_RECEIVED(AT91C_BASE_UDP->UDP_CSR[1]);
|
||||
|
||||
for(i = 0; i < len; i++) {
|
||||
UsbBuffer[UsbSoFarCount] = AT91C_BASE_UDP->UDP_FDR[1];
|
||||
UsbSoFarCount++;
|
||||
}
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK0;
|
||||
while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK0) {
|
||||
WDT_HIT();
|
||||
}
|
||||
|
||||
if(UsbSoFarCount >= 64) {
|
||||
UsbPacketReceived(UsbBuffer, UsbSoFarCount);
|
||||
UsbSoFarCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1) {
|
||||
len = UDP_CSR_BYTES_RECEIVED(AT91C_BASE_UDP->UDP_CSR[1]);
|
||||
|
||||
for(i = 0; i < len; i++) {
|
||||
UsbBuffer[UsbSoFarCount] = AT91C_BASE_UDP->UDP_FDR[1];
|
||||
UsbSoFarCount++;
|
||||
}
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[1] &= ~AT91C_UDP_RX_DATA_BK1;
|
||||
while(AT91C_BASE_UDP->UDP_CSR[1] & AT91C_UDP_RX_DATA_BK1) {
|
||||
WDT_HIT();
|
||||
}
|
||||
|
||||
if(UsbSoFarCount >= 64) {
|
||||
UsbPacketReceived(UsbBuffer, UsbSoFarCount);
|
||||
UsbSoFarCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
}
|
||||
|
||||
void UsbStart(void)
|
||||
{
|
||||
volatile int i;
|
||||
|
||||
UsbSoFarCount = 0;
|
||||
|
||||
USB_D_PLUS_PULLUP_OFF();
|
||||
|
||||
for(i = 0; i < 1000000; i++)
|
||||
;
|
||||
|
||||
USB_D_PLUS_PULLUP_ON();
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {
|
||||
AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;
|
||||
}
|
||||
}
|
||||
|
||||
int UsbConnected()
|
||||
{
|
||||
if (AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_CONFG)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
int UsbPoll(int blinkLeds)
|
||||
{
|
||||
int ret = FALSE;
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) {
|
||||
AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES;
|
||||
|
||||
// following a reset we should be ready to receive a setup packet
|
||||
AT91C_BASE_UDP->UDP_RSTEP = 0xf;
|
||||
AT91C_BASE_UDP->UDP_RSTEP = 0;
|
||||
|
||||
AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
|
||||
|
||||
AT91C_BASE_UDP->UDP_CSR[0] = AT91C_UDP_EPTYPE_CTRL | AT91C_UDP_EPEDS;
|
||||
|
||||
CurrentConfiguration = 0;
|
||||
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_ISR & UDP_INTERRUPT_ENDPOINT(0)) {
|
||||
if(AT91C_BASE_UDP->UDP_CSR[0] & AT91C_UDP_RXSETUP) {
|
||||
HandleRxdSetupData();
|
||||
ret = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
if(AT91C_BASE_UDP->UDP_ISR & UDP_INTERRUPT_ENDPOINT(1)) {
|
||||
HandleRxdData();
|
||||
ret = TRUE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
#ifndef _USB_HID_H_
|
||||
#define _USB_HID_H_
|
||||
|
||||
#include <common.h>
|
||||
#include <proxmark3.h>
|
||||
|
||||
//--------------------------------
|
||||
// USB defines
|
||||
|
||||
#define USB_D_PLUS_PULLUP_ON() { \
|
||||
HIGH(GPIO_USB_PU); \
|
||||
AT91C_BASE_PIOA->PIO_OER = GPIO_USB_PU; \
|
||||
}
|
||||
#define USB_D_PLUS_PULLUP_OFF() AT91C_BASE_PIOA->PIO_ODR = GPIO_USB_PU
|
||||
|
||||
//--------------------------------
|
||||
// USB declarations
|
||||
|
||||
void UsbSendPacket(uint8_t *packet, int len);
|
||||
int UsbConnected();
|
||||
int UsbPoll(int blinkLeds);
|
||||
void UsbStart(void);
|
||||
|
||||
// This function is provided by the apps/bootrom, and called from UsbPoll
|
||||
// if data are available.
|
||||
void UsbPacketReceived(uint8_t *packet, int len);
|
||||
|
||||
#endif // _USB_HID_H_
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue