lxlfw: move code opening LXL to helper function
[project/firmware-utils.git] / src / lxlfw.c
1 // SPDX-License-Identifier: GPL-2.0-or-later OR MIT
2 /*
3 * Luxul's firmware container format
4 *
5 * Copyright 2020 Legrand AV Inc.
6 */
7
8 #define _GNU_SOURCE
9
10 #include <byteswap.h>
11 #include <endian.h>
12 #include <errno.h>
13 #include <libgen.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #if __BYTE_ORDER == __BIG_ENDIAN
22 #define cpu_to_le32(x) bswap_32(x)
23 #define cpu_to_le16(x) bswap_16(x)
24 #define le32_to_cpu(x) bswap_32(x)
25 #define le16_to_cpu(x) bswap_16(x)
26 #elif __BYTE_ORDER == __LITTLE_ENDIAN
27 #define cpu_to_le32(x) (x)
28 #define cpu_to_le16(x) (x)
29 #define le32_to_cpu(x) (x)
30 #define le16_to_cpu(x) (x)
31 #endif
32
33 #define min(a, b) \
34 ({ \
35 __typeof__ (a) _a = (a); \
36 __typeof__ (b) _b = (b); \
37 _a < _b ? _a : _b; \
38 })
39
40 #define max(a, b) \
41 ({ \
42 __typeof__ (a) _a = (a); \
43 __typeof__ (b) _b = (b); \
44 _a > _b ? _a : _b; \
45 })
46
47 #define MAX_SUPPORTED_VERSION 2
48
49 #define LXL_FLAGS_VENDOR_LUXUL 0x00000001
50
51 struct lxl_hdr {
52 char magic[4]; /* "LXL#" */
53 uint32_t version;
54 uint32_t hdr_len;
55 uint8_t v0_end[0];
56 /* Version: 1+ */
57 uint32_t flags;
58 char board[16];
59 uint8_t v1_end[0];
60 /* Version: 2+ */
61 uint8_t release[4];
62 uint8_t v2_end[0];
63 } __packed;
64
65 /**************************************************
66 * Helpers
67 **************************************************/
68
69 static uint32_t lxlfw_hdr_len(uint32_t version)
70 {
71 switch (version) {
72 case 0:
73 return offsetof(struct lxl_hdr, v0_end);
74 case 1:
75 return offsetof(struct lxl_hdr, v1_end);
76 case 2:
77 return offsetof(struct lxl_hdr, v2_end);
78 default:
79 fprintf(stderr, "Unsupported version %d\n", version);
80 return 0;
81 }
82 }
83
84 /**
85 * lxlfw_open - open Luxul firmware file and validate it
86 *
87 * @pathname: Luxul firmware file
88 * @hdr: struct to read to
89 */
90 static FILE *lxlfw_open(const char *pathname, struct lxl_hdr *hdr)
91 {
92 size_t v0_len = lxlfw_hdr_len(0);
93 size_t min_hdr_len;
94 uint32_t version;
95 size_t bytes;
96 FILE *lxl;
97
98 lxl = fopen(pathname, "r");
99 if (!lxl) {
100 fprintf(stderr, "Could not open \"%s\" file\n", pathname);
101 goto err_out;
102 }
103
104 bytes = fread(hdr, 1, v0_len, lxl);
105 if (bytes != v0_len) {
106 fprintf(stderr, "Input file too small to use Luxul format\n");
107 goto err_close;
108 }
109
110 if (memcmp(hdr->magic, "LXL#", 4)) {
111 fprintf(stderr, "File <file> does not use Luxul's format\n");
112 goto err_close;
113 }
114
115 version = le32_to_cpu(hdr->version);
116
117 min_hdr_len = lxlfw_hdr_len(min(version, MAX_SUPPORTED_VERSION));
118
119 bytes = fread(((uint8_t *)hdr) + v0_len, 1, min_hdr_len - v0_len, lxl);
120 if (bytes != min_hdr_len - v0_len) {
121 fprintf(stderr, "Input file too small for header version %d\n", version);
122 goto err_close;
123 }
124
125 return lxl;
126
127 err_close:
128 fclose(lxl);
129 err_out:
130 return NULL;
131 }
132
133 /**************************************************
134 * Info
135 **************************************************/
136
137 static int lxlfw_info(int argc, char **argv) {
138 struct lxl_hdr hdr;
139 uint32_t version;
140 char board[17];
141 int err = 0;
142 FILE *lxl;
143 int flags;
144
145 if (argc < 3) {
146 fprintf(stderr, "Missing <file> argument\n");
147 err = -EINVAL;
148 goto out;
149 }
150
151 lxl = lxlfw_open(argv[2], &hdr);
152 if (!lxl) {
153 fprintf(stderr, "Could not open \"%s\" Luxul firmware\n", argv[2]);
154 err = -ENOENT;
155 goto out;
156 }
157
158 version = le32_to_cpu(hdr.version);
159
160 printf("Format version:\t%d\n", version);
161 printf("Header length:\t%d\n", le32_to_cpu(hdr.hdr_len));
162 if (version >= 1) {
163 printf("Flags:\t\t");
164 flags = le32_to_cpu(hdr.flags);
165 if (flags & LXL_FLAGS_VENDOR_LUXUL)
166 printf("VENDOR_LUXUL ");
167 printf("\n");
168 memcpy(board, hdr.board, sizeof(hdr.board));
169 board[16] = '\0';
170 printf("Board:\t\t%s\n", board);
171 }
172 if (version >= 2) {
173 printf("Release:\t");
174 if (hdr.release[0] || hdr.release[1] || hdr.release[2] || hdr.release[3]) {
175 printf("%hu.%hu.%hu", hdr.release[0], hdr.release[1], hdr.release[2]);
176 if (hdr.release[3])
177 printf(".%hu", hdr.release[3]);
178 }
179 printf("\n");
180 }
181
182 err_close:
183 fclose(lxl);
184 out:
185 return err;
186 }
187
188 /**************************************************
189 * Create
190 **************************************************/
191
192 static int lxlfw_create(int argc, char **argv) {
193 struct lxl_hdr hdr = {
194 .magic = { 'L', 'X', 'L', '#' },
195 };
196 char *in_path = NULL;
197 uint32_t version = 0;
198 uint32_t hdr_len;
199 ssize_t bytes;
200 char buf[512];
201 int err = 0;
202 FILE *lxl;
203 FILE *in;
204 int c;
205
206 if (argc < 3) {
207 fprintf(stderr, "Missing <file> argument\n");
208 err = -EINVAL;
209 goto out;
210 }
211
212 optind = 3;
213 while ((c = getopt(argc, argv, "i:lb:r:")) != -1) {
214 switch (c) {
215 case 'i':
216 in_path = optarg;
217 break;
218 case 'l':
219 hdr.flags |= cpu_to_le32(LXL_FLAGS_VENDOR_LUXUL);
220 version = max(version, 1);
221 break;
222 case 'b':
223 memcpy(hdr.board, optarg, strlen(optarg) > 16 ? 16 : strlen(optarg));
224 version = max(version, 1);
225 break;
226 case 'r':
227 if (sscanf(optarg, "%hhu.%hhu.%hhu.%hhu", &hdr.release[0], &hdr.release[1], &hdr.release[2], &hdr.release[3]) < 1) {
228 fprintf(stderr, "Failed to parse release number \"%s\"\n", optarg);
229 err = -EINVAL;
230 goto out;
231 }
232 version = max(version, 2);
233 break;
234 }
235 }
236
237 hdr.version = cpu_to_le32(version);
238 hdr_len = lxlfw_hdr_len(version);
239 if (!hdr_len) {
240 err = -EINVAL;
241 goto out;
242 }
243 hdr.hdr_len = cpu_to_le32(hdr_len);
244
245 if (!in_path) {
246 fprintf(stderr, "Missing input file argument\n");
247 err = -EINVAL;
248 goto out;
249 }
250
251 in = fopen(in_path, "r");
252 if (!in) {
253 fprintf(stderr, "Could not open input file %s\n", in_path);
254 err = -EIO;
255 goto out;
256 }
257
258 lxl = fopen(argv[2], "w+");
259 if (!lxl) {
260 fprintf(stderr, "Could not open \"%s\" file\n", argv[2]);
261 err = -EIO;
262 goto err_close_in;
263 }
264
265 bytes = fwrite(&hdr, 1, hdr_len, lxl);
266 if (bytes != hdr_len) {
267 fprintf(stderr, "Could not write Luxul's header\n");
268 err = -EIO;
269 goto err_close_lxl;
270 }
271
272 while ((bytes = fread(buf, 1, sizeof(buf), in)) > 0) {
273 if (fwrite(buf, 1, bytes, lxl) != bytes) {
274 fprintf(stderr, "Could not copy %zu bytes from input file\n", bytes);
275 err = -EIO;
276 goto err_close_lxl;
277 }
278 }
279
280 err_close_lxl:
281 fclose(lxl);
282 err_close_in:
283 fclose(in);
284 out:
285 return err;
286 }
287
288 /**************************************************
289 * Start
290 **************************************************/
291
292 static void usage() {
293 printf("Usage:\n");
294 printf("\n");
295 printf("Get info about Luxul firmware:\n");
296 printf("\tlxlfw info <file>\n");
297 printf("\n");
298 printf("Create new Luxul firmware:\n");
299 printf("\tlxlfw create <file> [options]\n");
300 printf("\t-i file\t\t\t\tinput file for Luxul's firmware container\n");
301 printf("\t-l\t\t\t\tmark firmware as created by Luxul company (DON'T USE)\n");
302 printf("\t-b board\t\t\tboard (device) name\n");
303 printf("\t-r release\t\t\trelease number (e.g. 5.1.0, 7.1.0.2)\n");
304 }
305
306 int main(int argc, char **argv) {
307 if (argc > 1) {
308 if (!strcmp(argv[1], "info"))
309 return lxlfw_info(argc, argv);
310 else if (!strcmp(argv[1], "create"))
311 return lxlfw_create(argc, argv);
312 }
313
314 usage();
315 return 0;
316 }