1 /*- 2 * Copyright (c) 2013 Adrian Chadd <adrian@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <unistd.h> 35 #include <errno.h> 36 #include <string.h> 37 #include <err.h> 38 #include <fcntl.h> 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 42 #include "ath3k_fw.h" 43 #include "ath3k_dbg.h" 44 45 int 46 ath3k_fw_read(struct ath3k_firmware *fw, const char *fwname) 47 { 48 int fd; 49 struct stat sb; 50 unsigned char *buf; 51 ssize_t r; 52 53 fd = open(fwname, O_RDONLY); 54 if (fd < 0) { 55 warn("%s: open: %s", __func__, fwname); 56 return (0); 57 } 58 59 if (fstat(fd, &sb) != 0) { 60 warn("%s: stat: %s", __func__, fwname); 61 close(fd); 62 return (0); 63 } 64 65 buf = calloc(1, sb.st_size); 66 if (buf == NULL) { 67 warn("%s: calloc", __func__); 68 close(fd); 69 return (0); 70 } 71 72 /* XXX handle partial reads */ 73 r = read(fd, buf, sb.st_size); 74 if (r < 0) { 75 warn("%s: read", __func__); 76 free(buf); 77 close(fd); 78 return (0); 79 } 80 81 if (r != sb.st_size) { 82 fprintf(stderr, "%s: read len %d != file size %d\n", 83 __func__, 84 (int) r, 85 (int) sb.st_size); 86 free(buf); 87 close(fd); 88 return (0); 89 } 90 91 /* We have everything, so! */ 92 93 bzero(fw, sizeof(*fw)); 94 95 fw->fwname = strdup(fwname); 96 fw->len = sb.st_size; 97 fw->size = sb.st_size; 98 fw->buf = buf; 99 100 close(fd); 101 return (1); 102 } 103 104 void 105 ath3k_fw_free(struct ath3k_firmware *fw) 106 { 107 if (fw->fwname) 108 free(fw->fwname); 109 if (fw->buf) 110 free(fw->buf); 111 bzero(fw, sizeof(*fw)); 112 } 113