storage: dump UFS error history when capturing bugreport

Bug: 264001741
Test: capture bugreport
Change-Id: Ie910377c6378240188ff09b2a686efb02b2ac972
Signed-off-by: Randall Huang <huangrandall@google.com>
diff --git a/storage/dump_storage.cpp b/storage/dump_storage.cpp
index bba1071..e6b3e4c 100644
--- a/storage/dump_storage.cpp
+++ b/storage/dump_storage.cpp
@@ -16,16 +16,47 @@
 
 #include <dump/pixel_dump.h>
 #include <android-base/file.h>
+#include <android-base/stringprintf.h>
 #include <android-base/properties.h>
 #include <string.h>
 #include <stdio.h>
 #include <log/log.h>
 #include <regex>
 #include <fstream>
+#include <map>
 
 #define F2FS_FSCK_TIME_PROPERTY "ro.boottime.init.fsck.data"
 #define F2FS_MNT_TIME_PROPERTY "ro.boottime.init.mount.data"
 #define BOOTDEVICE_PROPERTY "ro.boot.bootdevice"
+#define BUILD_TYPE_PROPERTY "ro.build.type"
+
+void read_buffer(int buf_id, int total_len, const char* path)
+{
+    int i, len;
+    system("mkdir -m 0770 -p /data/vendor/storage/");
+    auto cmd = android::base::StringPrintf("rm -f /data/vendor/storage/%s\n", path);
+    system(cmd.c_str());
+
+    for (i=0;i<=total_len;i+=524288) {
+        if (total_len-i < 524288) {
+            len = total_len-i;
+            printf("/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
+                buf_id, len, i, path);
+            auto cmd = android::base::StringPrintf(
+                "/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
+                buf_id, len, i, path);
+            system(cmd.c_str());
+            break;
+        }
+        len = 524288;
+        printf("/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
+            buf_id, len, i, path);
+        auto cmd = android::base::StringPrintf(
+            "/vendor/bin/sg_read_buffer -m 0x1c -i %d -l %d -o %d -r /dev/sg3 >> /data/vendor/storage/%s\n",
+            buf_id, len, i, path);
+        system(cmd.c_str());
+    }
+}
 
 int main() {
     //F2FS
@@ -146,5 +177,68 @@
             "",
             "/dev/sys/block/bootdevice/health_descriptor/life_time_estimation_c");
 
+    printf("\n------ UFS error history ------\n");
+    std::string build_type = android::base::GetProperty(BUILD_TYPE_PROPERTY, "");
+    if (build_type == "userdebug") {
+        std::string sg_read_buffer = "/vendor/bin/sg_read_buffer";
+        std::ifstream sg_read_buffer_file(sg_read_buffer.c_str());
+        if (sg_read_buffer_file.is_open()) {
+            const std::string ufs_ver_path(
+                "/dev/sys/block/bootdevice/device_descriptor/specification_version");
+            std::ifstream ufs_ver_file(ufs_ver_path);
+            std::string ufs_ver;
+            if (ufs_ver_file.is_open()) {
+                ufs_ver_file >> ufs_ver;
+                ufs_ver_file.close();
+            }
+            if (strcmp(ufs_ver.c_str(), "0x0210")) {
+                const std::string ufs_brand_path("/sys/block/sda/device/vendor");
+                std::ifstream ufs_brand_file(ufs_brand_path);
+                std::string ufs_brand;
+                if (ufs_brand_file.is_open()) {
+                    ufs_brand_file >> ufs_brand;
+                    ufs_brand_file.close();
+                }
+
+                std::map<std::string, int> const table =
+                    {
+                    {"MICRON", 0x12C},
+                    {"KIOXIA", 0x198},
+                    {"SKhynix", 0x1AD},
+                    {"SAMSUNG", 0x1CE}
+                    };
+                auto ufs_vendor_id = table.find(ufs_brand);
+                if (ufs_vendor_id != table.end()) {
+                    switch(ufs_vendor_id->second){
+                      case 0x12C: //MICRON
+                        read_buffer(16, 2097152, "micron_10_ufs_err_history.dat");
+                        read_buffer(18, 10485760, "micron_12_ufs_err_history.dat");
+                        read_buffer(19, 10485760, "micron_13_ufs_err_history.dat");
+                        break;
+                      case 0x198: //KIOXIA
+                        read_buffer(16, 16773120, "kioxia_10_ufs_err_history.dat");
+                        read_buffer(17, 2097152, "kioxia_11_ufs_err_history.dat");
+                        read_buffer(18, 131072, "kioxia_12_ufs_err_history.dat");
+                        break;
+                      case 0x1AD: //SKhynix
+                        read_buffer(0, 4096, "hynix_00_ufs_err_history.dat");
+                        read_buffer(16, 131072, "skhynix_10_ufs_err_history.dat");
+                        read_buffer(17, 131072, "skhynix_11_ufs_err_history.dat");
+                        read_buffer(18, 131072, "skhynix_12_ufs_err_history.dat");
+                        read_buffer(19, 131072, "skhynix_13_ufs_err_history.dat");
+                        break;
+                      case 0x1CE: //SAMSUNG
+                        read_buffer(16, 8404992, "samsung_10_ufs_err_history.dat");
+                        break;
+                      default:
+                        break;
+                    }
+                }
+                sg_read_buffer_file.close();
+            }
+        } else
+          printf("sg_read_buffer does not exist\n");
+    }
+
     return 0;
 }
