carl9170 tools: fix error code propagation
[carl9170fw.git] / tools / src / eeprom_fix.c
1 /*
2  * Copyright 2010-2011 Christian Lamparter <chunkeey@googlemail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <error.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26
27 #include "carlfw.h"
28
29 #include "compiler.h"
30
31 static int get_val(char *str, unsigned int *val)
32 {
33         int err;
34
35         err = sscanf(str, "%8x", val);
36         if (err != 1)
37                 return -EINVAL;
38
39         return 0;
40 }
41
42 static int get_addr(char *str, unsigned int *val)
43 {
44         int err;
45
46         err = get_val(str, val);
47         if (*val & 3) {
48                 fprintf(stderr, "Address 0x%.8x is not a multiple of 4.\n",
49                         *val);
50
51                 return -EINVAL;
52         }
53
54         return err;
55 }
56
57 static int
58 new_fix_entry(struct carlfw *fw, struct carl9170fw_fix_entry *fix_entry)
59 {
60         struct carl9170fw_fix_desc *fix;
61         unsigned int len;
62
63         len = sizeof(*fix) + sizeof(*fix_entry);
64         fix = malloc(len);
65         if (!fix)
66                 return -ENOMEM;
67
68         carl9170fw_fill_desc(&fix->head, (uint8_t *) FIX_MAGIC,
69                               cpu_to_le16(len),
70                               CARL9170FW_FIX_DESC_MIN_VER,
71                               CARL9170FW_FIX_DESC_CUR_VER);
72
73         memcpy(&fix->data[0], fix_entry, sizeof(*fix_entry));
74
75         return carlfw_desc_add_tail(fw, &fix->head);
76 }
77
78 static struct carl9170fw_fix_entry *
79 scan_for_similar_fix(struct carl9170fw_fix_desc *fix, __le32 address)
80 {
81         unsigned int i, entries;
82
83         entries = (le16_to_cpu(fix->head.length) - sizeof(*fix)) /
84                    sizeof(struct carl9170fw_fix_entry);
85
86         for (i = 0; i < entries; i++) {
87                 if (address == fix->data[i].address)
88                         return &fix->data[i];
89         }
90
91         return NULL;
92 }
93
94 static int
95 add_another_fix_entry(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
96                  struct carl9170fw_fix_entry *fix_entry)
97 {
98         unsigned int entry;
99
100         fix = carlfw_desc_mod_len(fw, &fix->head, sizeof(*fix_entry));
101         if (IS_ERR_OR_NULL(fix))
102                 return (int) PTR_ERR(fix);
103
104         entry = (le16_to_cpu(fix->head.length) - sizeof(*fix)) /
105                 sizeof(*fix_entry) - 1;
106
107         memcpy(&fix->data[entry], fix_entry, sizeof(*fix_entry));
108         return 0;
109 }
110
111 static int
112 update_entry(char option, struct carl9170fw_fix_entry *entry,
113              struct carl9170fw_fix_entry *fix)
114 {
115         switch (option) {
116         case '=':
117                 entry->mask = fix->mask;
118                 entry->value = fix->value;
119                 break;
120
121         case 'O':
122                 entry->mask |= fix->mask;
123                 entry->value |= fix->value;
124                 break;
125
126         case 'A':
127                 entry->mask &= fix->mask;
128                 entry->value &= fix->value;
129                 break;
130
131         default:
132                 fprintf(stderr, "Unknown option: '%c'\n", option);
133                 return -EINVAL;
134         }
135
136         return 0;
137 }
138
139 static void user_education(void)
140 {
141         fprintf(stderr, "Usage:\n");
142         fprintf(stderr, "\teeprom_fix FW-FILE SWITCH [ADDRESS [VALUE MASK]]\n");
143
144         fprintf(stderr, "\nDescription:\n");
145         fprintf(stderr, "\tThis utility manage a set of overrides which "
146                         "commands the driver\n\tto load customized EEPROM' "
147                         "data for all specified addresses.\n");
148
149         fprintf(stderr, "\nParameters:\n");
150         fprintf(stderr, "\t'FW-FILE'  = firmware file [basename]\n");
151         fprintf(stderr, "\t'SWITCH'   = [=|d|D]\n");
152         fprintf(stderr, "\t | '='       => add/set value for address\n");
153         fprintf(stderr, "\t | 'D'       => removes all EEPROM overrides\n");
154         fprintf(stderr, "\t * 'd'       => removed override for 'address'\n");
155         fprintf(stderr, "\n\t'ADDRESS'  = location of the EEPROM override\n");
156         fprintf(stderr, "\t\t     NB: must be a multiple of 4.\n");
157         fprintf(stderr, "\t\t     an address map can be found in eeprom.h.\n");
158         fprintf(stderr, "\n\t'VALUE'    = replacement value\n");
159         fprintf(stderr, "\t'MASK'     = mask for the value placement.\n\n");
160
161         exit(EXIT_FAILURE);
162 }
163
164 static int
165 set_fix(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
166         char __unused option, int __unused argc, char *args[])
167 {
168         struct carl9170fw_fix_entry fix_entry, *entry = NULL;
169         unsigned int address, value, mask;
170         int err;
171
172         err = get_addr(args[3], &address);
173         if (err)
174                 return err;
175
176         err = get_val(args[4], &value);
177         if (err)
178                 return err;
179
180         err = get_val(args[5], &mask);
181         if (err)
182                 return err;
183
184         fix_entry.address = cpu_to_le32(address);
185         fix_entry.value = cpu_to_le32(value);
186         fix_entry.mask = cpu_to_le32(mask);
187
188         if (!fix) {
189                 err = new_fix_entry(fw, &fix_entry);
190         } else {
191                 entry = scan_for_similar_fix(fix, fix_entry.address);
192                 if (entry) {
193                         err = update_entry(option, entry, &fix_entry);
194                         if (err)
195                                 fprintf(stdout, "Overwrite old entry.\n");
196                 } else {
197                         err = add_another_fix_entry(fw, fix, &fix_entry);
198                 }
199         }
200
201         return err;
202 }
203
204 static int
205 del_fix(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
206         char __unused option, int __unused argc, char *args[])
207 {
208         struct carl9170fw_fix_entry *entry = NULL;
209         unsigned int address;
210         unsigned long off;
211         unsigned int rem_len;
212         int err;
213
214         err = get_addr(args[3], &address);
215         if (err)
216                 return err;
217
218         if (fix)
219                 entry = scan_for_similar_fix(fix, cpu_to_le32(address));
220
221         if (!entry) {
222                 fprintf(stderr, "Entry for 0x%.8x not found\n", address);
223                 return -EINVAL;
224         }
225
226         off = (unsigned long) entry - (unsigned long) fix->data;
227         rem_len = le16_to_cpu(fix->head.length) - off;
228
229         if (rem_len) {
230                 unsigned long cont;
231                 cont = (unsigned long) entry + sizeof(*entry);
232                 memmove(entry, (void *)cont, rem_len);
233         }
234
235         fix = carlfw_desc_mod_len(fw, &fix->head, -sizeof(*entry));
236         err = IS_ERR_OR_NULL(fix);
237         return err;
238 }
239
240 static int del_all(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
241         char __unused option, int __unused argc, char __unused *args[])
242 {
243         if (!fix)
244                 return 0;
245
246         carlfw_desc_del(fw, &fix->head);
247         return 0;
248 }
249
250 static const struct {
251         char option;
252         int argc;
253         int (*func)(struct carlfw *, struct carl9170fw_fix_desc *,
254                     char, int, char **);
255 } programm_function[] = {
256         { '=', 6, set_fix },
257         { 'O', 6, set_fix },
258         { 'A', 6, set_fix },
259         { 'd', 4, del_fix },
260         { 'D', 3, del_all },
261 };
262
263 int main(int argc, char *args[])
264 {
265         struct carl9170fw_fix_desc *fix;
266         struct carlfw *fw = NULL;
267         unsigned int i;
268         int err = 0;
269         char option;
270
271         if (argc < 3 || argc > 6) {
272                 err = -EINVAL;
273                 goto out;
274         }
275
276         fw = carlfw_load(args[1]);
277         if (IS_ERR_OR_NULL(fw)) {
278                 err = PTR_ERR(fw);
279                 fprintf(stderr, "Failed to open file \"%s\" (%d).\n",
280                         args[1], err);
281                 goto out;
282         }
283
284         fix = carlfw_find_desc(fw, (uint8_t *)FIX_MAGIC, sizeof(*fix),
285                                CARL9170FW_FIX_DESC_CUR_VER);
286
287         option = args[2][0];
288         for (i = 0; i < ARRAY_SIZE(programm_function); i++) {
289                 if (programm_function[i].option != option)
290                         continue;
291
292                 if (argc != programm_function[i].argc) {
293                         err = -EINVAL;
294                         goto out;
295                 }
296
297                 err = programm_function[i].func(fw, fix, option, argc, args);
298                 if (err)
299                         goto out;
300
301                 break;
302         }
303         if (i == ARRAY_SIZE(programm_function)) {
304                 fprintf(stderr, "Unknown option: '%c'\n",
305                         args[2][0]);
306                 goto out;
307         }
308
309         err = carlfw_store(fw);
310         if (err) {
311                 fprintf(stderr, "Failed to apply changes (%d).\n", err);
312                 goto out;
313         }
314
315 out:
316         carlfw_release(fw);
317
318         if (err) {
319                 if (err == -EINVAL)
320                         user_education();
321                 else
322                         fprintf(stderr, "%s\n", strerror(err));
323         }
324
325         return err ? EXIT_FAILURE : EXIT_SUCCESS;
326 }