Generic firmware support. Currently implemented is loading from
[dragonfly.git] / sys / kern / kern_firmware.c
1 /*
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Johannes Hofmann <Hoannes.Hofmann.gmx.de> and
6  * Joerg Sonnenberger <joerg@bec.de.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $DragonFly: src/sys/kern/kern_firmware.c,v 1.1 2005/03/19 14:54:50 joerg Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/fcntl.h>
38 #include <sys/file.h>
39 #include <sys/firmware.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/stat.h>
43 #include <sys/systm.h>
44 #include <machine/limits.h>
45
46 static TAILQ_HEAD(, fw_image) images;
47
48 static struct fw_image  *firmware_image_load_file(const char *);
49 static struct fw_image  *firmware_prepare_image(const char *, size_t);
50 static void              firmware_destroy_image(struct fw_image *);
51
52 struct fw_image *
53 firmware_image_load(const char *image_name)
54 {
55         struct fw_image *img;
56
57         TAILQ_FOREACH(img, &images, fw_link) {
58                 if (strcmp(img->fw_name, image_name) == 0) {
59                         ++img->fw_refcnt;
60                         return(img);
61                 }
62         }
63
64         if ((img = firmware_image_load_file(image_name)) != NULL)
65                 return(img);
66
67         return(NULL);
68 }
69
70 void 
71 firmware_image_unload(struct fw_image *img)
72 {
73         KKASSERT(img->fw_refcnt > 0);
74         if (--img->fw_refcnt > 0)
75                 return;
76
77         TAILQ_REMOVE(&images, img, fw_link);
78         firmware_destroy_image(img);
79 }
80
81 static void
82 firmware_destroy_image(struct fw_image *img)
83 {
84         bus_dmamap_unload(img->fw_dma_tag, img->fw_dma_map);
85         bus_dmamem_free(img->fw_dma_tag, img->fw_image, img->fw_dma_map);
86         bus_dma_tag_destroy(img->fw_dma_tag);
87         free(__DECONST(char *, img->fw_name), M_DEVBUF);
88         free(img, M_DEVBUF);    
89 }
90
91 static void
92 firmware_dma_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
93 {
94         bus_addr_t *p;
95
96         KKASSERT(nseg == 1);
97
98         p = arg;
99         *p = segs->ds_addr;
100 }
101
102 static struct fw_image *
103 firmware_prepare_image(const char *imgname, size_t imglen)
104 {
105         struct fw_image *img;
106         int error;
107
108         img = malloc(sizeof(*img), M_DEVBUF, M_WAITOK | M_ZERO);
109         img->fw_name = strdup(imgname, M_DEVBUF); /* XXX necessary? */
110         img->fw_refcnt = 1;
111         img->fw_imglen = imglen;
112
113         error = bus_dma_tag_create(NULL, 1, 0,
114             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
115             img->fw_imglen, 1, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW,
116             &img->fw_dma_tag);
117         if (error)
118                 goto fail_tag_create;
119
120         error = bus_dmamem_alloc(img->fw_dma_tag, (void **)&img->fw_image,
121                                  BUS_DMA_WAITOK, &img->fw_dma_map);
122         if (error)
123                 goto fail_dma_alloc;
124
125         error = bus_dmamap_load(img->fw_dma_tag, img->fw_dma_map,
126                                 img->fw_image, img->fw_imglen,
127                                 firmware_dma_map, &img->fw_dma_addr, 0);
128         if (error)
129                 goto fail_dma_load;
130
131         return(img);
132
133 fail_dma_load:
134         bus_dmamem_free(img->fw_dma_tag, img->fw_image, img->fw_dma_map);
135 fail_dma_alloc:
136         bus_dma_tag_destroy(img->fw_dma_tag);
137 fail_tag_create:
138         free(__DECONST(char *, img->fw_name), M_DEVBUF);
139         free(img, M_DEVBUF);
140         return(NULL);
141 }
142
143 static char firmware_root[MAXPATHLEN] = "/etc/firmware/";
144
145 static struct fw_image *
146 firmware_image_load_file(const char *image_name) 
147 {
148         struct stat ub;
149         struct file *fp;
150         struct fw_image *img;
151         size_t nread;
152         char *fw_path;
153         int error;
154
155         fw_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
156         snprintf(fw_path, MAXPATHLEN, "%s/%s", firmware_root, image_name);
157
158         /* XXX: access? */
159
160         error = fp_open(fw_path, O_RDONLY|O_NOFOLLOW, 0600, &fp);
161         if (error != 0)
162                 goto fail_open;
163
164         if (fp_stat(fp, &ub) != 0) {
165                 if (bootverbose)
166                         printf("fp_stat on firmware image %s failed: %d\n",
167                                fw_path, error);
168                 goto fail_stat;
169         }
170         if (ub.st_size > SIZE_T_MAX) {
171                 printf("firmware image %s is too large\n", fw_path);
172                 goto fail_stat;
173         }
174
175         /* XXX: file type */
176         img = firmware_prepare_image(image_name, (size_t)ub.st_size);
177         if (img == NULL)
178                 goto fail_stat;
179
180         if ((error = fp_read(fp, img->fw_image, img->fw_imglen, &nread)) != 0 ||
181                 nread != img->fw_imglen) {
182                 printf("firmware image could not be read: %d\n", error);
183                 goto fail_read;
184         }
185         fp_close(fp);
186         TAILQ_INSERT_HEAD(&images, img, fw_link);
187
188         return(img);
189
190 fail_read:
191         firmware_destroy_image(img);
192 fail_stat:
193         fp_close(fp);
194 fail_open:
195         free(fw_path, M_TEMP);
196         return(NULL);
197 }