mirror of
https://github.com/tsukumijima/px4_drv.git
synced 2025-07-23 04:03:01 +02:00
fwtool: 出力メッセージの文面の変更とリファクタリング
This commit is contained in:
114
fwtool/fwtool.c
114
fwtool/fwtool.c
@@ -20,7 +20,7 @@
|
||||
|
||||
struct fwinfo {
|
||||
char *desc;
|
||||
long size;
|
||||
unsigned long size;
|
||||
uint32_t crc32;
|
||||
uint8_t align;
|
||||
uint32_t code_ofs;
|
||||
@@ -42,65 +42,80 @@ static const char *name[] = {
|
||||
|
||||
#define NAME_NUM (sizeof(name) / sizeof(name[0]))
|
||||
|
||||
static int load_file(const char *path, char **buf, long *size)
|
||||
static int load_file(const char *path, uint8_t **buf, unsigned long *size)
|
||||
{
|
||||
int ret = -1, fd;
|
||||
FILE *fp;
|
||||
int ret = -1, fd = -1;
|
||||
FILE *fp = NULL;
|
||||
struct stat stbuf;
|
||||
off_t sz;
|
||||
char *b;
|
||||
uint8_t *b;
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
fd = open(path, O_RDONLY | O_BINARY);
|
||||
if (_sopen_s(&fd, path, _O_RDONLY | O_BINARY, _SH_DENYWR, _S_IREAD) || fd == -1) {
|
||||
#else
|
||||
fd = open(path, O_RDONLY);
|
||||
#endif
|
||||
if (fd == -1) {
|
||||
#endif
|
||||
fprintf(stderr, "Couldn't open file '%s' to read.\n", path);
|
||||
goto end;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
fp = _fdopen(fd, "rb");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "_fdopen() failed.\n");
|
||||
#else
|
||||
fp = fdopen(fd, "rb");
|
||||
if (!fp) {
|
||||
fprintf(stderr, "fdopen() failed.\n");
|
||||
close(fd);
|
||||
goto end;
|
||||
#endif
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (fstat(fd, &stbuf) == -1) {
|
||||
fprintf(stderr, "fstat() failed.\n");
|
||||
goto end2;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
sz = stbuf.st_size;
|
||||
if (sz < 0) {
|
||||
fprintf(stderr, "Invalid file size.\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
b = malloc(sz);
|
||||
b = (uint8_t *)malloc(sz);
|
||||
if (b == NULL) {
|
||||
fprintf(stderr, "No enough memory.\n");
|
||||
goto end2;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (fread(b, sz, 1, fp) < 1) {
|
||||
fprintf(stderr, "Incorrect read size.\n");
|
||||
fprintf(stderr, "Failed to read from file '%s'.\n", path);
|
||||
free(b);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
*buf = b;
|
||||
*size = sz;
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
end2:
|
||||
fclose(fp);
|
||||
end:
|
||||
exit:
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
else if (fd != -1)
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
_close(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int load_tsv_file(struct tsv_data **tsv)
|
||||
{
|
||||
int ret = -1;
|
||||
char *buf;
|
||||
long size;
|
||||
uint8_t *buf;
|
||||
unsigned long size;
|
||||
|
||||
ret = load_file("fwinfo.tsv", &buf, &size);
|
||||
if (ret == -1)
|
||||
@@ -139,9 +154,9 @@ static int load_fwinfo(struct tsv_data *tsv, struct fwinfo *fi, int num)
|
||||
|
||||
for (i = 0; i < num; i++) {
|
||||
fi[i].desc = tsv->field[i][name_map[0]];
|
||||
fi[i].size = strtol(tsv->field[i][name_map[1]], NULL, 10);
|
||||
fi[i].size = strtoul(tsv->field[i][name_map[1]], NULL, 10);
|
||||
fi[i].crc32 = strtoul(tsv->field[i][name_map[2]], NULL, 16);
|
||||
fi[i].align = strtoul(tsv->field[i][name_map[3]], NULL, 10);
|
||||
fi[i].align = (uint8_t)strtoul(tsv->field[i][name_map[3]], NULL, 10);
|
||||
fi[i].code_ofs = strtoul(tsv->field[i][name_map[4]], NULL, 16);
|
||||
fi[i].segment_ofs = strtoul(tsv->field[i][name_map[5]], NULL, 16);
|
||||
fi[i].partition_ofs = strtoul(tsv->field[i][name_map[6]], NULL, 16);
|
||||
@@ -151,7 +166,7 @@ static int load_fwinfo(struct tsv_data *tsv, struct fwinfo *fi, int num)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int output_firmware(struct fwinfo *fi, const char *buf, long size, const char *path)
|
||||
static int output_firmware(struct fwinfo *fi, const uint8_t *buf, unsigned long size, const char *path)
|
||||
{
|
||||
uint8_t i, n;
|
||||
uint8_t align;
|
||||
@@ -208,18 +223,22 @@ static int output_firmware(struct fwinfo *fi, const char *buf, long size, const
|
||||
fprintf(stderr, "Firmware CRC32: %08x\n", crc32);
|
||||
|
||||
if (fi->fw_crc32 && crc32 != fi->fw_crc32) {
|
||||
fprintf(stderr, "Incorrect CRC32!\n");
|
||||
fprintf(stderr, "Incorrect CRC32 checksum!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
if (fopen_s(&fp, path, "wb") || !fp) {
|
||||
#else
|
||||
fp = fopen(path, "wb");
|
||||
if (!fp) {
|
||||
#endif
|
||||
fprintf(stderr, "Couldn't open file '%s' to write.\n", path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(&buf[code_ofs], code_len, 1, fp) < 1) {
|
||||
fprintf(stderr, "Incorrect write size.\n");
|
||||
fprintf(stderr, "Failed to write to file '%s'.\n", path);
|
||||
fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
@@ -237,10 +256,10 @@ static void usage()
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret, num, i;
|
||||
struct tsv_data *tsv;
|
||||
struct fwinfo *fi;
|
||||
char *buf;
|
||||
long size;
|
||||
struct tsv_data *tsv = NULL;
|
||||
struct fwinfo *fi = NULL;
|
||||
uint8_t *buf = NULL;
|
||||
unsigned long size;
|
||||
uint32_t crc32;
|
||||
|
||||
fprintf(stderr, "fwtool for px4 drivers\n\n");
|
||||
@@ -250,38 +269,38 @@ int main(int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(stderr, "Driver file: %s\n", argv[1]);
|
||||
fprintf(stderr, "Output file: %s\n\n", argv[2]);
|
||||
fprintf(stderr, "Driver file (in) : %s\n", argv[1]);
|
||||
fprintf(stderr, "Firmware file (out) : %s\n\n", argv[2]);
|
||||
|
||||
ret = load_tsv_file(&tsv);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Failed to load firmware information file.\n");
|
||||
return 1;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
num = tsv->row_num;
|
||||
|
||||
if (!num) {
|
||||
fprintf(stderr, "No rows in 'fwinfo.tsv'.\n");
|
||||
goto end;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
fi = malloc(sizeof(struct fwinfo) * num);
|
||||
fi = (struct fwinfo *)malloc(sizeof(struct fwinfo) * num);
|
||||
if (!fi) {
|
||||
fprintf(stderr, "No enough memory.\n");
|
||||
goto end;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = load_fwinfo(tsv, fi, num);
|
||||
if (ret) {
|
||||
fprintf(stderr, "Failed to load firmware information.\n");
|
||||
goto end2;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = load_file(argv[1], &buf, &size);
|
||||
if (ret == -1) {
|
||||
fprintf(stderr, "Failed to load driver file.\n");
|
||||
goto end2;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
crc32 = crc32_calc(buf, size);
|
||||
@@ -289,17 +308,18 @@ int main(int argc, char *argv[])
|
||||
for (i = 0; i < num; i++) {
|
||||
if (size == fi[i].size && crc32 == fi[i].crc32) {
|
||||
fprintf(stderr, "Driver description: %s\n", fi[i].desc);
|
||||
|
||||
ret = output_firmware(&fi[i], buf, size, argv[2]);
|
||||
if (!ret) {
|
||||
if (!ret)
|
||||
fprintf(stderr, "OK.\n");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i >= num) {
|
||||
fprintf(stderr, "Unknown driver file.\n");
|
||||
goto end3;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
@@ -308,11 +328,13 @@ int main(int argc, char *argv[])
|
||||
|
||||
return 0;
|
||||
|
||||
end3:
|
||||
free(buf);
|
||||
end2:
|
||||
free(fi);
|
||||
end:
|
||||
fail:
|
||||
if (buf)
|
||||
free(buf);
|
||||
|
||||
if (fi)
|
||||
free(fi);
|
||||
|
||||
tsv_free(tsv);
|
||||
|
||||
return 1;
|
||||
|
17
fwtool/tsv.c
17
fwtool/tsv.c
@@ -1,15 +1,16 @@
|
||||
// tsv.c
|
||||
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "tsv.h"
|
||||
|
||||
int load(const char *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool, size_t *str_poolsize)
|
||||
int load(const uint8_t *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool, size_t *str_poolsize)
|
||||
{
|
||||
const char *p = buf;
|
||||
char *pool_p = str_pool;
|
||||
const uint8_t *p = buf;
|
||||
uint8_t *pool_p = (uint8_t *)str_pool;
|
||||
size_t l = buf_len, pool_size = 0, pool_remain = 0;
|
||||
int col_num = 0, row_num = 0;
|
||||
|
||||
@@ -21,7 +22,7 @@ int load(const char *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool,
|
||||
int newline = 0;
|
||||
|
||||
do {
|
||||
const char *pb = p, *pp = NULL;
|
||||
const uint8_t *pb = p, *pp = NULL;
|
||||
|
||||
while(l && *p != '\0') {
|
||||
if (*p == '\t') {
|
||||
@@ -62,9 +63,9 @@ int load(const char *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool,
|
||||
pool_p[sl - 1] = '\0';
|
||||
|
||||
if (!col_num)
|
||||
tsv->name[num] = pool_p;
|
||||
tsv->name[num] = (char *)pool_p;
|
||||
else
|
||||
tsv->field[row_num][num] = pool_p;
|
||||
tsv->field[row_num][num] = (char *)pool_p;
|
||||
|
||||
pool_p += sl;
|
||||
pool_remain -= sl;
|
||||
@@ -73,7 +74,7 @@ int load(const char *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool,
|
||||
pool_size += sl;
|
||||
num++;
|
||||
}
|
||||
} while(!newline && l && *p != '\0');
|
||||
} while (!newline && l && *p != '\0');
|
||||
|
||||
if (num) {
|
||||
if (!col_num) {
|
||||
@@ -99,7 +100,7 @@ int load(const char *buf, size_t buf_len, struct tsv_data *tsv, void *str_pool,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tsv_load(const char *buf, size_t len, struct tsv_data **tsv)
|
||||
int tsv_load(const uint8_t *buf, size_t len, struct tsv_data **tsv)
|
||||
{
|
||||
int ret = 0, i;
|
||||
struct tsv_data tsv_tmp, *tsv_ret;
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct tsv_data {
|
||||
int col_num;
|
||||
int row_num;
|
||||
@@ -9,5 +11,5 @@ struct tsv_data {
|
||||
char ***field;
|
||||
};
|
||||
|
||||
int tsv_load(const char *buf, size_t len, struct tsv_data **tsv);
|
||||
int tsv_load(const uint8_t *buf, size_t len, struct tsv_data **tsv);
|
||||
void tsv_free(struct tsv_data *tsv);
|
||||
|
Reference in New Issue
Block a user