8c113d6d797c61d53113a46c9d1098ce7cf8c758
[project/firmware-utils.git] / src / zytrx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * zytrx - add header to images for ZyXEL NR7101
4 *
5 * Based on add_header.c - partially based on OpenWrt's
6 * motorola-bin.c
7 *
8 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
9 * Gabor Juhos <juhosg@openwrt.org>
10 * Copyright (C) 2021 Bjørn Mork <bjorn@mork.no>
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stddef.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <netinet/in.h>
23 #include <inttypes.h>
24
25 #define BPB 8 /* bits/byte */
26
27 static uint32_t crc32[1<<BPB];
28
29 static void init_crc32(void)
30 {
31 const uint32_t poly = ntohl(0x2083b8ed);
32 int n;
33
34 for (n = 0; n < 1<<BPB; n++) {
35 uint32_t crc = n;
36 int bit;
37
38 for (bit = 0; bit < BPB; bit++)
39 crc = (crc & 1) ? (poly ^ (crc >> 1)) : (crc >> 1);
40 crc32[n] = crc;
41 }
42 }
43
44 static uint32_t crc32buf(const unsigned char *buf, size_t len)
45 {
46 uint32_t crc = 0xFFFFFFFF;
47
48 for (; len; len--, buf++)
49 crc = crc32[(uint8_t)crc ^ *buf] ^ (crc >> BPB);
50 return ~crc;
51 }
52
53 /* HDR0 reversed, to be stored as BE */
54 #define MAGIC 0x30524448 /* HDR0 reversed, to be stored as BE */
55
56 /* All numbers are stored as BE */
57 struct zytrx_t {
58 uint32_t magic;
59 uint32_t len_h; /* Length of this header */
60 uint32_t len_t; /* Total length of file */
61 uint32_t crc32_p; /* Bit inverted 32-bit CRC of image payload */
62 uint8_t verInt[32]; /* String "5.0.0.0\n" zero padded */
63 uint8_t verExt[32]; /* String "\n" zero padded */
64 uint32_t len_p; /* Length of image payload */
65 uint8_t pad1[12]; /* zero padding(?) */
66 uint8_t code[164]; /* string "3 6035 122 0\n" zero padded */
67 uint8_t chipid[8]; /* string "MT7621A" zero padded */
68 uint8_t boardid[16]; /* string "NR7101" zero padded */
69 uint32_t modelid; /* modelid as 4 BCD digits: 0x07010001 */
70 uint8_t pad2[8]; /* zero padding(?) */
71 uint8_t swVersionInt[32]; /* ZyXEL version string: "1.00(ABUV.0)D0" zero padded */
72 uint8_t swVersionExt[32]; /* identical to swVersionInt */
73 uint8_t pad4[4]; /* zero padding(?) */
74 uint32_t kernelChksum; /* no idea how this is computed - reported but not validated */
75 uint8_t pad5[4]; /* zero padding(?) */
76 uint32_t crc32_h; /* Bit inverted 32-bit CRC of this header payload */
77 uint8_t pad6[4]; /* zero padding(?) */
78 };
79
80 /* static?() field values of unknown meaning - maybe ove to board
81 * table when we know the significance
82 */
83 #define VER_INT "5.0.0.0\n"
84 #define VER_EXT "\n"
85 #define CODE "3 6035 122 0\n"
86 #define KERNELCHKSUM 0x12345678
87
88 /* table of supported devices using this header format */
89 static struct board_t {
90 uint8_t chipid[8];
91 uint8_t boardid[16];
92 uint32_t modelid;
93 } boards[] = {
94 { "MT7621A", "NR7101", 0x07010001 },
95 {}
96 };
97
98 static int find_board(struct zytrx_t *h, char *board)
99 {
100 struct board_t *p;
101
102 for (p = boards; p->modelid; p++) {
103 if (strncmp((const char *)p->boardid, board, sizeof(p->boardid)))
104 continue;
105 memcpy(h->chipid, p->chipid, sizeof(h->chipid));
106 memcpy(h->boardid, p->boardid, sizeof(h->boardid));
107 h->modelid = htonl(p->modelid);
108 return 0;
109 }
110 return -1;
111 }
112
113 static void usage(const char *name)
114 {
115 struct board_t *p;
116
117 fprintf(stderr, "Usage:\n");
118 fprintf(stderr, " %s -B <board> -v <versionstr> -i <file> [-o <outputfile>]\n\n", name);
119 fprintf(stderr, "Supported <board> values:\n");
120 for (p = boards; p->modelid; p++)
121 fprintf(stderr, "\t%-12s\n", p->boardid);
122 fprintf(stderr, "\nExample:\n");
123 fprintf(stderr, " %s -B %s -v foobar-1.0 -i my.img -o out.img\n\n", name,
124 boards[0].boardid);
125 exit(EXIT_FAILURE);
126 }
127
128 static void errexit(const char *msg)
129 {
130 fprintf(stderr, "ERR: %s: %s\n", msg, errno ? strerror(errno) : "unknown");
131 exit(EXIT_FAILURE);
132 }
133
134 static void *map_input(const char *name, size_t *len)
135 {
136 struct stat stat;
137 void *mapped;
138 int fd;
139
140 fd = open(name, O_RDONLY);
141 if (fd < 0)
142 return NULL;
143 if (fstat(fd, &stat) < 0) {
144 close(fd);
145 return NULL;
146 }
147 *len = stat.st_size;
148 mapped = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, fd, 0);
149 if (close(fd) < 0) {
150 (void) munmap(mapped, stat.st_size);
151 return NULL;
152 }
153 return mapped;
154 }
155
156 int main(int argc, char **argv)
157 {
158 int c, fdout = STDOUT_FILENO;
159 void *input_file = NULL;
160 size_t file_len, len;
161 uint32_t crc;
162 struct zytrx_t h = {
163 .magic = htonl(MAGIC),
164 .len_h = htonl(sizeof(h)),
165 .verInt = VER_INT,
166 .verExt = VER_EXT,
167 .code = CODE,
168 .kernelChksum = htonl(KERNELCHKSUM),
169 };
170
171 while ((c = getopt(argc, argv, "B:v:i:o:")) != -1) {
172 switch (c) {
173 case 'B':
174 if (find_board(&h, optarg) < 0)
175 errexit("unsupported board");
176 break;
177 case 'v':
178 len = strlen(optarg);
179 if (len > sizeof(h.swVersionInt))
180 errexit("version string too long");
181 memcpy(h.swVersionInt, optarg, len);
182 memcpy(h.swVersionExt, optarg, len);
183 break;
184 case 'i':
185 input_file = map_input(optarg, &file_len);
186 if (!input_file)
187 errexit(optarg);
188 break;
189 case 'o':
190 fdout = open(optarg, O_WRONLY | O_CREAT, 0644);
191 if (fdout < 0)
192 errexit(optarg);
193 break;
194 default:
195 usage(argv[0]);
196 }
197 }
198
199 /* required paremeters */
200 if (!input_file || !h.modelid || !h.swVersionInt[0])
201 usage(argv[0]);
202
203 /* length fields */
204 h.len_t = htonl(sizeof(h) + file_len);
205 h.len_p = htonl(file_len);
206
207 /* crc fields */
208 init_crc32();
209 crc = crc32buf(input_file, file_len);
210 h.crc32_p = htonl(~crc);
211 crc = crc32buf((unsigned char *)&h, sizeof(h));
212 h.crc32_h = htonl(~crc);
213
214 /* dump new image */
215 write(fdout, &h, sizeof(h));
216 write(fdout, input_file, file_len);
217
218 /* close files */
219 munmap(input_file, file_len);
220 if (fdout != STDOUT_FILENO)
221 close(fdout);
222
223 return EXIT_SUCCESS;
224 }