forked from pepe2k/ar9300_eeprom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_eeproms.c
99 lines (79 loc) · 2.24 KB
/
io_eeproms.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
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#include <io.h>
#define open _open
#define read _read
#define write _write
#define lseek _lseek
#define close _close
#endif
#ifdef __GNUC__
#include <unistd.h>
#define O_BINARY 0
#endif
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include "ar9300_eeprom.h"
int read_eeproms(struct ar9300_eeprom eeproms[], const struct ar9300_layout *layout, char *filename)
{
int file;
int i = 0;
int offset;
if ((file = open(filename, O_BINARY | O_RDONLY)) == -1)
{
printf("\nFile not found: '%s'.\n", filename);
return -1;
}
for (i = 0; i < AR9300_MAX_EEPROMS && (offset = layout->offsets[i]) >= 0; i++)
{
struct ar9300_eeprom *eeprom = &eeproms[i];
if ((lseek(file, offset, SEEK_SET) != offset || read(file, eeprom, sizeof(struct ar9300_eeprom)) != sizeof(struct ar9300_eeprom)))
{
close(file);
return i;
}
}
close(file);
return AR9300_MAX_EEPROMS;
}
static char buffer[64*1024];
int write_eeproms(struct ar9300_eeprom eeproms[], const struct ar9300_layout *layout, char *filename, char *outname)
{
int i;
int out;
int file;
int bytes;
int offset;
if ((out = open(outname, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE)) == -1)
{
printf("\nFile not open for output: '%s'.\n", outname);
return -1;
}
if ((file = open(filename, O_BINARY | O_RDONLY)) == -1)
{
printf("\nFile not found: '%s'.\n", filename);
close(out);
return -1;
}
while ((bytes = read(file, buffer, sizeof(buffer))) > 0)
if (write(out, buffer, bytes) != bytes)
{
printf("\nFile not write to output: '%s'.\n", outname);
close(file);
close(out);
return -1;
}
close(file);
for (i = 0; i < AR9300_MAX_EEPROMS && (offset = layout->offsets[i]) >= 0; i++)
{
struct ar9300_eeprom *eeprom = &eeproms[i];
if ((lseek(out, offset, SEEK_SET) != offset || write(out, eeprom, sizeof(struct ar9300_eeprom)) != sizeof(struct ar9300_eeprom)))
{
close(out);
return i;
}
}
close(out);
return AR9300_MAX_EEPROMS;
}