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 | |
Yifan Hong | 8b72776 | 2019-09-04 12:40:13 -0700 | [diff] [blame] | 103 | def try_decompress_bytes(cmd, input_bytes): |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 104 | sp = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 105 | stderr=subprocess.PIPE) |
Yifan Hong | 8b72776 | 2019-09-04 12:40:13 -0700 | [diff] [blame] | 106 | o, _ = sp.communicate(input=input_bytes) |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 107 | # ignore errors |
| 108 | return o |
| 109 | |
| 110 | |
Yifan Hong | 8b72776 | 2019-09-04 12:40:13 -0700 | [diff] [blame] | 111 | def try_decompress(cmd, search_bytes, input_bytes): |
| 112 | idx = 0 |
| 113 | while True: |
| 114 | idx = input_bytes.find(search_bytes, idx) |
| 115 | if idx < 0: |
| 116 | raise StopIteration() |
| 117 | |
| 118 | yield try_decompress_bytes(cmd, input_bytes[idx:]) |
| 119 | idx += 1 |
| 120 | |
| 121 | |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 122 | def decompress_dump(func, input_bytes): |
| 123 | """ |
| 124 | Run func(input_bytes) first; and if that fails (returns value evaluates to |
| 125 | False), then try different decompression algorithm before running func. |
| 126 | """ |
| 127 | o = func(input_bytes) |
| 128 | if o: |
| 129 | return o |
| 130 | for cmd, search_bytes in COMPRESSION_ALGO: |
Yifan Hong | 8b72776 | 2019-09-04 12:40:13 -0700 | [diff] [blame] | 131 | for decompressed in try_decompress(cmd, search_bytes, input_bytes): |
| 132 | if decompressed: |
| 133 | o = decompress_dump(func, decompressed) |
| 134 | if o: |
| 135 | return o |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 136 | # Force decompress the whole file even if header doesn't match |
Yifan Hong | 8b72776 | 2019-09-04 12:40:13 -0700 | [diff] [blame] | 137 | decompressed = try_decompress_bytes(cmd, input_bytes) |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 138 | if decompressed: |
Yifan Hong | 8b72776 | 2019-09-04 12:40:13 -0700 | [diff] [blame] | 139 | o = decompress_dump(func, decompressed) |
Yifan Hong | 770ab05 | 2018-11-02 13:43:30 -0700 | [diff] [blame] | 140 | if o: |
| 141 | return o |
| 142 | |
| 143 | def main(): |
| 144 | parser = argparse.ArgumentParser( |
| 145 | formatter_class=argparse.RawTextHelpFormatter, |
| 146 | description=__doc__ + |
| 147 | "\nThese algorithms are tried when decompressing the image:\n " + |
| 148 | " ".join(tup[0][0] for tup in COMPRESSION_ALGO)) |
| 149 | parser.add_argument('--input', |
| 150 | help='Input kernel image. If not specified, use stdin', |
| 151 | metavar='FILE', |
| 152 | type=argparse.FileType('rb'), |
| 153 | default=sys.stdin) |
| 154 | parser.add_argument('--output-configs', |
| 155 | help='If specified, write configs. Use stdout if no file ' |
| 156 | 'is specified.', |
| 157 | metavar='FILE', |
| 158 | nargs='?', |
| 159 | type=argparse.FileType('wb'), |
| 160 | const=sys.stdout) |
| 161 | parser.add_argument('--output-version', |
| 162 | help='If specified, write version. Use stdout if no file ' |
| 163 | 'is specified.', |
| 164 | metavar='FILE', |
| 165 | nargs='?', |
| 166 | type=argparse.FileType('wb'), |
| 167 | const=sys.stdout) |
| 168 | parser.add_argument('--tools', |
| 169 | help='Decompression tools to use. If not specified, PATH ' |
| 170 | 'is searched.', |
| 171 | metavar='ALGORITHM:EXECUTABLE', |
| 172 | nargs='*') |
| 173 | args = parser.parse_args() |
| 174 | |
| 175 | tools = {pair[0]: pair[1] |
| 176 | for pair in (token.split(':') for token in args.tools or [])} |
| 177 | for cmd, _ in COMPRESSION_ALGO: |
| 178 | if cmd[0] in tools: |
| 179 | cmd[0] = tools[cmd[0]] |
| 180 | |
| 181 | input_bytes = args.input.read() |
| 182 | |
| 183 | ret = 0 |
| 184 | if args.output_configs is not None: |
| 185 | o = decompress_dump(dump_configs, input_bytes) |
| 186 | if o: |
| 187 | args.output_configs.write(o) |
| 188 | else: |
| 189 | sys.stderr.write( |
| 190 | "Cannot extract kernel configs in {}".format(args.input.name)) |
| 191 | ret = 1 |
| 192 | if args.output_version is not None: |
| 193 | o = decompress_dump(dump_version, input_bytes) |
| 194 | if o: |
| 195 | args.output_version.write(o) |
| 196 | else: |
| 197 | sys.stderr.write( |
| 198 | "Cannot extract kernel versions in {}".format(args.input.name)) |
| 199 | ret = 1 |
| 200 | |
| 201 | return ret |
| 202 | |
| 203 | |
| 204 | if __name__ == '__main__': |
| 205 | exit(main()) |