Path: blob/main/usr.sbin/bluetooth/ath3kfw/ath3k_fw.c
106842 views
/*-1* Copyright (c) 2013 Adrian Chadd <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any12* redistribution must be conditioned upon including a substantially13* similar Disclaimer requirement for further binary redistribution.14*15* NO WARRANTY16* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY19* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,21* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER24* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF26* THE POSSIBILITY OF SUCH DAMAGES.27*/2829#include <stdio.h>30#include <stdlib.h>31#include <unistd.h>32#include <errno.h>33#include <string.h>34#include <err.h>35#include <fcntl.h>36#include <sys/types.h>37#include <sys/stat.h>3839#include "ath3k_fw.h"40#include "ath3k_dbg.h"4142int43ath3k_fw_read(struct ath3k_firmware *fw, const char *fwname)44{45int fd;46struct stat sb;47unsigned char *buf;48ssize_t r;4950fd = open(fwname, O_RDONLY);51if (fd < 0) {52warn("%s: open: %s", __func__, fwname);53return (0);54}5556if (fstat(fd, &sb) != 0) {57warn("%s: stat: %s", __func__, fwname);58close(fd);59return (0);60}6162buf = calloc(1, sb.st_size);63if (buf == NULL) {64warn("%s: calloc", __func__);65close(fd);66return (0);67}6869/* XXX handle partial reads */70r = read(fd, buf, sb.st_size);71if (r < 0) {72warn("%s: read", __func__);73free(buf);74close(fd);75return (0);76}7778if (r != sb.st_size) {79fprintf(stderr, "%s: read len %d != file size %d\n",80__func__,81(int) r,82(int) sb.st_size);83free(buf);84close(fd);85return (0);86}8788/* We have everything, so! */8990bzero(fw, sizeof(*fw));9192fw->fwname = strdup(fwname);93fw->len = sb.st_size;94fw->size = sb.st_size;95fw->buf = buf;9697close(fd);98return (1);99}100101void102ath3k_fw_free(struct ath3k_firmware *fw)103{104if (fw->fwname)105free(fw->fwname);106if (fw->buf)107free(fw->buf);108bzero(fw, sizeof(*fw));109}110111112