1 /*
2 * Copyright (C) 2018 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 #define TRACE_TAG SERVICES
18
19 #include "sysdeps.h"
20
21 #include <stdlib.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <unistd.h>
25
26 #include <string>
27
28 #include <android-base/logging.h>
29 #include <android-base/properties.h>
30 #include <android-base/stringprintf.h>
31 #include <bootloader_message/bootloader_message.h>
32 #include <cutils/android_reboot.h>
33
34 #include "adb_io.h"
35 #include "adb_unique_fd.h"
36
reboot_service(unique_fd fd,const std::string & arg)37 void reboot_service(unique_fd fd, const std::string& arg) {
38 std::string reboot_arg = arg;
39 sync();
40
41 if (reboot_arg.empty()) reboot_arg = "adb";
42 std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str());
43
44 if (reboot_arg == "fastboot" &&
45 android::base::GetBoolProperty("ro.boot.dynamic_partitions", false) &&
46 access("/dev/socket/recovery", F_OK) == 0) {
47 LOG(INFO) << "Recovery specific reboot fastboot";
48 /*
49 * The socket is created to allow switching between recovery and
50 * fastboot.
51 */
52 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
53 if (sock < 0) {
54 WriteFdFmt(fd, "reboot (%s) create\n", strerror(errno));
55 PLOG(ERROR) << "Creating recovery socket failed";
56 return;
57 }
58
59 sockaddr_un addr = {.sun_family = AF_UNIX};
60 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
61 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) {
62 WriteFdFmt(fd, "reboot (%s) connect\n", strerror(errno));
63 PLOG(ERROR) << "Couldn't connect to recovery socket";
64 return;
65 }
66 const char msg_switch_to_fastboot = 'f';
67 auto ret = adb_write(sock, &msg_switch_to_fastboot, sizeof(msg_switch_to_fastboot));
68 if (ret != sizeof(msg_switch_to_fastboot)) {
69 WriteFdFmt(fd, "reboot (%s) write\n", strerror(errno));
70 PLOG(ERROR) << "Couldn't write message to recovery socket to switch to fastboot";
71 return;
72 }
73 } else {
74 if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
75 WriteFdFmt(fd.get(), "reboot (%s) failed\n", reboot_string.c_str());
76 return;
77 }
78 }
79 // Don't return early. Give the reboot command time to take effect
80 // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
81 while (true) {
82 pause();
83 }
84 }
85