2020-03-18 08:24:49 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2020, STMicroelectronics - All Rights Reserved
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dfu.h>
|
|
|
|
#include <g_dnl.h>
|
|
|
|
#include <usb.h>
|
|
|
|
#include <asm/arch/stm32prog.h>
|
|
|
|
#include <asm/arch/sys_proto.h>
|
|
|
|
#include "stm32prog.h"
|
|
|
|
|
|
|
|
static int stm32prog_set_phase(struct stm32prog_data *data, u8 phase,
|
|
|
|
u32 offset)
|
|
|
|
{
|
|
|
|
struct stm32prog_part_t *part;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (phase == data->phase) {
|
|
|
|
data->offset = offset;
|
2020-03-18 08:25:00 +00:00
|
|
|
data->dfu_seq = 0;
|
2020-03-18 08:24:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* found partition for phase */
|
|
|
|
for (i = 0; i < data->part_nb; i++) {
|
|
|
|
part = &data->part_array[i];
|
|
|
|
if (part->id == phase) {
|
|
|
|
data->cur_part = part;
|
|
|
|
data->phase = phase;
|
|
|
|
data->offset = offset;
|
2020-03-18 08:25:00 +00:00
|
|
|
data->dfu_seq = 0;
|
2020-03-18 08:24:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stm32prog_cmd_write(u64 offset, void *buf, long *len)
|
|
|
|
{
|
|
|
|
u8 phase;
|
|
|
|
u32 address;
|
|
|
|
u8 *pt = buf;
|
|
|
|
void (*entry)(void);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (*len < 5) {
|
2020-11-06 18:01:30 +00:00
|
|
|
log_err("size not allowed\n");
|
2020-03-18 08:24:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (offset) {
|
2020-11-06 18:01:30 +00:00
|
|
|
log_err("invalid offset\n");
|
2020-03-18 08:24:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
phase = pt[0];
|
|
|
|
address = (pt[1] << 24) | (pt[2] << 16) | (pt[3] << 8) | pt[4];
|
|
|
|
if (phase == PHASE_RESET) {
|
|
|
|
entry = (void *)address;
|
|
|
|
printf("## Starting application at 0x%x ...\n", address);
|
|
|
|
(*entry)();
|
|
|
|
printf("## Application terminated\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* set phase and offset */
|
|
|
|
ret = stm32prog_set_phase(stm32prog_data, phase, address);
|
|
|
|
if (ret)
|
2020-11-06 18:01:30 +00:00
|
|
|
log_err("failed: %d\n", ret);
|
2020-03-18 08:24:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PHASE_MIN_SIZE 9
|
|
|
|
static int stm32prog_cmd_read(u64 offset, void *buf, long *len)
|
|
|
|
{
|
|
|
|
u32 destination = DEFAULT_ADDRESS; /* destination address */
|
|
|
|
u32 dfu_offset;
|
|
|
|
u8 *pt_buf = buf;
|
|
|
|
int phase;
|
|
|
|
char *err_msg;
|
|
|
|
int length;
|
|
|
|
|
|
|
|
if (*len < PHASE_MIN_SIZE) {
|
2020-11-06 18:01:30 +00:00
|
|
|
log_err("request exceeds allowed area\n");
|
2020-03-18 08:24:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (offset) {
|
|
|
|
*len = 0; /* EOF for second request */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
phase = stm32prog_data->phase;
|
|
|
|
if (phase == PHASE_FLASHLAYOUT)
|
|
|
|
destination = STM32_DDR_BASE;
|
|
|
|
dfu_offset = stm32prog_data->offset;
|
|
|
|
|
|
|
|
/* mandatory header, size = PHASE_MIN_SIZE */
|
|
|
|
*pt_buf++ = (u8)(phase & 0xFF);
|
|
|
|
*pt_buf++ = (u8)(destination);
|
|
|
|
*pt_buf++ = (u8)(destination >> 8);
|
|
|
|
*pt_buf++ = (u8)(destination >> 16);
|
|
|
|
*pt_buf++ = (u8)(destination >> 24);
|
|
|
|
*pt_buf++ = (u8)(dfu_offset);
|
|
|
|
*pt_buf++ = (u8)(dfu_offset >> 8);
|
|
|
|
*pt_buf++ = (u8)(dfu_offset >> 16);
|
|
|
|
*pt_buf++ = (u8)(dfu_offset >> 24);
|
|
|
|
|
|
|
|
if (phase == PHASE_RESET || phase == PHASE_DO_RESET) {
|
|
|
|
err_msg = stm32prog_get_error(stm32prog_data);
|
|
|
|
length = strlen(err_msg);
|
|
|
|
if (length + PHASE_MIN_SIZE > *len)
|
|
|
|
length = *len - PHASE_MIN_SIZE;
|
|
|
|
|
|
|
|
memcpy(pt_buf, err_msg, length);
|
|
|
|
*len = PHASE_MIN_SIZE + length;
|
|
|
|
stm32prog_do_reset(stm32prog_data);
|
|
|
|
} else if (phase == PHASE_FLASHLAYOUT) {
|
|
|
|
*pt_buf++ = stm32prog_data->part_nb ? 1 : 0;
|
|
|
|
*len = PHASE_MIN_SIZE + 1;
|
|
|
|
} else {
|
|
|
|
*len = PHASE_MIN_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stm32prog_write_medium_virt(struct dfu_entity *dfu, u64 offset,
|
|
|
|
void *buf, long *len)
|
|
|
|
{
|
|
|
|
if (dfu->dev_type != DFU_DEV_VIRT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (dfu->data.virt.dev_num) {
|
|
|
|
case PHASE_CMD:
|
|
|
|
return stm32prog_cmd_write(offset, buf, len);
|
2020-03-18 08:24:58 +00:00
|
|
|
|
|
|
|
case PHASE_OTP:
|
|
|
|
return stm32prog_otp_write(stm32prog_data, (u32)offset,
|
|
|
|
buf, len);
|
2020-03-18 08:24:59 +00:00
|
|
|
|
|
|
|
case PHASE_PMIC:
|
|
|
|
return stm32prog_pmic_write(stm32prog_data, (u32)offset,
|
|
|
|
buf, len);
|
2020-03-18 08:24:49 +00:00
|
|
|
}
|
|
|
|
*len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stm32prog_read_medium_virt(struct dfu_entity *dfu, u64 offset,
|
|
|
|
void *buf, long *len)
|
|
|
|
{
|
|
|
|
if (dfu->dev_type != DFU_DEV_VIRT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (dfu->data.virt.dev_num) {
|
|
|
|
case PHASE_CMD:
|
|
|
|
return stm32prog_cmd_read(offset, buf, len);
|
2020-03-18 08:24:58 +00:00
|
|
|
|
|
|
|
case PHASE_OTP:
|
|
|
|
return stm32prog_otp_read(stm32prog_data, (u32)offset,
|
|
|
|
buf, len);
|
2020-03-18 08:24:59 +00:00
|
|
|
|
|
|
|
case PHASE_PMIC:
|
|
|
|
return stm32prog_pmic_read(stm32prog_data, (u32)offset,
|
|
|
|
buf, len);
|
2020-03-18 08:24:49 +00:00
|
|
|
}
|
|
|
|
*len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int stm32prog_get_medium_size_virt(struct dfu_entity *dfu, u64 *size)
|
|
|
|
{
|
|
|
|
if (dfu->dev_type != DFU_DEV_VIRT) {
|
|
|
|
*size = 0;
|
2020-11-06 18:01:30 +00:00
|
|
|
log_debug("%s, invalid dev_type = %d\n",
|
|
|
|
__func__, dfu->dev_type);
|
2020-03-18 08:24:49 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dfu->data.virt.dev_num) {
|
|
|
|
case PHASE_CMD:
|
2021-07-05 07:39:01 +00:00
|
|
|
*size = CMD_SIZE;
|
2020-03-18 08:24:49 +00:00
|
|
|
break;
|
2020-03-18 08:24:58 +00:00
|
|
|
case PHASE_OTP:
|
|
|
|
*size = OTP_SIZE;
|
|
|
|
break;
|
2020-03-18 08:24:59 +00:00
|
|
|
case PHASE_PMIC:
|
|
|
|
*size = PMIC_SIZE;
|
|
|
|
break;
|
2020-03-18 08:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool stm32prog_usb_loop(struct stm32prog_data *data, int dev)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
bool result;
|
|
|
|
/* USB download gadget for STM32 Programmer */
|
|
|
|
char product[128];
|
2020-06-16 16:25:14 +00:00
|
|
|
char name[SOC_NAME_SIZE];
|
2020-03-18 08:24:49 +00:00
|
|
|
|
2020-06-16 16:25:14 +00:00
|
|
|
get_soc_name(name);
|
2020-03-18 08:24:49 +00:00
|
|
|
snprintf(product, sizeof(product),
|
2020-06-16 16:25:14 +00:00
|
|
|
"USB download gadget@Device ID /0x%03X, @Revision ID /0x%04X, @Name /%s,",
|
|
|
|
get_cpu_dev(), get_cpu_rev(), name);
|
2020-03-18 08:24:49 +00:00
|
|
|
g_dnl_set_product(product);
|
|
|
|
|
|
|
|
if (stm32prog_data->phase == PHASE_FLASHLAYOUT) {
|
|
|
|
ret = run_usb_dnl_gadget(dev, "usb_dnl_dfu");
|
2021-05-18 13:12:08 +00:00
|
|
|
if (ret || stm32prog_data->phase != PHASE_FLASHLAYOUT)
|
2020-03-18 08:24:49 +00:00
|
|
|
return ret;
|
|
|
|
/* prepare the second enumeration with the FlashLayout */
|
2021-05-18 13:12:08 +00:00
|
|
|
stm32prog_dfu_init(data);
|
2020-03-18 08:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = run_usb_dnl_gadget(dev, "usb_dnl_dfu");
|
|
|
|
|
|
|
|
result = !!(ret) || (stm32prog_data->phase == PHASE_DO_RESET);
|
|
|
|
|
|
|
|
g_dnl_set_product(NULL);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int g_dnl_get_board_bcd_device_number(int gcnum)
|
|
|
|
{
|
2020-11-06 18:01:30 +00:00
|
|
|
log_debug("%s\n", __func__);
|
2020-03-18 08:24:49 +00:00
|
|
|
return 0x200;
|
|
|
|
}
|