Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | A tool to extract kernel information from a kernel image. |
| 19 | """ |
| 20 | |
| 21 | import argparse |
| 22 | import subprocess |
| 23 | import sys |
| 24 | import re |
| 25 | |
| 26 | CONFIG_PREFIX = b'IKCFG_ST' |
| 27 | GZIP_HEADER = b'\037\213\010' |
| 28 | COMPRESSION_ALGO = ( |
| 29 | (["gzip", "-d"], GZIP_HEADER), |
| 30 | (["xz", "-d"], b'\3757zXZ\000'), |
| 31 | (["bzip2", "-d"], b'BZh'), |
| 32 | (["lz4", "-d", "-l"], b'\002\041\114\030'), |
| 33 | |
| 34 | # These are not supported in the build system yet. |
| 35 | # (["unlzma"], b'\135\0\0\0'), |
| 36 | # (["lzop", "-d"], b'\211\114\132'), |
| 37 | ) |
| 38 | |
| 39 | # "Linux version " UTS_RELEASE " (" LINUX_COMPILE_BY "@" |
| 40 | # LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION "\n"; |
| 41 | LINUX_BANNER_PREFIX = b'Linux version ' |
| 42 | LINUX_BANNER_REGEX = LINUX_BANNER_PREFIX + \ |
| 43 | r'([0-9]+[.][0-9]+[.][0-9]+).* \(.*@.*\) \(.*\) .*\n' |
| 44 | |
| 45 | |
| 46 | def get_version(input_bytes, start_idx): |
| 47 | null_idx = input_bytes.find('\x00', start_idx) |
| 48 | if null_idx < 0: |
| 49 | return None |
Andrew Chant | 5d323c1 | 2019-07-08 15:53:00 -0700 | [diff] [blame] | 50 | try: |
| 51 | linux_banner = input_bytes[start_idx:null_idx].decode() |
| 52 | except UnicodeDecodeError: |
| 53 | return None |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 54 | mo = re.match(LINUX_BANNER_REGEX, linux_banner) |
| 55 | if mo: |
| 56 | return mo.group(1) |
| 57 | return None |
| 58 | |
| 59 | |
| 60 | def dump_version(input_bytes): |
| 61 | idx = 0 |
| 62 | while True: |
| 63 | idx = input_bytes.find(LINUX_BANNER_PREFIX, idx) |
| 64 | if idx < 0: |
| 65 | return None |
| 66 | |
| 67 | version = get_version(input_bytes, idx) |
| 68 | if version: |
| 69 | return version |
| 70 | |
| 71 | idx += len(LINUX_BANNER_PREFIX) |
| 72 | |
| 73 | |
| 74 | def dump_configs(input_bytes): |
| 75 | """ |
| 76 | Dump kernel configuration from input_bytes. This can be done when |
| 77 | CONFIG_IKCONFIG is enabled, which is a requirement on Treble devices. |
| 78 | |
| 79 | The kernel configuration is archived in GZip format right after the magic |
| 80 | string 'IKCFG_ST' in the built kernel. |
| 81 | """ |
| 82 | |
| 83 | # Search for magic string + GZip header |
| 84 | idx = input_bytes.find(CONFIG_PREFIX + GZIP_HEADER) |
| 85 | if idx < 0: |
| 86 | return None |
| 87 | |
| 88 | # Seek to the start of the archive |
| 89 | idx += len(CONFIG_PREFIX) |
| 90 | |
| 91 | sp = subprocess.Popen(["gzip", "-d", "-c"], stdin=subprocess.PIPE, |
| 92 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 93 | o, _ = sp.communicate(input=input_bytes[idx:]) |
| 94 | if sp.returncode == 1: # error |
| 95 | return None |
| 96 | |
| 97 | # success or trailing garbage warning |
| 98 | assert sp.returncode in (0, 2), sp.returncode |
| 99 | |
| 100 | return o |
| 101 | |
| 102 | |
| 103 | def try_decompress(cmd, search_bytes, input_bytes): |
| 104 | idx = input_bytes.find(search_bytes) |
| 105 | if idx < 0: |
| 106 | return None |
| 107 | |
| 108 | idx = 0 |
| 109 | sp = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 110 | stderr=subprocess.PIPE) |
| 111 | o, _ = sp.communicate(input=input_bytes[idx:]) |
| 112 | # ignore errors |
| 113 | return o |
| 114 | |
| 115 | |
| 116 | def decompress_dump(func, input_bytes): |
| 117 | """ |
| 118 | Run func(input_bytes) first; and if that fails (returns value evaluates to |
| 119 | False), then try different decompression algorithm before running func. |
| 120 | """ |
| 121 | o = func(input_bytes) |
| 122 | if o: |
| 123 | return o |
| 124 | for cmd, search_bytes in COMPRESSION_ALGO: |
| 125 | decompressed = try_decompress(cmd, search_bytes, input_bytes) |
| 126 | if decompressed: |
| 127 | o = func(decompressed) |
| 128 | if o: |
| 129 | return o |
| 130 | # Force decompress the whole file even if header doesn't match |
| 131 | decompressed = try_decompress(cmd, b"", input_bytes) |
| 132 | if decompressed: |
| 133 | o = func(decompressed) |
| 134 | if o: |
| 135 | return o |
| 136 | |
| 137 | def main(): |
| 138 | parser = argparse.ArgumentParser( |
| 139 | formatter_class=argparse.RawTextHelpFormatter, |
| 140 | description=__doc__ + |
| 141 | "\nThese algorithms are tried when decompressing the image:\n " + |
| 142 | " ".join(tup[0][0] for tup in COMPRESSION_ALGO)) |
| 143 | parser.add_argument('--input', |
| 144 | help='Input kernel image. If not specified, use stdin', |
| 145 | metavar='FILE', |
| 146 | type=argparse.FileType('rb'), |
| 147 | default=sys.stdin) |
| 148 | parser.add_argument('--output-configs', |
| 149 | help='If specified, write configs. Use stdout if no file ' |
| 150 | 'is specified.', |
| 151 | metavar='FILE', |
| 152 | nargs='?', |
| 153 | type=argparse.FileType('wb'), |
| 154 | const=sys.stdout) |
| 155 | parser.add_argument('--output-version', |
| 156 | help='If specified, write version. Use stdout if no file ' |
| 157 | 'is specified.', |
| 158 | metavar='FILE', |
| 159 | nargs='?', |
| 160 | type=argparse.FileType('wb'), |
| 161 | const=sys.stdout) |
| 162 | parser.add_argument('--tools', |
| 163 | help='Decompression tools to use. If not specified, PATH ' |
| 164 | 'is searched.', |
| 165 | metavar='ALGORITHM:EXECUTABLE', |
| 166 | nargs='*') |
| 167 | args = parser.parse_args() |
| 168 | |
| 169 | tools = {pair[0]: pair[1] |
| 170 | for pair in (token.split(':') for token in args.tools or [])} |
| 171 | for cmd, _ in COMPRESSION_ALGO: |
| 172 | if cmd[0] in tools: |
| 173 | cmd[0] = tools[cmd[0]] |
| 174 | |
| 175 | input_bytes = args.input.read() |
| 176 | |
| 177 | ret = 0 |
| 178 | if args.output_configs is not None: |
| 179 | o = decompress_dump(dump_configs, input_bytes) |
| 180 | if o: |
| 181 | args.output_configs.write(o) |
| 182 | else: |
| 183 | sys.stderr.write( |
| 184 | "Cannot extract kernel configs in {}".format(args.input.name)) |
| 185 | ret = 1 |
| 186 | if args.output_version is not None: |
| 187 | o = decompress_dump(dump_version, input_bytes) |
| 188 | if o: |
| 189 | args.output_version.write(o) |
| 190 | else: |
| 191 | sys.stderr.write( |
| 192 | "Cannot extract kernel versions in {}".format(args.input.name)) |
| 193 | ret = 1 |
| 194 | |
| 195 | return ret |
| 196 | |
| 197 | |
| 198 | if __name__ == '__main__': |
| 199 | exit(main()) |