diff options
author | Daniel Rosenberg <drosen@google.com> | 2016-06-27 23:03:48 -0700 |
---|---|---|
committer | Daniel Rosenberg <drosen@google.com> | 2016-08-19 15:31:01 -0700 |
commit | ad3d3c184e1f548051e24a1a78ae4dddf5ab34fd (patch) | |
tree | 81b10763869abc62fda8ed79355093ed7cb84d64 /fastboot/fastboot.cpp | |
parent | 13454095374239a807237d3e073bd3ff488bd98d (diff) | |
download | core-ad3d3c184e1f548051e24a1a78ae4dddf5ab34fd.tar.gz core-ad3d3c184e1f548051e24a1a78ae4dddf5ab34fd.tar.bz2 core-ad3d3c184e1f548051e24a1a78ae4dddf5ab34fd.zip |
Handle invalid suffix lists
Some devices will report an error string as a value
when unknown variables are queried. This can lead to
unexpected behavior, so we attempt to detect this case
by seeing if the suffix list doesn't make sense.
Change-Id: I939b1e01c40ddc05d881fd54423406db250cc8e5
(cherry-picked from commit 190d968414b3889441843e1bbebdf7acc2dc88c8)
Diffstat (limited to 'fastboot/fastboot.cpp')
-rw-r--r-- | fastboot/fastboot.cpp | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index a2af61a87..3c699646d 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -789,9 +789,30 @@ static std::vector<std::string> get_suffixes(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; +} + +// Given a current slot, this returns what the 'other' slot is. +static std::string get_other_slot(Transport* transport, std::string& current_slot) { + std::vector<std::string> suffixes = get_suffixes(transport); + + if (!suffixes.empty()) { + for (size_t i = 0; i < suffixes.size(); i++) { + if (current_slot == suffixes[i]) { + return suffixes[(i+1)%suffixes.size()]; + } + } + } + return ""; } static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) { @@ -815,23 +836,24 @@ static std::string verify_slot(Transport* transport, const char *slot, bool allo if (!fb_getvar(transport, "current-slot", ¤t_slot)) { die("Failed to identify current slot."); } - 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."); + std::string other = get_other_slot(transport, current_slot); + if (other == "") { + die("No known slots."); } + return other; } for (const std::string &suffix : suffixes) { if (suffix == slot) return slot; } - 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()); + if (suffixes.empty()) { + fprintf(stderr, "Device does not support slots.\n"); + } else { + 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); } @@ -882,6 +904,9 @@ static void do_for_partitions(Transport* transport, const char *part, const char } if (has_slot == "yes") { std::vector<std::string> suffixes = get_suffixes(transport); + if (suffixes.empty()) { + die("Error reading suffixes.\n"); + } for (std::string &suffix : suffixes) { do_for_partition(transport, part, suffix.c_str(), func, force_slot); } |