xref: /illumos-gate/usr/src/uts/common/os/firmload.c (revision 7855d95b30fd903e3918bad5a29b777e765db821)
1 /*	$NetBSD: firmload.c,v 1.19 2014/03/25 16:19:13 christos Exp $	*/
2 
3 /*
4  * Copyright 2016 Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
5  */
6 
7 /*
8  * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Jason R. Thorpe.
13  *
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.
22  *
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.
34  */
35 
36 /*
37  * The firmload API provides an interface for device drivers to access
38  * firmware images that must be loaded onto their devices.
39  */
40 
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>
51 
52 #include <sys/firmload.h>
53 
54 struct firmware_handle {
55 	struct _buf	*fh_buf;
56 	off_t		 fh_size;
57 };
58 
59 static firmware_handle_t
60 firmware_handle_alloc(void)
61 {
62 	return (kmem_alloc(sizeof (struct firmware_handle), KM_SLEEP));
63 }
64 
65 static void
66 firmware_handle_free(firmware_handle_t fh)
67 {
68 	kmem_free(fh, sizeof (struct firmware_handle));
69 }
70 
71 /*
72  * firmware_open:
73  *
74  *	Open a firmware image and return its handle.
75  */
76 int
77 firmware_open(const char *drvname, const char *imgname, firmware_handle_t *fhp)
78 {
79 	char *path;
80 	firmware_handle_t fh;
81 	int error;
82 
83 	if (drvname == NULL || imgname == NULL || fhp == NULL)
84 		return (EINVAL);
85 
86 	path = kmem_asprintf("firmware/%s/%s", drvname, imgname);
87 	fh = firmware_handle_alloc();
88 
89 	fh->fh_buf = kobj_open_path(path, 1, 0);
90 	strfree(path);
91 
92 	if (fh->fh_buf == (struct _buf *)-1) {
93 		firmware_handle_free(fh);
94 		return (ENOENT);
95 	}
96 
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);
102 	}
103 
104 	*fhp = fh;
105 	return (0);
106 }
107 
108 /*
109  * firmware_close:
110  *
111  *	Close a firmware image.
112  */
113 int
114 firmware_close(firmware_handle_t fh)
115 {
116 	if (fh != NULL) {
117 		kobj_close_file(fh->fh_buf);
118 		firmware_handle_free(fh);
119 	}
120 	return (0);
121 }
122 
123 /*
124  * firmware_get_size:
125  *
126  *	Return the total size of a firmware image.
127  */
128 off_t
129 firmware_get_size(firmware_handle_t fh)
130 {
131 	ASSERT(fh != NULL);
132 	return (fh->fh_size);
133 }
134 
135 /*
136  * firmware_read:
137  *
138  *	Read data from a firmware image at the specified offset into
139  *	the provided buffer.
140  */
141 int
142 firmware_read(firmware_handle_t fh, off_t offset, void *buf, size_t len)
143 {
144 	ASSERT(fh != NULL);
145 	if (kobj_read_file(fh->fh_buf, buf, len, offset) == -1)
146 		return (-1);
147 
148 	return (0);
149 }
150