mkorigenspl.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 Samsung Electronics
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/stat.h>
  12. #define BUFSIZE (16*1024)
  13. #define IMG_SIZE (16*1024)
  14. #define SPL_HEADER_SIZE 16
  15. #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
  16. | S_IWGRP | S_IROTH | S_IWOTH)
  17. #define SPL_HEADER "S5PC210 HEADER "
  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 "$(BOARD)-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 "$(BOARD)-spl.bin" file.
  30. */
  31. int main(int argc, char **argv)
  32. {
  33. int i, len;
  34. unsigned char buffer[BUFSIZE] = {0};
  35. int ifd, ofd;
  36. unsigned int checksum = 0, count;
  37. if (argc != 3) {
  38. printf(" %d Wrong number of arguments\n", argc);
  39. exit(EXIT_FAILURE);
  40. }
  41. ifd = open(argv[1], O_RDONLY);
  42. if (ifd < 0) {
  43. fprintf(stderr, "%s: Can't open %s: %s\n",
  44. argv[0], argv[1], strerror(errno));
  45. exit(EXIT_FAILURE);
  46. }
  47. ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
  48. if (ofd < 0) {
  49. fprintf(stderr, "%s: Can't open %s: %s\n",
  50. argv[0], argv[2], strerror(errno));
  51. if (ifd)
  52. close(ifd);
  53. exit(EXIT_FAILURE);
  54. }
  55. len = lseek(ifd, 0, SEEK_END);
  56. lseek(ifd, 0, SEEK_SET);
  57. memcpy(&buffer[0], SPL_HEADER, SPL_HEADER_SIZE);
  58. count = (len < (IMG_SIZE - SPL_HEADER_SIZE))
  59. ? len : (IMG_SIZE - SPL_HEADER_SIZE);
  60. if (read(ifd, buffer + SPL_HEADER_SIZE, count) != count) {
  61. fprintf(stderr, "%s: Can't read %s: %s\n",
  62. argv[0], argv[1], strerror(errno));
  63. if (ifd)
  64. close(ifd);
  65. if (ofd)
  66. close(ofd);
  67. exit(EXIT_FAILURE);
  68. }
  69. for (i = 0; i < IMG_SIZE - SPL_HEADER_SIZE; i++)
  70. checksum += buffer[i+16];
  71. *(unsigned long *)buffer ^= 0x1f;
  72. *(unsigned long *)(buffer+4) ^= checksum;
  73. for (i = 1; i < SPL_HEADER_SIZE; i++)
  74. buffer[i] ^= buffer[i-1];
  75. if (write(ofd, buffer, BUFSIZE) != BUFSIZE) {
  76. fprintf(stderr, "%s: Can't write %s: %s\n",
  77. argv[0], argv[2], strerror(errno));
  78. if (ifd)
  79. close(ifd);
  80. if (ofd)
  81. close(ofd);
  82. exit(EXIT_FAILURE);
  83. }
  84. if (ifd)
  85. close(ifd);
  86. if (ofd)
  87. close(ofd);
  88. return EXIT_SUCCESS;
  89. }