Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*-
 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 *    redistribution must be conditioned upon including a substantially
 *    similar Disclaimer requirement for further binary redistribution.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGES.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#ifdef _KERNEL
#include <sys/param.h>
#include <sys/ctype.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#else /* !_KERNEL */
#include <ctype.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif /* _KERNEL */

#include "bhnd_nvram_private.h"

#include "bhnd_nvram_datavar.h"

#include "bhnd_nvram_data_tlvreg.h"

/*
 * CFE TLV NVRAM data class.
 * 
 * The CFE-defined TLV NVRAM format is used on the WGT634U.
 */

struct bhnd_nvram_tlv {
	struct bhnd_nvram_data	 nv;	/**< common instance state */
	struct bhnd_nvram_io	*data;	/**< backing buffer */
	size_t			 count;	/**< variable count */
};

BHND_NVRAM_DATA_CLASS_DEFN(tlv, "WGT634U", BHND_NVRAM_DATA_CAP_DEVPATHS,
    sizeof(struct bhnd_nvram_tlv))

/** Minimal TLV_ENV record header */
struct bhnd_nvram_tlv_env_hdr {
	uint8_t		tag;
	uint8_t		size;
} __packed;

/** Minimal TLV_ENV record */
struct bhnd_nvram_tlv_env {
	struct bhnd_nvram_tlv_env_hdr	hdr;
	uint8_t				flags;
	char				envp[];
} __packed;

/* Return the length in bytes of an TLV_ENV's envp data */
#define	NVRAM_TLV_ENVP_DATA_LEN(_env)	\
	(((_env)->hdr.size < sizeof((_env)->flags)) ? 0 :	\
	    ((_env)->hdr.size - sizeof((_env)->flags)))

/* Maximum supported length of the envp data field, in bytes */
#define	NVRAM_TLV_ENVP_DATA_MAX_LEN	\
	(UINT8_MAX - sizeof(uint8_t) /* flags */)

static int				 bhnd_nvram_tlv_parse_size(
					     struct bhnd_nvram_io *io,
					     size_t *size);

static int				 bhnd_nvram_tlv_next_record(
					     struct bhnd_nvram_io *io,
					     size_t *next, size_t *offset,
					     uint8_t *tag);

static struct bhnd_nvram_tlv_env	*bhnd_nvram_tlv_next_env(
					     struct bhnd_nvram_tlv *tlv,
					     size_t *next, void **cookiep);

static struct bhnd_nvram_tlv_env	*bhnd_nvram_tlv_get_env(
					     struct bhnd_nvram_tlv *tlv,
					     void *cookiep);

static void				*bhnd_nvram_tlv_to_cookie(
					     struct bhnd_nvram_tlv *tlv,
					     size_t io_offset);
static size_t				 bhnd_nvram_tlv_to_offset(
					     struct bhnd_nvram_tlv *tlv,
					     void *cookiep);

static int
bhnd_nvram_tlv_probe(struct bhnd_nvram_io *io)
{
	struct bhnd_nvram_tlv_env	ident;
	size_t				nbytes;
	int				error;

	nbytes = bhnd_nvram_io_getsize(io);

	/* Handle what might be an empty TLV image */
	if (nbytes < sizeof(ident)) {
		uint8_t tag;

		/* Fetch just the first tag */
		error = bhnd_nvram_io_read(io, 0x0, &tag, sizeof(tag));
		if (error)
			return (error);

		/* This *could* be an empty TLV image, but all we're
		 * testing for here is a single 0x0 byte followed by EOF */
		if (tag == NVRAM_TLV_TYPE_END)
			return (BHND_NVRAM_DATA_PROBE_MAYBE);

		return (ENXIO);
	}

	/* Otherwise, look at the initial header for a valid TLV ENV tag,
	 * plus one byte of the entry data */
	error = bhnd_nvram_io_read(io, 0x0, &ident,
	    sizeof(ident) + sizeof(ident.envp[0]));
	if (error)
		return (error);

	/* First entry should be a variable record (which we statically
	 * assert as being defined to use a single byte size field) */
	if (ident.hdr.tag != NVRAM_TLV_TYPE_ENV)
		return (ENXIO);

	_Static_assert(NVRAM_TLV_TYPE_ENV & NVRAM_TLV_TF_U8_LEN,
	    "TYPE_ENV is not a U8-sized field");

	/* The entry must be at least 3 characters ('x=\0') in length */
	if (ident.hdr.size < 3)
		return (ENXIO);

	/* The first character should be a valid key char (alpha) */
	if (!bhnd_nv_isalpha(ident.envp[0]))
		return (ENXIO);

	return (BHND_NVRAM_DATA_PROBE_DEFAULT);
}

