Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 98699eca authored by Elliott Hughes's avatar Elliott Hughes Committed by Rom Lemarchand
Browse files

Sync with master fastboot.

This brings us all the fastboot changes, plus a change we need from libbase,
and then reverts part of a libziparchive cleanup and fixes dependencies on
new changes to the build system.

Bug: http://b/25375777
Change-Id: I813464da95dc6c81b0a1dc7145152f89bb2d14cd
parent c48d249c
Loading
Loading
Loading
Loading
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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 BASE_PARSEINT_H
#define BASE_PARSEINT_H

#include <errno.h>
#include <stdlib.h>

#include <limits>

namespace android {
namespace base {

// Parses the unsigned decimal integer in the string 's' and sets 'out' to
// that value. Optionally allows the caller to define a 'max' beyond which
// otherwise valid values will be rejected. Returns boolean success.
template <typename T>
bool ParseUint(const char* s, T* out,
               T max = std::numeric_limits<T>::max()) {
  int base = (s[0] == '0' && s[1] == 'x') ? 16 : 10;
  errno = 0;
  char* end;
  unsigned long long int result = strtoull(s, &end, base);
  if (errno != 0 || s == end || *end != '\0') {
    return false;
  }
  if (max < result) {
    return false;
  }
  *out = static_cast<T>(result);
  return true;
}

// Parses the signed decimal integer in the string 's' and sets 'out' to
// that value. Optionally allows the caller to define a 'min' and 'max
// beyond which otherwise valid values will be rejected. Returns boolean
// success.
template <typename T>
bool ParseInt(const char* s, T* out,
              T min = std::numeric_limits<T>::min(),
              T max = std::numeric_limits<T>::max()) {
  int base = (s[0] == '0' && s[1] == 'x') ? 16 : 10;
  errno = 0;
  char* end;
  long long int result = strtoll(s, &end, base);
  if (errno != 0 || s == end || *end != '\0') {
    return false;
  }
  if (result < min || max < result) {
    return false;
  }
  *out = static_cast<T>(result);
  return true;
}

}  // namespace base
}  // namespace android

#endif  // BASE_PARSEINT_H
+29 −42
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../mkbootimg \
  $(LOCAL_PATH)/../../extras/ext4_utils \
  $(LOCAL_PATH)/../../extras/f2fs_utils
LOCAL_SRC_FILES := protocol.c engine.c bootimg_utils.cpp fastboot.cpp util.c fs.c
LOCAL_SRC_FILES := protocol.cpp engine.cpp bootimg_utils.cpp fastboot.cpp util.cpp fs.cpp
LOCAL_MODULE := fastboot
LOCAL_MODULE_TAGS := debug
LOCAL_CONLYFLAGS += -std=gnu99
@@ -29,33 +29,7 @@ LOCAL_CFLAGS += -Wall -Wextra -Werror -Wunreachable-code

LOCAL_CFLAGS += -DFASTBOOT_REVISION='"$(fastboot_version)"'

ifeq ($(HOST_OS),linux)
  LOCAL_SRC_FILES += usb_linux.c util_linux.c
endif

ifeq ($(HOST_OS),darwin)
  LOCAL_SRC_FILES += usb_osx.c util_osx.c
  LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
  LOCAL_CFLAGS += -Wno-unused-parameter
endif

ifeq ($(HOST_OS),windows)
  LOCAL_SRC_FILES += usb_windows.c util_windows.c
  EXTRA_STATIC_LIBS := AdbWinApi
  ifneq ($(strip $(USE_CYGWIN)),)
    # Pure cygwin case
    LOCAL_LDLIBS += -lpthread
  endif
  ifneq ($(strip $(USE_MINGW)),)
    # MinGW under Linux case
    LOCAL_LDLIBS += -lws2_32
    USE_SYSDEPS_WIN32 := 1
  endif
  LOCAL_C_INCLUDES += development/host/windows/usb/api
endif

LOCAL_STATIC_LIBRARIES := \
    $(EXTRA_STATIC_LIBS) \
