github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/fdtdump/fdtdump.go (about)

     1  // Copyright 2019 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  // fdtdump prints a readable version of Flattened Device Tree or dtb.
     6  //
     7  // Synopsis:
     8  //     fdtdump [-json] FILE
     9  //
    10  // Options:
    11  //     -json: Print json with base64 encoded values.
    12  package main
    13  
    14  import (
    15  	"encoding/json"
    16  	"flag"
    17  	"fmt"
    18  	"log"
    19  	"os"
    20  
    21  	"github.com/u-root/u-root/pkg/dt"
    22  )
    23  
    24  var (
    25  	asJSON = flag.Bool("json", false, "Print json with base64 encoded values.")
    26  )
    27  
    28  func main() {
    29  	flag.Parse()
    30  
    31  	if flag.NArg() != 1 {
    32  		log.Fatalf("usage: %s [-json] FILE", os.Args[0])
    33  	}
    34  
    35  	// Open and parse Device Tree.
    36  	f, err := os.Open(flag.Arg(0))
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  	defer f.Close()
    41  	fdt, err := dt.ReadFDT(f)
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  
    46  	if *asJSON {
    47  		out, err := json.MarshalIndent(fdt, "", "    ")
    48  		if err != nil {
    49  			log.Fatal(err)
    50  		}
    51  		fmt.Println(string(out))
    52  	} else {
    53  		if err := fdt.PrintDTS(os.Stdout); err != nil {
    54  			log.Fatalf("error printing dts: %v", err)
    55  		}
    56  	}
    57  }