mkv310_image.c 2.9 KB

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