sdramfileA.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * int sdramfileA (int fd, char const * filename, flag_t flags);
  11. *
  12. * sdram.h
  13. *
  14. * open an SDRAM configuration file and validate it by checking the
  15. * file length; unfortunately there are not many things to check;
  16. *
  17. * Contributor(s):
  18. * Charles Maier <cmaier@qca.qualcomm.com>
  19. *
  20. *--------------------------------------------------------------------*/
  21. #ifndef SDRAMFILEA_SOURCE
  22. #define SDRAMFILEA_SOURCE
  23. #include <unistd.h>
  24. #include <memory.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include "../ram/sdram.h"
  28. #include "../tools/memory.h"
  29. #include "../tools/error.h"
  30. #include "../tools/flags.h"
  31. int sdramfileA (int fd, char const * filename, flag_t flags)
  32. {
  33. uint32_t checksum;
  34. struct config_ram config_ram;
  35. memset (& config_ram, 0, sizeof (config_ram));
  36. if (lseek (fd, 0, SEEK_SET) == - 1)
  37. {
  38. if (_allclr (flags, SDRAM_SILENCE))
  39. {
  40. error (0, errno, "Can't rewind file: %s", filename);
  41. }
  42. return (- 1);
  43. }
  44. if (read (fd, & config_ram, sizeof (config_ram)) != sizeof (config_ram))
  45. {
  46. if (_allclr (flags, SDRAM_SILENCE))
  47. {
  48. error (0, errno, "Wrong file size: %s", filename);
  49. }
  50. return (- 1);
  51. }
  52. if (read (fd, & checksum, sizeof (checksum)) != sizeof (checksum))
  53. {
  54. if (_allclr (flags, SDRAM_SILENCE))
  55. {
  56. error (0, errno, "Can't read checksum: %s", filename);
  57. }
  58. return (- 1);
  59. }
  60. if (checksum32 ((uint32_t *) (& config_ram), sizeof (config_ram) >> 2, checksum))
  61. {
  62. if (_allclr (flags, SDRAM_SILENCE))
  63. {
  64. error (0, 0, "Bad checksum: %s", filename);
  65. }
  66. return (- 1);
  67. }
  68. if (_anyset (flags, SDRAM_VERBOSE))
  69. {
  70. if ((filename) && (* filename))
  71. {
  72. printf ("------- %s -------\n", filename);
  73. }
  74. sdrampeek (& config_ram);
  75. }
  76. if (lseek (fd, 0, SEEK_SET) == - 1)
  77. {
  78. if (_allclr (flags, SDRAM_SILENCE))
  79. {
  80. error (0, errno, "Can't rewind file: %s", filename);
  81. }
  82. return (- 1);
  83. }
  84. return (0);
  85. }
  86. #endif