-
Notifications
You must be signed in to change notification settings - Fork 26
/
init_main.cpp
214 lines (186 loc) · 6 KB
/
init_main.cpp
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
/*
* Copyright (C) 2016 The CyanogenMod Project
*
* 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 <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include "init_board.h"
#include "init_prototypes.h"
// Main: executable
int main(int argc, char** argv)
{
// Execution variables
unsigned char i;
bool testBoard;
bool chargerBoot;
bool multiRomBoot;
bool recoveryBoot;
int keycheckStatus;
char buffer_event[20];
init_board_device init_board;
// Parameters input
testBoard = false;
for (i = 1; i < argc; ++i)
{
// Detect board test
if (strcmp(argv[i], "--board-test") == 0) {
testBoard = true;
}
}
// Generate boot log
unlink(BOOT_TXT);
write_string(BOOT_TXT, "SONY INIT");
write_date(BOOT_TXT, true);
// Delete /init symlink
unlink("/init");
// Create directories
mkdir("/dev/block", 0755);
mkdir("/dev/input", 0755);
mkdir("/proc", 0555);
mkdir("/sys", 0755);
// Create device nodes
mknod(DEV_BLOCK_PATH, S_IFBLK | 0600,
makedev(DEV_BLOCK_MAJOR, DEV_BLOCK_MINOR));
for (i = 0; i <= 12; ++i)
{
snprintf(buffer_event, sizeof(buffer_event), "/dev/input/event%u", i);
mknod(buffer_event, S_IFCHR | 0600, makedev(13, 64 + i));
}
mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3));
// Mount filesystems
mount("proc", "/proc", "proc", 0, NULL);
mount("sysfs", "/sys", "sysfs", 0, NULL);
// Additional board inits
init_board.start_init();
// Warmboots detection
chargerBoot = CHARGER_BYPASS &&
file_contains(WARMBOOT_CMDLINE, WARMBOOT_CHARGER);
multiRomBoot = file_contains(WARMBOOT_CMDLINE, WARMBOOT_MULTIROM);
recoveryBoot = file_contains(WARMBOOT_CMDLINE, WARMBOOT_RECOVERY);
// Keycheck introduction
if (!recoveryBoot && !multiRomBoot && !chargerBoot)
{
#if KEYCHECK_ENABLED
// Listen for volume keys
const char* argv_keycheck[] = {
EXEC_KEYCHECK,
#if KEYCHECK_NO_DOWN
"--no-down",
#endif
nullptr
};
pid_t keycheck_pid = system_exec_bg(argv_keycheck);
// Board keycheck introduction
init_board.introduce_keycheck();
// Retrieve keycheck result
keycheckStatus = system_exec_kill(keycheck_pid, KEYCHECK_TIMEOUT);
recoveryBoot = (keycheckStatus == KEYCHECK_RECOVERY_BOOT_ONLY ||
keycheckStatus == KEYCHECK_RECOVERY_FOTA_BOOT);
// Board keycheck introduction
init_board.finish_keycheck(recoveryBoot);
#endif
}
else if (multiRomBoot || chargerBoot)
{
// Direct boot to Android
recoveryBoot = false;
}
else
{
// Direct boot to FOTA / Boot Recovery
keycheckStatus = KEYCHECK_RECOVERY_FOTA_BOOT;
}
// Boot to Recovery
if (recoveryBoot)
{
// Unmount /sbin to fix recovery boot with Magisk 19+
umount("/sbin");
// Recovery boot
write_string(BOOT_TXT, "RECOVERY BOOT", true);
init_board.introduce_recovery();
// Clean ramdisk files before extraction on boot
if (!testBoard)
{
ramdisk_clean_files();
}
// FOTA Recovery importation
if (!testBoard && DEV_BLOCK_FOTA_MINOR != -1 &&
(!file_exists(SBIN_CPIO_RECOVERY) || \
keycheckStatus != KEYCHECK_RECOVERY_BOOT_ONLY))
{
write_string(BOOT_TXT, "RECOVERY FOTA " DEV_BLOCK_FOTA_PATH, true);
mknod(DEV_BLOCK_FOTA_PATH, S_IFBLK | 0600,
makedev(DEV_BLOCK_FOTA_MAJOR, DEV_BLOCK_FOTA_MINOR));
mount("/", "/", NULL, MS_MGC_VAL | MS_REMOUNT, "");
const char* argv_dd_img[] = { EXEC_TOYBOX, "dd", "if=" DEV_BLOCK_FOTA_PATH,
"of=" SBIN_IMG_RECOVERY, nullptr };
system_exec(argv_dd_img);
const char* argv_extract_elf[] = { "", "-i", SBIN_IMG_RECOVERY,
"-o", SBIN_CPIO_RECOVERY, "-t", "/",
FOTA_RAMDISK_CHECK ? "-c" : "-z" };
extract_ramdisk(sizeof(argv_extract_elf) / sizeof(const char*),
argv_extract_elf);
unlink(SBIN_IMG_RECOVERY);
}
// Recovery ramdisk
if (!testBoard && file_exists(SBIN_CPIO_RECOVERY))
{
const char* argv_ramdiskcpio[] = { EXEC_TOYBOX, "cpio", "-i", "-F",
SBIN_CPIO_RECOVERY, nullptr };
system_exec(argv_ramdiskcpio);
}
}
// Boot to Android
else
{
// Android boot
write_string(BOOT_TXT, "ANDROID BOOT", true);
init_board.introduce_android();
}
// Rename init ready for boot
if (file_exists("/init.real") && !testBoard)
{
unlink("/init");
rename("/init.real", "/init");
}
// Finish init outputs
init_board.finish_init();
write_date(BOOT_TXT, true);
// Delete init binaries
if (!testBoard)
{
unlink(EXEC_KEYCHECK);
unlink(EXEC_TOYBOX);
unlink(SBIN_CPIO_RECOVERY);
unlink(SBIN_INIT_SONY);
}
// Unmount filesystems
umount("/proc");
umount("/sys");
// Init normally without parameters
if (!testBoard)
{
// Unlink /dev/*
dir_unlink_r("/dev", false);
// Launch ramdisk /init in the current process
const char* argv_init[] = { "/init", nullptr };
system_exec_inline(argv_init);
}
return 0;
}