forked from Alex20129/eeprom_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
343 lines (292 loc) · 8.37 KB
/
Copy pathmain.c
File metadata and controls
343 lines (292 loc) · 8.37 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "eeprom_defs.h"
#include "eeprom_structure.h"
#include "eeprom_ops.h"
#include "ui.h"
#ifdef HAVE_I2C_SUPPORT
#include "i2c_eeprom.h"
#endif
#define MAX_FILENAME 256
static int read_eeprom_file(const char *filename, uint8_t *buffer)
{
FILE *file = fopen(filename, "rb");
if (!file)
{
printf("Error: Cannot open file %s\n", filename);
return -1;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
if (file_size <= 0 || file_size > EEPROM_SIZE)
{
printf("Error: Invalid file size: %ld bytes (expected 1-%d)\n",
file_size, EEPROM_SIZE);
fclose(file);
return -4;
}
size_t read_size = fread(buffer, 1, file_size, file);
fclose(file);
if (read_size != (size_t)file_size)
{
printf("Error: Failed to read file completely\n");
return -2;
}
printf("Read %zu bytes from %s\n", read_size, filename);
return 0;
}
static int write_eeprom_file(const char *filename, const uint8_t *buffer, size_t size)
{
FILE *file = fopen(filename, "wb");
if (!file)
{
printf("Error: Cannot open file %s for writing\n", filename);
return -1;
}
size_t written_size = fwrite(buffer, 1, size, file);
fclose(file);
if (written_size != size)
{
printf("Error: Failed to write all data. Wrote %zu of %zu bytes\n",
written_size, size);
return -3;
}
printf("Successfully wrote %zu bytes to %s\n", size, filename);
return 0;
}
// ═══════════════════════════════════════════════════════════════
// Функции для работы с EEPROM
// ═══════════════════════════════════════════════════════════════
static void decode_and_print_eeprom(uint8_t *data)
{
EEPROMVersion version = eeprom_detect_version(data);
// Decode data
if (eeprom_decode(data, EEPROM_SIZE, version) != EEPROM_SUCCESS)
{
ui_print_error("Failed to decode EEPROM");
return;
}
// Parse and print structure
switch(version)
{
case EEPROM_VERSION_V1:
{
EEPROMStructure_v1 eeprom_v1;
eeprom_v1_parse(&eeprom_v1, data);
ui_print_eeprom(&eeprom_v1, version);
break;
}
case EEPROM_VERSION_V4:
case EEPROM_VERSION_V5:
case EEPROM_VERSION_V6:
{
EEPROMStructure eeprom;
eeprom_from_bytes(&eeprom, data);
ui_print_eeprom(&eeprom, version);
break;
}
case EEPROM_VERSION_V17:
{
EEPROMStructure_v17 eeprom_v17;
eeprom_v17_parse(&eeprom_v17, data);
ui_print_eeprom(&eeprom_v17, version);
break;
}
default:
ui_print_error("Unsupported EEPROM version: %d", version);
break;
}
}
static void encode_and_save_eeprom(const char *filename, EEPROMStructure *eeprom)
{
uint8_t data[EEPROM_SIZE];
eeprom_to_bytes(eeprom, data);
if (eeprom_encode(data, EEPROM_SIZE, (EEPROMVersion)eeprom->eeprom_version) != EEPROM_SUCCESS)
{
printf("Error: Failed to encode EEPROM\n");
return;
}
if (write_eeprom_file(filename, data, EEPROM_SIZE) == 0)
{
printf("EEPROM data successfully encoded and saved to %s\n", filename);
}
}
static void encode_and_save_eeprom_v1(const char *filename, EEPROMStructure_v1 *eeprom)
{
uint8_t data[EEPROM_SIZE];
memset(data, 0xFF, EEPROM_SIZE);
eeprom_v1_serialize(eeprom, data);
if (eeprom_encode(data, EEPROM_SIZE, EEPROM_VERSION_V1) != EEPROM_SUCCESS)
{
printf("Error: Failed to encode EEPROM v1\n");
return;
}
size_t size = eeprom_get_used_size(EEPROM_VERSION_V1);
if (write_eeprom_file(filename, data, size) == 0)
{
printf("EEPROM v1 data successfully encoded and saved to %s\n", filename);
}
}
static void encode_and_save_eeprom_v17(const char *filename, EEPROMStructure_v17 *eeprom)
{
uint8_t data[EEPROM_SIZE];
memset(data, 0xFF, EEPROM_SIZE);
eeprom_v17_serialize(eeprom, data);
if (eeprom_encode(data, EEPROM_SIZE, EEPROM_VERSION_V17) != EEPROM_SUCCESS)
{
printf("Error: Failed to encode EEPROM v17\n");
return;
}
size_t size = eeprom_get_used_size(EEPROM_VERSION_V17);
if (write_eeprom_file(filename, data, size) == 0)
{
printf("EEPROM v17 data successfully encoded and saved to %s\n", filename);
}
}
// ═══════════════════════════════════════════════════════════════
// Главное меню
// ═══════════════════════════════════════════════════════════════
#include <locale.h>
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "en_US.UTF-8");
char input_filename[MAX_FILENAME];
char output_filename[MAX_FILENAME];
uint8_t data[EEPROM_SIZE];
while (1)
{
printf("\nEEPROM Tool Menu:\n");
printf("1. Decode and Print EEPROM from file\n");
printf("2. Decode, Edit, and Encode EEPROM from file\n");
#ifdef HAVE_I2C_SUPPORT
printf("3. Read EEPROM from I2C board\n");
printf("4. Exit\n");
#else
printf("3. Exit\n");
#endif
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter input filename: ");
scanf("%255s", input_filename);
memset(data, 0xFF, EEPROM_SIZE);
if (read_eeprom_file(input_filename, data) == 0)
{
decode_and_print_eeprom(data);
}
break;
case 2:
printf("Enter input filename: ");
scanf("%255s", input_filename);
memset(data, 0xFF, EEPROM_SIZE);
if (read_eeprom_file(input_filename, data) == 0)
{
EEPROMVersion version = eeprom_detect_version(data);
if (eeprom_decode(data, EEPROM_SIZE, version) != EEPROM_SUCCESS)
{
ui_print_error("Failed to decode EEPROM");
break;
}
if (version == EEPROM_VERSION_V1)
{
EEPROMStructure_v1 eeprom_v1;
eeprom_v1_parse(&eeprom_v1, data);
if (eeprom_edit_interactive(&eeprom_v1, version) == EEPROM_SUCCESS)
{
printf("Enter output filename: ");
scanf("%255s", output_filename);
encode_and_save_eeprom_v1(output_filename, &eeprom_v1);
}
}
else if (version >= EEPROM_VERSION_V4 && version <= EEPROM_VERSION_V6)
{
EEPROMStructure eeprom;
eeprom_from_bytes(&eeprom, data);
if (eeprom_edit_interactive(&eeprom, version) == EEPROM_SUCCESS)
{
printf("Enter output filename: ");
scanf("%255s", output_filename);
encode_and_save_eeprom(output_filename, &eeprom);
}
}
else if (version == EEPROM_VERSION_V17)
{
EEPROMStructure_v17 eeprom_v17;
eeprom_v17_parse(&eeprom_v17, data);
if (eeprom_edit_interactive(&eeprom_v17, version) == EEPROM_SUCCESS)
{
printf("Enter output filename: ");
scanf("%255s", output_filename);
encode_and_save_eeprom_v17(output_filename, &eeprom_v17);
}
}
else
{
ui_print_error("Unsupported EEPROM version for editing");
}
}
break;
#ifdef HAVE_I2C_SUPPORT
case 3:
{
char i2c_device[MAX_FILENAME];
int i2c_addr;
printf("Enter I2C device path (e.g., /dev/i2c-0): ");
scanf("%255s", i2c_device);
printf("Enter I2C device address (0x50-0x53, default 0x50): ");
if (scanf("%i", &i2c_addr) != 1)
{
i2c_addr = 0x50;
}
int fd = iic_open(i2c_device, NULL);
if (fd < 0)
{
ui_print_error("Failed to open I2C device: %s", i2c_device);
break;
}
memset(data, 0xFF, EEPROM_SIZE);
int result = iic_eeprom_load(fd, (uint8_t)i2c_addr, 0, data, EEPROM_SIZE);
iic_close(fd);
if (result < 0)
{
ui_print_error("Failed to read EEPROM from I2C");
break;
}
ui_print_success("Successfully read %d bytes from I2C device", EEPROM_SIZE);
// Decode and print
decode_and_print_eeprom(data);
// Ask if user wants to save to file
printf("\nSave to file? (y/n): ");
char save_choice;
scanf(" %c", &save_choice);
if (save_choice == 'y' || save_choice == 'Y')
{
printf("Enter output filename: ");
scanf("%255s", output_filename);
if (write_eeprom_file(output_filename, data, EEPROM_SIZE) == 0)
{
ui_print_success("Data saved to %s", output_filename);
}
}
break;
}
case 4:
printf("Exiting program.\n");
return 0;
#else
case 3:
printf("Exiting program.\n");
return 0;
#endif
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}