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$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <machine/_inttypes.h>
49 #include "bhnd_nvram_private.h"
50 #include "bhnd_nvram_io.h"
52 #include "bhnd_nvram_datavar.h"
53 #include "bhnd_nvram_data.h"
56 * Return a human-readable description for the given NVRAM data class.
58 * @param cls The NVRAM class.
61 bhnd_nvram_data_class_desc(bhnd_nvram_data_class *cls)
67 * Return the class-level capability flags (@see BHND_NVRAM_DATA_CAP_*) for
70 * @param cls The NVRAM class.
73 bhnd_nvram_data_class_caps(bhnd_nvram_data_class *cls)
79 * Serialize all NVRAM properties in @p plist using @p cls's NVRAM data
80 * format, writing the result to @p outp.
82 * @param cls The NVRAM data class to be used to perform
84 * @param props The raw property values to be serialized to
85 * @p outp, in serialization order.
86 * @param options Serialization options for @p cls, or NULL.
87 * @param[out] outp On success, the serialed NVRAM data will be
88 * written to this buffer. This argment may be
89 * NULL if the value is not desired.
90 * @param[in,out] olen The capacity of @p buf. On success, will be set
91 * to the actual length of the serialized data.
95 * @retval ENOMEM If @p outp is non-NULL and a buffer of @p olen is too
96 * small to hold the serialized data.
97 * @retval EINVAL If a property value required by @p cls is not found in
99 * @retval EFTYPE If a property value in @p plist cannot be represented
100 * as the data type required by @p cls.
101 * @retval ERANGE If a property value in @p plist would would overflow
102 * (or underflow) the data type required by @p cls.
103 * @retval non-zero If serialization otherwise fails, a regular unix error
104 * code will be returned.
107 bhnd_nvram_data_serialize(bhnd_nvram_data_class *cls,
108 bhnd_nvram_plist *props, bhnd_nvram_plist *options, void *outp,
111 return (cls->op_serialize(cls, props, options, outp, olen));
115 * Probe to see if this NVRAM data class class supports the data mapped by the
116 * given I/O context, returning a BHND_NVRAM_DATA_PROBE probe result.
118 * @param cls The NVRAM class.
119 * @param io An I/O context mapping the NVRAM data.
121 * @retval 0 if this is the only possible NVRAM data class for @p io.
122 * @retval negative if the probe succeeds, a negative value should be returned;
123 * the class returning the highest negative value should be selected to handle
125 * @retval ENXIO If the NVRAM format is not handled by @p cls.
126 * @retval positive if an error occurs during probing, a regular unix error
127 * code should be returned.
130 bhnd_nvram_data_probe(bhnd_nvram_data_class *cls, struct bhnd_nvram_io *io)
132 return (cls->op_probe(io));
136 * Probe to see if an NVRAM data class in @p classes supports parsing
137 * of the data mapped by @p io, returning the parsed data in @p data.
139 * The caller is responsible for deallocating the returned instance via
140 * bhnd_nvram_data_release().
142 * @param[out] data On success, the parsed NVRAM data instance.
143 * @param io An I/O context mapping the NVRAM data to be copied and parsed.
144 * @param classes An array of NVRAM data classes to be probed, or NULL to
145 * probe the default supported set.
146 * @param num_classes The number of NVRAM data classes in @p classes.
149 * @retval ENXIO if no class is found capable of parsing @p io.
150 * @retval non-zero if an error otherwise occurs during allocation,
151 * initialization, or parsing of the NVRAM data, a regular unix error code
155 bhnd_nvram_data_probe_classes(struct bhnd_nvram_data **data,
156 struct bhnd_nvram_io *io, bhnd_nvram_data_class *classes[],
159 bhnd_nvram_data_class *cls;
160 int error, prio, result;
166 /* If class array is NULL, default to our linker set */
167 if (classes == NULL) {
168 classes = SET_BEGIN(bhnd_nvram_data_class_set);
169 num_classes = SET_COUNT(bhnd_nvram_data_class_set);
172 /* Try to find the best data class capable of parsing io */
173 for (size_t i = 0; i < num_classes; i++) {
174 bhnd_nvram_data_class *next_cls;
176 next_cls = classes[i];
179 result = bhnd_nvram_data_probe(next_cls, io);
181 /* The parser did not match if an error was returned */
185 /* Lower priority than previous match; keep
187 if (cls != NULL && result <= prio)
190 /* Drop any previously parsed data */
192 bhnd_nvram_data_release(*data);
196 /* If this is a 'maybe' match, attempt actual parsing to
197 * verify that this does in fact match */
198 if (result <= BHND_NVRAM_DATA_PROBE_MAYBE) {
199 /* If parsing fails, keep searching */
200 error = bhnd_nvram_data_new(next_cls, data, io);
205 /* Record best new match */
209 /* Terminate search immediately on
210 * BHND_NVRAM_DATA_PROBE_SPECIFIC */
211 if (result == BHND_NVRAM_DATA_PROBE_SPECIFIC)
215 /* If no match, return error */
219 /* If the NVRAM data was not parsed above, do so now */
221 if ((error = bhnd_nvram_data_new(cls, data, io)))
229 * Read a variable directly from @p io and decode as @p type.
231 * This may be used to perform reading of NVRAM variables during the very
232 * early boot process, prior to the availability of the kernel allocator.
234 * @param cls An NVRAM class capable of parsing @p io.
235 * @param io NVRAM data to be parsed.
236 * @param name The raw name of the variable to be fetched,
237 * including any device path (/pci/1/1/varname) or
238 * alias prefix (0:varname).
239 * @param[out] buf On success, the requested value will be written
240 * to this buffer. This argment may be NULL if
241 * the value is not desired.
242 * @param[in,out] len The capacity of @p buf. On success, will be set
243 * to the actual size of the requested value.
244 * @param type The data type to be written to @p buf.
247 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
248 * small to hold the requested value.
249 * @retval ENOENT If @p name is not found in @p io.
250 * @retval EFTYPE If the variable data cannot be coerced to @p type.
251 * @retval ERANGE If value coercion would overflow @p type.
252 * @retval non-zero If parsing @p io otherwise fails, a regular unix error
253 * code will be returned.
256 bhnd_nvram_data_getvar_direct(bhnd_nvram_data_class *cls,
257 struct bhnd_nvram_io *io, const char *name, void *buf, size_t *len,
258 bhnd_nvram_type type)
260 return (cls->op_getvar_direct(io, name, buf, len, type));
264 * Allocate and initialize a new instance of data class @p cls, copying and
265 * parsing NVRAM data from @p io.
267 * The caller is responsible for releasing the returned parser instance
268 * reference via bhnd_nvram_data_release().
270 * @param cls If non-NULL, the data class to be allocated. If NULL,
271 * bhnd_nvram_data_probe_classes() will be used to determine the data format.
272 * @param[out] nv On success, a pointer to the newly allocated NVRAM data instance.
273 * @param io An I/O context mapping the NVRAM data to be copied and parsed.
276 * @retval non-zero if an error occurs during allocation or initialization, a
277 * regular unix error code will be returned.
280 bhnd_nvram_data_new(bhnd_nvram_data_class *cls, struct bhnd_nvram_data **nv,
281 struct bhnd_nvram_io *io)
283 struct bhnd_nvram_data *data;
286 /* If NULL, try to identify the appropriate class */
288 return (bhnd_nvram_data_probe_classes(nv, io, NULL, 0));
290 /* Allocate new instance */
291 BHND_NV_ASSERT(sizeof(struct bhnd_nvram_data) <= cls->size,
292 ("instance size %zu less than minimum %zu", cls->size,
293 sizeof(struct bhnd_nvram_data)));
295 data = bhnd_nv_calloc(1, cls->size);
297 refcount_init(&data->refs, 1);
299 /* Let the class handle initialization */
300 if ((error = cls->op_new(data, io))) {
310 * Retain and return a reference to the given data instance.
312 * @param nv The reference to be retained.
314 struct bhnd_nvram_data *
315 bhnd_nvram_data_retain(struct bhnd_nvram_data *nv)
317 refcount_acquire(&nv->refs);
322 * Release a reference to the given data instance.
324 * If this is the last reference, the data instance and its associated
325 * resources will be freed.
327 * @param nv The reference to be released.
330 bhnd_nvram_data_release(struct bhnd_nvram_data *nv)
332 if (!refcount_release(&nv->refs))
335 /* Free any internal resources */
336 nv->cls->op_free(nv);
338 /* Free the instance allocation */
343 * Return a pointer to @p nv's data class.
345 * @param nv The NVRAM data instance to be queried.
347 bhnd_nvram_data_class *
348 bhnd_nvram_data_get_class(struct bhnd_nvram_data *nv)
354 * Return the number of variables in @p nv.
356 * @param nv The NVRAM data to be queried.
359 bhnd_nvram_data_count(struct bhnd_nvram_data *nv)
361 return (nv->cls->op_count(nv));
365 * Return a borrowed reference to the serialization options for @p nv,
366 * suitable for use with bhnd_nvram_data_serialize(), or NULL if none.
368 * @param nv The NVRAM data to be queried.
371 bhnd_nvram_data_options(struct bhnd_nvram_data *nv)
373 return (nv->cls->op_options(nv));
377 * Return the capability flags (@see BHND_NVRAM_DATA_CAP_*) for @p nv.
379 * @param nv The NVRAM data to be queried.
382 bhnd_nvram_data_caps(struct bhnd_nvram_data *nv)
384 return (nv->cls->op_caps(nv));
388 * Iterate over @p nv, returning the names of subsequent variables.
390 * @param nv The NVRAM data to be iterated.
391 * @param[in,out] cookiep A pointer to a cookiep value previously returned
392 * by bhnd_nvram_data_next(), or a NULL value to
395 * @return Returns the next variable name, or NULL if there are no more
396 * variables defined in @p nv.
399 bhnd_nvram_data_next(struct bhnd_nvram_data *nv, void **cookiep)
402 #ifdef BHND_NV_INVARIANTS
403 void *prev = *cookiep;
407 if ((name = nv->cls->op_next(nv, cookiep)) == NULL)
410 /* Enforce precedence ordering invariant between bhnd_nvram_data_next()
411 * and bhnd_nvram_data_getvar_order() */
412 #ifdef BHND_NV_INVARIANTS
414 bhnd_nvram_data_getvar_order(nv, prev, *cookiep) > 0)
416 BHND_NV_PANIC("%s: returned out-of-order entry", __FUNCTION__);
424 * Search @p nv for a named variable, returning the variable's opaque reference
425 * if found, or NULL if unavailable.
427 * The BHND_NVRAM_DATA_CAP_INDEXED capability flag will be returned by
428 * bhnd_nvram_data_caps() if @p nv supports effecient name-based
431 * @param nv The NVRAM data to search.
432 * @param name The name to search for.
434 * @retval non-NULL If @p name is found, the opaque cookie value will be
436 * @retval NULL If @p name is not found.
439 bhnd_nvram_data_find(struct bhnd_nvram_data *nv, const char *name)
441 return (nv->cls->op_find(nv, name));
445 * A generic implementation of bhnd_nvram_data_find().
447 * This implementation will use bhnd_nvram_data_next() to perform a
448 * simple O(n) case-insensitve search for @p name.
451 bhnd_nvram_data_generic_find(struct bhnd_nvram_data *nv, const char *name)
457 while ((next = bhnd_nvram_data_next(nv, &cookiep))) {
458 if (strcmp(name, next) == 0)
467 * Compare the declaration order of two NVRAM variables.
469 * Variable declaration order is used to determine the current order of
470 * the variables in the source data, as well as to determine the precedence
471 * of variable declarations in data sources that define duplicate names.
473 * The comparison order will match the order of variables returned via
474 * bhnd_nvstore_path_data_next().
476 * @param nv The NVRAM data.
477 * @param cookiep1 An NVRAM variable cookie previously
478 * returned via bhnd_nvram_data_next() or
479 * bhnd_nvram_data_find().
480 * @param cookiep2 An NVRAM variable cookie previously
481 * returned via bhnd_nvram_data_next() or
482 * bhnd_nvram_data_find().
484 * @retval <= -1 If @p cookiep1 has an earlier declaration order than
486 * @retval 0 If @p cookiep1 and @p cookiep2 are identical.
487 * @retval >= 1 If @p cookiep has a later declaration order than
491 bhnd_nvram_data_getvar_order(struct bhnd_nvram_data *nv, void *cookiep1,
494 return (nv->cls->op_getvar_order(nv, cookiep1, cookiep2));
498 * Read a variable and decode as @p type.
500 * @param nv The NVRAM data.
501 * @param cookiep An NVRAM variable cookie previously returned
502 * via bhnd_nvram_data_next() or
503 * bhnd_nvram_data_find().
504 * @param[out] buf On success, the requested value will be written
505 * to this buffer. This argment may be NULL if
506 * the value is not desired.
507 * @param[in,out] len The capacity of @p buf. On success, will be set
508 * to the actual size of the requested value.
509 * @param type The data type to be written to @p buf.
512 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
513 * small to hold the requested value.
514 * @retval EFTYPE If the variable data cannot be coerced to @p type.
515 * @retval ERANGE If value coercion would overflow @p type.
518 bhnd_nvram_data_getvar(struct bhnd_nvram_data *nv, void *cookiep, void *buf,
519 size_t *len, bhnd_nvram_type type)
521 return (nv->cls->op_getvar(nv, cookiep, buf, len, type));
525 * Common bhnd_nvram_data_getvar_ptr() wrapper used by
526 * bhnd_nvram_data_generic_rp_getvar() and
527 * bhnd_nvram_data_generic_rp_copy_val().
529 * If a variable definition for the requested variable is found via
530 * bhnd_nvram_find_vardefn(), the definition will be used to populate fmt.
533 bhnd_nvram_data_getvar_ptr_info(struct bhnd_nvram_data *nv, void *cookiep,
534 size_t *len, bhnd_nvram_type *type, const bhnd_nvram_val_fmt **fmt)
536 const struct bhnd_nvram_vardefn *vdefn;
540 BHND_NV_ASSERT(bhnd_nvram_data_caps(nv) & BHND_NVRAM_DATA_CAP_READ_PTR,
541 ("instance does not advertise READ_PTR support"));
543 /* Fetch pointer to variable data */
544 vptr = bhnd_nvram_data_getvar_ptr(nv, cookiep, len, type);
548 /* Select a default value format implementation */
550 /* Fetch the reference variable name */
551 name = bhnd_nvram_data_getvar_name(nv, cookiep);
553 /* Trim path prefix, if any; the Broadcom NVRAM format assumes a global
554 * namespace for all variable definitions */
555 if (bhnd_nvram_data_caps(nv) & BHND_NVRAM_DATA_CAP_DEVPATHS)
556 name = bhnd_nvram_trim_path_name(name);
558 /* Check the variable definition table for a matching entry; if
559 * it exists, use it to populate the value format. */
560 vdefn = bhnd_nvram_find_vardefn(name);
562 BHND_NV_ASSERT(vdefn->fmt != NULL,
563 ("NULL format for %s", name));
565 } else if (*type == BHND_NVRAM_TYPE_STRING) {
566 /* Default to Broadcom-specific string interpretation */
567 *fmt = &bhnd_nvram_val_bcm_string_fmt;
569 /* Fall back on native formatting */
570 *fmt = bhnd_nvram_val_default_fmt(*type);
577 * A generic implementation of bhnd_nvram_data_getvar().
579 * This implementation will call bhnd_nvram_data_getvar_ptr() to fetch
580 * a pointer to the variable data and perform data coercion on behalf
583 * If a variable definition for the requested variable is available via
584 * bhnd_nvram_find_vardefn(), the definition will be used to provide a
585 * formatting instance to bhnd_nvram_val_init().
588 bhnd_nvram_data_generic_rp_getvar(struct bhnd_nvram_data *nv, void *cookiep,
589 void *outp, size_t *olen, bhnd_nvram_type otype)
592 const bhnd_nvram_val_fmt *fmt;
594 bhnd_nvram_type vtype;
598 BHND_NV_ASSERT(bhnd_nvram_data_caps(nv) & BHND_NVRAM_DATA_CAP_READ_PTR,
599 ("instance does not advertise READ_PTR support"));
601 /* Fetch variable data and value format*/
602 vptr = bhnd_nvram_data_getvar_ptr_info(nv, cookiep, &vlen, &vtype,
607 /* Attempt value coercion */
608 error = bhnd_nvram_val_init(&val, fmt, vptr, vlen, vtype,
609 BHND_NVRAM_VAL_BORROW_DATA);
613 error = bhnd_nvram_val_encode(&val, outp, olen, otype);
616 bhnd_nvram_val_release(&val);
621 * Return a caller-owned copy of an NVRAM entry's variable data.
623 * The caller is responsible for deallocating the returned value via
624 * bhnd_nvram_val_release().
626 * @param nv The NVRAM data.
627 * @param cookiep An NVRAM variable cookie previously returned
628 * via bhnd_nvram_data_next() or bhnd_nvram_data_find().
629 * @param[out] value On success, the caller-owned value instance.
632 * @retval ENOMEM If allocation fails.
633 * @retval non-zero If initialization of the value otherwise fails, a
634 * regular unix error code will be returned.
637 bhnd_nvram_data_copy_val(struct bhnd_nvram_data *nv, void *cookiep,
638 bhnd_nvram_val **value)
640 return (nv->cls->op_copy_val(nv, cookiep, value));
644 * A generic implementation of bhnd_nvram_data_copy_val().
646 * This implementation will call bhnd_nvram_data_getvar_ptr() to fetch
647 * a pointer to the variable data and perform data coercion on behalf
650 * If a variable definition for the requested variable is available via
651 * bhnd_nvram_find_vardefn(), the definition will be used to provide a
652 * formatting instance to bhnd_nvram_val_init().
655 bhnd_nvram_data_generic_rp_copy_val(struct bhnd_nvram_data *nv,
656 void *cookiep, bhnd_nvram_val **value)
658 const bhnd_nvram_val_fmt *fmt;
660 bhnd_nvram_type vtype;
663 BHND_NV_ASSERT(bhnd_nvram_data_caps(nv) & BHND_NVRAM_DATA_CAP_READ_PTR,
664 ("instance does not advertise READ_PTR support"));
666 /* Fetch variable data and value format*/
667 vptr = bhnd_nvram_data_getvar_ptr_info(nv, cookiep, &vlen, &vtype,
672 /* Allocate and return the new value instance */
673 return (bhnd_nvram_val_new(value, fmt, vptr, vlen, vtype,
674 BHND_NVRAM_VAL_DYNAMIC));
678 * If available and supported by the NVRAM data instance, return a reference
679 * to the internal buffer containing an entry's variable data,
681 * Note that string values may not be NUL terminated.
683 * @param nv The NVRAM data.
684 * @param cookiep An NVRAM variable cookie previously returned
685 * via bhnd_nvram_data_next() or
686 * bhnd_nvram_data_find().
687 * @param[out] len On success, will be set to the actual size of
688 * the requested value.
689 * @param[out] type The data type of the entry data.
691 * @retval non-NULL success
692 * @retval NULL if direct data access is unsupported by @p nv, or
693 * unavailable for @p cookiep.
696 bhnd_nvram_data_getvar_ptr(struct bhnd_nvram_data *nv, void *cookiep,
697 size_t *len, bhnd_nvram_type *type)
699 return (nv->cls->op_getvar_ptr(nv, cookiep, len, type));
703 * Return the variable name associated with a given @p cookiep.
704 * @param nv The NVRAM data to be iterated.
705 * @param[in,out] cookiep A pointer to a cookiep value previously returned
706 * via bhnd_nvram_data_next() or
707 * bhnd_nvram_data_find().
709 * @return Returns the variable's name.
712 bhnd_nvram_data_getvar_name(struct bhnd_nvram_data *nv, void *cookiep)
714 return (nv->cls->op_getvar_name(nv, cookiep));
718 * Filter a request to set variable @p name with @p value.
720 * On success, the caller owns a reference to @p result, and must release
721 * any held resources via bhnd_nvram_val_release().
723 * @param nv The NVRAM data instance.
724 * @param name The name of the variable to be set.
725 * @param value The proposed value to be set.
726 * @param[out] result On success, a caller-owned reference to the filtered
730 * @retval ENOENT if @p name is unrecognized by @p nv.
731 * @retval EINVAL if @p name is read-only.
732 * @retval EINVAL if @p value cannot be converted to the required value
736 bhnd_nvram_data_filter_setvar(struct bhnd_nvram_data *nv, const char *name,
737 bhnd_nvram_val *value, bhnd_nvram_val **result)
739 return (nv->cls->op_filter_setvar(nv, name, value, result));
743 * Filter a request to delete variable @p name.
745 * @param nv The NVRAM data instance.
746 * @param name The name of the variable to be deleted.
749 * @retval ENOENT if @p name is unrecognized by @p nv.
750 * @retval EINVAL if @p name is read-only.
753 bhnd_nvram_data_filter_unsetvar(struct bhnd_nvram_data *nv, const char *name)
755 return (nv->cls->op_filter_unsetvar(nv, name));