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>
34 #include <sys/endian.h>
37 #include <sys/systm.h>
38 #include <machine/_inttypes.h>
46 #include "bhnd_nvram_private.h"
47 #include "bhnd_nvram_data_spromvar.h"
49 static int bhnd_sprom_opcode_sort_idx(const void *lhs, const void *rhs);
50 static int bhnd_nvram_opcode_idx_vid_compare(const void *key,
53 static int bhnd_sprom_opcode_reset(bhnd_sprom_opcode_state *state);
55 static int bhnd_sprom_opcode_set_type(bhnd_sprom_opcode_state *state,
56 bhnd_nvram_type type);
58 static int bhnd_sprom_opcode_set_var(bhnd_sprom_opcode_state *state,
60 static int bhnd_sprom_opcode_clear_var(bhnd_sprom_opcode_state *state);
62 static int bhnd_sprom_opcode_flush_bind(bhnd_sprom_opcode_state *state);
64 static int bhnd_sprom_opcode_read_opval32(bhnd_sprom_opcode_state *state,
65 uint8_t type, uint32_t *opval);
67 static int bhnd_sprom_opcode_step(bhnd_sprom_opcode_state *state,
70 #define SPROM_OP_BAD(_state, _fmt, ...) \
71 BHND_NV_LOG("bad encoding at %td: " _fmt, \
72 (_state)->input - (_state)->layout->bindings, ##__VA_ARGS__)
75 * Initialize SPROM opcode evaluation state.
77 * @param state The opcode state to be initialized.
78 * @param layout The SPROM layout to be parsed by this instance.
82 * @retval non-zero If initialization fails, a regular unix error code will be
86 bhnd_sprom_opcode_init(bhnd_sprom_opcode_state *state,
87 const struct bhnd_sprom_layout *layout)
89 bhnd_sprom_opcode_idx_entry *idx;
90 size_t num_vars, num_idx;
95 state->layout = layout;
99 /* Initialize interpretation state */
100 if ((error = bhnd_sprom_opcode_reset(state)))
103 /* Allocate and populate our opcode index */
104 num_idx = state->layout->num_vars;
105 idx = bhnd_nv_calloc(num_idx, sizeof(*idx));
109 for (num_vars = 0; num_vars < num_idx; num_vars++) {
110 /* Seek to next entry */
111 if ((error = bhnd_sprom_opcode_next_var(state))) {
112 SPROM_OP_BAD(state, "error reading expected variable "
113 "entry: %d\n", error);
118 /* Record entry state in our index */
119 error = bhnd_sprom_opcode_init_entry(state, &idx[num_vars]);
121 SPROM_OP_BAD(state, "error initializing index for "
122 "entry: %d\n", error);
128 /* Should have reached end of binding table; next read must return
130 if ((error = bhnd_sprom_opcode_next_var(state)) != ENOENT) {
131 BHND_NV_LOG("expected EOF parsing binding table: %d\n", error);
136 /* Reset interpretation state */
137 if ((error = bhnd_sprom_opcode_reset(state))) {
142 /* Make index available to opcode state evaluation */
143 qsort(idx, num_idx, sizeof(idx[0]), bhnd_sprom_opcode_sort_idx);
146 state->num_idx = num_idx;
152 * Reset SPROM opcode evaluation state; future evaluation will be performed
153 * starting at the first opcode.
155 * @param state The opcode state to be reset.
158 * @retval non-zero If reset fails, a regular unix error code will be returned.
161 bhnd_sprom_opcode_reset(bhnd_sprom_opcode_state *state)
163 memset(&state->var, 0, sizeof(state->var));
165 state->input = state->layout->bindings;
168 state->var_state = SPROM_OPCODE_VAR_STATE_NONE;
169 bit_set(state->revs, state->layout->rev);
175 * Free any resources associated with @p state.
177 * @param state An opcode state previously successfully initialized with
178 * bhnd_sprom_opcode_init().
181 bhnd_sprom_opcode_fini(bhnd_sprom_opcode_state *state)
183 bhnd_nv_free(state->idx);
187 * Sort function used to prepare our index for querying; sorts
188 * bhnd_sprom_opcode_idx_entry values by variable ID, ascending.
191 bhnd_sprom_opcode_sort_idx(const void *lhs, const void *rhs)
193 const bhnd_sprom_opcode_idx_entry *l, *r;
206 * Binary search comparison function used by bhnd_sprom_opcode_index_find();
207 * searches bhnd_sprom_opcode_idx_entry values by variable ID, ascending.
210 bhnd_nvram_opcode_idx_vid_compare(const void *key, const void *rhs)
212 const bhnd_sprom_opcode_idx_entry *entry;
215 vid = *(const size_t *)key;
218 if (vid < entry->vid)
220 if (vid > entry->vid)
227 * Locate an index entry for the variable with @p name, or NULL if not found.
229 * @param state The opcode state to be queried.
230 * @param name The name to search for.
232 * @retval non-NULL If @p name is found, its index entry value will be
234 * @retval NULL If @p name is not found.
236 bhnd_sprom_opcode_idx_entry *
237 bhnd_sprom_opcode_index_find(bhnd_sprom_opcode_state *state, const char *name)
239 const struct bhnd_nvram_vardefn *var;
242 /* Determine the variable ID for the given name */
243 if ((var = bhnd_nvram_find_vardefn(name)) == NULL)
246 vid = bhnd_nvram_get_vardefn_id(var);
248 /* Search our index for the variable ID */
249 return (bsearch(&vid, state->idx, state->num_idx, sizeof(state->idx[0]),
250 bhnd_nvram_opcode_idx_vid_compare));
254 * Iterate over all index entries in @p state.
256 * @param state The opcode state to be iterated.
257 * @param[in,out] prev An entry previously returned by
258 * bhnd_sprom_opcode_index_next(), or a NULL value
259 * to begin iteration.
261 * @return Returns the next index entry name, or NULL if all entries have
264 bhnd_sprom_opcode_idx_entry *
265 bhnd_sprom_opcode_index_next(bhnd_sprom_opcode_state *state,
266 bhnd_sprom_opcode_idx_entry *prev)
270 /* Get next index position */
274 /* Determine current position */
275 idxpos = (size_t)(prev - state->idx);
276 BHND_NV_ASSERT(idxpos < state->num_idx,
277 ("invalid index %zu", idxpos));
279 /* Advance to next entry */
284 if (idxpos == state->num_idx)
287 return (&state->idx[idxpos]);
291 * Initialize @p entry with the current variable's opcode state.
293 * @param state The opcode state to be saved.
294 * @param[out] entry The opcode index entry to be initialized from @p state.
297 * @retval ENXIO if @p state cannot be serialized as an index entry.
300 bhnd_sprom_opcode_init_entry(bhnd_sprom_opcode_state *state,
301 bhnd_sprom_opcode_idx_entry *entry)
305 /* We limit the SPROM index representations to the minimal type widths
306 * capable of covering all known layouts */
308 /* Save SPROM image offset */
309 if (state->offset > UINT16_MAX) {
310 SPROM_OP_BAD(state, "cannot index large offset %u\n",
315 entry->offset = state->offset;
317 /* Save current variable ID */
318 if (state->vid > UINT16_MAX) {
319 SPROM_OP_BAD(state, "cannot index large vid %zu\n",
323 entry->vid = state->vid;
325 /* Save opcode position */
326 opcodes = (state->input - state->layout->bindings);
327 if (opcodes > UINT16_MAX) {
328 SPROM_OP_BAD(state, "cannot index large opcode offset "
332 entry->opcodes = opcodes;
338 * Reset SPROM opcode evaluation state and seek to the @p entry's position.
340 * @param state The opcode state to be reset.
341 * @param entry The indexed entry to which we'll seek the opcode state.
344 bhnd_sprom_opcode_seek(bhnd_sprom_opcode_state *state,
345 bhnd_sprom_opcode_idx_entry *entry)
349 BHND_NV_ASSERT(entry->opcodes < state->layout->bindings_size,
350 ("index entry references invalid opcode position"));
353 if ((error = bhnd_sprom_opcode_reset(state)))
356 /* Seek to the indexed sprom opcode offset */
357 state->input = state->layout->bindings + entry->opcodes;
359 /* Restore the indexed sprom data offset and VID */
360 state->offset = entry->offset;
362 /* Restore the indexed sprom variable ID */
363 if ((error = bhnd_sprom_opcode_set_var(state, entry->vid)))
370 * Set the current revision range for @p state. This also resets
373 * @param state The opcode state to update
374 * @param start The first revision in the range.
375 * @param end The last revision in the range.
378 * @retval non-zero If updating @p state fails, a regular unix error code will
382 bhnd_sprom_opcode_set_revs(bhnd_sprom_opcode_state *state, uint8_t start,
387 /* Validate the revision range */
388 if (start > SPROM_OP_REV_MAX ||
389 end > SPROM_OP_REV_MAX ||
392 SPROM_OP_BAD(state, "invalid revision range: %hhu-%hhu\n",
397 /* Clear variable state */
398 if ((error = bhnd_sprom_opcode_clear_var(state)))
401 /* Reset revision mask */
402 memset(state->revs, 0x0, sizeof(state->revs));
403 bit_nset(state->revs, start, end);
409 * Set the current variable's value mask for @p state.
411 * @param state The opcode state to update
412 * @param mask The mask to be set
415 * @retval non-zero If updating @p state fails, a regular unix error code will
419 bhnd_sprom_opcode_set_mask(bhnd_sprom_opcode_state *state, uint32_t mask)
421 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
422 SPROM_OP_BAD(state, "no open variable definition\n");
426 state->var.mask = mask;
431 * Set the current variable's value shift for @p state.
433 * @param state The opcode state to update
434 * @param shift The shift to be set
437 * @retval non-zero If updating @p state fails, a regular unix error code will
441 bhnd_sprom_opcode_set_shift(bhnd_sprom_opcode_state *state, int8_t shift)
443 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
444 SPROM_OP_BAD(state, "no open variable definition\n");
448 state->var.shift = shift;
453 * Register a new BIND/BINDN operation with @p state.
455 * @param state The opcode state to update.
456 * @param count The number of elements to be bound.
457 * @param skip_in The number of input elements to skip after each bind.
458 * @param skip_in_negative If true, the input skip should be subtracted from
459 * the current offset after each bind. If false, the input skip should be
461 * @param skip_out The number of output elements to skip after each bind.
464 * @retval EINVAL if a variable definition is not open.
465 * @retval EINVAL if @p skip_in and @p count would trigger an overflow or
466 * underflow when applied to the current input offset.
467 * @retval ERANGE if @p skip_in would overflow uint32_t when multiplied by
468 * @p count and the scale value.
469 * @retval ERANGE if @p skip_out would overflow uint32_t when multiplied by
470 * @p count and the scale value.
471 * @retval non-zero If updating @p state otherwise fails, a regular unix error
472 * code will be returned.
475 bhnd_sprom_opcode_set_bind(bhnd_sprom_opcode_state *state, uint8_t count,
476 uint8_t skip_in, bool skip_in_negative, uint8_t skip_out)
478 uint32_t iskip_total;
479 uint32_t iskip_scaled;
482 /* Must have an open variable */
483 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
484 SPROM_OP_BAD(state, "no open variable definition\n");
485 SPROM_OP_BAD(state, "BIND outside of variable definition\n");
489 /* Cannot overwite an existing bind definition */
490 if (state->var.have_bind) {
491 SPROM_OP_BAD(state, "BIND overwrites existing definition\n");
495 /* Must have a count of at least 1 */
497 SPROM_OP_BAD(state, "BIND with zero count\n");
501 /* Scale skip_in by the current type width */
502 iskip_scaled = skip_in;
503 if ((error = bhnd_sprom_opcode_apply_scale(state, &iskip_scaled)))
506 /* Calculate total input bytes skipped: iskip_scaled * count) */
507 if (iskip_scaled > 0 && UINT32_MAX / iskip_scaled < count) {
508 SPROM_OP_BAD(state, "skip_in %hhu would overflow", skip_in);
512 iskip_total = iskip_scaled * count;
514 /* Verify that the skip_in value won't under/overflow the current
516 if (skip_in_negative) {
517 if (iskip_total > state->offset) {
518 SPROM_OP_BAD(state, "skip_in %hhu would underflow "
519 "offset %u\n", skip_in, state->offset);
523 if (UINT32_MAX - iskip_total < state->offset) {
524 SPROM_OP_BAD(state, "skip_in %hhu would overflow "
525 "offset %u\n", skip_in, state->offset);
530 /* Set the actual count and skip values */
531 state->var.have_bind = true;
532 state->var.bind.count = count;
533 state->var.bind.skip_in = skip_in;
534 state->var.bind.skip_out = skip_out;
536 state->var.bind.skip_in_negative = skip_in_negative;
538 /* Update total bind count for the current variable */
539 state->var.bind_total++;
545 * Apply and clear the current opcode bind state, if any.
547 * @param state The opcode state to update.
550 * @retval non-zero If updating @p state otherwise fails, a regular unix error
551 * code will be returned.
554 bhnd_sprom_opcode_flush_bind(bhnd_sprom_opcode_state *state)
560 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN ||
561 !state->var.have_bind)
564 /* Apply SPROM offset adjustment */
565 if (state->var.bind.count > 0) {
566 skip = state->var.bind.skip_in * state->var.bind.count;
567 if ((error = bhnd_sprom_opcode_apply_scale(state, &skip)))
570 if (state->var.bind.skip_in_negative) {
571 state->offset -= skip;
573 state->offset += skip;
577 /* Clear bind state */
578 memset(&state->var.bind, 0, sizeof(state->var.bind));
579 state->var.have_bind = false;
585 * Set the current type to @p type, and reset type-specific
588 * @param state The opcode state to update.
589 * @param type The new type.
592 * @retval EINVAL if @p vid is not a valid variable ID.
595 bhnd_sprom_opcode_set_type(bhnd_sprom_opcode_state *state, bhnd_nvram_type type)
597 bhnd_nvram_type base_type;
601 /* Must have an open variable definition */
602 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
603 SPROM_OP_BAD(state, "type set outside variable definition\n");
607 /* Fetch type width for use as our scale value */
608 width = bhnd_nvram_type_width(type);
610 SPROM_OP_BAD(state, "unsupported variable-width type: %d\n",
613 } else if (width > UINT32_MAX) {
614 SPROM_OP_BAD(state, "invalid type width %zu for type: %d\n",
619 /* Determine default mask value for the element type */
620 base_type = bhnd_nvram_base_type(type);
622 case BHND_NVRAM_TYPE_UINT8:
623 case BHND_NVRAM_TYPE_INT8:
624 case BHND_NVRAM_TYPE_CHAR:
627 case BHND_NVRAM_TYPE_UINT16:
628 case BHND_NVRAM_TYPE_INT16:
631 case BHND_NVRAM_TYPE_UINT32:
632 case BHND_NVRAM_TYPE_INT32:
635 case BHND_NVRAM_TYPE_STRING:
636 /* fallthrough (unused by SPROM) */
638 SPROM_OP_BAD(state, "unsupported type: %d\n", type);
643 state->var.base_type = base_type;
644 state->var.mask = mask;
645 state->var.scale = (uint32_t)width;
651 * Clear current variable state, if any.
653 * @param state The opcode state to update.
656 bhnd_sprom_opcode_clear_var(bhnd_sprom_opcode_state *state)
658 if (state->var_state == SPROM_OPCODE_VAR_STATE_NONE)
661 BHND_NV_ASSERT(state->var_state == SPROM_OPCODE_VAR_STATE_DONE,
662 ("incomplete variable definition"));
663 BHND_NV_ASSERT(!state->var.have_bind, ("stale bind state"));
665 memset(&state->var, 0, sizeof(state->var));
666 state->var_state = SPROM_OPCODE_VAR_STATE_NONE;
672 * Set the current variable's array element count to @p nelem.
674 * @param state The opcode state to update.
675 * @param nelem The new array length.
678 * @retval EINVAL if no open variable definition exists.
679 * @retval EINVAL if @p nelem is zero.
680 * @retval ENXIO if @p nelem is greater than one, and the current variable does
681 * not have an array type.
682 * @retval ENXIO if @p nelem exceeds the array length of the NVRAM variable
686 bhnd_sprom_opcode_set_nelem(bhnd_sprom_opcode_state *state, uint8_t nelem)
688 const struct bhnd_nvram_vardefn *var;
690 /* Must have a defined variable */
691 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
692 SPROM_OP_BAD(state, "array length set without open variable "
697 /* Locate the actual variable definition */
698 if ((var = bhnd_nvram_get_vardefn(state->vid)) == NULL) {
699 SPROM_OP_BAD(state, "unknown variable ID: %zu\n", state->vid);
703 /* Must be greater than zero */
705 SPROM_OP_BAD(state, "invalid nelem: %hhu\n", nelem);
709 /* If the variable is not an array-typed value, the array length
711 if (!bhnd_nvram_is_array_type(var->type) && nelem != 1) {
712 SPROM_OP_BAD(state, "nelem %hhu on non-array %zu\n", nelem,
717 /* Cannot exceed the variable's defined array length */
718 if (nelem > var->nelem) {
719 SPROM_OP_BAD(state, "nelem %hhu exceeds %zu length %hhu\n",
720 nelem, state->vid, var->nelem);
724 /* Valid length; update state */
725 state->var.nelem = nelem;
731 * Set the current variable ID to @p vid, and reset variable-specific
734 * @param state The opcode state to update.
735 * @param vid The new variable ID.
738 * @retval EINVAL if @p vid is not a valid variable ID.
741 bhnd_sprom_opcode_set_var(bhnd_sprom_opcode_state *state, size_t vid)
743 const struct bhnd_nvram_vardefn *var;
746 BHND_NV_ASSERT(state->var_state == SPROM_OPCODE_VAR_STATE_NONE,
747 ("overwrite of open variable definition"));
749 /* Locate the variable definition */
750 if ((var = bhnd_nvram_get_vardefn(vid)) == NULL) {
751 SPROM_OP_BAD(state, "unknown variable ID: %zu\n", vid);
755 /* Update vid and var state */
757 state->var_state = SPROM_OPCODE_VAR_STATE_OPEN;
759 /* Initialize default variable record values */
760 memset(&state->var, 0x0, sizeof(state->var));
762 /* Set initial base type */
763 if ((error = bhnd_sprom_opcode_set_type(state, var->type)))
766 /* Set default array length */
767 if ((error = bhnd_sprom_opcode_set_nelem(state, var->nelem)))
774 * Mark the currently open variable definition as complete.
776 * @param state The opcode state to update.
779 * @retval EINVAL if no incomplete open variable definition exists.
782 bhnd_sprom_opcode_end_var(bhnd_sprom_opcode_state *state)
784 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
785 SPROM_OP_BAD(state, "no open variable definition\n");
789 state->var_state = SPROM_OPCODE_VAR_STATE_DONE;
794 * Apply the current scale to @p value.
796 * @param state The SPROM opcode state.
797 * @param[in,out] value The value to scale
800 * @retval EINVAL if no open variable definition exists.
801 * @retval EINVAL if applying the current scale would overflow.
804 bhnd_sprom_opcode_apply_scale(bhnd_sprom_opcode_state *state, uint32_t *value)
806 /* Must have a defined variable (and thus, scale) */
807 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN) {
808 SPROM_OP_BAD(state, "scaled value encoded without open "
813 /* Applying the scale value must not overflow */
814 if (UINT32_MAX / state->var.scale < *value) {
815 SPROM_OP_BAD(state, "cannot represent %" PRIu32 " * %" PRIu32
816 "\n", *value, state->var.scale);
820 *value = (*value) * state->var.scale;
825 * Read a SPROM_OP_DATA_* value from @p opcodes.
827 * @param state The SPROM opcode state.
828 * @param type The SROM_OP_DATA_* type to be read.
829 * @param opval On success, the 32bit data representation. If @p type is signed,
830 * the value will be appropriately sign extended and may be directly cast to
834 * @retval non-zero If reading the value otherwise fails, a regular unix error
835 * code will be returned.
838 bhnd_sprom_opcode_read_opval32(bhnd_sprom_opcode_state *state, uint8_t type,
846 case SPROM_OP_DATA_I8:
847 /* Convert to signed value first, then sign extend */
848 *opval = (int32_t)(int8_t)(*p);
851 case SPROM_OP_DATA_U8:
855 case SPROM_OP_DATA_U8_SCALED:
858 if ((error = bhnd_sprom_opcode_apply_scale(state, opval)))
863 case SPROM_OP_DATA_U16:
867 case SPROM_OP_DATA_U32:
872 SPROM_OP_BAD(state, "unsupported data type: %hhu\n", type);
876 /* Update read address */
883 * Return true if our layout revision is currently defined by the SPROM
886 * This may be used to test whether the current opcode stream state applies
887 * to the layout that we are actually parsing.
889 * A given opcode stream may cover multiple layout revisions, switching
890 * between them prior to defining a set of variables.
893 bhnd_sprom_opcode_matches_layout_rev(bhnd_sprom_opcode_state *state)
895 return (bit_test(state->revs, state->layout->rev));
899 * When evaluating @p state and @p opcode, rewrite @p opcode based on the
900 * current evaluation state.
902 * This allows the insertion of implicit opcodes into interpretation of the
905 * If @p opcode is rewritten, it should be returned from
906 * bhnd_sprom_opcode_step() instead of the opcode parsed from @p state's opcode
909 * If @p opcode remains unmodified, then bhnd_sprom_opcode_step() should
910 * proceed to standard evaluation.
913 bhnd_sprom_opcode_rewrite_opcode(bhnd_sprom_opcode_state *state,
919 op = SPROM_OPCODE_OP(*opcode);
920 switch (state->var_state) {
921 case SPROM_OPCODE_VAR_STATE_NONE:
922 /* No open variable definition */
925 case SPROM_OPCODE_VAR_STATE_OPEN:
926 /* Open variable definition; check for implicit closure. */
929 * If a variable definition contains no explicit bind
930 * instructions prior to closure, we must generate a DO_BIND
931 * instruction with count and skip values of 1.
933 if (SPROM_OP_IS_VAR_END(op) &&
934 state->var.bind_total == 0)
936 uint8_t count, skip_in, skip_out;
937 bool skip_in_negative;
939 /* Create bind with skip_in/skip_out of 1, count of 1 */
943 skip_in_negative = false;
945 error = bhnd_sprom_opcode_set_bind(state, count,
946 skip_in, skip_in_negative, skip_out);
951 *opcode = SPROM_OPCODE_DO_BIND |
952 (0 << SPROM_OP_BIND_SKIP_IN_SIGN) |
953 (1 << SPROM_OP_BIND_SKIP_IN_SHIFT) |
954 (1 << SPROM_OP_BIND_SKIP_OUT_SHIFT);
960 * If a variable is implicitly closed (e.g. by a new variable
961 * definition), we must generate a VAR_END instruction.
963 if (SPROM_OP_IS_IMPLICIT_VAR_END(op)) {
964 /* Mark as complete */
965 if ((error = bhnd_sprom_opcode_end_var(state)))
969 *opcode = SPROM_OPCODE_VAR_END;
974 case SPROM_OPCODE_VAR_STATE_DONE:
975 /* Previously completed variable definition. Discard variable
977 return (bhnd_sprom_opcode_clear_var(state));
985 * Evaluate one opcode from @p state.
987 * @param state The opcode state to be evaluated.
988 * @param[out] opcode On success, the evaluated opcode
991 * @retval ENOENT if EOF is reached
992 * @retval non-zero if evaluation otherwise fails, a regular unix error
993 * code will be returned.
996 bhnd_sprom_opcode_step(bhnd_sprom_opcode_state *state, uint8_t *opcode)
1000 while (*state->input != SPROM_OPCODE_EOF) {
1002 uint8_t op, rewrite, immd;
1005 *opcode = *state->input;
1006 op = SPROM_OPCODE_OP(*opcode);
1007 immd = SPROM_OPCODE_IMM(*opcode);
1009 /* Clear any existing bind state */
1010 if ((error = bhnd_sprom_opcode_flush_bind(state)))
1013 /* Insert local opcode based on current state? */
1015 if ((error = bhnd_sprom_opcode_rewrite_opcode(state, &rewrite)))
1018 if (rewrite != *opcode) {
1019 /* Provide rewritten opcode */
1022 /* We must keep evaluating until we hit a state
1023 * applicable to the SPROM revision we're parsing */
1024 if (!bhnd_sprom_opcode_matches_layout_rev(state))
1034 case SPROM_OPCODE_VAR_IMM:
1035 if ((error = bhnd_sprom_opcode_set_var(state, immd)))
1039 case SPROM_OPCODE_VAR_REL_IMM:
1040 error = bhnd_sprom_opcode_set_var(state,
1046 case SPROM_OPCODE_VAR:
1047 error = bhnd_sprom_opcode_read_opval32(state, immd,
1052 if ((error = bhnd_sprom_opcode_set_var(state, val)))
1057 case SPROM_OPCODE_VAR_END:
1058 if ((error = bhnd_sprom_opcode_end_var(state)))
1062 case SPROM_OPCODE_NELEM:
1063 immd = *state->input;
1064 if ((error = bhnd_sprom_opcode_set_nelem(state, immd)))
1070 case SPROM_OPCODE_DO_BIND:
1071 case SPROM_OPCODE_DO_BINDN: {
1072 uint8_t count, skip_in, skip_out;
1073 bool skip_in_negative;
1075 /* Fetch skip arguments */
1076 skip_in = (immd & SPROM_OP_BIND_SKIP_IN_MASK) >>
1077 SPROM_OP_BIND_SKIP_IN_SHIFT;
1080 ((immd & SPROM_OP_BIND_SKIP_IN_SIGN) != 0);
1082 skip_out = (immd & SPROM_OP_BIND_SKIP_OUT_MASK) >>
1083 SPROM_OP_BIND_SKIP_OUT_SHIFT;
1085 /* Fetch count argument (if any) */
1086 if (op == SPROM_OPCODE_DO_BINDN) {
1087 /* Count is provided as trailing U8 */
1088 count = *state->input;
1094 /* Set BIND state */
1095 error = bhnd_sprom_opcode_set_bind(state, count,
1096 skip_in, skip_in_negative, skip_out);
1102 case SPROM_OPCODE_DO_BINDN_IMM: {
1103 uint8_t count, skip_in, skip_out;
1104 bool skip_in_negative;
1106 /* Implicit skip_in/skip_out of 1, count encoded as immd
1111 skip_in_negative = false;
1113 error = bhnd_sprom_opcode_set_bind(state, count,
1114 skip_in, skip_in_negative, skip_out);
1120 case SPROM_OPCODE_REV_IMM:
1121 error = bhnd_sprom_opcode_set_revs(state, immd, immd);
1126 case SPROM_OPCODE_REV_RANGE: {
1128 uint8_t rstart, rend;
1130 /* Revision range is encoded in next byte, as
1131 * { uint8_t start:4, uint8_t end:4 } */
1132 range = *state->input;
1133 rstart = (range & SPROM_OP_REV_START_MASK) >>
1134 SPROM_OP_REV_START_SHIFT;
1135 rend = (range & SPROM_OP_REV_END_MASK) >>
1136 SPROM_OP_REV_END_SHIFT;
1138 /* Update revision bitmask */
1139 error = bhnd_sprom_opcode_set_revs(state, rstart, rend);
1147 case SPROM_OPCODE_MASK_IMM:
1148 if ((error = bhnd_sprom_opcode_set_mask(state, immd)))
1152 case SPROM_OPCODE_MASK:
1153 error = bhnd_sprom_opcode_read_opval32(state, immd,
1158 if ((error = bhnd_sprom_opcode_set_mask(state, val)))
1162 case SPROM_OPCODE_SHIFT_IMM:
1163 error = bhnd_sprom_opcode_set_shift(state, immd * 2);
1168 case SPROM_OPCODE_SHIFT: {
1171 if (immd == SPROM_OP_DATA_I8) {
1172 shift = (int8_t)(*state->input);
1173 } else if (immd == SPROM_OP_DATA_U8) {
1174 val = *state->input;
1175 if (val > INT8_MAX) {
1176 SPROM_OP_BAD(state, "invalid shift "
1177 "value: %#x\n", val);
1182 SPROM_OP_BAD(state, "unsupported shift data "
1183 "type: %#hhx\n", immd);
1187 if ((error = bhnd_sprom_opcode_set_shift(state, shift)))
1193 case SPROM_OPCODE_OFFSET_REL_IMM:
1194 /* Fetch unscaled relative offset */
1198 error = bhnd_sprom_opcode_apply_scale(state, &val);
1202 /* Adding val must not overflow our offset */
1203 if (UINT32_MAX - state->offset < val) {
1204 BHND_NV_LOG("offset out of range\n");
1209 state->offset += val;
1211 case SPROM_OPCODE_OFFSET:
1212 error = bhnd_sprom_opcode_read_opval32(state, immd,
1217 state->offset = val;
1220 case SPROM_OPCODE_TYPE:
1221 /* Type follows as U8 */
1222 immd = *state->input;
1226 case SPROM_OPCODE_TYPE_IMM:
1228 case BHND_NVRAM_TYPE_UINT8:
1229 case BHND_NVRAM_TYPE_UINT16:
1230 case BHND_NVRAM_TYPE_UINT32:
1231 case BHND_NVRAM_TYPE_UINT64:
1232 case BHND_NVRAM_TYPE_INT8:
1233 case BHND_NVRAM_TYPE_INT16:
1234 case BHND_NVRAM_TYPE_INT32:
1235 case BHND_NVRAM_TYPE_INT64:
1236 case BHND_NVRAM_TYPE_CHAR:
1237 case BHND_NVRAM_TYPE_STRING:
1238 error = bhnd_sprom_opcode_set_type(state,
1239 (bhnd_nvram_type)immd);
1244 BHND_NV_LOG("unrecognized type %#hhx\n", immd);
1250 BHND_NV_LOG("unrecognized opcode %#hhx\n", *opcode);
1254 /* We must keep evaluating until we hit a state applicable to
1255 * the SPROM revision we're parsing */
1256 if (bhnd_sprom_opcode_matches_layout_rev(state))
1260 /* End of opcode stream */
1265 * Reset SPROM opcode evaluation state, seek to the @p entry's position,
1266 * and perform complete evaluation of the variable's opcodes.
1268 * @param state The opcode state to be to be evaluated.
1269 * @param entry The indexed variable location.
1272 * @retval non-zero If evaluation fails, a regular unix error code will be
1276 bhnd_sprom_opcode_eval_var(bhnd_sprom_opcode_state *state,
1277 bhnd_sprom_opcode_idx_entry *entry)
1283 if ((error = bhnd_sprom_opcode_seek(state, entry)))
1286 /* Parse full variable definition */
1287 while ((error = bhnd_sprom_opcode_step(state, &opcode)) == 0) {
1288 /* Iterate until VAR_END */
1289 if (SPROM_OPCODE_OP(opcode) != SPROM_OPCODE_VAR_END)
1292 BHND_NV_ASSERT(state->var_state == SPROM_OPCODE_VAR_STATE_DONE,
1293 ("incomplete variable definition"));
1298 /* Error parsing definition */
1303 * Evaluate @p state until the next variable definition is found.
1305 * @param state The opcode state to be evaluated.
1308 * @retval ENOENT if no additional variable definitions are available.
1309 * @retval non-zero if evaluation otherwise fails, a regular unix error
1310 * code will be returned.
1313 bhnd_sprom_opcode_next_var(bhnd_sprom_opcode_state *state)
1318 /* Step until we hit a variable opcode */
1319 while ((error = bhnd_sprom_opcode_step(state, &opcode)) == 0) {
1320 switch (SPROM_OPCODE_OP(opcode)) {
1321 case SPROM_OPCODE_VAR:
1322 case SPROM_OPCODE_VAR_IMM:
1323 case SPROM_OPCODE_VAR_REL_IMM:
1325 state->var_state == SPROM_OPCODE_VAR_STATE_OPEN,
1326 ("missing variable definition"));
1334 /* Reached EOF, or evaluation failed */
1339 * Evaluate @p state until the next binding for the current variable definition
1342 * @param state The opcode state to be evaluated.
1345 * @retval ENOENT if no additional binding opcodes are found prior to reaching
1346 * a new variable definition, or the end of @p state's binding opcodes.
1347 * @retval non-zero if evaluation otherwise fails, a regular unix error
1348 * code will be returned.
1351 bhnd_sprom_opcode_next_binding(bhnd_sprom_opcode_state *state)
1356 if (state->var_state != SPROM_OPCODE_VAR_STATE_OPEN)
1359 /* Step until we hit a bind opcode, or a new variable */
1360 while ((error = bhnd_sprom_opcode_step(state, &opcode)) == 0) {
1361 switch (SPROM_OPCODE_OP(opcode)) {
1362 case SPROM_OPCODE_DO_BIND:
1363 case SPROM_OPCODE_DO_BINDN:
1364 case SPROM_OPCODE_DO_BINDN_IMM:
1365 /* Found next bind */
1367 state->var_state == SPROM_OPCODE_VAR_STATE_OPEN,
1368 ("missing variable definition"));
1369 BHND_NV_ASSERT(state->var.have_bind, ("missing bind"));
1373 case SPROM_OPCODE_VAR_END:
1374 /* No further binding opcodes */
1376 state->var_state == SPROM_OPCODE_VAR_STATE_DONE,
1377 ("variable definition still available"));
1382 /* Not found, or evaluation failed */