xref: /freebsd/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Adrian Chadd <adrian@freebsd.org>
5  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/types.h>
32 #include <sys/endian.h>
33 #include <sys/stat.h>
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "iwmbt_fw.h"
44 #include "iwmbt_dbg.h"
45 
46 int
47 iwmbt_fw_read(struct iwmbt_firmware *fw, const char *fwname)
48 {
49 	int fd;
50 	struct stat sb;
51 	unsigned char *buf;
52 	ssize_t r;
53 	int i;
54 
55 	fd = open(fwname, O_RDONLY);
56 	if (fd < 0) {
57 		warn("%s: open: %s", __func__, fwname);
58 		return (0);
59 	}
60 
61 	if (fstat(fd, &sb) != 0) {
62 		warn("%s: stat: %s", __func__, fwname);
63 		close(fd);
64 		return (0);
65 	}
66 
67 	buf = calloc(1, sb.st_size);
68 	if (buf == NULL) {
69 		warn("%s: calloc", __func__);
70 		close(fd);
71 		return (0);
72 	}
73 
74 	i = 0;
75 	/* XXX handle partial reads */
76 	r = read(fd, buf, sb.st_size);
77 	if (r < 0) {
78 		warn("%s: read", __func__);
79 		free(buf);
80 		close(fd);
81 		return (0);
82 	}
83 
84 	if (r != sb.st_size) {
85 		iwmbt_err("read len %d != file size %d",
86 		    (int) r,
87 		    (int) sb.st_size);
88 		free(buf);
89 		close(fd);
90 		return (0);
91 	}
92 
93 	/* We have everything, so! */
94 
95 	memset(fw, 0, sizeof(*fw));
96 
97 	fw->fwname = strdup(fwname);
98 	fw->len = sb.st_size;
99 	fw->buf = buf;
100 
101 	close(fd);
102 	return (1);
103 }
104 
105 void
106 iwmbt_fw_free(struct iwmbt_firmware *fw)
107 {
108 	if (fw->fwname)
109 		free(fw->fwname);
110 	if (fw->buf)
111 		free(fw->buf);
112 	memset(fw, 0, sizeof(*fw));
113 }
114 
115 char *
116 iwmbt_get_fwname(struct iwmbt_version *ver, struct iwmbt_boot_params *params,
117     const char *prefix, const char *suffix)
118 {
119 	struct stat sb;
120 	char *fwname;
121 
122 	switch (ver->hw_variant) {
123 	case 0x07:	/* 7260 */
124 	case 0x08:	/* 7265 */
125 		asprintf(&fwname, "%s/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.%s",
126 		    prefix,
127 		    le16toh(ver->hw_platform),
128 		    le16toh(ver->hw_variant),
129 		    le16toh(ver->hw_revision),
130 		    le16toh(ver->fw_variant),
131 		    le16toh(ver->fw_revision),
132 		    le16toh(ver->fw_build_num),
133 		    le16toh(ver->fw_build_ww),
134 		    le16toh(ver->fw_build_yy),
135 		    suffix);
136 		/*
137 		 * Fallback to the default firmware patch if
138 		 * the correct firmware patch file is not found.
139 		 */
140 		if (stat(fwname, &sb) != 0 && errno == ENOENT) {
141 			free(fwname);
142 			asprintf(&fwname, "%s/ibt-hw-%x.%x.%s",
143 			    prefix,
144 			    le16toh(ver->hw_platform),
145 			    le16toh(ver->hw_variant),
146 			    suffix);
147 		}
148 		break;
149 
150 	case 0x0b:	/* 8260 */
151 	case 0x0c:	/* 8265 */
152 		asprintf(&fwname, "%s/ibt-%u-%u.%s",
153 		    prefix,
154 		    le16toh(ver->hw_variant),
155 		    le16toh(params->dev_revid),
156 		    suffix);
157 		break;
158 
159 	case 0x11:	/* 9560 */
160 	case 0x12:	/* 9260 */
161 	case 0x13:
162 	case 0x14:	/* 22161 */
163 		asprintf(&fwname, "%s/ibt-%u-%u-%u.%s",
164 		    prefix,
165 		    le16toh(ver->hw_variant),
166 		    le16toh(ver->hw_revision),
167 		    le16toh(ver->fw_revision),
168 		    suffix);
169 		break;
170 
171 	default:
172 		fwname = NULL;
173 	}
174 
175 	return (fwname);
176 }
177