Chander Kashyap | 7fc795f | 2011-05-24 20:02:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * |
| 4 | * See file CREDITS for list of people who contributed to this |
| 5 | * project. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of |
| 10 | * the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 20 | * MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | #include <string.h> |
Dirk Behme | 43ddc7b | 2011-07-11 08:49:18 +0000 | [diff] [blame] | 29 | #include <sys/stat.h> |
Chander Kashyap | 7fc795f | 2011-05-24 20:02:57 +0000 | [diff] [blame] | 30 | |
| 31 | #define CHECKSUM_OFFSET (14*1024-4) |
| 32 | #define BUFSIZE (16*1024) |
| 33 | #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \ |
| 34 | | S_IWGRP | S_IROTH | S_IWOTH) |
| 35 | /* |
| 36 | * Requirement: |
| 37 | * IROM code reads first 14K bytes from boot device. |
| 38 | * It then calculates the checksum of 14K-4 bytes and compare with data at |
| 39 | * 14K-4 offset. |
| 40 | * |
| 41 | * This function takes two filenames: |
| 42 | * IN "u-boot-spl.bin" and |
| 43 | * OUT "u-boot-mmc-spl.bin" as filenames. |
| 44 | * It reads the "u-boot-spl.bin" in 16K buffer. |
| 45 | * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer. |
| 46 | * It writes the buffer to "u-boot-mmc-spl.bin" file. |
| 47 | */ |
| 48 | |
| 49 | int main(int argc, char **argv) |
| 50 | { |
| 51 | int i, len; |
| 52 | unsigned char buffer[BUFSIZE] = {0}; |
| 53 | int ifd, ofd; |
| 54 | unsigned int checksum = 0, count; |
| 55 | |
| 56 | if (argc != 3) { |
| 57 | printf(" %d Wrong number of arguments\n", argc); |
| 58 | exit(EXIT_FAILURE); |
| 59 | } |
| 60 | |
| 61 | ifd = open(argv[1], O_RDONLY); |
| 62 | if (ifd < 0) { |
| 63 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 64 | argv[0], argv[1], strerror(errno)); |
| 65 | exit(EXIT_FAILURE); |
| 66 | } |
| 67 | |
| 68 | ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM); |
| 69 | if (ifd < 0) { |
| 70 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 71 | argv[0], argv[2], strerror(errno)); |
| 72 | if (ifd) |
| 73 | close(ifd); |
| 74 | exit(EXIT_FAILURE); |
| 75 | } |
| 76 | |
| 77 | len = lseek(ifd, 0, SEEK_END); |
| 78 | lseek(ifd, 0, SEEK_SET); |
| 79 | |
| 80 | count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET; |
| 81 | |
| 82 | if (read(ifd, buffer, count) != count) { |
| 83 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 84 | argv[0], argv[1], strerror(errno)); |
| 85 | |
| 86 | if (ifd) |
| 87 | close(ifd); |
| 88 | if (ofd) |
| 89 | close(ofd); |
| 90 | |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | |
| 94 | for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++) |
| 95 | checksum += buffer[i]; |
| 96 | |
| 97 | memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum)); |
| 98 | |
| 99 | if (write(ofd, buffer, BUFSIZE) != BUFSIZE) { |
| 100 | fprintf(stderr, "%s: Can't write %s: %s\n", |
| 101 | argv[0], argv[2], strerror(errno)); |
| 102 | |
| 103 | if (ifd) |
| 104 | close(ifd); |
| 105 | if (ofd) |
| 106 | close(ofd); |
| 107 | |
| 108 | exit(EXIT_FAILURE); |
| 109 | } |
| 110 | |
| 111 | if (ifd) |
| 112 | close(ifd); |
| 113 | if (ofd) |
| 114 | close(ofd); |
| 115 | |
| 116 | return EXIT_SUCCESS; |
| 117 | } |