xref: /freebsd/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2013 Adrian Chadd <adrian@freebsd.org>
5  * Copyright (c) 2019 Vladimir Kondratyev <wulf@FreeBSD.org>
6  * Copyright (c) 2023 Future Crew LLC.
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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <sys/stat.h>
33 
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "iwmbt_fw.h"
43 #include "iwmbt_dbg.h"
44 
45 int
46 iwmbt_fw_read(struct iwmbt_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 		iwmbt_err("read len %d != file size %d",
83 		    (int) r,
84 		    (int) sb.st_size);
85 		free(buf);
86 		close(fd);
87 		return (0);
88 	}
89 
90 	/* We have everything, so! */
91 
92 	memset(fw, 0, sizeof(*fw));
93 
94 	fw->fwname = strdup(fwname);
95 	fw->len = sb.st_size;
96 	fw->buf = buf;
97 
98 	close(fd);
99 	return (1);
100 }
101 
102 void
103 iwmbt_fw_free(struct iwmbt_firmware *fw)
104 {
105 	if (fw->fwname)
106 		free(fw->fwname);
107 	if (fw->buf)
108 		free(fw->buf);
109 	memset(fw, 0, sizeof(*fw));
110 }
111 
112 char *
113 iwmbt_get_fwname(struct iwmbt_version *ver, struct iwmbt_boot_params *params,
114     const char *prefix, const char *suffix)
115 {
116 	struct stat sb;
117 	char *fwname;
118 
119 	switch (ver->hw_variant) {
120 	case 0x07:	/* 7260 */
121 	case 0x08:	/* 7265 */
122 		asprintf(&fwname, "%s/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.%s",
123 		    prefix,
124 		    le16toh(ver->hw_platform),
125 		    le16toh(ver->hw_variant),
126 		    le16toh(ver->hw_revision),
127 		    le16toh(ver->fw_variant),
128 		    le16toh(ver->fw_revision),
129 		    le16toh(ver->fw_build_num),
130 		    le16toh(ver->fw_build_ww),
131 		    le16toh(ver->fw_build_yy),
132 		    suffix);
133 		/*
134 		 * Fallback to the default firmware patch if
135 		 * the correct firmware patch file is not found.
136 		 */
137 		if (stat(fwname, &sb) != 0 && errno == ENOENT) {
138 			free(fwname);
139 			asprintf(&fwname, "%s/ibt-hw-%x.%x.%s",
140 			    prefix,
141 			    le16toh(ver->hw_platform),
142 			    le16toh(ver->hw_variant),
143 			    suffix);
144 		}
145 		break;
146 
147 	case 0x0b:	/* 8260 */
148 	case 0x0c:	/* 8265 */
149 		asprintf(&fwname, "%s/ibt-%u-%u.%s",
150 		    prefix,
151 		    le16toh(ver->hw_variant),
152 		    le16toh(params->dev_revid),
153 		    suffix);
154 		break;
155 
156 	case 0x11:	/* 9560 */
157 	case 0x12:	/* 9260 */
158 	case 0x13:
159 	case 0x14:	/* 22161 */
160 		asprintf(&fwname, "%s/ibt-%u-%u-%u.%s",
161 		    prefix,
162 		    le16toh(ver->hw_variant),
163 		    le16toh(ver->hw_revision),
164 		    le16toh(ver->fw_revision),
165 		    suffix);
166 		break;
167 
168 	default:
169 		fwname = NULL;
170 	}
171 
172 	return (fwname);
173 }
174 
175 char *
176 iwmbt_get_fwname_tlv(struct iwmbt_version_tlv *ver, const char *prefix,
177     const char *suffix)
178 {
179 	char *fwname;
180 
181 #define	IWMBT_PACK_CNVX_TOP(cnvx_top)	((uint16_t)(	\
182 	((cnvx_top) & 0x0f000000) >> 16 |		\
183 	((cnvx_top) & 0x0000000f) << 12 |		\
184 	((cnvx_top) & 0x00000ff0) >> 4))
185 
186 	asprintf(&fwname, "%s/ibt-%04x-%04x.%s",
187 	    prefix,
188 	    IWMBT_PACK_CNVX_TOP(ver->cnvi_top),
189 	    IWMBT_PACK_CNVX_TOP(ver->cnvr_top),
190 	    suffix);
191 
192 	return (fwname);
193 }
194