config2cfg.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * config2cfg.c - convert a .config file to a .cfg file;
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. #define _GETOPT_H
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. /*====================================================================*
  25. * custom header files;
  26. *--------------------------------------------------------------------*/
  27. #include "../tools/getoptv.h"
  28. #include "../tools/putoptv.h"
  29. #include "../tools/version.h"
  30. #include "../tools/number.h"
  31. #include "../tools/flags.h"
  32. #include "../tools/memory.h"
  33. #include "../tools/files.h"
  34. #include "../tools/error.h"
  35. #include "../ram/sdram.h"
  36. /*====================================================================*
  37. * custom source files;
  38. *--------------------------------------------------------------------*/
  39. #ifndef MAKEFILE
  40. #include "../tools/getoptv.c"
  41. #include "../tools/putoptv.c"
  42. #include "../tools/version.c"
  43. #include "../tools/error.c"
  44. #include "../tools/checksum32.c"
  45. #include "../tools/todigit.c"
  46. #include "../tools/hexencode.c"
  47. #endif
  48. /*====================================================================*
  49. *
  50. * int main (int argc, char const * argv []);
  51. *
  52. *--------------------------------------------------------------------*/
  53. int main (int argc, char const * argv [])
  54. {
  55. static char const * optv [] =
  56. {
  57. "qv",
  58. "file [file] [...]",
  59. "convert ASCII SDRAM configuration files (DM) to binary (toolkit) format",
  60. "q\tquiet mode",
  61. "v\tverbose mode",
  62. (char const *) (0)
  63. };
  64. struct config_ram config_ram;
  65. char string [(sizeof (config_ram) << 1) + 1];
  66. uint32_t checksum;
  67. flag_t flags = (flag_t) (0);
  68. signed state = 1;
  69. signed fd;
  70. signed c;
  71. optind = 1;
  72. while (~ (c = getoptv (argc, argv, optv)))
  73. {
  74. switch ((char) (c))
  75. {
  76. case 'q':
  77. _setbits (flags, SDRAM_SILENCE);
  78. break;
  79. case 'v':
  80. _setbits (flags, SDRAM_VERBOSE);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. argc -= optind;
  87. argv += optind;
  88. while ((argc-- > 0) && (* argv != (char const *) (0)))
  89. {
  90. #if 0
  91. char const * pathname;
  92. char const * filename;
  93. char const * extender;
  94. for (pathname = filename = * argv; * pathname; pathname++)
  95. {
  96. if ((* pathname == '/') || (* pathname == '\\'))
  97. {
  98. filename = pathname + 1;
  99. }
  100. }
  101. for (pathname = extender = filename; * pathname; pathname++)
  102. {
  103. if (* pathname == '.')
  104. {
  105. extender = pathname;
  106. }
  107. }
  108. if (extender == filename)
  109. {
  110. extender = pathname;
  111. }
  112. #endif
  113. if ((fd = open (* argv, O_BINARY | O_RDONLY)) == - 1)
  114. {
  115. error (0, errno, "can't open %s for input", * argv);
  116. state = 1;
  117. }
  118. else if (read (fd, & string, sizeof (string)) < (ssize_t) (sizeof (string) - 1))
  119. {
  120. error (0, errno, "can't read %s", * argv);
  121. state = 1;
  122. }
  123. else
  124. {
  125. close (fd);
  126. if (hexencode ((uint8_t *) (& config_ram), sizeof (config_ram), string) == sizeof (config_ram))
  127. {
  128. error (1, errno, "%s is suspect", * argv);
  129. }
  130. checksum = checksum32 (& config_ram, sizeof (config_ram), 0);
  131. if ((fd = open (* argv, O_BINARY | O_CREAT | O_RDWR | O_TRUNC, FILE_FILEMODE)) == - 1)
  132. {
  133. error (1, errno, "can't open %s for output", * argv);
  134. }
  135. write (fd, & config_ram, sizeof (config_ram));
  136. write (fd, & checksum, sizeof (checksum));
  137. }
  138. close (fd);
  139. argv++;
  140. }
  141. return (state);
  142. }