mksmdkv310spl.c 2.1 KB

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