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

Commit 17500816 authored by Mark Salyzyn's avatar Mark Salyzyn Committed by android-build-merger
Browse files

Merge "dumpstate: sscanf requires an asciiz string"

am: 8b105176

* commit '8b105176':
  dumpstate: sscanf requires an asciiz string

Change-Id: Iad314a7bfcbfd29ca95a0c8a76e52f8b894d04e2
parents 26c812f0 8b105176
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ LOCAL_SRC_FILES := dumpstate.cpp utils.cpp

LOCAL_MODULE := dumpstate

LOCAL_SHARED_LIBRARIES := libcutils liblog libselinux
LOCAL_SHARED_LIBRARIES := libcutils liblog libselinux libbase
LOCAL_HAL_STATIC_LIBRARIES := libdumpstate
LOCAL_CFLAGS += -Wall -Werror -Wno-unused-parameter
LOCAL_INIT_RC := dumpstate.rc
+45 −47
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <unistd.h>
#include <sys/prctl.h>

#include <android-base/file.h>
#include <cutils/debugger.h>
#include <cutils/properties.h>
#include <cutils/sockets.h>
@@ -923,52 +924,38 @@ void dump_route_tables() {
}

void dump_emmc_ecsd(const char *ext_csd_path) {
    static const size_t EXT_CSD_REV = 192;
    static const size_t EXT_PRE_EOL_INFO = 267;
    static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268;
    static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269;
    // List of interesting offsets
    struct hex {
        char str[2];
    } buffer[512];
    int fd, ext_csd_rev, ext_pre_eol_info;
    ssize_t bytes_read;
    static const char *ver_str[] = {
        "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0"
    };
    static const char *eol_str[] = {
        "Undefined",
        "Normal",
        "Warning (consumed 80% of reserve)",
        "Urgent (consumed 90% of reserve)"
    };

    printf("------ %s Extended CSD ------\n", ext_csd_path);
    static const size_t EXT_CSD_REV = 192 * sizeof(hex);
    static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex);
    static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex);
    static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex);

    fd = TEMP_FAILURE_RETRY(open(ext_csd_path,
                                 O_RDONLY | O_NONBLOCK | O_CLOEXEC));
    if (fd < 0) {
        printf("*** %s: %s\n\n", ext_csd_path, strerror(errno));
    std::string buffer;
    if (!android::base::ReadFileToString(ext_csd_path, &buffer)) {
        return;
    }

    bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
    close(fd);
    if (bytes_read < 0) {
        printf("*** %s: %s\n\n", ext_csd_path, strerror(errno));
        return;
    }
    if (bytes_read < (ssize_t)(EXT_CSD_REV * sizeof(struct hex))) {
        printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read);
    printf("------ %s Extended CSD ------\n", ext_csd_path);

    if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) {
        printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
        return;
    }

    ext_csd_rev = 0;
    if (sscanf(buffer[EXT_CSD_REV].str, "%02x", &ext_csd_rev) != 1) {
        printf("*** %s: EXT_CSD_REV parse error \"%.2s\"\n\n",
               ext_csd_path, buffer[EXT_CSD_REV].str);
    int ext_csd_rev = 0;
    std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex));
    if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) {
        printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n",
               ext_csd_path, sub.c_str());
        return;
    }

    static const char *ver_str[] = {
        "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0"
    };
    printf("rev 1.%d (MMC %s)\n",
           ext_csd_rev,
           (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ?
@@ -979,17 +966,25 @@ void dump_emmc_ecsd(const char *ext_csd_path) {
        return;
    }

    if (bytes_read < (ssize_t)(EXT_PRE_EOL_INFO * sizeof(struct hex))) {
        printf("*** %s: truncated content %zd\n\n", ext_csd_path, bytes_read);
    if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) {
        printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
        return;
    }

    ext_pre_eol_info = 0;
    if (sscanf(buffer[EXT_PRE_EOL_INFO].str, "%02x", &ext_pre_eol_info) != 1) {
        printf("*** %s: PRE_EOL_INFO parse error \"%.2s\"\n\n",
               ext_csd_path, buffer[EXT_PRE_EOL_INFO].str);
    int ext_pre_eol_info = 0;
    sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex));
    if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) {
        printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n",
               ext_csd_path, sub.c_str());
        return;
    }

    static const char *eol_str[] = {
        "Undefined",
        "Normal",
        "Warning (consumed 80% of reserve)",
        "Urgent (consumed 90% of reserve)"
    };
    printf("PRE_EOL_INFO %d (MMC %s)\n",
           ext_pre_eol_info,
           eol_str[(ext_pre_eol_info < (int)
@@ -998,7 +993,7 @@ void dump_emmc_ecsd(const char *ext_csd_path) {

    for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A;
            lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B;
            ++lifetime) {
            lifetime += sizeof(hex)) {
        int ext_device_life_time_est;
        static const char *est_str[] = {
            "Undefined",
@@ -1015,21 +1010,24 @@ void dump_emmc_ecsd(const char *ext_csd_path) {
            "Exceeded the maximum estimated device lifetime",
        };

        if (bytes_read < (ssize_t)(lifetime * sizeof(struct hex))) {
            printf("*** %s: truncated content %zd\n", ext_csd_path, bytes_read);
        if (buffer.length() < (lifetime + sizeof(hex))) {
            printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length());
            break;
        }

        ext_device_life_time_est = 0;
        if (sscanf(buffer[lifetime].str, "%02x", &ext_device_life_time_est) != 1) {
            printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%.2s\"\n",
        sub = buffer.substr(lifetime, sizeof(hex));
        if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) {
            printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n",
                   ext_csd_path,
                   (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A',
                   buffer[lifetime].str);
                   (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) /
                              sizeof(hex)) + 'A',
                   sub.c_str());
            continue;
        }
        printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n",
               (unsigned)(lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) + 'A',
               (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) /
                          sizeof(hex)) + 'A',
               ext_device_life_time_est,
               est_str[(ext_device_life_time_est < (int)
                           (sizeof(est_str) / sizeof(est_str[0]))) ?