#include <sys/param.h>
#ifdef _KERNEL
#include <sys/ctype.h>
#include <sys/kernel.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/_inttypes.h>
#else
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "bhnd_nvram_io.h"
#include "bhnd_nvram_private.h"
#include "bhnd_nvram_value.h"
#include "bhnd_nvram_map_data.h"
#ifdef _KERNEL
MALLOC_DEFINE(M_BHND_NVRAM, "bhnd_nvram", "bhnd nvram data");
#endif
const uint8_t bhnd_nvram_crc8_tab[] = {
0x00, 0xf7, 0xb9, 0x4e, 0x25, 0xd2, 0x9c, 0x6b, 0x4a, 0xbd, 0xf3,
0x04, 0x6f, 0x98, 0xd6, 0x21, 0x94, 0x63, 0x2d, 0xda, 0xb1, 0x46,
0x08, 0xff, 0xde, 0x29, 0x67, 0x90, 0xfb, 0x0c, 0x42, 0xb5, 0x7f,
0x88, 0xc6, 0x31, 0x5a, 0xad, 0xe3, 0x14, 0x35, 0xc2, 0x8c, 0x7b,
0x10, 0xe7, 0xa9, 0x5e, 0xeb, 0x1c, 0x52, 0xa5, 0xce, 0x39, 0x77,
0x80, 0xa1, 0x56, 0x18, 0xef, 0x84, 0x73, 0x3d, 0xca, 0xfe, 0x09,
0x47, 0xb0, 0xdb, 0x2c, 0x62, 0x95, 0xb4, 0x43, 0x0d, 0xfa, 0x91,
0x66, 0x28, 0xdf, 0x6a, 0x9d, 0xd3, 0x24, 0x4f, 0xb8, 0xf6, 0x01,
0x20, 0xd7, 0x99, 0x6e, 0x05, 0xf2, 0xbc, 0x4b, 0x81, 0x76, 0x38,
0xcf, 0xa4, 0x53, 0x1d, 0xea, 0xcb, 0x3c, 0x72, 0x85, 0xee, 0x19,
0x57, 0xa0, 0x15, 0xe2, 0xac, 0x5b, 0x30, 0xc7, 0x89, 0x7e, 0x5f,
0xa8, 0xe6, 0x11, 0x7a, 0x8d, 0xc3, 0x34, 0xab, 0x5c, 0x12, 0xe5,
0x8e, 0x79, 0x37, 0xc0, 0xe1, 0x16, 0x58, 0xaf, 0xc4, 0x33, 0x7d,
0x8a, 0x3f, 0xc8, 0x86, 0x71, 0x1a, 0xed, 0xa3, 0x54, 0x75, 0x82,
0xcc, 0x3b, 0x50, 0xa7, 0xe9, 0x1e, 0xd4, 0x23, 0x6d, 0x9a, 0xf1,
0x06, 0x48, 0xbf, 0x9e, 0x69, 0x27, 0xd0, 0xbb, 0x4c, 0x02, 0xf5,
0x40, 0xb7, 0xf9, 0x0e, 0x65, 0x92, 0xdc, 0x2b, 0x0a, 0xfd, 0xb3,
0x44, 0x2f, 0xd8, 0x96, 0x61, 0x55, 0xa2, 0xec, 0x1b, 0x70, 0x87,
0xc9, 0x3e, 0x1f, 0xe8, 0xa6, 0x51, 0x3a, 0xcd, 0x83, 0x74, 0xc1,
0x36, 0x78, 0x8f, 0xe4, 0x13, 0x5d, 0xaa, 0x8b, 0x7c, 0x32, 0xc5,
0xae, 0x59, 0x17, 0xe0, 0x2a, 0xdd, 0x93, 0x64, 0x0f, 0xf8, 0xb6,
0x41, 0x60, 0x97, 0xd9, 0x2e, 0x45, 0xb2, 0xfc, 0x0b, 0xbe, 0x49,
0x07, 0xf0, 0x9b, 0x6c, 0x22, 0xd5, 0xf4, 0x03, 0x4d, 0xba, 0xd1,
0x26, 0x68, 0x9f
};
const char *
bhnd_nvram_type_name(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_UINT8:
return ("uint8");
case BHND_NVRAM_TYPE_UINT16:
return ("uint16");
case BHND_NVRAM_TYPE_UINT32:
return ("uint32");
case BHND_NVRAM_TYPE_UINT64:
return ("uint64");
case BHND_NVRAM_TYPE_CHAR:
return ("char");
case BHND_NVRAM_TYPE_INT8:
return ("int8");
case BHND_NVRAM_TYPE_INT16:
return ("int16");
case BHND_NVRAM_TYPE_INT32:
return ("int32");
case BHND_NVRAM_TYPE_INT64:
return ("int64");
case BHND_NVRAM_TYPE_STRING:
return ("string");
case BHND_NVRAM_TYPE_BOOL:
return ("bool");
case BHND_NVRAM_TYPE_NULL:
return ("null");
case BHND_NVRAM_TYPE_DATA:
return ("data");
case BHND_NVRAM_TYPE_UINT8_ARRAY:
return ("uint8[]");
case BHND_NVRAM_TYPE_UINT16_ARRAY:
return ("uint16[]");
case BHND_NVRAM_TYPE_UINT32_ARRAY:
return ("uint32[]");
case BHND_NVRAM_TYPE_UINT64_ARRAY:
return ("uint64[]");
case BHND_NVRAM_TYPE_INT8_ARRAY:
return ("int8[]");
case BHND_NVRAM_TYPE_INT16_ARRAY:
return ("int16[]");
case BHND_NVRAM_TYPE_INT32_ARRAY:
return ("int32[]");
case BHND_NVRAM_TYPE_INT64_ARRAY:
return ("int64[]");
case BHND_NVRAM_TYPE_CHAR_ARRAY:
return ("char[]");
case BHND_NVRAM_TYPE_STRING_ARRAY:
return ("string[]");
case BHND_NVRAM_TYPE_BOOL_ARRAY:
return ("bool[]");
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
bool
bhnd_nvram_is_signed_type(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT64:
BHND_NV_ASSERT(bhnd_nvram_is_int_type(type), ("non-int type?"));
return (true);
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_BOOL:
case BHND_NVRAM_TYPE_NULL:
case BHND_NVRAM_TYPE_DATA:
case BHND_NVRAM_TYPE_UINT8_ARRAY:
case BHND_NVRAM_TYPE_UINT16_ARRAY:
case BHND_NVRAM_TYPE_UINT32_ARRAY:
case BHND_NVRAM_TYPE_UINT64_ARRAY:
case BHND_NVRAM_TYPE_INT8_ARRAY:
case BHND_NVRAM_TYPE_INT16_ARRAY:
case BHND_NVRAM_TYPE_INT32_ARRAY:
case BHND_NVRAM_TYPE_INT64_ARRAY:
case BHND_NVRAM_TYPE_CHAR_ARRAY:
case BHND_NVRAM_TYPE_STRING_ARRAY:
case BHND_NVRAM_TYPE_BOOL_ARRAY:
return (false);
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
bool
bhnd_nvram_is_unsigned_type(bhnd_nvram_type type)
{
if (!bhnd_nvram_is_int_type(type))
return (false);
return (!bhnd_nvram_is_signed_type(type));
}
bool
bhnd_nvram_is_int_type(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT64:
return (true);
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_BOOL:
case BHND_NVRAM_TYPE_NULL:
case BHND_NVRAM_TYPE_DATA:
case BHND_NVRAM_TYPE_UINT8_ARRAY:
case BHND_NVRAM_TYPE_UINT16_ARRAY:
case BHND_NVRAM_TYPE_UINT32_ARRAY:
case BHND_NVRAM_TYPE_UINT64_ARRAY:
case BHND_NVRAM_TYPE_INT8_ARRAY:
case BHND_NVRAM_TYPE_INT16_ARRAY:
case BHND_NVRAM_TYPE_INT32_ARRAY:
case BHND_NVRAM_TYPE_INT64_ARRAY:
case BHND_NVRAM_TYPE_CHAR_ARRAY:
case BHND_NVRAM_TYPE_STRING_ARRAY:
case BHND_NVRAM_TYPE_BOOL_ARRAY:
return (false);
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
bool
bhnd_nvram_is_array_type(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT64:
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_BOOL:
case BHND_NVRAM_TYPE_NULL:
case BHND_NVRAM_TYPE_DATA:
return (false);
case BHND_NVRAM_TYPE_UINT8_ARRAY:
case BHND_NVRAM_TYPE_UINT16_ARRAY:
case BHND_NVRAM_TYPE_UINT32_ARRAY:
case BHND_NVRAM_TYPE_UINT64_ARRAY:
case BHND_NVRAM_TYPE_INT8_ARRAY:
case BHND_NVRAM_TYPE_INT16_ARRAY:
case BHND_NVRAM_TYPE_INT32_ARRAY:
case BHND_NVRAM_TYPE_INT64_ARRAY:
case BHND_NVRAM_TYPE_CHAR_ARRAY:
case BHND_NVRAM_TYPE_STRING_ARRAY:
case BHND_NVRAM_TYPE_BOOL_ARRAY:
return (true);
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
bhnd_nvram_type
bhnd_nvram_base_type(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT64:
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_BOOL:
case BHND_NVRAM_TYPE_NULL:
case BHND_NVRAM_TYPE_DATA:
return (type);
case BHND_NVRAM_TYPE_UINT8_ARRAY: return (BHND_NVRAM_TYPE_UINT8);
case BHND_NVRAM_TYPE_UINT16_ARRAY: return (BHND_NVRAM_TYPE_UINT16);
case BHND_NVRAM_TYPE_UINT32_ARRAY: return (BHND_NVRAM_TYPE_UINT32);
case BHND_NVRAM_TYPE_UINT64_ARRAY: return (BHND_NVRAM_TYPE_UINT64);
case BHND_NVRAM_TYPE_INT8_ARRAY: return (BHND_NVRAM_TYPE_INT8);
case BHND_NVRAM_TYPE_INT16_ARRAY: return (BHND_NVRAM_TYPE_INT16);
case BHND_NVRAM_TYPE_INT32_ARRAY: return (BHND_NVRAM_TYPE_INT32);
case BHND_NVRAM_TYPE_INT64_ARRAY: return (BHND_NVRAM_TYPE_INT64);
case BHND_NVRAM_TYPE_CHAR_ARRAY: return (BHND_NVRAM_TYPE_CHAR);
case BHND_NVRAM_TYPE_STRING_ARRAY: return (BHND_NVRAM_TYPE_STRING);
case BHND_NVRAM_TYPE_BOOL_ARRAY: return (BHND_NVRAM_TYPE_BOOL);
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
bhnd_nvram_type
bhnd_nvram_raw_type(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_CHAR:
return (BHND_NVRAM_TYPE_UINT8);
case BHND_NVRAM_TYPE_CHAR_ARRAY:
return (BHND_NVRAM_TYPE_UINT8_ARRAY);
case BHND_NVRAM_TYPE_BOOL: {
_Static_assert(sizeof(bhnd_nvram_bool_t) == sizeof(uint8_t),
"bhnd_nvram_bool_t must be uint8-representable");
return (BHND_NVRAM_TYPE_UINT8);
}
case BHND_NVRAM_TYPE_BOOL_ARRAY:
return (BHND_NVRAM_TYPE_UINT8_ARRAY);
case BHND_NVRAM_TYPE_DATA:
return (BHND_NVRAM_TYPE_UINT8_ARRAY);
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_STRING_ARRAY:
return (BHND_NVRAM_TYPE_UINT8_ARRAY);
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT64:
case BHND_NVRAM_TYPE_NULL:
case BHND_NVRAM_TYPE_UINT8_ARRAY:
case BHND_NVRAM_TYPE_UINT16_ARRAY:
case BHND_NVRAM_TYPE_UINT32_ARRAY:
case BHND_NVRAM_TYPE_UINT64_ARRAY:
case BHND_NVRAM_TYPE_INT8_ARRAY:
case BHND_NVRAM_TYPE_INT16_ARRAY:
case BHND_NVRAM_TYPE_INT32_ARRAY:
case BHND_NVRAM_TYPE_INT64_ARRAY:
return (type);
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
size_t
bhnd_nvram_type_width(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_STRING_ARRAY:
case BHND_NVRAM_TYPE_DATA:
return (0);
case BHND_NVRAM_TYPE_NULL:
return (0);
case BHND_NVRAM_TYPE_BOOL:
case BHND_NVRAM_TYPE_BOOL_ARRAY:
return (sizeof(bhnd_nvram_bool_t));
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_CHAR_ARRAY:
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT8_ARRAY:
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT8_ARRAY:
return (sizeof(uint8_t));
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT16_ARRAY:
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT16_ARRAY:
return (sizeof(uint16_t));
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT32_ARRAY:
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT32_ARRAY:
return (sizeof(uint32_t));
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_UINT64_ARRAY:
case BHND_NVRAM_TYPE_INT64:
case BHND_NVRAM_TYPE_INT64_ARRAY:
return (sizeof(uint64_t));
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
size_t
bhnd_nvram_type_host_align(bhnd_nvram_type type)
{
switch (type) {
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_CHAR_ARRAY:
case BHND_NVRAM_TYPE_DATA:
case BHND_NVRAM_TYPE_STRING:
case BHND_NVRAM_TYPE_STRING_ARRAY:
return (_Alignof(uint8_t));
case BHND_NVRAM_TYPE_BOOL:
case BHND_NVRAM_TYPE_BOOL_ARRAY: {
_Static_assert(sizeof(bhnd_nvram_bool_t) == sizeof(uint8_t),
"bhnd_nvram_bool_t must be uint8-representable");
return (_Alignof(uint8_t));
}
case BHND_NVRAM_TYPE_NULL:
return (1);
case BHND_NVRAM_TYPE_UINT8:
case BHND_NVRAM_TYPE_UINT8_ARRAY:
return (_Alignof(uint8_t));
case BHND_NVRAM_TYPE_UINT16:
case BHND_NVRAM_TYPE_UINT16_ARRAY:
return (_Alignof(uint16_t));
case BHND_NVRAM_TYPE_UINT32:
case BHND_NVRAM_TYPE_UINT32_ARRAY:
return (_Alignof(uint32_t));
case BHND_NVRAM_TYPE_UINT64:
case BHND_NVRAM_TYPE_UINT64_ARRAY:
return (_Alignof(uint64_t));
case BHND_NVRAM_TYPE_INT8:
case BHND_NVRAM_TYPE_INT8_ARRAY:
return (_Alignof(int8_t));
case BHND_NVRAM_TYPE_INT16:
case BHND_NVRAM_TYPE_INT16_ARRAY:
return (_Alignof(int16_t));
case BHND_NVRAM_TYPE_INT32:
case BHND_NVRAM_TYPE_INT32_ARRAY:
return (_Alignof(int32_t));
case BHND_NVRAM_TYPE_INT64:
case BHND_NVRAM_TYPE_INT64_ARRAY:
return (_Alignof(int64_t));
}
BHND_NV_PANIC("bhnd nvram type %u unknown", type);
}
const char *
bhnd_nvram_string_array_next(const char *inp, size_t ilen, const char *prev,
size_t *olen)
{
return (bhnd_nvram_value_array_next(inp, ilen,
BHND_NVRAM_TYPE_STRING_ARRAY, prev, olen));
}
static int
bhnd_nvram_find_vardefn_compare(const void *key, const void *rhs)
{
const struct bhnd_nvram_vardefn *r = rhs;
return (strcmp((const char *)key, r->name));
}
const struct bhnd_nvram_vardefn *
bhnd_nvram_find_vardefn(const char *varname)
{
return (bsearch(varname, bhnd_nvram_vardefns, bhnd_nvram_num_vardefns,
sizeof(bhnd_nvram_vardefns[0]), bhnd_nvram_find_vardefn_compare));
}
size_t
bhnd_nvram_get_vardefn_id(const struct bhnd_nvram_vardefn *defn)
{
BHND_NV_ASSERT(
defn >= bhnd_nvram_vardefns &&
defn <= &bhnd_nvram_vardefns[bhnd_nvram_num_vardefns-1],
("invalid variable definition pointer %p", defn));
return (defn - bhnd_nvram_vardefns);
}
const struct bhnd_nvram_vardefn *
bhnd_nvram_get_vardefn(size_t id)
{
if (id >= bhnd_nvram_num_vardefns)
return (NULL);
return (&bhnd_nvram_vardefns[id]);
}
bool
bhnd_nvram_validate_name(const char *name)
{
if (bhnd_nvram_trim_path_name(name) != name)
return (false);
if (strncmp(name, "devpath", strlen("devpath")) == 0) {
const char *p;
char *endp;
p = name + strlen("devpath");
strtoul(p, &endp, 10);
if (endp != p)
return (false);
}
for (const char *p = name; *p != '\0'; p++) {
switch (*p) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '_':
break;
default:
if (!bhnd_nv_isalpha(*p))
return (false);
break;
}
}
return (true);
}
int
bhnd_nvram_parse_int(const char *str, size_t maxlen, u_int base,
size_t *nbytes, void *outp, size_t *olen, bhnd_nvram_type otype)
{
uint64_t value;
uint64_t carry_max, value_max;
uint64_t type_max;
size_t limit, local_nbytes;
size_t ndigits;
bool negative, sign, twos_compl;
if (!bhnd_nvram_is_int_type(otype))
return (EINVAL);
if (outp != NULL)
limit = *olen;
else
limit = 0;
if (nbytes == NULL)
nbytes = &local_nbytes;
value = 0;
ndigits = 0;
*nbytes = 0;
negative = false;
sign = false;
if (base != 0 && !(base >= 2 && base <= 36))
return (EINVAL);
for (; *nbytes < maxlen; (*nbytes)++) {
if (!bhnd_nv_isspace(str[*nbytes]))
break;
}
if (*nbytes == maxlen)
return (EFTYPE);
if (str[*nbytes] == '-') {
negative = true;
sign = true;
(*nbytes)++;
} else if (str[*nbytes] == '+') {
sign = true;
(*nbytes)++;
}
if (*nbytes == maxlen)
return (EFTYPE);
if (base == 16 || base == 0) {
if (maxlen - *nbytes >= 2 && str[*nbytes] == '0' &&
(str[*nbytes+1] == 'x' || str[*nbytes+1] == 'X'))
{
base = 16;
(*nbytes) += 2;
}
}
if (*nbytes == maxlen)
return (EFTYPE);
if (base == 0) {
if (str[*nbytes] == '0') {
base = 8;
} else {
base = 10;
}
}
if (!sign && base == 16)
twos_compl = true;
else
twos_compl = false;
switch (otype) {
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_UINT8:
type_max = (uint64_t)UINT8_MAX;
break;
case BHND_NVRAM_TYPE_UINT16:
type_max = (uint64_t)UINT16_MAX;
break;
case BHND_NVRAM_TYPE_UINT32:
type_max = (uint64_t)UINT32_MAX;
break;
case BHND_NVRAM_TYPE_UINT64:
type_max = (uint64_t)UINT64_MAX;
break;
case BHND_NVRAM_TYPE_INT8:
if (twos_compl)
type_max = (uint64_t)UINT8_MAX;
else if (negative)
type_max = -(uint64_t)INT8_MIN;
else
type_max = (uint64_t)INT8_MAX;
break;
case BHND_NVRAM_TYPE_INT16:
if (twos_compl)
type_max = (uint64_t)UINT16_MAX;
else if (negative)
type_max = -(uint64_t)INT16_MIN;
else
type_max = (uint64_t)INT16_MAX;
break;
case BHND_NVRAM_TYPE_INT32:
if (twos_compl)
type_max = (uint64_t)UINT32_MAX;
else if (negative)
type_max = -(uint64_t)INT32_MIN;
else
type_max = (uint64_t)INT32_MAX;
break;
case BHND_NVRAM_TYPE_INT64:
if (twos_compl)
type_max = (uint64_t)UINT64_MAX;
else if (negative)
type_max = -(uint64_t)INT64_MIN;
else
type_max = (uint64_t)INT64_MAX;
break;
default:
BHND_NV_LOG("unsupported integer type: %d\n", otype);
return (EINVAL);
}
value_max = type_max / (uint64_t)base;
carry_max = type_max % (uint64_t)base;
for (; *nbytes < maxlen; (*nbytes)++) {
u_long carry;
char c;
c = str[*nbytes];
if (bhnd_nv_isdigit(c)) {
carry = c - '0';
} else if (bhnd_nv_isxdigit(c)) {
if (bhnd_nv_isupper(c))
carry = (c - 'A') + 10;
else
carry = (c - 'a') + 10;
} else {
break;
}
if (carry >= (uint64_t)base)
break;
ndigits++;
if (value > value_max) {
return (ERANGE);
} else if (value == value_max && carry > carry_max) {
return (ERANGE);
}
value *= (uint64_t)base;
value += carry;
}
if (ndigits == 0)
return (EFTYPE);
if (negative)
value = -value;
*olen = bhnd_nvram_type_width(otype);
if (outp == NULL)
return (0);
else if (limit < *olen)
return (ENOMEM);
switch (otype) {
case BHND_NVRAM_TYPE_CHAR:
case BHND_NVRAM_TYPE_UINT8:
*(uint8_t *)outp = (uint8_t)value;
break;
case BHND_NVRAM_TYPE_UINT16:
*(uint16_t *)outp = (uint16_t)value;
break;
case BHND_NVRAM_TYPE_UINT32:
*(uint32_t *)outp = (uint32_t)value;
break;
case BHND_NVRAM_TYPE_UINT64:
*(uint64_t *)outp = (uint64_t)value;
break;
case BHND_NVRAM_TYPE_INT8:
*(int8_t *)outp = (int8_t)(int64_t)value;
break;
case BHND_NVRAM_TYPE_INT16:
*(int16_t *)outp = (int16_t)(int64_t)value;
break;
case BHND_NVRAM_TYPE_INT32:
*(int32_t *)outp = (int32_t)(int64_t)value;
break;
case BHND_NVRAM_TYPE_INT64:
*(int64_t *)outp = (int64_t)value;
break;
default:
BHND_NV_PANIC("unhandled type %d\n", otype);
}
return (0);
}
const char *
bhnd_nvram_trim_path_name(const char *name)
{
char *endp;
if (bhnd_nv_isdigit(*name)) {
strtoul(name, &endp, 10);
if (endp != name && *endp == ':') {
return (endp+1);
}
}
if ((endp = strrchr(name, '/')) != NULL) {
return (endp+1);
}
return (name);
}
int
bhnd_nvram_parse_env(const char *env, size_t env_len, char delim,
const char **name, size_t *name_len, const char **value, size_t *value_len)
{
const char *p;
if ((p = memchr(env, delim, env_len)) == NULL) {
BHND_NV_LOG("delimiter '%c' not found in '%.*s'\n", delim,
BHND_NV_PRINT_WIDTH(env_len), env);
return (EINVAL);
}
if (name != NULL)
*name = env;
if (name_len != NULL)
*name_len = p - env;
p++;
if (value != NULL)
*value = p;
if (value_len != NULL)
*value_len = env_len - (p - env);
return (0);
}
size_t
bhnd_nvram_parse_field(const char **inp, size_t ilen, char delim)
{
const char *p, *sp;
for (sp = *inp; (size_t)(sp-*inp) < ilen && bhnd_nv_isspace(*sp); sp++)
continue;
*inp = sp;
for (p = *inp; (size_t)(p - *inp) < ilen; p++) {
if (*p == delim || *p == '\0')
break;
}
return (p - *inp);
}
size_t
bhnd_nvram_trim_field(const char **inp, size_t ilen, char delim)
{
const char *sp;
size_t plen;
plen = bhnd_nvram_parse_field(inp, ilen, delim);
sp = *inp;
while (plen > 0) {
if (!bhnd_nv_isspace(*(sp + plen - 1)))
break;
plen--;
}
return (plen);
}