LOCAL_STATIC_LIBRARIES += \
    libziparchive-host \
    libext4_utils_host \
    libsparse_host \
@@ -64,21 +38,35 @@ LOCAL_STATIC_LIBRARIES := \
    libz \
    libbase

ifneq ($(HOST_OS),windows)
ifeq ($(HOST_OS),linux)
  LOCAL_SRC_FILES += usb_linux.cpp util_linux.cpp
  LOCAL_STATIC_LIBRARIES += libselinux
endif # HOST_OS != windows

ifeq ($(HOST_OS),linux)
  # libf2fs_dlutils_host will dlopen("libf2fs_fmt_host_dyn")
  LOCAL_CFLAGS += -DUSE_F2FS
  LOCAL_LDFLAGS += -ldl -rdynamic -Wl,-rpath,.
LOCAL_REQUIRED_MODULES := libf2fs_fmt_host_dyn
  LOCAL_REQUIRED_MODULES += libf2fs_fmt_host_dyn
  # The following libf2fs_* are from system/extras/f2fs_utils,
  # and do not use code in external/f2fs-tools.
  LOCAL_STATIC_LIBRARIES += libf2fs_utils_host libf2fs_ioutils_host libf2fs_dlutils_host
endif

# libc++ not available on windows yet
ifeq ($(HOST_OS),darwin)
  LOCAL_SRC_FILES += usb_osx.cpp util_osx.cpp
  LOCAL_LDLIBS += -lpthread -framework CoreFoundation -framework IOKit -framework Carbon
  LOCAL_CFLAGS += -Wno-unused-parameter
  LOCAL_STATIC_LIBRARIES += libselinux
endif

ifeq ($(HOST_OS),windows)
  LOCAL_SRC_FILES += usb_windows.cpp util_windows.cpp
  LOCAL_STATIC_LIBRARIES += AdbWinApi
  LOCAL_REQUIRED_MODULES += AdbWinApi
  LOCAL_LDLIBS += -lws2_32
  LOCAL_C_INCLUDES += development/host/windows/usb/api
endif

# libc++ not available on windows yet.
ifneq ($(HOST_OS),windows)
  LOCAL_CXX_STL := libc++_static
endif
@@ -97,10 +85,9 @@ endif
$(call dist-for-goals,dist_files sdk,$(my_dist_files))
my_dist_files :=


ifeq ($(HOST_OS),linux)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := usbtest.c usb_linux.c util.c
LOCAL_SRC_FILES := usbtest.cpp usb_linux.cpp util.cpp
LOCAL_MODULE := usbtest
LOCAL_CFLAGS := -Werror
include $(BUILD_HOST_EXECUTABLE)
+14 −22
Original line number Diff line number Diff line
@@ -37,27 +37,22 @@ void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline)
    strcpy((char*) h->cmdline, cmdline);
}

boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
                        void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
                        void *second, unsigned second_size, unsigned second_offset,
                        unsigned page_size, unsigned base, unsigned tags_offset,
                        unsigned *bootimg_size)
boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
                        void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
                        void* second, int64_t second_size, off_t second_offset,
                        size_t page_size, size_t base, off_t tags_offset,
                        int64_t* bootimg_size)
{
    unsigned kernel_actual;
    unsigned ramdisk_actual;
    unsigned second_actual;
    unsigned page_mask;
    size_t page_mask = page_size - 1;

    page_mask = page_size - 1;

    kernel_actual = (kernel_size + page_mask) & (~page_mask);
    ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask);
    second_actual = (second_size + page_mask) & (~page_mask);
    int64_t kernel_actual = (kernel_size + page_mask) & (~page_mask);
    int64_t ramdisk_actual = (ramdisk_size + page_mask) & (~page_mask);
    int64_t second_actual = (second_size + page_mask) & (~page_mask);

    *bootimg_size = page_size + kernel_actual + ramdisk_actual + second_actual;

    boot_img_hdr* hdr = reinterpret_cast<boot_img_hdr*>(calloc(*bootimg_size, 1));
    if (hdr == 0) {
    if (hdr == nullptr) {
        return hdr;
    }

@@ -74,12 +69,9 @@ boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offs

    hdr->page_size =    page_size;

    memcpy(hdr->magic + page_size, kernel, kernel_size);
    memcpy(hdr->magic + page_size + kernel_actual, ramdisk, ramdisk_size);
    memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual, second, second_size);

    memcpy(hdr->magic + page_size,
           kernel, kernel_size);
    memcpy(hdr->magic + page_size + kernel_actual,
           ramdisk, ramdisk_size);
    memcpy(hdr->magic + page_size + kernel_actual + ramdisk_actual,
           second, second_size);
    return hdr;
}
+8 −14
Original line number Diff line number Diff line
@@ -30,20 +30,14 @@
#define _FASTBOOT_BOOTIMG_UTILS_H_

