1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <stdint.h>
27#include <fcntl.h>
28#include <string.h>
29#include <unistd.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include "crcek.h"
33
34extern uint32_t crc32(uint32_t, const unsigned char *, uint);
35
36static uint32_t data[LOADER_SIZE/4 + 3];
37
38static int do_crc(char *path, unsigned version)
39{
40 uint32_t *p;
41 ssize_t size;
42 int fd;
43
44 fd = open(path, O_RDONLY);
45 if (fd == -1) {
46 perror("Error opening file");
47 return EXIT_FAILURE;
48 }
49 p = data + 2;
50 size = read(fd, p, LOADER_SIZE + 4);
51 if (size == -1) {
52 perror("Error reading file");
53 return EXIT_FAILURE;
54 }
55 if (size > LOADER_SIZE) {
56 fprintf(stderr, "File too large\n");
57 return EXIT_FAILURE;
58 }
59 size = (size + 3) & ~3;
60 size += 4;
61 data[0] = size;
62 data[1] = version;
63 data[size/4 + 1] = crc32(0, (unsigned char *)(data + 1), size);
64 close(fd);
65
66 if (write(STDOUT_FILENO, data, size + 4 + 4 ) == -1) {
67 perror("Error writing file");
68 return EXIT_FAILURE;
69 }
70
71 return EXIT_SUCCESS;
72}
73
74int main(int argc, char * const *argv)
75{
76 if (argc == 2) {
77 return do_crc(argv[1], 0);
78 } else if ((argc == 4) && (strcmp(argv[1], "-v") == 0)) {
79 char *endptr, *nptr = argv[2];
80 unsigned ver = strtoul(nptr, &endptr, 0);
81 if (*nptr != '\0' && *endptr == '\0')
82 return do_crc(argv[3], ver);
83 }
84 fprintf(stderr, "Usage: crcit [-v version] <image>\n");
85
86 return EXIT_FAILURE;
87}
88