2 * Copyright (C) 2012 Samsung Electronics
4 * See file CREDITS for list of people who contributed to this
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.
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.
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,
32 #define CHECKSUM_OFFSET (14*1024-4)
33 #define BUFSIZE (14*1024)
34 #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
35 | S_IWGRP | S_IROTH | S_IWOTH)
38 * IROM code reads first 14K bytes from boot device.
39 * It then calculates the checksum of 14K-4 bytes and compare with data at
42 * This function takes two filenames:
43 * IN "u-boot-spl.bin" and
44 * OUT "$(BOARD)-spl.bin as filenames.
45 * It reads the "u-boot-spl.bin" in 16K buffer.
46 * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
47 * It writes the buffer to "$(BOARD)-spl.bin" file.
50 int main(int argc, char **argv)
52 unsigned char buffer[BUFSIZE];
54 uint32_t checksum = 0;
60 fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]);
64 ifd = open(argv[1], O_RDONLY);
66 fprintf(stderr, "%s: Can't open %s: %s\n",
67 argv[0], argv[1], strerror(errno));
71 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
73 fprintf(stderr, "%s: Can't open %s: %s\n",
74 argv[0], argv[2], strerror(errno));
79 if (fstat(ifd, &stat)) {
80 fprintf(stderr, "%s: Unable to get size of %s: %s\n",
81 argv[0], argv[1], strerror(errno));
89 count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
91 if (read(ifd, buffer, count) != count) {
92 fprintf(stderr, "%s: Can't read %s: %s\n",
93 argv[0], argv[1], strerror(errno));
101 for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
102 checksum += buffer[i];
104 checksum = cpu_to_le32(checksum);
106 memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
108 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
109 fprintf(stderr, "%s: Can't write %s: %s\n",
110 argv[0], argv[2], strerror(errno));