2 * Copyright (C) 2012 Samsung Electronics
4 * SPDX-License-Identifier: GPL-2.0+
16 #define CHECKSUM_OFFSET (14*1024-4)
17 #define BUFSIZE (14*1024)
18 #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
19 | S_IWGRP | S_IROTH | S_IWOTH)
22 * IROM code reads first 14K bytes from boot device.
23 * It then calculates the checksum of 14K-4 bytes and compare with data at
26 * This function takes two filenames:
27 * IN "u-boot-spl.bin" and
28 * OUT "$(BOARD)-spl.bin as filenames.
29 * It reads the "u-boot-spl.bin" in 16K buffer.
30 * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
31 * It writes the buffer to "$(BOARD)-spl.bin" file.
34 int main(int argc, char **argv)
36 unsigned char buffer[BUFSIZE];
38 uint32_t checksum = 0;
44 fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]);
48 ifd = open(argv[1], O_RDONLY);
50 fprintf(stderr, "%s: Can't open %s: %s\n",
51 argv[0], argv[1], strerror(errno));
55 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
57 fprintf(stderr, "%s: Can't open %s: %s\n",
58 argv[0], argv[2], strerror(errno));
63 if (fstat(ifd, &stat)) {
64 fprintf(stderr, "%s: Unable to get size of %s: %s\n",
65 argv[0], argv[1], strerror(errno));
73 count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
75 if (read(ifd, buffer, count) != count) {
76 fprintf(stderr, "%s: Can't read %s: %s\n",
77 argv[0], argv[1], strerror(errno));
85 for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
86 checksum += buffer[i];
88 checksum = cpu_to_le32(checksum);
90 memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
92 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
93 fprintf(stderr, "%s: Can't write %s: %s\n",
94 argv[0], argv[2], strerror(errno));