mkexynosspl.c 5.1 KB

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