-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbareboxtlv.c
More file actions
183 lines (154 loc) · 3.59 KB
/
bareboxtlv.c
File metadata and controls
183 lines (154 loc) · 3.59 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: 2021 Ahmad Fatoum, Pengutronix
/* bareboxtlv.c - generate a barebox TLV header */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include "compiler.h"
#include <linux/stringify.h>
#define debug(...)
#define printk_once(...)
#include "../include/tlv/format.h"
#include "../crypto/crc32.c"
#include "common.h"
#include "common.c"
static void usage(char *prgname)
{
fprintf(stderr,
"USAGE: %s [FILE]\n"
"Manipulate barebox TLV blobs\n"
"\n"
"options:\n"
" -m TLV magic to use (default: " __stringify(TLV_MAGIC_BAREBOX_V1) " or input magic for -s)\n"
" -s strip input TLV header from output\n"
" (default is adding header to headerless input)\n"
" -@ offset to start reading at\n"
" -v verify CRC\n",
prgname);
}
static inline ssize_t write_full_verbose(int fd, const void *buf, size_t len)
{
ssize_t ret;
ret = write_full(fd, buf, len);
if (ret < 0)
perror("write");
return ret;
}
int main(int argc, char *argv[])
{
u32 magic = 0;
struct tlv_header *inhdr = NULL;
struct tlv_header hdr;
size_t offset = 0, size = 0;
ssize_t ret;
bool strip_header = false, verify = false;
int opt, ecode = EXIT_FAILURE;
int infd = STDIN_FILENO;
const u8 *p;
char *endptr;
void *buf;
u32 crc = ~0;
while((opt = getopt(argc, argv, "svm:@:h")) != -1) {
switch (opt) {
case 'v':
verify = true;
/* fallthrough; */
case 's':
strip_header = true;
break;
case 'm':
magic = strtoul(optarg, NULL, 16);
if (!magic) {
fprintf(stderr, "invalid magic: %s\n", optarg);
return EXIT_FAILURE;
}
break;
case '@':
offset = strtoul(optarg, &endptr, 0);
if (endptr == optarg) {
fprintf(stderr, "invalid offset: %s\n", optarg);
return EXIT_FAILURE;
}
break;
case 'h':
usage(argv[0]);
return 0;
}
}
if (argc - optind > 1) {
usage(argv[0]);
return EXIT_FAILURE;
}
if (argv[optind]) {
infd = open(argv[optind], O_RDONLY);
if (infd < 0) {
perror("open");
return EXIT_FAILURE;
}
lseek(infd, offset, SEEK_SET);
} else if (offset) {
fprintf(stderr, "can't combine -@ with stdin\n");
return EXIT_FAILURE;
}
p = buf = read_fd(infd, &size);
if (!buf) {
perror("read_fd");
goto out;
}
/* validate, but skip, if header given */
if (strip_header) {
inhdr = buf;
if (size < tlv_total_len(inhdr)) {
fprintf(stderr, "short input: got %zu bytes, but header claims %zu\n",
size, tlv_total_len(inhdr));
goto out;
}
if (!magic)
magic = get_unaligned_be32(p);
p += sizeof(struct tlv_header);
size = cpu_to_be32(inhdr->length_tlv);
if (cpu_to_be32(inhdr->length_sig) != 0) {
fprintf(stderr, "Signatures not yet supported\n");
goto out;
}
}
if (!magic)
magic = TLV_MAGIC_BAREBOX_V1;
hdr.magic = cpu_to_be32(magic);
hdr.length_tlv = cpu_to_be32(size);
hdr.length_sig = cpu_to_be32(0);
if (!verify) {
if (!strip_header) {
ret = write_full_verbose(1, &hdr, sizeof(hdr));
if (ret < 0)
goto out;
}
ret = write_full_verbose(1, p, size);
if (ret < 0)
goto out;
}
crc = crc32_be(crc, &hdr, sizeof(hdr));
crc = crc32_be(crc, p, size);
if (verify) {
u32 oldcrc;
oldcrc = get_unaligned_be32(p + size);
if (oldcrc != crc) {
fprintf(stderr, "CRC mismatch: expected %08x, but calculated %08x\n",
oldcrc, crc);
goto out;
}
}
if (!strip_header) {
crc = cpu_to_be32(crc);
ret = write_full_verbose(1, &crc, sizeof(crc));
if (ret < 0)
goto out;
}
ecode = 0;
out:
free(buf);
if (argv[optind])
close(infd);
return ecode;
}