#include <bootimg.h>

#if defined(__cplusplus)
extern "C" {
#endif
#include <inttypes.h>
#include <sys/types.h>

void bootimg_set_cmdline(boot_img_hdr* h, const char* cmdline);
boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
                        void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
                        void *second, unsigned second_size, unsigned second_offset,
                        unsigned page_size, unsigned base, unsigned tags_offset,
                        unsigned *bootimg_size);

#if defined(__cplusplus)
}
#endif
boot_img_hdr* mkbootimg(void* kernel, int64_t kernel_size, off_t kernel_offset,
                        void* ramdisk, int64_t ramdisk_size, off_t ramdisk_offset,
                        void* second, int64_t second_size, off_t second_offset,
                        size_t page_size, size_t base, off_t tags_offset,
                        int64_t* bootimg_size);

#endif
+34 −80
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@

#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -39,16 +38,6 @@
#include <sys/types.h>
#include <unistd.h>

#ifdef USE_MINGW
#include <fcntl.h>
#else
#include <sys/mman.h>
#endif

#ifndef __unused
#define __unused __attribute__((__unused__))
#endif

#define ARRAY_SIZE(x)           (sizeof(x)/sizeof(x[0]))

#define OP_DOWNLOAD   1
@@ -62,18 +51,20 @@ typedef struct Action Action;

#define CMD_SIZE 64

struct Action
{
struct Action {
    unsigned op;
    Action* next;

    char cmd[CMD_SIZE];
    const char* prod;
    void* data;
    unsigned size;

    // The protocol only supports 32-bit sizes, so you'll have to break
    // anything larger into chunks.
    uint32_t size;

    const char *msg;
    int (*func)(Action *a, int status, char *resp);
    int (*func)(Action* a, int status, const char* resp);

