mkv310_image.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2011 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. #define BUFSIZE (16*1024)
  30. #define IMG_SIZE (16*1024)
  31. #define SPL_HEADER_SIZE 16
  32. #define FILE_PERM (S_IRUSR | S_IWUSR | S_IRGRP \
  33. | S_IWGRP | S_IROTH | S_IWOTH)
  34. #define SPL_HEADER "S5PC210 HEADER "
  35. /*
  36. * Requirement:
  37. * IROM code reads first 14K bytes from boot device.
  38. * It then calculates the checksum of 14K-4 bytes and compare with data at
  39. * 14K-4 offset.
  40. *
  41. * This function takes two filenames:
  42. * IN "u-boot-spl.bin" and
  43. * OUT "$(BOARD)-spl.bin as filenames.
  44. * It reads the "u-boot-spl.bin" in 16K buffer.
  45. * It calculates checksum of 14K-4 Bytes and stores at 14K-4 offset in buffer.
  46. * It writes the buffer to "$(BOARD)-spl.bin" file.
  47. */
  48. int main(int argc, char **argv)
  49. {
  50. int i, len;
  51. unsigned char buffer[BUFSIZE] = {0};
  52. int ifd, ofd;
  53. unsigned int checksum = 0, count;
  54. if (argc != 3) {
  55. printf(" %d Wrong number of arguments\n", argc);
  56. exit(EXIT_FAILURE);
  57. }
  58. ifd = open(argv[1], O_RDONLY);
  59. if (ifd < 0) {
  60. fprintf(stderr, "%s: Can't open %s: %s\n",
  61. argv[0], argv[1], strerror(errno));
  62. exit(EXIT_FAILURE);
  63. }
  64. ofd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_PERM);
  65. if (ifd < 0) {
  66. fprintf(stderr, "%s: Can't open %s: %s\n",
  67. argv[0], argv[2], strerror(errno));
  68. if (ifd)
  69. close(ifd);
  70. exit(EXIT_FAILURE);
  71. }
  72. len = lseek(ifd, 0, SEEK_END);
  73. lseek(ifd, 0, SEEK_SET);
  74. memcpy(&buffer[0], SPL_HEADER, SPL_HEADER_SIZE);
  75. count = (len < (IMG_SIZE - SPL_HEADER_SIZE))
  76. ? len : (IMG_SIZE - SPL_HEADER_SIZE);
  77. if (read(ifd, buffer + SPL_HEADER_SIZE, count) != count) {
  78. fprintf(stderr, "%s: Can't read %s: %s\n",
  79. argv[0], argv[1], strerror(errno));
  80. if (ifd)
  81. close(ifd);
  82. if (ofd)
  83. close(ofd);
  84. exit(EXIT_FAILURE);
  85. }
  86. for (i = 0; i < IMG_SIZE - SPL_HEADER_SIZE; i++)
  87. checksum += buffer[i+16];
  88. *(ulong *)buffer ^= 0x1f;
  89. *(ulong *)(buffer+4) ^= checksum;
  90. for (i = 1; i < SPL_HEADER_SIZE; i++)
  91. buffer[i] ^= buffer[i-1];
  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. if (ifd)
  96. close(ifd);
  97. if (ofd)
  98. close(ofd);
  99. exit(EXIT_FAILURE);
  100. }
  101. if (ifd)
  102. close(ifd);
  103. if (ofd)
  104. close(ofd);
  105. return EXIT_SUCCESS;
  106. }