Merge "fastboot: Refactor string constants to constants.h"

This commit is contained in:
Treehugger Robot 2018-07-16 23:48:49 +00:00 committed by Gerrit Code Review
commit fcc16c53e1
4 changed files with 87 additions and 31 deletions

51
fastboot/constants.h Normal file
View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 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.
*/
#pragma once
#define FB_CMD_GETVAR "getvar"
#define FB_CMD_DOWNLOAD "download"
#define FB_CMD_UPLOAD "upload"
#define FB_CMD_VERIFY "verify"
#define FB_CMD_FLASH "flash"
#define FB_CMD_ERASE "erase"
#define FB_CMD_BOOT "boot"
#define FB_CMD_SET_ACTIVE "set_active"
#define FB_CMD_CONTINUE "continue"
#define FB_CMD_REBOOT "reboot"
#define FB_CMD_SHUTDOWN "shutdown"
#define FB_CMD_REBOOT_BOOTLOADER "reboot-bootloader"
#define FB_CMD_POWERDOWN "powerdown"
#define RESPONSE_OKAY "OKAY"
#define RESPONSE_FAIL "FAIL"
#define RESPONSE_DATA "DATA"
#define RESPONSE_INFO "INFO"
#define FB_COMMAND_SZ 64
#define FB_RESPONSE_SZ 64
#define FB_VAR_VERSION "version"
#define FB_VAR_VERSION_BOOTLOADER "version-bootloader"
#define FB_VAR_VERSION_BASEBAND "version-baseband"
#define FB_VAR_PRODUCT "product"
#define FB_VAR_SERIALNO "serialno"
#define FB_VAR_SECURE "secure"
#define FB_VAR_UNLOCKED "unlocked"
#define FB_VAR_CURRENT_SLOT "current-slot"
#define FB_VAR_MAX_DOWNLOAD_SIZE "max-download-size"
#define FB_VAR_HAS_SLOT "has-slot"
#define FB_VAR_SLOT_COUNT "slot-count"
#define FB_VAR_PARTITION_SIZE "partition-size"

View file

