summaryrefslogtreecommitdiffstats
path: root/carl9170fw/tools/src/eeprom_fix.c
blob: 088510edcc87e60b6ac0dc318590a7ff48232def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright 2010-2011 Christian Lamparter <chunkeey@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "carlfw.h"

#include "compiler.h"

static int get_val(char *str, unsigned int *val)
{
	int err;

	err = sscanf(str, "%8x", val);
	if (err != 1)
		return -EINVAL;

	return 0;
}

static int get_addr(char *str, unsigned int *val)
{
	int err;

	err = get_val(str, val);
	if (*val & 3) {
		fprintf(stderr, "Address 0x%.8x is not a multiple of 4.\n",
			*val);

		return -EINVAL;
	}

	return err;
}

static int
new_fix_entry(struct carlfw *fw, struct carl9170fw_fix_entry *fix_entry)
{
	struct carl9170fw_fix_desc *fix;
	unsigned int len;

	len = sizeof(*fix) + sizeof(*fix_entry);
	fix = malloc(len);
	if (!fix)
		return -ENOMEM;

	carl9170fw_fill_desc(&fix->head, (uint8_t *) FIX_MAGIC,
			      cpu_to_le16(len),
			      CARL9170FW_FIX_DESC_MIN_VER,
			      CARL9170FW_FIX_DESC_CUR_VER);

	memcpy(&fix->data[0], fix_entry, sizeof(*fix_entry));

	return carlfw_desc_add_tail(fw, &fix->head);
}

static struct carl9170fw_fix_entry *
scan_for_similar_fix(struct carl9170fw_fix_desc *fix, __le32 address)
{
	unsigned int i, entries;

	entries = (le16_to_cpu(fix->head.length) - sizeof(*fix)) /
		   sizeof(struct carl9170fw_fix_entry);

	for (i = 0; i < entries; i++) {
		if (address == fix->data[i].address)
			return &fix->data[i];
	}

	return NULL;
}

static int
add_another_fix_entry(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
		 struct carl9170fw_fix_entry *fix_entry)
{
	unsigned int entry;

	fix = carlfw_desc_mod_len(fw, &fix->head, sizeof(*fix_entry));
	if (IS_ERR_OR_NULL(fix))
		return (int) PTR_ERR(fix);

	entry = (le16_to_cpu(fix->head.length) - sizeof(*fix)) /
		sizeof(*fix_entry) - 1;

	memcpy(&fix->data[entry], fix_entry, sizeof(*fix_entry));
	return 0;
}

static int
update_entry(char option, struct carl9170fw_fix_entry *entry,
	     struct carl9170fw_fix_entry *fix)
{
	switch (option) {
	case '=':
		entry->mask = fix->mask;
		entry->value = fix->value;
		break;

	case 'O':
		entry->mask |= fix->mask;
		entry->value |= fix->value;
		break;

	case 'A':
		entry->mask &= fix->mask;
		entry->value &= fix->value;
		break;

	default:
		fprintf(stderr, "Unknown option: '%c'\n", option);
		return -EINVAL;
	}

	return 0;
}

static void user_education(void)
{
	fprintf(stderr, "Usage:\n");
	fprintf(stderr, "\teeprom_fix FW-FILE SWITCH [ADDRESS [VALUE MASK]]\n");

	fprintf(stderr, "\nDescription:\n");
	fprintf(stderr, "\tThis utility manage a set of overrides which "
			"commands the driver\n\tto load customized EEPROM' "
			"data for all specified addresses.\n");

	fprintf(stderr, "\nParameters:\n");
	fprintf(stderr, "\t'FW-FILE'  = firmware file [basename]\n");
	fprintf(stderr, "\t'SWITCH'   = [=|d|D]\n");
	fprintf(stderr, "\t | '='       => add/set value for address\n");
	fprintf(stderr, "\t | 'D'       => removes all EEPROM overrides\n");
	fprintf(stderr, "\t * 'd'       => removed override for 'address'\n");
	fprintf(stderr, "\n\t'ADDRESS'  = location of the EEPROM override\n");
	fprintf(stderr, "\t\t     NB: must be a multiple of 4.\n");
	fprintf(stderr, "\t\t     an address map can be found in eeprom.h.\n");
	fprintf(stderr, "\n\t'VALUE'    = replacement value\n");
	fprintf(stderr, "\t'MASK'     = mask for the value placement.\n\n");

	exit(EXIT_FAILURE);
}

