mkv310_image.c 2.9 KB

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