some cleanup

This commit is contained in:
Olaf Rempel 2010-12-25 13:08:02 +01:00
parent 7aa4456607
commit 9da00855c1
5 changed files with 70 additions and 50 deletions

View File

@ -25,7 +25,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include "twiboot.h" #include "filedata.h"
#define FILETYPE_UNKNOWN 0 #define FILETYPE_UNKNOWN 0
#define FILETYPE_BINARY 1 #define FILETYPE_BINARY 1

19
linux/filedata.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _FILEDATA_H_
#define _FILEDATA_H_
#include <stdint.h>
struct databuf {
uint32_t size; // allocation size
uint32_t length; // used size
uint8_t data[0];
};
int dbuf_alloc(struct databuf **dbuf, uint32_t size);
void dbuf_free(struct databuf *dbuf);
int file_getsize(const char *filename, uint32_t *size);
int file_read(const char *filename, struct databuf *dbuf);
int file_write(const char *filename, struct databuf *dbuf);
#endif /* _FILEDATA_H_ */

View File

@ -31,10 +31,12 @@
#include <linux/i2c.h> #include <linux/i2c.h>
#include <linux/i2c-dev.h> #include <linux/i2c-dev.h>
#include "filedata.h"
#include "list.h" #include "list.h"
#include "twiboot.h" #include "twb.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
#define READ_BLOCK_SIZE 128 /* bytes in one flash/eeprom read request */ #define READ_BLOCK_SIZE 128 /* bytes in one flash/eeprom read request */
#define WRITE_BLOCK_SIZE 16 /* bytes in one eeprom write request */ #define WRITE_BLOCK_SIZE 16 /* bytes in one eeprom write request */
@ -58,6 +60,28 @@
#define MEMTYPE_EEPROM 0x02 #define MEMTYPE_EEPROM 0x02
#define MEMTYPE_PARAMETERS 0x03 /* only in APP */ #define MEMTYPE_PARAMETERS 0x03 /* only in APP */
struct chipinfo {
uint8_t sig[3];
const char name[16];
};
static struct chipinfo chips[] = {
{ { 0x1E, 0x93, 0x07 }, "AVR Mega 8" },
{ { 0x1E, 0x93, 0x0A }, "AVR Mega 88" },
{ { 0x1E, 0x94, 0x06 }, "AVR Mega 168" },
};
static const char * twb_get_chipname(uint8_t *sig)
{
int i;
for (i = 0; i < ARRAY_SIZE(chips); i++) {
struct chipinfo *chip = &chips[i];
if (chip->sig[0] == sig[0] && chip->sig[1] == sig[1] && chip->sig[2] == sig[2])
return chip->name;
}
return "unknown";
}
static int twb_switch_application(struct twiboot *twb, uint8_t application) static int twb_switch_application(struct twiboot *twb, uint8_t application)
{ {
@ -206,6 +230,8 @@ int twb_open(struct twiboot *twb)
} }
memcpy(twb->signature, chipinfo, sizeof(twb->signature)); memcpy(twb->signature, chipinfo, sizeof(twb->signature));
twb->chipname = twb_get_chipname(twb->signature);
twb->pagesize = chipinfo[3]; twb->pagesize = chipinfo[3];
twb->flashsize = (chipinfo[4] << 8) + chipinfo[5]; twb->flashsize = (chipinfo[4] << 8) + chipinfo[5];
twb->eepromsize = (chipinfo[6] << 8) + chipinfo[7]; twb->eepromsize = (chipinfo[6] << 8) + chipinfo[7];

View File