static int
bhnd_nvram_tlv_getvar_direct(struct bhnd_nvram_io *io, const char *name,
    void *buf, size_t *len, bhnd_nvram_type type)
{
	struct bhnd_nvram_tlv_env	 env;
	char				 data[NVRAM_TLV_ENVP_DATA_MAX_LEN];
	size_t				 data_len;
	const char			*key, *value;
	size_t				 keylen, vlen;
	size_t				 namelen;
	size_t				 next, off;
	uint8_t				 tag;
	int				 error;

	namelen = strlen(name);

	/* Iterate over the input looking for the requested variable */
	next = 0;
	while (!(error = bhnd_nvram_tlv_next_record(io, &next, &off, &tag))) {
		switch (tag) {
		case NVRAM_TLV_TYPE_END:
			/* Not found */
			return (ENOENT);

		case NVRAM_TLV_TYPE_ENV:
			/* Read the record header */
			error = bhnd_nvram_io_read(io, off, &env, sizeof(env));
			if (error) {
				BHND_NV_LOG("error reading TLV_ENV record "
				    "header: %d\n", error);
				return (error);
			}

			/* Read the record data */
			data_len = NVRAM_TLV_ENVP_DATA_LEN(&env);
			error = bhnd_nvram_io_read(io, off + sizeof(env), data,
			    data_len);
			if (error) {
				BHND_NV_LOG("error reading TLV_ENV record "
				    "data: %d\n", error);
				return (error);
			}

			/* Parse the key=value string */
			error = bhnd_nvram_parse_env(data, data_len, '=', &key,
			    &keylen, &value, &vlen);
			if (error) {
				BHND_NV_LOG("error parsing TLV_ENV data: %d\n",
				    error);
				return (error);
			}

			/* Match against requested variable name */
			if (keylen == namelen && 
			    strncmp(key, name, namelen) == 0)
			{
				return (bhnd_nvram_value_coerce(value, vlen,
				    BHND_NVRAM_TYPE_STRING, buf, len, type));
			}

			break;

		default:
			/* Skip unknown tags */
			break;
		}
	}

	/* Hit I/O error */
	return (error);
}

