CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/wl/dhd/ucode_download.c
Views: 3960
1
/*
2
* Ucode download related utility functions
3
*
4
* $Copyright Open Broadcom Corporation$
5
*
6
* $Id: ucode_download.c 297277 2011-11-18 14:10:09Z $
7
*/
8
9
#include <unistd.h>
10
#include <errno.h>
11
#include <trxhdr.h>
12
#include <bcmendian.h>
13
#include <wlu_common.h>
14
15
#define DEVPRESENT_DELAY 10000 /* in microsecs */
16
#define DEVPRESENT_RETRIES 100
17
18
extern int wl_validatedev(void *dev_handle);
19
20
int
21
dload_generic_data(void *wl, uint16 dload_type, unsigned char *dload_buf, int len)
22
{
23
struct wl_dload_data *dload_ptr = (struct wl_dload_data *)dload_buf;
24
int err = 0;
25
int actual_data_offset;
26
char *buf;
27
28
actual_data_offset = OFFSETOF(struct wl_dload_data, data);
29
dload_ptr->flag = (DLOAD_HANDLER_VER << DLOAD_FLAG_VER_SHIFT);
30
dload_ptr->flag |= DL_CRC_NOT_INUSE;
31
dload_ptr->dload_type = dload_type;
32
dload_ptr->len = htod32(len - actual_data_offset);
33
dload_ptr->crc = 0;
34
35
len = len + 8 - (len%8);
36
37
buf = malloc(WLC_IOCTL_MEDLEN);
38
if (buf) {
39
bzero(buf, WLC_IOCTL_MEDLEN);
40
err = wlu_iovar_setbuf(wl, "generic_dload", dload_buf, len, buf,
41
WLC_IOCTL_MEDLEN);
42
}
43
free(buf);
44
return err;
45
}
46
47
int
48
dload_ucode_part(void *wl, uint8 ucode_type, uint32 datalen, unsigned char *org_buf)
49
{
50
int num_chunks, chunk_len, cumulative_len = 0;
51
int size2alloc, ucode_chunk_len = 0;
52
unsigned char *new_buf;
53
struct wl_ucode_info *ucode_ptr;
54
int err = 0, ucode_offset, chunk_offset;
55
56
ucode_offset = OFFSETOF(wl_dload_data_t, data);
57
chunk_offset = OFFSETOF(wl_ucode_info_t, data_chunk);
58
59
err = wlu_iovar_getint(wl, "ucdload_chunk_len",
60
&ucode_chunk_len);
61
if (err) {
62
printf("err in getting ucode chunk len, exiting\n");
63
return err;
64
}
65
66
num_chunks = datalen/ucode_chunk_len;
67
if (datalen % ucode_chunk_len != 0)
68
num_chunks++;
69
size2alloc = ucode_offset + chunk_offset + ucode_chunk_len;
70
71
/* single chunk buffer */
72
new_buf = (unsigned char *)malloc(size2alloc);
73
memset(new_buf, 0, size2alloc);
74
ucode_ptr = (struct wl_ucode_info *)((uint8 *)new_buf+ucode_offset);
75
ucode_ptr->ucode_type = ucode_type;
76
ucode_ptr->num_chunks = num_chunks;
77
do {
78
if (datalen >= ucode_chunk_len)
79
chunk_len = ucode_chunk_len;
80
else
81
chunk_len = datalen;
82
memset(new_buf+ucode_offset+chunk_offset, 0, size2alloc-ucode_offset-chunk_offset);
83
ucode_ptr->chunk_len = htod32(chunk_len);
84
ucode_ptr->chunk_num++;
85
memcpy(&ucode_ptr->data_chunk[0], org_buf + cumulative_len, chunk_len);
86
cumulative_len += chunk_len;
87
err = dload_generic_data(wl, DL_TYPE_UCODE, new_buf, size2alloc);
88
if (err) {
89
printf("error while writing %s to the memory\n",
90
(ucode_type == UCODE_FW)? "ucode" : "initvals");
91
break;
92
}
93
datalen = datalen - chunk_len;
94
} while (datalen > 0);
95
free(new_buf);
96
97
return err;
98
}
99
100
static int
101
check_ucode_file(unsigned char *headers)
102
{
103
struct trx_header *trx;
104
int actual_data_len = -1;
105
106
/* Extract trx header */
107
trx = (struct trx_header *)headers;
108
if (trx->magic != TRX_MAGIC) {
109
printf("Error: trx bad hdr\n");
110
goto err;
111
}
112
actual_data_len = ROUNDUP(trx->offsets[0], 4) + ROUNDUP(trx->offsets[1], 4);
113
err:
114
return actual_data_len;
115
}
116
117
int
118
proc_ucode_download(char* fw_filename, void *dev_handle)
119
{
120
FILE *fp = NULL;
121
int ret = 0, loopcnt = 0;
122
struct trx_header main_trx_hdr, *ucode_trx_hdr;
123
uint32 maintrx_hdr_len, tmp_len;
124
uint32 fw_size, second_offset, ucode_trx_offset;
125
long ucode_pos;
126
unsigned long ucode_info_len = 0, status;
127
unsigned char *ucodetrx_buf, *initvals_ptr;
128
int ucode_len, initvals_len;
129
int ucdload_status = 0;
130
int is_devpresent;
131
132
/* read the file and push blocks down to memory */
133
if ((fp = fopen(fw_filename, "rb")) == NULL) {
134
fprintf(stderr, "%s: unable to open %s: %s\n",
135
__FUNCTION__, fw_filename, strerror(errno));
136
ret = -1;
137
goto exit;
138
}
139
140
maintrx_hdr_len = sizeof(struct trx_header);
141
tmp_len = fread(&main_trx_hdr, sizeof(uint8), maintrx_hdr_len, fp);
142
143
if (tmp_len == maintrx_hdr_len) {
144
if (main_trx_hdr.magic == TRX_MAGIC) {
145
fw_size = main_trx_hdr.offsets[0];
146
second_offset = main_trx_hdr.offsets[2];
147
148
if (second_offset == maintrx_hdr_len) {
149
second_offset = 0;
150
}
151
ucode_trx_offset = maintrx_hdr_len +
152
ROUNDUP(fw_size, 4) + ROUNDUP(second_offset, 4);
153
ucode_pos = fseek(fp, ucode_trx_offset, SEEK_SET);
154
BCM_REFERENCE(ucode_pos);
155
156
if ((ucode_trx_hdr = malloc(sizeof(struct trx_header)))
157
== NULL) {
158
printf("Unable to allocate %d bytes!\n", maintrx_hdr_len);
159
ret = -ENOMEM;
160
goto exit;
161
}
162
163
/* Read ONLY the firmware-file-header into the new_buffer */
164
status = fread(ucode_trx_hdr, sizeof(uint8),
165
maintrx_hdr_len, fp);
166
if (status < sizeof(struct trx_header)) {
167
printf("Short read in hdr read for %s!\n", fw_filename);
168
ret = -EINVAL;
169
goto exit;
170
}
171
172
if ((ucode_info_len = check_ucode_file(
173
(unsigned char *)ucode_trx_hdr)) <= 0) {
174
printf("not a valid ucode.trx\n");
175
ret = -1;
176
goto exit;
177
}
178
179
ucodetrx_buf = (unsigned char *)malloc(ucode_info_len *
180
sizeof(char));
181
tmp_len = fread(ucodetrx_buf, sizeof(uint8),
182
ucode_info_len, fp);
183
if (ucode_info_len > 0) {
184
ucode_len = ucode_trx_hdr->offsets[0];
185
initvals_ptr = ucodetrx_buf +
186
ROUNDUP(ucode_trx_hdr->offsets[0], 4);
187
initvals_len = ucode_trx_hdr->offsets[1];
188
}
189
free(ucode_trx_hdr);
190
191
init_cmd_batchingmode();
192
do {
193
is_devpresent = wl_validatedev(dev_handle);
194
loopcnt++;
195
/* in USB after dongle fw starts running wl interface
196
might not appear in the list of interfaces immediately, hence try
197
after some delay of 10ms
198
*/
199
if (!is_devpresent)
200
usleep(DEVPRESENT_DELAY);
201
else {
202
/* below iovar to verify if the for foundout
203
interface has already ucode been downloaded
204
*/
205
ret = wlu_iovar_getint(dev_handle, "ucdload_status",
206
&ucdload_status);
207
if (ret) {
208
printf("err in ucdload_status, exiting\n");
209
goto exit;
210
}
211
if (ucdload_status) {
212
/* Number of 'wl' interfaces to skip
213
in the next round of going thru wl_find
214
*/
215
printf("ucode is already downloaded\n");
216
}
217
}
218
/* usb seems to take some time to come up, hence the
219
loop value of 100
220
*/
221
} while (loopcnt < DEVPRESENT_RETRIES && !is_devpresent);
222
223
if (loopcnt < DEVPRESENT_RETRIES) {
224
/* download the ucode fw */
225
ret = dload_ucode_part(dev_handle, UCODE_FW, ucode_len,
226
ucodetrx_buf);
227
if (ret) {
228
printf("error while downloading ucode, exiting\n");
229
goto exit;
230
}
231
/* download the initvals to the dongle */
232
ret = dload_ucode_part(dev_handle, INIT_VALS,
233
initvals_len, initvals_ptr);
234
235
if (ret) {
236
printf("error while downloading initvals, exiting\n");
237
goto exit;
238
}
239
}
240
else {
241
printf("wl device is not present\n");
242
}
243
free(ucodetrx_buf);
244
}
245
}
246
247
exit:
248
if (fp)
249
fclose(fp);
250
return ret;
251
}
252
253