storaged_info.cpp revision 4fc338e60bf1d85212f1540d109beb1b248c4830
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "storaged"
18
19#include <string.h>
20
21#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/parseint.h>
24#include <log/log_event_list.h>
25
26#include "storaged.h"
27
28using namespace std;
29using namespace android;
30using namespace android::base;
31
32void storage_info_t::publish()
33{
34    if (eol == 0 && lifetime_a == 0 && lifetime_b == 0) {
35        return;
36    }
37
38    android_log_event_list(EVENTLOGTAG_EMMCINFO)
39        << version << eol << lifetime_a << lifetime_b
40        << LOG_ID_EVENTS;
41}
42
43bool emmc_info_t::init()
44{
45    string buffer;
46    if (!ReadFileToString(ext_csd_file, &buffer) ||
47        buffer.length() < (size_t)EXT_CSD_FILE_MIN_SIZE) {
48        return false;
49    }
50
51    string ver_str = buffer.substr(EXT_CSD_REV_IDX, sizeof(str_hex));
52    uint8_t ext_csd_rev;
53    if (!ParseUint(ver_str, &ext_csd_rev)) {
54        LOG_TO(SYSTEM, ERROR) << "Failure on parsing EXT_CSD_REV.";
55        return false;
56    }
57
58    version = "emmc ";
59    version += (ext_csd_rev < ARRAY_SIZE(emmc_ver_str)) ?
60                emmc_ver_str[ext_csd_rev] : "Unknown";
61
62    if (ext_csd_rev < 7) {
63        return false;
64    }
65
66    return update();
67}
68
69bool emmc_info_t::update()
70{
71    string buffer;
72    if (!ReadFileToString(ext_csd_file, &buffer) ||
73        buffer.length() < (size_t)EXT_CSD_FILE_MIN_SIZE) {
74        return false;
75    }
76
77    string str = buffer.substr(EXT_PRE_EOL_INFO_IDX, sizeof(str_hex));
78    if (!ParseUint(str, &eol)) {
79        LOG_TO(SYSTEM, ERROR) << "Failure on parsing EXT_PRE_EOL_INFO.";
80        return false;
81    }
82
83    str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_A_IDX, sizeof(str_hex));
84    if (!ParseUint(str, &lifetime_a)) {
85        LOG_TO(SYSTEM, ERROR)
86            << "Failure on parsing EXT_DEVICE_LIFE_TIME_EST_TYP_A.";
87        return false;
88    }
89
90    str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_B_IDX, sizeof(str_hex));
91    if (!ParseUint(str, &lifetime_b)) {
92        LOG_TO(SYSTEM, ERROR)
93            << "Failure on parsing EXT_DEVICE_LIFE_TIME_EST_TYP_B.";
94        return false;
95    }
96
97    return true;
98}
99