/ util / intelp2m / main.go
main.go
  1  package main
  2  
  3  import (
  4  	"flag"
  5  	"fmt"
  6  	"os"
  7  
  8  	"review.coreboot.org/coreboot.git/util/intelp2m/config"
  9  	"review.coreboot.org/coreboot.git/util/intelp2m/parser"
 10  )
 11  
 12  type Printer struct{}
 13  
 14  func (Printer) Linef(lvl int, format string, args ...interface{}) {
 15  	if config.InfoLevelGet() >= lvl {
 16  		fmt.Fprintf(config.OutputGenFile, format, args...)
 17  	}
 18  }
 19  
 20  func (Printer) Line(lvl int, str string) {
 21  	if config.InfoLevelGet() >= lvl {
 22  		fmt.Fprint(config.OutputGenFile, str)
 23  	}
 24  }
 25  
 26  var (
 27  	// Version is injected into main during project build
 28  	Version string = "Unknown"
 29  )
 30  
 31  // printVersion - print the utility version in the console
 32  func printVersion() {
 33  	fmt.Printf("[ intelp2m ] Version: %s\n", Version)
 34  }
 35  
 36  // main
 37  func main() {
 38  	// Command line arguments
 39  	inputFileName := flag.String("file",
 40  		"inteltool.log",
 41  		"the path to the inteltool log file\n")
 42  
 43  	outputFileName := flag.String("o",
 44  		"generate/gpio.h",
 45  		"the path to the generated file with GPIO configuration\n")
 46  
 47  	ignFlag := flag.Bool("ign",
 48  		false,
 49  		"exclude fields that should be ignored from advanced macros\n")
 50  
 51  	nonCheckFlag := flag.Bool("n",
 52  		false,
 53  		"Generate macros without checking.\n"+
 54  			"\tIn this case, some fields of the configuration registers\n"+
 55  			"\tDW0 will be ignored.\n")
 56  
 57  	infoLevels := []*bool{
 58  		flag.Bool("i", false, "Show pads function in the comments"),
 59  		flag.Bool("ii", false, "Show DW0/DW1 value in the comments"),
 60  		flag.Bool("iii", false, "Show ignored bit fields in the comments"),
 61  		flag.Bool("iiii", false, "Show target PAD_CFG() macro in the comments"),
 62  	}
 63  
 64  	template := flag.Int("t", 0, "template type number\n"+
 65  		"\t0 - inteltool.log (default)\n"+
 66  		"\t1 - gpio.h\n"+
 67  		"\t2 - your template\n\t")
 68  
 69  	platform := flag.String("p", "snr", "set platform:\n"+
 70  		"\tsnr - Sunrise PCH or Skylake/Kaby Lake SoC\n"+
 71  		"\tlbg - Lewisburg PCH with Xeon SP\n"+
 72  		"\tapl - Apollo Lake SoC\n"+
 73  		"\tcnl - CannonLake-LP or Whiskeylake/Coffeelake/Cometlake-U SoC\n"+
 74  		"\ttgl - TigerLake-H SoC\n"+
 75  		"\tadl - AlderLake PCH\n"+
 76  		"\tjsl - Jasper Lake SoC\n"+
 77  		"\tmtl - MeteorLake SoC\n"+
 78  		"\tebg - Emmitsburg PCH with Xeon SP\n")
 79  
 80  	fieldstyle := flag.String("fld", "none", "set fields macros style:\n"+
 81  		"\tcb  - use coreboot style for bit fields macros\n"+
 82  		"\tfsp - use fsp style\n"+
 83  		"\traw - do not convert, print as is\n")
 84  
 85  	printVersion()
 86  	flag.Parse()
 87  
 88  	config.IgnoredFieldsFlagSet(*ignFlag)
 89  	config.NonCheckingFlagSet(*nonCheckFlag)
 90  
 91  	for level, flag := range infoLevels {
 92  		if *flag {
 93  			config.InfoLevelSet(level + 1)
 94  			fmt.Printf("Info level: Use level %d!\n", level+1)
 95  			break
 96  		}
 97  	}
 98  
 99  	if !config.TemplateSet(*template) {
100  		fmt.Printf("Error! Unknown template format of input file!\n")
101  		os.Exit(1)
102  	}
103  
104  	if valid := config.PlatformSet(*platform); valid != 0 {
105  		fmt.Printf("Error: invalid platform -%s!\n", *platform)
106  		os.Exit(1)
107  	}
108  
109  	fmt.Println("Log file:", *inputFileName)
110  	fmt.Println("Output generated file:", *outputFileName)
111  
112  	inputRegDumpFile, err := os.Open(*inputFileName)
113  	if err != nil {
114  		fmt.Printf("Error: inteltool log file was not found!\n")
115  		os.Exit(1)
116  	}
117  
118  	if config.FldStyleSet(*fieldstyle) != 0 {
119  		fmt.Printf("Error! Unknown bit fields style option -%s!\n", *fieldstyle)
120  		os.Exit(1)
121  	}
122  
123  	// create dir for output files
124  	err = os.MkdirAll("generate", os.ModePerm)
125  	if err != nil {
126  		fmt.Printf("Error! Can not create a directory for the generated files!\n")
127  		os.Exit(1)
128  	}
129  
130  	// create empty gpio.h file
131  	outputGenFile, err := os.Create(*outputFileName)
132  	if err != nil {
133  		fmt.Printf("Error: unable to generate GPIO config file!\n")
134  		os.Exit(1)
135  	}
136  
137  	defer inputRegDumpFile.Close()
138  	defer outputGenFile.Close()
139  
140  	config.OutputGenFile = outputGenFile
141  	config.InputRegDumpFile = inputRegDumpFile
142  
143  	prs := parser.ParserData{}
144  	prs.Parse()
145  
146  	generator := parser.Generator{
147  		PrinterIf: Printer{},
148  		Data:      &prs,
149  	}
150  	header := fmt.Sprintf(`/* SPDX-License-Identifier: GPL-2.0-only */
151  
152  #ifndef CFG_GPIO_H
153  #define CFG_GPIO_H
154  
155  #include <gpio.h>
156  
157  /* Pad configuration was generated automatically using intelp2m %s */
158  static const struct pad_config gpio_table[] = {`, Version)
159  	config.OutputGenFile.WriteString(header + "\n")
160  	// Add the pads map
161  
162  	if err := generator.Run(); err != nil {
163  		fmt.Printf("Error: %v", err)
164  		os.Exit(1)
165  	}
166  	config.OutputGenFile.WriteString(`};
167  
168  #endif /* CFG_GPIO_H */
169  `)
170  }