@ -1,21 +1,8 @@
#ifndef _TWIBOOT_H_ #ifndef _TWB_H_
#define _TWIBOOT_H_ #define _TWB_H_
#include <stdint.h> #include <stdint.h>
struct databuf {
uint32_t size; // allocation size
uint32_t length; // used size
uint8_t data[0];
};
int dbuf_alloc(struct databuf **dbuf, uint32_t size);
void dbuf_free(struct databuf *dbuf);
int file_getsize(const char *filename, uint32_t *size);
int file_read(const char *filename, struct databuf *dbuf);
int file_write(const char *filename, struct databuf *dbuf);
struct twiboot { struct twiboot {
char *device; char *device;
uint8_t address; uint8_t address;
@ -24,6 +11,8 @@ struct twiboot {
char version[16]; char version[16];
uint8_t signature[3]; uint8_t signature[3];
const char *chipname;
uint8_t pagesize; uint8_t pagesize;
uint16_t flashsize; uint16_t flashsize;
uint16_t eepromsize; uint16_t eepromsize;
@ -35,9 +24,6 @@ struct twiboot {
int twb_open(struct twiboot *twb); int twb_open(struct twiboot *twb);
int twb_close(struct twiboot *twb); int twb_close(struct twiboot *twb);
#define DATATYPE_FLASH 0x01
#define DATATYPE_EEPROM 0x02
int twb_read(struct twiboot *twb, struct databuf *dbuf, int memtype); int twb_read(struct twiboot *twb, struct databuf *dbuf, int memtype);
int twb_verify(struct twiboot *twb, struct databuf *dbuf, int memtype); int twb_verify(struct twiboot *twb, struct databuf *dbuf, int memtype);
int twb_write(struct twiboot *twb, struct databuf *dbuf, int memtype); int twb_write(struct twiboot *twb, struct databuf *dbuf, int memtype);

View File

@ -23,11 +23,14 @@
#include <getopt.h> #include <getopt.h>
#include "filedata.h"
#include "list.h" #include "list.h"
#include "twiboot.h" #include "twb.h"
#define OP_READ 0x01 #define OP_MODE_READ 0x01
#define OP_WRITE 0x02 #define OP_MODE_WRITE 0x02
#define OP_TYPE_FLASH 0x01
#define OP_TYPE_EEPROM 0x02
struct operation { struct operation {
struct list_head list; struct list_head list;
@ -61,11 +64,11 @@ static struct operation * alloc_operation(const char *arg)
} }
if (strncmp(arg, "flash:", 6) == 0) { if (strncmp(arg, "flash:", 6) == 0) {
op->memtype = DATATYPE_FLASH; op->memtype = OP_TYPE_FLASH;
op->filename = strdup(arg + 6); op->filename = strdup(arg + 6);
} else if (strncmp(arg, "eeprom:", 7) == 0) { } else if (strncmp(arg, "eeprom:", 7) == 0) {
op->memtype = DATATYPE_EEPROM; op->memtype = OP_TYPE_EEPROM;
op->filename = strdup(arg + 7); op->filename = strdup(arg + 7);
} else { } else {
@ -76,20 +79,6 @@ static struct operation * alloc_operation(const char *arg)
return op; return op;
} }
static char * check_signature(uint8_t *sig)
{
if (sig[0] == 0x1E && sig[1] == 0x93 && sig[2] == 0x07)
return "AVR Mega 8";
if (sig[0] == 0x1E && sig[1] == 0x93 && sig[2] == 0x0A)
return "AVR Mega 88";
if (sig[0] == 0x1E && sig[1] == 0x94 && sig[2] == 0x06)
return "AVR Mega 168";
return "unknown";
}
static void progress_cb(const char *msg, int pos, int size) static void progress_cb(const char *msg, int pos, int size)
{ {
if (pos != -1 && size != -1) { if (pos != -1 && size != -1) {
@ -149,7 +138,7 @@ int main(int argc, char *argv[])
{ {
struct operation *op = alloc_operation(optarg); struct operation *op = alloc_operation(optarg);
if (op != NULL) { if (op != NULL) {
op->mode = OP_READ; op->mode = OP_MODE_READ;
list_add_tail(&op->list, &operation_list); list_add_tail(&op->list, &operation_list);
} else { } else {
@ -162,7 +151,7 @@ int main(int argc, char *argv[])
{ {
struct operation *op = alloc_operation(optarg); struct operation *op = alloc_operation(optarg);
if (op != NULL) { if (op != NULL) {
op->mode = OP_WRITE; op->mode = OP_MODE_WRITE;
list_add_tail(&op->list, &operation_list); list_add_tail(&op->list, &operation_list);
} else { } else {
@ -219,7 +208,7 @@ int main(int argc, char *argv[])
if (!abort) { if (!abort) {
printf("device : %-16s (address: 0x%02x)\n", twb.device, twb.address); printf("device : %-16s (address: 0x%02x)\n", twb.device, twb.address);
printf("version : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n", twb.version, twb.signature[0], twb.signature[1], twb.signature[2], check_signature(twb.signature)); printf("version : %-16s (sig: 0x%02x 0x%02x 0x%02x => %s)\n", twb.version, twb.signature[0], twb.signature[1], twb.signature[2], twb.chipname);
printf("flash size : 0x%04x (0x%02x bytes/page)\n", twb.flashsize, twb.pagesize); printf("flash size : 0x%04x (0x%02x bytes/page)\n", twb.flashsize, twb.pagesize);
printf("eeprom size : 0x%04x\n", twb.eepromsize); printf("eeprom size : 0x%04x\n", twb.eepromsize);
@ -231,14 +220,14 @@ int main(int argc, char *argv[])
struct operation *op; struct operation *op;
list_for_each_entry(op, &operation_list, list) { list_for_each_entry(op, &operation_list, list) {
abort = 1; abort = 1;
if (op->mode == OP_READ) { if (op->mode == OP_MODE_READ) {
struct databuf *dbuf; struct databuf *dbuf;
int result; int result;
if (op->memtype == DATATYPE_FLASH) { if (op->memtype == OP_TYPE_FLASH) {
twb.progress_msg = "reading flash"; twb.progress_msg = "reading flash";
result = dbuf_alloc(&dbuf, twb.flashsize); result = dbuf_alloc(&dbuf, twb.flashsize);
} else if (op->memtype == DATATYPE_EEPROM) { } else if (op->memtype == OP_TYPE_EEPROM) {
twb.progress_msg = "reading eeprom"; twb.progress_msg = "reading eeprom";
result = dbuf_alloc(&dbuf, twb.eepromsize); result = dbuf_alloc(&dbuf, twb.eepromsize);
} }
@ -262,7 +251,7 @@ int main(int argc, char *argv[])
dbuf_free(dbuf); dbuf_free(dbuf);
} else if (op->mode == OP_WRITE) { } else if (op->mode == OP_MODE_WRITE) {
struct databuf *dbuf; struct databuf *dbuf;
unsigned int size; unsigned int size;
int result; int result;
@ -282,7 +271,7 @@ int main(int argc, char *argv[])
break; break;
} }
if (op->memtype == DATATYPE_FLASH) { if (op->memtype == OP_TYPE_FLASH) {
twb.progress_msg = "writing flash"; twb.progress_msg = "writing flash";
if (dbuf->length > twb.flashsize) { if (dbuf->length > twb.flashsize) {
@ -291,7 +280,7 @@ int main(int argc, char *argv[])
break; break;
} }
} else if (op->memtype == DATATYPE_EEPROM) { } else if (op->memtype == OP_TYPE_EEPROM) {
twb.progress_msg = "writing eeprom"; twb.progress_msg = "writing eeprom";
if (dbuf->length > twb.eepromsize) { if (dbuf->length > twb.eepromsize) {
@ -309,9 +298,9 @@ int main(int argc, char *argv[])
} }
if (verify) { if (verify) {
if (op->memtype == DATATYPE_FLASH) { if (op->memtype == OP_TYPE_FLASH) {
twb.progress_msg = "verifing flash"; twb.progress_msg = "verifing flash";
} else if (op->memtype == DATATYPE_EEPROM) { } else if (op->memtype == OP_TYPE_EEPROM) {
twb.progress_msg = "verifing eeprom"; twb.progress_msg = "verifing eeprom";
} }