Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 <stdlib.h> |
| 18 | #include <stdio.h> |
Elliott Hughes | cd3c55a | 2015-01-29 20:50:08 -0800 | [diff] [blame] | 19 | #include <string.h> |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | |
Dan Albert | 432584f | 2015-02-24 22:48:25 -0800 | [diff] [blame] | 22 | #include "sysdeps.h" |
| 23 | |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 24 | #include "adb.h" |
Dan Albert | 432584f | 2015-02-24 22:48:25 -0800 | [diff] [blame] | 25 | #include "adb_io.h" |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 26 | #include "fuse_adb_provider.h" |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 27 | #include "fuse_sideload.h" |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 28 | |
Dan Albert | ffd6c31 | 2015-02-26 15:33:00 -0800 | [diff] [blame] | 29 | int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, |
| 30 | uint32_t fetch_size) { |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 31 | struct adb_data* ad = (struct adb_data*)cookie; |
| 32 | |
| 33 | char buf[10]; |
| 34 | snprintf(buf, sizeof(buf), "%08u", block); |
Dan Albert | 432584f | 2015-02-24 22:48:25 -0800 | [diff] [blame] | 35 | if (!WriteStringFully(ad->sfd, buf)) { |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 36 | fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); |
| 37 | return -EIO; |
| 38 | } |
| 39 | |
Dan Albert | 1909771 | 2015-02-26 13:50:01 -0800 | [diff] [blame] | 40 | if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) { |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 41 | fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); |
| 42 | return -EIO; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static void close_adb(void* cookie) { |
| 49 | struct adb_data* ad = (struct adb_data*)cookie; |
| 50 | |
Dan Albert | 432584f | 2015-02-24 22:48:25 -0800 | [diff] [blame] | 51 | WriteStringFully(ad->sfd, "DONEDONE"); |
Doug Zongker | 18a78e0 | 2014-07-10 07:31:46 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { |
| 55 | struct adb_data ad; |
| 56 | struct provider_vtab vtab; |
| 57 | |
| 58 | ad.sfd = sfd; |
| 59 | ad.file_size = file_size; |
| 60 | ad.block_size = block_size; |
| 61 | |
| 62 | vtab.read_block = read_block_adb; |
| 63 | vtab.close = close_adb; |
| 64 | |
| 65 | return run_fuse_sideload(&vtab, &ad, file_size, block_size); |
| 66 | } |