tonvm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*====================================================================*
  2. * Copyright (c) 2013-2021 Qualcomm Technologies, Inc.
  3. * All Rights Reserved.
  4. * Confidential and Proprietary - Qualcomm Technologies, Inc.
  5. * ******************************************************************
  6. * 2013 Qualcomm Atheros, Inc.
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * tonvm.c - prepend NVM image header;
  11. *
  12. *
  13. *--------------------------------------------------------------------*/
  14. /*====================================================================*
  15. * system header files;
  16. *--------------------------------------------------------------------*/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. /*====================================================================*
  21. * custom header files;
  22. *--------------------------------------------------------------------*/
  23. #include "../tools/getoptv.h"
  24. #include "../tools/putoptv.h"
  25. #include "../tools/version.h"
  26. #include "../tools/error.h"
  27. #include "../tools/files.h"
  28. #include "../tools/endian.h"
  29. #include "../tools/number.h"
  30. #include "../tools/memory.h"
  31. #include "../nvm/nvm.h"
  32. /*====================================================================*
  33. * custom source files;
  34. *--------------------------------------------------------------------*/
  35. #ifndef MAKEFILE
  36. #include "../tools/getoptv.c"
  37. #include "../tools/putoptv.c"
  38. #include "../tools/version.c"
  39. #include "../tools/basespec.c"
  40. #include "../tools/checksum32.c"
  41. #include "../tools/todigit.c"
  42. #include "../tools/efreopen.c"
  43. #include "../tools/error.c"
  44. #endif
  45. /*====================================================================*
  46. *
  47. * void function (void);
  48. *
  49. *--------------------------------------------------------------------*/
  50. void function (char const * filename, struct lightning_nvm_header * header)
  51. {
  52. signed fd;
  53. signed length;
  54. signed length_word_aligned;
  55. void * memory;
  56. if ((fd = open (filename, O_BINARY | O_RDONLY)) == - 1)
  57. {
  58. error (1, errno, FILE_CANTOPEN, filename);
  59. }
  60. if ((length = lseek (fd, 0, SEEK_END)) == - 1)
  61. {
  62. error (1, errno, FILE_CANTSIZE, filename);
  63. }
  64. length_word_aligned = length;
  65. if (length_word_aligned % sizeof (uint32_t))
  66. {
  67. length_word_aligned += sizeof(uint32_t) - (length_word_aligned % sizeof(uint32_t));
  68. }
  69. if (! (memory = malloc (length_word_aligned)))
  70. {
  71. error (1, errno, FILE_CANTLOAD, filename);
  72. }
  73. if (lseek (fd, 0, SEEK_SET))
  74. {
  75. error (1, errno, FILE_CANTHOME, filename);
  76. }
  77. if (read (fd, memory, length) != length)
  78. {
  79. error (1, errno, FILE_CANTREAD, filename);
  80. }
  81. close (fd);
  82. if (* (int32_t *) (memory) == HTOLE32 (0x60000000))
  83. {
  84. error (0, 0, "Nothing to do.");
  85. return;
  86. }
  87. header->IMAGELENGTH = HTOLE32 (length);
  88. header->IMAGECHECKSUM = checksum32 (memory, length, 0);
  89. header->HEADERCHECKSUM = checksum32 (header, sizeof (* header), 0);
  90. if ((fd = open (filename, O_BINARY | O_WRONLY)) == - 1)
  91. {
  92. error (1, errno, FILE_CANTOPEN, filename);
  93. }
  94. if (write (fd, header, sizeof (* header)) != sizeof (* header))
  95. {
  96. error (1, errno, FILE_CANTSAVE, filename);
  97. }
  98. if (write (fd, memory, length) != length)
  99. {
  100. error (1, errno, FILE_CANTSAVE, filename);
  101. }
  102. close (fd);
  103. return;
  104. }
  105. /*====================================================================*
  106. *
  107. * int main (int argc, char const * argv []);
  108. *
  109. *--------------------------------------------------------------------*/
  110. int main (int argc, char const * argv [])
  111. {
  112. struct lightning_nvm_header header;
  113. static char const * optv [] =
  114. {
  115. "E:F:I:M:S:T:",
  116. "file [file] [file] [...]",
  117. "Prepend Lightning/Thunderbolt NVM Image header",
  118. "E x\tEntry Point",
  119. "F x\tFlash Address",
  120. "M x\tModule ID",
  121. "m x\tModule SubID",
  122. "S x\tSDRAM Address",
  123. "T x\tImage Type",
  124. "I x\tIgnore Mask",
  125. (char const *) (0)
  126. };
  127. signed c;
  128. memset (& header, 0, sizeof (header));
  129. header.HEADERVERSION = HTOLE32 (0x60000000);
  130. header.HEADERMINORVERSION = 1;
  131. while (~ (c = getoptv (argc, argv, optv)))
  132. {
  133. switch (c)
  134. {
  135. case 'E':
  136. header.ENTRYPOINT = (uint32_t) (basespec (optarg, 16, sizeof (header.ENTRYPOINT)));
  137. break;
  138. case 'F':
  139. header.IMAGEROMADDR = (uint32_t) (basespec (optarg, 16, sizeof (header.IMAGEROMADDR)));
  140. break;
  141. case 'I':
  142. header.IGNOREMASK = (uint32_t) (basespec (optarg, 16, sizeof (header.IGNOREMASK)));
  143. break;
  144. case 'M':
  145. header.MODULEID = (uint32_t) (basespec (optarg, 16, sizeof (header.MODULEID)));
  146. break;
  147. case 'm':
  148. header.MODULESUBID = (uint32_t) (basespec (optarg, 16, sizeof (header.MODULESUBID)));
  149. break;
  150. case 'S':
  151. header.IMAGEADDRESS = (uint32_t) (basespec (optarg, 16, sizeof (header.IMAGEADDRESS)));
  152. break;
  153. case 'T':
  154. header.IMAGETYPE = (uint8_t) (basespec (optarg, 16, sizeof (header.IMAGETYPE)));
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. argc -= optind;
  161. argv += optind;
  162. if (! argc)
  163. {
  164. error (1, ENOTSUP, "No files given.");
  165. }
  166. while ((argc) && (* argv))
  167. {
  168. function (* argv, & header);
  169. argc--;
  170. argv++;
  171. }
  172. exit (0);
  173. }