github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/acpigrep/main.go (about)

     1  // Copyright 2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // grep a stream of ACPI tables by regexp
     6  //
     7  // Synopsis:
     8  //
     9  //	acpigrep [-v] [-d] regexp
    10  //
    11  // Description:
    12  //
    13  //		Read tables from stdin and write tables with ACPI signatures
    14  //		matching a pattern (or not matching, with -v, as in grep)
    15  //		to stdout.
    16  //
    17  //		Read all tables from sysfs and discard MADT
    18  //		sudo cat /sys/firmware/acpi/tables/[A-Z]* | ./acpigrep -v MADT
    19  //
    20  //		Read a large blob and print out its tables
    21  //		acpigrep -d '.*' >/dev/null < blob
    22  //
    23  //	     Read all the files in /sys and discard any DSDT. Useful for coreboot work.
    24  //		sudo cat /sys/firmware/acpi/tables/[A-Z]* | ./acpigrep -v DSDT > nodsdt.bin
    25  //
    26  //		Read all the files, keeping only SRAT and MADT
    27  //		sudo cat /sys/firmware/acpi/tables/[A-Z]* | ./acpigrep 'MADT|SRAT' > madtsrat.bin
    28  //
    29  //		Read all the files, keeping only SRAT and MADT, and print what is done
    30  //		sudo cat /sys/firmware/acpi/tables/[A-Z]* | ./acpigrep -d 'MADT|SRAT' > madtsrat.bin
    31  //
    32  // Options:
    33  //
    34  //	-d print debug information about what is kept and what is discarded.
    35  //	-v reverse the sense of the match to "discard is matching"
    36  package main
    37  
    38  import (
    39  	"flag"
    40  	"log"
    41  	"os"
    42  	"regexp"
    43  
    44  	"github.com/mvdan/u-root-coreutils/pkg/acpi"
    45  )
    46  
    47  var (
    48  	v     = flag.Bool("v", false, "Only non-matching signatures will be kept")
    49  	d     = flag.Bool("d", false, "Print debug messages")
    50  	debug = func(string, ...interface{}) {}
    51  )
    52  
    53  func main() {
    54  	flag.Parse()
    55  	if *d {
    56  		debug = log.Printf
    57  	}
    58  	if len(flag.Args()) != 1 {
    59  		log.Fatal("Usage: acpigrep [-v] [-d] pattern")
    60  	}
    61  	r := regexp.MustCompile(flag.Args()[0])
    62  	tabs, err := acpi.RawFromFile(os.Stdin)
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  	for _, t := range tabs {
    67  		m := r.MatchString(t.Sig())
    68  		if m == *v {
    69  			debug("Dropping %s", acpi.String(t))
    70  			continue
    71  		}
    72  		debug("Keeping %s", acpi.String(t))
    73  		os.Stdout.Write(t.Data())
    74  	}
    75  }