forked from projectceladon/log_capture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintel_prop.c
251 lines (208 loc) · 4.88 KB
/
intel_prop.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
/* Copyright (C) Intel 2014
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <libgen.h>
#include <cutils/properties.h>
#ifdef ENABLE_DMI
#include <libdmi.h>
#endif
#define LOG_TAG "intel_props"
#include <log/log.h>
#define DEFAULT_CFG_FILE "/system/vendor/etc/intel_prop.cfg"
#define MIN_LINE_LENGTH 3
#define FIELD_SEPARATOR ':'
#define EPOK 0
#define EPINVAL 1
#define EPNSUPP 2
#define EPNOTFOUND 3
#define EPFAIL 4
/**
* Setup prop execution function
* @param src - source of the property
* @param dst - destination property
* @return < 0 if it fails
*/
typedef int (*set_param)(const char *src, const char *dst);
struct type_setup {
const char *signature;
set_param fnc;
const char *desc;
};
static int fs_to_prop(const char *src, const char *dst);
#ifdef ENABLE_DMI
static int dmi_to_prop(const char *src, const char *dst, enum smbios_type type);
static int dmi_b_to_prop(const char *src, const char *dst);
static int dmi_s_to_prop(const char *src, const char *dst);
#endif
/**
* Type execution table
*/
struct type_setup type_tbl[] = {
{ "fs", fs_to_prop, "Copy the content of a fs node" },
#ifdef ENABLE_DMI
{ "dmi_bi", dmi_b_to_prop, "DMI BIOS_INFORMATION parameter" },
{ "dmi_is", dmi_s_to_prop, "DMI INTEL_SMBIOS parameter" },
#endif
{ NULL,NULL,NULL }, /*null ended */
};
#ifdef ENABLE_DMI
static int dmi_to_prop(const char *src, const char *dst, enum smbios_type type) {
int ret;
char *dmi_value;
dmi_value = libdmi_getfield(type, src);
if (!dmi_value) {
return -EPNOTFOUND;
}
ret = property_set(dst, dmi_value);
free(dmi_value);
if (ret)
return -EPFAIL;
return EPOK;
}
static int dmi_b_to_prop(const char *src, const char *dst) {
return dmi_to_prop(src, dst, BIOS_INFORMATION);
}
static int dmi_s_to_prop(const char *src, const char *dst) {
return dmi_to_prop(src, dst, INTEL_SMBIOS);
}
#endif
static int fs_to_prop(const char *src, const char *dst) {
char prop_val[PROP_VALUE_MAX];
FILE *f;
size_t size;
f = fopen(src, "r");
if (!f)
return -EPNOTFOUND;
size = fread(prop_val, 1, PROP_VALUE_MAX - 1, f);
if (size) {
prop_val[size - 1] = 0;
property_set(dst, prop_val);
}
fclose(f);
return EPOK;
}
static int parse(const char *line) {
char *dst, *src, *type, *tmp;
int ret;
struct type_setup *entry;
type = strdup(line);
if (!type) {
return -EPINVAL;
}
src = strchr(type, FIELD_SEPARATOR);
if (!src) {
free(type);
return -EPINVAL;
}
src[0] = 0;
src++;
dst = strchr(src, FIELD_SEPARATOR);
if (!dst) {
free(type);
return -EPINVAL;
}
dst[0] = 0;
dst++;
/*remove \n */
tmp = strchr(dst, '\n');
if (tmp)
tmp[0] = 0;
if (!strlen(dst) || !strlen(src) || !strlen(type)) {
free(type);
return -EPINVAL;
}
ret = 0;
entry = type_tbl;
while (entry->signature) {
if (strcmp(type, entry->signature) == 0) {
ret = entry->fnc(src, dst);
break;
}
entry++;
}
if (ret) {
ALOGW("Cannot set %s from %s of type %s ", dst, src,
type);
free(type);
return -EPNSUPP;
}
if (!entry->signature) {
ALOGW("Unknown type %s for %s ", type, dst);
free(type);
return -EPINVAL;
}
free(type);
return EPOK;
}
static const char *cfg_file = NULL;
static void print_types() {
struct type_setup *entry = type_tbl;
printf("Supported parameter types:\n");
while (entry->signature) {
printf("\t%s\t-> %s\n", entry->signature, entry->desc);
entry++;
}
}
static void usage(const char *app) {
char *base = basename(app);
printf("Usage: %s [-l | -f <path> ]\n", (!base) ? ((!app) ? "intel_prop" : app): base);
printf("\t-l\t-list available types\n");
printf("\t-f <path>\t-load config from <path>\n");
free(base);
}
static int get_args(int argc, char **argv) {
int option;
while ((option = getopt(argc, argv, "lf:")) != -1)
switch (option) {
case 'l':
print_types();
return -1;
break;
case 'f':
cfg_file = optarg;
break;
default:
cfg_file = NULL;
}
if (argc >= 2 && (argc - optind)) {
usage(argv[0]);
return -1;
}
return 0;
}
int main(int argc, char **argv) {
FILE *f;
char *line = NULL;
size_t len = 0;
if (get_args(argc, argv))
return -1;
if (!cfg_file)
cfg_file = DEFAULT_CFG_FILE;
f = fopen(cfg_file, "r");
if (!f)
return -1;
while (getline(&line, &len, f) > 0) {
if (line[0] == '#' || strlen(line) < MIN_LINE_LENGTH)
continue;
parse(line);
}
free(line);
fclose(f);
return 0;
}