lightning_pib_file.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed lightning_pib_file (struct _file_ const * file);
  11. *
  12. * pib.h
  13. *
  14. * open a thunderbolt/lightning PIB file and validate it by
  15. * checking file size, checksum and selected internal parameters;
  16. * return a file descriptor on success; terminate the program on
  17. * error;
  18. *
  19. * Contributor(s):
  20. * Charles Maier <cmaier@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. #ifndef LIGHTNING_PIB_FILE_SOURCE
  24. #define LIGHTNING_PIB_FILE_SOURCE
  25. #include <unistd.h>
  26. #include <errno.h>
  27. #include "../tools/files.h"
  28. #include "../tools/error.h"
  29. #include "../pib/pib.h"
  30. signed lightning_pib_file (struct _file_ const * file)
  31. {
  32. struct simple_pib simple_pib;
  33. if (lseek (file->file, 0, SEEK_SET))
  34. {
  35. error (1, errno, FILE_CANTHOME, file->name);
  36. }
  37. if (read (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  38. {
  39. error (1, errno, FILE_CANTREAD, file->name);
  40. }
  41. if (lseek (file->file, 0, SEEK_SET))
  42. {
  43. error (1, errno, FILE_CANTHOME, file->name);
  44. }
  45. if ((simple_pib.RESERVED1) || (simple_pib.RESERVED2))
  46. {
  47. error (1, errno, PIB_BADCONTENT, file->name);
  48. }
  49. if (fdchecksum32 (file->file, LE16TOH (simple_pib.PIBLENGTH), 0))
  50. {
  51. error (1, errno, PIB_BADCHECKSUM, file->name);
  52. }
  53. if (lseek (file->file, 0, SEEK_SET))
  54. {
  55. error (1, errno, FILE_CANTHOME, file->name);
  56. }
  57. return (0);
  58. }
  59. #endif