static int
bhnd_nvram_tlv_serialize(bhnd_nvram_data_class *cls, bhnd_nvram_plist *props,
    bhnd_nvram_plist *options, void *outp, size_t *olen)
{
	bhnd_nvram_prop	*prop;
	size_t		 limit, nbytes;
	int		 error;

	/* Determine output byte limit */
	if (outp != NULL)
		limit = *olen;
	else
		limit = 0;

	nbytes = 0;

	/* Write all properties */
	prop = NULL;
	while ((prop = bhnd_nvram_plist_next(props, prop)) != NULL) {
		struct bhnd_nvram_tlv_env	 env;
		const char			*name;
		uint8_t				*p;
		size_t				 name_len, value_len;
		size_t				 rec_size;

		env.hdr.tag = NVRAM_TLV_TYPE_ENV;
		env.hdr.size = sizeof(env.flags);
		env.flags = 0x0;

		/* Fetch name value and add to record length */
		name = bhnd_nvram_prop_name(prop);
		name_len = strlen(name) + 1 /* '=' */;

		if (UINT8_MAX - env.hdr.size < name_len) {
			BHND_NV_LOG("%s name exceeds maximum TLV record "
			    "length\n", name);
			return (EFTYPE); /* would overflow TLV size */
		}

		env.hdr.size += name_len;

		/* Add string value to record length */
		error = bhnd_nvram_prop_encode(prop, NULL, &value_len,
		    BHND_NVRAM_TYPE_STRING);
		if (error) {
			BHND_NV_LOG("error serializing %s to required type "
			    "%s: %d\n", name,
			    bhnd_nvram_type_name(BHND_NVRAM_TYPE_STRING),
			    error);
			return (error);
		}

		if (UINT8_MAX - env.hdr.size < value_len) {
			BHND_NV_LOG("%s value exceeds maximum TLV record "
			    "length\n", name);
			return (EFTYPE); /* would overflow TLV size */
		}

		env.hdr.size += value_len;

		/* Calculate total record size */
		rec_size = sizeof(env.hdr) + env.hdr.size;
		if (SIZE_MAX - nbytes < rec_size)
			return (EFTYPE); /* would overflow size_t */

		/* Calculate our output pointer */
		if (nbytes > limit || limit - nbytes < rec_size) {
			/* buffer is full; cannot write */
			p = NULL;
		} else {
			p = (uint8_t *)outp + nbytes;
		}

		/* Write to output */
		if (p != NULL) {
			memcpy(p, &env, sizeof(env));
			p += sizeof(env);

			memcpy(p, name, name_len - 1);
			p[name_len - 1] = '=';
			p += name_len;

			error = bhnd_nvram_prop_encode(prop, p, &value_len,
			    BHND_NVRAM_TYPE_STRING);
			if (error) {
				BHND_NV_LOG("error serializing %s to required "
				    "type %s: %d\n", name,
				    bhnd_nvram_type_name(
					BHND_NVRAM_TYPE_STRING),
				    error);
				return (error);
			}
		}

		nbytes += rec_size;
	}

	/* Write terminating END record */
	if (limit > nbytes)
		*((uint8_t *)outp + nbytes) = NVRAM_TLV_TYPE_END;

	if (nbytes == SIZE_MAX)
		return (EFTYPE); /* would overflow size_t */
	nbytes++;

	/* Provide required length */
	*olen = nbytes;
	if (limit < *olen) {
		if (outp == NULL)
			return (0);

		return (ENOMEM);
	}

	return (0);
}

/**
 * Initialize @p tlv with the provided NVRAM TLV data mapped by @p src.
 * 
 * @param tlv A newly allocated data instance.
 */
static int
bhnd_nvram_tlv_init(struct bhnd_nvram_tlv *tlv, struct bhnd_nvram_io *src)
{
	struct bhnd_nvram_tlv_env	*env;
	size_t				 size;
	size_t				 next;
	int				 error;

	BHND_NV_ASSERT(tlv->data == NULL, ("tlv data already initialized"));

	/* Determine the actual size of the TLV source data */
	if ((error = bhnd_nvram_tlv_parse_size(src, &size)))
		return (error);

	/* Copy to our own internal buffer */
	if ((tlv->data = bhnd_nvram_iobuf_copy_range(src, 0x0, size)) == NULL)
		return (ENOMEM);

	/* Initialize our backing buffer */
	tlv->count = 0;
	next = 0;
	while ((env = bhnd_nvram_tlv_next_env(tlv, &next, NULL)) != NULL) {
		size_t env_len;
		size_t name_len;

		/* TLV_ENV data must not be empty */
		env_len = NVRAM_TLV_ENVP_DATA_LEN(env);
		if (env_len == 0) {
			BHND_NV_LOG("cannot parse zero-length TLV_ENV record "
			    "data\n");
			return (EINVAL);
		}

		/* Parse the key=value string, and then replace the '='
		 * delimiter with '\0' to allow us to provide direct 
		 * name pointers from our backing buffer */
		error = bhnd_nvram_parse_env(env->envp, env_len, '=', NULL,
		    &name_len, NULL, NULL);
		if (error) {
			BHND_NV_LOG("error parsing TLV_ENV data: %d\n", error);
			return (error);
		}

		/* Replace '=' with '\0' */
		*(env->envp + name_len) = '\0';

		/* Add to variable count */
		tlv->count++;
	};

	return (0);
}

