Merge "Kill load_file." am: b42e4a6b63
am: 198a727891
* commit '198a727891aef33469d82b263fc222e4f4335b1b':
Kill load_file.
Change-Id: I81d715153b8b882ace9deb073abfc4dde5535811
This commit is contained in:
commit
500c7e5bc7
7 changed files with 46 additions and 129 deletions
|
|
@ -72,19 +72,23 @@ void send_auth_response(uint8_t *token, size_t token_size, atransport *t)
|
||||||
void send_auth_publickey(atransport *t)
|
void send_auth_publickey(atransport *t)
|
||||||
{
|
{
|
||||||
D("Calling send_auth_publickey");
|
D("Calling send_auth_publickey");
|
||||||
apacket *p = get_apacket();
|
std::string key = adb_auth_get_userkey();
|
||||||
int ret;
|
if (key.empty()) {
|
||||||
|
|
||||||
ret = adb_auth_get_userkey(p->data, MAX_PAYLOAD_V1);
|
|
||||||
if (!ret) {
|
|
||||||
D("Failed to get user public key");
|
D("Failed to get user public key");
|
||||||
put_apacket(p);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (key.size() >= MAX_PAYLOAD_V1) {
|
||||||
|
D("User public key too large (%zu B)", key.size());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
apacket* p = get_apacket();
|
||||||
|
memcpy(p->data, key.c_str(), key.size() + 1);
|
||||||
|
|
||||||
p->msg.command = A_AUTH;
|
p->msg.command = A_AUTH;
|
||||||
p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
|
p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
|
||||||
p->msg.data_length = ret;
|
p->msg.data_length = key.size();
|
||||||
send_packet(p, t);
|
send_packet(p, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ void adb_auth_init(void);
|
||||||
int adb_auth_sign(void *key, const unsigned char* token, size_t token_size,
|
int adb_auth_sign(void *key, const unsigned char* token, size_t token_size,
|
||||||
unsigned char* sig);
|
unsigned char* sig);
|
||||||
void *adb_auth_nextkey(void *current);
|
void *adb_auth_nextkey(void *current);
|
||||||
int adb_auth_get_userkey(unsigned char *data, size_t len);
|
std::string adb_auth_get_userkey();
|
||||||
|
|
||||||
static inline int adb_auth_generate_token(void *token, size_t token_size) {
|
static inline int adb_auth_generate_token(void *token, size_t token_size) {
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -60,9 +60,7 @@ static inline int adb_auth_sign(void* key, const unsigned char* token,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
static inline void *adb_auth_nextkey(void *current) { return NULL; }
|
static inline void *adb_auth_nextkey(void *current) { return NULL; }
|
||||||
static inline int adb_auth_get_userkey(unsigned char *data, size_t len) {
|
static inline std::string adb_auth_get_userkey() { return ""; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void adbd_auth_init(void);
|
void adbd_auth_init(void);
|
||||||
void adbd_cloexec_auth_socket();
|
void adbd_cloexec_auth_socket();
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@
|
||||||
#include "adb.h"
|
#include "adb.h"
|
||||||
|
|
||||||
#include <android-base/errors.h>
|
#include <android-base/errors.h>
|
||||||
|
#include <android-base/file.h>
|
||||||
#include <android-base/stringprintf.h>
|
#include <android-base/stringprintf.h>
|
||||||
#include <android-base/strings.h>
|
#include <android-base/strings.h>
|
||||||
#include <crypto_utils/android_pubkey.h>
|
#include <crypto_utils/android_pubkey.h>
|
||||||
|
|
@ -334,39 +335,21 @@ void *adb_auth_nextkey(void *current)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int adb_auth_get_userkey(unsigned char *data, size_t len)
|
std::string adb_auth_get_userkey() {
|
||||||
{
|
|
||||||
char path[PATH_MAX];
|
char path[PATH_MAX];
|
||||||
int ret = get_user_keyfilepath(path, sizeof(path) - 4);
|
int ret = get_user_keyfilepath(path, sizeof(path) - 4);
|
||||||
if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
|
if (ret < 0 || ret >= (signed)(sizeof(path) - 4)) {
|
||||||
D("Error getting user key filename");
|
D("Error getting user key filename");
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
strcat(path, ".pub");
|
strcat(path, ".pub");
|
||||||
|
|
||||||
// TODO(danalbert): ReadFileToString
|
std::string content;
|
||||||
// Note that on Windows, load_file() does not do CR/LF translation, but
|
if (!android::base::ReadFileToString(path, &content)) {
|
||||||
// ReadFileToString() uses the C Runtime which uses CR/LF translation by
|
|
||||||
// default (by is overridable with _setmode()).
|
|
||||||
unsigned size;
|
|
||||||
char* file_data = reinterpret_cast<char*>(load_file(path, &size));
|
|
||||||
if (file_data == nullptr) {
|
|
||||||
D("Can't load '%s'", path);
|
D("Can't load '%s'", path);
|
||||||
return 0;
|
return "";
|
||||||
}
|
}
|
||||||
|
return content;
|
||||||
if (len < (size_t)(size + 1)) {
|
|
||||||
D("%s: Content too large ret=%d", path, size);
|
|
||||||
free(file_data);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(data, file_data, size);
|
|
||||||
free(file_data);
|
|
||||||
file_data = nullptr;
|
|
||||||
data[size] = '\0';
|
|
||||||
|
|
||||||
return size + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int adb_auth_keygen(const char* filename) {
|
int adb_auth_keygen(const char* filename) {
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,7 @@ struct AdbCloser {
|
||||||
|
|
||||||
using unique_fd = android::base::unique_fd_impl<AdbCloser>;
|
using unique_fd = android::base::unique_fd_impl<AdbCloser>;
|
||||||
|
|
||||||
|
// TODO: switch remaining users over to unique_fd...
|
||||||
class ScopedFd {
|
class ScopedFd {
|
||||||
public:
|
public:
|
||||||
ScopedFd() {
|
ScopedFd() {
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <android-base/file.h>
|
||||||
#include <android-base/logging.h>
|
#include <android-base/logging.h>
|
||||||
#include <android-base/stringprintf.h>
|
#include <android-base/stringprintf.h>
|
||||||
#include <android-base/strings.h>
|
#include <android-base/strings.h>
|
||||||
|
|
@ -879,47 +880,47 @@ static int adb_download_buffer(const char *service, const char *fn, const void*
|
||||||
* we hang up.
|
* we hang up.
|
||||||
*/
|
*/
|
||||||
static int adb_sideload_host(const char* fn) {
|
static int adb_sideload_host(const char* fn) {
|
||||||
unsigned sz;
|
|
||||||
size_t xfer = 0;
|
|
||||||
int status;
|
|
||||||
int last_percent = -1;
|
|
||||||
int opt = SIDELOAD_HOST_BLOCK_SIZE;
|
|
||||||
|
|
||||||
printf("loading: '%s'", fn);
|
printf("loading: '%s'", fn);
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
uint8_t* data = reinterpret_cast<uint8_t*>(load_file(fn, &sz));
|
|
||||||
if (data == 0) {
|
std::string content;
|
||||||
|
if (!android::base::ReadFileToString(fn, &content)) {
|
||||||
printf("\n");
|
printf("\n");
|
||||||
fprintf(stderr, "* cannot read '%s' *\n", fn);
|
fprintf(stderr, "* cannot read '%s' *\n", fn);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const uint8_t* data = reinterpret_cast<const uint8_t*>(content.data());
|
||||||
|
unsigned sz = content.size();
|
||||||
|
|
||||||
std::string service =
|
std::string service =
|
||||||
android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
|
android::base::StringPrintf("sideload-host:%d:%d", sz, SIDELOAD_HOST_BLOCK_SIZE);
|
||||||
std::string error;
|
std::string error;
|
||||||
int fd = adb_connect(service, &error);
|
unique_fd fd(adb_connect(service, &error));
|
||||||
if (fd < 0) {
|
if (fd >= 0) {
|
||||||
// Try falling back to the older sideload method. Maybe this
|
// Try falling back to the older sideload method. Maybe this
|
||||||
// is an older device that doesn't support sideload-host.
|
// is an older device that doesn't support sideload-host.
|
||||||
printf("\n");
|
printf("\n");
|
||||||
status = adb_download_buffer("sideload", fn, data, sz, true);
|
return adb_download_buffer("sideload", fn, data, sz, true);
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt));
|
int opt = SIDELOAD_HOST_BLOCK_SIZE;
|
||||||
|
adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
|
||||||
|
|
||||||
|
size_t xfer = 0;
|
||||||
|
int last_percent = -1;
|
||||||
while (true) {
|
while (true) {
|
||||||
char buf[9];
|
char buf[9];
|
||||||
if (!ReadFdExactly(fd, buf, 8)) {
|
if (!ReadFdExactly(fd, buf, 8)) {
|
||||||
fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
|
fprintf(stderr, "* failed to read command: %s\n", strerror(errno));
|
||||||
status = -1;
|
return -1;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
buf[8] = '\0';
|
buf[8] = '\0';
|
||||||
|
|
||||||
if (strcmp("DONEDONE", buf) == 0) {
|
if (strcmp("DONEDONE", buf) == 0) {
|
||||||
status = 0;
|
printf("\rTotal xfer: %.2fx%*s\n",
|
||||||
break;
|
(double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int block = strtol(buf, NULL, 10);
|
int block = strtol(buf, NULL, 10);
|
||||||
|
|
@ -927,21 +928,19 @@ static int adb_sideload_host(const char* fn) {
|
||||||
size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
|
size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
|
||||||
if (offset >= sz) {
|
if (offset >= sz) {
|
||||||
fprintf(stderr, "* attempt to read block %d past end\n", block);
|
fprintf(stderr, "* attempt to read block %d past end\n", block);
|
||||||
status = -1;
|
return -1;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
uint8_t* start = data + offset;
|
const uint8_t* start = data + offset;
|
||||||
size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
|
size_t offset_end = offset + SIDELOAD_HOST_BLOCK_SIZE;
|
||||||
size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
|
size_t to_write = SIDELOAD_HOST_BLOCK_SIZE;
|
||||||
if (offset_end > sz) {
|
if (offset_end > sz) {
|
||||||
to_write = sz - offset;
|
to_write = sz - offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!WriteFdExactly(fd, start, to_write)) {
|
if (!WriteFdExactly(fd, start, to_write)) {
|
||||||
adb_status(fd, &error);
|
adb_status(fd, &error);
|
||||||
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
|
fprintf(stderr,"* failed to write data '%s' *\n", error.c_str());
|
||||||
status = -1;
|
return -1;
|
||||||
goto done;
|
|
||||||
}
|
}
|
||||||
xfer += to_write;
|
xfer += to_write;
|
||||||
|
|
||||||
|
|
@ -958,13 +957,6 @@ static int adb_sideload_host(const char* fn) {
|
||||||
last_percent = percent;
|
last_percent = percent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\rTotal xfer: %.2fx%*s\n", (double)xfer / (sz ? sz : 1), (int)strlen(fn)+10, "");
|
|
||||||
|
|
||||||
done:
|
|
||||||
if (fd >= 0) adb_close(fd);
|
|
||||||
free(data);
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1075,10 +1067,9 @@ static bool wait_for_device(const char* service, TransportType t, const char* se
|
||||||
|
|
||||||
static bool adb_root(const char* command) {
|
static bool adb_root(const char* command) {
|
||||||
std::string error;
|
std::string error;
|
||||||
ScopedFd fd;
|
|
||||||
|
|
||||||
fd.Reset(adb_connect(android::base::StringPrintf("%s:", command), &error));
|
unique_fd fd(adb_connect(android::base::StringPrintf("%s:", command), &error));
|
||||||
if (!fd.valid()) {
|
if (fd < 0) {
|
||||||
fprintf(stderr, "adb: unable to connect for %s: %s\n", command, error.c_str());
|
fprintf(stderr, "adb: unable to connect for %s: %s\n", command, error.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -1088,7 +1079,7 @@ static bool adb_root(const char* command) {
|
||||||
char* cur = buf;
|
char* cur = buf;
|
||||||
ssize_t bytes_left = sizeof(buf);
|
ssize_t bytes_left = sizeof(buf);
|
||||||
while (bytes_left > 0) {
|
while (bytes_left > 0) {
|
||||||
ssize_t bytes_read = adb_read(fd.fd(), cur, bytes_left);
|
ssize_t bytes_read = adb_read(fd, cur, bytes_left);
|
||||||
if (bytes_read == 0) {
|
if (bytes_read == 0) {
|
||||||
break;
|
break;
|
||||||
} else if (bytes_read < 0) {
|
} else if (bytes_read < 0) {
|
||||||
|
|
|
||||||
|
|
@ -269,9 +269,6 @@ extern int unix_open(const char* path, int options, ...);
|
||||||
int unix_isatty(int fd);
|
int unix_isatty(int fd);
|
||||||
#define isatty ___xxx_isatty
|
#define isatty ___xxx_isatty
|
||||||
|
|
||||||
/* normally provided by <cutils/misc.h> */
|
|
||||||
extern void* load_file(const char* pathname, unsigned* psize);
|
|
||||||
|
|
||||||
static __inline__ void adb_sleep_ms( int mseconds )
|
static __inline__ void adb_sleep_ms( int mseconds )
|
||||||
{
|
{
|
||||||
Sleep( mseconds );
|
Sleep( mseconds );
|
||||||
|
|
@ -458,7 +455,6 @@ size_t ParseCompleteUTF8(const char* first, const char* last, std::vector<char>*
|
||||||
|
|
||||||
#else /* !_WIN32 a.k.a. Unix */
|
#else /* !_WIN32 a.k.a. Unix */
|
||||||
|
|
||||||
#include <cutils/misc.h>
|
|
||||||
#include <cutils/sockets.h>
|
#include <cutils/sockets.h>
|
||||||
#include <cutils/threads.h>
|
#include <cutils/threads.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
|
||||||
|
|
@ -107,62 +107,6 @@ void handle_deleter::operator()(HANDLE h) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************/
|
|
||||||
/**************************************************************************/
|
|
||||||
/***** *****/
|
|
||||||
/***** replaces libs/cutils/load_file.c *****/
|
|
||||||
/***** *****/
|
|
||||||
/**************************************************************************/
|
|
||||||
/**************************************************************************/
|
|
||||||
|
|
||||||
void *load_file(const char *fn, unsigned *_sz)
|
|
||||||
{
|
|
||||||
HANDLE file;
|
|
||||||
char *data;
|
|
||||||
DWORD file_size;
|
|
||||||
|
|
||||||
std::wstring fn_wide;
|
|
||||||
if (!android::base::UTF8ToWide(fn, &fn_wide))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
file = CreateFileW( fn_wide.c_str(),
|
|
||||||
GENERIC_READ,
|
|
||||||
FILE_SHARE_READ,
|
|
||||||
NULL,
|
|
||||||
OPEN_EXISTING,
|
|
||||||
0,
|
|
||||||
NULL );
|
|
||||||
|
|
||||||
if (file == INVALID_HANDLE_VALUE)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
file_size = GetFileSize( file, NULL );
|
|
||||||
data = NULL;
|
|
||||||
|
|
||||||
if (file_size > 0) {
|
|
||||||
data = (char*) malloc( file_size + 1 );
|
|
||||||
if (data == NULL) {
|
|
||||||
D("load_file: could not allocate %ld bytes", file_size );
|
|
||||||
file_size = 0;
|
|
||||||
} else {
|
|
||||||
DWORD out_bytes;
|
|
||||||
|
|
||||||
if ( !ReadFile( file, data, file_size, &out_bytes, NULL ) ||
|
|
||||||
out_bytes != file_size )
|
|
||||||
{
|
|
||||||
D("load_file: could not read %ld bytes from '%s'", file_size, fn);
|
|
||||||
free(data);
|
|
||||||
data = NULL;
|
|
||||||
file_size = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
CloseHandle( file );
|
|
||||||
|
|
||||||
*_sz = (unsigned) file_size;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
/***** *****/
|
/***** *****/
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue