-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuf22bin.c
74 lines (62 loc) · 1.8 KB
/
uf22bin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// libc
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
// microsoft uf2
#include "uf2.h"
// libuf2
#include "libuf2.h"
void uf22bin(FILE *in, int fd_out, off_t start_address) {
UF2_Block uf2;
// read UF2 blocks from input file
while (uf2_read_block(&uf2, in))
{
off_t off;
// adjust output file offset for the first byte to be at start_address
if (uf2.targetAddr < start_address)
uf2_fatal("data from UF2 before the base offset specified on command line");
off = uf2.targetAddr - start_address;
// write data out at the specified offset filling gaps with '\0'
if (pwrite(fd_out, uf2.data, uf2.payloadSize, off) < uf2.payloadSize)
uf2_fatal("writing binary data out");
}
}
void usage(char const *arg0) {
fprintf(stderr, "usage: %s [-a 0x01230000] [-o file.bin] [file.uf2]\n", arg0);
exit(1);
}
int main(int argc, char **argv) {
char const *arg0 = *argv;
FILE *in = stdin;
int out = STDOUT_FILENO;
uint32_t start_address = 0x00000000;
char *p;
arg0 = *argv;
for (int c; (c = getopt(argc, argv, "a:o:")) != -1;) {
switch (c) {
case 'a':
start_address = strtoul(optarg, &p, 0);
if (*p != '\0')
uf2_fatal("invalid start address number format");
break;
case 'o':
if ((out = open(optarg, O_WRONLY | O_CREAT, 0644)) == -1)
uf2_fatal(optarg);
break;
default:
usage(arg0);
}
}
argv += optind;
argc -= optind;
if (argc > 1)
usage(arg0);
if (argc == 1) {
if ((in = fopen(argv[0], "r")) == NULL)
uf2_fatal(argv[0]);
}
uf22bin(in, out, start_address);
return 0;
}