1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2020 Andreas Boehler <dev@aboehler.at>
5 * This tool was based on:
6 * firmware-crc.pl by Atheros Communications
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
18 #include <getopt.h> /* for getopt() */
29 #define CHUNK_SIZE 256
31 uint32_t crc32_for_byte(uint32_t r
)
33 for (int j
= 0; j
< 8; ++j
)
34 r
= (r
& 1 ? 0 : (uint32_t)0xEDB88320L
) ^ r
>> 1;
35 return r
^ (uint32_t)0xFF000000L
;
38 void crc32(const void *data
, size_t n_bytes
, uint32_t *crc
)
40 static uint32_t table
[0x100];
43 for (size_t i
= 0; i
< 0x100; ++i
)
44 table
[i
] = crc32_for_byte(i
);
45 for (size_t i
= 0; i
< n_bytes
; ++i
)
46 *crc
= table
[(uint8_t)*crc
^ ((uint8_t *)data
)[i
]] ^ *crc
>> 8;
49 static void usage(int status
)
51 fprintf(stderr
, "Usage: %s [OPTIONS...]\n", progname
);
55 " -i input file name\n"
56 " -o output file name\n"
57 " -m model (3390, x490 for 3490/5490/7490)\n"
58 " -h show this screen\n"
64 int main(int argc
, char *argv
[])
69 uint32_t buf
[CHUNK_SIZE
];
77 c
= getopt(argc
, argv
, "i:o:m:h");
89 if (strcmp(optarg
, "3390") == 0)
91 else if (strcmp(optarg
, "x490") == 0)
104 if (!infile
|| !outfile
)
107 in_fp
= fopen(infile
, "r");
109 fprintf(stderr
, "Error opening input file: %s\n", infile
);
112 out_fp
= fopen(outfile
, "w");
114 fprintf(stderr
, "Error opening output file: %s\n", outfile
);
119 while (!feof(in_fp
)) {
122 read
= fread(buf
, sizeof(uint32_t), CHUNK_SIZE
, in_fp
);
124 fprintf(stderr
, "Error reading input file: %s\n", infile
);
129 for (int i
= 0; i
< read
; i
++)
131 fwrite(buf
, sizeof(uint32_t), read
, out_fp
);
132 if (ferror(out_fp
)) {
133 fprintf(stderr
, "Error writing output file: %s\n", outfile
);
140 read
= fread(buf
, 1, sizeof(uint32_t) * CHUNK_SIZE
, in_fp
);
142 fprintf(stderr
, "Error reading input file: %s\n", infile
);
147 crc32(buf
, read
, &crc
);
148 fwrite(buf
, 1, read
, out_fp
);
149 if (ferror(out_fp
)) {
150 fprintf(stderr
, "Error writing output file: %s\n", outfile
);
158 if (model
== MODEL_X490
)
160 fwrite(&crc
, sizeof(uint32_t), 1, out_fp
);
161 if (ferror(out_fp
)) {
162 fprintf(stderr
, "Error writing checksum to output file: %s\n", outfile
);