Skip to content

Commit 4d0df88

Browse files
author
Hongguang Chen
committed
Add EthernetDevice to manage ethernet connection.
The current fastbootd only supports USB protocol. But some Android TV devices are built without USB port. The fastbootd cannot be used on those ATV devices due to it. aosp/1295566 enables fastbootd over ethernet. This change adds an EthernetDevice to manage ethernet connection and set fastbootd protcol property fastbootd.protocol to enable fastbootd over ethernet in recovery mode. It uses IPv6 link-local address to not expose the devices out of the network segment. The devices who want to use this EthernetDevice should add “TARGET_RECOVERY_UI_LIB := librecovery_ui_ethernet” into BoardConfig.mk. BUG: 152544169 BUG: 155198345 Test: Enter and exit fastboot mode and check eth0 IPv6 link-local address on screen UI. Change-Id: I73fe44be3790bdba5a6059fbba3f7264b21eed99 Merged-In: I73fe44be3790bdba5a6059fbba3f7264b21eed99
1 parent b62ff1a commit 4d0df88

File tree

8 files changed

+244
-3
lines changed

8 files changed

+244
-3
lines changed

Android.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ LOCAL_PATH := $(call my-dir)
1818
RECOVERY_API_VERSION := 3
1919
RECOVERY_FSTAB_VERSION := 2
2020

21-
# TARGET_RECOVERY_UI_LIB should be one of librecovery_ui_{default,wear,vr} or a device-specific
22-
# module that defines make_device() and the exact RecoveryUI class for the target. It defaults to
23-
# librecovery_ui_default, which uses ScreenRecoveryUI.
21+
# TARGET_RECOVERY_UI_LIB should be one of librecovery_ui_{default,wear,vr,ethernet} or a
22+
# device-specific module that defines make_device() and the exact RecoveryUI class for the
23+
# target. It defaults to librecovery_ui_default, which uses ScreenRecoveryUI.
2424
TARGET_RECOVERY_UI_LIB ?= librecovery_ui_default
2525

2626
# librecovery_ui_ext (shared library)

