fastbootd: userspace implementation of the fastboot device-side protocol
Initial commit of fastbootd. A few commands work, but not fully functional yet. Change-Id: I589dee7b327b4460e94b4434aaf9bcf780faa839
This commit is contained in:
parent
fb3280886d
commit
a3d386ea56
11 changed files with 1388 additions and 0 deletions
38
fastbootd/Android.mk
Normal file
38
fastbootd/Android.mk
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# Copyright (C) 2013 Google Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
LOCAL_PATH:= $(call my-dir)
|
||||
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
LOCAL_SRC_FILES := \
|
||||
config.c \
|
||||
commands.c \
|
||||
fastbootd.c \
|
||||
protocol.c \
|
||||
transport.c \
|
||||
usb_linux_client.c
|
||||
|
||||
LOCAL_MODULE := fastbootd
|
||||
LOCAL_MODULE_TAGS := optional
|
||||
LOCAL_CFLAGS := -Wall -Werror -Wno-unused-parameter
|
||||
|
||||
LOCAL_STATIC_LIBRARIES := \
|
||||
libsparse_static \
|
||||
libc \
|
||||
libcutils
|
||||
|
||||
LOCAL_FORCE_STATIC_EXECUTABLE := true
|
||||
|
||||
include $(BUILD_EXECUTABLE)
|
||||
97
fastbootd/bootimg.h
Normal file
97
fastbootd/bootimg.h
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _BOOT_IMAGE_H_
|
||||
#define _BOOT_IMAGE_H_
|
||||
|
||||
typedef struct boot_img_hdr boot_img_hdr;
|
||||
|
||||
#define BOOT_MAGIC "ANDROID!"
|
||||
#define BOOT_MAGIC_SIZE 8
|
||||
#define BOOT_NAME_SIZE 16
|
||||
#define BOOT_ARGS_SIZE 512
|
||||
|
||||
struct boot_img_hdr
|
||||
{
|
||||
unsigned char magic[BOOT_MAGIC_SIZE];
|
||||
|
||||
unsigned kernel_size; /* size in bytes */
|
||||
unsigned kernel_addr; /* physical load addr */
|
||||
|
||||
unsigned ramdisk_size; /* size in bytes */
|
||||
unsigned ramdisk_addr; /* physical load addr */
|
||||
|
||||
unsigned second_size; /* size in bytes */
|
||||
unsigned second_addr; /* physical load addr */
|
||||
|
||||
unsigned tags_addr; /* physical addr for kernel tags */
|
||||
unsigned page_size; /* flash page size we assume */
|
||||
unsigned unused[2]; /* future expansion: should be 0 */
|
||||
|
||||
unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
|
||||
|
||||
unsigned char cmdline[BOOT_ARGS_SIZE];
|
||||
|
||||
unsigned id[8]; /* timestamp / checksum / sha1 / etc */
|
||||
};
|
||||
|
||||
/*
|
||||
** +-----------------+
|
||||
** | boot header | 1 page
|
||||
** +-----------------+
|
||||
** | kernel | n pages
|
||||
** +-----------------+
|
||||
** | ramdisk | m pages
|
||||
** +-----------------+
|
||||
** | second stage | o pages
|
||||
** +-----------------+
|
||||
**
|
||||
** n = (kernel_size + page_size - 1) / page_size
|
||||
** m = (ramdisk_size + page_size - 1) / page_size
|
||||
** o = (second_size + page_size - 1) / page_size
|
||||
**
|
||||
** 0. all entities are page_size aligned in flash
|
||||
** 1. kernel and ramdisk are required (size != 0)
|
||||
** 2. second is optional (second_size == 0 -> no second)
|
||||
** 3. load each element (kernel, ramdisk, second) at
|
||||
** the specified physical address (kernel_addr, etc)
|
||||
** 4. prepare tags at tag_addr. kernel_args[] is
|
||||
** appended to the kernel commandline in the tags.
|
||||
** 5. r0 = 0, r1 = MACHINE_TYPE, r2 = tags_addr
|
||||
** 6. if second_size != 0: jump to second_addr
|
||||
** else: jump to kernel_addr
|
||||
*/
|
||||
|
||||
boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
|
||||
void *ramdisk, unsigned ramdisk_size,
|
||||
void *second, unsigned second_size,
|
||||
unsigned page_size,
|
||||
unsigned *bootimg_size);
|
||||
|
||||
void bootimg_set_cmdline(boot_img_hdr *hdr, const char *cmdline);
|
||||
#endif
|
||||
206
fastbootd/commands.c
Normal file
206
fastbootd/commands.c
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
/*
|
||||
* Copyright (c) 2009-2013, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google, Inc. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "bootimg.h"
|
||||
#include "debug.h"
|
||||
#include "protocol.h"
|
||||
|
||||
static void cmd_boot(struct protocol_handle *phandle, const char *arg)
|
||||
{
|
||||
#if 0
|
||||
unsigned kernel_actual;
|
||||
unsigned ramdisk_actual;
|
||||
static struct boot_img_hdr hdr;
|
||||
char *ptr = ((char*) data);
|
||||
|
||||
if (sz < sizeof(hdr)) {
|
||||
fastboot_fail(phandle, "invalid bootimage header");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(&hdr, data, sizeof(hdr));
|
||||
|
||||
/* ensure commandline is terminated */
|
||||
hdr.cmdline[BOOT_ARGS_SIZE-1] = 0;
|
||||
|
||||
kernel_actual = ROUND_TO_PAGE(hdr.kernel_size);
|
||||
ramdisk_actual = ROUND_TO_PAGE(hdr.ramdisk_size);
|
||||
|
||||
if (2048 + kernel_actual + ramdisk_actual < sz) {
|
||||
fastboot_fail(phandle, "incomplete bootimage");
|
||||
return;
|
||||
}
|
||||
|
||||
/*memmove((void*) KERNEL_ADDR, ptr + 2048, hdr.kernel_size);
|
||||
memmove((void*) RAMDISK_ADDR, ptr + 2048 + kernel_actual, hdr.ramdisk_size);*/
|
||||
|
||||
fastboot_okay(phandle, "");
|
||||
udc_stop();
|
||||
|
||||
|
||||
/*boot_linux((void*) KERNEL_ADDR, (void*) TAGS_ADDR,
|
||||
(const char*) hdr.cmdline, LINUX_MACHTYPE,
|
||||
(void*) RAMDISK_ADDR, hdr.ramdisk_size);*/
|
||||
#endif
|
||||
}
|
||||
|
||||
static void cmd_erase(struct protocol_handle *phandle, const char *arg)
|
||||
{
|
||||
#if 0
|
||||
struct ptentry *ptn;
|
||||
struct ptable *ptable;
|
||||
|
||||
ptable = flash_get_ptable();
|
||||
if (ptable == NULL) {
|
||||
fastboot_fail(phandle, "partition table doesn't exist");
|
||||
return;
|
||||
}
|
||||
|
||||
ptn = ptable_find(ptable, arg);
|
||||
if (ptn == NULL) {
|
||||
fastboot_fail(phandle, "unknown partition name");
|
||||
return;
|
||||
}
|
||||
|
||||
if (flash_erase(ptn)) {
|
||||
fastboot_fail(phandle, "failed to erase partition");
|
||||
return;
|
||||
}
|
||||
fastboot_okay(phandle, "");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void cmd_flash(struct protocol_handle *phandle, const char *arg)
|
||||
{
|
||||
#if 0
|
||||
struct ptentry *ptn;
|
||||
struct ptable *ptable;
|
||||
unsigned extra = 0;
|
||||
|
||||
ptable = flash_get_ptable();
|
||||
if (ptable == NULL) {
|
||||
fastboot_fail(phandle, "partition table doesn't exist");
|
||||
return;
|
||||
}
|
||||
|
||||
ptn = ptable_find(ptable, arg);
|
||||
if (ptn == NULL) {
|
||||
fastboot_fail(phandle, "unknown partition name");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
|
||||
if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
|
||||
fastboot_fail(phandle, "image is not a boot image");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(ptn->name, "system") || !strcmp(ptn->name, "userdata"))
|
||||
extra = 64;
|
||||
else
|
||||
sz = ROUND_TO_PAGE(sz);
|
||||
|
||||
D(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
|
||||
if (flash_write(ptn, extra, data, sz)) {
|
||||
fastboot_fail(phandle, "flash write failure");
|
||||
return;
|
||||
}
|
||||
D(INFO, "partition '%s' updated\n", ptn->name);
|
||||
#endif
|
||||
fastboot_okay(phandle, "");
|
||||
}
|
||||
|
||||
static void cmd_continue(struct protocol_handle *phandle, const char *arg)
|
||||
{
|
||||
fastboot_okay(phandle, "");
|
||||
#if 0
|
||||
udc_stop();
|
||||
|
||||
boot_linux_from_flash();
|
||||
#endif
|
||||
}
|
||||
|
||||
static void cmd_getvar(struct protocol_handle *phandle, const char *arg)
|
||||
{
|
||||
const char *value;
|
||||
D(DEBUG, "cmd_getvar %s\n", arg);
|
||||
|
||||
value = fastboot_getvar(arg);
|
||||
|
||||
fastboot_okay(phandle, value);
|
||||
}
|
||||
|
||||
static void cmd_download(struct protocol_handle *phandle, const char *arg)
|
||||
{
|
||||
unsigned len = strtoul(arg, NULL, 16);
|
||||
int old_fd;
|
||||
|
||||
if (len > 256 * 1024 * 1024) {
|
||||
fastboot_fail(phandle, "data too large");
|
||||
return;
|
||||
}
|
||||
|
||||
fastboot_data(phandle, len);
|
||||
|
||||
old_fd = protocol_get_download(phandle);
|
||||
if (old_fd >= 0) {
|
||||
off_t len = lseek(old_fd, 0, SEEK_END);
|
||||
D(INFO, "disposing of unused fd %d, size %ld", old_fd, len);
|
||||
close(old_fd);
|
||||
}
|
||||
|
||||
phandle->download_fd = protocol_handle_download(phandle, len);
|
||||
if (phandle->download_fd < 0) {
|
||||
//handle->state = STATE_ERROR;
|
||||
fastboot_fail(phandle, "download failed");
|
||||
return;
|
||||
}
|
||||
|
||||
fastboot_okay(phandle, "");
|
||||
}
|
||||
|
||||
void commands_init()
|
||||
{
|
||||
fastboot_register("boot", cmd_boot);
|
||||
fastboot_register("erase:", cmd_erase);
|
||||
fastboot_register("flash:", cmd_flash);
|
||||
fastboot_register("continue", cmd_continue);
|
||||
fastboot_register("getvar:", cmd_getvar);
|
||||
fastboot_register("download:", cmd_download);
|
||||
//fastboot_publish("version", "0.5");
|
||||
//fastboot_publish("product", "swordfish");
|
||||
//fastboot_publish("kernel", "lk");
|
||||
}
|
||||
161
fastbootd/config.c
Normal file
161
fastbootd/config.c
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google, Inc. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
// TODO: change config path
|
||||
#define CONFIG_PATH "/data/fastboot.cfg"
|
||||
|
||||
static char *strip(char *str)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = strspn(str, " \t");
|
||||
str += n;
|
||||
|
||||
for (n = strlen(str) - 1; n >= 0; n--) {
|
||||
if (str[n] == ' ' || str[n] == '\t')
|
||||
str[n] = '\0';
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
static int config_parse_line(char *line)
|
||||
{
|
||||
char *c;
|
||||
char *key;
|
||||
char *value;
|
||||
|
||||
c = strchr(line, '#');
|
||||
if (c)
|
||||
*c = '\0';
|
||||
|
||||
if (strspn(line, " \t") == strlen(line))
|
||||
return 0;
|
||||
|
||||
c = strchr(line, '=');
|
||||
if (c == NULL)
|
||||
return -1;
|
||||
|
||||
key = line;
|
||||
*c = '\0';
|
||||
value = c + 1;
|
||||
|
||||
key = strip(key);
|
||||
value = strip(value);
|
||||
|
||||
key = strdup(key);
|
||||
value = strdup(value);
|
||||
|
||||
fastboot_publish(key, value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void config_parse(char *buffer)
|
||||
{
|
||||
char *saveptr;
|
||||
char *str = buffer;
|
||||
char *line = buffer;
|
||||
int c;
|
||||
int ret;
|
||||
|
||||
for (c = 1; line != NULL; c++) {
|
||||
line = strtok_r(str, "\r\n", &saveptr);
|
||||
if (line != NULL) {
|
||||
D(VERBOSE, "'%s'", line);
|
||||
ret = config_parse_line(line);
|
||||
if (ret < 0) {
|
||||
D(WARN, "error parsing " CONFIG_PATH " line %d", c);
|
||||
}
|
||||
}
|
||||
str = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void config_init()
|
||||
{
|
||||
int fd;
|
||||
off_t len;
|
||||
ssize_t ret;
|
||||
size_t count = 0;
|
||||
char *buffer;
|
||||
|
||||
fd = open(CONFIG_PATH, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
D(ERR, "failed to open " CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
|
||||
len = lseek(fd, 0, SEEK_END);
|
||||
if (len < 0) {
|
||||
D(ERR, "failed to seek to end of " CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
|
||||
buffer = malloc(len + 1);
|
||||
if (buffer == NULL) {
|
||||
D(ERR, "failed to allocate %ld bytes", len);
|
||||
return;
|
||||
}
|
||||
|
||||
while (count < (size_t)len) {
|
||||
ret = read(fd, buffer + count, len - count);
|
||||
if (ret < 0 && errno != EINTR) {
|
||||
D(ERR, "failed to read " CONFIG_PATH ": %d %s", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
if (ret == 0) {
|
||||
D(ERR, "early EOF reading " CONFIG_PATH);
|
||||
return;
|
||||
}
|
||||
|
||||
count += ret;
|
||||
}
|
||||
|
||||
buffer[len] = '\0';
|
||||
|
||||
config_parse(buffer);
|
||||
|
||||
free(buffer);
|
||||
}
|
||||
42
fastbootd/debug.h
Normal file
42
fastbootd/debug.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _FASTBOOTD_DEBUG_H_
|
||||
#define _FASTBOOTD_DEBUG_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <cutils/klog.h>
|
||||
|
||||
#define ERR 0
|
||||
#define WARN 1
|
||||
#define INFO 2
|
||||
#define VERBOSE 3
|
||||
#define DEBUG 4
|
||||
|
||||
extern unsigned int debug_level;
|
||||
|
||||
//#define DLOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
|
||||
#define DLOG(fmt, ...) KLOG_INFO("fastbootd", fmt, ##__VA_ARGS__)
|
||||
|
||||
#define D(level, fmt, ...) \
|
||||
do { \
|
||||
if (debug_level == level || debug_level > level) { \
|
||||
DLOG("%s:%d " fmt "\n", __BASE_FILE__, __LINE__, ##__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
45
fastbootd/fastbootd.c
Normal file
45
fastbootd/fastbootd.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cutils/klog.h>
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
unsigned int debug_level = DEBUG;
|
||||
|
||||
void commands_init();
|
||||
void usb_init();
|
||||
void config_init();
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
klog_init();
|
||||
klog_set_level(6);
|
||||
|
||||
config_init();
|
||||
commands_init();
|
||||
usb_init();
|
||||
while (1) {
|
||||
sleep(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
195
fastbootd/protocol.c
Normal file
195
fastbootd/protocol.c
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
/*
|
||||
* Copyright (c) 2009-2013, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google, Inc. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "protocol.h"
|
||||
#include "transport.h"
|
||||
|
||||
#define STATE_OFFLINE 0
|
||||
#define STATE_COMMAND 1
|
||||
#define STATE_COMPLETE 2
|
||||
#define STATE_ERROR 3
|
||||
|
||||
struct fastboot_cmd {
|
||||
struct fastboot_cmd *next;
|
||||
const char *prefix;
|
||||
unsigned prefix_len;
|
||||
void (*execute)(struct protocol_handle *phandle, const char *arg);
|
||||
};
|
||||
|
||||
struct fastboot_var {
|
||||
struct fastboot_var *next;
|
||||
const char *name;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
static struct fastboot_cmd *cmdlist;
|
||||
|
||||
void fastboot_register(const char *prefix,
|
||||
void (*phandle)(struct protocol_handle *phandle, const char *arg))
|
||||
{
|
||||
struct fastboot_cmd *cmd;
|
||||
cmd = malloc(sizeof(*cmd));
|
||||
if (cmd) {
|
||||
cmd->prefix = prefix;
|
||||
cmd->prefix_len = strlen(prefix);
|
||||
cmd->execute = phandle;
|
||||
cmd->next = cmdlist;
|
||||
cmdlist = cmd;
|
||||
}
|
||||
}
|
||||
|
||||
static struct fastboot_var *varlist;
|
||||
|
||||
void fastboot_publish(const char *name, const char *value)
|
||||
{
|
||||
struct fastboot_var *var;
|
||||
var = malloc(sizeof(*var));
|
||||
if (var) {
|
||||
var->name = name;
|
||||
var->value = value;
|
||||
var->next = varlist;
|
||||
varlist = var;
|
||||
}
|
||||
}
|
||||
|
||||
const char *fastboot_getvar(const char *name)
|
||||
{
|
||||
struct fastboot_var *var;
|
||||
|
||||
for (var = varlist; var; var = var->next) {
|
||||
if (!strcmp(var->name, name)) {
|
||||
return var->value;
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
int protocol_handle_download(struct protocol_handle *phandle, size_t len)
|
||||
{
|
||||
return transport_handle_download(phandle->transport_handle, len);
|
||||
}
|
||||
|
||||
static ssize_t protocol_handle_write(struct protocol_handle *phandle,
|
||||
char *buffer, size_t len)
|
||||
{
|
||||
return transport_handle_write(phandle->transport_handle, buffer, len);
|
||||
}
|
||||
|
||||
static void fastboot_ack(struct protocol_handle *phandle, const char *code,
|
||||
const char *reason)
|
||||
{
|
||||
char response[64];
|
||||
|
||||
if (phandle->state != STATE_COMMAND)
|
||||
return;
|
||||
|
||||
if (reason == 0)
|
||||
reason = "";
|
||||
|
||||
snprintf(response, 64, "%s%s", code, reason);
|
||||
phandle->state = STATE_COMPLETE;
|
||||
|
||||
protocol_handle_write(phandle, response, strlen(response));
|
||||
}
|
||||
|
||||
void fastboot_fail(struct protocol_handle *phandle, const char *reason)
|
||||
{
|
||||
fastboot_ack(phandle, "FAIL", reason);
|
||||
}
|
||||
|
||||
void fastboot_okay(struct protocol_handle *phandle, const char *info)
|
||||
{
|
||||
fastboot_ack(phandle, "OKAY", info);
|
||||
}
|
||||
|
||||
void fastboot_data(struct protocol_handle *phandle, size_t len)
|
||||
{
|
||||
char response[64];
|
||||
ssize_t ret;
|
||||
|
||||
snprintf(response, 64, "DATA%08x", len);
|
||||
ret = protocol_handle_write(phandle, response, strlen(response));
|
||||
if (ret < 0)
|
||||
return;
|
||||
}
|
||||
|
||||
void protocol_handle_command(struct protocol_handle *phandle, char *buffer)
|
||||
{
|
||||
D(INFO,"fastboot: %s\n", buffer);
|
||||
|
||||
struct fastboot_cmd *cmd;
|
||||
|
||||
for (cmd = cmdlist; cmd; cmd = cmd->next) {
|
||||
if (memcmp(buffer, cmd->prefix, cmd->prefix_len))
|
||||
continue;
|
||||
phandle->state = STATE_COMMAND;
|
||||
cmd->execute(phandle, buffer + cmd->prefix_len);
|
||||
if (phandle->state == STATE_COMMAND)
|
||||
fastboot_fail(phandle, "unknown reason");
|
||||
return;
|
||||
}
|
||||
|
||||
fastboot_fail(phandle, "unknown command");
|
||||
}
|
||||
|
||||
struct protocol_handle *create_protocol_handle(struct transport_handle *thandle)
|
||||
{
|
||||
struct protocol_handle *phandle;
|
||||
|
||||
phandle = calloc(sizeof(struct protocol_handle), 1);
|
||||
|
||||
phandle->transport_handle = thandle;
|
||||
phandle->state = STATE_OFFLINE;
|
||||
phandle->download_fd = -1;
|
||||
|
||||
pthread_mutex_init(&phandle->lock, NULL);
|
||||
|
||||
return phandle;
|
||||
}
|
||||
|
||||
int protocol_get_download(struct protocol_handle *phandle)
|
||||
{
|
||||
int fd;
|
||||
|
||||
pthread_mutex_lock(&phandle->lock);
|
||||
fd = phandle->download_fd;
|
||||
phandle->download_fd = -1;
|
||||
pthread_mutex_unlock(&phandle->lock);
|
||||
|
||||
return fd;
|
||||
}
|
||||
61
fastbootd/protocol.h
Normal file
61
fastbootd/protocol.h
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2013, Google Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* * Neither the name of Google, Inc. nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _FASTBOOTD_PROTOCOL_H_
|
||||
#define _FASTBOOTD_PROTOCOL_H_
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct protocol_handle {
|
||||
struct transport_handle *transport_handle;
|
||||
unsigned int state;
|
||||
int download_fd;
|
||||
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
void fastboot_register(const char *prefix,
|
||||
void (*handle)(struct protocol_handle *handle, const char *arg));
|
||||
|
||||
void fastboot_publish(const char *name, const char *value);
|
||||
const char *fastboot_getvar(const char *name);
|
||||
|
||||
struct protocol_handle *create_protocol_handle(struct transport_handle *t);
|
||||
void protocol_handle_command(struct protocol_handle *handle, char *buffer);
|
||||
int protocol_handle_download(struct protocol_handle *phandle, size_t len);
|
||||
int protocol_get_download(struct protocol_handle *phandle);
|
||||
|
||||
void fastboot_fail(struct protocol_handle *handle, const char *reason);
|
||||
void fastboot_okay(struct protocol_handle *handle, const char *reason);
|
||||
void fastboot_data(struct protocol_handle *handle, size_t len);
|
||||
|
||||
#endif
|
||||
149
fastbootd/transport.c
Normal file
149
fastbootd/transport.c
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "protocol.h"
|
||||
#include "transport.h"
|
||||
|
||||
#define COMMAND_BUF_SIZE 64
|
||||
|
||||
ssize_t transport_handle_write(struct transport_handle *thandle, char *buffer, size_t len)
|
||||
{
|
||||
return thandle->transport->write(thandle, buffer, len);
|
||||
}
|
||||
|
||||
void transport_handle_close(struct transport_handle *thandle)
|
||||
{
|
||||
thandle->transport->close(thandle);
|
||||
}
|
||||
|
||||
int transport_handle_download(struct transport_handle *thandle, size_t len)
|
||||
{
|
||||
ssize_t ret;
|
||||
size_t n = 0;
|
||||
int fd;
|
||||
// TODO: move out of /dev
|
||||
char tempname[] = "/dev/fastboot_download_XXXXXX";
|
||||
char *buffer;
|
||||
|
||||
fd = mkstemp(tempname);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
unlink(tempname);
|
||||
|
||||
ftruncate(fd, len);
|
||||
|
||||
buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (buffer == NULL) {
|
||||
D(ERR, "mmap(%u) failed: %d %s", len, errno, strerror(errno));
|
||||
goto err;
|
||||
}
|
||||
|
||||
while (n < len) {
|
||||
ret = thandle->transport->read(thandle, buffer + n, len - n);
|
||||
if (ret <= 0) {
|
||||
D(WARN, "transport read failed, ret=%d %s", ret, strerror(-ret));
|
||||
break;
|
||||
}
|
||||
n += ret;
|
||||
}
|
||||
|
||||
munmap(buffer, len);
|
||||
|
||||
if (n != len)
|
||||
goto err;
|
||||
|
||||
return fd;
|
||||
|
||||
err:
|
||||
close(fd);
|
||||
transport_handle_close(thandle);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void *transport_data_thread(void *arg)
|
||||
{
|
||||
struct transport_handle *thandle = arg;
|
||||
struct protocol_handle *phandle = create_protocol_handle(thandle);
|
||||
|
||||
while (!thandle->stopped) {
|
||||
int ret;
|
||||
char buffer[COMMAND_BUF_SIZE + 1];
|
||||
D(VERBOSE, "transport_data_thread\n");
|
||||
|
||||
ret = thandle->transport->read(thandle, buffer, COMMAND_BUF_SIZE);
|
||||
if (ret <= 0) {
|
||||
D(DEBUG, "ret = %d\n", ret);
|
||||
break;
|
||||
}
|
||||
if (ret > 0) {
|
||||
buffer[ret] = 0;
|
||||
protocol_handle_command(phandle, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
transport_handle_close(thandle);
|
||||
free(thandle);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *transport_connect_thread(void *arg)
|
||||
{
|
||||
struct transport *transport = arg;
|
||||
while (!transport->stopped) {
|
||||
struct transport_handle *thandle;
|
||||
pthread_t thread;
|
||||
pthread_attr_t attr;
|
||||
|
||||
D(VERBOSE, "transport_connect_thread\n");
|
||||
thandle = transport->connect(transport);
|
||||
if (thandle == NULL) {
|
||||
D(ERR, "transport connect failed\n");
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
thandle->transport = transport;
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
|
||||
pthread_create(&thread, &attr, transport_data_thread, thandle);
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void transport_register(struct transport *transport)
|
||||
{
|
||||
pthread_t thread;
|
||||
pthread_attr_t attr;
|
||||
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
|
||||
pthread_create(&thread, &attr, transport_connect_thread, transport);
|
||||
}
|
||||
41
fastbootd/transport.h
Normal file
41
fastbootd/transport.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _FASTBOOTD_TRANSPORT_H_
|
||||
#define _FASTBOOTD_TRANSPORT_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct transport_handle {
|
||||
struct transport *transport;
|
||||
|
||||
bool stopped;
|
||||
};
|
||||
|
||||
struct transport {
|
||||
void (*init)();
|
||||
void (*close)(struct transport_handle *thandle);
|
||||
ssize_t (*read)(struct transport_handle *thandle, void *data, size_t len);
|
||||
ssize_t (*write)(struct transport_handle *thandle, const void *data, size_t len);
|
||||
struct transport_handle *(*connect)(struct transport *transport);
|
||||
bool stopped;
|
||||
};
|
||||
|
||||
void transport_register(struct transport *transport);
|
||||
ssize_t transport_handle_write(struct transport_handle *handle, char *buffer, size_t len);
|
||||
int transport_handle_download(struct transport_handle *handle, size_t len);
|
||||
|
||||
#endif
|
||||
353
fastbootd/usb_linux_client.c
Normal file
353
fastbootd/usb_linux_client.c
Normal file
|
|
@ -0,0 +1,353 @@
|
|||
/*
|
||||
* Copyright (C) 2007 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <endian.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <linux/usb/ch9.h>
|
||||
#include <linux/usb/functionfs.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "transport.h"
|
||||
|
||||
#define TRACE_TAG TRACE_USB
|
||||
|
||||
#define MAX_PACKET_SIZE_FS 64
|
||||
#define MAX_PACKET_SIZE_HS 512
|
||||
|
||||
#define cpu_to_le16(x) htole16(x)
|
||||
#define cpu_to_le32(x) htole32(x)
|
||||
|
||||
#define FASTBOOT_CLASS 0xff
|
||||
#define FASTBOOT_SUBCLASS 0x42
|
||||
#define FASTBOOT_PROTOCOL 0x3
|
||||
|
||||
#define USB_FFS_FASTBOOT_PATH "/dev/usb-ffs/adb/"
|
||||
#define USB_FFS_FASTBOOT_EP(x) USB_FFS_FASTBOOT_PATH#x
|
||||
|
||||
#define USB_FFS_FASTBOOT_EP0 USB_FFS_FASTBOOT_EP(ep0)
|
||||
#define USB_FFS_FASTBOOT_OUT USB_FFS_FASTBOOT_EP(ep1)
|
||||
#define USB_FFS_FASTBOOT_IN USB_FFS_FASTBOOT_EP(ep2)
|
||||
|
||||
#define READ_BUF_SIZE (16*1024)
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
((type*)((char*)(ptr) - offsetof(type, member)))
|
||||
|
||||
struct usb_transport {
|
||||
struct transport transport;
|
||||
|
||||
pthread_cond_t notify;
|
||||
pthread_mutex_t lock;
|
||||
|
||||
int control;
|
||||
int bulk_out; /* "out" from the host's perspective => source for fastbootd */
|
||||
int bulk_in; /* "in" from the host's perspective => sink for fastbootd */
|
||||
};
|
||||
|
||||
struct usb_handle {
|
||||
struct transport_handle handle;
|
||||
};
|
||||
|
||||
static const struct {
|
||||
struct usb_functionfs_descs_head header;
|
||||
struct {
|
||||
struct usb_interface_descriptor intf;
|
||||
struct usb_endpoint_descriptor_no_audio source;
|
||||
struct usb_endpoint_descriptor_no_audio sink;
|
||||
} __attribute__((packed)) fs_descs, hs_descs;
|
||||
} __attribute__((packed)) descriptors = {
|
||||
.header = {
|
||||
.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
|
||||
.length = cpu_to_le32(sizeof(descriptors)),
|
||||
.fs_count = 3,
|
||||
.hs_count = 3,
|
||||
},
|
||||
.fs_descs = {
|
||||
.intf = {
|
||||
.bLength = sizeof(descriptors.fs_descs.intf),
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bNumEndpoints = 2,
|
||||
.bInterfaceClass = FASTBOOT_CLASS,
|
||||
.bInterfaceSubClass = FASTBOOT_SUBCLASS,
|
||||
.bInterfaceProtocol = FASTBOOT_PROTOCOL,
|
||||
.iInterface = 1, /* first string from the provided table */
|
||||
},
|
||||
.source = {
|
||||
.bLength = sizeof(descriptors.fs_descs.source),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 1 | USB_DIR_OUT,
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = MAX_PACKET_SIZE_FS,
|
||||
},
|
||||
.sink = {
|
||||
.bLength = sizeof(descriptors.fs_descs.sink),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 2 | USB_DIR_IN,
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = MAX_PACKET_SIZE_FS,
|
||||
},
|
||||
},
|
||||
.hs_descs = {
|
||||
.intf = {
|
||||
.bLength = sizeof(descriptors.hs_descs.intf),
|
||||
.bDescriptorType = USB_DT_INTERFACE,
|
||||
.bInterfaceNumber = 0,
|
||||
.bNumEndpoints = 2,
|
||||
.bInterfaceClass = FASTBOOT_CLASS,
|
||||
.bInterfaceSubClass = FASTBOOT_SUBCLASS,
|
||||
.bInterfaceProtocol = FASTBOOT_PROTOCOL,
|
||||
.iInterface = 1, /* first string from the provided table */
|
||||
},
|
||||
.source = {
|
||||
.bLength = sizeof(descriptors.hs_descs.source),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 1 | USB_DIR_OUT,
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = MAX_PACKET_SIZE_HS,
|
||||
},
|
||||
.sink = {
|
||||
.bLength = sizeof(descriptors.hs_descs.sink),
|
||||
.bDescriptorType = USB_DT_ENDPOINT,
|
||||
.bEndpointAddress = 2 | USB_DIR_IN,
|
||||
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
||||
.wMaxPacketSize = MAX_PACKET_SIZE_HS,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
#define STR_INTERFACE_ "Fastboot Interface"
|
||||
|
||||
static const struct {
|
||||
struct usb_functionfs_strings_head header;
|
||||
struct {
|
||||
__le16 code;
|
||||
const char str1[sizeof(STR_INTERFACE_)];
|
||||
} __attribute__((packed)) lang0;
|
||||
} __attribute__((packed)) strings = {
|
||||
.header = {
|
||||
.magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
|
||||
.length = cpu_to_le32(sizeof(strings)),
|
||||
.str_count = cpu_to_le32(1),
|
||||
.lang_count = cpu_to_le32(1),
|
||||
},
|
||||
.lang0 = {
|
||||
cpu_to_le16(0x0409), /* en-us */
|
||||
STR_INTERFACE_,
|
||||
},
|
||||
};
|
||||
|
||||
static int init_functionfs(struct usb_transport *usb_transport)
|
||||
{
|
||||
ssize_t ret;
|
||||
|
||||
D(VERBOSE, "OPENING %s", USB_FFS_FASTBOOT_EP0);
|
||||
usb_transport->control = open(USB_FFS_FASTBOOT_EP0, O_RDWR);
|
||||
if (usb_transport->control < 0) {
|
||||
D(ERR, "[ %s: cannot open control endpoint: errno=%d]", USB_FFS_FASTBOOT_EP0, errno);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = write(usb_transport->control, &descriptors, sizeof(descriptors));
|
||||
if (ret < 0) {
|
||||
D(ERR, "[ %s: write descriptors failed: errno=%d ]", USB_FFS_FASTBOOT_EP0, errno);
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = write(usb_transport->control, &strings, sizeof(strings));
|
||||
if (ret < 0) {
|
||||
D(ERR, "[ %s: writing strings failed: errno=%d]", USB_FFS_FASTBOOT_EP0, errno);
|
||||
goto err;
|
||||
}
|
||||
|
||||
usb_transport->bulk_out = open(USB_FFS_FASTBOOT_OUT, O_RDWR);
|
||||
if (usb_transport->bulk_out < 0) {
|
||||
D(ERR, "[ %s: cannot open bulk-out ep: errno=%d ]", USB_FFS_FASTBOOT_OUT, errno);
|
||||
goto err;
|
||||
}
|
||||
|
||||
usb_transport->bulk_in = open(USB_FFS_FASTBOOT_IN, O_RDWR);
|
||||
if (usb_transport->bulk_in < 0) {
|
||||
D(ERR, "[ %s: cannot open bulk-in ep: errno=%d ]", USB_FFS_FASTBOOT_IN, errno);
|
||||
goto err;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
err:
|
||||
if (usb_transport->bulk_in > 0) {
|
||||
close(usb_transport->bulk_in);
|
||||
usb_transport->bulk_in = -1;
|
||||
}
|
||||
if (usb_transport->bulk_out > 0) {
|
||||
close(usb_transport->bulk_out);
|
||||
usb_transport->bulk_out = -1;
|
||||
}
|
||||
if (usb_transport->control > 0) {
|
||||
close(usb_transport->control);
|
||||
usb_transport->control = -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static ssize_t bulk_write(int bulk_in, const char *buf, size_t length)
|
||||
{
|
||||
size_t count = 0;
|
||||
ssize_t ret;
|
||||
|
||||
do {
|
||||
ret = TEMP_FAILURE_RETRY(write(bulk_in, buf + count, length - count));
|
||||
if (ret < 0) {
|
||||
D(WARN, "[ bulk_read failed fd=%d length=%d errno=%d %s ]",
|
||||
bulk_in, length, errno, strerror(errno));
|
||||
return -1;
|
||||
} else {
|
||||
count += ret;
|
||||
}
|
||||
} while (count < length);
|
||||
|
||||
D(VERBOSE, "[ bulk_write done fd=%d ]", bulk_in);
|
||||
return count;
|
||||
}
|
||||
|
||||
static ssize_t usb_write(struct transport_handle *thandle, const void *data, size_t len)
|
||||
{
|
||||
ssize_t ret;
|
||||
struct transport *t = thandle->transport;
|
||||
struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
|
||||
|
||||
D(DEBUG, "about to write (fd=%d, len=%d)", usb_transport->bulk_in, len);
|
||||
ret = bulk_write(usb_transport->bulk_in, data, len);
|
||||
if (ret < 0) {
|
||||
D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_in, ret);
|
||||
return -1;
|
||||
}
|
||||
D(DEBUG, "[ usb_write done fd=%d ]", usb_transport->bulk_in);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t bulk_read(int bulk_out, char *buf, size_t length)
|
||||
{
|
||||
ssize_t ret;
|
||||
size_t n = 0;
|
||||
|
||||
while (n < length) {
|
||||
size_t to_read = (length - n > READ_BUF_SIZE) ? READ_BUF_SIZE : length - n;
|
||||
ret = TEMP_FAILURE_RETRY(read(bulk_out, buf + n, to_read));
|
||||
if (ret < 0) {
|
||||
D(WARN, "[ bulk_read failed fd=%d length=%d errno=%d %s ]",
|
||||
bulk_out, length, errno, strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
n += ret;
|
||||
if (ret < (ssize_t)to_read) {
|
||||
D(VERBOSE, "bulk_read short read, ret=%zd to_read=%u n=%u length=%u",
|
||||
ret, to_read, n, length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
ssize_t usb_read(struct transport_handle *thandle, void *data, size_t len)
|
||||
{
|
||||
ssize_t ret;
|
||||
struct transport *t = thandle->transport;
|
||||
struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
|
||||
|
||||
D(DEBUG, "about to read (fd=%d, len=%d)", usb_transport->bulk_out, len);
|
||||
ret = bulk_read(usb_transport->bulk_out, data, len);
|
||||
if (ret < 0) {
|
||||
D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_out, ret);
|
||||
return -1;
|
||||
}
|
||||
D(DEBUG, "[ usb_read done fd=%d ret=%zd]", usb_transport->bulk_out, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void usb_close(struct transport_handle *thandle)
|
||||
{
|
||||
int err;
|
||||
struct transport *t = thandle->transport;
|
||||
struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
|
||||
|
||||
err = ioctl(usb_transport->bulk_in, FUNCTIONFS_CLEAR_HALT);
|
||||
if (err < 0)
|
||||
D(WARN, "[ kick: source (fd=%d) clear halt failed (%d) ]", usb_transport->bulk_in, errno);
|
||||
|
||||
err = ioctl(usb_transport->bulk_out, FUNCTIONFS_CLEAR_HALT);
|
||||
if (err < 0)
|
||||
D(WARN, "[ kick: sink (fd=%d) clear halt failed (%d) ]", usb_transport->bulk_out, errno);
|
||||
|
||||
pthread_mutex_lock(&usb_transport->lock);
|
||||
close(usb_transport->control);
|
||||
close(usb_transport->bulk_out);
|
||||
close(usb_transport->bulk_in);
|
||||
usb_transport->control = usb_transport->bulk_out = usb_transport->bulk_in = -1;
|
||||
|
||||
pthread_cond_signal(&usb_transport->notify);
|
||||
pthread_mutex_unlock(&usb_transport->lock);
|
||||
}
|
||||
|
||||
struct transport_handle *usb_connect(struct transport *transport)
|
||||
{
|
||||
int ret;
|
||||
struct usb_handle *usb_handle = calloc(sizeof(struct usb_handle), 1);
|
||||
struct usb_transport *usb_transport = container_of(transport, struct usb_transport, transport);
|
||||
|
||||
pthread_mutex_lock(&usb_transport->lock);
|
||||
while (usb_transport->control != -1)
|
||||
pthread_cond_wait(&usb_transport->notify, &usb_transport->lock);
|
||||
pthread_mutex_unlock(&usb_transport->lock);
|
||||
|
||||
ret = init_functionfs(usb_transport);
|
||||
if (ret < 0) {
|
||||
D(ERR, "usb connect: failed to initialize usb transport");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
D(DEBUG, "[ usb_thread - registering device ]");
|
||||
return &usb_handle->handle;
|
||||
}
|
||||
|
||||
void usb_init()
|
||||
{
|
||||
struct usb_transport *usb_transport = calloc(1, sizeof(struct usb_transport));
|
||||
|
||||
usb_transport->transport.connect = usb_connect;
|
||||
usb_transport->transport.close = usb_close;
|
||||
usb_transport->transport.read = usb_read;
|
||||
usb_transport->transport.write = usb_write;
|
||||
usb_transport->control = -1;
|
||||
usb_transport->bulk_out = -1;
|
||||
usb_transport->bulk_out = -1;
|
||||
|
||||
pthread_cond_init(&usb_transport->notify, NULL);
|
||||
pthread_mutex_init(&usb_transport->lock, NULL);
|
||||
|
||||
transport_register(&usb_transport->transport);
|
||||
}
|
||||
|
||||
Loading…
Add table
Reference in a new issue