Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 17 | #include "minadbd_services.h" |
| 18 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 19 | #include <errno.h> |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 25 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 26 | #include <functional> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 27 | #include <memory> |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 28 | #include <string> |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 29 | #include <string_view> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 30 | #include <thread> |
| 31 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 32 | #include <android-base/file.h> |
| 33 | #include <android-base/logging.h> |
| 34 | #include <android-base/memory.h> |
| 35 | #include <android-base/stringprintf.h> |
| 36 | #include <android-base/strings.h> |
| 37 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 38 | #include "adb.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 39 | #include "adb_unique_fd.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 40 | #include "fdevent.h" |
| 41 | #include "fuse_adb_provider.h" |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 42 | #include "fuse_sideload.h" |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 43 | #include "minadbd_types.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 44 | #include "services.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 45 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 46 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 47 | static int minadbd_socket = -1; |
| 48 | void SetMinadbdSocketFd(int socket_fd) { |
| 49 | minadbd_socket = socket_fd; |
| 50 | } |
| 51 | |
| 52 | static bool WriteCommandToFd(MinadbdCommands cmd, int fd) { |
| 53 | char message[kMinadbdMessageSize]; |
| 54 | memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix)); |
| 55 | android::base::put_unaligned(message + strlen(kMinadbdStatusPrefix), cmd); |
| 56 | |
| 57 | if (!android::base::WriteFully(fd, message, kMinadbdMessageSize)) { |
| 58 | PLOG(ERROR) << "Failed to write message " << message; |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | // Blocks and reads the command status from |fd|. Returns false if the received message has a |
| 65 | // format error. |
| 66 | static bool WaitForCommandStatus(int fd, MinadbdCommandStatus* status) { |
| 67 | char buffer[kMinadbdMessageSize]; |
| 68 | if (!android::base::ReadFully(fd, buffer, kMinadbdMessageSize)) { |
| 69 | PLOG(ERROR) << "Failed to response status from socket"; |
| 70 | exit(kMinadbdSocketIOError); |
| 71 | } |
| 72 | |
| 73 | std::string message(buffer, buffer + kMinadbdMessageSize); |
| 74 | if (!android::base::StartsWith(message, kMinadbdStatusPrefix)) { |
| 75 | LOG(ERROR) << "Failed to parse status in " << message; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | *status = android::base::get_unaligned<MinadbdCommandStatus>( |
| 80 | message.substr(strlen(kMinadbdStatusPrefix)).c_str()); |
| 81 | return true; |
| 82 | } |
| 83 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 84 | static void sideload_host_service(unique_fd sfd, const std::string& args) { |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 85 | int64_t file_size; |
| 86 | int block_size; |
| 87 | if ((sscanf(args.c_str(), "%" SCNd64 ":%d", &file_size, &block_size) != 2) || file_size <= 0 || |
| 88 | block_size <= 0) { |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 89 | LOG(ERROR) << "bad sideload-host arguments: " << args; |
| 90 | exit(kMinadbdPackageSizeError); |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 91 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 92 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 93 | LOG(INFO) << "sideload-host file size " << file_size << ", block size " << block_size; |
| 94 | |
| 95 | if (!WriteCommandToFd(MinadbdCommands::kInstall, minadbd_socket)) { |
| 96 | exit(kMinadbdSocketIOError); |
| 97 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 98 | |
Tao Bao | 2be9737 | 2019-04-15 12:45:50 -0700 | [diff] [blame] | 99 | auto adb_data_reader = std::make_unique<FuseAdbDataProvider>(sfd, file_size, block_size); |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 100 | if (int result = run_fuse_sideload(std::move(adb_data_reader)); result != 0) { |
| 101 | LOG(ERROR) << "Failed to start fuse"; |
| 102 | exit(kMinadbdFuseStartError); |
| 103 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 104 | |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 105 | MinadbdCommandStatus status; |
| 106 | if (!WaitForCommandStatus(minadbd_socket, &status)) { |
| 107 | exit(kMinadbdMessageFormatError); |
| 108 | } |
| 109 | LOG(INFO) << "Got command status: " << static_cast<unsigned int>(status); |
| 110 | |
| 111 | LOG(INFO) << "sideload_host finished"; |
| 112 | exit(kMinadbdSuccess); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 115 | unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { |
| 116 | if (name.starts_with("sideload:")) { |
| 117 | // This exit status causes recovery to print a special error message saying to use a newer adb |
| 118 | // (that supports sideload-host). |
xunchang | 34690ce | 2019-04-05 16:16:07 -0700 | [diff] [blame] | 119 | exit(kMinadbdAdbVersionError); |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 120 | } else if (name.starts_with("sideload-host:")) { |
| 121 | std::string arg(name.substr(strlen("sideload-host:"))); |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 122 | return create_service_thread("sideload-host", |
| 123 | std::bind(sideload_host_service, std::placeholders::_1, arg)); |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 124 | } |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 125 | return unique_fd{}; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 126 | } |