static int
bhnd_nvram_tlv_new(struct bhnd_nvram_data *nv, struct bhnd_nvram_io *io)
{

	struct bhnd_nvram_tlv	*tlv;
	int			 error;

	/* Allocate and initialize the TLV data instance */
	tlv = (struct bhnd_nvram_tlv *)nv;

	/* Parse the TLV input data and initialize our backing
	 * data representation */
	if ((error = bhnd_nvram_tlv_init(tlv, io))) {
		bhnd_nvram_tlv_free(nv);
		return (error);
	}

	return (0);
}

static void
bhnd_nvram_tlv_free(struct bhnd_nvram_data *nv)
{
	struct bhnd_nvram_tlv *tlv = (struct bhnd_nvram_tlv *)nv;
	if (tlv->data != NULL)
		bhnd_nvram_io_free(tlv->data);
}

size_t
bhnd_nvram_tlv_count(struct bhnd_nvram_data *nv)
{
	struct bhnd_nvram_tlv *tlv = (struct bhnd_nvram_tlv *)nv;
	return (tlv->count);
}

static bhnd_nvram_plist *
bhnd_nvram_tlv_options(struct bhnd_nvram_data *nv)
{
	return (NULL);
}

static uint32_t
bhnd_nvram_tlv_caps(struct bhnd_nvram_data *nv)
{
	return (BHND_NVRAM_DATA_CAP_READ_PTR|BHND_NVRAM_DATA_CAP_DEVPATHS);
}

static const char *
bhnd_nvram_tlv_next(struct bhnd_nvram_data *nv, void **cookiep)
{
	struct bhnd_nvram_tlv		*tlv;
	struct bhnd_nvram_tlv_env	*env;
	size_t				 io_offset;

	tlv = (struct bhnd_nvram_tlv *)nv;

	/* Find next readable TLV record */
	if (*cookiep == NULL) {
		/* Start search at offset 0x0 */
		io_offset = 0x0;
		env = bhnd_nvram_tlv_next_env(tlv, &io_offset, cookiep);
	} else {
		/* Seek past the previous env record */
		io_offset = bhnd_nvram_tlv_to_offset(tlv, *cookiep);
		env = bhnd_nvram_tlv_next_env(tlv, &io_offset, NULL);
		if (env == NULL)
			BHND_NV_PANIC("invalid cookiep; record missing");

		/* Advance to next env record, update the caller's cookiep */
		env = bhnd_nvram_tlv_next_env(tlv, &io_offset, cookiep);
	}

	/* Check for EOF */
	if (env == NULL)
		return (NULL);

	/* Return the NUL terminated name */
	return (env->envp);
}

static void *
bhnd_nvram_tlv_find(struct bhnd_nvram_data *nv, const char *name)
{
	return (bhnd_nvram_data_generic_find(nv, name));
}

static int
bhnd_nvram_tlv_getvar_order(struct bhnd_nvram_data *nv, void *cookiep1,
    void *cookiep2)
{
	if (cookiep1 < cookiep2)
		return (-1);

	if (cookiep1 > cookiep2)
		return (1);

	return (0);
}

static int
bhnd_nvram_tlv_getvar(struct bhnd_nvram_data *nv, void *cookiep, void *buf,
    size_t *len, bhnd_nvram_type type)
{
	return (bhnd_nvram_data_generic_rp_getvar(nv, cookiep, buf, len, type));
}

static int
bhnd_nvram_tlv_copy_val(struct bhnd_nvram_data *nv, void *cookiep,
    bhnd_nvram_val **value)
{
	return (bhnd_nvram_data_generic_rp_copy_val(nv, cookiep, value));
}

