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 <net/ethernet.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
44 #include <machine/_inttypes.h>
56 #include "bhnd_nvram_private.h"
58 #include "bhnd_nvram_valuevar.h"
60 static bool bhnd_nvram_ident_octet_string(const char *inp,
61 size_t ilen, char *delim, size_t *nelem);
62 static bool bhnd_nvram_ident_num_string(const char *inp,
63 size_t ilen, u_int base, u_int *obase);
65 static int bhnd_nvram_val_bcm_macaddr_filter(
66 const bhnd_nvram_val_fmt **fmt, const void *inp,
67 size_t ilen, bhnd_nvram_type itype);
68 static int bhnd_nvram_val_bcm_macaddr_encode(
69 bhnd_nvram_val *value, void *outp, size_t *olen,
70 bhnd_nvram_type otype);
72 static int bhnd_nvram_val_bcm_macaddr_string_filter(
73 const bhnd_nvram_val_fmt **fmt, const void *inp,
74 size_t ilen, bhnd_nvram_type itype);
75 static int bhnd_nvram_val_bcm_macaddr_string_encode_elem(
76 bhnd_nvram_val *value, const void *inp,
77 size_t ilen, void *outp, size_t *olen,
78 bhnd_nvram_type otype);
79 static const void *bhnd_nvram_val_bcm_macaddr_string_next(
80 bhnd_nvram_val *value, const void *prev,
84 static int bhnd_nvram_val_bcm_int_filter(
85 const bhnd_nvram_val_fmt **fmt, const void *inp,
86 size_t ilen, bhnd_nvram_type itype);
87 static int bhnd_nvram_val_bcm_int_encode(bhnd_nvram_val *value,
88 void *outp, size_t *olen, bhnd_nvram_type otype);
90 static int bhnd_nvram_val_bcm_decimal_encode_elem(
91 bhnd_nvram_val *value, const void *inp,
92 size_t ilen, void *outp, size_t *olen,
93 bhnd_nvram_type otype);
94 static int bhnd_nvram_val_bcm_hex_encode_elem(
95 bhnd_nvram_val *value, const void *inp,
96 size_t ilen, void *outp, size_t *olen,
97 bhnd_nvram_type otype);
99 static int bhnd_nvram_val_bcm_leddc_filter(
100 const bhnd_nvram_val_fmt **fmt, const void *inp,
101 size_t ilen, bhnd_nvram_type itype);
102 static int bhnd_nvram_val_bcm_leddc_encode_elem(
103 bhnd_nvram_val *value, const void *inp,
104 size_t ilen, void *outp, size_t *olen,
105 bhnd_nvram_type otype);
108 static int bhnd_nvram_val_bcmstr_encode(bhnd_nvram_val *value,
109 void *outp, size_t *olen, bhnd_nvram_type otype);
111 static int bhnd_nvram_val_bcmstr_csv_filter(
112 const bhnd_nvram_val_fmt **fmt, const void *inp,
113 size_t ilen, bhnd_nvram_type itype);
114 static const void *bhnd_nvram_val_bcmstr_csv_next(bhnd_nvram_val *value,
115 const void *prev, size_t *len);
118 * Broadcom NVRAM MAC address format.
120 const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_macaddr_fmt = {
121 .name = "bcm-macaddr",
122 .native_type = BHND_NVRAM_TYPE_UINT8_ARRAY,
123 .op_filter = bhnd_nvram_val_bcm_macaddr_filter,
124 .op_encode = bhnd_nvram_val_bcm_macaddr_encode,
127 /** Broadcom NVRAM MAC address string format. */
128 static const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_macaddr_string_fmt = {
129 .name = "bcm-macaddr-string",
130 .native_type = BHND_NVRAM_TYPE_STRING,
131 .op_filter = bhnd_nvram_val_bcm_macaddr_string_filter,
132 .op_encode_elem = bhnd_nvram_val_bcm_macaddr_string_encode_elem,
133 .op_next = bhnd_nvram_val_bcm_macaddr_string_next,
137 * Broadcom NVRAM LED duty-cycle format.
139 const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_leddc_fmt = {
141 .native_type = BHND_NVRAM_TYPE_UINT32,
142 .op_filter = bhnd_nvram_val_bcm_leddc_filter,
143 .op_encode_elem = bhnd_nvram_val_bcm_leddc_encode_elem,
147 * Broadcom NVRAM decimal integer format.
149 * Extends standard integer handling, encoding the string representation of
150 * the integer value as a decimal string:
151 * - Positive values will be string-encoded without a prefix.
152 * - Negative values will be string-encoded with a leading '-' sign.
154 const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_decimal_fmt = {
155 .name = "bcm-decimal",
156 .native_type = BHND_NVRAM_TYPE_UINT64,
157 .op_filter = bhnd_nvram_val_bcm_int_filter,
158 .op_encode = bhnd_nvram_val_bcm_int_encode,
159 .op_encode_elem = bhnd_nvram_val_bcm_decimal_encode_elem,
163 * Broadcom NVRAM decimal integer format.
165 * Extends standard integer handling, encoding the string representation of
166 * unsigned and positive signed integer values as an 0x-prefixed hexadecimal
169 * For compatibility with standard Broadcom NVRAM parsing, if the integer is
170 * both signed and negative, it will be string encoded as a negative decimal
171 * value, not as a twos-complement hexadecimal value.
173 const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_hex_fmt = {
175 .native_type = BHND_NVRAM_TYPE_UINT64,
176 .op_filter = bhnd_nvram_val_bcm_int_filter,
177 .op_encode = bhnd_nvram_val_bcm_int_encode,
178 .op_encode_elem = bhnd_nvram_val_bcm_hex_encode_elem,
182 * Broadcom NVRAM string format.
184 * Handles standard, comma-delimited, and octet-string values as used in
185 * Broadcom NVRAM data.
187 const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_string_fmt = {
188 .name = "bcm-string",
189 .native_type = BHND_NVRAM_TYPE_STRING,
190 .op_encode = bhnd_nvram_val_bcmstr_encode,
193 /** Broadcom comma-delimited string. */
194 static const bhnd_nvram_val_fmt bhnd_nvram_val_bcm_string_csv_fmt = {
195 .name = "bcm-string[]",
196 .native_type = BHND_NVRAM_TYPE_STRING,
197 .op_filter = bhnd_nvram_val_bcmstr_csv_filter,
198 .op_next = bhnd_nvram_val_bcmstr_csv_next,
202 /* Built-in format definitions */
203 #define BHND_NVRAM_VAL_FMT_NATIVE(_n, _type) \
204 const bhnd_nvram_val_fmt bhnd_nvram_val_ ## _n ## _fmt = { \
205 .name = __STRING(_n), \
206 .native_type = BHND_NVRAM_TYPE_ ## _type, \
209 BHND_NVRAM_VAL_FMT_NATIVE(uint8, UINT8);
210 BHND_NVRAM_VAL_FMT_NATIVE(uint16, UINT16);
211 BHND_NVRAM_VAL_FMT_NATIVE(uint32, UINT32);
212 BHND_NVRAM_VAL_FMT_NATIVE(uint64, UINT64);
213 BHND_NVRAM_VAL_FMT_NATIVE(int8, INT8);
214 BHND_NVRAM_VAL_FMT_NATIVE(int16, INT16);
215 BHND_NVRAM_VAL_FMT_NATIVE(int32, INT32);
216 BHND_NVRAM_VAL_FMT_NATIVE(int64, INT64);
217 BHND_NVRAM_VAL_FMT_NATIVE(char, CHAR);
218 BHND_NVRAM_VAL_FMT_NATIVE(bool, BOOL);
219 BHND_NVRAM_VAL_FMT_NATIVE(string, STRING);
220 BHND_NVRAM_VAL_FMT_NATIVE(data, DATA);
221 BHND_NVRAM_VAL_FMT_NATIVE(null, NULL);
223 BHND_NVRAM_VAL_FMT_NATIVE(uint8_array, UINT8_ARRAY);
224 BHND_NVRAM_VAL_FMT_NATIVE(uint16_array, UINT16_ARRAY);
225 BHND_NVRAM_VAL_FMT_NATIVE(uint32_array, UINT32_ARRAY);
226 BHND_NVRAM_VAL_FMT_NATIVE(uint64_array, UINT64_ARRAY);
227 BHND_NVRAM_VAL_FMT_NATIVE(int8_array, INT8_ARRAY);
228 BHND_NVRAM_VAL_FMT_NATIVE(int16_array, INT16_ARRAY);
229 BHND_NVRAM_VAL_FMT_NATIVE(int32_array, INT32_ARRAY);
230 BHND_NVRAM_VAL_FMT_NATIVE(int64_array, INT64_ARRAY);
231 BHND_NVRAM_VAL_FMT_NATIVE(char_array, CHAR_ARRAY);
232 BHND_NVRAM_VAL_FMT_NATIVE(bool_array, BOOL_ARRAY);
233 BHND_NVRAM_VAL_FMT_NATIVE(string_array, STRING_ARRAY);
236 * Common hex/decimal integer filter implementation.
239 bhnd_nvram_val_bcm_int_filter(const bhnd_nvram_val_fmt **fmt, const void *inp,
240 size_t ilen, bhnd_nvram_type itype)
242 bhnd_nvram_type itype_base;
244 itype_base = bhnd_nvram_base_type(itype);
246 switch (itype_base) {
247 case BHND_NVRAM_TYPE_STRING:
249 * If the input is a string, delegate to the Broadcom
250 * string format -- preserving the original string value
251 * takes priority over enforcing hexadecimal/integer string
254 *fmt = &bhnd_nvram_val_bcm_string_fmt;
258 if (bhnd_nvram_is_int_type(itype_base))
266 * Broadcom hex/decimal integer encode implementation.
269 bhnd_nvram_val_bcm_int_encode(bhnd_nvram_val *value, void *outp, size_t *olen,
270 bhnd_nvram_type otype)
272 /* If encoding to a string, format multiple elements (if any) with a
273 * comma delimiter. */
274 if (otype == BHND_NVRAM_TYPE_STRING)
275 return (bhnd_nvram_val_printf(value, "%[]s", outp, olen, ","));
277 return (bhnd_nvram_val_generic_encode(value, outp, olen, otype));
281 * Broadcom hex integer encode_elem implementation.
284 bhnd_nvram_val_bcm_hex_encode_elem(bhnd_nvram_val *value, const void *inp,
285 size_t ilen, void *outp, size_t *olen, bhnd_nvram_type otype)
287 bhnd_nvram_type itype;
291 itype = bhnd_nvram_val_elem_type(value);
292 BHND_NV_ASSERT(bhnd_nvram_is_int_type(itype), ("invalid type"));
294 /* If not encoding as a string, perform generic value encoding */
295 if (otype != BHND_NVRAM_TYPE_STRING)
296 return (bhnd_nvram_val_generic_encode_elem(value, inp, ilen,
299 /* If the value is a signed, negative value, encode as a decimal
301 if (bhnd_nvram_is_signed_type(itype)) {
304 bhnd_nvram_type stype;
306 stype = BHND_NVRAM_TYPE_INT64;
309 /* Fetch 64-bit signed representation */
310 error = bhnd_nvram_value_coerce(inp, ilen, itype, &sval, &slen,
315 /* Decimal encoding required? */
317 return (bhnd_nvram_value_printf("%I64d", &sval, slen,
318 stype, outp, olen, otype));
322 * Encode the value as a hex string.
324 * Most producers of Broadcom NVRAM values zero-pad hex values out to
325 * their native width (width * two hex characters), and we do the same
328 width = bhnd_nvram_type_width(itype) * 2;
329 return (bhnd_nvram_value_printf("0x%0*I64X", inp, ilen, itype,
334 * Broadcom decimal integer encode_elem implementation.
337 bhnd_nvram_val_bcm_decimal_encode_elem(bhnd_nvram_val *value, const void *inp,
338 size_t ilen, void *outp, size_t *olen, bhnd_nvram_type otype)
341 bhnd_nvram_type itype;
343 itype = bhnd_nvram_val_elem_type(value);
344 BHND_NV_ASSERT(bhnd_nvram_is_int_type(itype), ("invalid type"));
346 /* If not encoding as a string, perform generic value encoding */
347 if (otype != BHND_NVRAM_TYPE_STRING)
348 return (bhnd_nvram_val_generic_encode_elem(value, inp, ilen,
351 sfmt = bhnd_nvram_is_signed_type(itype) ? "%I64d" : "%I64u";
352 return (bhnd_nvram_value_printf(sfmt, inp, ilen, itype, outp, olen));
356 * Broadcom LED duty-cycle filter.
359 bhnd_nvram_val_bcm_leddc_filter(const bhnd_nvram_val_fmt **fmt,
360 const void *inp, size_t ilen, bhnd_nvram_type itype)
366 case BHND_NVRAM_TYPE_UINT16:
367 case BHND_NVRAM_TYPE_UINT32:
370 case BHND_NVRAM_TYPE_STRING:
371 /* Trim any whitespace */
373 plen = bhnd_nvram_trim_field(&p, ilen, '\0');
375 /* If the value is not a valid integer string, delegate to the
376 * Broadcom string format */
377 if (!bhnd_nvram_ident_num_string(p, plen, 0, NULL))
378 *fmt = &bhnd_nvram_val_bcm_string_fmt;
387 * Broadcom LED duty-cycle encode.
390 bhnd_nvram_val_bcm_leddc_encode_elem(bhnd_nvram_val *value, const void *inp,
391 size_t ilen, void *outp, size_t *olen, bhnd_nvram_type otype)
393 bhnd_nvram_type itype;
394 size_t limit, nbytes;
405 * LED duty-cycle values represent the on/off periods as a 32-bit
406 * integer, with the top 16 bits representing on cycles, and the
407 * bottom 16 representing off cycles.
409 * LED duty cycle values have three different formats:
411 * - SPROM: A 16-bit unsigned integer, with on/off cycles encoded
413 * - NVRAM: A 16-bit decimal or hexadecimal string, with on/off
414 * cycles encoded as 8-bit values as per the SPROM format.
415 * - NVRAM: A 32-bit decimal or hexadecimal string, with on/off
416 * cycles encoded as 16-bit values.
418 * To convert from a 16-bit representation to a 32-bit representation:
419 * ((value & 0xFF00) << 16) | ((value & 0x00FF) << 8)
421 * To convert from a 32-bit representation to a 16-bit representation,
422 * perform the same operation in reverse, discarding the lower 8-bits
423 * of each half of the 32-bit representation:
424 * ((value >> 16) & 0xFF00) | ((value >> 8) & 0x00FF)
427 itype = bhnd_nvram_val_elem_type(value);
431 /* Determine output byte limit */
437 /* If the input/output types match, just delegate to standard value
438 * encoding support */
439 if (otype == itype) {
440 return (bhnd_nvram_value_coerce(inp, ilen, itype, outp, olen,
444 /* If our value is a string, it may either be a 16-bit or a 32-bit
445 * representation of the duty cycle */
446 if (itype == BHND_NVRAM_TYPE_STRING) {
451 /* Parse integer value */
454 error = bhnd_nvram_parse_int(p, ilen, 0, &parsed, &ival, &nlen,
455 BHND_NVRAM_TYPE_UINT32);
459 /* Trailing garbage? */
460 if (parsed < ilen && *(p+parsed) != '\0')
463 /* Point inp and itype to either our parsed 32-bit or 16-bit
466 if (ival & 0xFFFF0000) {
468 itype = BHND_NVRAM_TYPE_UINT32;
471 itype = BHND_NVRAM_TYPE_UINT16;
475 /* Populate both u32 and (possibly lossy) u16 LEDDC representations */
477 case BHND_NVRAM_TYPE_UINT16: {
478 led16 = *(const uint16_t *)inp;
479 led32 = ((led16 & 0xFF00) << 16) | ((led16 & 0x00FF) << 8);
481 /* If all bits are set in the 16-bit value (indicating that
482 * the value is 'unset' in SPROM), we must update the 32-bit
483 * representation to match. */
484 if (led16 == UINT16_MAX)
490 case BHND_NVRAM_TYPE_UINT32:
491 led32 = *(const uint32_t *)inp;
492 led16 = ((led32 >> 16) & 0xFF00) | ((led32 >> 8) & 0x00FF);
495 * Determine whether the led16 conversion is lossy:
497 * - If the lower 8 bits of each half of the 32-bit value
498 * aren't set, we can safely use the 16-bit representation
499 * without losing data.
500 * - If all bits in the 32-bit value are set, the variable is
501 * treated as unset in SPROM. We can safely use the 16-bit
502 * representation without losing data.
504 if ((led32 & 0x00FF00FF) != 0 && led32 != UINT32_MAX)
509 BHND_NV_PANIC("unsupported backing data type: %s",
510 bhnd_nvram_type_name(itype));
514 * Encode as requested output type.
517 case BHND_NVRAM_TYPE_STRING:
519 * Prefer 16-bit format.
522 return (bhnd_nvram_value_printf("0x%04hX", &led16,
523 sizeof(led16), BHND_NVRAM_TYPE_UINT16, outp, olen));
525 return (bhnd_nvram_value_printf("0x%04X", &led32,
526 sizeof(led32), BHND_NVRAM_TYPE_UINT32, outp, olen));
531 case BHND_NVRAM_TYPE_UINT16: {
532 /* Can we encode as uint16 without losing data? */
536 /* Write led16 format */
537 nbytes += sizeof(uint16_t);
539 *(uint16_t *)outp = led16;
544 case BHND_NVRAM_TYPE_UINT32:
545 /* Write led32 format */
546 nbytes += sizeof(uint32_t);
548 *(uint32_t *)outp = led32;
552 /* No other output formats are supported */
556 /* Provide the actual length */
559 /* Report insufficient space (if output was requested) */
560 if (limit < nbytes && outp != NULL)
567 * Broadcom NVRAM string encoding.
570 bhnd_nvram_val_bcmstr_encode(bhnd_nvram_val *value, void *outp, size_t *olen,
571 bhnd_nvram_type otype)
573 bhnd_nvram_val array;
574 const bhnd_nvram_val_fmt *array_fmt;
576 bhnd_nvram_type itype;
580 inp = bhnd_nvram_val_bytes(value, &ilen, &itype);
582 /* If the output is not an array type (or if it's a character array),
583 * we can fall back on standard string encoding */
584 if (!bhnd_nvram_is_array_type(otype) ||
585 otype == BHND_NVRAM_TYPE_CHAR_ARRAY)
587 return (bhnd_nvram_value_coerce(inp, ilen, itype, outp, olen,
591 /* Otherwise, we need to interpret our value as either a macaddr
592 * string, or a comma-delimited string. */
593 inp = bhnd_nvram_val_bytes(value, &ilen, &itype);
594 if (bhnd_nvram_ident_octet_string(inp, ilen, NULL, NULL))
595 array_fmt = &bhnd_nvram_val_bcm_macaddr_string_fmt;
597 array_fmt = &bhnd_nvram_val_bcm_string_csv_fmt;
599 /* Wrap in array-typed representation */
600 error = bhnd_nvram_val_init(&array, array_fmt, inp, ilen, itype,
601 BHND_NVRAM_VAL_BORROW_DATA);
603 BHND_NV_LOG("error initializing array representation: %d\n",
608 /* Ask the array-typed value to perform the encode */
609 error = bhnd_nvram_val_encode(&array, outp, olen, otype);
611 BHND_NV_LOG("error encoding array representation: %d\n", error);
613 bhnd_nvram_val_release(&array);
619 * Broadcom NVRAM comma-delimited string filter.
622 bhnd_nvram_val_bcmstr_csv_filter(const bhnd_nvram_val_fmt **fmt,
623 const void *inp, size_t ilen, bhnd_nvram_type itype)
626 case BHND_NVRAM_TYPE_STRING:
627 case BHND_NVRAM_TYPE_STRING_ARRAY:
635 * Broadcom NVRAM comma-delimited string iteration.
638 bhnd_nvram_val_bcmstr_csv_next(bhnd_nvram_val *value, const void *prev,
643 bhnd_nvram_type itype;
647 /* Fetch backing representation */
648 inp = bhnd_nvram_val_bytes(value, &ilen, &itype);
650 /* Fetch next value */
652 case BHND_NVRAM_TYPE_STRING:
653 /* Zero-length array? */
663 /* Advance to the previous element's delimiter */
664 next = (const char *)prev + *len;
666 /* Did we hit the end of the string? */
667 if ((size_t)(next - inp) >= ilen)
670 /* Fetch (and skip past) the delimiter */
673 remain = ilen - (size_t)(next - inp);
675 /* Was the delimiter the final character? */
680 /* Parse the field value, up to the next delimiter */
681 *len = bhnd_nvram_parse_field(&next, remain, delim);
685 case BHND_NVRAM_TYPE_STRING_ARRAY:
686 /* Delegate to default array iteration */
687 return (bhnd_nvram_value_array_next(inp, ilen, itype, prev,
690 BHND_NV_PANIC("unsupported type: %d", itype);
695 * MAC address filter.
698 bhnd_nvram_val_bcm_macaddr_filter(const bhnd_nvram_val_fmt **fmt,
699 const void *inp, size_t ilen, bhnd_nvram_type itype)
702 case BHND_NVRAM_TYPE_UINT8_ARRAY:
704 case BHND_NVRAM_TYPE_STRING:
705 /* Let bcm_macaddr_string format handle it */
706 *fmt = &bhnd_nvram_val_bcm_macaddr_string_fmt;
714 * MAC address encoding.
717 bhnd_nvram_val_bcm_macaddr_encode(bhnd_nvram_val *value, void *outp,
718 size_t *olen, bhnd_nvram_type otype)
721 bhnd_nvram_type itype;
725 * If converting to a string (or a single-element string array),
726 * produce an octet string (00:00:...).
728 if (bhnd_nvram_base_type(otype) == BHND_NVRAM_TYPE_STRING) {
729 return (bhnd_nvram_val_printf(value, "%[]02hhX", outp, olen,
733 /* Otherwise, use standard encoding support */
734 inp = bhnd_nvram_val_bytes(value, &ilen, &itype);
735 return (bhnd_nvram_value_coerce(inp, ilen, itype, outp, olen, otype));}
738 * MAC address string filter.
741 bhnd_nvram_val_bcm_macaddr_string_filter(const bhnd_nvram_val_fmt **fmt,
742 const void *inp, size_t ilen, bhnd_nvram_type itype)
745 case BHND_NVRAM_TYPE_STRING:
746 /* Use the standard Broadcom string format implementation if
747 * the input is not an octet string. */
748 if (!bhnd_nvram_ident_octet_string(inp, ilen, NULL, NULL))
749 *fmt = &bhnd_nvram_val_bcm_string_fmt;
759 * MAC address string octet encoding.
762 bhnd_nvram_val_bcm_macaddr_string_encode_elem(bhnd_nvram_val *value,
763 const void *inp, size_t ilen, void *outp, size_t *olen,
764 bhnd_nvram_type otype)
769 /* If integer encoding is requested, explicitly parse our
770 * non-0x-prefixed as a base 16 integer value */
771 if (bhnd_nvram_is_int_type(otype)) {
772 error = bhnd_nvram_parse_int(inp, ilen, 16, &nparsed, outp,
783 /* Otherwise, use standard encoding support */
784 return (bhnd_nvram_value_coerce(inp, ilen,
785 bhnd_nvram_val_elem_type(value), outp, olen, otype));
789 * MAC address string octet iteration.
792 bhnd_nvram_val_bcm_macaddr_string_next(bhnd_nvram_val *value, const void *prev,
797 bhnd_nvram_type stype;
801 /* Fetch backing string */
802 str = bhnd_nvram_val_bytes(value, &slen, &stype);
803 BHND_NV_ASSERT(stype == BHND_NVRAM_TYPE_STRING,
804 ("unsupported type: %d", stype));
806 /* Zero-length array? */
813 /* Determine delimiter */
814 if (!bhnd_nvram_ident_octet_string(str, slen, &delim, NULL)) {
815 /* Default to comma-delimited parsing */
819 /* Parsing will start at the base string pointer */
823 /* Advance to the previous element's delimiter */
824 next = (const char *)prev + *len;
826 /* Did we hit the end of the string? */
827 if ((size_t)(next - str) >= slen)
830 /* Fetch (and skip past) the delimiter */
833 remain = slen - (size_t)(next - str);
835 /* Was the delimiter the final character? */
840 /* Parse the field value, up to the next delimiter */
841 *len = bhnd_nvram_parse_field(&next, remain, delim);
848 * Determine whether @p inp is in octet string format, consisting of a
849 * fields of two hex characters, separated with ':' or '-' delimiters.
851 * This may be used to identify MAC address octet strings
852 * (BHND_NVRAM_SFMT_MACADDR).
854 * @param inp The string to be parsed.
855 * @param ilen The length of @p inp, in bytes.
856 * @param[out] delim On success, the delimiter used by this octet
857 * string. May be set to NULL if the field
858 * delimiter is not desired.
859 * @param[out] nelem On success, the number of fields in this
860 * octet string. May be set to NULL if the field
861 * count is not desired.
864 * @retval true if @p inp is a valid octet string
865 * @retval false if @p inp is not a valid octet string.
868 bhnd_nvram_ident_octet_string(const char *inp, size_t ilen, char *delim,
872 size_t max_elem_count, min_elem_count;
878 /* Require exactly two digits. If we relax this, there is room
879 * for ambiguity with signed integers and the '-' delimiter */
883 /* Identify the delimiter used. The standard delimiter for MAC
884 * addresses is ':', but some earlier NVRAM formats may use '-' */
885 for (const char *d = ":-";; d++) {
888 /* No delimiter found, not an octet string */
892 /* Look for the delimiter */
893 if ((loc = memchr(inp, *d, ilen)) == NULL)
896 /* Delimiter found */
901 /* To disambiguate from signed integers, if the delimiter is "-",
902 * the octets must be exactly 2 chars each */
906 /* String must be composed of individual octets (zero or more hex
907 * digits) separated by our delimiter. */
909 for (const char *p = inp; (size_t)(p - inp) < ilen; p++) {
914 /* Hit a delim character; all delims must match
915 * the first delimiter used */
916 if (*p != '\0' && *p != idelim)
919 /* Must have parsed at least min_elem_count digits */
920 if (elem_count < min_elem_count)
923 /* Reset element count */
926 /* Bump field count */
930 /* More than maximum number of hex digits? */
931 if (elem_count >= max_elem_count)
934 /* Octet values must be hex digits */
935 if (!bhnd_nv_isxdigit(*p))
947 *nelem = field_count;
954 * Determine whether @p inp is in hexadecimal, octal, or decimal string
957 * - A @p str may be prefixed with a single optional '+' or '-' sign denoting
959 * - A hexadecimal @p str may include an '0x' or '0X' prefix, denoting that a
960 * base 16 integer follows.
961 * - An octal @p str may include a '0' prefix, denoting that an octal integer
964 * @param inp The string to be parsed.
965 * @param ilen The length of @p inp, in bytes.
966 * @param base The input string's base (2-36), or 0.
967 * @param[out] obase On success, will be set to the base of the parsed
968 * integer. May be set to NULL if the base is not
971 * @retval true if @p inp is a valid number string
972 * @retval false if @p inp is not a valid number string.
973 * @retval false if @p base is invalid.
976 bhnd_nvram_ident_num_string(const char *inp, size_t ilen, u_int base,
979 size_t nbytes, ndigits;
984 /* Parse and skip sign */
988 if (inp[nbytes] == '-' || inp[nbytes] == '+')
991 /* Truncated after sign character? */
995 /* Identify (or validate) hex base, skipping 0x/0X prefix */
996 if (base == 16 || base == 0) {
997 /* Check for (and skip) 0x/0X prefix */
998 if (ilen - nbytes >= 2 && inp[nbytes] == '0' &&
999 (inp[nbytes+1] == 'x' || inp[nbytes+1] == 'X'))
1006 /* Truncated after hex prefix? */
1010 /* Differentiate decimal/octal by looking for a leading 0 */
1012 if (inp[nbytes] == '0') {
1019 /* Consume and validate all remaining digit characters */
1020 for (; nbytes < ilen; nbytes++) {
1024 /* Parse carry value */
1026 if (bhnd_nv_isdigit(c)) {
1028 } else if (bhnd_nv_isxdigit(c)) {
1029 if (bhnd_nv_isupper(c))
1030 carry = (c - 'A') + 10;
1032 carry = (c - 'a') + 10;
1034 /* Hit a non-digit character */
1038 /* If carry is outside the base, it's not a valid digit
1039 * in the current parse context; consider it a non-digit
1044 /* Increment parsed digit count */
1048 /* Empty integer string? */
1052 /* Valid integer -- provide the base and return */