-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfconfig.c
317 lines (284 loc) · 7.35 KB
/
fconfig.c
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
/*
* fconfig.c
*
* $Id: fconfig.c,v 1.3 2008/03/29 12:20:55 andrzej Exp $
*
* Redboot Flash Configuration parser.
* Main file.
*
* Copyright (C) 2006 Ekiert sp z o.o.
* Author: Andrzej Ekiert <a.ekiert@ekiert.com>
*
* Changes:
* 2007/10/21 - 'list' option added by Hamish Moffatt
* 2008/03/29 - 'offset' option added by wimpunk
*
* 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; either version
* 2 of the License, or (at your option) any later version.
*
* Usage hints:
* The 'device' is probably going to be an MTD device. You may also operate
* on an ordinary file. However, if you're working with MTD, it's highly
* recommended to open it as a character device, not as a block device
* (eg. open /dev/mtd[n], not /dev/mtdblock[n]). If an MTD device is emulating
* a block device, then the OS will do heavy caching (a lot of unneccessary
* reads), and will do block writes, while in character device mode it's
* possible to actually write exactly these bytes, that have been changed
* (see my 24xx I2C EEPROM driver - ee24.c). In case of operation with slow
* I2C EEPROMs this allows to speed up things and to save on EEPROM wear-out.
*
* The above should explain why mmap() is not used in this application ;-)
* (initially it was, but the performance was poor, when we're forced to work
* with slow block devices).
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include "debug.h"
#include "ftypes.h"
#include "crunchfc.h"
/*
* Parse type name, return type ID.
* Type ID is the type number in the type table.
*/
/*
static int8_t parse_type(uint8_t *type)
{
uint8_t i, ret = -1;
MESSAGE(VERB_HIGH, "Parsing type: %s\n", type);
for (i = 0; i < NUM_TYPES; i++) {
if (strncmp(TYPE_NAME(i), type, MAX_TYPE_NAME)==0) {
MESSAGE(VERB_HIGH, "Found type: ID = %d\n", i);
ret = i;
break;
}
}
return ret;
}
*/
/*
* Print usage information.
*/
static void usage(void)
{
uint8_t i;
if (verbosity == 0) {
return;
}
fputs("Read, write or list Redboot configuration\n", stdout);
fputs("usage: fconfig [-r|-w|-l]"
" -d dev -o offset -n nickname -x value\n", stdout);
fputs("'dev' may be a char device, block device or a file\n", stdout);
fputs("'offset' is the offset in bytes on the device\n", stdout);
fputs("Supported types: \n", stdout);
for (i = 0; i < NUM_TYPES; i++) {
printf(" - %s\n", TYPE_NAME(i));
}
fputs("Additional switches: \n", stdout);
fputs(" -v:\tVerbose mode, use more to increase verbosity\n", stdout);
fputs(" -s:\tSilent mode, absolutely no messages printed\n", stdout);
}
/*
* Open the file or device containing configuration and copy the configuration
* data to a buffer. We could mmap() here and operate directly on mmaped data
* (it'd make life easier later), but that would force us to work with MTD in
* block device emulation mode (see comments at the top of this file).
*
* Remember, that the MTD partition is likely to be "forced read-only",
* if the partition size is not equal to erase block size.
*/
static uint8_t buffer[MAX_CONFIG_DATA];
struct config_data *get_fconfig_handle(struct config_data *data,
uint8_t *dev, int offset, mode_t mode)
{
uint16_t count;
if ((data->fd = open(dev, mode)) < 0) {
MESSAGE(VERB_LOW, "Failed to open device or file %s!\n", dev);
return NULL;
}
data->offset = offset;
if (data->offset > 0) {
lseek(data->fd, data->offset, SEEK_SET);
}
count = read(data->fd, buffer, MAX_CONFIG_DATA);
if (count <= 0) {
MESSAGE(VERB_LOW, "Nothing read!\n");
close(data->fd);
return NULL;
}
MESSAGE(VERB_NORMAL, "Read %d bytes\n", count);
data->buf = buffer;
data->maxlen = count;
return data;
}
/*
* Synchronize the data back and close the file or device containing
* the configuration data.
*/
void close_fconfig_handle(struct config_data *data)
{
close(data->fd);
}
/*
* Write mode of operation: set parameter values in the configuration.
*/
static int write_mode(uint8_t *device, int offset,
uint8_t *nickname, uint8_t *value)
{
struct config_data data;
if (value == NULL) {
MESSAGE(VERB_LOW, "You must provide a value in WRITE mode\n");
return 1;
}
if (get_fconfig_handle(&data, device, offset, O_RDWR) == NULL) {
MESSAGE(VERB_LOW, "Could not get a config data handle!\n");
return 1;
}
if (verify_fconfig(&data)) {
MESSAGE(VERB_LOW, "Config verification failed!\n");
goto exit_fail;
}
if (set_key_value(&data, nickname, value)) {
goto exit_fail;
}
recalculate_crc(&data);
close_fconfig_handle(&data);
return 0;
exit_fail:
close_fconfig_handle(&data);
return 1;
}
/*
* Read mode of operation: get parameter values from the configuration.
*/
static int read_mode(uint8_t *device, int offset, uint8_t *nickname)
{
struct config_data data;
if (get_fconfig_handle(&data, device, offset, O_RDONLY) == NULL) {
MESSAGE(VERB_LOW, "Could not get a config data handle!\n");
return 1;
}
if (verify_fconfig(&data)) {
MESSAGE(VERB_LOW, "Config verification failed!\n");
goto exit_fail;
}
if (get_key_value(&data, nickname)) {
goto exit_fail;
}
close_fconfig_handle(&data);
return 0;
exit_fail:
close_fconfig_handle(&data);
return 1;
}
/*
* List mode of operation: list parameter values from the configuration.
*/
static int list_mode(uint8_t *device, int offset)
{
struct config_data data;
if (get_fconfig_handle(&data, device, offset, O_RDONLY) == NULL) {
MESSAGE(VERB_LOW, "Could not get a config data handle!\n");
return 1;
}
if (verify_fconfig(&data)) {
MESSAGE(VERB_LOW, "Config verification failed!\n");
goto exit_fail;
}
if (list_keys(&data)) {
goto exit_fail;
}
close_fconfig_handle(&data);
return 0;
exit_fail:
close_fconfig_handle(&data);
return 1;
}
#define MODE_NONE 0
#define MODE_WRITE 1
#define MODE_READ 2
#define MODE_LIST 3
/*
* main(). ...nuff said.
*/
int main(int argc, char **argv)
{
int c, ret;
uint8_t mode = MODE_NONE;
uint8_t *nickname = NULL;
uint8_t *value = NULL;
uint8_t *device = NULL;
int offset = 0;
while ((c = getopt(argc, argv, "hrwlvsd:n:o:x:")) != -1) {
switch (c) {
case 'r':
mode = MODE_READ;
break;
case 'w':
mode = MODE_WRITE;
break;
case 'l':
mode = MODE_LIST;
break;
case 'n':
nickname = optarg;
break;
case 'o':
sscanf(optarg, "%i", &(offset));
break;
case 'x':
value = optarg;
break;
case 'v':
verbosity++;
break;
case 's':
verbosity = 0;
break;
case 'd':
device = optarg;
break;
case 'h':
usage();
break;
case '?':
default:
usage();
exit(1);
break;
}
}
MESSAGE(VERB_LOW, "Low verbosity messages are printed.\n");
MESSAGE(VERB_NORMAL, "Normal verbosity messages are printed.\n");
MESSAGE(VERB_HIGH, "High verbosity messages are printed.\n");
if ((nickname == NULL) && (mode != MODE_LIST)) {
usage();
exit(1);
}
if (device == NULL) {
MESSAGE(VERB_LOW, "You must provide a device name.\n");
exit(1);
}
switch (mode) {
case MODE_WRITE :
ret = write_mode(device, offset, nickname, value);
break;
case MODE_READ :
ret = read_mode(device, offset, nickname);
break;
case MODE_LIST :
ret = list_mode(device, offset);
break;
default :
MESSAGE(VERB_LOW, "Unknown mode of operation\n");
usage();
ret = 1;
} //switch
return ret;
}