-
Notifications
You must be signed in to change notification settings - Fork 0
/
getdtb.c
123 lines (105 loc) · 2.38 KB
/
getdtb.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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#define BUF_SIZE 1024
#define BL_NAME_LEN (sizeof("I9300") - 1)
#define NUM_DTBS 2
struct device {
// first 5 chars of androidboot.bootlodaer
const char *bl_name;
// list of DTBs to try, in order
const char *dtbs[NUM_DTBS];
};
struct device devices[] = {
{
.bl_name = "I9300",
.dtbs = {"m0", "galaxy-s3-common"},
}, {
.bl_name = "I9305",
.dtbs = {"m3", "galaxy-s3-common"},
}, {
.bl_name = "N7100",
.dtbs = {"t0-3g", "t0"},
}, {
.bl_name = "N7105",
.dtbs = {"t0-lte", "t0"},
}, {
// sentinel
.bl_name = NULL,
},
};
int main(int argc, char* argv[]) {
char buf[BUF_SIZE];
char *bl_name;
char *chosen_dtb;
int dtbfd;
struct device *device;
ssize_t ret;
if (argc < 2) {
fprintf(stderr, "usage: %s <dtb-base>\n", argv[0]);
return 1;
}
int fd = open("/proc/cmdline", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Failed to to read cmdline: %s\n", strerror(errno));
return 1;
}
while ((ret = read(fd, buf, BUF_SIZE)) > 0) {
if (bl_name = strstr(buf, "androidboot.bootloader=")) {
bl_name = strstr(bl_name, "=") + 1;
bl_name[BL_NAME_LEN] = 0; // hack
break;
}
}
if (ret <= 0) {
fprintf(stderr, "Failed to find androidboot.bootloader parameter: %s\n", (ret == 0 ? "EOF" : strerror(errno)));
goto err_find;
}
for (int i = 0; devices[i].bl_name != NULL; i++) {
device = &devices[i];
fprintf(stderr, "strcmp(%s, %s) = ", bl_name, device->bl_name);
if (strncmp(bl_name, device->bl_name, BL_NAME_LEN) != 0) {
device = NULL;
fprintf(stderr, "no match\n");
} else {
fprintf(stderr, "match\n");
break;
}
}
if (device == NULL) {
fprintf(stderr, "Couldn't find DTB matching device '%s'\n", bl_name);
goto err_find;
}
for (int i = 0; i < NUM_DTBS; i++) {
if (device->dtbs[i] == NULL)
break;
memset(buf, 0, BUF_SIZE);
strcpy(buf, argv[1]);
strcat(buf, "exynos4412-");
strcat(buf, device->dtbs[i]);
strcat(buf, ".dtb");
dtbfd = open(buf, O_RDONLY);
if (dtbfd < 0) {
fprintf(stderr, "Failed to open dtb '%s': %s\n", buf, strerror(errno));
continue;
}
close(dtbfd);
dtbfd = 0;
break;
}
if (dtbfd < 0) {
fprintf(stderr, "no dtb found\n");
return 1;
}
printf("%s\n", buf);
return 0;
err_find:
close(fd);
return 1;
}