static const void *
bhnd_nvram_tlv_getvar_ptr(struct bhnd_nvram_data *nv, void *cookiep,
    size_t *len, bhnd_nvram_type *type)
{
	struct bhnd_nvram_tlv		*tlv;
	struct bhnd_nvram_tlv_env	*env;
	const char			*val;
	int				 error;

	tlv = (struct bhnd_nvram_tlv *)nv;

	/* Fetch pointer to the TLV_ENV record */
	if ((env = bhnd_nvram_tlv_get_env(tlv, cookiep)) == NULL)
		BHND_NV_PANIC("invalid cookiep: %p", cookiep);

	/* Parse value pointer and length from key\0value data */
	error = bhnd_nvram_parse_env(env->envp, NVRAM_TLV_ENVP_DATA_LEN(env),
	    '\0', NULL, NULL, &val, len);
	if (error)
		BHND_NV_PANIC("unexpected error parsing '%s'", env->envp);

	/* Type is always CSTR */
	*type = BHND_NVRAM_TYPE_STRING;

	return (val);
}

static const char *
bhnd_nvram_tlv_getvar_name(struct bhnd_nvram_data *nv, void *cookiep)
{
	struct bhnd_nvram_tlv		*tlv;
	const struct bhnd_nvram_tlv_env	*env;

	tlv = (struct bhnd_nvram_tlv *)nv;

	/* Fetch pointer to the TLV_ENV record */
	if ((env = bhnd_nvram_tlv_get_env(tlv, cookiep)) == NULL)
		BHND_NV_PANIC("invalid cookiep: %p", cookiep);

	/* Return name pointer */
	return (&env->envp[0]);
}

static int
bhnd_nvram_tlv_filter_setvar(struct bhnd_nvram_data *nv, const char *name,
    bhnd_nvram_val *value, bhnd_nvram_val **result)
{
	bhnd_nvram_val	*str;
	const char	*inp;
	bhnd_nvram_type	 itype;
	size_t		 ilen;
	size_t		 name_len, tlv_nremain;
	int		 error;

	tlv_nremain = NVRAM_TLV_ENVP_DATA_MAX_LEN;

	/* Name (trimmed of any path prefix) must be valid */
	if (!bhnd_nvram_validate_name(bhnd_nvram_trim_path_name(name)))
		return (EINVAL);

	/* 'name=' must fit within the maximum TLV_ENV record length */
	name_len = strlen(name) + 1; /* '=' */
	if (tlv_nremain < name_len) {
		BHND_NV_LOG("'%s=' exceeds maximum TLV_ENV record length\n",
		    name);
		return (EINVAL);
	}
	tlv_nremain -= name_len;

	/* Convert value to a (bcm-formatted) string */
	error = bhnd_nvram_val_convert_new(&str, &bhnd_nvram_val_bcm_string_fmt,
	    value, BHND_NVRAM_VAL_DYNAMIC);
	if (error)
		return (error);

	/* The string value must fit within remaining TLV_ENV record length */
	inp = bhnd_nvram_val_bytes(str, &ilen, &itype);
	if (tlv_nremain < ilen) {
		BHND_NV_LOG("'%.*s\\0' exceeds maximum TLV_ENV record length\n",
		    BHND_NV_PRINT_WIDTH(ilen), inp);

		bhnd_nvram_val_release(str);
		return (EINVAL);
	}
	tlv_nremain -= name_len;

	/* Success. Transfer result ownership to the caller. */
	*result = str;
	return (0);
}

static int
bhnd_nvram_tlv_filter_unsetvar(struct bhnd_nvram_data *nv, const char *name)
{
	/* We permit deletion of any variable */
	return (0);
}

/**
 * Iterate over the records starting at @p next, returning the parsed
 * record's @p tag, @p size, and @p offset.
 * 
 * @param		io		The I/O context to parse.
 * @param[in,out]	next		The next offset to be parsed, or 0x0
 *					to begin parsing. Upon successful
 *					return, will be set to the offset of the
 *					next record (or EOF, if
 *					NVRAM_TLV_TYPE_END was parsed).
 * @param[out]		offset		The record's value offset.
 * @param[out]		tag		The record's tag.
 * 
 * @retval 0		success
 * @retval EINVAL	if parsing @p io as TLV fails.
 * @retval non-zero	if reading @p io otherwise fails, a regular unix error
 *			code will be returned.
 */
