2 * Copyright (C) 2011 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,
30 #define CHECKSUM_OFFSET (14*1024-4)
31 #define BUFSIZE (16*1024)
32 #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
33 | S_IWGRP | S_IROTH | S_IWOTH)
36 * IROM code reads first 14K bytes from boot device.
37 * It then calculates the checksum of 14K-4 bytes and compare with data at
40 * This function takes two filenames:
41 * IN "u-boot-spl.bin" and
42 * OUT "u-boot-mmc-spl.bin" as filenames.
43 * It reads the "u-boot-spl.bin" in 16K buffer.
44 * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
45 * It writes the buffer to "u-boot-mmc-spl.bin" file.
48 int main(int argc, char **argv)
51 unsigned char buffer[BUFSIZE] = {0};
53 unsigned int checksum = 0, count;
56 printf(" %d Wrong number of arguments\n", argc);
60 ifd = open(argv[1], O_RDONLY);
62 fprintf(stderr, "%s: Can't open %s: %s\n",
63 argv[0], argv[1], strerror(errno));
67 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
69 fprintf(stderr, "%s: Can't open %s: %s\n",
70 argv[0], argv[2], strerror(errno));
76 len = lseek(ifd, 0, SEEK_END);
77 lseek(ifd, 0, SEEK_SET);
79 count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
81 if (read(ifd, buffer, count) != count) {
82 fprintf(stderr, "%s: Can't read %s: %s\n",
83 argv[0], argv[1], strerror(errno));
93 for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
94 checksum += buffer[i];
96 memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
98 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
99 fprintf(stderr, "%s: Can't write %s: %s\n",
100 argv[0], argv[2], strerror(errno));