1 /* $NetBSD: firmload.c,v 1.19 2014/03/25 16:19:13 christos Exp $ */
4 * Copyright 2016 Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
8 * Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
11 * This code is derived from software contributed to The NetBSD Foundation
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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>
47 #include <sys/cmn_err.h>
48 #include <sys/modctl.h>
50 #include <sys/kobj_impl.h>
52 #include <sys/firmload.h>
54 struct firmware_handle
{
59 static firmware_handle_t
60 firmware_handle_alloc(void)
62 return (kmem_alloc(sizeof (struct firmware_handle
), KM_SLEEP
));
66 firmware_handle_free(firmware_handle_t fh
)
68 kmem_free(fh
, sizeof (struct firmware_handle
));
74 * Open a firmware image and return its handle.
77 firmware_open(const char *drvname
, const char *imgname
, firmware_handle_t
*fhp
)
83 if (drvname
== NULL
|| imgname
== NULL
|| fhp
== NULL
)
86 path
= kmem_asprintf("firmware/%s/%s", drvname
, imgname
);
87 fh
= firmware_handle_alloc();
89 fh
->fh_buf
= kobj_open_path(path
, 1, 0);
92 if (fh
->fh_buf
== (struct _buf
*)-1) {
93 firmware_handle_free(fh
);
97 error
= kobj_get_filesize(fh
->fh_buf
, (uint64_t *)&fh
->fh_size
);
99 kobj_close_file(fh
->fh_buf
);
100 firmware_handle_free(fh
);
111 * Close a firmware image.
114 firmware_close(firmware_handle_t fh
)
117 kobj_close_file(fh
->fh_buf
);
118 firmware_handle_free(fh
);
126 * Return the total size of a firmware image.
129 firmware_get_size(firmware_handle_t fh
)
132 return (fh
->fh_size
);
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
)
145 if (kobj_read_file(fh
->fh_buf
, buf
, len
, offset
) == -1)