-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirmware.c
211 lines (174 loc) · 4.51 KB
/
firmware.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
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <stdlib.h>
FILE* gLogFile = NULL;
typedef enum __tag_LogLevel {
LL_Emergency,
LL_Error,
LL_Warning,
LL_Information,
LL_Debug
} LogLevel;
#define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
#define LOG_FATAL(msgPat, ...) WRITE_LOG(LL_Emergency, msgPat, ##__VA_ARGS__)
#define LOG_ERROR(msgPat, ...) WRITE_LOG(LL_Error, msgPat, ##__VA_ARGS__)
#define LOG_WARNING(msgPat, ...) WRITE_LOG(LL_Warning, msgPat, ##__VA_ARGS__)
#define LOG_INFO(msgPat, ...) WRITE_LOG(LL_Information, msgPat, ##__VA_ARGS__)
#define LOG_DEBUG(msgPat, ...) WRITE_LOG(LL_Debug, msgPat, ##__VA_ARGS__)
#define WRITE_LOG(Cat, MsgPat, ...) do { \
char s[1024] = {0}; \
sprintf(s, "file{%s}:func{%s}:line{%d}: ", __FILE__, __FUNCTION__, __LINE__); \
sprintf(s + strlen(s), MsgPat, ##__VA_ARGS__); \
writeLog(Cat, s); \
} while (0)
#define LOG_PRINT(msg) if (gLogFile) fputs(msg, gLogFile); else puts(msg)
void writeLog(LogLevel cat, const char* msg) {
switch (cat) {
case LL_Emergency:
LOG_PRINT("EMERG: ");
break;
case LL_Error:
LOG_PRINT("ERROR: ");
break;
case LL_Warning:
LOG_PRINT("WARN: ");
break;
case LL_Information:
LOG_PRINT("INFO: ");
break;
case LL_Debug:
LOG_PRINT("DEBUG: ");
break;
default:
LOG_PRINT("UNKNOWN: ");
break;
}
LOG_PRINT(msg);
LOG_PRINT("\n");
}
static bool set_loading(char *loadpath, const char *state)
{
FILE *ldfile;
ldfile = fopen(loadpath, "we");
if (ldfile == NULL) {
LOG_ERROR("error: can not open '%s'\n", loadpath);
return false;
};
fprintf(ldfile, "%s\n", state);
fclose(ldfile);
return true;
}
static bool copy_firmware(const char *source, const char *target, size_t size)
{
char *buf = NULL;
FILE *fsource = NULL, *ftarget = NULL;
bool ret = false;
buf = malloc(size);
if (buf == NULL) {
LOG_ERROR("No memory available to load firmware file");
return false;
}
LOG_DEBUG("writing '%s' (%zi) to '%s'\n", source, size, target);
fsource = fopen(source, "re");
if (fsource == NULL) {
LOG_ERROR("Unable to open source: %s", source);
goto exit;
}
ftarget = fopen(target, "we");
if (ftarget == NULL) {
LOG_ERROR("Unable to open target: %s", target);
goto exit;
}
if (fread(buf, size, 1, fsource) != 1) {
LOG_ERROR("Fail to read from source");
goto exit;
}
if (fwrite(buf, size, 1, ftarget) == 1) {
ret = true;
}
else {
LOG_ERROR("Fail to write to target");
}
exit:
if (ftarget != NULL) {
fclose(ftarget);
}
if (fsource != NULL) {
fclose(fsource);
}
free(buf);
return ret;
}
static int builtin_firmware(const char* firmware, const char* devpath)
{
static const char *searchpath[] = { "/lib/firmware", "/lib/firmware/update" };
char loadpath[1024] = { 0 };
char datapath[1024] = { 0 };
char fwpath[1024] = { 0 };
FILE *fwfile = NULL;
struct stat statbuf;
unsigned int i = 0;
int rc = 0;
/* lookup firmware file */
for (i = 0; i < ELEMENTSOF(searchpath); i++) {
sprintf(fwpath, "%s/%s", searchpath[i], firmware);
fwfile = fopen(fwpath, "re");
if (fwfile != NULL) {
break;
}
}
sprintf(loadpath, "%s/%s", devpath, "loading");
if (fwfile == NULL) {
LOG_ERROR("did not find firmware file '%s'\n", firmware);
rc = -1;
set_loading(loadpath, "-1");
goto exit;
}
if (stat(fwpath, &statbuf) < 0 || statbuf.st_size == 0) {
LOG_ERROR("firmware file stat wrong '%s'\n", firmware);
set_loading(loadpath, "-1");
rc = -1;
goto exit;
}
if (!set_loading(loadpath, "1")) {
LOG_ERROR("fail to set loading status to 1");
rc = -1;
goto exit;
}
sprintf(datapath, "%s/%s", devpath, "data");
if (!copy_firmware(fwpath, datapath, statbuf.st_size)) {
LOG_ERROR("error sending firmware '%s' to device\n", firmware);
set_loading(loadpath, "-1");
rc = -1;
goto exit;
};
set_loading(loadpath, "0");
exit:
if (fwfile) {
fclose(fwfile);
}
return rc;
}
int main(int argc, char* argv[]) {
char* logFileName = NULL;
char* devPath = NULL;
char* firmwareName = NULL;
LOG_INFO("args: %d", argc);
if (argc < 3 || argc > 4) {
return -1;
}
else if (argc == 4) {
logFileName = argv[3];
gLogFile = fopen(logFileName, "w");
}
firmwareName = argv[1];
devPath = argv[2];
builtin_firmware(firmwareName, devPath);
if (gLogFile) {
fclose(gLogFile);
gLogFile = NULL;
}
return 0;
}