1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00

loader: add usb

This commit is contained in:
Pavol Rusnak 2017-03-28 13:04:54 +02:00
parent 38b99b306a
commit 435ac7081a
9 changed files with 93 additions and 44 deletions

View File

@ -61,6 +61,7 @@ OBJ_FW += $(addprefix $(BUILD_FW)/, \
extmod/modtrezorui/font_bitmap.o \
trezorhal/common.o \
trezorhal/image.o \
trezorhal/flash.o \
trezorhal/sdcard.o \
trezorhal/stm32_it.o \
trezorhal/stm32_system.o \

View File

@ -52,6 +52,10 @@ OBJ_HAL += $(addprefix $(BUILD_MP)/,\
stmhal/hal/f4/src/stm32f4xx_hal.o \
stmhal/hal/f4/src/stm32f4xx_ll_sdmmc.o \
stmhal/hal/f4/src/stm32f4xx_ll_usb.o \
\
stmhal/usbdev/core/src/usbd_core.o \
stmhal/usbdev/core/src/usbd_ctlreq.o \
stmhal/usbdev/core/src/usbd_ioreq.o \
)
# OBJ micropython/
@ -66,7 +70,10 @@ OBJ_FW += $(addprefix $(BUILD_FW)/, \
extmod/modtrezorui/font_robotomono_regular_20.o \
trezorhal/common.o \
trezorhal/image.o \
trezorhal/flash.o \
trezorhal/touch.o \
trezorhal/usb.o \
trezorhal/usbd_conf.o \
trezorhal/stm32_it.o \
trezorhal/stm32_system.o \
trezorhal/hal/stm32f4xx_hal_sram.o \

View File

@ -5,6 +5,7 @@
#include "common.h"
#include "display.h"
#include "image.h"
#include "flash.h"
#include "sdcard.h"
#include "version.h"
@ -144,9 +145,18 @@ int main(void)
SCB->VTOR = BOOTLOADER_START;
periph_init();
sdcard_init();
if (0 != display_init()) {
__fatal_error("display_init failed");
}
if (0 != flash_init()) {
__fatal_error("flash_init failed");
}
if (0 != sdcard_init()) {
__fatal_error("sdcard_init failed");
}
display_init();
display_clear();
display_backlight(255);

View File

@ -27,31 +27,31 @@ int usb_init_all(void) {
.vendor_id = 0x1209,
.product_id = 0x53C1,
.release_num = 0x0002,
.product_str = (uint8_t *)"product_str",
.manufacturer_str = (uint8_t *)"manufacturer_str",
.serial_number_str = (uint8_t *)"serial_number_str",
.configuration_str = (uint8_t *)"configuration_str",
.interface_str = (uint8_t *)"interface_str",
.manufacturer_str = (const uint8_t *)"manufacturer_str",
.product_str = (const uint8_t *)"product_str",
.serial_number_str = (const uint8_t *)"serial_number_str",
.configuration_str = (const uint8_t *)"configuration_str",
.interface_str = (const uint8_t *)"interface_str",
};
static uint8_t hid_rx_buffer[64];
static const usb_hid_info_t hid_info = {
.iface_num = 0x00,
.ep_in = 0x81,
.ep_out = 0x01,
.ep_in = USB_EP_DIR_IN | 0x01,
.ep_out = USB_EP_DIR_OUT | 0x01,
.subclass = 0,
.protocol = 0,
.rx_buffer = hid_rx_buffer,
.max_packet_len = sizeof(hid_rx_buffer),
.polling_interval = 1,
.report_desc_len = 34,
.report_desc = (uint8_t*)"\x06\x00\xff\x09\x01\xa1\x01\x09\x20\x15\x00\x26\xff\x00\x75\x08\x95\x40\x81\x02\x09\x21\x15\x00\x26\xff\x00\x75\x08\x95\x40\x91\x02\xc0",
.report_desc = (const uint8_t *)"\x06\x00\xff\x09\x01\xa1\x01\x09\x20\x15\x00\x26\xff\x00\x75\x08\x95\x40\x81\x02\x09\x21\x15\x00\x26\xff\x00\x75\x08\x95\x40\x91\x02\xc0",
};
static const usb_vcp_info_t vcp_info = {
.iface_num = 0x01,
.data_iface_num = 0x02,
.ep_cmd = 0x82,
.ep_in = 0x83,
.ep_out = 0x03,
.ep_cmd = USB_EP_DIR_IN | 0x02,
.ep_in = USB_EP_DIR_IN | 0x03,
.ep_out = USB_EP_DIR_OUT | 0x03,
.polling_interval = 1,
.max_cmd_packet_len = 8,
.max_data_packet_len = 64,

View File

@ -5,7 +5,9 @@
#include "common.h"
#include "display.h"
#include "image.h"
#include "flash.h"
#include "touch.h"
#include "usb.h"
#include "version.h"
#define IMAGE_MAGIC 0x465A5254 // TRZF
@ -81,9 +83,42 @@ void check_and_jump(void)
}
}
void mainloop(void)
{
__fatal_error("touch detected - launch aborted");
int usb_init_all(void) {
static const usb_dev_info_t dev_info = {
.vendor_id = 0x1209,
.product_id = 0x53C0,
.release_num = 0x0002,
.manufacturer_str = (const uint8_t *)"manufacturer_str",
.product_str = (const uint8_t *)"product_str",
.serial_number_str = (const uint8_t *)"serial_number_str",
.configuration_str = (const uint8_t *)"configuration_str",
.interface_str = (const uint8_t *)"interface_str",
};
static uint8_t hid_rx_buffer[64];
static const usb_hid_info_t hid_info = {
.iface_num = 0x00,
.ep_in = USB_EP_DIR_IN | 0x01,
.ep_out = USB_EP_DIR_OUT | 0x01,
.subclass = 0,
.protocol = 0,
.rx_buffer = hid_rx_buffer,
.max_packet_len = sizeof(hid_rx_buffer),
.polling_interval = 1,
.report_desc_len = 34,
.report_desc = (const uint8_t *)"\x06\x00\xff\x09\x01\xa1\x01\x09\x20\x15\x00\x26\xff\x00\x75\x08\x95\x40\x81\x02\x09\x21\x15\x00\x26\xff\x00\x75\x08\x95\x40\x91\x02\xc0",
};
if (0 != usb_init(&dev_info)) {
__fatal_error("usb_init failed");
}
if (0 != usb_hid_add(&hid_info)) {
__fatal_error("usb_hid_add failed");
}
if (0 != usb_start()) {
__fatal_error("usb_start failed");
}
return 0;
}
int main(void)
@ -91,9 +126,22 @@ int main(void)
SCB->VTOR = LOADER_START + HEADER_SIZE;
periph_init();
touch_init();
if (0 != display_init()) {
__fatal_error("display_init failed");
}
if (0 != flash_init()) {
__fatal_error("flash_init failed");
}
if (0 != touch_init()) {
__fatal_error("touch_init failed");
}
if (0 != usb_init_all()) {
__fatal_error("usb_init_all failed");
}
display_init();
display_clear();
display_backlight(255);

View File

@ -1,27 +1,10 @@
#include STM32_HAL_H
int flash_init(void) {
// Enable the flash IRQ, which is used to also call our storage IRQ handler
// It needs to go at a higher priority than all those components that rely on
// the flash storage (eg higher than USB MSC).
HAL_NVIC_SetPriority(FLASH_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(FLASH_IRQn);
int flash_init(void)
{
return 0;
}
void FLASH_IRQHandler(void) {
// This calls the real flash IRQ handler, if needed
/*
uint32_t flash_cr = FLASH->CR;
if ((flash_cr & FLASH_IT_EOP) || (flash_cr & FLASH_IT_ERR)) {
HAL_FLASH_IRQHandler();
}
*/
// This call the storage IRQ handler, to check if the flash cache needs flushing
// storage_irq_handler();
}
#define WANTED_WRP (OB_WRP_SECTOR_0 | OB_WRP_SECTOR_1)
#define WANTED_RDP (OB_RDP_LEVEL_2)
#define WANTED_BOR (OB_BOR_LEVEL3)

View File

@ -10,10 +10,6 @@
#define USB_MAX_CONFIG_DESC_SIZE 128
#define USB_MAX_STR_DESC_SIZE 256
#define USB_EP_DIR_OUT 0x00
#define USB_EP_DIR_IN 0x80
#define USB_EP_DIR_MSK 0x80
extern PCD_HandleTypeDef pcd_fs_handle;
static USBD_HandleTypeDef usb_dev_handle;

View File

@ -1,6 +1,10 @@
#ifndef __TREZORHAL_USB_H__
#define __TREZORHAL_USB_H__
#define USB_EP_DIR_OUT 0x00
#define USB_EP_DIR_IN 0x80
#define USB_EP_DIR_MSK 0x80
typedef struct __attribute__((packed)) {
uint8_t bLength;
uint8_t bDescriptorType;
@ -83,8 +87,8 @@ typedef struct {
uint16_t vendor_id;
uint16_t product_id;
uint16_t release_num;
const uint8_t *product_str;
const uint8_t *manufacturer_str;
const uint8_t *product_str;
const uint8_t *serial_number_str;
const uint8_t *configuration_str;
const uint8_t *interface_str;

View File

@ -256,11 +256,11 @@ static int usb_hid_class_setup(USBD_HandleTypeDef *dev, usb_hid_state_t *state,
switch (req->wValue >> 8) {
case USB_DESC_TYPE_HID:
USBD_CtlSendData(dev, (uint8_t*)&state->desc_block->hid, MIN(req->wLength, sizeof(state->desc_block->hid)));
USBD_CtlSendData(dev, (uint8_t *)&state->desc_block->hid, MIN(req->wLength, sizeof(state->desc_block->hid)));
break;
case USB_DESC_TYPE_REPORT:
USBD_CtlSendData(dev, (uint8_t*)state->report_desc, MIN(req->wLength, state->report_desc_len));
USBD_CtlSendData(dev, UNCONST(state->report_desc), MIN(req->wLength, state->report_desc_len));
break;
}
break;