nvram.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * NVRAM variable manipulation
  3. *
  4. * Copyright 2007, Broadcom Corporation
  5. * Copyright 2009, OpenWrt.org
  6. * All Rights Reserved.
  7. *
  8. * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
  9. * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
  10. * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
  11. * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
  12. *
  13. */
  14. #ifndef _nvram_h_
  15. #define _nvram_h_
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <errno.h>
  23. #include <sys/mman.h>
  24. #include <sys/stat.h>
  25. #include <linux/limits.h>
  26. #include "sdinitvals.h"
  27. struct nvram_header {
  28. uint32_t magic;
  29. uint32_t len;
  30. uint32_t crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
  31. uint32_t config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
  32. uint32_t config_ncdl; /* ncdl values for memc */
  33. } __attribute__((__packed__));
  34. struct nvram_tuple {
  35. char *name;
  36. char *value;
  37. struct nvram_tuple *next;
  38. };
  39. struct nvram_handle {
  40. int fd;
  41. char *mmap;
  42. unsigned int length;
  43. unsigned int offset;
  44. struct nvram_tuple *nvram_hash[257];
  45. struct nvram_tuple *nvram_dead;
  46. };
  47. typedef struct nvram_handle nvram_handle_t;
  48. typedef struct nvram_header nvram_header_t;
  49. typedef struct nvram_tuple nvram_tuple_t;
  50. /* Get nvram header. */
  51. nvram_header_t * nvram_header(nvram_handle_t *h);
  52. /* Set the value of an NVRAM variable */
  53. int nvram_set(nvram_handle_t *h, const char *name, const char *value);
  54. /* Get the value of an NVRAM variable. */
  55. char * nvram_get(nvram_handle_t *h, const char *name);
  56. /* Unset the value of an NVRAM variable. */
  57. int nvram_unset(nvram_handle_t *h, const char *name);
  58. /* Get all NVRAM variables. */
  59. nvram_tuple_t * nvram_getall(nvram_handle_t *h);
  60. /* Regenerate NVRAM. */
  61. int nvram_commit(nvram_handle_t *h);
  62. /* Open NVRAM and obtain a handle. */
  63. nvram_handle_t * nvram_open(const char *file, int rdonly);
  64. /* Close NVRAM and free memory. */
  65. int nvram_close(nvram_handle_t *h);
  66. /* Get the value of an NVRAM variable in a safe way, use "" instead of NULL. */
  67. #define nvram_safe_get(h, name) (nvram_get(h, name) ? : "")
  68. /* Computes a crc8 over the input data. */
  69. uint8_t hndcrc8 (uint8_t * pdata, uint32_t nbytes, uint8_t crc);
  70. /* Returns the crc value of the nvram. */
  71. uint8_t nvram_calc_crc(nvram_header_t * nvh);
  72. /* Determine NVRAM device node. */
  73. char * nvram_find_mtd(void);
  74. /* Copy NVRAM contents to staging file. */
  75. int nvram_to_staging(void);
  76. /* Copy staging file to NVRAM device. */
  77. int staging_to_nvram(void);
  78. /* Check NVRAM staging file. */
  79. char * nvram_find_staging(void);
  80. /* Staging file for NVRAM */
  81. #define NVRAM_STAGING "/tmp/.nvram"
  82. #define NVRAM_RO 1
  83. #define NVRAM_RW 0
  84. /* Helper macros */
  85. #define NVRAM_ARRAYSIZE(a) sizeof(a)/sizeof(a[0])
  86. #define NVRAM_ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
  87. /* NVRAM constants */
  88. #define NVRAM_MIN_SPACE 0x8000
  89. #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
  90. #define NVRAM_VERSION 1
  91. #define NVRAM_CRC_START_POSITION 9 /* magic, len, crc8 to be skipped */
  92. #endif /* _nvram_h_ */