    double start;
};
@@ -84,45 +75,20 @@ static Action *action_last = 0;



int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
{
    char cmd[CMD_SIZE] = "getvar:";
    int getvar_len = strlen(cmd);
    va_list args;

    response[FB_RESPONSE_SZ] = '\0';
    va_start(args, fmt);
    vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
    va_end(args);
    cmd[CMD_SIZE - 1] = '\0';
    return fb_command_response(usb, cmd, response);
}
bool fb_getvar(usb_handle* usb, const std::string& key, std::string* value) {
    std::string cmd = "getvar:";
    cmd += key;


/* Return true if this partition is supported by the fastboot format command.
 * It is also used to determine if we should first erase a partition before
 * flashing it with an ext4 filesystem.  See needs_erase()
 *
 * Not all devices report the filesystem type, so don't report any errors,
 * just return false.
 */
int fb_format_supported(usb_handle *usb, const char *partition, const char *type_override)
{
    char fs_type[FB_RESPONSE_SZ + 1] = {0,};
    int status;

    if (type_override) {
        return !!fs_get_generator(type_override);
    }
    status = fb_getvar(usb, fs_type, "partition-type:%s", partition);
    if (status) {
        return 0;
    char buf[FB_RESPONSE_SZ + 1];
    memset(buf, 0, sizeof(buf));
    if (fb_command_response(usb, cmd.c_str(), buf)) {
      return false;
    }
    return !!fs_get_generator(fs_type);
    *value = buf;
    return true;
}

static int cb_default(Action *a, int status, char *resp)
{
static int cb_default(Action* a, int status, const char* resp) {
    if (status) {
        fprintf(stderr,"FAILED (%s)\n", resp);
    } else {
@@ -135,12 +101,11 @@ static int cb_default(Action *a, int status, char *resp)

static Action *queue_action(unsigned op, const char *fmt, ...)
{
    Action *a;
    va_list ap;
    size_t cmdsize;

    a = calloc(1, sizeof(Action));
    if (a == 0) die("out of memory");
    Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action)));
    if (a == nullptr) die("out of memory");

    va_start(ap, fmt);
    cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
@@ -198,8 +163,7 @@ void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
    a->msg = mkmsg("writing '%s'", ptn);
}

static int match(char *str, const char **value, unsigned count)
{
static int match(const char* str, const char** value, unsigned count) {
    unsigned n;

    for (n = 0; n < count; n++) {
@@ -222,9 +186,9 @@ static int match(char *str, const char **value, unsigned count)



static int cb_check(Action *a, int status, char *resp, int invert)
static int cb_check(Action* a, int status, const char* resp, int invert)
{
    const char **value = a->data;
    const char** value = reinterpret_cast<const char**>(a->data);
    unsigned count = a->size;
    unsigned n;
    int yes;
@@ -265,18 +229,16 @@ static int cb_check(Action *a, int status, char *resp, int invert)
    return -1;
}

static int cb_require(Action *a, int status, char *resp)
{
static int cb_require(Action*a, int status, const char* resp) {
    return cb_check(a, status, resp, 0);
}

static int cb_reject(Action *a, int status, char *resp)
{
static int cb_reject(Action* a, int status, const char* resp) {
    return cb_check(a, status, resp, 1);
}

void fb_queue_require(const char *prod, const char *var,
		int invert, unsigned nvalues, const char **value)
                      bool invert, size_t nvalues, const char **value)
{
    Action *a;
    a = queue_action(OP_QUERY, "getvar:%s", var);
@@ -285,11 +247,10 @@ void fb_queue_require(const char *prod, const char *var,
    a->size = nvalues;
    a->msg = mkmsg("checking %s", var);
    a->func = invert ? cb_reject : cb_require;
    if (a->data == 0) die("out of memory");
    if (a->data == nullptr) die("out of memory");
}

static int cb_display(Action *a, int status, char *resp)
{
static int cb_display(Action* a, int status, const char* resp) {
    if (status) {
        fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
        return status;
@@ -303,17 +264,16 @@ void fb_queue_display(const char *var, const char *prettyname)
    Action *a;
    a = queue_action(OP_QUERY, "getvar:%s", var);
    a->data = strdup(prettyname);
    if (a->data == 0) die("out of memory");
    if (a->data == nullptr) die("out of memory");
    a->func = cb_display;
}

static int cb_save(Action *a, int status, char *resp)
{
static int cb_save(Action* a, int status, const char* resp) {
    if (status) {
        fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
        return status;
    }
    strncpy(a->data, resp, a->size);
    strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
    return 0;
}

@@ -326,8 +286,7 @@ void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
    a->func = cb_save;
}

static int cb_do_nothing(Action *a __unused, int status __unused, char *resp __unused)
{
static int cb_do_nothing(Action*, int , const char*) {
    fprintf(stderr,"\n");
    return 0;
}
@@ -398,7 +357,7 @@ int fb_execute_queue(usb_handle *usb)
        } else if (a->op == OP_NOTICE) {
            fprintf(stderr,"%s\n",(char*)a->data);
        } else if (a->op == OP_DOWNLOAD_SPARSE) {
            status = fb_download_data_sparse(usb, a->data);
            status = fb_download_data_sparse(usb, reinterpret_cast<sparse_file*>(a->data));
            status = a->func(a, status, status ? fb_get_error() : "");
            if (status) break;
        } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
@@ -411,8 +370,3 @@ int fb_execute_queue(usb_handle *usb)
    fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
    return status;
}

int fb_queue_is_empty(void)
{
    return (action_list == NULL);
}
Loading