-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatheros_extract.c
115 lines (102 loc) · 3.05 KB
/
atheros_extract.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
/*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*
* Copyright (C) 2013 Álvaro Fernández Rojas <noltari@gmail.com>
*/
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "atheros_lib.h"
#include "atheros_extract.h"
#define pr_fmt(fmt) "athext: " fmt
int main (int argc, char **argv) {
char *file_input = NULL, *file_output = NULL;
int eeprom_offset = -1, eeprom_size = ATHEROS_EEPROM_SIZE;
bool help = false;
// Parse arguments
while(true) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, NULL, 0},
{"input", required_argument, NULL, 0},
{"output", required_argument, NULL, 0},
{"offset", required_argument, NULL, 0},
{"size", required_argument, NULL, 0},
{NULL, no_argument, NULL, 0}
};
int c = getopt_long_only(argc, argv, "", long_options, &option_index);
if(c == 0) {
switch(option_index) {
case OPT_HELP:
help = true;
break;
case OPT_INPUT:
file_input = optarg;
break;
case OPT_OUTPUT:
file_output = optarg;
break;
case OPT_OFFSET:
eeprom_offset = atoi(optarg);
break;
case OPT_SIZE:
eeprom_size = atoi(optarg);
break;
}
}
else {
break;
}
}
// Check if needed arguments have been supplied
if(file_input == NULL || file_output == NULL || eeprom_offset < 0) {
help = true;
}
if(help) {
// Show help
printf("%s\n", argv[0]);
printf("\tinput: file where the eeprom is read from\n");
printf("\toutput: file where the eeprom is stored\n");
printf("\toffset: offset of the eeprom (>0)\n");
printf("\t<size>: size of the eeprom [%d]\n", ATHEROS_EEPROM_SIZE);
}
else {
uint8_t bytes[eeprom_size];
int fdin, fdout, bytes_read, bytes_written;
// Open input file
fdin = open(file_input, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
printf("Input: %s\n", file_input);
printf("Offset: 0x%x (%d)\n", eeprom_offset, eeprom_offset);
printf("Size: 0x%x (%d)\n", eeprom_size, eeprom_size);
// Read eeprom
bytes_read = athlib_bytes_read(fdin, (uint8_t*) bytes, eeprom_offset, eeprom_size);
if(bytes_read == eeprom_size) {
// Open output file
fdout = open(file_output, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
printf("Output: %s\n", file_output);
// Write eeprom
bytes_written = athlib_bytes_write(fdout, (uint8_t*) bytes, 0, eeprom_size, true);
if(bytes_written == eeprom_size) {
printf("Result: success.\n", file_output);
}
else {
// Error while writing eeprom
printf("Result: write error.\n", file_output);
}
}
else {
// Error while reading eeprom
printf("Result: read error.\n", file_output);
}
// Close files
close(fdin);
close(fdout);
}
exit(EXIT_SUCCESS);
}