2 * Copyright (c) 2015-2016 Landon Fuller <landonf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/queue.h>
39 #include <sys/ctype.h>
40 #include <sys/systm.h>
42 #include <machine/_inttypes.h>
57 #include "bhnd_nvram_private.h"
58 #include "bhnd_nvram_datavar.h"
60 #include "bhnd_nvram_storevar.h"
62 static int bhnd_nvstore_idx_cmp(void *ctx,
63 const void *lhs, const void *rhs);
66 * Allocate and initialize a new path instance.
68 * The caller is responsible for deallocating the instance via
69 * bhnd_nvstore_path_free().
71 * @param path_str The path's canonical string representation.
72 * @param path_len The length of @p path_str.
74 * @retval non-NULL success
75 * @retval NULL if allocation fails.
78 bhnd_nvstore_path_new(const char *path_str, size_t path_len)
80 bhnd_nvstore_path *path;
82 /* Allocate new entry */
83 path = bhnd_nv_malloc(sizeof(*path));
90 path->pending = bhnd_nvram_plist_new();
91 if (path->pending == NULL)
94 path->path_str = bhnd_nv_strndup(path_str, path_len);
95 if (path->path_str == NULL)
101 if (path->pending != NULL)
102 bhnd_nvram_plist_release(path->pending);
104 if (path->path_str != NULL)
105 bhnd_nv_free(path->path_str);
113 * Free an NVRAM path instance, releasing all associated resources.
116 bhnd_nvstore_path_free(struct bhnd_nvstore_path *path)
118 /* Free the per-path index */
119 if (path->index != NULL)
120 bhnd_nvstore_index_free(path->index);
122 bhnd_nvram_plist_release(path->pending);
123 bhnd_nv_free(path->path_str);
128 * Allocate and initialize a new index instance with @p capacity.
130 * The caller is responsible for deallocating the instance via
131 * bhnd_nvstore_index_free().
133 * @param capacity The maximum number of variables to be indexed.
135 * @retval non-NULL success
136 * @retval NULL if allocation fails.
139 bhnd_nvstore_index_new(size_t capacity)
141 bhnd_nvstore_index *index;
144 /* Allocate and populate variable index */
145 bytes = sizeof(struct bhnd_nvstore_index) + (sizeof(void *) * capacity);
146 index = bhnd_nv_malloc(bytes);
148 BHND_NV_LOG("error allocating %zu byte index\n", bytes);
153 index->capacity = capacity;
159 * Free an index instance, releasing all associated resources.
161 * @param index An index instance previously allocated via
162 * bhnd_nvstore_index_new().
165 bhnd_nvstore_index_free(bhnd_nvstore_index *index)
171 * Append a new NVRAM variable's @p cookiep value to @p index.
173 * After one or more append requests, the index must be prepared via
174 * bhnd_nvstore_index_prepare() before any indexed lookups are performed.
176 * @param sc The NVRAM store from which NVRAM values will be queried.
177 * @param index The index to be modified.
178 * @param cookiep The cookiep value (as provided by the backing NVRAM
179 * data instance of @p sc) to be included in @p index.
182 * @retval ENOMEM if appending an additional entry would exceed the
183 * capacity of @p index.
186 bhnd_nvstore_index_append(struct bhnd_nvram_store *sc,
187 bhnd_nvstore_index *index, void *cookiep)
189 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
191 if (index->count >= index->capacity)
194 index->cookiep[index->count] = cookiep;
199 /* sort function for bhnd_nvstore_index_prepare() */
201 bhnd_nvstore_idx_cmp(void *ctx, const void *lhs, const void *rhs)
203 struct bhnd_nvram_store *sc;
204 void *l_cookiep, *r_cookiep;
205 const char *l_str, *r_str;
206 const char *l_name, *r_name;
210 l_cookiep = *(void * const *)lhs;
211 r_cookiep = *(void * const *)rhs;
213 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
215 /* Fetch string pointers from the cookiep values */
216 l_str = bhnd_nvram_data_getvar_name(sc->data, l_cookiep);
217 r_str = bhnd_nvram_data_getvar_name(sc->data, r_cookiep);
219 /* Trim device path prefixes */
220 if (sc->data_caps & BHND_NVRAM_DATA_CAP_DEVPATHS) {
221 l_name = bhnd_nvram_trim_path_name(l_str);
222 r_name = bhnd_nvram_trim_path_name(r_str);
228 /* Perform comparison */
229 order = strcmp(l_name, r_name);
230 if (order != 0 || lhs == rhs)
233 /* If the backing data incorrectly contains variables with duplicate
234 * names, we need a sort order that provides stable behavior.
236 * Since Broadcom's own code varies wildly on this question, we just
237 * use a simple precedence rule: The first declaration of a variable
238 * takes precedence. */
239 return (bhnd_nvram_data_getvar_order(sc->data, l_cookiep, r_cookiep));
243 * Prepare @p index for querying via bhnd_nvstore_index_lookup().
245 * After one or more append requests, the index must be prepared via
246 * bhnd_nvstore_index_prepare() before any indexed lookups are performed.
248 * @param sc The NVRAM store from which NVRAM values will be queried.
249 * @param index The index to be prepared.
252 * @retval non-zero if preparing @p index otherwise fails, a regular unix
253 * error code will be returned.
256 bhnd_nvstore_index_prepare(struct bhnd_nvram_store *sc,
257 bhnd_nvstore_index *index)
259 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
261 /* Sort the index table */
262 qsort_r(index->cookiep, index->count, sizeof(index->cookiep[0]), sc,
263 bhnd_nvstore_idx_cmp);
269 * Return a borrowed reference to the root path node.
271 * @param sc The NVRAM store.
274 bhnd_nvstore_get_root_path(struct bhnd_nvram_store *sc)
276 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
277 return (sc->root_path);
281 * Return true if @p path is the root path node.
283 * @param sc The NVRAM store.
284 * @param path The path to query.
287 bhnd_nvstore_is_root_path(struct bhnd_nvram_store *sc, bhnd_nvstore_path *path)
289 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
290 return (sc->root_path == path);
294 * Return the update entry matching @p name in @p path, or NULL if no entry
297 * @param sc The NVRAM store.
298 * @param path The path to query.
299 * @param name The NVRAM variable name to search for in @p path's update list.
301 * @retval non-NULL success
302 * @retval NULL if @p name is not found in @p path.
305 bhnd_nvstore_path_get_update(struct bhnd_nvram_store *sc,
306 bhnd_nvstore_path *path, const char *name)
308 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
309 return (bhnd_nvram_plist_get_prop(path->pending, name));
313 * Register or remove an update record for @p name in @p path.
315 * @param sc The NVRAM store.
316 * @param path The path to be modified.
317 * @param name The path-relative variable name to be modified.
318 * @param value The new value. A value of BHND_NVRAM_TYPE_NULL denotes deletion.
321 * @retval ENOMEM if allocation fails.
322 * @retval ENOENT if @p name is unknown.
323 * @retval EINVAL if @p value is NULL, and deletion of @p is not
325 * @retval EINVAL if @p value cannot be converted to a supported value
329 bhnd_nvstore_path_register_update(struct bhnd_nvram_store *sc,
330 bhnd_nvstore_path *path, const char *name, bhnd_nvram_val *value)
332 bhnd_nvram_val *prop_val;
333 const char *full_name;
337 bool nvram_committed;
342 /* Determine whether the variable is currently defined in the
343 * backing NVRAM data, and derive its full path-prefixed name */
344 nvram_committed = false;
345 cookiep = bhnd_nvstore_path_data_lookup(sc, path, name);
346 if (cookiep != NULL) {
347 /* Variable is defined in the backing data */
348 nvram_committed = true;
350 /* Use the existing variable name */
351 full_name = bhnd_nvram_data_getvar_name(sc->data, cookiep);
352 } else if (path == sc->root_path) {
353 /* No prefix required for root path */
356 bhnd_nvstore_alias *alias;
359 /* New variable is being set; we need to determine the
360 * appropriate path prefix */
361 alias = bhnd_nvstore_find_alias(sc, path->path_str);
363 /* Use <alias>:name */
364 len = bhnd_nv_asprintf(&namebuf, "%lu:%s", alias->alias,
368 len = bhnd_nv_asprintf(&namebuf, "%s/%s",
369 path->path_str, name);
378 /* Allow the data store to filter the NVRAM operation */
379 if (bhnd_nvram_val_type(value) == BHND_NVRAM_TYPE_NULL) {
380 error = bhnd_nvram_data_filter_unsetvar(sc->data, full_name);
382 BHND_NV_LOG("cannot unset property %s: %d\n", full_name,
387 if ((prop_val = bhnd_nvram_val_copy(value)) == NULL) {
392 error = bhnd_nvram_data_filter_setvar(sc->data, full_name,
395 BHND_NV_LOG("cannot set property %s: %d\n", full_name,
401 /* Add relative variable name to the per-path update list */
402 if (bhnd_nvram_val_type(value) == BHND_NVRAM_TYPE_NULL &&
405 /* This is a deletion request for a variable not defined in
406 * out backing store; we can simply remove the corresponding
408 bhnd_nvram_plist_remove(path->pending, name);
410 /* Update or append a pending update entry */
411 error = bhnd_nvram_plist_replace_val(path->pending, name,
422 bhnd_nv_free(namebuf);
424 if (prop_val != NULL)
425 bhnd_nvram_val_release(prop_val);
431 * Iterate over all variable cookiep values retrievable from the backing
432 * data store in @p path.
434 * @warning Pending updates in @p path are ignored by this function.
436 * @param sc The NVRAM store.
437 * @param path The NVRAM path to be iterated.
438 * @param[in,out] indexp A pointer to an opaque indexp value previously
439 * returned by bhnd_nvstore_path_data_next(), or a
440 * NULL value to begin iteration.
442 * @return Returns the next variable name, or NULL if there are no more
443 * variables defined in @p path.
446 bhnd_nvstore_path_data_next(struct bhnd_nvram_store *sc,
447 bhnd_nvstore_path *path, void **indexp)
451 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
454 if (path->index == NULL) {
455 /* An index is required for all non-empty, non-root path
457 BHND_NV_ASSERT(bhnd_nvstore_is_root_path(sc, path),
458 ("missing index for non-root path %s", path->path_str));
460 /* Iterate NVRAM data directly, using the NVRAM data's cookiep
461 * value as our indexp context */
462 if ((bhnd_nvram_data_next(sc->data, indexp)) == NULL)
469 if (path->index->count == 0)
472 if (*indexp == NULL) {
473 /* First index entry */
474 index_ref = &path->index->cookiep[0];
478 /* Advance to next index entry */
482 /* Hit end of index? */
483 BHND_NV_ASSERT(index_ref > path->index->cookiep,
486 idxpos = (index_ref - path->index->cookiep);
487 if (idxpos >= path->index->count)
491 /* Provide new index position */
494 /* Return the data's cookiep value */
499 * Perform an lookup of @p name in the backing NVRAM data for @p path,
500 * returning the associated cookiep value, or NULL if the variable is not found
501 * in the backing NVRAM data.
503 * @warning Pending updates in @p path are ignored by this function.
505 * @param sc The NVRAM store from which NVRAM values will be queried.
506 * @param path The path to be queried.
507 * @param name The variable name to be queried.
509 * @retval non-NULL success
510 * @retval NULL if @p name is not found in @p index.
513 bhnd_nvstore_path_data_lookup(struct bhnd_nvram_store *sc,
514 bhnd_nvstore_path *path, const char *name)
516 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
519 if (path->index == NULL) {
520 /* An index is required for all non-empty, non-root path
522 BHND_NV_ASSERT(bhnd_nvstore_is_root_path(sc, path),
523 ("missing index for non-root path %s", path->path_str));
525 /* Look up directly in NVRAM data */
526 return (bhnd_nvram_data_find(sc->data, name));
529 /* Otherwise, delegate to an index-based lookup */
530 return (bhnd_nvstore_index_lookup(sc, path->index, name));
534 * Perform an index lookup of @p name, returning the associated cookiep
535 * value, or NULL if the variable does not exist.
537 * @param sc The NVRAM store from which NVRAM values will be queried.
538 * @param index The index to be queried.
539 * @param name The variable name to be queried.
541 * @retval non-NULL success
542 * @retval NULL if @p name is not found in @p index.
545 bhnd_nvstore_index_lookup(struct bhnd_nvram_store *sc,
546 bhnd_nvstore_index *index, const char *name)
549 const char *indexed_name;
550 size_t min, mid, max;
554 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
555 BHND_NV_ASSERT(index != NULL, ("NULL index"));
558 * Locate the requested variable using a binary search.
560 if (index->count == 0)
563 data_caps = sc->data_caps;
565 max = index->count - 1;
568 /* Select midpoint */
569 mid = (min + max) / 2;
570 cookiep = index->cookiep[mid];
572 /* Fetch variable name */
573 indexed_name = bhnd_nvram_data_getvar_name(sc->data, cookiep);
575 /* Trim any path prefix */
576 if (data_caps & BHND_NVRAM_DATA_CAP_DEVPATHS)
577 indexed_name = bhnd_nvram_trim_path_name(indexed_name);
579 /* Determine which side of the partition to search */
580 order = strcmp(indexed_name, name);
582 /* Search upper partition */
584 } else if (order > 0) {
585 /* Search (non-empty) lower partition */
589 } else if (order == 0) {
595 * If this happens to be a key with multiple definitions
596 * in the backing store, we need to find the entry with
597 * the highest declaration precedence.
599 * Duplicates are sorted in order of descending
600 * precedence; to find the highest precedence entry,
601 * we search backwards through the index.
606 const char *dup_name;
608 /* Fetch preceding index entry */
610 dup_cookiep = index->cookiep[idx];
611 dup_name = bhnd_nvram_data_getvar_name(sc->data,
614 /* Trim any path prefix */
615 if (data_caps & BHND_NVRAM_DATA_CAP_DEVPATHS) {
616 dup_name = bhnd_nvram_trim_path_name(
620 /* If no match, current cookiep is the variable
621 * definition with the highest precedence */
622 if (strcmp(indexed_name, dup_name) != 0)
625 /* Otherwise, prefer this earlier definition,
626 * and keep searching for a higher-precedence
628 cookiep = dup_cookiep;
640 * Return the device path entry registered for @p path, if any.
642 * @param sc The NVRAM store to be queried.
643 * @param path The device path to search for.
644 * @param path_len The length of @p path.
646 * @retval non-NULL if found.
647 * @retval NULL if not found.
650 bhnd_nvstore_get_path(struct bhnd_nvram_store *sc, const char *path,
653 bhnd_nvstore_path_list *plist;
654 bhnd_nvstore_path *p;
657 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
659 /* Use hash lookup */
660 h = hash32_strn(path, path_len, HASHINIT);
661 plist = &sc->paths[h % nitems(sc->paths)];
663 LIST_FOREACH(p, plist, np_link) {
664 /* Check for prefix match */
665 if (strncmp(p->path_str, path, path_len) != 0)
668 /* Check for complete match */
669 if (strnlen(path, path_len) != strlen(p->path_str))
680 * Resolve @p aval to its corresponding device path entry, if any.
682 * @param sc The NVRAM store to be queried.
683 * @param aval The device path alias value to search for.
685 * @retval non-NULL if found.
686 * @retval NULL if not found.
689 bhnd_nvstore_resolve_path_alias(struct bhnd_nvram_store *sc, u_long aval)
691 bhnd_nvstore_alias *alias;
693 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
695 /* Fetch alias entry */
696 if ((alias = bhnd_nvstore_get_alias(sc, aval)) == NULL)
699 return (alias->path);
703 * Register a device path entry for the path referenced by variable name
706 * @param sc The NVRAM store to be updated.
707 * @param info The NVRAM variable name info.
708 * @param cookiep The NVRAM variable's cookiep value.
710 * @retval 0 if the path was successfully registered, or an identical
711 * path or alias entry exists.
712 * @retval EEXIST if a conflicting entry already exists for the path or
713 * alias referenced by @p info.
714 * @retval ENOENT if @p info contains a dangling alias reference.
715 * @retval EINVAL if @p info contains an unsupported bhnd_nvstore_var_type
716 * and bhnd_nvstore_path_type combination.
717 * @retval ENOMEM if allocation fails.
720 bhnd_nvstore_var_register_path(struct bhnd_nvram_store *sc,
721 bhnd_nvstore_name_info *info, void *cookiep)
723 switch (info->type) {
724 case BHND_NVSTORE_VAR:
726 switch (info->path_type) {
727 case BHND_NVSTORE_PATH_STRING:
728 /* Variable contains a full path string
729 * (pci/1/1/varname); register the path */
730 return (bhnd_nvstore_register_path(sc,
731 info->path.str.value, info->path.str.value_len));
733 case BHND_NVSTORE_PATH_ALIAS:
734 /* Variable contains an alias reference (0:varname).
735 * There's no path to register */
739 BHND_NV_PANIC("unsupported path type %d", info->path_type);
742 case BHND_NVSTORE_ALIAS_DECL:
743 /* Alias declaration */
744 return (bhnd_nvstore_register_alias(sc, info, cookiep));
747 BHND_NV_PANIC("unsupported var type %d", info->type);
751 * Resolve the device path entry referenced referenced by @p info.
753 * @param sc The NVRAM store to be updated.
754 * @param info Variable name information descriptor containing
755 * the path or path alias to be resolved.
757 * @retval non-NULL if found.
758 * @retval NULL if not found.
761 bhnd_nvstore_var_get_path(struct bhnd_nvram_store *sc,
762 bhnd_nvstore_name_info *info)
764 switch (info->path_type) {
765 case BHND_NVSTORE_PATH_STRING:
766 return (bhnd_nvstore_get_path(sc, info->path.str.value,
767 info->path.str.value_len));
768 case BHND_NVSTORE_PATH_ALIAS:
769 return (bhnd_nvstore_resolve_path_alias(sc,
770 info->path.alias.value));
773 BHND_NV_PANIC("unsupported path type %d", info->path_type);
777 * Return the device path alias entry registered for @p alias_val, if any.
779 * @param sc The NVRAM store to be queried.
780 * @param alias_val The alias value to search for.
782 * @retval non-NULL if found.
783 * @retval NULL if not found.
786 bhnd_nvstore_get_alias(struct bhnd_nvram_store *sc, u_long alias_val)
788 bhnd_nvstore_alias_list *alist;
789 bhnd_nvstore_alias *alias;
791 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
793 /* Can use hash lookup */
794 alist = &sc->aliases[alias_val % nitems(sc->aliases)];
795 LIST_FOREACH(alias, alist, na_link) {
796 if (alias->alias == alias_val)
805 * Return the device path alias entry registered for @p path, if any.
807 * @param sc The NVRAM store to be queried.
808 * @param path The alias path to search for.
810 * @retval non-NULL if found.
811 * @retval NULL if not found.
814 bhnd_nvstore_find_alias(struct bhnd_nvram_store *sc, const char *path)
816 bhnd_nvstore_alias *alias;
818 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
820 /* Have to scan the full table */
821 for (size_t i = 0; i < nitems(sc->aliases); i++) {
822 LIST_FOREACH(alias, &sc->aliases[i], na_link) {
823 if (strcmp(alias->path->path_str, path) == 0)
833 * Register a device path entry for @p path.
835 * @param sc The NVRAM store to be updated.
836 * @param path_str The absolute device path string.
837 * @param path_len The length of @p path_str.
839 * @retval 0 if the path was successfully registered, or an identical
840 * path/alias entry already exists.
841 * @retval ENOMEM if allocation fails.
844 bhnd_nvstore_register_path(struct bhnd_nvram_store *sc, const char *path_str,
847 bhnd_nvstore_path_list *plist;
848 bhnd_nvstore_path *path;
851 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
853 /* Already exists? */
854 if (bhnd_nvstore_get_path(sc, path_str, path_len) != NULL)
857 /* Can't represent more than SIZE_MAX paths */
858 if (sc->num_paths == SIZE_MAX)
861 /* Allocate new entry */
862 path = bhnd_nvstore_path_new(path_str, path_len);
866 /* Insert in path hash table */
867 h = hash32_str(path->path_str, HASHINIT);
868 plist = &sc->paths[h % nitems(sc->paths)];
869 LIST_INSERT_HEAD(plist, path, np_link);
871 /* Increment path count */
878 * Register a device path alias for an NVRAM 'devpathX' variable.
880 * The path value for the alias will be fetched from the backing NVRAM data.
882 * @param sc The NVRAM store to be updated.
883 * @param info The NVRAM variable name info.
884 * @param cookiep The NVRAM variable's cookiep value.
886 * @retval 0 if the alias was successfully registered, or an
887 * identical alias entry exists.
888 * @retval EEXIST if a conflicting alias or path entry already exists.
889 * @retval EINVAL if @p info is not a BHND_NVSTORE_ALIAS_DECL or does
890 * not contain a BHND_NVSTORE_PATH_ALIAS entry.
891 * @retval ENOMEM if allocation fails.
894 bhnd_nvstore_register_alias(struct bhnd_nvram_store *sc,
895 const bhnd_nvstore_name_info *info, void *cookiep)
897 bhnd_nvstore_alias_list *alist;
898 bhnd_nvstore_alias *alias;
899 bhnd_nvstore_path *path;
904 BHND_NVSTORE_LOCK_ASSERT(sc, MA_OWNED);
909 /* Can't represent more than SIZE_MAX aliases */
910 if (sc->num_aliases == SIZE_MAX)
913 /* Must be an alias declaration */
914 if (info->type != BHND_NVSTORE_ALIAS_DECL)
917 if (info->path_type != BHND_NVSTORE_PATH_ALIAS)
920 /* Fetch the devpath variable's value length */
921 error = bhnd_nvram_data_getvar(sc->data, cookiep, NULL, &path_len,
922 BHND_NVRAM_TYPE_STRING);
926 /* Allocate path string buffer */
927 if ((path_str = bhnd_nv_malloc(path_len)) == NULL)
930 /* Decode to our new buffer */
931 error = bhnd_nvram_data_getvar(sc->data, cookiep, path_str, &path_len,
932 BHND_NVRAM_TYPE_STRING);
936 /* Trim trailing '/' character(s) from the path length */
937 path_len = strnlen(path_str, path_len);
938 while (path_len > 0 && path_str[path_len-1] == '/') {
939 path_str[path_len-1] = '\0';
943 /* Is a conflicting alias entry already registered for this alias
945 alias = bhnd_nvstore_get_alias(sc, info->path.alias.value);
947 if (alias->cookiep != cookiep ||
948 strcmp(alias->path->path_str, path_str) != 0)
955 /* Is a conflicting entry already registered for the alias path? */
956 if ((alias = bhnd_nvstore_find_alias(sc, path_str)) != NULL) {
957 if (alias->alias != info->path.alias.value ||
958 alias->cookiep != cookiep ||
959 strcmp(alias->path->path_str, path_str) != 0)
966 /* Get (or register) the target path entry */
967 path = bhnd_nvstore_get_path(sc, path_str, path_len);
969 error = bhnd_nvstore_register_path(sc, path_str, path_len);
973 path = bhnd_nvstore_get_path(sc, path_str, path_len);
974 BHND_NV_ASSERT(path != NULL, ("missing registered path"));
977 /* Allocate alias entry */
978 alias = bhnd_nv_calloc(1, sizeof(*alias));
985 alias->cookiep = cookiep;
986 alias->alias = info->path.alias.value;
988 /* Insert in alias hash table */
989 alist = &sc->aliases[alias->alias % nitems(sc->aliases)];
990 LIST_INSERT_HEAD(alist, alias, na_link);
992 /* Increment alias count */
995 bhnd_nv_free(path_str);
999 if (path_str != NULL)
1000 bhnd_nv_free(path_str);
1003 bhnd_nv_free(alias);
1009 * If @p child is equal to or a child path of @p parent, return a pointer to
1010 * @p child's path component(s) relative to @p parent; otherwise, return NULL.
1013 bhnd_nvstore_parse_relpath(const char *parent, const char *child)
1017 /* All paths have an implicit leading '/'; this allows us to treat
1018 * our manufactured root path of "/" as a prefix to all NVRAM-defined
1019 * paths (which do not necessarily include a leading '/' */
1026 /* Is parent a prefix of child? */
1027 prefix_len = strlen(parent);
1028 if (strncmp(parent, child, prefix_len) != 0)
1031 /* A zero-length prefix matches everything */
1032 if (prefix_len == 0)
1035 /* Is child equal to parent? */
1036 if (child[prefix_len] == '\0')
1037 return (child + prefix_len);
1039 /* Is child actually a child of parent? */
1040 if (child[prefix_len] == '/')
1041 return (child + prefix_len + 1);
1043 /* No match (e.g. parent=/foo..., child=/fooo...) */
1048 * Parse a raw NVRAM variable name and return its @p entry_type, its
1049 * type-specific @p prefix (e.g. '0:', 'pci/1/1', 'devpath'), and its
1050 * type-specific @p suffix (e.g. 'varname', '0').
1052 * @param name The NVRAM variable name to be parsed. This
1053 * value must remain valid for the lifetime of
1055 * @param type The NVRAM name type -- either INTERNAL for names
1056 * parsed from backing NVRAM data, or EXTERNAL for
1057 * names provided by external NVRAM store clients.
1058 * @param data_caps The backing NVRAM data capabilities
1059 * (see bhnd_nvram_data_caps()).
1060 * @param[out] info On success, the parsed variable name info.
1063 * @retval non-zero if parsing @p name otherwise fails, a regular unix
1064 * error code will be returned.
1067 bhnd_nvstore_parse_name_info(const char *name, bhnd_nvstore_name_type type,
1068 uint32_t data_caps, bhnd_nvstore_name_info *info)
1073 /* Skip path parsing? */
1074 if (data_caps & BHND_NVRAM_DATA_CAP_DEVPATHS) {
1075 /* devpath declaration? (devpath0=pci/1/1) */
1076 if (strncmp(name, "devpath", strlen("devpath")) == 0) {
1079 /* Perform standard validation on the relative
1081 if (type != BHND_NVSTORE_NAME_INTERNAL &&
1082 !bhnd_nvram_validate_name(name))
1087 /* Parse alias value that should follow a 'devpath'
1089 p = name + strlen("devpath");
1090 alias = strtoul(p, &endp, 10);
1091 if (endp != p && *endp == '\0') {
1092 info->type = BHND_NVSTORE_ALIAS_DECL;
1093 info->path_type = BHND_NVSTORE_PATH_ALIAS;
1095 info->path.alias.value = alias;
1101 /* device aliased variable? (0:varname) */
1102 if (bhnd_nv_isdigit(*name)) {
1105 /* Parse '0:' alias prefix */
1106 alias = strtoul(name, &endp, 10);
1107 if (endp != name && *endp == ':') {
1108 /* Perform standard validation on the relative
1110 if (type != BHND_NVSTORE_NAME_INTERNAL &&
1111 !bhnd_nvram_validate_name(name))
1116 info->type = BHND_NVSTORE_VAR;
1117 info->path_type = BHND_NVSTORE_PATH_ALIAS;
1119 /* name follows 0: prefix */
1120 info->name = endp + 1;
1121 info->path.alias.value = alias;
1127 /* device variable? (pci/1/1/varname) */
1128 if ((p = strrchr(name, '/')) != NULL) {
1129 const char *path, *relative_name;
1132 /* Determine the path length; 'p' points at the last
1133 * path separator in 'name' */
1134 path_len = p - name;
1137 /* The relative variable name directly follows the
1138 * final path separator '/' */
1139 relative_name = path + path_len + 1;
1141 /* Now that we calculated the name offset, exclude all
1142 * trailing '/' characters from the path length */
1143 while (path_len > 0 && path[path_len-1] == '/')
1146 /* Perform standard validation on the relative
1148 if (type != BHND_NVSTORE_NAME_INTERNAL &&
1149 !bhnd_nvram_validate_name(relative_name))
1154 /* Initialize result with pointers into the name
1156 info->type = BHND_NVSTORE_VAR;
1157 info->path_type = BHND_NVSTORE_PATH_STRING;
1158 info->name = relative_name;
1159 info->path.str.value = path;
1160 info->path.str.value_len = path_len;
1166 /* If all other parsing fails, the result is a simple variable with
1167 * an implicit path of "/" */
1168 if (type != BHND_NVSTORE_NAME_INTERNAL &&
1169 !bhnd_nvram_validate_name(name))
1171 /* Invalid relative name */
1175 info->type = BHND_NVSTORE_VAR;
1176 info->path_type = BHND_NVSTORE_PATH_STRING;
1178 info->path.str.value = BHND_NVSTORE_ROOT_PATH;
1179 info->path.str.value_len = BHND_NVSTORE_ROOT_PATH_LEN;