2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2012-02-28 16:21:08 +01:00
|
|
|
* the documentation and/or other materials provided with the
|
2009-03-04 04:32:55 +01:00
|
|
|
* 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
|
2012-02-28 16:21:08 +01:00
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
2009-03-04 04:32:55 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-12-16 02:50:18 +01:00
|
|
|
#include "fastboot.h"
|
2014-03-12 02:28:15 +01:00
|
|
|
#include "fs.h"
|
2011-12-16 02:50:18 +01:00
|
|
|
|
2013-01-24 04:13:43 +01:00
|
|
|
#include <errno.h>
|
2015-08-26 04:34:13 +02:00
|
|
|
#include <stdarg.h>
|
2014-04-30 23:05:28 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <string.h>
|
2011-12-16 02:50:18 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2011-12-16 02:50:18 +01:00
|
|
|
|
2011-12-16 02:50:18 +01:00
|
|
|
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#define OP_DOWNLOAD 1
|
|
|
|
#define OP_COMMAND 2
|
|
|
|
#define OP_QUERY 3
|
|
|
|
#define OP_NOTICE 4
|
2014-03-12 02:28:15 +01:00
|
|
|
#define OP_DOWNLOAD_SPARSE 5
|
|
|
|
#define OP_WAIT_FOR_DISCONNECT 6
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
typedef struct Action Action;
|
|
|
|
|
2011-12-16 02:50:18 +01:00
|
|
|
#define CMD_SIZE 64
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
struct Action {
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned op;
|
2015-04-08 05:12:50 +02:00
|
|
|
Action* next;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2011-12-16 02:50:18 +01:00
|
|
|
char cmd[CMD_SIZE];
|
2015-04-08 05:12:50 +02:00
|
|
|
const char* prod;
|
|
|
|
void* data;
|
|
|
|
|
|
|
|
// The protocol only supports 32-bit sizes, so you'll have to break
|
|
|
|
// anything larger into chunks.
|
|
|
|
uint32_t size;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
const char *msg;
|
2015-06-24 05:27:58 +02:00
|
|
|
int (*func)(Action* a, int status, const char* resp);
|
2010-02-25 20:05:33 +01:00
|
|
|
|
|
|
|
double start;
|
2009-03-04 04:32:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static Action *action_list = 0;
|
|
|
|
static Action *action_last = 0;
|
|
|
|
|
2011-12-16 02:50:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
bool fb_getvar(Transport* transport, const std::string& key, std::string* value) {
|
2015-10-30 19:49:47 +01:00
|
|
|
std::string cmd = "getvar:";
|
|
|
|
cmd += key;
|
|
|
|
|
|
|
|
char buf[FB_RESPONSE_SZ + 1];
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2015-10-30 19:22:01 +01:00
|
|
|
if (fb_command_response(transport, cmd.c_str(), buf)) {
|
2015-10-30 19:49:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*value = buf;
|
|
|
|
return true;
|
2012-05-25 03:24:53 +02:00
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_default(Action* a, int status, const char* resp) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (status) {
|
|
|
|
fprintf(stderr,"FAILED (%s)\n", resp);
|
|
|
|
} else {
|
2010-02-25 20:05:33 +01:00
|
|
|
double split = now();
|
|
|
|
fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
|
|
|
|
a->start = split;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Action *queue_action(unsigned op, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2010-07-15 17:52:01 +02:00
|
|
|
size_t cmdsize;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action)));
|
|
|
|
if (a == nullptr) die("out of memory");
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2010-07-15 17:52:01 +02:00
|
|
|
cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
|
2009-03-04 04:32:55 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
2010-07-15 17:52:01 +02:00
|
|
|
if (cmdsize >= sizeof(a->cmd)) {
|
|
|
|
free(a);
|
|
|
|
die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
if (action_last) {
|
|
|
|
action_last->next = a;
|
|
|
|
} else {
|
|
|
|
action_list = a;
|
|
|
|
}
|
|
|
|
action_last = a;
|
|
|
|
a->op = op;
|
|
|
|
a->func = cb_default;
|
2010-02-25 20:05:33 +01:00
|
|
|
|
|
|
|
a->start = -1;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2015-09-15 06:05:41 +02:00
|
|
|
void fb_set_active(const char *slot)
|
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
a = queue_action(OP_COMMAND, "set_active:%s", slot);
|
|
|
|
a->msg = mkmsg("Setting current slot to '%s'", slot);
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
void fb_queue_erase(const char *ptn)
|
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
a = queue_action(OP_COMMAND, "erase:%s", ptn);
|
|
|
|
a->msg = mkmsg("erasing '%s'", ptn);
|
|
|
|
}
|
|
|
|
|
2014-05-08 01:31:59 +02:00
|
|
|
void fb_queue_flash(const char *ptn, void *data, unsigned sz)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
|
|
|
|
a = queue_action(OP_DOWNLOAD, "");
|
|
|
|
a->data = data;
|
|
|
|
a->size = sz;
|
|
|
|
a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
|
|
|
|
|
|
|
|
a = queue_action(OP_COMMAND, "flash:%s", ptn);
|
|
|
|
a->msg = mkmsg("writing '%s'", ptn);
|
|
|
|
}
|
|
|
|
|
2014-05-08 01:31:59 +02:00
|
|
|
void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
|
2012-05-25 02:18:41 +02:00
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
|
|
|
|
a = queue_action(OP_DOWNLOAD_SPARSE, "");
|
|
|
|
a->data = s;
|
|
|
|
a->size = 0;
|
|
|
|
a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
|
|
|
|
|
|
|
|
a = queue_action(OP_COMMAND, "flash:%s", ptn);
|
|
|
|
a->msg = mkmsg("writing '%s'", ptn);
|
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int match(const char* str, const char** value, unsigned count) {
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
for (n = 0; n < count; n++) {
|
|
|
|
const char *val = value[n];
|
|
|
|
int len = strlen(val);
|
|
|
|
int match;
|
|
|
|
|
|
|
|
if ((len > 1) && (val[len-1] == '*')) {
|
|
|
|
len--;
|
|
|
|
match = !strncmp(val, str, len);
|
|
|
|
} else {
|
|
|
|
match = !strcmp(val, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match) return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_check(Action* a, int status, const char* resp, int invert)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-06-24 05:27:58 +02:00
|
|
|
const char** value = reinterpret_cast<const char**>(a->data);
|
2009-03-04 04:32:55 +01:00
|
|
|
unsigned count = a->size;
|
|
|
|
unsigned n;
|
|
|
|
int yes;
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
fprintf(stderr,"FAILED (%s)\n", resp);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-04-05 02:54:59 +02:00
|
|
|
if (a->prod) {
|
|
|
|
if (strcmp(a->prod, cur_product) != 0) {
|
|
|
|
double split = now();
|
|
|
|
fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
|
|
|
|
cur_product, a->prod, (split - a->start));
|
|
|
|
a->start = split;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
yes = match(resp, value, count);
|
|
|
|
if (invert) yes = !yes;
|
|
|
|
|
|
|
|
if (yes) {
|
2010-02-25 20:05:33 +01:00
|
|
|
double split = now();
|
|
|
|
fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
|
|
|
|
a->start = split;
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,"FAILED\n\n");
|
|
|
|
fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
|
|
|
|
fprintf(stderr,"Update %s '%s'",
|
|
|
|
invert ? "rejects" : "requires", value[0]);
|
|
|
|
for (n = 1; n < count; n++) {
|
|
|
|
fprintf(stderr," or '%s'", value[n]);
|
|
|
|
}
|
|
|
|
fprintf(stderr,".\n\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_require(Action*a, int status, const char* resp) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return cb_check(a, status, resp, 0);
|
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_reject(Action* a, int status, const char* resp) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return cb_check(a, status, resp, 1);
|
|
|
|
}
|
|
|
|
|
2011-04-05 02:54:59 +02:00
|
|
|
void fb_queue_require(const char *prod, const char *var,
|
2015-04-08 05:12:50 +02:00
|
|
|
bool invert, size_t nvalues, const char **value)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
a = queue_action(OP_QUERY, "getvar:%s", var);
|
2011-04-05 02:54:59 +02:00
|
|
|
a->prod = prod;
|
2009-03-04 04:32:55 +01:00
|
|
|
a->data = value;
|
|
|
|
a->size = nvalues;
|
|
|
|
a->msg = mkmsg("checking %s", var);
|
|
|
|
a->func = invert ? cb_reject : cb_require;
|
2015-06-24 05:27:58 +02:00
|
|
|
if (a->data == nullptr) die("out of memory");
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_display(Action* a, int status, const char* resp) {
|
2009-03-04 04:32:55 +01:00
|
|
|
if (status) {
|
|
|
|
fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_queue_display(const char *var, const char *prettyname)
|
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
a = queue_action(OP_QUERY, "getvar:%s", var);
|
|
|
|
a->data = strdup(prettyname);
|
2015-06-24 05:27:58 +02:00
|
|
|
if (a->data == nullptr) die("out of memory");
|
2009-03-04 04:32:55 +01:00
|
|
|
a->func = cb_display;
|
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_save(Action* a, int status, const char* resp) {
|
2011-04-05 02:54:59 +02:00
|
|
|
if (status) {
|
|
|
|
fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
|
|
|
|
return status;
|
|
|
|
}
|
2015-06-24 05:27:58 +02:00
|
|
|
strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
|
2011-04-05 02:54:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
|
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
a = queue_action(OP_QUERY, "getvar:%s", var);
|
|
|
|
a->data = (void *)dest;
|
|
|
|
a->size = dest_size;
|
|
|
|
a->func = cb_save;
|
|
|
|
}
|
|
|
|
|
2015-06-24 05:27:58 +02:00
|
|
|
static int cb_do_nothing(Action*, int , const char*) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fprintf(stderr,"\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_queue_reboot(void)
|
|
|
|
{
|
|
|
|
Action *a = queue_action(OP_COMMAND, "reboot");
|
|
|
|
a->func = cb_do_nothing;
|
|
|
|
a->msg = "rebooting";
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_queue_command(const char *cmd, const char *msg)
|
|
|
|
{
|
|
|
|
Action *a = queue_action(OP_COMMAND, cmd);
|
|
|
|
a->msg = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_queue_download(const char *name, void *data, unsigned size)
|
|
|
|
{
|
|
|
|
Action *a = queue_action(OP_DOWNLOAD, "");
|
|
|
|
a->data = data;
|
|
|
|
a->size = size;
|
|
|
|
a->msg = mkmsg("downloading '%s'", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fb_queue_notice(const char *notice)
|
|
|
|
{
|
|
|
|
Action *a = queue_action(OP_NOTICE, "");
|
|
|
|
a->data = (void*) notice;
|
|
|
|
}
|
|
|
|
|
2013-10-02 15:35:38 +02:00
|
|
|
void fb_queue_wait_for_disconnect(void)
|
|
|
|
{
|
|
|
|
queue_action(OP_WAIT_FOR_DISCONNECT, "");
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
int fb_execute_queue(Transport* transport)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
Action *a;
|
|
|
|
char resp[FB_RESPONSE_SZ+1];
|
2010-04-23 21:38:51 +02:00
|
|
|
int status = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
a = action_list;
|
2012-04-06 21:39:30 +02:00
|
|
|
if (!a)
|
|
|
|
return status;
|
2009-03-04 04:32:55 +01:00
|
|
|
resp[FB_RESPONSE_SZ] = 0;
|
|
|
|
|
2010-02-25 20:05:33 +01:00
|
|
|
double start = -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
for (a = action_list; a; a = a->next) {
|
2010-02-25 20:05:33 +01:00
|
|
|
a->start = now();
|
|
|
|
if (start < 0) start = a->start;
|
2009-03-04 04:32:55 +01:00
|
|
|
if (a->msg) {
|
2010-06-28 20:14:26 +02:00
|
|
|
// fprintf(stderr,"%30s... ",a->msg);
|
|
|
|
fprintf(stderr,"%s...\n",a->msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
if (a->op == OP_DOWNLOAD) {
|
2015-10-30 19:22:01 +01:00
|
|
|
status = fb_download_data(transport, a->data, a->size);
|
2009-03-04 04:32:55 +01:00
|
|
|
status = a->func(a, status, status ? fb_get_error() : "");
|
|
|
|
if (status) break;
|
|
|
|
} else if (a->op == OP_COMMAND) {
|
2015-10-30 19:22:01 +01:00
|
|
|
status = fb_command(transport, a->cmd);
|
2009-03-04 04:32:55 +01:00
|
|
|
status = a->func(a, status, status ? fb_get_error() : "");
|
|
|
|
if (status) break;
|
|
|
|
} else if (a->op == OP_QUERY) {
|
2015-10-30 19:22:01 +01:00
|
|
|
status = fb_command_response(transport, a->cmd, resp);
|
2009-03-04 04:32:55 +01:00
|
|
|
status = a->func(a, status, status ? fb_get_error() : resp);
|
|
|
|
if (status) break;
|
|
|
|
} else if (a->op == OP_NOTICE) {
|
|
|
|
fprintf(stderr,"%s\n",(char*)a->data);
|
2012-05-25 02:18:41 +02:00
|
|
|
} else if (a->op == OP_DOWNLOAD_SPARSE) {
|
2015-10-30 19:22:01 +01:00
|
|
|
status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data));
|
2012-05-25 02:18:41 +02:00
|
|
|
status = a->func(a, status, status ? fb_get_error() : "");
|
|
|
|
if (status) break;
|
2013-10-02 15:35:38 +02:00
|
|
|
} else if (a->op == OP_WAIT_FOR_DISCONNECT) {
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->WaitForDisconnect();
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
|
|
|
die("bogus action");
|
|
|
|
}
|
|
|
|
}
|
2010-02-25 20:05:33 +01:00
|
|
|
|
|
|
|
fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
|
2010-04-23 21:38:51 +02:00
|
|
|
return status;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|