Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/delta.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <string.h>
#include <delta.h>


#define ESC 0x7f


Expand Down Expand Up @@ -174,17 +173,30 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h> /* INT_MAX */
#include <inttypes.h> /* PRIu32 */

static uint32_t wolfboot_sector_size = 0;

int wb_diff_get_sector_size(void)
{
uint32_t sec_sz = 0;
char *env_sector_size = NULL;
#if defined(_MSC_VER) /* MSVC only, not _WIN32 that includes MSYS2/MinGW */
char* dup = NULL;
size_t len = 0;
if ((_dupenv_s(&dup, &len, "WOLFBOOT_SECTOR_SIZE") == 0) && dup) {
env_sector_size = dup;
}
#else
env_sector_size = getenv("WOLFBOOT_SECTOR_SIZE");
#endif
if (!env_sector_size) {
fprintf(stderr, "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n"
"order to sign a delta update.\n");
#if defined(_MSC_VER)
free(dup);
#endif
exit(6);
} else {
sec_sz = atoi(env_sector_size);
Expand All @@ -193,11 +205,28 @@ int wb_diff_get_sector_size(void)
sec_sz = strtol(env_sector_size, NULL, 16);
if (errno != 0) {
fprintf(stderr, "Invalid WOLFBOOT_SECTOR_SIZE value\n");
#if defined(_MSC_VER)
free(dup);
#endif
exit(6);
}
}
}
return sec_sz;

#if defined(_MSC_VER)
free(dup);
#endif

if (sec_sz == 0) {
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE cannot be 0\n");
exit(6);
}
if (sec_sz > (uint32_t)INT_MAX) {
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n",
sec_sz, INT_MAX);
exit(6);
}
return (int)sec_sz;
}

int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_b, uint32_t len_b)
Expand Down
83 changes: 60 additions & 23 deletions tools/bin-assemble/bin-assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,25 @@ int binentry_address_compare(const void* a, const void* b)
}
}

#ifdef _WIN32
/* 'strerror' has been explicitly marked deprecated */
static const char* win_strerror(int err, char* buf, size_t bufsz) {
if (strerror_s(buf, bufsz, err) != 0) {
buf[0] = '\0';
}
return buf;
}
#endif

int main(int argc, const char* argv[]) {
#ifdef _WIN32
char errbuf[128] = { 0 };
errno_t fe;
#endif
const char* outname = NULL;
FILE* fo = NULL;
FILE* fi = NULL;

size_t i = 0;
size_t num_entries = 0;
binentry_t* entries = NULL;
Expand All @@ -98,43 +115,48 @@ int main(int argc, const char* argv[]) {
return EXIT_FAILURE;
}

for (i=0; i<num_entries; i++) {
for (i = 0; i < num_entries; i++) {
char* endptr = NULL;
struct stat st;

entries[i].address = strtol(argv[2*i + 2], &endptr, 0);
entries[i].address = strtol(argv[2 * i + 2], &endptr, 0);
if (*endptr) {
fprintf(stderr,
"Remaining characters in address field %s\n", endptr);
"Remaining characters in address field %s\n", endptr);
}
entries[i].fname = argv[2*i + 3];
entries[i].fname = argv[2 * i + 3];

if (stat(entries[i].fname, &st)) {
#ifdef _WIN32
fprintf(stderr, "unable to stat %s: %s\n",
entries[i].fname, win_strerror(errno, errbuf, sizeof errbuf));
#else
fprintf(stderr, "unable to stat %s: %s\n", entries[i].fname,
strerror(errno));
strerror(errno));
#endif
return EXIT_FAILURE;
}

entries[i].nbytes = st.st_size;

#if VERBOSE
printf("%s %zu %zu\n",
entries[i].fname,
entries[i].address,
entries[i].nbytes);
entries[i].fname,
entries[i].address,
entries[i].nbytes);
#endif
}

qsort(entries, num_entries, sizeof(binentry_t), binentry_address_compare);

// check for overlap
for (i=1; i<num_entries; i++) {
size_t endaddr = entries[i-1].address + entries[i-1].nbytes;
for (i = 1; i < num_entries; i++) {
size_t endaddr = entries[i - 1].address + entries[i - 1].nbytes;
if (endaddr > entries[i].address) {
fprintf(stderr,
"overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n",
entries[i-1].fname, endaddr,
entries[i].fname, entries[i].address);
"overlap with %s(end address 0x%zx) and %s (start address 0x%zx \n",
entries[i - 1].fname, endaddr,
entries[i].fname, entries[i].address);
err = 1;
}
if (err) {
Expand All @@ -144,23 +166,39 @@ int main(int argc, const char* argv[]) {
}

// TODO: consider handling stdout "-"

FILE* fo = fopen(outname, "wb");
if (fo == NULL) {
#ifdef _WIN32
fe = fopen_s(&fo, outname, "wb");
if (fo == NULL || fe != 0) {
fprintf(stderr, "opening %s failed %s\n",
outname, strerror(errno));
outname, win_strerror((int)fe, errbuf, sizeof errbuf));
return EXIT_FAILURE;
}
#else
fo = fopen(outname, "wb");
if (fo == NULL) {
fprintf(stderr, "opening %s failed %s\n", outname, strerror(errno));
return EXIT_FAILURE;
}
#endif

cur_add = entries[0].address;
for (i=0; i<num_entries; i++) {
size_t fillSz = entries[i].address - cur_add;
FILE* fi = fopen(entries[i].fname, "rb");
if (fi == NULL){
#ifdef _WIN32
fe = fopen_s(&fi, entries[i].fname, "rb");
if (fi == NULL || fe != 0) {
fprintf(stderr, "opening %s failed %s\n",
entries[i].fname, win_strerror((int)fe, errbuf, sizeof errbuf));
return EXIT_FAILURE;
}
#else
fi = fopen(entries[i].fname, "rb");
if (fi == NULL) {
fprintf(stderr, "opening %s failed %s\n",
entries[i].fname, strerror(errno));
entries[i].fname, strerror(errno));
return EXIT_FAILURE;
}
#endif

if (fillSz > 0 && fillSz < INT_MAX) {
/* fill until address - blocks then bytes */
Expand Down Expand Up @@ -194,7 +232,7 @@ int main(int argc, const char* argv[]) {
cur_add += nw;
if (nr != nw) {
fprintf(stderr,
"Failed to wrote %zu bytes of the %zu bytes read from %s\n",
"Failed to write %zu bytes of the %zu bytes read from %s\n",
nw, nr, entries[i].fname);
return EXIT_FAILURE;
}
Expand All @@ -209,10 +247,9 @@ int main(int argc, const char* argv[]) {
if (ferror(fo)) {
fprintf(stderr,
"error writing to %s\n",
entries[i].fname);
outname);
return EXIT_FAILURE;
}

}

fprintf(stderr, "\tAdded %12zu bytes at 0x%08zx from %s\n",
Expand Down