-
Notifications
You must be signed in to change notification settings - Fork 271
/
main.c
114 lines (94 loc) · 2.97 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
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <cutils/android_reboot.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mount.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "multirom.h"
#include "lib/framebuffer.h"
#include "lib/log.h"
#include "version.h"
#include "lib/util.h"
#include "lib/mrom_data.h"
#define EXEC_MASK (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#define KEEP_REALDATA "/dev/.keep_realdata"
#define REALDATA "/realdata"
static void do_kexec(void)
{
emergency_remount_ro();
execl("/kexec", "/kexec", "-e", NULL);
ERROR("kexec -e failed! (%d: %s)", errno, strerror(errno));
while(1);
}
int main(int argc, const char *argv[])
{
int i;
const char *rom_to_boot = NULL;
for(i = 1; i < argc; ++i)
{
if(strcmp(argv[i], "-v") == 0)
{
printf("%d%s\n", VERSION_MULTIROM, VERSION_DEV_FIX);
fflush(stdout);
return 0;
}
else if(strncmp(argv[i], "--boot-rom=", sizeof("--boot-rom")) == 0)
{
rom_to_boot = argv[i] + sizeof("--boot-rom");
}
}
srand(time(0));
klog_init();
// output all messages to dmesg,
// but it is possible to filter out INFO messages
klog_set_level(6);
mrom_set_log_tag("multirom");
ERROR("Running MultiROM v%d%s\n", VERSION_MULTIROM, VERSION_DEV_FIX);
// root is mounted read only in android and MultiROM uses
// it to store some temp files, so remount it.
// Yes, there is better solution to this.
if(rom_to_boot)
mount(NULL, "/", NULL, MS_REMOUNT, NULL);
int exit = multirom(rom_to_boot);
if(rom_to_boot)
mount(NULL, "/", NULL, MS_RDONLY | MS_REMOUNT, NULL);
if(exit >= 0)
{
if(exit & EXIT_REBOOT_RECOVERY)
do_reboot(REBOOT_RECOVERY);
else if(exit & EXIT_REBOOT_BOOTLOADER)
do_reboot(REBOOT_BOOTLOADER);
else if(exit & EXIT_SHUTDOWN)
do_reboot(REBOOT_SHUTDOWN);
else if(exit & EXIT_REBOOT)
do_reboot(REBOOT_SYSTEM);
if(exit & EXIT_KEXEC)
{
do_kexec();
return 0;
}
// indicates trampoline to keep /realdata mounted
if(!(exit & EXIT_UMOUNT))
close(open(KEEP_REALDATA, O_WRONLY | O_CREAT, 0000));
}
return 0;
}