-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathfirmware.c
More file actions
125 lines (108 loc) · 2.8 KB
/
firmware.c
File metadata and controls
125 lines (108 loc) · 2.8 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
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <linux/limits.h>
#include <sys/types.h>
#include <unistd.h>
#include "cjson/cJSON.h"
#include "cjson/cYAML.h"
#include "firmware.h"
#include "hal/common.h"
#include "tools.h"
#include "uboot.h"
static void get_god_app(cJSON *j_inner) {
char sname[1024];
pid_t godpid;
if ((godpid = get_god_pid(NULL, 0)) > 0) {
if (get_pid_cmdline(godpid, sname))
ADD_PARAM("main-app", sname);
}
}
static void get_kernel_version(cJSON *j_inner) {
FILE *fp = fopen("/proc/version", "r");
if (!fp)
return;
char line[1020];
if (!fgets(line, sizeof(line), fp))
return;
fclose(fp);
const char *build = "";
char *pound = strchr(line, '#');
if (pound) {
char *buildstr = strchr(pound, ' ');
if (buildstr) {
char *end = strchr(buildstr, '\n');
if (end)
*end = 0;
build = buildstr + 1;
}
}
char *ptr, *toolchain = NULL;
int pars = 0, group = 0;
for (ptr = line; *ptr; ptr++) {
if (*ptr == '(') {
pars++;
if (group == 1 && pars == 1) {
toolchain = ptr + 1;
}
} else if (*ptr == ')') {
pars--;
if (pars == 0) {
group++;
if (group == 2) {
*ptr = 0;
break;
}
}
}
}
char *version = line;
int spaces = 0;
for (ptr = line; *ptr; ptr++) {
if (*ptr == ' ') {
spaces++;
if (spaces == 2) {
version = ptr + 1;
} else if (spaces == 3) {
*ptr = 0;
break;
}
}
}
ADD_PARAM_FMT("kernel", "%s (%s)", version, build);
if (toolchain)
ADD_PARAM("toolchain", toolchain);
}
static void get_libc(cJSON *j_inner) {
char buf[PATH_MAX] = {0};
if (readlink("/lib/libc.so.0", buf, sizeof(buf)) == -1)
return;
if (!strncmp(buf, "libuClibc-", 10)) {
char *ver = buf + 10;
for (char *ptr = ver + strlen(ver) - 1; ptr != ver; ptr--) {
if (*ptr == '.') {
*ptr = 0;
break;
}
}
ADD_PARAM_FMT("libc", "uClibc %s", ver);
}
}
cJSON *detect_firmare() {
cJSON *j_inner = cJSON_CreateObject();
const char *uver = uboot_env_get_param("ver");
if (uver) {
const char *stver = strchr(uver, ' ');
if (stver && *(stver + 1)) {
ADD_PARAM("u-boot", stver + 1);
}
}
get_kernel_version(j_inner);
get_libc(j_inner);
if (hal_firmware_props)
hal_firmware_props(j_inner);
get_god_app(j_inner);
return j_inner;
}