static int
bhnd_nvram_tlv_next_record(struct bhnd_nvram_io *io, size_t *next, size_t
    *offset, uint8_t *tag)
{
	size_t		io_offset, io_size;
	uint16_t	parsed_len;
	uint8_t		len_hdr[2];
	int		error;

	io_offset = *next;
	io_size = bhnd_nvram_io_getsize(io);

	/* Save the record offset */
	if (offset != NULL)
		*offset = io_offset;

	/* Fetch initial tag */
	error = bhnd_nvram_io_read(io, io_offset, tag, sizeof(*tag));
	if (error)
		return (error);
	io_offset++;

	/* EOF */
	if (*tag == NVRAM_TLV_TYPE_END) {
		*next = io_offset;
		return (0);
	}

	/* Read length field */
	if (*tag & NVRAM_TLV_TF_U8_LEN) {
		error = bhnd_nvram_io_read(io, io_offset, &len_hdr,
		    sizeof(len_hdr[0]));
		if (error) {
			BHND_NV_LOG("error reading TLV record size: %d\n",
			    error);
			return (error);
		}

		parsed_len = len_hdr[0];
		io_offset++;
	} else {
		error = bhnd_nvram_io_read(io, io_offset, &len_hdr,
		    sizeof(len_hdr));
		if (error) {
			BHND_NV_LOG("error reading 16-bit TLV record "
			    "size: %d\n", error);
			return (error);
		}

		parsed_len = (len_hdr[0] << 8) | len_hdr[1];
		io_offset += 2;
	}

	/* Advance to next record */
	if (parsed_len > io_size || io_size - parsed_len < io_offset) {
		/* Hit early EOF */
		BHND_NV_LOG("TLV record length %hu truncated by input "
		    "size of %zu\n", parsed_len, io_size);
		return (EINVAL);
	}

	*next = io_offset + parsed_len;

	/* Valid record found */
	return (0);
}

/**
 * Parse the TLV data in @p io to determine the total size of the TLV
 * data mapped by @p io (which may be less than the size of @p io).
 */
static int
bhnd_nvram_tlv_parse_size(struct bhnd_nvram_io *io, size_t *size)
{
	size_t		next;
	uint8_t		tag;
	int		error;

	/* We have to perform a minimal parse to determine the actual length */
	next = 0x0;
	*size = 0x0;

	/* Iterate over the input until we hit END tag or the read fails */
	do {
		error = bhnd_nvram_tlv_next_record(io, &next, NULL, &tag);
		if (error)
			return (error);
	} while (tag != NVRAM_TLV_TYPE_END);

	/* Offset should now point to EOF */
	BHND_NV_ASSERT(next <= bhnd_nvram_io_getsize(io),
	    ("parse returned invalid EOF offset"));

	*size = next;
	return (0);
}

/**
 * Iterate over the records in @p tlv, returning a pointer to the next
 * NVRAM_TLV_TYPE_ENV record, or NULL if EOF is reached.
 * 
 * @param		tlv		The TLV instance.
 * @param[in,out]	next		The next offset to be parsed, or 0x0
 *					to begin parsing. Upon successful
 *					return, will be set to the offset of the
 *					next record.
 */
static struct bhnd_nvram_tlv_env *
bhnd_nvram_tlv_next_env(struct bhnd_nvram_tlv *tlv, size_t *next,
    void **cookiep)
{
	uint8_t	tag;
	int	error;

	/* Find the next TLV_ENV record, starting at @p next */
	do {
		void	*c;
		size_t	 offset;

		/* Fetch the next TLV record */
		error = bhnd_nvram_tlv_next_record(tlv->data, next, &offset,
		    &tag);
		if (error) {
			BHND_NV_LOG("unexpected error in next_record(): %d\n",
			    error);
			return (NULL);
		}

		/* Only interested in ENV records */
		if (tag != NVRAM_TLV_TYPE_ENV)
			continue;

		/* Map and return TLV_ENV record pointer */
		c = bhnd_nvram_tlv_to_cookie(tlv, offset);

		/* Provide the cookiep value for the returned record */
		if (cookiep != NULL)
			*cookiep = c;

		return (bhnd_nvram_tlv_get_env(tlv, c));
	} while (tag != NVRAM_TLV_TYPE_END);

	/* No remaining ENV records */
	return (NULL);
}

