| 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.10 2008/02/14 12:30:31 sephe 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 = TAILQ_HEAD_INITIALIZER(images); |
| 47 | |
| 48 | static struct fw_image *firmware_image_load_file(const char *, bus_dma_tag_t); |
| 49 | static struct fw_image *firmware_prepare_image(const char *, size_t, |
| 50 | bus_dma_tag_t); |
| 51 | static void firmware_destroy_image(struct fw_image *); |
| 52 | |
| 53 | struct fw_image * |
| 54 | firmware_image_load(const char *image_name, bus_dma_tag_t ptag) |
| 55 | { |
| 56 | struct fw_image *img; |
| 57 | |
| 58 | TAILQ_FOREACH(img, &images, fw_link) { |
| 59 | if (strcmp(img->fw_name, image_name) == 0) { |
| 60 | ++img->fw_refcnt; |
| 61 | return(img); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if ((img = firmware_image_load_file(image_name, ptag)) != NULL) |
| 66 | return(img); |
| 67 | |
| 68 | return(NULL); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | firmware_image_unload(struct fw_image *img) |
| 73 | { |
| 74 | KKASSERT(img->fw_refcnt > 0); |
| 75 | if (--img->fw_refcnt > 0) |
| 76 | return; |
| 77 | |
| 78 | TAILQ_REMOVE(&images, img, fw_link); |
| 79 | firmware_destroy_image(img); |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | firmware_destroy_image(struct fw_image *img) |
| 84 | { |
| 85 | if (img->fw_dma_tag != NULL) { |
| 86 | bus_dmamap_unload(img->fw_dma_tag, img->fw_dma_map); |
| 87 | bus_dmamem_free(img->fw_dma_tag, img->fw_image, img->fw_dma_map); |
| 88 | bus_dma_tag_destroy(img->fw_dma_tag); |
| 89 | } else { |
| 90 | kfree(img->fw_image, M_DEVBUF); |
| 91 | } |
| 92 | kfree(__DECONST(char *, img->fw_name), M_DEVBUF); |
| 93 | kfree(img, M_DEVBUF); |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | firmware_dma_map(void *arg, bus_dma_segment_t *segs, int nseg, int error) |
| 98 | { |
| 99 | bus_addr_t *p; |
| 100 | |
| 101 | KKASSERT(nseg == 1); |
| 102 | |
| 103 | p = arg; |
| 104 | *p = segs->ds_addr; |
| 105 | } |
| 106 | |
| 107 | static struct fw_image * |
| 108 | firmware_prepare_image(const char *imgname, size_t imglen, bus_dma_tag_t ptag) |
| 109 | { |
| 110 | struct fw_image *img; |
| 111 | int error; |
| 112 | |
| 113 | img = kmalloc(sizeof(*img), M_DEVBUF, M_WAITOK | M_ZERO); |
| 114 | img->fw_name = kstrdup(imgname, M_DEVBUF); /* XXX necessary? */ |
| 115 | img->fw_refcnt = 1; |
| 116 | img->fw_imglen = imglen; |
| 117 | |
| 118 | if (ptag == NULL) { |
| 119 | /* busdma(9) is not required */ |
| 120 | img->fw_image = kmalloc(img->fw_imglen, M_DEVBUF, M_WAITOK); |
| 121 | return img; |
| 122 | } |
| 123 | |
| 124 | error = bus_dma_tag_create(ptag, 1, 0, |
| 125 | BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, |
| 126 | img->fw_imglen, 1, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW, |
| 127 | &img->fw_dma_tag); |
| 128 | if (error) |
| 129 | goto fail_tag_create; |
| 130 | |
| 131 | error = bus_dmamem_alloc(img->fw_dma_tag, (void **)&img->fw_image, |
| 132 | BUS_DMA_WAITOK, &img->fw_dma_map); |
| 133 | if (error) |
| 134 | goto fail_dma_alloc; |
| 135 | |
| 136 | error = bus_dmamap_load(img->fw_dma_tag, img->fw_dma_map, |
| 137 | img->fw_image, img->fw_imglen, |
| 138 | firmware_dma_map, &img->fw_dma_addr, 0); |
| 139 | if (error) |
| 140 | goto fail_dma_load; |
| 141 | |
| 142 | return(img); |
| 143 | |
| 144 | fail_dma_load: |
| 145 | bus_dmamem_free(img->fw_dma_tag, img->fw_image, img->fw_dma_map); |
| 146 | fail_dma_alloc: |
| 147 | bus_dma_tag_destroy(img->fw_dma_tag); |
| 148 | fail_tag_create: |
| 149 | kfree(__DECONST(char *, img->fw_name), M_DEVBUF); |
| 150 | kfree(img, M_DEVBUF); |
| 151 | return(NULL); |
| 152 | } |
| 153 | |
| 154 | static char firmware_root[MAXPATHLEN] = "/etc/firmware/"; |
| 155 | |
| 156 | static struct fw_image * |
| 157 | firmware_image_load_file(const char *image_name, bus_dma_tag_t ptag) |
| 158 | { |
| 159 | struct stat ub; |
| 160 | struct file *fp; |
| 161 | struct fw_image *img; |
| 162 | size_t nread; |
| 163 | char *fw_path; |
| 164 | int error; |
| 165 | |
| 166 | fw_path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK); |
| 167 | ksnprintf(fw_path, MAXPATHLEN, "%s/%s", firmware_root, image_name); |
| 168 | |
| 169 | /* XXX: access? */ |
| 170 | |
| 171 | error = fp_open(fw_path, O_RDONLY|O_NOFOLLOW, 0600, &fp); |
| 172 | if (error != 0) |
| 173 | goto fail_open; |
| 174 | |
| 175 | if (fp_stat(fp, &ub) != 0) { |
| 176 | if (bootverbose) |
| 177 | kprintf("fp_stat on firmware image %s failed: %d\n", |
| 178 | fw_path, error); |
| 179 | goto fail_stat; |
| 180 | } |
| 181 | if (ub.st_size > SIZE_T_MAX) { |
| 182 | kprintf("firmware image %s is too large\n", fw_path); |
| 183 | goto fail_stat; |
| 184 | } |
| 185 | |
| 186 | /* XXX: file type */ |
| 187 | img = firmware_prepare_image(image_name, (size_t)ub.st_size, ptag); |
| 188 | if (img == NULL) |
| 189 | goto fail_stat; |
| 190 | |
| 191 | error = fp_read(fp, img->fw_image, img->fw_imglen, &nread, |
| 192 | 1, UIO_SYSSPACE); |
| 193 | if (error != 0 || nread != img->fw_imglen) { |
| 194 | kprintf("firmware image could not be read: %d\n", error); |
| 195 | goto fail_read; |
| 196 | } |
| 197 | fp_close(fp); |
| 198 | TAILQ_INSERT_HEAD(&images, img, fw_link); |
| 199 | |
| 200 | kfree(fw_path, M_TEMP); |
| 201 | return(img); |
| 202 | |
| 203 | fail_read: |
| 204 | firmware_destroy_image(img); |
| 205 | fail_stat: |
| 206 | fp_close(fp); |
| 207 | fail_open: |
| 208 | kfree(fw_path, M_TEMP); |
| 209 | return(NULL); |
| 210 | } |
| 211 | |
| 212 | MODULE_VERSION(firmware, 1); |