static int
set_fix(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
	char __unused option, int __unused argc, char *args[])
{
	struct carl9170fw_fix_entry fix_entry, *entry = NULL;
	unsigned int address, value, mask;
	int err;

	err = get_addr(args[3], &address);
	if (err)
		return err;

	err = get_val(args[4], &value);
	if (err)
		return err;

	err = get_val(args[5], &mask);
	if (err)
		return err;

	fix_entry.address = cpu_to_le32(address);
	fix_entry.value = cpu_to_le32(value);
	fix_entry.mask = cpu_to_le32(mask);

	if (!fix) {
		err = new_fix_entry(fw, &fix_entry);
	} else {
		entry = scan_for_similar_fix(fix, fix_entry.address);
		if (entry) {
			err = update_entry(option, entry, &fix_entry);
			if (err)
				fprintf(stdout, "Overwrite old entry.\n");
		} else {
			err = add_another_fix_entry(fw, fix, &fix_entry);
		}
	}

	return err;
}

static int
del_fix(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
	char __unused option, int __unused argc, char *args[])
{
	struct carl9170fw_fix_entry *entry = NULL;
	unsigned int address;
	unsigned long off;
	unsigned int rem_len;
	int err;

	err = get_addr(args[3], &address);
	if (err)
		return err;

	if (fix)
		entry = scan_for_similar_fix(fix, cpu_to_le32(address));

	if (!entry) {
		fprintf(stderr, "Entry for 0x%.8x not found\n", address);
		return -EINVAL;
	}

	off = (unsigned long) entry - (unsigned long) fix->data;
	rem_len = le16_to_cpu(fix->head.length) - off;

	if (rem_len) {
		unsigned long cont;
		cont = (unsigned long) entry + sizeof(*entry);
		memmove(entry, (void *)cont, rem_len);
	}

	fix = carlfw_desc_mod_len(fw, &fix->head, -sizeof(*entry));
	err = IS_ERR_OR_NULL(fix);
	return err;
}

static int del_all(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
	char __unused option, int __unused argc, char __unused *args[])
{
	if (!fix)
		return 0;

	carlfw_desc_del(fw, &fix->head);
	return 0;
}

static const struct {
	char option;
	int argc;
	int (*func)(struct carlfw *, struct carl9170fw_fix_desc *,
		    char, int, char **);
} programm_function[] = {
	{ '=', 6, set_fix },
	{ 'O', 6, set_fix },
	{ 'A', 6, set_fix },
	{ 'd', 4, del_fix },
	{ 'D', 3, del_all },
};

int main(int argc, char *args[])
{
	struct carl9170fw_fix_desc *fix;
	struct carlfw *fw = NULL;
	unsigned int i;
	int err = 0;
	char option;

	if (argc < 3 || argc > 6) {
		err = -EINVAL;
		goto out;
	}

	fw = carlfw_load(args[1]);
	if (IS_ERR_OR_NULL(fw)) {
		err = PTR_ERR(fw);
		fprintf(stderr, "Failed to open file \"%s\" (%d).\n",
			args[1], err);
		goto out;
	}

	fix = carlfw_find_desc(fw, (uint8_t *)FIX_MAGIC, sizeof(*fix),
			       CARL9170FW_FIX_DESC_CUR_VER);

	option = args[2][0];
	for (i = 0; i < ARRAY_SIZE(programm_function); i++) {
		if (programm_function[i].option != option)
			continue;

		if (argc != programm_function[i].argc) {
			err = -EINVAL;
			goto out;
		}

		err = programm_function[i].func(fw, fix, option, argc, args);
		if (err)
			goto out;

		break;
	}
	if (i == ARRAY_SIZE(programm_function)) {
		fprintf(stderr, "Unknown option: '%c'\n",
			args[2][0]);
		goto out;
	}

	err = carlfw_store(fw);
	if (err) {
		fprintf(stderr, "Failed to apply changes (%d).\n", err);
		goto out;
	}

out:
	carlfw_release(fw);

	if (err) {
		if (err == -EINVAL)
			user_education();
		else
			fprintf(stderr, "%s\n", strerror(err));
	}

	return err ? EXIT_FAILURE : EXIT_SUCCESS;
}