]> git.ipfire.org Git - people/ms/u-boot.git/blame - tools/mkexynosspl.c
Add GPL-2.0+ SPDX-License-Identifier to source files
[people/ms/u-boot.git] / tools / mkexynosspl.c
CommitLineData
81e35203
CK
1/*
2 * Copyright (C) 2012 Samsung Electronics
3 *
1a459660 4 * SPDX-License-Identifier: GPL-2.0+
81e35203
CK
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <fcntl.h>
11#include <errno.h>
12#include <string.h>
13#include <sys/stat.h>
14#include <compiler.h>
15
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)
20/*
21* Requirement:
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
24* 14K-4 offset.
25*
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.
32*/
33
34int main(int argc, char **argv)
35{
36 unsigned char buffer[BUFSIZE];
37 int i, ifd, ofd;
38 uint32_t checksum = 0;
39 off_t len;
40 ssize_t count;
41 struct stat stat;
42
43 if (argc != 3) {
44 fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]);
45 exit(EXIT_FAILURE);
46 }
47
48 ifd = open(argv[1], O_RDONLY);
49 if (ifd < 0) {
50 fprintf(stderr, "%s: Can't open %s: %s\n",
51 argv[0], argv[1], strerror(errno));
52 exit(EXIT_FAILURE);
53 }
54
55 ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
56 if (ifd < 0) {
57 fprintf(stderr, "%s: Can't open %s: %s\n",
58 argv[0], argv[2], strerror(errno));
59 close(ifd);
60 exit(EXIT_FAILURE);
61 }
62
63 if (fstat(ifd, &stat)) {
64 fprintf(stderr, "%s: Unable to get size of %s: %s\n",
65 argv[0], argv[1], strerror(errno));
66 close(ifd);
67 close(ofd);
68 exit(EXIT_FAILURE);
69 }
70
71 len = stat.st_size;
72
73 count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
74
75 if (read(ifd, buffer, count) != count) {
76 fprintf(stderr, "%s: Can't read %s: %s\n",
77 argv[0], argv[1], strerror(errno));
78
79 close(ifd);
80 close(ofd);
81
82 exit(EXIT_FAILURE);
83 }
84
85 for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
86 checksum += buffer[i];
87
88 checksum = cpu_to_le32(checksum);
89
90 memcpy(&buffer[CHECKSUM_OFFSET], &checksum, sizeof(checksum));
91
92 if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
93 fprintf(stderr, "%s: Can't write %s: %s\n",
94 argv[0], argv[2], strerror(errno));
95
96 close(ifd);
97 close(ofd);
98
99 exit(EXIT_FAILURE);
100 }
101
102 close(ifd);
103 close(ofd);
104
105 return EXIT_SUCCESS;
106}