blob: d71807dfbb9639838548df708c41ff289a80ccc2 [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
Dan Albert432584f2015-02-24 22:48:25 -080022#include "sysdeps.h"
23
Doug Zongker18a78e02014-07-10 07:31:46 -070024#include "adb.h"
Dan Albert432584f2015-02-24 22:48:25 -080025#include "adb_io.h"
Dan Albertffd6c312015-02-26 15:33:00 -080026#include "fuse_adb_provider.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070027#include "fuse_sideload.h"
Doug Zongker18a78e02014-07-10 07:31:46 -070028
Elliott Hughes15931922015-05-01 17:07:44 -070029int read_block_adb(void* data, uint32_t block, uint8_t* buffer, uint32_t fetch_size) {
30 adb_data* ad = reinterpret_cast<adb_data*>(data);
Doug Zongker18a78e02014-07-10 07:31:46 -070031
Elliott Hughes15931922015-05-01 17:07:44 -070032 if (!WriteFdFmt(ad->sfd, "%08u", block)) {
Doug Zongker18a78e02014-07-10 07:31:46 -070033 fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno));
34 return -EIO;
35 }
36
Dan Albert19097712015-02-26 13:50:01 -080037 if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) {
Doug Zongker18a78e02014-07-10 07:31:46 -070038 fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno));
39 return -EIO;
40 }
41
42 return 0;
43}
44
Elliott Hughes15931922015-05-01 17:07:44 -070045static void close_adb(void* data) {
46 adb_data* ad = reinterpret_cast<adb_data*>(data);
47 WriteFdExactly(ad->sfd, "DONEDONE");
Doug Zongker18a78e02014-07-10 07:31:46 -070048}
49
50int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) {
Elliott Hughes15931922015-05-01 17:07:44 -070051 adb_data ad;
Doug Zongker18a78e02014-07-10 07:31:46 -070052 ad.sfd = sfd;
53 ad.file_size = file_size;
54 ad.block_size = block_size;
55
Elliott Hughes15931922015-05-01 17:07:44 -070056 provider_vtab vtab;
Doug Zongker18a78e02014-07-10 07:31:46 -070057 vtab.read_block = read_block_adb;
58 vtab.close = close_adb;
59
60 return run_fuse_sideload(&vtab, &ad, file_size, block_size);
61}