blob: 1a5da411f4c781f57b1bc4945e2b4079c30b0dba [file] [log] [blame]
Jin Qian4fc338e2017-03-15 19:03:06 -07001/*
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
Jin Qian726339c2017-03-17 16:36:59 -070019#include <stdio.h>
Jin Qian4fc338e2017-03-15 19:03:06 -070020#include <string.h>
21
22#include <android-base/file.h>
Jin Qian4fc338e2017-03-15 19:03:06 -070023#include <android-base/parseint.h>
Jin Qian726339c2017-03-17 16:36:59 -070024#include <android-base/logging.h>
Jin Qian4fc338e2017-03-15 19:03:06 -070025#include <log/log_event_list.h>
26
27#include "storaged.h"
28
29using namespace std;
Jin Qian4fc338e2017-03-15 19:03:06 -070030using namespace android::base;
31
Jin Qian726339c2017-03-17 16:36:59 -070032void report_storage_health()
33{
34 emmc_info_t mmc;
35 mmc.report();
36}
37
Jin Qian4fc338e2017-03-15 19:03:06 -070038void storage_info_t::publish()
39{
Jin Qian4fc338e2017-03-15 19:03:06 -070040 android_log_event_list(EVENTLOGTAG_EMMCINFO)
41 << version << eol << lifetime_a << lifetime_b
42 << LOG_ID_EVENTS;
43}
44
Jin Qian726339c2017-03-17 16:36:59 -070045bool emmc_info_t::report()
46{
47 if (!report_sysfs() && !report_debugfs())
48 return false;
49
50 publish();
51 return true;
52}
53
54bool emmc_info_t::report_sysfs()
Jin Qian4fc338e2017-03-15 19:03:06 -070055{
56 string buffer;
Jin Qian726339c2017-03-17 16:36:59 -070057 uint16_t rev = 0;
58
59 if (!ReadFileToString(emmc_sysfs + "rev", &buffer)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070060 return false;
61 }
62
Jin Qian726339c2017-03-17 16:36:59 -070063 if (sscanf(buffer.c_str(), "0x%hx", &rev) < 1 ||
64 rev < 7 || rev > ARRAY_SIZE(emmc_ver_str)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070065 return false;
66 }
67
68 version = "emmc ";
Jin Qian726339c2017-03-17 16:36:59 -070069 version += emmc_ver_str[rev];
Jin Qian4fc338e2017-03-15 19:03:06 -070070
Jin Qian726339c2017-03-17 16:36:59 -070071 if (!ReadFileToString(emmc_sysfs + "pre_eol_info", &buffer)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070072 return false;
73 }
74
Jin Qian726339c2017-03-17 16:36:59 -070075 if (sscanf(buffer.c_str(), "%hx", &eol) < 1 || eol == 0) {
Jin Qian4fc338e2017-03-15 19:03:06 -070076 return false;
77 }
78
Jin Qian726339c2017-03-17 16:36:59 -070079 if (!ReadFileToString(emmc_sysfs + "life_time", &buffer)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070080 return false;
81 }
82
Jin Qian726339c2017-03-17 16:36:59 -070083 if (sscanf(buffer.c_str(), "0x%hx 0x%hx", &lifetime_a, &lifetime_b) < 2 ||
84 (lifetime_a == 0 && lifetime_b == 0)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070085 return false;
86 }
87
88 return true;
89}
Jin Qian726339c2017-03-17 16:36:59 -070090
91const size_t EXT_CSD_FILE_MIN_SIZE = 1024;
92/* 2 characters in string for each byte */
93const size_t EXT_CSD_REV_IDX = 192 * 2;
94const size_t EXT_PRE_EOL_INFO_IDX = 267 * 2;
95const size_t EXT_DEVICE_LIFE_TIME_EST_A_IDX = 268 * 2;
96const size_t EXT_DEVICE_LIFE_TIME_EST_B_IDX = 269 * 2;
97
98bool emmc_info_t::report_debugfs()
99{
100 string buffer;
101 uint16_t rev = 0;
102
103 if (!ReadFileToString(emmc_debugfs, &buffer) ||
104 buffer.length() < (size_t)EXT_CSD_FILE_MIN_SIZE) {
105 return false;
106 }
107
108 string str = buffer.substr(EXT_CSD_REV_IDX, 2);
109 if (!ParseUint(str, &rev) ||
110 rev < 7 || rev > ARRAY_SIZE(emmc_ver_str)) {
111 return false;
112 }
113
114 version = "emmc ";
115 version += emmc_ver_str[rev];
116
117 str = buffer.substr(EXT_PRE_EOL_INFO_IDX, 2);
118 if (!ParseUint(str, &eol)) {
119 return false;
120 }
121
122 str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_A_IDX, 2);
123 if (!ParseUint(str, &lifetime_a)) {
124 return false;
125 }
126
127 str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_B_IDX, 2);
128 if (!ParseUint(str, &lifetime_b)) {
129 return false;
130 }
131
132 return true;
133}