@ -42,6 +42,7 @@
#include <android-base/stringprintf.h>
#include "constants.h"
#include "transport.h"
enum Op {
@ -79,7 +80,7 @@ struct Action {
static std::vector<std::unique_ptr<Action>> action_list;
bool fb_getvar(Transport* transport, const std::string& key, std::string* value) {
std::string cmd = "getvar:" + key;
std::string cmd = FB_CMD_GETVAR ":" + key;
char buf[FB_RESPONSE_SZ + 1];
memset(buf, 0, sizeof(buf));
@ -110,12 +111,12 @@ static Action& queue_action(Op op, const std::string& cmd) {
}
void fb_set_active(const std::string& slot) {
Action& a = queue_action(OP_COMMAND, "set_active:" + slot);
Action& a = queue_action(OP_COMMAND, FB_CMD_SET_ACTIVE ":" + slot);
a.msg = "Setting current slot to '" + slot + "'";
}
void fb_queue_erase(const std::string& partition) {
Action& a = queue_action(OP_COMMAND, "erase:" + partition);
Action& a = queue_action(OP_COMMAND, FB_CMD_ERASE ":" + partition);
a.msg = "Erasing '" + partition + "'";
}
@ -125,7 +126,7 @@ void fb_queue_flash_fd(const std::string& partition, int fd, uint32_t sz) {
a.size = sz;
a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024);
Action& b = queue_action(OP_COMMAND, "flash:" + partition);
Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition);
b.msg = "Writing '" + partition + "'";
}
@ -135,7 +136,7 @@ void fb_queue_flash(const std::string& partition, void* data, uint32_t sz) {
a.size = sz;
a.msg = android::base::StringPrintf("Sending '%s' (%u KB)", partition.c_str(), sz / 1024);
Action& b = queue_action(OP_COMMAND, "flash:" + partition);
Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition);
b.msg = "Writing '" + partition + "'";
}
@ -147,7 +148,7 @@ void fb_queue_flash_sparse(const std::string& partition, struct sparse_file* s,
a.msg = android::base::StringPrintf("Sending sparse '%s' %zu/%zu (%u KB)", partition.c_str(),
current, total, sz / 1024);
Action& b = queue_action(OP_COMMAND, "flash:" + partition);
Action& b = queue_action(OP_COMMAND, FB_CMD_FLASH ":" + partition);
b.msg = android::base::StringPrintf("Writing sparse '%s' %zu/%zu", partition.c_str(), current,
total);
}
@ -223,7 +224,7 @@ static int cb_reject(Action& a, int status, const char* resp) {
void fb_queue_require(const std::string& product, const std::string& var, bool invert,
size_t nvalues, const char** values) {
Action& a = queue_action(OP_QUERY, "getvar:" + var);
Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var);
a.product = product;
a.data = values;
a.size = nvalues;
@ -243,7 +244,7 @@ static int cb_display(Action& a, int status, const char* resp) {
}
void fb_queue_display(const std::string& label, const std::string& var) {
Action& a = queue_action(OP_QUERY, "getvar:" + var);
Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var);
a.data = xstrdup(label.c_str());
a.func = cb_display;
}
@ -258,7 +259,7 @@ static int cb_save(Action& a, int status, const char* resp) {
}
void fb_queue_query_save(const std::string& var, char* dest, uint32_t dest_size) {
Action& a = queue_action(OP_QUERY, "getvar:" + var);
Action& a = queue_action(OP_QUERY, FB_CMD_GETVAR ":" + var);
a.data = dest;
a.size = dest_size;
a.func = cb_save;
@ -270,7 +271,7 @@ static int cb_do_nothing(Action&, int, const char*) {
}
void fb_queue_reboot() {
Action& a = queue_action(OP_COMMAND, "reboot");
Action& a = queue_action(OP_COMMAND, FB_CMD_REBOOT);
a.func = cb_do_nothing;
a.msg = "Rebooting";
}

View file

@ -35,6 +35,8 @@
#include <bootimg.h>
#include "constants.h"
class Transport;
struct sparse_file;
@ -47,9 +49,6 @@ int fb_download_data_sparse(Transport* transport, struct sparse_file* s);
int64_t fb_upload_data(Transport* transport, const char* outfile);
const std::string fb_get_error();
#define FB_COMMAND_SZ 64
#define FB_RESPONSE_SZ 64
/* engine.c - high level command queue engine */
bool fb_getvar(Transport* transport, const std::string& key, std::string* value);
void fb_queue_flash(const std::string& partition, void* data, uint32_t sz);

View file

@ -44,6 +44,7 @@
#include <sparse/sparse.h>
#include <utils/FileMap.h>
#include "constants.h"
#include "fastboot.h"
#include "transport.h"
@ -68,39 +69,39 @@ static int64_t check_response(Transport* transport, uint32_t size, char* respons
}
status[r] = 0;
if (r < 4) {
if (static_cast<size_t>(r) < strlen(RESPONSE_OKAY)) {
g_error = android::base::StringPrintf("status malformed (%d bytes)", r);
transport->Close();
return -1;
}
if (!memcmp(status, "INFO", 4)) {
verbose("received INFO \"%s\"", status + 4);
fprintf(stderr, "(bootloader) %s\n", status + 4);
if (!memcmp(status, RESPONSE_INFO, strlen(RESPONSE_INFO))) {
verbose("received INFO \"%s\"", status + strlen(RESPONSE_INFO));
fprintf(stderr, "(bootloader) %s\n", status + strlen(RESPONSE_INFO));
continue;
}
if (!memcmp(status, "OKAY", 4)) {
verbose("received OKAY \"%s\"", status + 4);
if (!memcmp(status, RESPONSE_OKAY, strlen(RESPONSE_OKAY))) {
verbose("received OKAY \"%s\"", status + strlen(RESPONSE_OKAY));
if (response) {
strcpy(response, status + 4);
strcpy(response, status + strlen(RESPONSE_OKAY));
}
return 0;
}
if (!memcmp(status, "FAIL", 4)) {
verbose("received FAIL \"%s\"", status + 4);
if (r > 4) {
g_error = android::base::StringPrintf("remote: %s", status + 4);
if (!memcmp(status, RESPONSE_FAIL, strlen(RESPONSE_FAIL))) {
verbose("received FAIL \"%s\"", status + strlen(RESPONSE_FAIL));
if (static_cast<size_t>(r) > strlen(RESPONSE_FAIL)) {
g_error = android::base::StringPrintf("remote: %s", status + strlen(RESPONSE_FAIL));
} else {
g_error = "remote failure";
}
return -1;
}
if (!memcmp(status, "DATA", 4) && size > 0){
verbose("received DATA %s", status + 4);
uint32_t dsize = strtol(status + 4, 0, 16);
if (!memcmp(status, RESPONSE_DATA, strlen(RESPONSE_DATA)) && size > 0){
verbose("received DATA %s", status + strlen(RESPONSE_DATA));
uint32_t dsize = strtol(status + strlen(RESPONSE_DATA), 0, 16);
if (dsize > size) {
g_error = android::base::StringPrintf("data size too large (%d)", dsize);
transport->Close();
@ -247,18 +248,21 @@ int fb_command_response(Transport* transport, const std::string& cmd, char* resp
}
int64_t fb_download_data(Transport* transport, const void* data, uint32_t size) {
std::string cmd(android::base::StringPrintf("download:%08x", size));
std::string cmd(android::base::StringPrintf(
FB_CMD_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));
std::string cmd(android::base::StringPrintf(
FB_CMD_DOWNLOAD ":" "%08x", size));
return _command_send_fd(transport, cmd.c_str(), fd, size, 0) < 0 ? -1 : 0;
}
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);
int64_t r = _command_start(transport, FB_CMD_UPLOAD,
std::numeric_limits<int32_t>::max(), nullptr);
if (r <= 0) {
g_error = android::base::StringPrintf("command start failed (%s)", strerror(errno));
return r;
@ -345,7 +349,8 @@ int fb_download_data_sparse(Transport* transport, struct sparse_file* s) {
return -1;
}
std::string cmd(android::base::StringPrintf("download:%08" PRIx64, size));
std::string cmd(android::base::StringPrintf(
FB_CMD_DOWNLOAD ":" "%08" PRIx64, size));
int r = _command_start(transport, cmd, size, 0);
if (r < 0) {
return -1;