forked from orangecms/txt-suite
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
80 lines (68 loc) · 2.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"fmt"
"os"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/diff"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/displayeventlog"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/displayfwinfo"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/dumpfit"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/dumpregisters"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/printnodes"
"github.com/9elements/converged-security-suite/v2/cmd/pcr0tool/commands/sum"
"github.com/9elements/converged-security-suite/v2/pkg/log"
"github.com/linuxboot/fiano/pkg/intel/metadata/manifest"
fianoLog "github.com/linuxboot/fiano/pkg/log"
)
var knownCommands = map[string]commands.Command{
"diff": &diff.Command{},
"display_eventlog": &displayeventlog.Command{},
"display_fwinfo": &displayfwinfo.Command{},
"dump_fit": &dumpfit.Command{},
"dump_registers": &dumpregisters.Command{},
"printnodes": &printnodes.Command{},
"sum": &sum.Command{},
}
func usageAndExit() {
flag.Usage()
os.Exit(2) // the standard Go's exit-code on invalid flags
}
func setupFlag() {
flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "syntax: pcr0tool <command> [options] {arguments}\n")
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "\nPossible commands:\n")
for commandName, command := range knownCommands {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), " pcr0tool %-36s%s\n",
fmt.Sprintf("%s %s", commandName, command.Usage()), command.Description())
}
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "\n")
}
flag.Parse()
if flag.NArg() < 1 {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: no command specified\n\n")
usageAndExit()
}
}
func main() {
manifest.StrictOrderCheck = false // some firmwares have incorrect elements order, should parse them anyway
fianoLog.DefaultLogger = log.DummyLogger{}
setupFlag()
commandName := flag.Arg(0)
command := knownCommands[commandName]
if command == nil {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "error: unknown command '%s'\n\n", commandName)
usageAndExit()
}
flagSet := flag.NewFlagSet(commandName, flag.ExitOnError)
flagSet.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "syntax: pcr0tool %s [options] %s\n\nOptions:\n",
commandName, command.Usage())
flagSet.PrintDefaults()
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "\n")
}
flag.Usage = flagSet.Usage // so a the "command" could just call `flag.Usage()` to print it's usage
command.SetupFlagSet(flagSet)
_ = flagSet.Parse(os.Args[2:])
command.Execute(flagSet.Args())
}