Merge commit '7e934d3acc051b7ee3ef0d11571fd1225800a607'
[unleashed.git] / kernel / os / firmload.c
blobef6c15ba5bb787b57280f82707b7c9b34ffd803f
1 /* $NetBSD: firmload.c,v 1.19 2014/03/25 16:19:13 christos Exp $ */
3 /*
4 * Copyright 2016 Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
5 */
7 /*
8 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
9 * All rights reserved.
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Jason R. Thorpe.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * The firmload API provides an interface for device drivers to access
38 * firmware images that must be loaded onto their devices.
41 #include <sys/param.h>
42 #include <sys/fcntl.h>
43 #include <sys/systm.h>
44 #include <sys/vnode.h>
45 #include <sys/lwp.h>
46 #include <sys/file.h>
47 #include <sys/cmn_err.h>
48 #include <sys/modctl.h>
49 #include <sys/kobj.h>
50 #include <sys/kobj_impl.h>
52 #include <sys/firmload.h>
54 struct firmware_handle {
55 struct _buf *fh_buf;
56 off_t fh_size;
59 static firmware_handle_t
60 firmware_handle_alloc(void)
62 return (kmem_alloc(sizeof (struct firmware_handle), KM_SLEEP));
65 static void
66 firmware_handle_free(firmware_handle_t fh)
68 kmem_free(fh, sizeof (struct firmware_handle));
72 * firmware_open:
74 * Open a firmware image and return its handle.
76 int
77 firmware_open(const char *drvname, const char *imgname, firmware_handle_t *fhp)
79 char *path;
80 firmware_handle_t fh;
81 int error;
83 if (drvname == NULL || imgname == NULL || fhp == NULL)
84 return (EINVAL);
86 path = kmem_asprintf("firmware/%s/%s", drvname, imgname);
87 fh = firmware_handle_alloc();
89 fh->fh_buf = kobj_open_path(path, 1);
90 strfree(path);
92 if (fh->fh_buf == (struct _buf *)-1) {
93 firmware_handle_free(fh);
94 return (ENOENT);
97 error = kobj_get_filesize(fh->fh_buf, (uint64_t *)&fh->fh_size);
98 if (error != 0) {
99 kobj_close_file(fh->fh_buf);
100 firmware_handle_free(fh);
101 return (error);
104 *fhp = fh;
105 return (0);
109 * firmware_close:
111 * Close a firmware image.
114 firmware_close(firmware_handle_t fh)
116 if (fh != NULL) {
117 kobj_close_file(fh->fh_buf);
118 firmware_handle_free(fh);
120 return (0);
124 * firmware_get_size:
126 * Return the total size of a firmware image.
128 off_t
129 firmware_get_size(firmware_handle_t fh)
131 ASSERT(fh != NULL);
132 return (fh->fh_size);
136 * firmware_read:
138 * Read data from a firmware image at the specified offset into
139 * the provided buffer.
142 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len)
144 ASSERT(fh != NULL);
145 if (kobj_read_file(fh->fh_buf, buf, len, offset) == -1)
146 return (-1);
148 return (0);