/**
 * Return a pointer to the TLV_ENV record for @p cookiep, or NULL
 * if none vailable.
 */
static struct bhnd_nvram_tlv_env *
bhnd_nvram_tlv_get_env(struct bhnd_nvram_tlv *tlv, void *cookiep)
{
	struct bhnd_nvram_tlv_env	*env;
	void				*ptr;
	size_t				 navail;
	size_t				 io_offset, io_size;
	int				 error;

	io_size = bhnd_nvram_io_getsize(tlv->data);
	io_offset = bhnd_nvram_tlv_to_offset(tlv, cookiep);

	/* At EOF? */
	if (io_offset == io_size)
		return (NULL);

	/* Fetch non-const pointer to the record entry */
	error = bhnd_nvram_io_write_ptr(tlv->data, io_offset, &ptr,
	    sizeof(env->hdr), &navail);
	if (error) {
		/* Should never occur with a valid cookiep */
		BHND_NV_LOG("error mapping record for cookiep: %d\n", error);
		return (NULL);
	}

	/* Validate the record pointer */
	env = ptr;
	if (env->hdr.tag != NVRAM_TLV_TYPE_ENV) {
		/* Should never occur with a valid cookiep */
		BHND_NV_LOG("non-ENV record mapped for %p\n", cookiep);
		return (NULL);
	}

	/* Is the required variable name data is mapped? */
	if (navail < sizeof(struct bhnd_nvram_tlv_env_hdr) + env->hdr.size ||
	    env->hdr.size == sizeof(env->flags))
	{
		/* Should never occur with a valid cookiep */
		BHND_NV_LOG("TLV_ENV variable data not mapped for %p\n",
		    cookiep);
		return (NULL);
	}

	return (env);
}

/**
 * Return a cookiep for the given I/O offset.
 */
static void *
bhnd_nvram_tlv_to_cookie(struct bhnd_nvram_tlv *tlv, size_t io_offset)
{
	const void	*ptr;
	int		 error;

	BHND_NV_ASSERT(io_offset < bhnd_nvram_io_getsize(tlv->data),
	    ("io_offset %zu out-of-range", io_offset));
	BHND_NV_ASSERT(io_offset < UINTPTR_MAX,
	    ("io_offset %#zx exceeds UINTPTR_MAX", io_offset));

	error = bhnd_nvram_io_read_ptr(tlv->data, 0x0, &ptr, io_offset, NULL);
	if (error)
		BHND_NV_PANIC("error mapping offset %zu: %d", io_offset, error);

	ptr = (const uint8_t *)ptr + io_offset;
	return (__DECONST(void *, ptr));
}

/* Convert a cookiep back to an I/O offset */
static size_t
bhnd_nvram_tlv_to_offset(struct bhnd_nvram_tlv *tlv, void *cookiep)
{
	const void	*ptr;
	intptr_t	 offset;
	size_t		 io_size;
	int		 error;

	BHND_NV_ASSERT(cookiep != NULL, ("null cookiep"));

	io_size = bhnd_nvram_io_getsize(tlv->data);

	error = bhnd_nvram_io_read_ptr(tlv->data, 0x0, &ptr, io_size, NULL);
	if (error)
		BHND_NV_PANIC("error mapping offset %zu: %d", io_size, error);

	offset = (const uint8_t *)cookiep - (const uint8_t *)ptr;
	BHND_NV_ASSERT(offset >= 0, ("invalid cookiep"));
	BHND_NV_ASSERT((uintptr_t)offset < SIZE_MAX, ("cookiep > SIZE_MAX)"));
	BHND_NV_ASSERT((uintptr_t)offset <= io_size, ("cookiep > io_size)"));

	return ((size_t)offset);
}