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.
32 #ifndef _BHND_NVRAM_BHND_NVRAM_SPROMVAR_H_
33 #define _BHND_NVRAM_BHND_NVRAM_SPROMVAR_H_
36 #include <sys/bitstring.h>
38 #include <bitstring.h>
41 #include "bhnd_nvram_private.h"
43 #include "bhnd_nvram_datavar.h"
44 #include "bhnd_nvram_io.h"
46 /** The maximum number of array elements encoded in a single SPROM variable */
47 #define BHND_SPROM_ARRAY_MAXLEN 12
49 typedef struct bhnd_sprom_opcode_state bhnd_sprom_opcode_state;
50 typedef struct bhnd_sprom_opcode_bind bhnd_sprom_opcode_bind;
51 typedef struct bhnd_sprom_opcode_var bhnd_sprom_opcode_var;
52 typedef struct bhnd_sprom_opcode_idx_entry bhnd_sprom_opcode_idx_entry;
54 int bhnd_sprom_opcode_init(
55 bhnd_sprom_opcode_state *state,
56 const bhnd_sprom_layout *layout);
57 void bhnd_sprom_opcode_fini(
58 bhnd_sprom_opcode_state *state);
60 bhnd_sprom_opcode_idx_entry *bhnd_sprom_opcode_index_find(
61 bhnd_sprom_opcode_state *state,
63 bhnd_sprom_opcode_idx_entry *bhnd_sprom_opcode_index_next(
64 bhnd_sprom_opcode_state *state,
65 bhnd_sprom_opcode_idx_entry *prev);
67 int bhnd_sprom_opcode_init_entry(
68 bhnd_sprom_opcode_state *state,
69 bhnd_sprom_opcode_idx_entry *entry);
71 int bhnd_sprom_opcode_eval_var(
72 bhnd_sprom_opcode_state *state,
73 bhnd_sprom_opcode_idx_entry *entry);
75 int bhnd_sprom_opcode_seek(
76 bhnd_sprom_opcode_state *state,
77 bhnd_sprom_opcode_idx_entry *entry);
78 int bhnd_sprom_opcode_next_var(
79 bhnd_sprom_opcode_state *state);
80 int bhnd_sprom_opcode_next_binding(
81 bhnd_sprom_opcode_state *state);
82 int bhnd_sprom_opcode_apply_scale(
83 bhnd_sprom_opcode_state *state,
87 * SPROM opcode per-bind evaluation state.
89 struct bhnd_sprom_opcode_bind {
91 uint32_t skip_in; /**< input element skips */
92 bool skip_in_negative; /**< skip_in should be subtracted */
93 uint32_t skip_out; /**< output element skip */
97 * SPROM opcode per-variable evaluation state.
99 struct bhnd_sprom_opcode_var {
100 uint8_t nelem; /**< variable array length */
101 uint32_t mask; /**< current bind input mask */
102 int8_t shift; /**< current bind input shift */
103 bhnd_nvram_type base_type; /**< current bind input type */
104 uint32_t scale; /**< current scale to apply to scaled encodings */
105 bhnd_sprom_opcode_bind bind; /**< current bind state */
106 bool have_bind; /**< if bind state is defined */
107 size_t bind_total; /**< total count of bind operations performed */
111 * SPROM opcode variable definition states.
113 * Ordered to support inequality comparisons
114 * (e.g. >= SPROM_OPCODE_VAR_STATE_OPEN)
117 SPROM_OPCODE_VAR_STATE_NONE = 1, /**< no variable entry available */
118 SPROM_OPCODE_VAR_STATE_OPEN = 2, /**< currently parsing a variable entry */
119 SPROM_OPCODE_VAR_STATE_DONE = 3 /**< full variable entry has been parsed */
120 } bhnd_sprom_opcode_var_state;
123 * SPROM opcode evaluation state
125 struct bhnd_sprom_opcode_state {
126 const bhnd_sprom_layout *layout; /**< SPROM layout */
128 bhnd_sprom_opcode_idx_entry *idx; /**< variable index (NULL during initialization) */
129 size_t num_idx; /**< variable index entry count */
131 /** Current SPROM revision range */
132 bitstr_t bit_decl(revs, SPROM_OP_REV_MAX);
134 const uint8_t *input; /**< opcode input position */
136 /* State preserved across variable definitions */
137 uint32_t offset; /**< SPROM offset */
138 size_t vid; /**< Variable ID */
140 /* State reset after end of each variable definition */
141 bhnd_sprom_opcode_var var; /**< variable record (if any) */
142 bhnd_sprom_opcode_var_state var_state; /**< variable record state */
146 * SPROM opcode variable index entry
148 struct bhnd_sprom_opcode_idx_entry {
149 uint16_t vid; /**< SPROM variable ID */
150 uint16_t offset; /**< SPROM input offset */
151 uint16_t opcodes; /**< SPROM opcode offset */
155 * SPROM value storage.
157 * Sufficient for representing the native encoding of any defined SPROM
160 union bhnd_nvram_sprom_storage {
161 uint8_t u8[BHND_SPROM_ARRAY_MAXLEN];
162 uint16_t u16[BHND_SPROM_ARRAY_MAXLEN];
163 uint32_t u32[BHND_SPROM_ARRAY_MAXLEN];
164 int8_t i8[BHND_SPROM_ARRAY_MAXLEN];
165 int16_t i16[BHND_SPROM_ARRAY_MAXLEN];
166 int32_t i32[BHND_SPROM_ARRAY_MAXLEN];
167 char ch[BHND_SPROM_ARRAY_MAXLEN];
171 * SPROM data class instance state.
173 struct bhnd_nvram_sprom {
174 struct bhnd_nvram_data nv; /**< common instance state */
175 struct bhnd_nvram_io *data; /**< backing SPROM image */
176 const bhnd_sprom_layout *layout; /**< layout definition */
177 bhnd_sprom_opcode_state state; /**< opcode eval state */
180 #endif /* _BHND_NVRAM_BHND_NVRAM_SPROMVAR_H_ */