Simon Glass | 76cbda7 | 2024-07-20 11:49:42 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright 2022 Google LLC |
| 3 | # |
| 4 | """Bintool implementation for fdtgrep |
| 5 | |
| 6 | fdtgrepprovides a way to grep devicetree-binary files to extract or remove |
| 7 | certain elements. |
| 8 | |
| 9 | Usage: fdtgrep - extract portions from device tree |
| 10 | |
| 11 | Usage: |
| 12 | fdtgrep <options> <dt file>|- |
| 13 | |
| 14 | Output formats are: |
| 15 | dts - device tree soure text |
| 16 | dtb - device tree blob (sets -Hmt automatically) |
| 17 | bin - device tree fragment (may not be a valid .dtb) |
| 18 | |
| 19 | Options: -[haAc:b:C:defg:G:HIlLmn:N:o:O:p:P:rRsStTvhV] |
| 20 | -a, --show-address Display address |
| 21 | -A, --colour Show all nodes/tags, colour those that match |
| 22 | -b, --include-node-with-prop <arg> Include contains containing property |
| 23 | -c, --include-compat <arg> Compatible nodes to include in grep |
| 24 | -C, --exclude-compat <arg> Compatible nodes to exclude in grep |
| 25 | -d, --diff Diff: Mark matching nodes with +, others with - |
| 26 | -e, --enter-node Enter direct subnode names of matching nodes |
| 27 | -f, --show-offset Display offset |
| 28 | -g, --include-match <arg> Node/property/compatible string to include in grep |
| 29 | -G, --exclude-match <arg> Node/property/compatible string to exclude in grep |
| 30 | -H, --show-header Output a header |
| 31 | -I, --show-version Put "/dts-v1/;" on first line of dts output |
| 32 | -l, --list-regions Output a region list |
| 33 | -L, --list-strings List strings in string table |
| 34 | -m, --include-mem Include mem_rsvmap section in binary output |
| 35 | -n, --include-node <arg> Node to include in grep |
| 36 | -N, --exclude-node <arg> Node to exclude in grep |
| 37 | -p, --include-prop <arg> Property to include in grep |
| 38 | -P, --exclude-prop <arg> Property to exclude in grep |
| 39 | -r, --remove-strings Remove unused strings from string table |
| 40 | -R, --include-root Include root node and all properties |
| 41 | -s, --show-subnodes Show all subnodes matching nodes |
| 42 | -S, --skip-supernodes Don't include supernodes of matching nodes |
| 43 | -t, --show-stringtab Include string table in binary output |
| 44 | -T, --show-aliases Include matching aliases in output |
| 45 | -o, --out <arg> -o <output file> |
| 46 | -O, --out-format <arg> -O <output format> |
| 47 | -v, --invert-match Invert the sense of matching (select non-matching lines) |
| 48 | -h, --help Print this help and exit |
| 49 | -V, --version Print version and exit |
| 50 | """ |
| 51 | |
| 52 | import tempfile |
| 53 | |
| 54 | from u_boot_pylib import tools |
| 55 | from binman import bintool |
| 56 | |
| 57 | class Bintoolfdtgrep(bintool.Bintool): |
| 58 | """Handles the 'fdtgrep' tool |
| 59 | |
| 60 | This bintool supports running `fdtgrep` with parameters suitable for |
| 61 | producing SPL devicetrees from the main one. |
| 62 | """ |
| 63 | def __init__(self, name): |
| 64 | super().__init__(name, 'Grep devicetree files') |
| 65 | |
| 66 | def create_for_phase(self, infile, phase, outfile, remove_props): |
| 67 | """Create the FDT for a particular phase |
| 68 | |
| 69 | Args: |
| 70 | infile (str): Input filename containing the full FDT contents (with |
| 71 | all nodes and properties) |
| 72 | phase (str): Phase to generate for ('tpl', 'vpl', 'spl') |
| 73 | outfile (str): Output filename to write the grepped FDT contents to |
| 74 | (with only neceesary nodes and properties) |
| 75 | |
| 76 | Returns: |
| 77 | CommandResult: Resulting output from the bintool, or None if the |
| 78 | tool is not present |
| 79 | """ |
| 80 | if phase == 'tpl': |
| 81 | tag = 'bootph-pre-sram' |
| 82 | elif phase == 'vpl': |
| 83 | tag = 'bootph-verify' |
| 84 | elif phase == 'spl': |
| 85 | tag = 'bootph-pre-ram' |
| 86 | else: |
Simon Glass | 7081a94 | 2024-07-20 11:49:45 +0100 | [diff] [blame] | 87 | raise ValueError(f"Invalid U-Boot phase '{phase}': Use tpl/vpl/spl") |
Simon Glass | 76cbda7 | 2024-07-20 11:49:42 +0100 | [diff] [blame] | 88 | |
| 89 | # These args mirror those in cmd_fdtgrep in scripts/Makefile.lib |
| 90 | # First do the first stage |
| 91 | with tempfile.NamedTemporaryFile(prefix='fdtgrep.tmp', |
| 92 | dir=tools.get_output_dir()) as tmp: |
| 93 | args = [ |
| 94 | infile, |
| 95 | '-o', tmp.name, |
| 96 | '-b', 'bootph-all', |
| 97 | '-b', tag, |
| 98 | '-u', |
| 99 | '-RT', |
| 100 | '-n', '/chosen', |
| 101 | '-n', '/config', |
| 102 | '-O', 'dtb', |
| 103 | ] |
| 104 | self.run_cmd(*args) |
| 105 | args = [ |
| 106 | tmp.name, |
| 107 | '-o', outfile, |
| 108 | '-r', |
| 109 | '-O', 'dtb', |
| 110 | '-P', 'bootph-all', |
| 111 | '-P', 'bootph-pre-ram', |
| 112 | '-P', 'bootph-pre-sram', |
| 113 | '-P', 'bootph-verify', |
| 114 | ] |
| 115 | for prop_name in remove_props: |
| 116 | args += ['-P', prop_name] |
| 117 | return self.run_cmd(*args) |
| 118 | |
| 119 | def fetch(self, method): |
| 120 | """Fetch handler for fdtgrep |
| 121 | |
| 122 | This installs fdtgrep using the apt utility, which assumes that it is |
| 123 | packaged in u-boot tools, which it is not. |
| 124 | |
| 125 | Args: |
| 126 | method (FETCH_...): Method to use |
| 127 | |
| 128 | Returns: |
| 129 | True if the file was fetched and now installed, None if a method |
| 130 | other than FETCH_BIN was requested |
| 131 | |
| 132 | Raises: |
| 133 | Valuerror: Fetching could not be completed |
| 134 | """ |
| 135 | if method != bintool.FETCH_BIN: |
| 136 | return None |
| 137 | return self.apt_install('u-boot-tools') |