summaryrefslogtreecommitdiff
path: root/packages/linux-kernel/list-required-firmware.py
blob: bbb8c7d7a8156853ce9e6bef776814d560ad1b2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# Copyright (C) 2020 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import argparse
import re
import os
import sys
import glob

def load_config(path):
    with open(path, 'r') as f:
        config = f.read()
    targets = re.findall(r'(.*)=(?:y|m)', config)
    return targets

def find_subdirs(config, path):
    try:
        with open(os.path.join(path, 'Makefile'), 'r') as f:
            makefile = f.read()
    except OSError:
        # No Makefile
        return []

    dir_stmts = re.findall(r'obj-\$\((.*)\)\s+\+=\s+(.*)(?:\n|$)', makefile)
    subdirs = []

    for ds in dir_stmts:
        if args.debug:
            print("Processing make targets from {0} ({1})".format(ds[1], ds[0]), file=sys.stderr)
        if ds[0] in config:
            dirname = os.path.dirname(ds[1])
            if dirname:
                subdirs.append(dirname)
        elif args.debug:
            print("{0} is disabled in the config, ignoring {1}".format(ds[0], ds[1]), file=sys.stderr)

    return subdirs


def find_firmware(file):
    with open(file, 'r') as f:
        source = f.read()
    fws = re.findall(r'MODULE_FIRMWARE\((.*)\)', source)
    return fws

def walk_dir(config, path):
    subdirs = find_subdirs(config, path)

    if args.debug:
        print("Looking for C files in {0}".format(path), file=sys.stderr)
    c_files = glob.glob("{0}/*.c".format(path))

    for cf in c_files:
        fws = find_firmware(cf)
        if fws:
            print(cf)
            if args.debug:
                print("Referenced firmware: {0}".format(fws))

    for d in subdirs:
        d = os.path.join(path, d)
        walk_dir(config, d)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--source-dir", action="append", help="Kernel source directory")
    parser.add_argument("-k", "--kernel-dir", action="store", help="Kernel source directory")
    parser.add_argument("-c", "--kernel-config", action="store", help="Kernel configuration")
    parser.add_argument("-d", "--debug", action="store_true", help="Enable Debug output")
    args = parser.parse_args()

    if args.source_dir and args.kernel_dir and args.kernel_config:
        config = load_config(args.kernel_config)

        cwd = os.getcwd()
        os.chdir(f'{cwd}/{args.kernel_dir}')
        for directory in args.source_dir:
            walk_dir(config, directory)

    else:
        parser.print_help()
        sys.exit(1)