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.
|
|
|
|
*/
|
|
|
|
|
2012-05-25 02:18:41 +02:00
|
|
|
#define round_down(a, b) \
|
|
|
|
({ typeof(a) _a = (a); typeof(b) _b = (b); _a - (_a % _b); })
|
|
|
|
|
2017-01-27 23:17:56 +01:00
|
|
|
#include <fcntl.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
#include <algorithm>
|
2017-01-27 23:17:56 +01:00
|
|
|
#include <vector>
|
2015-04-08 05:12:50 +02:00
|
|
|
|
2017-01-27 23:17:56 +01:00
|
|
|
#include <android-base/file.h>
|
2016-04-25 23:31:18 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-01-27 23:17:56 +01:00
|
|
|
#include <android-base/unique_fd.h>
|
2012-05-25 02:18:41 +02:00
|
|
|
#include <sparse/sparse.h>
|
2017-04-12 17:25:57 +02:00
|
|
|
#include <utils/FileMap.h>
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
#include "fastboot.h"
|
2015-10-30 19:22:01 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2016-04-25 23:31:18 +02:00
|
|
|
static std::string g_error;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-01-27 23:17:56 +01:00
|
|
|
using android::base::unique_fd;
|
|
|
|
using android::base::WriteStringToFile;
|
|
|
|
|
2016-04-25 23:31:18 +02:00
|
|
|
const std::string fb_get_error() {
|
|
|
|
return g_error;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-04-04 16:52:47 +02:00
|
|
|
static int64_t check_response(Transport* transport, uint32_t size, char* response) {
|
2015-04-08 05:12:50 +02:00
|
|
|
char status[65];
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
while (true) {
|
2015-10-30 19:22:01 +01:00
|
|
|
int r = transport->Read(status, 64);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (r < 0) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = android::base::StringPrintf("status read failed (%s)", strerror(errno));
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
status[r] = 0;
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (r < 4) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = android::base::StringPrintf("status malformed (%d bytes)", r);
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (!memcmp(status, "INFO", 4)) {
|
2010-06-28 20:14:26 +02:00
|
|
|
fprintf(stderr,"(bootloader) %s\n", status + 4);
|
2009-03-04 04:32:55 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (!memcmp(status, "OKAY", 4)) {
|
|
|
|
if (response) {
|
2009-03-04 04:32:55 +01:00
|
|
|
strcpy(response, (char*) status + 4);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (!memcmp(status, "FAIL", 4)) {
|
|
|
|
if (r > 4) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = android::base::StringPrintf("remote: %s", status + 4);
|
2009-03-04 04:32:55 +01:00
|
|
|
} else {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = "remote failure";
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (!memcmp(status, "DATA", 4) && size > 0){
|
|
|
|
uint32_t dsize = strtol(status + 4, 0, 16);
|
|
|
|
if (dsize > size) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = android::base::StringPrintf("data size too large (%d)", dsize);
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return dsize;
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = "unknown status code";
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2009-03-04 04:32:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:52:47 +02:00
|
|
|
static int64_t _command_start(Transport* transport, const char* cmd, uint32_t size, char* response) {
|
2015-04-08 05:12:50 +02:00
|
|
|
size_t cmdsize = strlen(cmd);
|
|
|
|
if (cmdsize > 64) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = android::base::StringPrintf("command too large (%zu)", cmdsize);
|
2015-04-08 05:12:50 +02:00
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 05:12:50 +02:00
|
|
|
if (response) {
|
|
|
|
response[0] = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (transport->Write(cmd, cmdsize) != static_cast<int>(cmdsize)) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = android::base::StringPrintf("command write failed (%s)", strerror(errno));
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
return check_response(transport, size, response);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2017-01-27 23:17:56 +01:00
|
|
|
static int64_t _command_write_data(Transport* transport, const void* data, uint32_t size) {
|
2017-04-04 16:52:47 +02:00
|
|
|
int64_t r = transport->Write(data, size);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (r < 0) {
|
2017-01-27 23:17:56 +01:00
|
|
|
g_error = android::base::StringPrintf("data write failure (%s)", strerror(errno));
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2012-05-25 02:18:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2017-04-04 16:52:47 +02:00
|
|
|
if (r != static_cast<int64_t>(size)) {
|
2017-01-27 23:17:56 +01:00
|
|
|
g_error = "data write failure (short transfer)";
|
|
|
|
transport->Close();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t _command_read_data(Transport* transport, void* data, uint32_t size) {
|
|
|
|
int64_t r = transport->Read(data, size);
|
|
|
|
if (r < 0) {
|
|
|
|
g_error = android::base::StringPrintf("data read failure (%s)", strerror(errno));
|
|
|
|
transport->Close();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (r != (static_cast<int64_t>(size))) {
|
|
|
|
g_error = "data read failure (short transfer)";
|
2015-10-30 19:22:01 +01:00
|
|
|
transport->Close();
|
2012-05-25 02:18:41 +02:00
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2012-05-25 02:18:41 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-04-04 16:52:47 +02:00
|
|
|
static int64_t _command_end(Transport* transport) {
|
2015-10-30 19:22:01 +01:00
|
|
|
return check_response(transport, 0, 0) < 0 ? -1 : 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2017-04-04 16:52:47 +02:00
|
|
|
static int64_t _command_send(Transport* transport, const char* cmd, const void* data, uint32_t size,
|
2015-04-08 05:12:50 +02:00
|
|
|
char* response) {
|
2012-05-25 02:18:41 +02:00
|
|
|
if (size == 0) {
|
|
|
|
return -1;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2012-02-28 16:21:08 +01:00
|
|
|
|
2017-04-04 16:52:47 +02:00
|
|
|
int64_t r = _command_start(transport, cmd, size, response);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (r < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-01-27 23:17:56 +01:00
|
|
|
r = _command_write_data(transport, data, size);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (r < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
r = _command_end(transport);
|
2015-04-08 05:12:50 +02:00
|
|
|
if (r < 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-05-25 02:18:41 +02:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:25:57 +02:00
|
|
|
static int64_t _command_send_fd(Transport* transport, const char* cmd, int fd, uint32_t size,
|
|
|
|
char* response) {
|
|
|
|
static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024;
|
|
|
|
off64_t offset = 0;
|
|
|
|
uint32_t remaining = size;
|
|
|
|
|
|
|
|
if (_command_start(transport, cmd, size, response) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (remaining) {
|
|
|
|
android::FileMap filemap;
|
|
|
|
size_t len = std::min(remaining, MAX_MAP_SIZE);
|
|
|
|
|
|
|
|
if (!filemap.create(NULL, fd, offset, len, true)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-27 23:17:56 +01:00
|
|
|
if (_command_write_data(transport, filemap.getDataPtr(), len) < 0) {
|
2017-04-12 17:25:57 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining -= len;
|
|
|
|
offset += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_command_end(transport) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static int _command_send_no_data(Transport* transport, const char* cmd, char* response) {
|
|
|
|
return _command_start(transport, cmd, 0, response);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
int fb_command(Transport* transport, const char* cmd) {
|
|
|
|
return _command_send_no_data(transport, cmd, 0);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
int fb_command_response(Transport* transport, const char* cmd, char* response) {
|
|
|
|
return _command_send_no_data(transport, cmd, response);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-04-04 16:52:47 +02:00
|
|
|
int64_t fb_download_data(Transport* transport, const void* data, uint32_t size) {
|
2017-04-12 17:25:57 +02:00
|
|
|
std::string cmd(android::base::StringPrintf("download:%08x", size));
|
|
|
|
return _command_send(transport, cmd.c_str(), data, size, 0) < 0 ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t fb_download_data_fd(Transport* transport, int fd, uint32_t size) {
|
|
|
|
std::string cmd(android::base::StringPrintf("download:%08x", size));
|
|
|
|
return _command_send_fd(transport, cmd.c_str(), fd, size, 0) < 0 ? -1 : 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
2017-01-27 23:17:56 +01:00
|
|
|
int64_t fb_upload_data(Transport* transport, const char* outfile) {
|
|
|
|
// positive return value is the upload size sent by the device
|
|
|
|
int64_t r = _command_start(transport, "upload", std::numeric_limits<int32_t>::max(), nullptr);
|
|
|
|
if (r <= 0) {
|
|
|
|
g_error = android::base::StringPrintf("command start failed (%s)", strerror(errno));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string data;
|
|
|
|
data.resize(r);
|
|
|
|
if ((r = _command_read_data(transport, &data[0], data.size())) == -1) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WriteStringToFile(data, outfile, true)) {
|
|
|
|
g_error = android::base::StringPrintf("write to '%s' failed", outfile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _command_end(transport);
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
#define TRANSPORT_BUF_SIZE 1024
|
|
|
|
static char transport_buf[TRANSPORT_BUF_SIZE];
|
|
|
|
static int transport_buf_len;
|
2012-05-25 02:18:41 +02:00
|
|
|
|
|
|
|
static int fb_download_data_sparse_write(void *priv, const void *data, int len)
|
|
|
|
{
|
|
|
|
int r;
|
2015-10-30 19:22:01 +01:00
|
|
|
Transport* transport = reinterpret_cast<Transport*>(priv);
|
2012-05-25 02:18:41 +02:00
|
|
|
int to_write;
|
2015-06-24 05:27:58 +02:00
|
|
|
const char* ptr = reinterpret_cast<const char*>(data);
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (transport_buf_len) {
|
|
|
|
to_write = std::min(TRANSPORT_BUF_SIZE - transport_buf_len, len);
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
memcpy(transport_buf + transport_buf_len, ptr, to_write);
|
|
|
|
transport_buf_len += to_write;
|
2012-05-25 02:18:41 +02:00
|
|
|
ptr += to_write;
|
|
|
|
len -= to_write;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (transport_buf_len == TRANSPORT_BUF_SIZE) {
|
2017-01-27 23:17:56 +01:00
|
|
|
r = _command_write_data(transport, transport_buf, TRANSPORT_BUF_SIZE);
|
2015-10-30 19:22:01 +01:00
|
|
|
if (r != TRANSPORT_BUF_SIZE) {
|
2012-05-25 02:18:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-30 19:22:01 +01:00
|
|
|
transport_buf_len = 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
if (len > TRANSPORT_BUF_SIZE) {
|
|
|
|
if (transport_buf_len > 0) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = "internal error: transport_buf not empty";
|
2012-05-25 02:18:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-30 19:22:01 +01:00
|
|
|
to_write = round_down(len, TRANSPORT_BUF_SIZE);
|
2017-01-27 23:17:56 +01:00
|
|
|
r = _command_write_data(transport, ptr, to_write);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (r != to_write) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ptr += to_write;
|
|
|
|
len -= to_write;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0) {
|
2015-10-30 19:22:01 +01:00
|
|
|
if (len > TRANSPORT_BUF_SIZE) {
|
2016-04-25 23:31:18 +02:00
|
|
|
g_error = "internal error: too much left for transport_buf";
|
2012-05-25 02:18:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-30 19:22:01 +01:00
|
|
|
memcpy(transport_buf, ptr, len);
|
|
|
|
transport_buf_len = len;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
static int fb_download_data_sparse_flush(Transport* transport) {
|
|
|
|
if (transport_buf_len > 0) {
|
2017-01-27 23:17:56 +01:00
|
|
|
int64_t r = _command_write_data(transport, transport_buf, transport_buf_len);
|
2017-04-04 16:52:47 +02:00
|
|
|
if (r != static_cast<int64_t>(transport_buf_len)) {
|
2012-05-25 02:18:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-30 19:22:01 +01:00
|
|
|
transport_buf_len = 0;
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
int fb_download_data_sparse(Transport* transport, struct sparse_file* s) {
|
2012-05-25 02:18:41 +02:00
|
|
|
int size = sparse_file_len(s, true, false);
|
|
|
|
if (size <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:25:57 +02:00
|
|
|
std::string cmd(android::base::StringPrintf("download:%08x", size));
|
|
|
|
int r = _command_start(transport, cmd.c_str(), size, 0);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (r < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
r = sparse_file_callback(s, true, false, fb_download_data_sparse_write, transport);
|
2012-05-25 02:18:41 +02:00
|
|
|
if (r < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
r = fb_download_data_sparse_flush(transport);
|
2015-02-22 10:47:16 +01:00
|
|
|
if (r < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2012-05-25 02:18:41 +02:00
|
|
|
|
2015-10-30 19:22:01 +01:00
|
|
|
return _command_end(transport);
|
2012-05-25 02:18:41 +02:00
|
|
|
}
|