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