2 * Copyright (c) 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$");
34 #include <sys/param.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
44 #include "bhnd_nvram_private.h"
46 #include "bhnd_nvram_io.h"
47 #include "bhnd_nvram_iovar.h"
50 * Memory-backed NVRAM I/O context.
52 * ioptr instances are gauranteed to provide persistent references to its
53 * backing contigious memory via bhnd_nvram_io_read_ptr() and
54 * bhnd_nvram_io_write_ptr().
56 struct bhnd_nvram_ioptr {
57 struct bhnd_nvram_io io; /**< common I/O instance state */
58 void *ptr; /**< backing memory */
59 size_t size; /**< size at @p ptr */
60 size_t capacity; /**< capacity at @p ptr */
61 uint32_t flags; /**< flags (see BHND_NVRAM_IOPTR_*) */
64 BHND_NVRAM_IOPS_DEFN(ioptr)
67 * Allocate and return a new I/O context, mapping @p size bytes at @p ptr.
69 * The caller is responsible for deallocating the returned I/O context via
70 * bhnd_nvram_io_free().
72 * @param ptr The pointer to be mapped by the returned I/O
73 * context. Must remain valid for the lifetime of
74 * the returned I/O context.
75 * @param size The total number of bytes mapped at @p ptr.
76 * @param capacity The maximum number of bytes that may be mapped
77 * at @p ptr via bhnd_nvram_ioptr_setsize().
78 * @param flags Access flags (see BHND_NVRAM_IOPTR_*).
80 * @retval bhnd_nvram_io success.
81 * @retval NULL allocation failed.
82 * @retval NULL the requested @p capacity is less than @p size.
84 struct bhnd_nvram_io *
85 bhnd_nvram_ioptr_new(const void *ptr, size_t size, size_t capacity,
88 struct bhnd_nvram_ioptr *ioptr;
90 /* Sanity check the capacity */
94 /* Allocate I/O context */
95 ioptr = bhnd_nv_malloc(sizeof(*ioptr));
99 ioptr->io.iops = &bhnd_nvram_ioptr_ops;
100 ioptr->ptr = __DECONST(void *, ptr);
102 ioptr->capacity = capacity;
103 ioptr->flags = flags;
109 bhnd_nvram_ioptr_free(struct bhnd_nvram_io *io)
115 bhnd_nvram_ioptr_getsize(struct bhnd_nvram_io *io)
117 struct bhnd_nvram_ioptr *ioptr = (struct bhnd_nvram_ioptr *)io;
118 return (ioptr->size);
122 bhnd_nvram_ioptr_setsize(struct bhnd_nvram_io *io, size_t size)
124 struct bhnd_nvram_ioptr *ioptr = (struct bhnd_nvram_ioptr *)io;
126 /* Must be writable */
127 if (!(ioptr->flags & BHND_NVRAM_IOPTR_RDWR))
130 /* Can't exceed the actual capacity */
131 if (size > ioptr->capacity)
138 /* Common ioptr_(read|write)_ptr implementation */
140 bhnd_nvram_ioptr_ptr(struct bhnd_nvram_ioptr *ioptr, size_t offset, void **ptr,
141 size_t nbytes, size_t *navail)
145 /* Verify offset+nbytes fall within the buffer range */
146 if (offset > ioptr->size)
149 avail = ioptr->size - offset;
153 /* Valid I/O range, provide a pointer to the buffer and the
154 * total count of available bytes */
155 *ptr = ((uint8_t *)ioptr->ptr) + offset;
163 bhnd_nvram_ioptr_read_ptr(struct bhnd_nvram_io *io, size_t offset,
164 const void **ptr, size_t nbytes, size_t *navail)
166 struct bhnd_nvram_ioptr *ioptr;
170 ioptr = (struct bhnd_nvram_ioptr *) io;
172 /* Return a pointer into our backing buffer */
173 error = bhnd_nvram_ioptr_ptr(ioptr, offset, &writep, nbytes, navail);
183 bhnd_nvram_ioptr_write_ptr(struct bhnd_nvram_io *io, size_t offset,
184 void **ptr, size_t nbytes, size_t *navail)
186 struct bhnd_nvram_ioptr *ioptr;
188 ioptr = (struct bhnd_nvram_ioptr *) io;
190 /* Must be writable */
191 if (!(ioptr->flags & BHND_NVRAM_IOPTR_RDWR))
194 /* Return a pointer into our backing buffer */
195 return (bhnd_nvram_ioptr_ptr(ioptr, offset, ptr, nbytes, navail));
199 bhnd_nvram_ioptr_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
205 /* Try to fetch a direct pointer for at least nbytes */
206 if ((error = bhnd_nvram_io_read_ptr(io, offset, &ptr, nbytes, NULL)))
209 /* Copy out the requested data */
210 memcpy(buffer, ptr, nbytes);
215 bhnd_nvram_ioptr_write(struct bhnd_nvram_io *io, size_t offset,
216 void *buffer, size_t nbytes)
221 /* Try to fetch a direct pointer for at least nbytes */
222 if ((error = bhnd_nvram_io_write_ptr(io, offset, &ptr, nbytes, NULL)))
225 /* Copy in the provided data */
226 memcpy(ptr, buffer, nbytes);