blob: b5fb13e0c03863c0dcc7227a2d346ce1ebdb5150 [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>
Jin Qian8847c622017-07-17 15:06:11 -070021#include <sys/statvfs.h>
Jin Qian4fc338e2017-03-15 19:03:06 -070022
23#include <android-base/file.h>
Jin Qian4fc338e2017-03-15 19:03:06 -070024#include <android-base/parseint.h>
Jin Qian726339c2017-03-17 16:36:59 -070025#include <android-base/logging.h>
Jin Qianb90f1ae2017-03-21 16:57:44 -070026#include <android-base/strings.h>
Jin Qian4fc338e2017-03-15 19:03:06 -070027#include <log/log_event_list.h>
28
29#include "storaged.h"
30
31using namespace std;
Jin Qian4fc338e2017-03-15 19:03:06 -070032using namespace android::base;
33
Jin Qian8847c622017-07-17 15:06:11 -070034const string emmc_info_t::emmc_sysfs = "/sys/bus/mmc/devices/mmc0:0001/";
35const string emmc_info_t::emmc_debugfs = "/d/mmc0/mmc0:0001/ext_csd";
36const char* emmc_info_t::emmc_ver_str[9] = {
37 "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0", "5.1"
38};
Jin Qianb90f1ae2017-03-21 16:57:44 -070039
Jin Qian8847c622017-07-17 15:06:11 -070040const string ufs_info_t::health_file = "/sys/devices/soc/624000.ufshc/health";
41
42static bool FileExists(const std::string& filename)
43{
44 struct stat buffer;
45 return stat(filename.c_str(), &buffer) == 0;
46}
47
48storage_info_t* storage_info_t::get_storage_info()
49{
50 if (FileExists(emmc_info_t::emmc_sysfs) ||
51 FileExists(emmc_info_t::emmc_debugfs)) {
52 return new emmc_info_t;
53 }
54 if (FileExists(ufs_info_t::health_file)) {
55 return new ufs_info_t;
56 }
57 return new storage_info_t;
58}
59
60void storage_info_t::refresh()
61{
62 struct statvfs buf;
63 if (statvfs(userdata_path.c_str(), &buf) != 0) {
64 PLOG_TO(SYSTEM, WARNING) << "Failed to get userdata info";
65 return;
66 }
67
68 userdata_total_kb = buf.f_bsize * buf.f_blocks >> 10;
69 userdata_free_kb = buf.f_bfree * buf.f_blocks >> 10;
Jin Qian726339c2017-03-17 16:36:59 -070070}
71
Jin Qian4fc338e2017-03-15 19:03:06 -070072void storage_info_t::publish()
73{
Jin Qian4fc338e2017-03-15 19:03:06 -070074 android_log_event_list(EVENTLOGTAG_EMMCINFO)
75 << version << eol << lifetime_a << lifetime_b
76 << LOG_ID_EVENTS;
77}
78
Jin Qian8847c622017-07-17 15:06:11 -070079void emmc_info_t::report()
Jin Qian726339c2017-03-17 16:36:59 -070080{
81 if (!report_sysfs() && !report_debugfs())
Jin Qian8847c622017-07-17 15:06:11 -070082 return;
Jin Qian726339c2017-03-17 16:36:59 -070083
84 publish();
Jin Qian726339c2017-03-17 16:36:59 -070085}
86
87bool emmc_info_t::report_sysfs()
Jin Qian4fc338e2017-03-15 19:03:06 -070088{
89 string buffer;
Jin Qian726339c2017-03-17 16:36:59 -070090 uint16_t rev = 0;
91
92 if (!ReadFileToString(emmc_sysfs + "rev", &buffer)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070093 return false;
94 }
95
Jin Qian726339c2017-03-17 16:36:59 -070096 if (sscanf(buffer.c_str(), "0x%hx", &rev) < 1 ||
97 rev < 7 || rev > ARRAY_SIZE(emmc_ver_str)) {
Jin Qian4fc338e2017-03-15 19:03:06 -070098 return false;
99 }
100
101 version = "emmc ";
Jin Qian726339c2017-03-17 16:36:59 -0700102 version += emmc_ver_str[rev];
Jin Qian4fc338e2017-03-15 19:03:06 -0700103
Jin Qian726339c2017-03-17 16:36:59 -0700104 if (!ReadFileToString(emmc_sysfs + "pre_eol_info", &buffer)) {
Jin Qian4fc338e2017-03-15 19:03:06 -0700105 return false;
106 }
107
Jin Qian726339c2017-03-17 16:36:59 -0700108 if (sscanf(buffer.c_str(), "%hx", &eol) < 1 || eol == 0) {
Jin Qian4fc338e2017-03-15 19:03:06 -0700109 return false;
110 }
111
Jin Qian726339c2017-03-17 16:36:59 -0700112 if (!ReadFileToString(emmc_sysfs + "life_time", &buffer)) {
Jin Qian4fc338e2017-03-15 19:03:06 -0700113 return false;
114 }
115
Jin Qian726339c2017-03-17 16:36:59 -0700116 if (sscanf(buffer.c_str(), "0x%hx 0x%hx", &lifetime_a, &lifetime_b) < 2 ||
117 (lifetime_a == 0 && lifetime_b == 0)) {
Jin Qian4fc338e2017-03-15 19:03:06 -0700118 return false;
119 }
120
121 return true;
122}
Jin Qian726339c2017-03-17 16:36:59 -0700123
124const size_t EXT_CSD_FILE_MIN_SIZE = 1024;
125/* 2 characters in string for each byte */
126const size_t EXT_CSD_REV_IDX = 192 * 2;
127const size_t EXT_PRE_EOL_INFO_IDX = 267 * 2;
128const size_t EXT_DEVICE_LIFE_TIME_EST_A_IDX = 268 * 2;
129const size_t EXT_DEVICE_LIFE_TIME_EST_B_IDX = 269 * 2;
130
131bool emmc_info_t::report_debugfs()
132{
133 string buffer;
134 uint16_t rev = 0;
135
136 if (!ReadFileToString(emmc_debugfs, &buffer) ||
137 buffer.length() < (size_t)EXT_CSD_FILE_MIN_SIZE) {
138 return false;
139 }
140
141 string str = buffer.substr(EXT_CSD_REV_IDX, 2);
142 if (!ParseUint(str, &rev) ||
143 rev < 7 || rev > ARRAY_SIZE(emmc_ver_str)) {
144 return false;
145 }
146
147 version = "emmc ";
148 version += emmc_ver_str[rev];
149
150 str = buffer.substr(EXT_PRE_EOL_INFO_IDX, 2);
151 if (!ParseUint(str, &eol)) {
152 return false;
153 }
154
155 str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_A_IDX, 2);
156 if (!ParseUint(str, &lifetime_a)) {
157 return false;
158 }
159
160 str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_B_IDX, 2);
161 if (!ParseUint(str, &lifetime_b)) {
162 return false;
163 }
164
165 return true;
Jin Qianb90f1ae2017-03-21 16:57:44 -0700166}
167
Jin Qian8847c622017-07-17 15:06:11 -0700168void ufs_info_t::report()
Jin Qianb90f1ae2017-03-21 16:57:44 -0700169{
170 string buffer;
171 if (!ReadFileToString(health_file, &buffer)) {
Jin Qian8847c622017-07-17 15:06:11 -0700172 return;
Jin Qianb90f1ae2017-03-21 16:57:44 -0700173 }
174
175 vector<string> lines = Split(buffer, "\n");
176 if (lines.empty()) {
Jin Qian8847c622017-07-17 15:06:11 -0700177 return;
Jin Qianb90f1ae2017-03-21 16:57:44 -0700178 }
179
180 char rev[8];
181 if (sscanf(lines[0].c_str(), "ufs version: 0x%7s\n", rev) < 1) {
Jin Qian8847c622017-07-17 15:06:11 -0700182 return;
Jin Qianb90f1ae2017-03-21 16:57:44 -0700183 }
184
185 version = "ufs " + string(rev);
186
187 for (size_t i = 1; i < lines.size(); i++) {
188 char token[32];
189 uint16_t val;
190 int ret;
191 if ((ret = sscanf(lines[i].c_str(),
192 "Health Descriptor[Byte offset 0x%*d]: %31s = 0x%hx",
193 token, &val)) < 2) {
194 continue;
195 }
196
197 if (string(token) == "bPreEOLInfo") {
198 eol = val;
199 } else if (string(token) == "bDeviceLifeTimeEstA") {
200 lifetime_a = val;
201 } else if (string(token) == "bDeviceLifeTimeEstB") {
202 lifetime_b = val;
203 }
204 }
205
206 if (eol == 0 || (lifetime_a == 0 && lifetime_b == 0)) {
Jin Qian8847c622017-07-17 15:06:11 -0700207 return;
Jin Qianb90f1ae2017-03-21 16:57:44 -0700208 }
209
210 publish();
Jin Qianb90f1ae2017-03-21 16:57:44 -0700211}
212