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