fastboot/fastboot.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Device::BuiltinAction StartFastboot(Device* device, const std::vector<std::strin
5252
ui->ResetKeyInterruptStatus();
5353
ui->SetTitle(title_lines);
5454
ui->ShowText(true);
55+
device->StartFastboot();
5556

5657
// Reset to normal system boot so recovery won't cycle indefinitely.
5758
// TODO(b/112277594) Clear only if 'recovery' field of BCB is empty. If not,

recovery_main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,11 @@ int main(int argc, char** argv) {
471471
std::string usb_config =
472472
fastboot ? "fastboot" : IsRoDebuggable() || IsDeviceUnlocked() ? "adb" : "none";
473473
std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
474+
if (fastboot) {
475+
device->PreFastboot();
476+
} else {
477+
device->PreRecovery();
478+
}
474479
if (usb_config != usb_state) {
475480
if (!SetUsbConfig("none")) {
476481
LOG(ERROR) << "Failed to clear USB config";

recovery_ui/Android.bp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ cc_library {
2222

2323
srcs: [
2424
"device.cpp",
25+
"ethernet_ui.cpp",
2526
"screen_ui.cpp",
2627
"stub_ui.cpp",
2728
"ui.cpp",
@@ -90,3 +91,23 @@ cc_library_static {
9091

9192
export_include_dirs: ["include"],
9293
}
94+
95+
// The default device that uses EthernetRecoveryUI.
96+
cc_library_static {
97+
name: "librecovery_ui_ethernet",
98+
recovery_available: true,
99+
100+
defaults: [
101+
"recovery_defaults",
102+
],
103+
104+
srcs: [
105+
"ethernet_device.cpp",
106+
],
107+
108+
shared_libs: [
109+
"libbase",
110+
],
111+
112+
export_include_dirs: ["include"],
113+
}

recovery_ui/ethernet_device.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 <android-base/logging.h>
18+
#include <android-base/properties.h>
19+
#include <android-base/strings.h>
20+
#include <android-base/unique_fd.h>
21+
#include <arpa/inet.h>
22+
#include <ifaddrs.h>
23+
#include <linux/if.h>
24+
#include <string.h>
25+
#include <sys/ioctl.h>
26+
#include <sys/socket.h>
27+
#include <sys/types.h>
28+
29+
#include "recovery_ui/device.h"
30+
#include "recovery_ui/ethernet_ui.h"
31+
32+
class EthernetDevice : public Device {
33+
public:
34+
explicit EthernetDevice(EthernetRecoveryUI* ui);
35+
36+
void PreRecovery() override;
37+
void PreFastboot() override;
38+
39+
private:
40+
int SetInterfaceFlags(const unsigned set, const unsigned clr);
41+
void SetTitleIPv6LinkLocalAddress(const bool interface_up);
42+
43+
android::base::unique_fd ctl_sock_;
44+
static const std::string interface;
45+
};
46+
47+
const std::string EthernetDevice::interface = "eth0";
48+
49+
EthernetDevice::EthernetDevice(EthernetRecoveryUI* ui)
50+
: Device(ui), ctl_sock_(socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0)) {
51+
if (ctl_sock_ < 0) {
52+
PLOG(ERROR) << "Failed to open socket";
53+
}
54+
}
55+
56+
void EthernetDevice::PreRecovery() {
57+
SetInterfaceFlags(0, IFF_UP);
58+
SetTitleIPv6LinkLocalAddress(false);
59+
}
60+
61+
void EthernetDevice::PreFastboot() {
62+
android::base::SetProperty("fastbootd.protocol", "tcp");
63+
64+
if (SetInterfaceFlags(IFF_UP, 0) < 0) {
65+
LOG(ERROR) << "Failed to bring up interface";
66+
return;
67+
}
68+
69+
SetTitleIPv6LinkLocalAddress(true);
70+
}
71+
72+
int EthernetDevice::SetInterfaceFlags(const unsigned set, const unsigned clr) {
73+
struct ifreq ifr;
74+
75+
if (ctl_sock_ < 0) {
76+
return -1;
77+
}
78+
79+
memset(&ifr, 0, sizeof(struct ifreq));
80+
strncpy(ifr.ifr_name, interface.c_str(), IFNAMSIZ);
81+
ifr.ifr_name[IFNAMSIZ - 1] = 0;
82+
83+
if (ioctl(ctl_sock_, SIOCGIFFLAGS, &ifr) < 0) {
84+
PLOG(ERROR) << "Failed to get interface active flags";
85+
return -1;
86+
}
87+
ifr.ifr_flags = (ifr.ifr_flags & (~clr)) | set;
88+
89+
if (ioctl(ctl_sock_, SIOCSIFFLAGS, &ifr) < 0) {
90+
PLOG(ERROR) << "Failed to set interface active flags";
91+
return -1;
92+
}
93+
94+
return 0;
95+
}
96+
97+
void EthernetDevice::SetTitleIPv6LinkLocalAddress(const bool interface_up) {
98+
auto recovery_ui = reinterpret_cast<EthernetRecoveryUI*>(GetUI());
99+
if (!interface_up) {
100+
recovery_ui->SetIPv6LinkLocalAddress();
101+
return;
102+
}
103+
104+
struct ifaddrs* ifaddr;
105+
if (getifaddrs(&ifaddr) == -1) {
106+
PLOG(ERROR) << "Failed to get interface addresses";
107+
recovery_ui->SetIPv6LinkLocalAddress();
108+
return;
109+
}
110+
111+
std::unique_ptr<struct ifaddrs, decltype(&freeifaddrs)> guard{ ifaddr, freeifaddrs };
112+
for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) {
113+
if (ifa->ifa_addr->sa_family != AF_INET6 || interface != ifa->ifa_name) {
114+
continue;
115+
}
116+
117+
auto current_addr = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr);
118+
if (!IN6_IS_ADDR_LINKLOCAL(&(current_addr->sin6_addr))) {
119+
continue;
120+
}
121+
122+
char addrstr[INET6_ADDRSTRLEN];
123+
inet_ntop(AF_INET6, reinterpret_cast<const void*>(&current_addr->sin6_addr), addrstr,
124+
INET6_ADDRSTRLEN);
125+
LOG(INFO) << "Our IPv6 link-local address is " << addrstr;
126+
recovery_ui->SetIPv6LinkLocalAddress(addrstr);
127+
return;
128+
}
129+
130+
recovery_ui->SetIPv6LinkLocalAddress();
131+
}
132+
133+
// -----------------------------------------------------------------------------------------
134+
Device* make_device() {
135+
return new EthernetDevice(new EthernetRecoveryUI);
136+
}

recovery_ui/ethernet_ui.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 "recovery_ui/ethernet_ui.h"
18+
19+
#include <android-base/logging.h>
20+
21+
void EthernetRecoveryUI::SetTitle(const std::vector<std::string>& lines) {
22+
ScreenRecoveryUI::SetTitle(lines);
23+
24+
// Append IP address, if any
25+
if (!address_.empty()) {
26+
title_lines_.push_back("IPv6 link-local address - " + address_);
27+
}
28+
}
29+
30+
void EthernetRecoveryUI::SetIPv6LinkLocalAddress(const std::string& address) {
31+
address_ = address;
32+
}

recovery_ui/include/recovery_ui/device.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,22 @@ class Device {
7979
ui_.reset(ui);
8080
}
8181

82+
// Called before recovery mode started up, to perform whatever device-specific recovery mode
83+
// preparation as needed.
84+
virtual void PreRecovery() {}
85+
8286
// Called when recovery starts up (after the UI has been obtained and initialized and after the
8387
// arguments have been parsed, but before anything else).
8488
virtual void StartRecovery() {}
8589

90+
// Called before fastboot mode is started up, to perform whatever device-specific fastboot mode
91+
// preparation as needed.
92+
virtual void PreFastboot() {}
93+
94+
// Called when fastboot starts up (after the UI has been obtained and initialized and after the
95+
// arguments have been parsed, but before anything else).
96+
virtual void StartFastboot() {}
97+
8698
// Called from the main thread when recovery is at the main menu and waiting for input, and a key
8799
// is pressed. (Note that "at" the main menu does not necessarily mean the menu is visible;
88100
// recovery will be at the main menu with it invisible after an unsuccessful operation, such as
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#ifndef RECOVERY_ETHERNET_UI_H
18+
#define RECOVERY_ETHERNET_UI_H
19+
20+
#include "screen_ui.h"
21+
22+
class EthernetRecoveryUI : public ScreenRecoveryUI {
23+
public:
24+
EthernetRecoveryUI() {}
25+
void SetTitle(const std::vector<std::string>& lines) override;
26+
27+
// For EthernetDevice
28+
void SetIPv6LinkLocalAddress(const std::string& address = "");
29+
30+
private:
31+
std::string address_;
32+
};
33+
34+
#endif // RECOVERY_ETHERNET_UI_H

0 commit comments

Comments
 (0)