Merge "debuggerd: set SOCK_NONBLOCK in accept rather than later."
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 45d26f8..be390d9 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -360,7 +360,7 @@
adb_auth_verified(t);
t->failed_auth_attempts = 0;
} else {
- if (t->failed_auth_attempts++ > 10) adb_sleep_ms(1000);
+ if (t->failed_auth_attempts++ > 256) adb_sleep_ms(1000);
send_auth_request(t);
}
} else if (p->msg.arg0 == ADB_AUTH_RSAPUBLICKEY) {
diff --git a/adb/adb_auth.cpp b/adb/adb_auth.cpp
index 446c3df..0b07158 100644
--- a/adb/adb_auth.cpp
+++ b/adb/adb_auth.cpp
@@ -64,12 +64,14 @@
p->msg.command = A_AUTH;
p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;
- p->msg.data_length = key.size();
+
+ // adbd expects a null-terminated string.
+ p->msg.data_length = key.size() + 1;
send_packet(p, t);
}
void send_auth_response(uint8_t* token, size_t token_size, atransport* t) {
- RSA* key = t->NextKey();
+ std::shared_ptr<RSA> key = t->NextKey();
if (key == nullptr) {
// No more private keys to try, send the public key.
send_auth_publickey(t);
@@ -79,12 +81,7 @@
LOG(INFO) << "Calling send_auth_response";
apacket* p = get_apacket();
- int ret = adb_auth_sign(key, token, token_size, p->data);
-
- // Stop sharing this key.
- RSA_free(key);
- key = nullptr;
-
+ int ret = adb_auth_sign(key.get(), token, token_size, p->data);
if (!ret) {
D("Error signing the token");
put_apacket(p);
diff --git a/adb/adb_auth.h b/adb/adb_auth.h
index 723ded5..59b80d8 100644
--- a/adb/adb_auth.h
+++ b/adb/adb_auth.h
@@ -20,6 +20,7 @@
#include "adb.h"
#include <deque>
+#include <memory>
#include <openssl/rsa.h>
@@ -43,7 +44,7 @@
void adb_auth_init();
int adb_auth_sign(RSA* key, const unsigned char* token, size_t token_size, unsigned char* sig);
std::string adb_auth_get_userkey();
-std::deque<RSA*> adb_auth_get_private_keys();
+std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys();
static inline bool adb_auth_generate_token(void*, size_t) { abort(); }
static inline bool adb_auth_verify(void*, size_t, void*, int) { abort(); }
@@ -53,7 +54,7 @@
static inline int adb_auth_sign(void*, const unsigned char*, size_t, unsigned char*) { abort(); }
static inline std::string adb_auth_get_userkey() { abort(); }
-static inline std::deque<RSA*> adb_auth_get_private_keys() { abort(); }
+static inline std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys() { abort(); }
void adbd_auth_init(void);
void adbd_cloexec_auth_socket();
diff --git a/adb/adb_auth_host.cpp b/adb/adb_auth_host.cpp
index ce269cc..4f4f382 100644
--- a/adb/adb_auth_host.cpp
+++ b/adb/adb_auth_host.cpp
@@ -16,16 +16,18 @@
#define TRACE_TAG AUTH
-#include "adb_auth.h"
-#include "adb.h"
-#include "adb_utils.h"
-#include "sysdeps.h"
-
+#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#if defined(__linux__)
+#include <sys/inotify.h>
+#endif
+#include <map>
#include <mutex>
+#include <set>
+#include <string>
#include <android-base/errors.h>
#include <android-base/file.h>
@@ -39,10 +41,16 @@
#include <openssl/rsa.h>
#include <openssl/sha.h>
+#include "adb.h"
+#include "adb_auth.h"
+#include "adb_utils.h"
+#include "sysdeps.h"
#include "sysdeps/mutex.h"
-static std::mutex& g_key_list_mutex = *new std::mutex;
-static std::deque<RSA*>& g_key_list = *new std::deque<RSA*>;
+static std::mutex& g_keys_mutex = *new std::mutex;
+static std::map<std::string, std::shared_ptr<RSA>>& g_keys =
+ *new std::map<std::string, std::shared_ptr<RSA>>;
+static std::map<int, std::string>& g_monitored_paths = *new std::map<int, std::string>;
static std::string get_user_info() {
LOG(INFO) << "get_user_info...";
@@ -146,8 +154,23 @@
return ret;
}
-static bool read_key(const std::string& file) {
- LOG(INFO) << "read_key '" << file << "'...";
+static std::string hash_key(RSA* key) {
+ unsigned char* pubkey = nullptr;
+ int len = i2d_RSA_PUBKEY(key, &pubkey);
+ if (len < 0) {
+ LOG(ERROR) << "failed to encode RSA public key";
+ return std::string();
+ }
+
+ std::string result;
+ result.resize(SHA256_DIGEST_LENGTH);
+ SHA256(pubkey, len, reinterpret_cast<unsigned char*>(&result[0]));
+ OPENSSL_free(pubkey);
+ return result;
+}
+
+static bool read_key_file(const std::string& file) {
+ LOG(INFO) << "read_key_file '" << file << "'...";
std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(file.c_str(), "r"), fclose);
if (!fp) {
@@ -162,10 +185,66 @@
return false;
}
- g_key_list.push_back(key);
+ std::lock_guard<std::mutex> lock(g_keys_mutex);
+ std::string fingerprint = hash_key(key);
+ if (g_keys.find(fingerprint) != g_keys.end()) {
+ LOG(INFO) << "ignoring already-loaded key: " << file;
+ RSA_free(key);
+ } else {
+ g_keys[fingerprint] = std::shared_ptr<RSA>(key, RSA_free);
+ }
+
return true;
}
+static bool read_keys(const std::string& path, bool allow_dir = true) {
+ LOG(INFO) << "read_keys '" << path << "'...";
+
+ struct stat st;
+ if (stat(path.c_str(), &st) != 0) {
+ PLOG(ERROR) << "failed to stat '" << path << "'";
+ return false;
+ }
+
+ if (S_ISREG(st.st_mode)) {
+ if (!android::base::EndsWith(path, ".adb_key")) {
+ LOG(INFO) << "skipping non-adb_key '" << path << "'";
+ return false;
+ }
+
+ return read_key_file(path);
+ } else if (S_ISDIR(st.st_mode)) {
+ if (!allow_dir) {
+ // inotify isn't recursive. It would break expectations to load keys in nested
+ // directories but not monitor them for new keys.
+ LOG(WARNING) << "refusing to recurse into directory '" << path << "'";
+ return false;
+ }
+
+ std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
+ if (!dir) {
+ PLOG(ERROR) << "failed to open directory '" << path << "'";
+ return false;
+ }
+
+ bool result = false;
+ while (struct dirent* dent = readdir(dir.get())) {
+ std::string name = dent->d_name;
+
+ // We can't use dent->d_type here because it's not available on Windows.
+ if (name == "." || name == "..") {
+ continue;
+ }
+
+ result |= read_keys((path + OS_PATH_SEPARATOR + name).c_str(), false);
+ }
+ return result;
+ }
+
+ LOG(ERROR) << "unexpected type for '" << path << "': 0x" << std::hex << st.st_mode;
+ return false;
+}
+
static std::string get_user_key_path() {
const std::string home = adb_get_homedir_path(true);
LOG(DEBUG) << "adb_get_homedir_path returned '" << home << "'";
@@ -200,31 +279,29 @@
}
}
- return read_key(path);
+ return read_key_file(path);
}
-static void get_vendor_keys() {
+static std::set<std::string> get_vendor_keys() {
const char* adb_keys_path = getenv("ADB_VENDOR_KEYS");
if (adb_keys_path == nullptr) {
- return;
+ return std::set<std::string>();
}
+ std::set<std::string> result;
for (const auto& path : android::base::Split(adb_keys_path, ENV_PATH_SEPARATOR_STR)) {
- if (!read_key(path.c_str())) {
- PLOG(ERROR) << "Failed to read '" << path << "'";
- }
+ result.emplace(path);
}
+ return result;
}
-std::deque<RSA*> adb_auth_get_private_keys() {
- std::deque<RSA*> result;
+std::deque<std::shared_ptr<RSA>> adb_auth_get_private_keys() {
+ std::deque<std::shared_ptr<RSA>> result;
- // Copy all the currently known keys, increasing their reference count so they're
- // usable until both we and the caller have freed our pointers.
- std::lock_guard<std::mutex> lock(g_key_list_mutex);
- for (const auto& key : g_key_list) {
- RSA_up_ref(key); // Since we're handing out another pointer to this key...
- result.push_back(key);
+ // Copy all the currently known keys.
+ std::lock_guard<std::mutex> lock(g_keys_mutex);
+ for (const auto& it : g_keys) {
+ result.push_back(it.second);
}
// Add a sentinel to the list. Our caller uses this to mean "out of private keys,
@@ -270,6 +347,77 @@
return (generate_key(filename) == 0);
}
+#if defined(__linux__)
+static void adb_auth_inotify_update(int fd, unsigned fd_event, void*) {
+ LOG(INFO) << "adb_auth_inotify_update called";
+ if (!(fd_event & FDE_READ)) {
+ return;
+ }
+
+ char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
+ while (true) {
+ ssize_t rc = TEMP_FAILURE_RETRY(unix_read(fd, buf, sizeof(buf)));
+ if (rc == -1) {
+ if (errno == EAGAIN) {
+ LOG(INFO) << "done reading inotify fd";
+ break;
+ }
+ PLOG(FATAL) << "read of inotify event failed";
+ }
+
+ // The read potentially returned multiple events.
+ char* start = buf;
+ char* end = buf + rc;
+
+ while (start < end) {
+ inotify_event* event = reinterpret_cast<inotify_event*>(start);
+ auto root_it = g_monitored_paths.find(event->wd);
+ if (root_it == g_monitored_paths.end()) {
+ LOG(FATAL) << "observed inotify event for unmonitored path, wd = " << event->wd;
+ }
+
+ std::string path = root_it->second;
+ if (event->len > 0) {
+ path += '/';
+ path += event->name;
+ }
+
+ if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
+ if (event->mask & IN_ISDIR) {
+ LOG(INFO) << "ignoring new directory at '" << path << "'";
+ } else {
+ LOG(INFO) << "observed new file at '" << path << "'";
+ read_keys(path, false);
+ }
+ } else {
+ LOG(WARNING) << "unmonitored event for " << path << ": 0x" << std::hex
+ << event->mask;
+ }
+
+ start += sizeof(struct inotify_event) + event->len;
+ }
+ }
+}
+
+static void adb_auth_inotify_init(const std::set<std::string>& paths) {
+ LOG(INFO) << "adb_auth_inotify_init...";
+ int infd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
+ for (const std::string& path : paths) {
+ int wd = inotify_add_watch(infd, path.c_str(), IN_CREATE | IN_MOVED_TO);
+ if (wd < 0) {
+ PLOG(ERROR) << "failed to inotify_add_watch on path '" << path;
+ continue;
+ }
+
+ g_monitored_paths[wd] = path;
+ LOG(INFO) << "watch descriptor " << wd << " registered for " << path;
+ }
+
+ fdevent* event = fdevent_create(infd, adb_auth_inotify_update, nullptr);
+ fdevent_add(event, FDE_READ);
+}
+#endif
+
void adb_auth_init() {
LOG(INFO) << "adb_auth_init...";
@@ -278,6 +426,13 @@
return;
}
- std::lock_guard<std::mutex> lock(g_key_list_mutex);
- get_vendor_keys();
+ const auto& key_paths = get_vendor_keys();
+
+#if defined(__linux__)
+ adb_auth_inotify_init(key_paths);
+#endif
+
+ for (const std::string& path : key_paths) {
+ read_keys(path.c_str());
+ }
}
diff --git a/adb/transport.cpp b/adb/transport.cpp
index e0216e3..f5ef174 100644
--- a/adb/transport.cpp
+++ b/adb/transport.cpp
@@ -1076,10 +1076,10 @@
}
}
-RSA* atransport::NextKey() {
+std::shared_ptr<RSA> atransport::NextKey() {
if (keys_.empty()) keys_ = adb_auth_get_private_keys();
- RSA* result = keys_[0];
+ std::shared_ptr<RSA> result = keys_[0];
keys_.pop_front();
return result;
}
diff --git a/adb/transport.h b/adb/transport.h
index d41c8bd..959681f 100644
--- a/adb/transport.h
+++ b/adb/transport.h
@@ -21,6 +21,7 @@
#include <deque>
#include <list>
+#include <memory>
#include <string>
#include <unordered_set>
@@ -107,7 +108,7 @@
return type == kTransportLocal && local_port_for_emulator_ == -1;
}
- RSA* NextKey();
+ std::shared_ptr<RSA> NextKey();
unsigned char token[TOKEN_SIZE] = {};
size_t failed_auth_attempts = 0;
@@ -160,7 +161,7 @@
// A list of adisconnect callbacks called when the transport is kicked.
std::list<adisconnect*> disconnects_;
- std::deque<RSA*> keys_;
+ std::deque<std::shared_ptr<RSA>> keys_;
DISALLOW_COPY_AND_ASSIGN(atransport);
};
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index c097d04..1f2408f 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -97,19 +97,40 @@
};
static struct {
- char img_name[13];
- char sig_name[13];
+ char img_name[17];
+ char sig_name[17];
char part_name[9];
bool is_optional;
+ bool is_secondary;
} images[] = {
- {"boot.img", "boot.sig", "boot", false},
- {"recovery.img", "recovery.sig", "recovery", true},
- {"system.img", "system.sig", "system", false},
- {"vendor.img", "vendor.sig", "vendor", true},
+ {"boot.img", "boot.sig", "boot", false, false},
+ {"boot_other.img", "boot.sig", "boot", true, true},
+ {"recovery.img", "recovery.sig", "recovery", true, false},
+ {"system.img", "system.sig", "system", false, false},
+ {"system_other.img", "system.sig", "system", true, true},
+ {"vendor.img", "vendor.sig", "vendor", true, false},
+ {"vendor_other.img", "vendor.sig", "vendor", true, true},
};
-static std::string find_item(const char* item, const char* product) {
+static std::string find_item_given_name(const char* img_name, const char* product) {
+ if(product) {
+ std::string path = get_my_path();
+ path.erase(path.find_last_of('/'));
+ return android::base::StringPrintf("%s/../../../target/product/%s/%s",
+ path.c_str(), product, img_name);
+ }
+
+ char *dir = getenv("ANDROID_PRODUCT_OUT");
+ if (dir == nullptr || dir[0] == '\0') {
+ die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
+ }
+
+ return android::base::StringPrintf("%s/%s", dir, img_name);
+}
+
+std::string find_item(const char* item, const char* product) {
const char *fn;
+
if (!strcmp(item,"boot")) {
fn = "boot.img";
} else if(!strcmp(item,"recovery")) {
@@ -129,19 +150,7 @@
return "";
}
- if (product) {
- std::string path = get_my_path();
- path.erase(path.find_last_of('/'));
- return android::base::StringPrintf("%s/../../../target/product/%s/%s",
- path.c_str(), product, fn);
- }
-
- char* dir = getenv("ANDROID_PRODUCT_OUT");
- if (dir == nullptr || dir[0] == '\0') {
- die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
- }
-
- return android::base::StringPrintf("%s/%s", dir, fn);
+ return find_item_given_name(fn, product);
}
static int64_t get_file_size(int fd) {
@@ -307,8 +316,13 @@
"\n"
"commands:\n"
" update <filename> Reflash device from update.zip.\n"
+ " Sets the flashed slot as active.\n"
" flashall Flash boot, system, vendor, and --\n"
- " if found -- recovery.\n"
+ " if found -- recovery. If the device\n"
+ " supports slots, the slot that has\n"
+ " been flashed to is set as active.\n"
+ " Secondary images may be flashed to\n"
+ " an inactive slot.\n"
" flash <partition> [ <filename> ] Write a file to a flash partition.\n"
" flashing lock Locks the device. Prevents flashing.\n"
" flashing unlock Unlocks the device. Allows flashing\n"
@@ -331,7 +345,7 @@
" override the fs type and/or size\n"
" the bootloader reports.\n"
" getvar <variable> Display a bootloader variable.\n"
- " set_active <suffix> Sets the active slot. If slots are\n"
+ " set_active <slot> Sets the active slot. If slots are\n"
" not supported, this does nothing.\n"
" boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
" flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
@@ -368,19 +382,24 @@
" (default: 2048).\n"
" -S <size>[K|M|G] Automatically sparse files greater\n"
" than 'size'. 0 to disable.\n"
- " --slot <suffix> Specify slot suffix to be used if the\n"
- " device supports slots. This will be\n"
- " added to all partition names that use\n"
- " slots. 'all' can be given to refer\n"
- " to all slots. 'other' can be given to\n"
- " refer to a non-current slot. If this\n"
- " flag is not used, slotted partitions\n"
- " will default to the current active slot.\n"
- " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
+ " --slot <slot> Specify slot name to be used if the\n"
+ " device supports slots. All operations\n"
+ " on partitions that support slots will\n"
+ " be done on the slot specified.\n"
+ " 'all' can be given to refer to all slots.\n"
+ " 'other' can be given to refer to a\n"
+ " non-current slot. If this flag is not\n"
+ " used, slotted partitions will default\n"
+ " to the current active slot.\n"
+ " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
" provided, this will default to the value\n"
" given by --slot. If slots are not\n"
- " supported, this does nothing. This will\n"
- " run after all non-reboot commands.\n"
+ " supported, this sets the current slot\n"
+ " to be active. This will run after all\n"
+ " non-reboot commands.\n"
+ " --skip-secondary Will not flash secondary slots when\n"
+ " performing a flashall or update. This\n"
+ " will preserve data on other slots.\n"
" --unbuffered Do not buffer input or output.\n"
" --version Display version.\n"
" -h, --help show this message.\n"
@@ -782,83 +801,138 @@
}
}
-static std::vector<std::string> get_suffixes(Transport* transport) {
+static std::string get_current_slot(Transport* transport)
+{
+ std::string current_slot;
+ if (fb_getvar(transport, "current-slot", ¤t_slot)) {
+ if (current_slot == "_a") return "a"; // Legacy support
+ if (current_slot == "_b") return "b"; // Legacy support
+ return current_slot;
+ }
+ return "";
+}
+
+// Legacy support
+static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
std::vector<std::string> suffixes;
std::string suffix_list;
if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
- die("Could not get suffixes.\n");
+ return suffixes;
}
- return android::base::Split(suffix_list, ",");
+ suffixes = android::base::Split(suffix_list, ",");
+ // Unfortunately some devices will return an error message in the
+ // guise of a valid value. If we only see only one suffix, it's probably
+ // not real.
+ if (suffixes.size() == 1) {
+ suffixes.clear();
+ }
+ return suffixes;
}
-static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
- if (strcmp(slot, "all") == 0) {
+// Legacy support
+static bool supports_AB_obsolete(Transport* transport) {
+ return !get_suffixes_obsolete(transport).empty();
+}
+
+static int get_slot_count(Transport* transport) {
+ std::string var;
+ int count;
+ if (!fb_getvar(transport, "slot-count", &var)) {
+ if (supports_AB_obsolete(transport)) return 2; // Legacy support
+ }
+ if (!android::base::ParseInt(var.c_str(), &count)) return 0;
+ return count;
+}
+
+static bool supports_AB(Transport* transport) {
+ return get_slot_count(transport) >= 2;
+}
+
+// Given a current slot, this returns what the 'other' slot is.
+static std::string get_other_slot(const std::string& current_slot, int count) {
+ if (count == 0) return "";
+
+ char next = (current_slot[0] - 'a' + 1)%count + 'a';
+ return std::string(1, next);
+}
+
+static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
+ return get_other_slot(current_slot, get_slot_count(transport));
+}
+
+static std::string get_other_slot(Transport* transport, int count) {
+ return get_other_slot(get_current_slot(transport), count);
+}
+
+static std::string get_other_slot(Transport* transport) {
+ return get_other_slot(get_current_slot(transport), get_slot_count(transport));
+}
+
+static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
+ std::string slot = slot_name;
+ if (slot == "_a") slot = "a"; // Legacy support
+ if (slot == "_b") slot = "b"; // Legacy support
+ if (slot == "all") {
if (allow_all) {
return "all";
} else {
- std::vector<std::string> suffixes = get_suffixes(transport);
- if (!suffixes.empty()) {
- return suffixes[0];
+ int count = get_slot_count(transport);
+ if (count > 0) {
+ return "a";
} else {
die("No known slots.");
}
}
}
- std::vector<std::string> suffixes = get_suffixes(transport);
+ int count = get_slot_count(transport);
+ if (count == 0) die("Device does not support slots.\n");
- if (strcmp(slot, "other") == 0) {
- std::string current_slot;
- if (!fb_getvar(transport, "current-slot", ¤t_slot)) {
- die("Failed to identify current slot.");
+ if (slot == "other") {
+ std::string other = get_other_slot(transport, count);
+ if (other == "") {
+ die("No known slots.");
}
- if (!suffixes.empty()) {
- for (size_t i = 0; i < suffixes.size(); i++) {
- if (current_slot == suffixes[i])
- return suffixes[(i+1)%suffixes.size()];
- }
- } else {
- die("No known slots.");
- }
+ return other;
}
- for (const std::string &suffix : suffixes) {
- if (suffix == slot)
- return slot;
+ if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
+
+ fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
+ for (int i=0; i<count; i++) {
+ fprintf(stderr, "%c\n", (char)(i + 'a'));
}
- fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
- for (const std::string &suffix : suffixes) {
- fprintf(stderr, "%s\n", suffix.c_str());
- }
+
exit(1);
}
-static std::string verify_slot(Transport* transport, const char *slot) {
+static std::string verify_slot(Transport* transport, const std::string& slot) {
return verify_slot(transport, slot, true);
}
-static void do_for_partition(Transport* transport, const char *part, const char *slot,
+static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
const std::function<void(const std::string&)>& func, bool force_slot) {
std::string has_slot;
std::string current_slot;
- if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
+ if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
/* If has-slot is not supported, the answer is no. */
has_slot = "no";
}
if (has_slot == "yes") {
- if (!slot || slot[0] == 0) {
- if (!fb_getvar(transport, "current-slot", ¤t_slot)) {
+ if (slot == "") {
+ current_slot = get_current_slot(transport);
+ if (current_slot == "") {
die("Failed to identify current slot.\n");
}
- func(std::string(part) + current_slot);
+ func(part + "_" + current_slot);
} else {
- func(std::string(part) + slot);
+ func(part + '_' + slot);
}
} else {
- if (force_slot && slot && slot[0]) {
+ if (force_slot && slot != "") {
fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
- part, slot);
+ part.c_str(), slot.c_str());
}
func(part);
}
@@ -869,18 +943,17 @@
* partition names. If force_slot is true, it will fail if a slot is specified, and the given
* partition does not support slots.
*/
-static void do_for_partitions(Transport* transport, const char *part, const char *slot,
+static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
const std::function<void(const std::string&)>& func, bool force_slot) {
std::string has_slot;
- if (slot && strcmp(slot, "all") == 0) {
- if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
- die("Could not check if partition %s has slot.", part);
+ if (slot == "all") {
+ if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
+ die("Could not check if partition %s has slot.", part.c_str());
}
if (has_slot == "yes") {
- std::vector<std::string> suffixes = get_suffixes(transport);
- for (std::string &suffix : suffixes) {
- do_for_partition(transport, part, suffix.c_str(), func, force_slot);
+ for (int i=0; i < get_slot_count(transport); i++) {
+ do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
}
} else {
do_for_partition(transport, part, "", func, force_slot);
@@ -907,7 +980,28 @@
fb_queue_command("signature", "installing signature");
}
-static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
+// Sets slot_override as the active slot. If slot_override is blank,
+// set current slot as active instead. This clears slot-unbootable.
+static void set_active(Transport* transport, const std::string& slot_override) {
+ std::string separator = "";
+ if (!supports_AB(transport)) {
+ if (supports_AB_obsolete(transport)) {
+ separator = "_"; // Legacy support
+ } else {
+ return;
+ }
+ }
+ if (slot_override != "") {
+ fb_set_active((separator + slot_override).c_str());
+ } else {
+ std::string current_slot = get_current_slot(transport);
+ if (current_slot != "") {
+ fb_set_active((separator + current_slot).c_str());
+ }
+ }
+}
+
+static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
queue_info_dump();
fb_queue_query_save("product", cur_product, sizeof(cur_product));
@@ -928,7 +1022,30 @@
setup_requirements(reinterpret_cast<char*>(data), sz);
+ std::string secondary;
+ if (!skip_secondary) {
+ if (slot_override != "") {
+ secondary = get_other_slot(transport, slot_override);
+ } else {
+ secondary = get_other_slot(transport);
+ }
+ if (secondary == "") {
+ if (supports_AB(transport)) {
+ fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
+ }
+ skip_secondary = true;
+ }
+ }
for (size_t i = 0; i < arraysize(images); ++i) {
+ const char* slot = slot_override.c_str();
+ if (images[i].is_secondary) {
+ if (!skip_secondary) {
+ slot = secondary.c_str();
+ } else {
+ continue;
+ }
+ }
+
int fd = unzip_to_file(zip, images[i].img_name);
if (fd == -1) {
if (images[i].is_optional) {
@@ -953,49 +1070,74 @@
* program exits.
*/
};
- do_for_partitions(transport, images[i].part_name, slot_override, update, false);
+ do_for_partitions(transport, images[i].part_name, slot, update, false);
}
CloseArchive(zip);
+ if (slot_override == "all") {
+ set_active(transport, "a");
+ } else {
+ set_active(transport, slot_override);
+ }
}
-static void do_send_signature(const char* filename) {
- if (android::base::EndsWith(filename, ".img") == false) {
- return;
- }
+static void do_send_signature(const std::string& fn) {
+ std::size_t extension_loc = fn.find(".img");
+ if (extension_loc == std::string::npos) return;
- std::string sig_path = filename;
- sig_path.erase(sig_path.size() - 4);
- sig_path += ".sig";
+ std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
int64_t sz;
- void* data = load_file(sig_path, &sz);
+ void* data = load_file(fs_sig.c_str(), &sz);
if (data == nullptr) return;
fb_queue_download("signature", data, sz);
fb_queue_command("signature", "installing signature");
}
-static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
+static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
+ std::string fname;
queue_info_dump();
fb_queue_query_save("product", cur_product, sizeof(cur_product));
- std::string fname = find_item("info", product);
+ fname = find_item("info", product);
if (fname.empty()) die("cannot find android-info.txt");
int64_t sz;
- void* data = load_file(fname, &sz);
+ void* data = load_file(fname.c_str(), &sz);
if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
setup_requirements(reinterpret_cast<char*>(data), sz);
+ std::string secondary;
+ if (!skip_secondary) {
+ if (slot_override != "") {
+ secondary = get_other_slot(transport, slot_override);
+ } else {
+ secondary = get_other_slot(transport);
+ }
+ if (secondary == "") {
+ if (supports_AB(transport)) {
+ fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
+ }
+ skip_secondary = true;
+ }
+ }
+
for (size_t i = 0; i < arraysize(images); i++) {
- fname = find_item(images[i].part_name, product);
+ const char* slot = NULL;
+ if (images[i].is_secondary) {
+ if (!skip_secondary) slot = secondary.c_str();
+ } else {
+ slot = slot_override.c_str();
+ }
+ if (!slot) continue;
+ fname = find_item_given_name(images[i].img_name, product);
fastboot_buffer buf;
if (!load_buf(transport, fname.c_str(), &buf)) {
if (images[i].is_optional) continue;
- die("could not load '%s': %s", images[i].img_name, strerror(errno));
+ die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
}
auto flashall = [&](const std::string &partition) {
@@ -1005,7 +1147,13 @@
}
flash_buf(partition.c_str(), &buf);
};
- do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
+ do_for_partitions(transport, images[i].part_name, slot, flashall, false);
+ }
+
+ if (slot_override == "all") {
+ set_active(transport, "a");
+ } else {
+ set_active(transport, slot_override);
}
}
@@ -1184,6 +1332,7 @@
bool wants_reboot = false;
bool wants_reboot_bootloader = false;
bool wants_set_active = false;
+ bool skip_secondary = false;
bool erase_first = true;
void *data;
int64_t sz;
@@ -1207,6 +1356,7 @@
{"slot", required_argument, 0, 0},
{"set_active", optional_argument, 0, 'a'},
{"set-active", optional_argument, 0, 'a'},
+ {"skip-secondary", no_argument, 0, 0},
{0, 0, 0, 0}
};
@@ -1288,6 +1438,12 @@
return 0;
} else if (strcmp("slot", longopts[longindex].name) == 0) {
slot_override = std::string(optarg);
+ } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
+ skip_secondary = true;
+ } else {
+ fprintf(stderr, "Internal error in options processing for %s\n",
+ longopts[longindex].name);
+ return 1;
}
break;
default:
@@ -1319,17 +1475,23 @@
return 1;
}
- if (slot_override != "")
- slot_override = verify_slot(transport, slot_override.c_str());
- if (next_active != "")
- next_active = verify_slot(transport, next_active.c_str(), false);
+ if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
+ fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
+ }
+ if (slot_override != "") slot_override = verify_slot(transport, slot_override);
+ if (next_active != "") next_active = verify_slot(transport, next_active, false);
if (wants_set_active) {
if (next_active == "") {
if (slot_override == "") {
- wants_set_active = false;
+ std::string current_slot;
+ if (fb_getvar(transport, "current-slot", ¤t_slot)) {
+ next_active = verify_slot(transport, current_slot, false);
+ } else {
+ wants_set_active = false;
+ }
} else {
- next_active = verify_slot(transport, slot_override.c_str(), false);
+ next_active = verify_slot(transport, slot_override, false);
}
}
}
@@ -1352,7 +1514,7 @@
fb_queue_erase(partition.c_str());
};
- do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
+ do_for_partitions(transport, argv[1], slot_override, erase, true);
skip(2);
} else if(!strncmp(*argv, "format", strlen("format"))) {
char *overrides;
@@ -1387,7 +1549,7 @@
}
fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
};
- do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
+ do_for_partitions(transport, argv[1], slot_override, format, true);
skip(2);
} else if(!strcmp(*argv, "signature")) {
require(2);
@@ -1454,7 +1616,7 @@
}
do_flash(transport, partition.c_str(), fname.c_str());
};
- do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
+ do_for_partitions(transport, pname, slot_override, flash, true);
} else if(!strcmp(*argv, "flash:raw")) {
char *kname = argv[2];
char *rname = 0;
@@ -1474,23 +1636,32 @@
auto flashraw = [&](const std::string &partition) {
fb_queue_flash(partition.c_str(), data, sz);
};
- do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
+ do_for_partitions(transport, argv[1], slot_override, flashraw, true);
} else if(!strcmp(*argv, "flashall")) {
skip(1);
- do_flashall(transport, slot_override.c_str(), erase_first);
+ if (slot_override == "all") {
+ fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
+ do_flashall(transport, slot_override, erase_first, true);
+ } else {
+ do_flashall(transport, slot_override, erase_first, skip_secondary);
+ }
wants_reboot = true;
} else if(!strcmp(*argv, "update")) {
+ bool slot_all = (slot_override == "all");
+ if (slot_all) {
+ fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
+ }
if (argc > 1) {
- do_update(transport, argv[1], slot_override.c_str(), erase_first);
+ do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
skip(2);
} else {
- do_update(transport, "update.zip", slot_override.c_str(), erase_first);
+ do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
skip(1);
}
wants_reboot = 1;
} else if(!strcmp(*argv, "set_active")) {
require(2);
- std::string slot = verify_slot(transport, argv[1], false);
+ std::string slot = verify_slot(transport, std::string(argv[1]), false);
fb_set_active(slot.c_str());
skip(2);
} else if(!strcmp(*argv, "oem")) {
diff --git a/liblog/logd_writer.c b/liblog/logd_writer.c
index 059f170..ed82902 100644
--- a/liblog/logd_writer.c
+++ b/liblog/logd_writer.c
@@ -67,12 +67,9 @@
int i, ret = 0;
if (logdLoggerWrite.context.sock < 0) {
- i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
+ i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
if (i < 0) {
ret = -errno;
- } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
- ret = -errno;
- close(i);
} else {
struct sockaddr_un un;
memset(&un, 0, sizeof(struct sockaddr_un));