-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.c
380 lines (317 loc) · 7.84 KB
/
init.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/syscall.h>
struct linux_dirent {
unsigned long d_ino;
unsigned long d_off;
unsigned short d_reclen;
char d_name[];
};
#define ROOT_DEVICE "cardblksd2"
#define ROOT_FS_TYPE "ext3"
#define INIT_NAME "/sbin/init"
#define MINORBITS 20
#define MINORMASK ((1U << MINORBITS) - 1)
#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
#define _SYSCALL_WR_NUM(x) # x
#define _SYSCALL_TEXTNUM(x) _SYSCALL_WR_NUM(x)
#define __SYSCALL(x, pref, suff) asm volatile( pref "mov r7, #" _SYSCALL_WR_NUM(x) "\nsvc 0\n" suff "mov pc, lr\n" )
#define _SYSCALL(x) __SYSCALL(x, "", "")
#define __syscall __attribute__((naked))
#define __init __attribute__((naked))
void print(const char *t);
void prepare(void);
void getprocname(const char *id, char *name, int len);
int isdecnum(const char *t);
int dectoint(const char *t);
void strappend(char *dst, const char *src);
int _strcmp(const char *a, const char *b);
__syscall int s_write(int fd, const void *buf, int l);
__syscall int s_read(int fd, void *buf, int l);
__syscall int s_execve(const char *path, char *const argv[], char *const envp[]);
__syscall int s_mount(const char *dev, const char *path, const char *filesystem, int flags, const void *data);
__syscall int s_umount2(const char *path, int flags);
__syscall int s_getdents(int fd, struct linux_dirent *d, int count);
__syscall int s_setpriority(int which, int who, int prior);
__syscall int s_open(const char *path, int flags);
__syscall int s_close(int fd);
__syscall int s_chroot(const char *path);
__syscall int s_mknod(const char *path, int mode, int dev);
__syscall int s_pause(void);
int s_usleep(int usec);
__syscall int s_nanosleep(struct timespec *req, struct timespec *rem);
__init void _start(void);
int _start_prog(int argc, char **arg);
int main(int argc, char **argv, char **envp) {
char name[] = INIT_NAME;
prepare();
if(argc>0) argv[0] = name;
s_execve(INIT_NAME, argv, envp);
return 0;
}
void prepare(void) {
char name[256];
char dirents[16384];
struct linux_dirent *d;
int pid;
int fd;
int maj;
int min;
int i;
int n;
print("* Mounting /proc\n");
i = s_mount("proc", "/proc", "proc", MS_NOATIME, NULL);
if(i<0) {
print("Warning: Unable to mount /proc\n");
}
print("* Mounting /sys\n");
i = s_mount("sysfs", "/sys", "sysfs", MS_NOATIME, NULL);
if(i<0) {
print("Warning: Unable to mount /sys\n");
}
print("* Mounting tmpfs on /dev\n");
i = s_mount("tmpfs", "/dev", "tmpfs", 0, NULL);
if(i<0) {
print("Warning: Unable to mount /dev\n");
}
fd = s_open("/proc", O_RDONLY | O_DIRECTORY | O_NONBLOCK);
if(fd<0) {
print("Warning: Unable to open /proc\n");
}
d = (struct linux_dirent*) &dirents[0];
n = s_getdents(fd, d, sizeof(dirents));
if(fd>=0) s_close(fd);
pid = 0;
for(i=0; i<n; i+=d->d_reclen) {
char d_type;
d = (struct linux_dirent*) &dirents[i];
d_type = *(((char*) d) + d->d_reclen - 1);
if(d_type==DT_DIR || d_type==DT_UNKNOWN) {
if(isdecnum(d->d_name)) {
getprocname(d->d_name, name, sizeof(name));
if(name[0]==0) continue;
if(_strcmp(name, "adc_keypad")==0) {
pid = dectoint(d->d_name);
break;
}
}
}
}
if(pid) {
print("* Applying workaround for adc_keypad (set nice to 20)\n");
n = s_setpriority(PRIO_PROCESS, pid, 20);
if(n<0) {
print("Warning: Unable to change the priority\n");
}
}
print("* Waiting for root device\n");
name[0] = 0;
strappend(name, "/sys/class/block/");
strappend(name, ROOT_DEVICE);
strappend(name, "/dev");
fd = -1;
n = -1;
for(i=0; i<100; i++) {
fd = s_open(name, O_RDONLY);
if(fd>0) break;
s_usleep(100000);
}
if(fd>=0) {
n = s_read(fd, name, sizeof(name));
s_close(fd);
}
maj = -1;
if(n>0) {
name[n] = 0;
for(i=0; name[i]; i++) {
if(name[i]=='\n') {
name[i] = 0;
break;
}
}
for(i=0; name[i]; i++) {
if(name[i]==':') break;
}
if(name[i]) {
name[i] = 0;
maj = dectoint(name);
min = dectoint(&name[i+1]);
}
}
if(maj>0) {
print("* Creating root device in /dev\n");
name[0] = 0;
strappend(name, "/dev/");
strappend(name, ROOT_DEVICE);
i = s_mknod(name, S_IFBLK | 0660, maj<<8 | min);
if(i<0) {
print("Error: Unable to create root device node\n");
s_pause();
}
} else {
print("Error: Unable to find root device\n");
s_pause();
}
print("* Mounting root filesystem on /real_root\n");
i = s_mount(name, "/real_root", ROOT_FS_TYPE, MS_RDONLY, NULL);
if(i<0) {
print("Error: Unable to mount root filesystem\n");
s_pause();
}
print("* Unmounting filesystems\n");
s_umount2("/sys", 0);
s_umount2("/proc", 0);
s_umount2("/dev", 0);
print("* Changing root directory to /real_root\n");
i = s_chroot("/real_root");
if(i<0) {
print("Error: Unable to change root directory\n");
s_pause();
}
print("* Executing init\n");
}
int isdecnum(const char *t) {
if(*t==0) return 0;
while(*t) {
if(*t<'0' || *t>'9') return 0;
t++;
}
return 1;
}
int dectoint(const char *t) {
int r;
r = 0;
while(*t) {
if((*t)<'0' || (*t)>'9') return -1;
r *= 10;
r += (*t) - '0';
t++;
}
return r;
}
void getprocname(const char *id, char *name, int len) {
char fname[256];
char buf[256];
char *pname;
int n;
int fd;
name[0] = 0;
fname[0] = 0;
strappend(fname, "/proc/");
strappend(fname, id);
strappend(fname, "/stat");
fd = s_open(fname, O_RDONLY);
if(fd<0) return;
n = s_read(fd, buf, sizeof(buf));
s_close(fd);
if(n<=0) return;
buf[n] = 0;
for(pname=buf; *pname; pname++) {
if(*pname==' ') {
pname++;
break;
}
}
if(pname[0]==0) return;
if(pname[0]=='(') pname++;
for(n=0; pname[n]; n++) {
if(n>=len) break;
if(pname[n]==' ') break;
name[n] = pname[n];
}
if(n) {
if(name[n-1]==')') n--;
}
name[n] = 0;
}
void strappend(char *dst, const char *src) {
while(*dst) dst++;
while(*src) {
*dst = *src;
dst++;
src++;
}
*dst = 0;
}
int _strcmp(const char *a, const char *b) {
while(*a) {
if(*a!=*b) return 1;
a++;
b++;
}
return 0;
}
void print(const char *t) {
int n;
n = 0;
while(t[n]) n++;
s_write(1, t, n);
}
int s_write(int fd, const void *buf, int l) {
_SYSCALL(SYS_write);
}
int s_read(int fd, void *buf, int l) {
_SYSCALL(SYS_read);
}
int s_execve(const char *path, char *const argv[], char *const envp[]) {
_SYSCALL(SYS_execve);
}
int s_mount(const char *dev, const char *path, const char *filesystem, int flags, const void *data) {
__SYSCALL(SYS_mount, "push {r4}\nldr r4, [sp, #4]\n", "pop {r4}\n");
}
int s_getdents(int fd, struct linux_dirent *d, int count) {
_SYSCALL(SYS_getdents);
}
int s_setpriority(int which, int who, int prior) {
_SYSCALL(SYS_setpriority);
}
int s_open(const char *path, int flags) {
_SYSCALL(SYS_open);
}
int s_close(int fd) {
_SYSCALL(SYS_close);
}
int s_chroot(const char *path) {
_SYSCALL(SYS_chroot);
}
int s_mknod(const char *path, int mode, int dev) {
_SYSCALL(SYS_mknod);
}
int s_pause(void) {
_SYSCALL(SYS_pause);
}
int s_umount2(const char *path, int flags) {
_SYSCALL(SYS_umount2);
}
int s_usleep(int usec) {
struct timespec t;
t.tv_sec = 0;
t.tv_nsec = usec*1000;
return s_nanosleep(&t, NULL);
}
int s_nanosleep(struct timespec *req, struct timespec *rem) {
_SYSCALL(SYS_nanosleep);
}
void _start(void) {
asm volatile ( "mov fp, #0\n"
"ldr r0, [sp, #0]\n"
"add r1, sp, #4\n"
"mov lr, pc\n"
"b _start_prog\n"
"mov r7, #" _SYSCALL_TEXTNUM(SYS_exit) "\n"
"svc 0\n");
}
int _start_prog(int argc, char **arg) {
char **argv;
char **envp;
argv = arg;
envp = &arg[argc+1];
return main(argc, argv, envp);
}