2 * Copyright (C) 2011 Samsung Electronics
4 * SPDX-License-Identifier: GPL-2.0+
15 #define CHECKSUM_OFFSET (14*1024-4)
16 #define BUFSIZE (16*1024)
17 #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
18 | S_IWGRP | S_IROTH | S_IWOTH)
21 * IROM code reads first 14K bytes from boot device.
22 * It then calculates the checksum of 14K-4 bytes and compare with data at
25 * This function takes two filenames:
26 * IN "u-boot-spl.bin" and
27 * OUT "u-boot-mmc-spl.bin" as filenames.
28 * It reads the "u-boot-spl.bin" in 16K buffer.
29 * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
30 * It writes the buffer to "u-boot-mmc-spl.bin" file.
33 int main(int argc, char **argv)
36 unsigned char buffer[BUFSIZE] = {0};
38 unsigned int checksum = 0, count;
41 printf(" %d Wrong number of arguments\n", argc);
45 ifd = open(argv[1], O_RDONLY);
47 fprintf(stderr, "%s: Can't open %s: %s\n",
48 argv[0], argv[1], strerror(errno));
52 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
54 fprintf(stderr, "%s: Can't open %s: %s\n",
55 argv[0], argv[2], strerror(errno));
61 len = lseek(ifd, 0, SEEK_END);
62 lseek(ifd, 0, SEEK_SET);
64 count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
66 if (read(ifd, buffer, count) != count) {
67 fprintf(stderr, "%s: Can't read %s: %s\n",
68 argv[0], argv[1], strerror(errno));
78 for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
79 checksum += buffer[i];
81 memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
83 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
84 fprintf(stderr, "%s: Can't write %s: %s\n",
85 argv[0], argv[2], strerror(errno));