2014-12-26 11:36:32 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 10/2010 by Olaf Rempel *
|
|
|
|
* razzor@kopf-tisch.de *
|
|
|
|
* *
|
|
|
|
* 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 2 of the License, *
|
|
|
|
* *
|
|
|
|
* 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, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/i2c-dev.h>
|
|
|
|
|
|
|
|
#include "chipinfo_avr.h"
|
|
|
|
#include "filedata.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "multiboot.h"
|
|
|
|
#include "optarg.h"
|
|
|
|
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
|
|
|
|
|
|
|
|
#define TWI_DEFAULT_DEVICE "/dev/i2c-0"
|
|
|
|
|
2020-10-17 22:20:05 +02:00
|
|
|
#define READ_BLOCK_SIZE 128 /* bytes in one flash/eeprom read request */
|
|
|
|
#define WRITE_BLOCK_SIZE 16 /* bytes in one eeprom write request */
|
2020-10-17 22:57:35 +02:00
|
|
|
#define WRITE_RETRY_COUNT 50 /* write retries when slave does not not acknowledge */
|
|
|
|
#define WRITE_RETRY_DELAY_US 2000 /* delay in us per retry */
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
/* SLA+R */
|
2020-10-17 22:20:05 +02:00
|
|
|
#define CMD_WAIT 0x00
|
|
|
|
#define CMD_READ_VERSION 0x01
|
|
|
|
#define CMD_READ_MEMORY 0x02
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
/* SLA+W */
|
2020-10-17 22:20:05 +02:00
|
|
|
#define CMD_SWITCH_APPLICATION CMD_READ_VERSION
|
|
|
|
#define CMD_WRITE_MEMORY CMD_READ_MEMORY
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
/* CMD_SWITCH_APPLICATION parameter */
|
2020-10-17 22:20:05 +02:00
|
|
|
#define BOOTTYPE_BOOTLOADER 0x00 /* only in APP */
|
|
|
|
#define BOOTTYPE_APPLICATION 0x80
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
/* CMD_{READ|WRITE}_* parameter */
|
2020-10-17 22:20:05 +02:00
|
|
|
#define MEMTYPE_CHIPINFO 0x00
|
|
|
|
#define MEMTYPE_FLASH 0x01
|
|
|
|
#define MEMTYPE_EEPROM 0x02
|
|
|
|
#define MEMTYPE_PARAMETERS 0x03 /* only in APP */
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
struct multiboot_ops twi_ops;
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
struct twi_privdata
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
char *device;
|
|
|
|
uint8_t address;
|
|
|
|
int fd;
|
|
|
|
int connected;
|
2021-03-02 13:23:16 +01:00
|
|
|
int stay_in_bootloader;
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
uint8_t pagesize;
|
|
|
|
uint16_t flashsize;
|
|
|
|
uint16_t eepromsize;
|
|
|
|
};
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
static struct option twi_optargs[] =
|
|
|
|
{
|
2021-03-02 13:23:16 +01:00
|
|
|
{ "address", 1, 0, 'a' }, /* -a <addr> */
|
|
|
|
{ "device", 1, 0, 'd' }, /* [ -d <device> ] */
|
|
|
|
{ "stay", 0, 0, 's' }, /* [ -s ] */
|
2014-12-26 11:36:32 +01:00
|
|
|
};
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
|
2020-10-17 22:57:35 +02:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_write_retries
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_write_retries(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
int retries = WRITE_RETRY_COUNT;
|
|
|
|
do {
|
|
|
|
result = write(fd, buf, count);
|
|
|
|
if (result >= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* slave device does not acknowledge (eg. not able to clockstretch)
|
|
|
|
* actual error code may be driver specific
|
|
|
|
*/
|
|
|
|
if ((errno == ENXIO) || (errno == EREMOTEIO) || (errno == EIO))
|
|
|
|
{
|
|
|
|
usleep(WRITE_RETRY_DELAY_US);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "twi_write_retries(): %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
retries = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (retries--);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
} /* twi_write_retries */
|
|
|
|
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_switch_application
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_switch_application(struct twi_privdata *twi,
|
|
|
|
uint8_t application)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
uint8_t cmd[] = { CMD_SWITCH_APPLICATION, application };
|
|
|
|
|
2020-10-17 22:57:35 +02:00
|
|
|
return (twi_write_retries(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd));
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_switch_application */
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
/* *************************************************************************
|
|
|
|
* twi_switch_application
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_read_version(struct twi_privdata *twi,
|
|
|
|
char *version, int length)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
uint8_t cmd[] = { CMD_READ_VERSION };
|
|
|
|
|
2020-10-17 22:57:35 +02:00
|
|
|
if (twi_write_retries(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd))
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
memset(version, 0, length);
|
|
|
|
if (read(twi->fd, version, length) != length)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < length; i++)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
version[i] &= ~0x80;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_read_version */
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
/* *************************************************************************
|
|
|
|
* twi_read_memory
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_read_memory(struct twi_privdata *twi,
|
|
|
|
uint8_t *buffer, uint8_t size,
|
|
|
|
uint8_t memtype, uint16_t address)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
uint8_t cmd[] = { CMD_READ_MEMORY, memtype, (address >> 8) & 0xFF, (address & 0xFF) };
|
2017-03-07 20:09:48 +01:00
|
|
|
|
2020-10-17 22:57:35 +02:00
|
|
|
if (twi_write_retries(twi->fd, cmd, sizeof(cmd)) != sizeof(cmd))
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
return (read(twi->fd, buffer, size) != size);
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_read_memory */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_write_memory
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_write_memory(struct twi_privdata *twi,
|
|
|
|
uint8_t *buffer, uint8_t size,
|
|
|
|
uint8_t memtype, uint16_t address)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
int bufsize;
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
if (memtype == MEMTYPE_FLASH)
|
|
|
|
{
|
|
|
|
if ((address & (twi->pagesize -1)) != 0x00)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "twi_write_memory(): address 0x%04x not aligned to pagesize 0x%02x\n",
|
|
|
|
address, twi->pagesize);
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
bufsize = 4 + twi->pagesize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
bufsize = 4 + size;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *cmd = malloc(bufsize);
|
|
|
|
if (cmd == NULL)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
cmd[0] = CMD_WRITE_MEMORY;
|
|
|
|
cmd[1] = memtype;
|
|
|
|
cmd[2] = (address >> 8) & 0xFF;
|
|
|
|
cmd[3] = (address & 0xFF);
|
|
|
|
memcpy(cmd +4, buffer, size);
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
if (memtype == MEMTYPE_FLASH)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
memset(cmd +4 +size, 0xFF, twi->pagesize - size);
|
|
|
|
}
|
|
|
|
|
2020-10-17 22:57:35 +02:00
|
|
|
int result = twi_write_retries(twi->fd, cmd, bufsize);
|
2014-12-26 11:36:32 +01:00
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
return (result != bufsize);
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_write_memory */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_close_device
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static void twi_close_device(struct twi_privdata *twi)
|
|
|
|
{
|
|
|
|
if (twi->connected)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
close(twi->fd);
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
twi->connected = 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_close_device */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_open_device
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static int twi_open_device(struct twi_privdata *twi)
|
|
|
|
{
|
|
|
|
twi->fd = open(twi->device, O_RDWR);
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi->fd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to open '%s': %s\n",
|
|
|
|
twi->device, strerror(errno));
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long funcs;
|
2017-03-07 20:09:48 +01:00
|
|
|
if (ioctl(twi->fd, I2C_FUNCS, &funcs))
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
perror("ioctl(I2C_FUNCS)");
|
|
|
|
close(twi->fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
if (!(funcs & I2C_FUNC_I2C))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "I2C_FUNC_I2C not supported on '%s'!\n",
|
|
|
|
twi->device);
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
close(twi->fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
if (ioctl(twi->fd, I2C_SLAVE, twi->address) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to select slave address '%d': %s\n",
|
|
|
|
twi->address, strerror(errno));
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
close(twi->fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
twi->connected = 1;
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_open_device */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_close
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static int twi_close(struct multiboot *mboot)
|
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
|
2021-03-02 13:23:16 +01:00
|
|
|
if (twi->connected && !twi->stay_in_bootloader)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
twi_switch_application(twi, BOOTTYPE_APPLICATION);
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
twi_close_device(twi);
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_close */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_open
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static int twi_open(struct multiboot *mboot)
|
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi->address == 0)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
fprintf(stderr, "abort: no address given\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi->device == NULL)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
twi->device = strdup(TWI_DEFAULT_DEVICE);
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi->device == NULL)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
perror("strdup()");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (twi_open_device(twi) != 0)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return -1;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (twi_switch_application(twi, BOOTTYPE_BOOTLOADER))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to switch to bootloader (invalid address?): %s\n",
|
|
|
|
strerror(errno));
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
twi_close(mboot);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for watchdog and startup time */
|
|
|
|
usleep(100000);
|
|
|
|
|
2019-11-02 15:27:34 +01:00
|
|
|
char version[16 +1];
|
|
|
|
if (twi_read_version(twi, version, sizeof(version) -1))
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "failed to get bootloader version: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
twi_close(mboot);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-02 15:27:34 +01:00
|
|
|
version[16] = '\0';
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
uint8_t chipinfo[8];
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi_read_memory(twi, chipinfo, sizeof(chipinfo), MEMTYPE_CHIPINFO, 0x0000))
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
fprintf(stderr, "failed to get chipinfo: %s\n", strerror(errno));
|
|
|
|
twi_close(mboot);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *chipname = chipinfo_get_avr_name(chipinfo);
|
|
|
|
|
|
|
|
twi->pagesize = chipinfo[3];
|
|
|
|
twi->flashsize = (chipinfo[4] << 8) + chipinfo[5];
|
|
|
|
twi->eepromsize = (chipinfo[6] << 8) + chipinfo[7];
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
printf("device : %-16s (address: 0x%02X)\n",
|
|
|
|
twi->device, twi->address);
|
|
|
|
|
|
|
|
printf("version : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n",
|
|
|
|
version, chipinfo[0], chipinfo[1], chipinfo[2], chipname);
|
|
|
|
|
|
|
|
printf("flash size : 0x%04x / %5d (0x%02x bytes/page)\n",
|
|
|
|
twi->flashsize, twi->flashsize, twi->pagesize);
|
|
|
|
|
|
|
|
printf("eeprom size : 0x%04x / %5d\n",
|
|
|
|
twi->eepromsize, twi->eepromsize);
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_open */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_read
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_read(struct multiboot *mboot,
|
|
|
|
struct databuf *dbuf,
|
|
|
|
int memtype)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
char *progress_msg = (memtype == MEMTYPE_FLASH) ? "reading flash" : "reading eeprom";
|
|
|
|
|
2019-08-11 11:16:26 +02:00
|
|
|
uint16_t pos = 0;
|
|
|
|
uint16_t size = (memtype == MEMTYPE_FLASH) ? twi->flashsize : twi->eepromsize;
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
while (pos < size)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, pos, size);
|
|
|
|
|
2019-08-11 11:16:26 +02:00
|
|
|
uint8_t len = MIN(READ_BLOCK_SIZE, size - pos);
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
if (twi_read_memory(twi, dbuf->data + pos, len, memtype, pos))
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, -1, -1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbuf->length = pos;
|
|
|
|
|
|
|
|
mboot->progress_cb(progress_msg, pos, size);
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_read */
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
/* *************************************************************************
|
|
|
|
* twi_write
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_write(struct multiboot *mboot,
|
|
|
|
struct databuf *dbuf,
|
|
|
|
int memtype)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
char *progress_msg = (memtype == MEMTYPE_FLASH) ? "writing flash" : "writing eeprom";
|
|
|
|
|
2019-08-11 11:16:26 +02:00
|
|
|
uint16_t pos = 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
while (pos < dbuf->length)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, pos, dbuf->length);
|
|
|
|
|
2019-08-11 11:16:26 +02:00
|
|
|
uint8_t len = (memtype == MEMTYPE_FLASH) ? twi->pagesize : WRITE_BLOCK_SIZE;
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
len = MIN(len, dbuf->length - pos);
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
if (twi_write_memory(twi, dbuf->data + pos, len, memtype, pos))
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, -1, -1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
mboot->progress_cb(progress_msg, pos, dbuf->length);
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_write */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_verify
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static int twi_verify(struct multiboot *mboot, struct databuf *dbuf, int memtype)
|
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
char *progress_msg = (memtype == MEMTYPE_FLASH) ? "verifing flash" : "verifing eeprom";
|
|
|
|
|
2019-08-11 11:16:26 +02:00
|
|
|
uint16_t pos = 0;
|
2014-12-26 11:36:32 +01:00
|
|
|
uint8_t comp[READ_BLOCK_SIZE];
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
while (pos < dbuf->length)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, pos, dbuf->length);
|
|
|
|
|
|
|
|
int len = MIN(READ_BLOCK_SIZE, dbuf->length - pos);
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi_read_memory(twi, comp, len, memtype, pos))
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, -1, -1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
if (memcmp(comp, dbuf->data + pos, len) != 0x00)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
mboot->progress_cb(progress_msg, -1, -1);
|
|
|
|
fprintf(stderr, "verify failed at page 0x%04x!!\n", pos);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbuf->length = pos;
|
|
|
|
|
|
|
|
mboot->progress_cb(progress_msg, pos, dbuf->length);
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_verify */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_optarg_cb
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static int twi_optarg_cb(int val, const char *arg, void *privdata)
|
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)privdata;
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
switch (val)
|
|
|
|
{
|
|
|
|
case 'a': /* address */
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
twi->address = strtol(arg, &endptr, 16);
|
|
|
|
if (*endptr != '\0' || twi->address < 0x01 || twi->address > 0x7F)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "invalid address: '%s'\n", arg);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
}
|
2017-03-07 20:09:48 +01:00
|
|
|
break;
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
case 'd': /* device */
|
|
|
|
if (twi->device != NULL)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
fprintf(stderr, "invalid device: '%s'\n", optarg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
twi->device = strdup(optarg);
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi->device == NULL)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
perror("strdup()");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-07 20:09:48 +01:00
|
|
|
break;
|
|
|
|
|
2021-03-02 13:23:16 +01:00
|
|
|
case 's': /* stay in bootloader */
|
|
|
|
twi->stay_in_bootloader = 1;
|
|
|
|
break;
|
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
case 'h':
|
|
|
|
case '?': /* error */
|
|
|
|
fprintf(stderr, "Usage: twiboot [options]\n"
|
|
|
|
" -a <address> - selects i2c address (0x01 - 0x7F)\n"
|
|
|
|
" -d <device> - selects i2c device (default: /dev/i2c-0)\n"
|
2021-03-02 13:23:16 +01:00
|
|
|
" -s - stay in bootloader afterwards\n"
|
2017-03-07 20:09:48 +01:00
|
|
|
" -r <flash|eeprom>:<file> - reads flash/eeprom to file (.bin | .hex | -)\n"
|
|
|
|
" -w <flash|eeprom>:<file> - write flash/eeprom from file (.bin | .hex)\n"
|
|
|
|
" -n - disable verify after write\n"
|
|
|
|
" -p <0|1|2> - progress bar mode\n"
|
|
|
|
"\n"
|
2021-03-02 13:22:52 +01:00
|
|
|
"Example: twiboot -a 0x22 -w flash:blmc.hex -w eeprom:blmc_eeprom.hex\n"
|
2017-03-07 20:09:48 +01:00
|
|
|
"\n");
|
|
|
|
return -1;
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
default:
|
|
|
|
return 1;
|
2014-12-26 11:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_optarg_cb */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_alloc
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static struct multiboot * twi_alloc(void)
|
|
|
|
{
|
|
|
|
struct multiboot * mboot = malloc(sizeof(struct multiboot));
|
|
|
|
if (mboot == NULL)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return NULL;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
memset(mboot, 0x00, sizeof(struct multiboot));
|
|
|
|
mboot->ops = &twi_ops;
|
|
|
|
|
|
|
|
struct twi_privdata *twi = malloc(sizeof(struct twi_privdata));
|
2017-03-07 20:09:48 +01:00
|
|
|
if (twi == NULL)
|
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
free(mboot);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(twi, 0x00, sizeof(struct twi_privdata));
|
|
|
|
twi->device = NULL;
|
|
|
|
twi->address = 0;
|
|
|
|
|
|
|
|
optarg_register(twi_optargs, ARRAY_SIZE(twi_optargs), twi_optarg_cb, (void *)twi);
|
|
|
|
|
|
|
|
mboot->privdata = twi;
|
|
|
|
return mboot;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_alloc */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_free
|
|
|
|
* ************************************************************************* */
|
2014-12-26 11:36:32 +01:00
|
|
|
static void twi_free(struct multiboot *mboot)
|
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
|
|
|
|
if (twi->device != NULL)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
free(twi->device);
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
free(twi);
|
|
|
|
free(mboot);
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_free */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_get_memtype
|
|
|
|
* ************************************************************************* */
|
|
|
|
static int twi_get_memtype(struct multiboot *mboot,
|
|
|
|
const char *memname)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
2019-08-11 11:16:26 +02:00
|
|
|
/* unused parameter */
|
|
|
|
(void)mboot;
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
if (strcmp(memname, "flash") == 0)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return MEMTYPE_FLASH;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
else if (strcmp(memname, "eeprom") == 0)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return MEMTYPE_EEPROM;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
|
|
|
return -1;
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_get_memtype */
|
|
|
|
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
/* *************************************************************************
|
|
|
|
* twi_get_memsize
|
|
|
|
* ************************************************************************* */
|
2020-10-17 21:29:47 +02:00
|
|
|
static uint32_t twi_get_memsize(struct multiboot *mboot,
|
|
|
|
int memtype)
|
2014-12-26 11:36:32 +01:00
|
|
|
{
|
|
|
|
struct twi_privdata *twi = (struct twi_privdata *)mboot->privdata;
|
|
|
|
|
|
|
|
if (!twi->connected)
|
2017-03-07 20:09:48 +01:00
|
|
|
{
|
2014-12-26 11:36:32 +01:00
|
|
|
return 0;
|
2017-03-07 20:09:48 +01:00
|
|
|
}
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
switch (memtype)
|
|
|
|
{
|
|
|
|
case MEMTYPE_FLASH:
|
|
|
|
return twi->flashsize;
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
case MEMTYPE_EEPROM:
|
|
|
|
return twi->eepromsize;
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
default:
|
|
|
|
return 0;
|
2014-12-26 11:36:32 +01:00
|
|
|
}
|
2017-03-07 20:09:48 +01:00
|
|
|
} /* twi_get_memsize */
|
2014-12-26 11:36:32 +01:00
|
|
|
|
2017-03-07 20:09:48 +01:00
|
|
|
|
|
|
|
struct multiboot_ops twi_ops =
|
|
|
|
{
|
2019-08-11 11:16:26 +02:00
|
|
|
.exec_name = "twiboot",
|
2014-12-26 11:36:32 +01:00
|
|
|
.alloc = twi_alloc,
|
|
|
|
.free = twi_free,
|
|
|
|
.get_memtype = twi_get_memtype,
|
|
|
|
.get_memsize = twi_get_memsize,
|
|
|
|
|
|
|
|
.open = twi_open,
|
|
|
|
.close = twi_close,
|
|
|
|
.read = twi_read,
|
|
|
|
.write = twi_write,
|
|
|
|
.verify = twi_verify,
|
|
|
|
};
|