diff --git a/storage/init.storage.rc b/storage/init.storage.rc
index 445f74b..56e8e4f 100644
--- a/storage/init.storage.rc
+++ b/storage/init.storage.rc
@@ -1,2 +1,3 @@
 on property:ro.build.type=userdebug
     write /dev/sys/block/bootdevice/pixel/enable_pixel_ufs_logging 1
+    chown system /dev/sg3
diff --git a/storage/sepolicy/dump_storage.te b/storage/sepolicy/dump_storage.te
index a404ee2..5324c17 100644
--- a/storage/sepolicy/dump_storage.te
+++ b/storage/sepolicy/dump_storage.te
@@ -6,6 +6,15 @@
 userdebug_or_eng(`
   allow dump_storage debugfs_f2fs:dir r_dir_perms;
   allow dump_storage debugfs_f2fs:file r_file_perms;
+  allow dump_storage sg_device:chr_file rw_file_perms;
+  allow dump_storage proc:file r_file_perms;
+  allow dump_storage vendor_shell_exec:file execute_no_trans;
+  allow dump_storage vendor_toolbox_exec:file execute_no_trans;
+  allow dump_storage sg_util_exec:file execute_no_trans;
+  allow dump_storage vendor_data_file:dir create_dir_perms;
+  allow dump_storage vendor_data_file:file create_file_perms;
+  allow dump_storage dump_storage_data_file:dir rw_dir_perms;
+  allow dump_storage dump_storage_data_file:file create_file_perms;
 ')
 
 get_prop(dump_storage, boottime_public_prop)
diff --git a/storage/sepolicy/file.te b/storage/sepolicy/file.te
index 64b6b4e..c1f082d 100644
--- a/storage/sepolicy/file.te
+++ b/storage/sepolicy/file.te
@@ -1 +1,3 @@
 type debugfs_f2fs, debugfs_type, fs_type;
+type dump_storage_data_file, file_type, data_file_type;
+type sg_util_exec, exec_type, vendor_file_type, file_type;
diff --git a/storage/sepolicy/file_contexts b/storage/sepolicy/file_contexts
index bc10bc8..ecca954 100644
--- a/storage/sepolicy/file_contexts
+++ b/storage/sepolicy/file_contexts
@@ -1,2 +1,5 @@
 /vendor/bin/dump/dump_storage      u:object_r:dump_storage_exec:s0
 /sys/devices/platform/[0-9]+\.ufs/pixel/enable_pixel_ufs_logging  u:object_r:sysfs_scsi_devices_0000:s0
+/dev/sg[0-9]                       u:object_r:sg_device:s0
+/data/vendor/storage(/.*)?         u:object_r:dump_storage_data_file:s0
+/vendor/bin/sg_read_buffer         u:object_r:sg_util_exec:s0