xref: /aosp_15_r20/external/cpuinfo/scripts/arm-linux-filesystem-dump.py (revision 2b54f0db79fd8303838913b20ff3780cddaa909f)
1#!/usr/bin/env python
2
3import os
4import sys
5import argparse
6import shutil
7
8
9parser = argparse.ArgumentParser(description='Android system files extractor')
10parser.add_argument("-p", "--prefix", metavar="NAME", required=True,
11                    help="Prefix for stored files, e.g. galaxy-s7-us")
12
13
14SYSTEM_FILES = [
15    "/proc/cpuinfo",
16    "/sys/devices/system/cpu/kernel_max",
17    "/sys/devices/system/cpu/possible",
18    "/sys/devices/system/cpu/present",
19]
20
21CPU_FILES = [
22    "cpufreq/cpuinfo_max_freq",
23    "cpufreq/cpuinfo_min_freq",
24    "topology/physical_package_id",
25    "topology/core_siblings_list",
26    "topology/core_id",
27    "topology/thread_siblings_list",
28]
29
30CACHE_FILES = [
31    "allocation_policy",
32    "coherency_line_size",
33    "level",
34    "number_of_sets",
35    "shared_cpu_list",
36    "size",
37    "type",
38    "ways_of_associativity",
39    "write_policy",
40]
41
42def c_escape(string):
43    c_string = ""
44    for c in string:
45        if c == "\\":
46            c_string += "\\\\"
47        elif c == "\"":
48            c_string += "\\\""
49        elif c == "\t":
50            c_string += "\\t"
51        elif c == "\n":
52            c_string += "\\n"
53        elif c == "\r":
54            c_string += "\\r"
55        elif ord(c) == 0:
56            c_string += "\\0"
57        elif 32 <= ord(c) < 127:
58            c_string += c
59        else:
60            c_string += "x%02X" % ord(c)
61    return c_string
62
63
64def dump_system_file(stream, path):
65    try:
66        with open(path, "rb") as device_file:
67            content = device_file.read()
68            stream.write("\t{\n")
69            stream.write("\t\t.path = \"%s\",\n" % path)
70            stream.write("\t\t.size = %d,\n" % len(content))
71            if len(content.splitlines()) > 1:
72                stream.write("\t\t.content =")
73                for line in content.splitlines(True):
74                    stream.write("\n\t\t\t\"%s\"" % c_escape(line))
75                stream.write(",\n")
76            else:
77                stream.write("\t\t.content = \"%s\",\n" % c_escape(content))
78            stream.write("\t},\n")
79            return True
80    except IOError:
81        pass
82
83
84def main(args):
85    options = parser.parse_args(args)
86
87    # with open(os.path.join("test", "dmesg", options.prefix + ".log"), "w") as dmesg_log:
88    #     dmesg_log.write(device.Shell("dmesg"))
89    with open(os.path.join("test", options.prefix + ".h"), "w") as file_header:
90        file_header.write("struct cpuinfo_mock_file filesystem[] = {\n")
91        for path in SYSTEM_FILES:
92            dump_system_file(file_header, path)
93        for cpu in range(16):
94            for filename in CPU_FILES:
95                path = "/sys/devices/system/cpu/cpu%d/%s" % (cpu, filename)
96                dump_system_file(file_header, path)
97            for index in range(10):
98                for filename in CACHE_FILES:
99                    path = "/sys/devices/system/cpu/cpu%d/cache/index%d/%s" % (cpu, index, filename)
100                    dump_system_file(file_header, path)
101        file_header.write("\t{ NULL },\n")
102        file_header.write("};\n")
103    shutil.copy("/proc/cpuinfo",
104        os.path.join("test", "cpuinfo", options.prefix + ".log"))
105
106if __name__ == "__main__":
107    main(sys.argv[1:])
108