Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2005 |
| 3 | * 2N Telekomunikace, Ladislav Michl <michl@2n.cz> |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 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 | |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 34 | extern uint32_t crc32(uint32_t, const unsigned char *, uint); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 35 | |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 36 | static uint32_t data[LOADER_SIZE/4 + 3]; |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 37 | |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 38 | static int do_crc(char *path, unsigned version) |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 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 | } |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 59 | size = (size + 3) & ~3; /* round up to 4 bytes */ |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 60 | data[0] = size + 4; /* add size of version field */ |
| 61 | data[1] = version; |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 62 | data[2 + (size >> 2)] = crc32(0, (unsigned char *)(data + 1), data[0]); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 63 | close(fd); |
| 64 | |
| 65 | if (write(STDOUT_FILENO, data, size + 3*4) == -1) { |
| 66 | perror("Error writing file"); |
| 67 | return EXIT_FAILURE; |
| 68 | } |
| 69 | |
| 70 | return EXIT_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | int main(int argc, char **argv) |
| 74 | { |
| 75 | if (argc == 2) { |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 76 | return do_crc(argv[1], 0); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 77 | } else if ((argc == 4) && (strcmp(argv[1], "-v") == 0)) { |
| 78 | char *endptr, *nptr = argv[2]; |
| 79 | unsigned ver = strtoul(nptr, &endptr, 0); |
Wolfgang Denk | 87a5c73 | 2006-07-21 11:38:33 +0200 | [diff] [blame] | 80 | if (*nptr != '\0' && *endptr == '\0') |
Ladislav Michl | 3f464b0 | 2009-03-16 23:31:20 +0100 | [diff] [blame] | 81 | return do_crc(argv[3], ver); |
Wolfgang Denk | ac7eb8a | 2005-09-14 23:53:32 +0200 | [diff] [blame] | 82 | } |
| 83 | fprintf(stderr, "Usage: crcit [-v version] <image>\n"); |
| 84 | |
| 85 | return EXIT_FAILURE; |
| 86 | } |