1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "first_stage_console.h"
18 
19 #include <stdio.h>
20 #include <sys/stat.h>
21 #include <sys/sysmacros.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <termios.h>
25 
26 #include <string>
27 #include <thread>
28 
29 #include <android-base/chrono_utils.h>
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32 
KernelConsolePresent(const std::string & cmdline)33 static bool KernelConsolePresent(const std::string& cmdline) {
34     size_t pos = 0;
35     while (true) {
36         pos = cmdline.find("console=", pos);
37         if (pos == std::string::npos) return false;
38         if (pos == 0 || cmdline[pos - 1] == ' ') return true;
39         pos++;
40     }
41 }
42 
SetupConsole()43 static bool SetupConsole() {
44     if (mknod("/dev/console", S_IFCHR | 0600, makedev(5, 1))) {
45         PLOG(ERROR) << "unable to create /dev/console";
46         return false;
47     }
48     int fd = -1;
49     int tries = 50;  // should timeout after 5s
50     // The device driver for console may not be ready yet so retry for a while in case of failure.
51     while (tries--) {
52         fd = open("/dev/console", O_RDWR);
53         if (fd != -1) break;
54         std::this_thread::sleep_for(100ms);
55     }
56     if (fd == -1) {
57         PLOG(ERROR) << "could not open /dev/console";
58         return false;
59     }
60     ioctl(fd, TIOCSCTTY, 0);
61     dup2(fd, STDIN_FILENO);
62     dup2(fd, STDOUT_FILENO);
63     dup2(fd, STDERR_FILENO);
64     close(fd);
65     return true;
66 }
67 
RunScript()68 static void RunScript() {
69     LOG(INFO) << "Attempting to run /first_stage.sh...";
70     pid_t pid = fork();
71     if (pid != 0) {
72         int status;
73         waitpid(pid, &status, 0);
74         LOG(INFO) << "/first_stage.sh exited with status " << status;
75         return;
76     }
77     const char* path = "/system/bin/sh";
78     const char* args[] = {path, "/first_stage.sh", nullptr};
79     int rv = execv(path, const_cast<char**>(args));
80     LOG(ERROR) << "unable to execv /first_stage.sh, returned " << rv << " errno " << errno;
81 }
82 
83 namespace android {
84 namespace init {
85 
StartConsole(const std::string & cmdline)86 void StartConsole(const std::string& cmdline) {
87     bool console = KernelConsolePresent(cmdline);
88 
89     pid_t pid = fork();
90     if (pid != 0) {
91         int status;
92         waitpid(pid, &status, 0);
93         LOG(ERROR) << "console shell exited with status " << status;
94         return;
95     }
96 
97     if (console) console = SetupConsole();
98     RunScript();
99     if (console) {
100         const char* path = "/system/bin/sh";
101         const char* args[] = {path, nullptr};
102         int rv = execv(path, const_cast<char**>(args));
103         LOG(ERROR) << "unable to execv, returned " << rv << " errno " << errno;
104     }
105     _exit(127);
106 }
107 
FirstStageConsole(const std::string & cmdline,const std::string & bootconfig)108 int FirstStageConsole(const std::string& cmdline, const std::string& bootconfig) {
109     auto pos = bootconfig.find("androidboot.first_stage_console =");
110     if (pos != std::string::npos) {
111         int val = 0;
112         if (sscanf(bootconfig.c_str() + pos, "androidboot.first_stage_console = \"%d\"", &val) !=
113             1) {
114             return FirstStageConsoleParam::DISABLED;
115         }
116         if (val <= FirstStageConsoleParam::MAX_PARAM_VALUE && val >= 0) {
117             return val;
118         }
119     }
120 
121     pos = cmdline.find("androidboot.first_stage_console=");
122     if (pos != std::string::npos) {
123         int val = 0;
124         if (sscanf(cmdline.c_str() + pos, "androidboot.first_stage_console=%d", &val) != 1) {
125             return FirstStageConsoleParam::DISABLED;
126         }
127         if (val <= FirstStageConsoleParam::MAX_PARAM_VALUE && val >= 0) {
128             return val;
129         }
130     }
131     return FirstStageConsoleParam::DISABLED;
132 }
133 
134 }  // namespace init
135 }  // namespace android
136