blob: 378b85d72294099f9e2f6ddb774f27345486f55b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chander Kashyap7fc795f2011-05-24 20:02:57 +00002/*
3 * Copyright (C) 2011 Samsung Electronics
Chander Kashyap7fc795f2011-05-24 20:02:57 +00004 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <unistd.h>
9#include <fcntl.h>
10#include <errno.h>
11#include <string.h>
Dirk Behme43ddc7b2011-07-11 08:49:18 +000012#include <sys/stat.h>
Chander Kashyap7fc795f2011-05-24 20:02:57 +000013
14#define CHECKSUM_OFFSET (14*1024-4)
15#define BUFSIZE (16*1024)
16#define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
17 | S_IWGRP | S_IROTH | S_IWOTH)
18/*
19* Requirement:
20* IROM code reads first 14K bytes from boot device.
21* It then calculates the checksum of 14K-4 bytes and compare with data at
22* 14K-4 offset.
23*
24* This function takes two filenames:
25* IN "u-boot-spl.bin" and
26* OUT "u-boot-mmc-spl.bin" as filenames.
27* It reads the "u-boot-spl.bin" in 16K buffer.
28* It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
29* It writes the buffer to "u-boot-mmc-spl.bin" file.
30*/
31
32int main(int argc, char **argv)
33{
34 int i, len;
35 unsigned char buffer[BUFSIZE] = {0};
36 int ifd, ofd;
37 unsigned int checksum = 0, count;
38
39 if (argc != 3) {
40 printf(" %d Wrong number of arguments\n", argc);
41 exit(EXIT_FAILURE);
42 }
43
44 ifd = open(argv[1], O_RDONLY);
45 if (ifd < 0) {
46 fprintf(stderr, "%s: Can't open %s: %s\n",
47 argv[0], argv[1], strerror(errno));
48 exit(EXIT_FAILURE);
49 }
50
51 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
Thomas Huth7a169ca2015-08-25 17:09:40 +020052 if (ofd < 0) {
Chander Kashyap7fc795f2011-05-24 20:02:57 +000053 fprintf(stderr, "%s: Can't open %s: %s\n",
54 argv[0], argv[2], strerror(errno));
55 if (ifd)
56 close(ifd);
57 exit(EXIT_FAILURE);
58 }
59
60 len = lseek(ifd, 0, SEEK_END);
61 lseek(ifd, 0, SEEK_SET);
62
63 count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
64
65 if (read(ifd, buffer, count) != count) {
66 fprintf(stderr, "%s: Can't read %s: %s\n",
67 argv[0], argv[1], strerror(errno));
68
69 if (ifd)
70 close(ifd);
71 if (ofd)
72 close(ofd);
73
74 exit(EXIT_FAILURE);
75 }
76
77 for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
78 checksum += buffer[i];
79
80 memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
81
82 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
83 fprintf(stderr, "%s: Can't write %s: %s\n",
84 argv[0], argv[2], strerror(errno));
85
86 if (ifd)
87 close(ifd);
88 if (ofd)
89 close(ofd);
90
91 exit(EXIT_FAILURE);
92 }
93
94 if (ifd)
95 close(ifd);
96 if (ofd)
97 close(ofd);
98
99 return EXIT_SUCCESS;
100}