sdramfile.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * int sdramfile (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 SDRAMFILE_SOURCE
  22. #define SDRAMFILE_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 sdramfile (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 (& config_ram, sizeof (config_ram), checksum))
  61. {
  62. if (_allclr (flags, SDRAM_SILENCE))
  63. {
  64. error (0, 0, "Bad checksum: %s", filename);
  65. }
  66. return (- 1);
  67. }
  68. if (lseek (fd, 0, SEEK_SET) == - 1)
  69. {
  70. if (_allclr (flags, SDRAM_SILENCE))
  71. {
  72. error (0, errno, "Can't rewind file: %s", filename);
  73. }
  74. return (- 1);
  75. }
  76. return (0);
  77. }
  78. #endif