#ifndef _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_
#define _BHND_NVRAM_BHND_NVRAM_STOREVAR_H_
#include <sys/types.h>
#ifndef _KERNEL
#include <pthread.h>
#endif
#include "bhnd_nvram_plist.h"
#include "bhnd_nvram_store.h"
#define BHND_NV_IDX_VAR_THRESHOLD 15
#define BHND_NVSTORE_ROOT_PATH "/"
#define BHND_NVSTORE_ROOT_PATH_LEN sizeof(BHND_NVSTORE_ROOT_PATH)
#define BHND_NVSTORE_GET_FLAG(_value, _flag) \
(((_value) & BHND_NVSTORE_ ## _flag) != 0)
#define BHND_NVSTORE_GET_BITS(_value, _field) \
((_value) & BHND_NVSTORE_ ## _field ## _MASK)
typedef struct bhnd_nvstore_name_info bhnd_nvstore_name_info;
typedef struct bhnd_nvstore_index bhnd_nvstore_index;
typedef struct bhnd_nvstore_path bhnd_nvstore_path;
typedef struct bhnd_nvstore_alias bhnd_nvstore_alias;
typedef struct bhnd_nvstore_alias_list bhnd_nvstore_alias_list;
typedef struct bhnd_nvstore_update_list bhnd_nvstore_update_list;
typedef struct bhnd_nvstore_path_list bhnd_nvstore_path_list;
LIST_HEAD(bhnd_nvstore_alias_list, bhnd_nvstore_alias);
LIST_HEAD(bhnd_nvstore_update_list, bhnd_nvstore_update);
LIST_HEAD(bhnd_nvstore_path_list, bhnd_nvstore_path);
typedef enum {
BHND_NVSTORE_VAR = 0,
BHND_NVSTORE_ALIAS_DECL = 1,
} bhnd_nvstore_var_type;
typedef enum {
BHND_NVSTORE_PATH_STRING = 0,
BHND_NVSTORE_PATH_ALIAS = 1
} bhnd_nvstore_path_type;
typedef enum {
BHND_NVSTORE_NAME_INTERNAL = 1,
BHND_NVSTORE_NAME_EXTERNAL = 2,
} bhnd_nvstore_name_type;
bhnd_nvstore_path *bhnd_nvstore_path_new(const char *path_str,
size_t path_len);
void bhnd_nvstore_path_free(struct bhnd_nvstore_path *path);
bhnd_nvstore_index *bhnd_nvstore_index_new(size_t capacity);
void bhnd_nvstore_index_free(bhnd_nvstore_index *index);
int bhnd_nvstore_index_append(struct bhnd_nvram_store *sc,
bhnd_nvstore_index *index,
void *cookiep);
int bhnd_nvstore_index_prepare(
struct bhnd_nvram_store *sc,
bhnd_nvstore_index *index);
void *bhnd_nvstore_index_lookup(struct bhnd_nvram_store *sc,
bhnd_nvstore_index *index, const char *name);
bhnd_nvstore_path *bhnd_nvstore_get_root_path(
struct bhnd_nvram_store *sc);
bool bhnd_nvstore_is_root_path(struct bhnd_nvram_store *sc,
bhnd_nvstore_path *path);
void *bhnd_nvstore_path_data_next(
struct bhnd_nvram_store *sc,
bhnd_nvstore_path *path, void **indexp);
void *bhnd_nvstore_path_data_lookup(
struct bhnd_nvram_store *sc,
bhnd_nvstore_path *path, const char *name);
bhnd_nvram_prop *bhnd_nvstore_path_get_update(
struct bhnd_nvram_store *sc,
bhnd_nvstore_path *path, const char *name);
int bhnd_nvstore_path_register_update(
struct bhnd_nvram_store *sc,
bhnd_nvstore_path *path, const char *name,
bhnd_nvram_val *value);
bhnd_nvstore_alias *bhnd_nvstore_find_alias(struct bhnd_nvram_store *sc,
const char *path);
bhnd_nvstore_alias *bhnd_nvstore_get_alias(struct bhnd_nvram_store *sc,
u_long alias_val);
bhnd_nvstore_path *bhnd_nvstore_get_path(struct bhnd_nvram_store *sc,
const char *path, size_t path_len);
bhnd_nvstore_path *bhnd_nvstore_resolve_path_alias(
struct bhnd_nvram_store *sc, u_long aval);
bhnd_nvstore_path *bhnd_nvstore_var_get_path(struct bhnd_nvram_store *sc,
bhnd_nvstore_name_info *info);
int bhnd_nvstore_var_register_path(
struct bhnd_nvram_store *sc,
bhnd_nvstore_name_info *info, void *cookiep);
int bhnd_nvstore_register_path(struct bhnd_nvram_store *sc,
const char *path, size_t path_len);
int bhnd_nvstore_register_alias(
struct bhnd_nvram_store *sc,
const bhnd_nvstore_name_info *info, void *cookiep);
const char *bhnd_nvstore_parse_relpath(const char *parent,
const char *child);
int bhnd_nvstore_parse_name_info(const char *name,
bhnd_nvstore_name_type name_type,
uint32_t data_caps, bhnd_nvstore_name_info *info);
struct bhnd_nvstore_name_info {
const char *name;
bhnd_nvstore_var_type type;
bhnd_nvstore_path_type path_type;
union {
struct {
const char *value;
size_t value_len;
} str;
struct {
u_long value;
} alias;
} path;
};
struct bhnd_nvstore_index {
size_t count;
size_t capacity;
void *cookiep[];
};
struct bhnd_nvstore_path {
char *path_str;
size_t num_vars;
bhnd_nvstore_index *index;
bhnd_nvram_plist *pending;
LIST_ENTRY(bhnd_nvstore_path) np_link;
};
struct bhnd_nvstore_alias {
bhnd_nvstore_path *path;
void *cookiep;
u_long alias;
LIST_ENTRY(bhnd_nvstore_alias) na_link;
};
struct bhnd_nvram_store {
#ifdef _KERNEL
struct mtx mtx;
#else
pthread_mutex_t mtx;
#endif
struct bhnd_nvram_data *data;
uint32_t data_caps;
bhnd_nvram_plist *data_opts;
bhnd_nvstore_alias_list aliases[4];
size_t num_aliases;
bhnd_nvstore_path *root_path;
bhnd_nvstore_path_list paths[4];
size_t num_paths;
};
#ifdef _KERNEL
#define BHND_NVSTORE_LOCK_INIT(sc) \
mtx_init(&(sc)->mtx, "BHND NVRAM store lock", NULL, MTX_DEF)
#define BHND_NVSTORE_LOCK(sc) mtx_lock(&(sc)->mtx)
#define BHND_NVSTORE_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
#define BHND_NVSTORE_LOCK_ASSERT(sc, what) mtx_assert(&(sc)->mtx, what)
#define BHND_NVSTORE_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx)
#else
#define BHND_NVSTORE_LOCK_INIT(sc) do { \
int error = pthread_mutex_init(&(sc)->mtx, NULL); \
if (error) \
BHND_NV_PANIC("pthread_mutex_init() failed: %d", \
error); \
} while(0)
#define BHND_NVSTORE_LOCK(sc) pthread_mutex_lock(&(sc)->mtx)
#define BHND_NVSTORE_UNLOCK(sc) pthread_mutex_unlock(&(sc)->mtx)
#define BHND_NVSTORE_LOCK_DESTROY(sc) pthread_mutex_destroy(&(sc)->mtx)
#define BHND_NVSTORE_LOCK_ASSERT(sc, what)
#endif
#endif