xref: /freebsd/usr.sbin/bluetooth/iwmbtfw/iwmbt_fw.c (revision ccfd87fe2ac0e2e6aeb1911a7d7cce6712a8564f)
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  *
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 
54 	fd = open(fwname, O_RDONLY);
55 	if (fd < 0) {
56 		warn("%s: open: %s", __func__, fwname);
57 		return (0);
58 	}
59 
60 	if (fstat(fd, &sb) != 0) {
61 		warn("%s: stat: %s", __func__, fwname);
62 		close(fd);
63 		return (0);
64 	}
65 
66 	buf = calloc(1, sb.st_size);
67 	if (buf == NULL) {
68 		warn("%s: calloc", __func__);
69 		close(fd);
70 		return (0);
71 	}
72 
73 	/* XXX handle partial reads */
74 	r = read(fd, buf, sb.st_size);
75 	if (r < 0) {
76 		warn("%s: read", __func__);
77 		free(buf);
78 		close(fd);
79 		return (0);
80 	}
81 
82 	if (r != sb.st_size) {
83 		iwmbt_err("read len %d != file size %d",
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 	memset(fw, 0, sizeof(*fw));
94 
95 	fw->fwname = strdup(fwname);
96 	fw->len = sb.st_size;
97 	fw->buf = buf;
98 
99 	close(fd);
100 	return (1);
101 }
102 
103 void
104 iwmbt_fw_free(struct iwmbt_firmware *fw)
105 {
106 	if (fw->fwname)
107 		free(fw->fwname);
108 	if (fw->buf)
109 		free(fw->buf);
110 	memset(fw, 0, sizeof(*fw));
111 }
112 
113 char *
114 iwmbt_get_fwname(struct iwmbt_version *ver, struct iwmbt_boot_params *params,
115     const char *prefix, const char *suffix)
116 {
117 	struct stat sb;
118 	char *fwname;
119 
120 	switch (ver->hw_variant) {
121 	case 0x07:	/* 7260 */
122 	case 0x08:	/* 7265 */
123 		asprintf(&fwname, "%s/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.%s",
124 		    prefix,
125 		    le16toh(ver->hw_platform),
126 		    le16toh(ver->hw_variant),
127 		    le16toh(ver->hw_revision),
128 		    le16toh(ver->fw_variant),
129 		    le16toh(ver->fw_revision),
130 		    le16toh(ver->fw_build_num),
131 		    le16toh(ver->fw_build_ww),
132 		    le16toh(ver->fw_build_yy),
133 		    suffix);
134 		/*
135 		 * Fallback to the default firmware patch if
136 		 * the correct firmware patch file is not found.
137 		 */
138 		if (stat(fwname, &sb) != 0 && errno == ENOENT) {
139 			free(fwname);
140 			asprintf(&fwname, "%s/ibt-hw-%x.%x.%s",
141 			    prefix,
142 			    le16toh(ver->hw_platform),
143 			    le16toh(ver->hw_variant),
144 			    suffix);
145 		}
146 		break;
147 
148 	case 0x0b:	/* 8260 */
149 	case 0x0c:	/* 8265 */
150 		asprintf(&fwname, "%s/ibt-%u-%u.%s",
151 		    prefix,
152 		    le16toh(ver->hw_variant),
153 		    le16toh(params->dev_revid),
154 		    suffix);
155 		break;
156 
157 	case 0x11:	/* 9560 */
158 	case 0x12:	/* 9260 */
159 	case 0x13:
160 	case 0x14:	/* 22161 */
161 		asprintf(&fwname, "%s/ibt-%u-%u-%u.%s",
162 		    prefix,
163 		    le16toh(ver->hw_variant),
164 		    le16toh(ver->hw_revision),
165 		    le16toh(ver->fw_revision),
166 		    suffix);
167 		break;
168 
169 	default:
170 		fwname = NULL;
171 	}
172 
173 	return (fwname);
174 }
175