1*77cb4d3eSLandon J. Fuller /*-
2*77cb4d3eSLandon J. Fuller * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
3*77cb4d3eSLandon J. Fuller * All rights reserved.
4*77cb4d3eSLandon J. Fuller *
5*77cb4d3eSLandon J. Fuller * Redistribution and use in source and binary forms, with or without
6*77cb4d3eSLandon J. Fuller * modification, are permitted provided that the following conditions
7*77cb4d3eSLandon J. Fuller * are met:
8*77cb4d3eSLandon J. Fuller * 1. Redistributions of source code must retain the above copyright
9*77cb4d3eSLandon J. Fuller * notice, this list of conditions and the following disclaimer,
10*77cb4d3eSLandon J. Fuller * without modification.
11*77cb4d3eSLandon J. Fuller * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*77cb4d3eSLandon J. Fuller * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13*77cb4d3eSLandon J. Fuller * redistribution must be conditioned upon including a substantially
14*77cb4d3eSLandon J. Fuller * similar Disclaimer requirement for further binary redistribution.
15*77cb4d3eSLandon J. Fuller *
16*77cb4d3eSLandon J. Fuller * NO WARRANTY
17*77cb4d3eSLandon J. Fuller * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*77cb4d3eSLandon J. Fuller * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*77cb4d3eSLandon J. Fuller * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20*77cb4d3eSLandon J. Fuller * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21*77cb4d3eSLandon J. Fuller * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22*77cb4d3eSLandon J. Fuller * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*77cb4d3eSLandon J. Fuller * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*77cb4d3eSLandon J. Fuller * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*77cb4d3eSLandon J. Fuller * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*77cb4d3eSLandon J. Fuller * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27*77cb4d3eSLandon J. Fuller * THE POSSIBILITY OF SUCH DAMAGES.
28*77cb4d3eSLandon J. Fuller */
29*77cb4d3eSLandon J. Fuller
30*77cb4d3eSLandon J. Fuller #include <sys/cdefs.h>
31*77cb4d3eSLandon J. Fuller #ifdef _KERNEL
32*77cb4d3eSLandon J. Fuller #include <sys/param.h>
33*77cb4d3eSLandon J. Fuller #include <sys/malloc.h>
34*77cb4d3eSLandon J. Fuller #include <sys/systm.h>
35*77cb4d3eSLandon J. Fuller #else /* !_KERNEL */
36*77cb4d3eSLandon J. Fuller #include <errno.h>
37*77cb4d3eSLandon J. Fuller #include <stdint.h>
38*77cb4d3eSLandon J. Fuller #include <stdlib.h>
39*77cb4d3eSLandon J. Fuller #include <string.h>
40*77cb4d3eSLandon J. Fuller #endif /* _KERNEL */
41*77cb4d3eSLandon J. Fuller
42*77cb4d3eSLandon J. Fuller #include "bhnd_nvram_private.h"
43*77cb4d3eSLandon J. Fuller
44*77cb4d3eSLandon J. Fuller #include "bhnd_nvram_io.h"
45*77cb4d3eSLandon J. Fuller #include "bhnd_nvram_iovar.h"
46*77cb4d3eSLandon J. Fuller
47*77cb4d3eSLandon J. Fuller /**
48*77cb4d3eSLandon J. Fuller * Buffer-backed NVRAM I/O context.
49*77cb4d3eSLandon J. Fuller *
50*77cb4d3eSLandon J. Fuller * iobuf instances are gauranteed to provide persistent references to its
51*77cb4d3eSLandon J. Fuller * backing contigious buffer via bhnd_nvram_io_read_ptr() and
52*77cb4d3eSLandon J. Fuller * bhnd_nvram_io_write_ptr().
53*77cb4d3eSLandon J. Fuller */
54*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf {
55*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io io; /**< common I/O instance state */
56*77cb4d3eSLandon J. Fuller void *buf; /**< backing buffer. if inline-allocated, will
57*77cb4d3eSLandon J. Fuller be a reference to data[]. */
58*77cb4d3eSLandon J. Fuller size_t size; /**< size of @p buf */
59*77cb4d3eSLandon J. Fuller size_t capacity; /**< capacity of @p buf */
60*77cb4d3eSLandon J. Fuller uint8_t data[]; /**< inline buffer allocation */
61*77cb4d3eSLandon J. Fuller };
62*77cb4d3eSLandon J. Fuller
BHND_NVRAM_IOPS_DEFN(iobuf)63*77cb4d3eSLandon J. Fuller BHND_NVRAM_IOPS_DEFN(iobuf)
64*77cb4d3eSLandon J. Fuller
65*77cb4d3eSLandon J. Fuller /**
66*77cb4d3eSLandon J. Fuller * Allocate and return a new I/O context with an uninitialized
67*77cb4d3eSLandon J. Fuller * buffer of @p size and @p capacity.
68*77cb4d3eSLandon J. Fuller *
69*77cb4d3eSLandon J. Fuller * The caller is responsible for deallocating the returned I/O context via
70*77cb4d3eSLandon J. Fuller * bhnd_nvram_io_free().
71*77cb4d3eSLandon J. Fuller *
72*77cb4d3eSLandon J. Fuller * If @p capacity is less than @p size, a capacity of @p size will be used.
73*77cb4d3eSLandon J. Fuller *
74*77cb4d3eSLandon J. Fuller * @param size The initial size of the I/O context.
75*77cb4d3eSLandon J. Fuller * @param capacity The total capacity of the I/O context buffer;
76*77cb4d3eSLandon J. Fuller * the returned I/O context may be resized up to
77*77cb4d3eSLandon J. Fuller * @p capacity via bhnd_nvram_io_setsize().
78*77cb4d3eSLandon J. Fuller *
79*77cb4d3eSLandon J. Fuller * @retval bhnd_nvram_iobuf success.
80*77cb4d3eSLandon J. Fuller * @retval NULL allocation failed.
81*77cb4d3eSLandon J. Fuller * @retval NULL the requested @p capacity is less than
82*77cb4d3eSLandon J. Fuller * @p size.
83*77cb4d3eSLandon J. Fuller */
84*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io *
85*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_empty(size_t size, size_t capacity)
86*77cb4d3eSLandon J. Fuller {
87*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf;
88*77cb4d3eSLandon J. Fuller size_t iosz;
89*77cb4d3eSLandon J. Fuller bool inline_alloc;
90*77cb4d3eSLandon J. Fuller
91*77cb4d3eSLandon J. Fuller /* Sanity check the capacity */
92*77cb4d3eSLandon J. Fuller if (size > capacity)
93*77cb4d3eSLandon J. Fuller return (NULL);
94*77cb4d3eSLandon J. Fuller
95*77cb4d3eSLandon J. Fuller /* Would sizeof(iobuf)+capacity overflow? */
96*77cb4d3eSLandon J. Fuller if (SIZE_MAX - sizeof(*iobuf) < capacity) {
97*77cb4d3eSLandon J. Fuller inline_alloc = false;
98*77cb4d3eSLandon J. Fuller iosz = sizeof(*iobuf);
99*77cb4d3eSLandon J. Fuller } else {
100*77cb4d3eSLandon J. Fuller inline_alloc = true;
101*77cb4d3eSLandon J. Fuller iosz = sizeof(*iobuf) + capacity;
102*77cb4d3eSLandon J. Fuller }
103*77cb4d3eSLandon J. Fuller
104*77cb4d3eSLandon J. Fuller /* Allocate I/O context */
105*77cb4d3eSLandon J. Fuller iobuf = bhnd_nv_malloc(iosz);
106*77cb4d3eSLandon J. Fuller if (iobuf == NULL)
107*77cb4d3eSLandon J. Fuller return (NULL);
108*77cb4d3eSLandon J. Fuller
109*77cb4d3eSLandon J. Fuller iobuf->io.iops = &bhnd_nvram_iobuf_ops;
110*77cb4d3eSLandon J. Fuller iobuf->buf = NULL;
111*77cb4d3eSLandon J. Fuller iobuf->size = size;
112*77cb4d3eSLandon J. Fuller iobuf->capacity = capacity;
113*77cb4d3eSLandon J. Fuller
114*77cb4d3eSLandon J. Fuller /* Either allocate our backing buffer, or initialize the
115*77cb4d3eSLandon J. Fuller * backing buffer with a reference to our inline allocation. */
116*77cb4d3eSLandon J. Fuller if (inline_alloc)
117*77cb4d3eSLandon J. Fuller iobuf->buf = &iobuf->data;
118*77cb4d3eSLandon J. Fuller else
119*77cb4d3eSLandon J. Fuller iobuf->buf = bhnd_nv_malloc(iobuf->capacity);
120*77cb4d3eSLandon J. Fuller
121*77cb4d3eSLandon J. Fuller if (iobuf->buf == NULL) {
122*77cb4d3eSLandon J. Fuller bhnd_nv_free(iobuf);
123*77cb4d3eSLandon J. Fuller return (NULL);
124*77cb4d3eSLandon J. Fuller }
125*77cb4d3eSLandon J. Fuller
126*77cb4d3eSLandon J. Fuller return (&iobuf->io);
127*77cb4d3eSLandon J. Fuller }
128*77cb4d3eSLandon J. Fuller
129*77cb4d3eSLandon J. Fuller /**
130*77cb4d3eSLandon J. Fuller * Allocate and return a new I/O context, copying @p size from @p buffer.
131*77cb4d3eSLandon J. Fuller *
132*77cb4d3eSLandon J. Fuller * The caller is responsible for deallocating the returned I/O context via
133*77cb4d3eSLandon J. Fuller * bhnd_nvram_io_free().
134*77cb4d3eSLandon J. Fuller *
135*77cb4d3eSLandon J. Fuller * @param buffer The buffer data be copied by the returned I/O context.
136*77cb4d3eSLandon J. Fuller * @param size The size of @p buffer, in bytes.
137*77cb4d3eSLandon J. Fuller *
138*77cb4d3eSLandon J. Fuller * @retval bhnd_nvram_io success.
139*77cb4d3eSLandon J. Fuller * @retval NULL allocation failed.
140*77cb4d3eSLandon J. Fuller */
141*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io *
bhnd_nvram_iobuf_new(const void * buffer,size_t size)142*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_new(const void *buffer, size_t size)
143*77cb4d3eSLandon J. Fuller {
144*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io *io;
145*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf;
146*77cb4d3eSLandon J. Fuller
147*77cb4d3eSLandon J. Fuller /* Allocate the iobuf */
148*77cb4d3eSLandon J. Fuller if ((io = bhnd_nvram_iobuf_empty(size, size)) == NULL)
149*77cb4d3eSLandon J. Fuller return (NULL);
150*77cb4d3eSLandon J. Fuller
151*77cb4d3eSLandon J. Fuller /* Copy the input to our new iobuf instance */
152*77cb4d3eSLandon J. Fuller iobuf = (struct bhnd_nvram_iobuf *)io;
153*77cb4d3eSLandon J. Fuller memcpy(iobuf->buf, buffer, iobuf->size);
154*77cb4d3eSLandon J. Fuller
155*77cb4d3eSLandon J. Fuller return (io);
156*77cb4d3eSLandon J. Fuller }
157*77cb4d3eSLandon J. Fuller
158*77cb4d3eSLandon J. Fuller /**
159*77cb4d3eSLandon J. Fuller * Allocate and return a new I/O context providing an in-memory copy
160*77cb4d3eSLandon J. Fuller * of the data mapped by @p src.
161*77cb4d3eSLandon J. Fuller *
162*77cb4d3eSLandon J. Fuller * The caller is responsible for deallocating the returned I/O context via
163*77cb4d3eSLandon J. Fuller * bhnd_nvram_io_free().
164*77cb4d3eSLandon J. Fuller *
165*77cb4d3eSLandon J. Fuller * @param src The I/O context to be copied.
166*77cb4d3eSLandon J. Fuller *
167*77cb4d3eSLandon J. Fuller * @retval bhnd_nvram_io success.
168*77cb4d3eSLandon J. Fuller * @retval NULL allocation failed.
169*77cb4d3eSLandon J. Fuller * @retval NULL copying @p src failed.
170*77cb4d3eSLandon J. Fuller */
171*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io *
bhnd_nvram_iobuf_copy(struct bhnd_nvram_io * src)172*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_copy(struct bhnd_nvram_io *src)
173*77cb4d3eSLandon J. Fuller {
174*77cb4d3eSLandon J. Fuller return (bhnd_nvram_iobuf_copy_range(src, 0x0,
175*77cb4d3eSLandon J. Fuller bhnd_nvram_io_getsize(src)));
176*77cb4d3eSLandon J. Fuller }
177*77cb4d3eSLandon J. Fuller
178*77cb4d3eSLandon J. Fuller /**
179*77cb4d3eSLandon J. Fuller * Allocate and return a new I/O context providing an in-memory copy
180*77cb4d3eSLandon J. Fuller * of @p size bytes mapped at @p offset by @p src.
181*77cb4d3eSLandon J. Fuller *
182*77cb4d3eSLandon J. Fuller * The caller is responsible for deallocating the returned I/O context via
183*77cb4d3eSLandon J. Fuller * bhnd_nvram_io_free().
184*77cb4d3eSLandon J. Fuller *
185*77cb4d3eSLandon J. Fuller * @param src The I/O context to be copied.
186*77cb4d3eSLandon J. Fuller * @param offset The offset of the bytes to be copied from @p src.
187*77cb4d3eSLandon J. Fuller * @param size The number of bytes to copy at @p offset from @p src.
188*77cb4d3eSLandon J. Fuller *
189*77cb4d3eSLandon J. Fuller * @retval bhnd_nvram_io success.
190*77cb4d3eSLandon J. Fuller * @retval NULL allocation failed.
191*77cb4d3eSLandon J. Fuller * @retval NULL copying @p src failed.
192*77cb4d3eSLandon J. Fuller */
193*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io *
bhnd_nvram_iobuf_copy_range(struct bhnd_nvram_io * src,size_t offset,size_t size)194*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_copy_range(struct bhnd_nvram_io *src, size_t offset,
195*77cb4d3eSLandon J. Fuller size_t size)
196*77cb4d3eSLandon J. Fuller {
197*77cb4d3eSLandon J. Fuller struct bhnd_nvram_io *io;
198*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf;
199*77cb4d3eSLandon J. Fuller int error;
200*77cb4d3eSLandon J. Fuller
201*77cb4d3eSLandon J. Fuller /* Check if offset+size would overflow */
202*77cb4d3eSLandon J. Fuller if (SIZE_MAX - size < offset)
203*77cb4d3eSLandon J. Fuller return (NULL);
204*77cb4d3eSLandon J. Fuller
205*77cb4d3eSLandon J. Fuller /* Allocate the iobuf instance */
206*77cb4d3eSLandon J. Fuller if ((io = bhnd_nvram_iobuf_empty(size, size)) == NULL)
207*77cb4d3eSLandon J. Fuller return (NULL);
208*77cb4d3eSLandon J. Fuller
209*77cb4d3eSLandon J. Fuller /* Copy the input I/O context */
210*77cb4d3eSLandon J. Fuller iobuf = (struct bhnd_nvram_iobuf *)io;
211*77cb4d3eSLandon J. Fuller if ((error = bhnd_nvram_io_read(src, offset, iobuf->buf, size))) {
212*77cb4d3eSLandon J. Fuller bhnd_nvram_io_free(&iobuf->io);
213*77cb4d3eSLandon J. Fuller return (NULL);
214*77cb4d3eSLandon J. Fuller }
215*77cb4d3eSLandon J. Fuller
216*77cb4d3eSLandon J. Fuller return (io);
217*77cb4d3eSLandon J. Fuller }
218*77cb4d3eSLandon J. Fuller
219*77cb4d3eSLandon J. Fuller static void
bhnd_nvram_iobuf_free(struct bhnd_nvram_io * io)220*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_free(struct bhnd_nvram_io *io)
221*77cb4d3eSLandon J. Fuller {
222*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf = (struct bhnd_nvram_iobuf *)io;
223*77cb4d3eSLandon J. Fuller
224*77cb4d3eSLandon J. Fuller /* Free the backing buffer if it wasn't allocated inline */
225*77cb4d3eSLandon J. Fuller if (iobuf->buf != &iobuf->data)
226*77cb4d3eSLandon J. Fuller bhnd_nv_free(iobuf->buf);
227*77cb4d3eSLandon J. Fuller
228*77cb4d3eSLandon J. Fuller bhnd_nv_free(iobuf);
229*77cb4d3eSLandon J. Fuller }
230*77cb4d3eSLandon J. Fuller
231*77cb4d3eSLandon J. Fuller static size_t
bhnd_nvram_iobuf_getsize(struct bhnd_nvram_io * io)232*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_getsize(struct bhnd_nvram_io *io)
233*77cb4d3eSLandon J. Fuller {
234*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf = (struct bhnd_nvram_iobuf *)io;
235*77cb4d3eSLandon J. Fuller return (iobuf->size);
236*77cb4d3eSLandon J. Fuller }
237*77cb4d3eSLandon J. Fuller
238*77cb4d3eSLandon J. Fuller static int
bhnd_nvram_iobuf_setsize(struct bhnd_nvram_io * io,size_t size)239*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_setsize(struct bhnd_nvram_io *io, size_t size)
240*77cb4d3eSLandon J. Fuller {
241*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf = (struct bhnd_nvram_iobuf *)io;
242*77cb4d3eSLandon J. Fuller
243*77cb4d3eSLandon J. Fuller /* Can't exceed the actual capacity */
244*77cb4d3eSLandon J. Fuller if (size > iobuf->capacity)
245*77cb4d3eSLandon J. Fuller return (ENXIO);
246*77cb4d3eSLandon J. Fuller
247*77cb4d3eSLandon J. Fuller iobuf->size = size;
248*77cb4d3eSLandon J. Fuller return (0);
249*77cb4d3eSLandon J. Fuller }
250*77cb4d3eSLandon J. Fuller
251*77cb4d3eSLandon J. Fuller /* Common iobuf_(read|write)_ptr implementation */
252*77cb4d3eSLandon J. Fuller static int
bhnd_nvram_iobuf_ptr(struct bhnd_nvram_iobuf * iobuf,size_t offset,void ** ptr,size_t nbytes,size_t * navail)253*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_ptr(struct bhnd_nvram_iobuf *iobuf, size_t offset, void **ptr,
254*77cb4d3eSLandon J. Fuller size_t nbytes, size_t *navail)
255*77cb4d3eSLandon J. Fuller {
256*77cb4d3eSLandon J. Fuller size_t avail;
257*77cb4d3eSLandon J. Fuller
258*77cb4d3eSLandon J. Fuller /* Verify offset+nbytes fall within the buffer range */
259*77cb4d3eSLandon J. Fuller if (offset > iobuf->size)
260*77cb4d3eSLandon J. Fuller return (ENXIO);
261*77cb4d3eSLandon J. Fuller
262*77cb4d3eSLandon J. Fuller avail = iobuf->size - offset;
263*77cb4d3eSLandon J. Fuller if (avail < nbytes)
264*77cb4d3eSLandon J. Fuller return (ENXIO);
265*77cb4d3eSLandon J. Fuller
266*77cb4d3eSLandon J. Fuller /* Valid I/O range, provide a pointer to the buffer and the
267*77cb4d3eSLandon J. Fuller * total count of available bytes */
268*77cb4d3eSLandon J. Fuller *ptr = ((uint8_t *)iobuf->buf) + offset;
269*77cb4d3eSLandon J. Fuller if (navail != NULL)
270*77cb4d3eSLandon J. Fuller *navail = avail;
271*77cb4d3eSLandon J. Fuller
272*77cb4d3eSLandon J. Fuller return (0);
273*77cb4d3eSLandon J. Fuller }
274*77cb4d3eSLandon J. Fuller
275*77cb4d3eSLandon J. Fuller static int
bhnd_nvram_iobuf_read_ptr(struct bhnd_nvram_io * io,size_t offset,const void ** ptr,size_t nbytes,size_t * navail)276*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_read_ptr(struct bhnd_nvram_io *io, size_t offset,
277*77cb4d3eSLandon J. Fuller const void **ptr, size_t nbytes, size_t *navail)
278*77cb4d3eSLandon J. Fuller {
279*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf;
280*77cb4d3eSLandon J. Fuller void *ioptr;
281*77cb4d3eSLandon J. Fuller int error;
282*77cb4d3eSLandon J. Fuller
283*77cb4d3eSLandon J. Fuller iobuf = (struct bhnd_nvram_iobuf *) io;
284*77cb4d3eSLandon J. Fuller
285*77cb4d3eSLandon J. Fuller /* Return a pointer into our backing buffer */
286*77cb4d3eSLandon J. Fuller error = bhnd_nvram_iobuf_ptr(iobuf, offset, &ioptr, nbytes, navail);
287*77cb4d3eSLandon J. Fuller if (error)
288*77cb4d3eSLandon J. Fuller return (error);
289*77cb4d3eSLandon J. Fuller
290*77cb4d3eSLandon J. Fuller *ptr = ioptr;
291*77cb4d3eSLandon J. Fuller
292*77cb4d3eSLandon J. Fuller return (0);
293*77cb4d3eSLandon J. Fuller }
294*77cb4d3eSLandon J. Fuller
295*77cb4d3eSLandon J. Fuller static int
bhnd_nvram_iobuf_write_ptr(struct bhnd_nvram_io * io,size_t offset,void ** ptr,size_t nbytes,size_t * navail)296*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_write_ptr(struct bhnd_nvram_io *io, size_t offset,
297*77cb4d3eSLandon J. Fuller void **ptr, size_t nbytes, size_t *navail)
298*77cb4d3eSLandon J. Fuller {
299*77cb4d3eSLandon J. Fuller struct bhnd_nvram_iobuf *iobuf;
300*77cb4d3eSLandon J. Fuller
301*77cb4d3eSLandon J. Fuller iobuf = (struct bhnd_nvram_iobuf *) io;
302*77cb4d3eSLandon J. Fuller
303*77cb4d3eSLandon J. Fuller /* Return a pointer into our backing buffer */
304*77cb4d3eSLandon J. Fuller return (bhnd_nvram_iobuf_ptr(iobuf, offset, ptr, nbytes, navail));
305*77cb4d3eSLandon J. Fuller }
306*77cb4d3eSLandon J. Fuller
307*77cb4d3eSLandon J. Fuller static int
bhnd_nvram_iobuf_read(struct bhnd_nvram_io * io,size_t offset,void * buffer,size_t nbytes)308*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
309*77cb4d3eSLandon J. Fuller size_t nbytes)
310*77cb4d3eSLandon J. Fuller {
311*77cb4d3eSLandon J. Fuller const void *ptr;
312*77cb4d3eSLandon J. Fuller int error;
313*77cb4d3eSLandon J. Fuller
314*77cb4d3eSLandon J. Fuller /* Try to fetch a direct pointer for at least nbytes */
315*77cb4d3eSLandon J. Fuller if ((error = bhnd_nvram_io_read_ptr(io, offset, &ptr, nbytes, NULL)))
316*77cb4d3eSLandon J. Fuller return (error);
317*77cb4d3eSLandon J. Fuller
318*77cb4d3eSLandon J. Fuller /* Copy out the requested data */
319*77cb4d3eSLandon J. Fuller memcpy(buffer, ptr, nbytes);
320*77cb4d3eSLandon J. Fuller return (0);
321*77cb4d3eSLandon J. Fuller }
322*77cb4d3eSLandon J. Fuller
323*77cb4d3eSLandon J. Fuller static int
bhnd_nvram_iobuf_write(struct bhnd_nvram_io * io,size_t offset,void * buffer,size_t nbytes)324*77cb4d3eSLandon J. Fuller bhnd_nvram_iobuf_write(struct bhnd_nvram_io *io, size_t offset,
325*77cb4d3eSLandon J. Fuller void *buffer, size_t nbytes)
326*77cb4d3eSLandon J. Fuller {
327*77cb4d3eSLandon J. Fuller void *ptr;
328*77cb4d3eSLandon J. Fuller int error;
329*77cb4d3eSLandon J. Fuller
330*77cb4d3eSLandon J. Fuller /* Try to fetch a direct pointer for at least nbytes */
331*77cb4d3eSLandon J. Fuller if ((error = bhnd_nvram_io_write_ptr(io, offset, &ptr, nbytes, NULL)))
332*77cb4d3eSLandon J. Fuller return (error);
333*77cb4d3eSLandon J. Fuller
334*77cb4d3eSLandon J. Fuller /* Copy in the provided data */
335*77cb4d3eSLandon J. Fuller memcpy(ptr, buffer, nbytes);
336*77cb4d3eSLandon J. Fuller return (0);
337*77cb4d3eSLandon J. Fuller }
338