-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
201 lines (186 loc) · 5.84 KB
/
main.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
#include "main.h"
int main(int argc, char *argv[])
{
setup_rootfs();
populate_devices();
mount_rootfs();
// list_files();
// show_variables(argc, argv);
reboot_device(RB_KEXEC);
return sleep(0XFFFFFF);
}
static inline void setup_rootfs()
{
mkdir("/proc", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
mkdir("/sys", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
mkdir("/dev", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
mount("proc", "/proc", "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV, "");
mount("sysfs", "/sys", "sysfs", MS_NOSUID | MS_NOEXEC | MS_NODEV, "");
mount("tmpfs", "/dev", "tmpfs", MS_STRICTATIME, "uid=0,gid=0,mode=755");
mkdir("/dev/block", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
static inline void populate_devices()
{
mknod("/dev/null", S_IFCHR | DEFFILEMODE, MKDEV(1, 3));
mknod("/dev/port", S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, MKDEV(1, 4));
mknod("/dev/zero", S_IFCHR | DEFFILEMODE, MKDEV(1, 5));
mknod("/dev/full", S_IFCHR | DEFFILEMODE, MKDEV(1, 7));
mknod("/dev/random", S_IFCHR | DEFFILEMODE, MKDEV(1, 8));
mknod("/dev/urandom", S_IFCHR | DEFFILEMODE, MKDEV(1, 9));
mknod("/dev/kmsg", S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, MKDEV(1, 11));
mknod("/dev/tty", S_IFCHR | DEFFILEMODE, MKDEV(5, 0));
mknod("/dev/console", S_IFCHR | S_IRUSR | S_IWUSR | S_IWGRP, MKDEV(5, 1));
mknod("/dev/ptmx", S_IFCHR | DEFFILEMODE, MKDEV(5, 2));
mknod("/dev/fb0", S_IFCHR | DEFFILEMODE, MKDEV(29, 0));
mknod("/dev/block/mmcblk0", S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, MKDEV(179, 1));
mknod("/dev/block/mmcblk0p31", S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, MKDEV(179, 31));
mknod("/dev/block/mmcblk0p32", S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, MKDEV(179, 32));
}
static inline void list_files(char *path)
{
DIR *directory;
struct dirent *folder;
directory = opendir(path);
if (directory)
{
while ((folder = readdir(directory)) != NULL)
{
klog(folder->d_name);
}
closedir(directory);
}
}
static inline void klog(char *message)
{
int size = strlen(message) + 17;
char buffer[size];
sprintf(buffer, "DEBUG MESSAGE: %s", message);
printf("%s\n", buffer);
int kmsg = open("/dev/kmsg", O_WRONLY);
if (write(kmsg, buffer, size) == -1)
show_error("Write Error");
close(kmsg);
}
static inline void show_error(char *target)
{
char message[20 + strlen(target) + strlen(strerror(errno))];
sprintf(message, "DEBUG MESSAGE: %s %s\n", target, strerror(errno));
printf("%s\n", message);
klog(strerror(errno));
}
static inline void print_file(char *path)
{
FILE *mounts = fopen(path, "r");
char buffer[128];
klog(path);
while (fgets(buffer, sizeof(buffer), mounts) != NULL)
{
klog(buffer);
}
}
static inline void mount_rootfs()
{
mkdir("/mnt", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (mount(rootfs, "/mnt", "ext4", MS_RELATIME | MS_STRICTATIME, "") == 0)
{
klog("Mount Successful");
print_file("/proc/mounts");
FILE *log = fopen("/mnt/log.txt", "w");
list_files("/mnt");
fprintf(log, "Some text");
fclose(log);
reboot_device(RB_KEXEC);
}
else
show_error("Mount ROOTFS");
}
static inline void show_variables(int count, char *argument[])
{
FILE *file = fopen("/proc/cmdline", "r");
char buffer[get_file_size(file)];
if (fread(buffer, sizeof *buffer, 2048, file) > 0)
{
klog("Parameters");
char *parameter = strtok(buffer, " ");
while (parameter != NULL && parameter != "")
{
char buffer[strlen(parameter)];
sprintf(buffer, "%s", parameter);
// set_reason(buffer);
parameter = strtok(NULL, " ");
}
}
fclose(file);
klog("Init Arguments");
for (int i = 0; i < count; i++)
{
// klog(argument[i]);
}
klog("Environment Variables");
for (int i = 0; environ[i] != NULL; i++)
{
// klog(environ[i]);
}
}
static inline int get_file_size(FILE *file)
{
int count = 0;
for (char c = getc(file); getc(file) != EOF; count++)
;
rewind(file);
return count - 1;
}
static inline void reboot_device(int command)
{
if (mountpoint("/mnt"))
umount("/mnt");
sync();
switch (command)
{
case RB_POWER_OFF:
syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_POWER_OFF, NULL);
break;
case RB_KEXEC:
syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, "bootloader");
break;
default:
syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, "PMIC_cold_reboot");
break;
}
}
static inline void set_reason(char *parameter)
{
if ((strstr(parameter, "androidboot.bootreason")) != NULL)
{
char *pointer;
char *token = strtok(parameter, "=");
while (token != NULL)
{
if (strcmp(token, "reboot") == 0)
reboot_device(RB_AUTOBOOT);
else if (strcmp(token, "PMIC_cold_reboot") == 0)
bootloader = true;
else if (strcmp(token, "reboot_longkey") == 0)
reboot_device(RB_AUTOBOOT);
else if (strcmp(token, "androidboot.bootreason") != 0)
{
klog(token);
reboot_device(RB_AUTOBOOT);
}
token = strtok(NULL, " ");
}
}
}
static bool mountpoint(char *path)
{
struct stat parent;
struct stat child;
char buffer[strlen(path) + 4];
sprintf(buffer, "%s/..", path);
stat(buffer, &parent);
stat(path, &child);
return parent.st_dev == child.st_dev;
}