1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2012 Samsung Electronics
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <stdio.h>
8*4882a593Smuzhiyun #include <stdlib.h>
9*4882a593Smuzhiyun #include <unistd.h>
10*4882a593Smuzhiyun #include <fcntl.h>
11*4882a593Smuzhiyun #include <errno.h>
12*4882a593Smuzhiyun #include <string.h>
13*4882a593Smuzhiyun #include <sys/stat.h>
14*4882a593Smuzhiyun #include <compiler.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #define CHECKSUM_OFFSET (14*1024-4)
17*4882a593Smuzhiyun #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
18*4882a593Smuzhiyun | S_IWGRP | S_IROTH | S_IWOTH)
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun * Requirement for the fixed size SPL header:
21*4882a593Smuzhiyun * IROM code reads first (CHECKSUM_OFFSET + 4) bytes from boot device. It then
22*4882a593Smuzhiyun * calculates the checksum of CHECKSUM_OFFSET bytes and compares with data at
23*4882a593Smuzhiyun * CHECKSUM_OFFSET location.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Requirement for the variable size SPL header:
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun * IROM code reads the below header to find out the size of the blob (total
28*4882a593Smuzhiyun * size, header size included) and its checksum. Then it reads the rest of the
29*4882a593Smuzhiyun * blob [i.e size - sizeof(struct var_size_header) bytes], calculates the
30*4882a593Smuzhiyun * checksum and compares it with value read from the header.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun struct var_size_header {
33*4882a593Smuzhiyun uint32_t spl_size;
34*4882a593Smuzhiyun uint32_t spl_checksum;
35*4882a593Smuzhiyun uint32_t reserved[2];
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static const char *prog_name;
39*4882a593Smuzhiyun
write_to_file(int ofd,void * buffer,int size)40*4882a593Smuzhiyun static void write_to_file(int ofd, void *buffer, int size)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun if (write(ofd, buffer, size) == size)
43*4882a593Smuzhiyun return;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun fprintf(stderr, "%s: Failed to write to output file: %s\n",
46*4882a593Smuzhiyun prog_name, strerror(errno));
47*4882a593Smuzhiyun exit(EXIT_FAILURE);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * The argv is expected to include one optional parameter and two filenames:
52*4882a593Smuzhiyun * [--vs] IN OUT
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * --vs - turns on the variable size SPL mode
55*4882a593Smuzhiyun * IN - the u-boot SPL binary, usually u-boot-spl.bin
56*4882a593Smuzhiyun * OUT - the prepared SPL blob, usually ${BOARD}-spl.bin
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * This utility first reads the "u-boot-spl.bin" into a buffer. In case of
59*4882a593Smuzhiyun * fixed size SPL the buffer size is exactly CHECKSUM_OFFSET (such that
60*4882a593Smuzhiyun * smaller u-boot-spl.bin gets padded with 0xff bytes, the larger than limit
61*4882a593Smuzhiyun * u-boot-spl.bin causes an error). For variable size SPL the buffer size is
62*4882a593Smuzhiyun * eqaul to size of the IN file.
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Then it calculates checksum of the buffer by just summing up all bytes.
65*4882a593Smuzhiyun * Then
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * - for fixed size SPL the buffer is written into the output file and the
68*4882a593Smuzhiyun * checksum is appended to the file in little endian format, which results
69*4882a593Smuzhiyun * in checksum added exactly at CHECKSUM_OFFSET.
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * - for variable size SPL the checksum and file size are stored in the
72*4882a593Smuzhiyun * var_size_header structure (again, in little endian format) and the
73*4882a593Smuzhiyun * structure is written into the output file. Then the buffer is written
74*4882a593Smuzhiyun * into the output file.
75*4882a593Smuzhiyun */
main(int argc,char ** argv)76*4882a593Smuzhiyun int main(int argc, char **argv)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun unsigned char *buffer;
79*4882a593Smuzhiyun int i, ifd, ofd;
80*4882a593Smuzhiyun uint32_t checksum = 0;
81*4882a593Smuzhiyun off_t len;
82*4882a593Smuzhiyun int var_size_flag, read_size, count;
83*4882a593Smuzhiyun struct stat stat;
84*4882a593Smuzhiyun const int if_index = argc - 2; /* Input file name index in argv. */
85*4882a593Smuzhiyun const int of_index = argc - 1; /* Output file name index in argv. */
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Strip path off the program name. */
88*4882a593Smuzhiyun prog_name = strrchr(argv[0], '/');
89*4882a593Smuzhiyun if (prog_name)
90*4882a593Smuzhiyun prog_name++;
91*4882a593Smuzhiyun else
92*4882a593Smuzhiyun prog_name = argv[0];
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun if ((argc < 3) ||
95*4882a593Smuzhiyun (argc > 4) ||
96*4882a593Smuzhiyun ((argc == 4) && strcmp(argv[1], "--vs"))) {
97*4882a593Smuzhiyun fprintf(stderr, "Usage: %s [--vs] <infile> <outfile>\n",
98*4882a593Smuzhiyun prog_name);
99*4882a593Smuzhiyun exit(EXIT_FAILURE);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* four args mean variable size SPL wrapper is required */
103*4882a593Smuzhiyun var_size_flag = (argc == 4);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun ifd = open(argv[if_index], O_RDONLY);
106*4882a593Smuzhiyun if (ifd < 0) {
107*4882a593Smuzhiyun fprintf(stderr, "%s: Can't open %s: %s\n",
108*4882a593Smuzhiyun prog_name, argv[if_index], strerror(errno));
109*4882a593Smuzhiyun exit(EXIT_FAILURE);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun ofd = open(argv[of_index], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
113*4882a593Smuzhiyun if (ofd < 0) {
114*4882a593Smuzhiyun fprintf(stderr, "%s: Can't open %s: %s\n",
115*4882a593Smuzhiyun prog_name, argv[of_index], strerror(errno));
116*4882a593Smuzhiyun exit(EXIT_FAILURE);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun if (fstat(ifd, &stat)) {
120*4882a593Smuzhiyun fprintf(stderr, "%s: Unable to get size of %s: %s\n",
121*4882a593Smuzhiyun prog_name, argv[if_index], strerror(errno));
122*4882a593Smuzhiyun exit(EXIT_FAILURE);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun len = stat.st_size;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (var_size_flag) {
128*4882a593Smuzhiyun read_size = len;
129*4882a593Smuzhiyun count = len;
130*4882a593Smuzhiyun } else {
131*4882a593Smuzhiyun if (len > CHECKSUM_OFFSET) {
132*4882a593Smuzhiyun fprintf(stderr,
133*4882a593Smuzhiyun "%s: %s is too big (exceeds %d bytes)\n",
134*4882a593Smuzhiyun prog_name, argv[if_index], CHECKSUM_OFFSET);
135*4882a593Smuzhiyun exit(EXIT_FAILURE);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun count = CHECKSUM_OFFSET;
138*4882a593Smuzhiyun read_size = len;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun buffer = malloc(count);
142*4882a593Smuzhiyun if (!buffer) {
143*4882a593Smuzhiyun fprintf(stderr,
144*4882a593Smuzhiyun "%s: Failed to allocate %d bytes to store %s\n",
145*4882a593Smuzhiyun prog_name, count, argv[if_index]);
146*4882a593Smuzhiyun exit(EXIT_FAILURE);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun if (read(ifd, buffer, read_size) != read_size) {
150*4882a593Smuzhiyun fprintf(stderr, "%s: Can't read %s: %s\n",
151*4882a593Smuzhiyun prog_name, argv[if_index], strerror(errno));
152*4882a593Smuzhiyun exit(EXIT_FAILURE);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* Pad if needed with 0xff to make flashing faster. */
156*4882a593Smuzhiyun if (read_size < count)
157*4882a593Smuzhiyun memset((char *)buffer + read_size, 0xff, count - read_size);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun for (i = 0, checksum = 0; i < count; i++)
160*4882a593Smuzhiyun checksum += buffer[i];
161*4882a593Smuzhiyun checksum = cpu_to_le32(checksum);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (var_size_flag) {
164*4882a593Smuzhiyun /* Prepare and write out the variable size SPL header. */
165*4882a593Smuzhiyun struct var_size_header vsh;
166*4882a593Smuzhiyun uint32_t spl_size;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun memset(&vsh, 0, sizeof(vsh));
169*4882a593Smuzhiyun memcpy(&vsh.spl_checksum, &checksum, sizeof(checksum));
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun spl_size = cpu_to_le32(count + sizeof(struct var_size_header));
172*4882a593Smuzhiyun memcpy(&vsh.spl_size, &spl_size, sizeof(spl_size));
173*4882a593Smuzhiyun write_to_file(ofd, &vsh, sizeof(vsh));
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun write_to_file(ofd, buffer, count);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* For fixed size SPL checksum is appended in the end. */
179*4882a593Smuzhiyun if (!var_size_flag)
180*4882a593Smuzhiyun write_to_file(ofd, &checksum, sizeof(checksum));
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun close(ifd);
183*4882a593Smuzhiyun close(ofd);
184*4882a593Smuzhiyun free(buffer);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun return EXIT_SUCCESS;
187*4882a593Smuzhiyun }
188