Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad3d3c18 authored by Daniel Rosenberg's avatar Daniel Rosenberg
Browse files

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 190d9684)
parent 13454095
Loading
Loading
Loading
Loading
+37 −12
Original line number Diff line number Diff line
@@ -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,24 +836,25 @@ static std::string verify_slot(Transport* transport, const char *slot, bool allo
        if (!fb_getvar(transport, "current-slot", &current_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 {
        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;
    }
    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);
            }