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 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 17 | #include <errno.h> |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 23 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 24 | #include <functional> |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 25 | #include <memory> |
Tianjie Xu | 6af51a0 | 2017-04-19 14:00:52 -0700 | [diff] [blame] | 26 | #include <string> |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 27 | #include <string_view> |
Josh Gao | 2aa0d3a | 2017-04-13 12:45:55 -0700 | [diff] [blame] | 28 | #include <thread> |
| 29 | |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 30 | #include "adb.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 31 | #include "adb_unique_fd.h" |
Dan Albert | 8f6eb5c | 2015-02-24 22:07:18 -0800 | [diff] [blame] | 32 | #include "fdevent.h" |
| 33 | #include "fuse_adb_provider.h" |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 34 | #include "fuse_sideload.h" |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 35 | #include "services.h" |
Elliott Hughes | 24eb8a0 | 2016-06-15 15:12:17 -0700 | [diff] [blame] | 36 | #include "sysdeps.h" |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 37 | |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 38 | static void sideload_host_service(unique_fd sfd, const std::string& args) { |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 39 | int64_t file_size; |
| 40 | int block_size; |
| 41 | if ((sscanf(args.c_str(), "%" SCNd64 ":%d", &file_size, &block_size) != 2) || file_size <= 0 || |
| 42 | block_size <= 0) { |
| 43 | printf("bad sideload-host arguments: %s\n", args.c_str()); |
| 44 | exit(1); |
| 45 | } |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 46 | |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 47 | printf("sideload-host file size %" PRId64 " block size %d\n", file_size, block_size); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 48 | |
xunchang | 5e6832a | 2019-03-15 16:04:32 -0700 | [diff] [blame] | 49 | auto adb_data_reader = |
| 50 | std::make_unique<FuseAdbDataProvider>(std::move(sfd), file_size, block_size); |
| 51 | int result = run_fuse_sideload(std::move(adb_data_reader)); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 52 | |
katao | 77d6173 | 2018-09-20 20:34:05 +0800 | [diff] [blame] | 53 | printf("sideload_host finished\n"); |
| 54 | exit(result == 0 ? 0 : 1); |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 57 | unique_fd daemon_service_to_fd(std::string_view name, atransport* /* transport */) { |
| 58 | if (name.starts_with("sideload:")) { |
| 59 | // This exit status causes recovery to print a special error message saying to use a newer adb |
| 60 | // (that supports sideload-host). |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 61 | exit(3); |
Tao Bao | 5de19e2 | 2019-01-02 09:35:59 -0800 | [diff] [blame] | 62 | } else if (name.starts_with("sideload-host:")) { |
| 63 | std::string arg(name.substr(strlen("sideload-host:"))); |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 64 | return create_service_thread("sideload-host", |
| 65 | std::bind(sideload_host_service, std::placeholders::_1, arg)); |
Tao Bao | 99f0d9e | 2016-10-13 12:46:38 -0700 | [diff] [blame] | 66 | } |
Josh Gao | 038c4a1 | 2018-07-27 11:18:30 -0700 | [diff] [blame] | 67 | return unique_fd{}; |
Doug Zongker | 9270a20 | 2012-01-09 15:16:13 -0800 | [diff] [blame] | 68 | } |