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