lightning_pib_lock.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed lightning_pib_lock (struct _file_ const * pib);
  11. *
  12. * recompute and update the checksum on a thunderbolt or lightning
  13. * PIB file where the checksum is located inside the PIB;
  14. *
  15. *--------------------------------------------------------------------*/
  16. #ifndef LIGHTNING_PIB_LOCK_SOURCE
  17. #define LIGHTNING_PIB_LOCK_SOURCE
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include "../tools/error.h"
  21. #include "../tools/files.h"
  22. #include "../pib/pib.h"
  23. signed lightning_pib_lock (struct _file_ const * file)
  24. {
  25. struct simple_pib simple_pib;
  26. if (lseek (file->file, 0, SEEK_SET))
  27. {
  28. error (1, errno, "1 " FILE_CANTHOME, file->name);
  29. }
  30. if (read (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  31. {
  32. error (1, errno, FILE_CANTREAD, file->name);
  33. }
  34. if (lseek (file->file, 0, SEEK_SET))
  35. {
  36. error (1, errno, FILE_CANTHOME, file->name);
  37. }
  38. simple_pib.CHECKSUM = fdchecksum32 (file->file, LE16TOH (simple_pib.PIBLENGTH), simple_pib.CHECKSUM);
  39. if (lseek (file->file, 0, SEEK_SET))
  40. {
  41. error (1, errno, "2 " FILE_CANTHOME, file->name);
  42. }
  43. if (write (file->file, & simple_pib, sizeof (simple_pib)) != sizeof (simple_pib))
  44. {
  45. error (1, errno, FILE_CANTSAVE, file->name);
  46. }
  47. if (lseek (file->file, 0, SEEK_SET))
  48. {
  49. error (1, errno, FILE_CANTHOME, file->name);
  50. }
  51. return (0);
  52. }
  53. #endif