blob: 2386e82bbd33b834936bded65479efb03cbcb762 [file] [log] [blame]
Doug Zongker18a78e02014-07-10 07:31:46 -07001/*
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 Hughescd3c55a2015-01-29 20:50:08 -080019#include <string.h>
Doug Zongker18a78e02014-07-10 07:31:46 -070020#include <errno.h>
21
22#include "adb.h"
23#include "fuse_sideload.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080024#include "transport.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070025
26struct adb_data {
27 int sfd; // file descriptor for the adb channel
28
29 uint64_t file_size;
30 uint32_t block_size;
31};
32
33static int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
34 struct adb_data* ad = (struct adb_data*)cookie;
35
36 char buf[10];
37 snprintf(buf, sizeof(buf), "%08u", block);
38 if (writex(ad->sfd, buf, 8) < 0) {
39 fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno));
40 return -EIO;
41 }
42
43 if (readx(ad->sfd, buffer, fetch_size) < 0) {
44 fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno));
45 return -EIO;
46 }
47
48 return 0;
49}
50
51static void close_adb(void* cookie) {
52 struct adb_data* ad = (struct adb_data*)cookie;
53
54 writex(ad->sfd, "DONEDONE", 8);
55}
56
57int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) {
58 struct adb_data ad;
59 struct provider_vtab vtab;
60
61 ad.sfd = sfd;
62 ad.file_size = file_size;
63 ad.block_size = block_size;
64
65 vtab.read_block = read_block_adb;
66 vtab.close = close_adb;
67
68 return run_fuse_sideload(&vtab, &ad, file_size, block_size);
69}