mkexynosspl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2012 Samsung Electronics
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <compiler.h>
  30. #define CHECKSUM_OFFSET (14*1024-4)
  31. #define BUFSIZE (14*1024)
  32. #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
  33. | S_IWGRP | S_IROTH | S_IWOTH)
  34. /*
  35. * Requirement:
  36. * IROM code reads first 14K bytes from boot device.
  37. * It then calculates the checksum of 14K-4 bytes and compare with data at
  38. * 14K-4 offset.
  39. *
  40. * This function takes two filenames:
  41. * IN "u-boot-spl.bin" and
  42. * OUT "$(BOARD)-spl.bin as filenames.
  43. * It reads the "u-boot-spl.bin" in 16K buffer.
  44. * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
  45. * It writes the buffer to "$(BOARD)-spl.bin" file.
  46. */
  47. int main(int argc, char **argv)
  48. {
  49. unsigned char buffer[BUFSIZE];
  50. int i, ifd, ofd;
  51. uint32_t checksum = 0;
  52. off_t len;
  53. ssize_t count;
  54. struct stat stat;
  55. if (argc != 3) {
  56. fprintf(stderr, "Usage: %s <infile> <outfile>\n", argv[0]);
  57. exit(EXIT_FAILURE);
  58. }
  59. ifd = open(argv[1], O_RDONLY);
  60. if (ifd < 0) {
  61. fprintf(stderr, "%s: Can't open %s: %s\n",
  62. argv[0], argv[1], strerror(errno));
  63. exit(EXIT_FAILURE);
  64. }
  65. ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
  66. if (ifd < 0) {
  67. fprintf(stderr, "%s: Can't open %s: %s\n",
  68. argv[0], argv[2], strerror(errno));
  69. close(ifd);
  70. exit(EXIT_FAILURE);
  71. }
  72. if (fstat(ifd, &stat)) {
  73. fprintf(stderr, "%s: Unable to get size of %s: %s\n",
  74. argv[0], argv[1], strerror(errno));
  75. close(ifd);
  76. close(ofd);
  77. exit(EXIT_FAILURE);
  78. }
  79. len = stat.st_size;
  80. count = (len < CHECKSUM_OFFSET) ? len : CHECKSUM_OFFSET;
  81. if (read(ifd, buffer, count) != count) {
  82. fprintf(stderr, "%s: Can't read %s: %s\n",
  83. argv[0], argv[1], strerror(errno));
  84. close(ifd);
  85. close(ofd);
  86. exit(EXIT_FAILURE);
  87. }
  88. for (i = 0, checksum = 0; i < CHECKSUM_OFFSET; i++)
  89. checksum += buffer[i];
  90. checksum = cpu_to_le32(checksum);
  91. 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));
  95. close(ifd);
  96. close(ofd);
  97. exit(EXIT_FAILURE);
  98. }
  99. close(ifd);
  100. close(ofd);
  101. return EXIT_SUCCESS;
  102. }