1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 Sam Leffler, Errno Consulting
5 * All rights reserved.
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 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
30 *
31 * $FreeBSD$
32 */
33
34 /*
35 * Atheros AR5523 USB Station Firmware downloader.
36 *
37 * uathload -d ugen-device [firmware-file]
38 *
39 * Intended to be called from devd on device discovery.
40 */
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/endian.h>
44 #include <sys/mman.h>
45
46 #include <sys/ioctl.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usb_ioctl.h>
49
50 #include <err.h>
51 #include <fcntl.h>
52 #include <libgen.h>
53 #include <paths.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <strings.h>
58 #include <unistd.h>
59
60 /* all fields are big endian */
61 struct uath_fwmsg {
62 uint32_t flags;
63 #define UATH_WRITE_BLOCK (1 << 4)
64
65 uint32_t len;
66 #define UATH_MAX_FWBLOCK_SIZE 2048
67
68 uint32_t total;
69 uint32_t remain;
70 uint32_t rxtotal;
71 uint32_t pad[123];
72 } __packed;
73
74 #define UATH_DATA_TIMEOUT 10000
75 #define UATH_CMD_TIMEOUT 1000
76
77 #define VERBOSE(_fmt, ...) do { \
78 if (verbose) { \
79 printf(_fmt, __VA_ARGS__); \
80 fflush(stdout); \
81 } \
82 } while (0)
83
84 extern uint8_t _binary_ar5523_bin_start;
85 extern uint8_t _binary_ar5523_bin_end;
86
87 static int
getdevname(const char * udevname,char * msgdev,char * datadev)88 getdevname(const char *udevname, char *msgdev, char *datadev)
89 {
90 char *bn, *bnbuf, *dn, *dnbuf;
91
92 dnbuf = strdup(udevname);
93 if (dnbuf == NULL)
94 return (-1);
95 dn = dirname(dnbuf);
96 bnbuf = strdup(udevname);
97 if (bnbuf == NULL) {
98 free(dnbuf);
99 return (-1);
100 }
101 bn = basename(bnbuf);
102 if (strncmp(bn, "ugen", 4) != 0) {
103 free(dnbuf);
104 free(bnbuf);
105 return (-1);
106 }
107 bn += 4;
108
109 /* NB: pipes are hardcoded */
110 snprintf(msgdev, 256, "%s/usb/%s.1", dn, bn);
111 snprintf(datadev, 256, "%s/usb/%s.2", dn, bn);
112 free(dnbuf);
113 free(bnbuf);
114 return (0);
115 }
116
117 static void
usage(void)118 usage(void)
119 {
120 errx(-1, "usage: uathload [-v] -d devname [firmware]");
121 }
122
123 int
main(int argc,char * argv[])124 main(int argc, char *argv[])
125 {
126 const char *fwname, *udevname;
127 char msgdev[256], datadev[256];
128 struct uath_fwmsg txmsg, rxmsg;
129 char *txdata;
130 struct stat sb;
131 int msg, data, fw, timeout, b, c;
132 int bufsize = 512, verbose = 0;
133 ssize_t len;
134
135 udevname = NULL;
136 while ((c = getopt(argc, argv, "d:v")) != -1) {
137 switch (c) {
138 case 'd':
139 udevname = optarg;
140 break;
141 case 'v':
142 verbose = 1;
143 break;
144 default:
145 usage();
146 /*NOTREACHED*/
147 }
148 }
149 argc -= optind;
150 argv += optind;
151
152 if (udevname == NULL)
153 errx(-1, "No device name; use -d to specify the ugen device");
154 if (argc > 1)
155 usage();
156
157 if (argc == 1)
158 fwname = argv[0];
159 else
160 fwname = _PATH_FIRMWARE "/ar5523.bin";
161 fw = open(fwname, O_RDONLY, 0);
162 if (fw < 0)
163 err(-1, "open(%s)", fwname);
164 if (fstat(fw, &sb) < 0)
165 err(-1, "fstat(%s)", fwname);
166 txdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fw, 0);
167 if (txdata == MAP_FAILED)
168 err(-1, "mmap(%s)", fwname);
169 len = sb.st_size;
170 /* XXX verify device is an AR5005 part */
171 if (getdevname(udevname, msgdev, datadev))
172 err(-1, "getdevname error");
173
174 msg = open(msgdev, O_RDWR, 0);
175 if (msg < 0)
176 err(-1, "open(%s)", msgdev);
177 timeout = UATH_DATA_TIMEOUT;
178 if (ioctl(msg, USB_SET_RX_TIMEOUT, &timeout) < 0)
179 err(-1, "%s: USB_SET_RX_TIMEOUT(%u)", msgdev, UATH_DATA_TIMEOUT);
180 if (ioctl(msg, USB_SET_RX_BUFFER_SIZE, &bufsize) < 0)
181 err(-1, "%s: USB_SET_RX_BUFFER_SIZE(%u)", msgdev, bufsize);
182
183 data = open(datadev, O_WRONLY, 0);
184 if (data < 0)
185 err(-1, "open(%s)", datadev);
186 timeout = UATH_DATA_TIMEOUT;
187 if (ioctl(data, USB_SET_TX_TIMEOUT, &timeout) < 0)
188 err(-1, "%s: USB_SET_TX_TIMEOUT(%u)", datadev,
189 UATH_DATA_TIMEOUT);
190
191 VERBOSE("Load firmware %s to %s\n", fwname, udevname);
192
193 bzero(&txmsg, sizeof (struct uath_fwmsg));
194 txmsg.flags = htobe32(UATH_WRITE_BLOCK);
195 txmsg.total = htobe32(len);
196
197 b = 0;
198 while (len > 0) {
199 int mlen;
200
201 mlen = len;
202 if (mlen > UATH_MAX_FWBLOCK_SIZE)
203 mlen = UATH_MAX_FWBLOCK_SIZE;
204 txmsg.remain = htobe32(len - mlen);
205 txmsg.len = htobe32(mlen);
206
207 /* send firmware block meta-data */
208 VERBOSE("send block %2u: %zd bytes remaining", b, len - mlen);
209 if (write(msg, &txmsg, sizeof(txmsg)) != sizeof(txmsg)) {
210 VERBOSE("%s", "\n");
211 err(-1, "error sending msg (%s)", msgdev);
212 break;
213 }
214
215 /* send firmware block data */
216 VERBOSE("%s", "\n : data...");
217 if (write(data, txdata, mlen) != mlen) {
218 VERBOSE("%s", "\n");
219 err(-1, "error sending data (%s)", datadev);
220 break;
221 }
222
223 /* wait for ack from firmware */
224 VERBOSE("%s", "\n : wait for ack...");
225 bzero(&rxmsg, sizeof(rxmsg));
226 if (read(msg, &rxmsg, sizeof(rxmsg)) != sizeof(rxmsg)) {
227 VERBOSE("%s", "\n");
228 err(-1, "error reading msg (%s)", msgdev);
229 break;
230 }
231
232 VERBOSE("flags=0x%x total=%d\n",
233 be32toh(rxmsg.flags), be32toh(rxmsg.rxtotal));
234 len -= mlen;
235 txdata += mlen;
236 b++;
237 }
238 sleep(1);
239 close(fw);
240 close(msg);
241 close(data);
242 return 0;
243 }
244