Skip to content

Commit

Permalink
feat(cmd): move context into its own package
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Walter <[email protected]>
  • Loading branch information
walterchris committed Mar 20, 2024
1 parent 8e176a0 commit 5cd09f8
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 117 deletions.
26 changes: 11 additions & 15 deletions cmd/amd-suite/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ import (
"github.com/linuxboot/fiano/pkg/amd/apcb"
"github.com/linuxboot/fiano/pkg/amd/psb"

"github.com/9elements/converged-security-suite/v2/internal"
amd_manifest "github.com/linuxboot/fiano/pkg/amd/manifest"
)

// Context for kong command line parser
type context struct {
debug bool
}

type outputFirmwareCmd struct {
FwPath string `required:"" name:"fwpath" help:"Path to UEFI firmware image." type:"path"`
}
Expand Down Expand Up @@ -99,7 +95,7 @@ var cli struct {
SetSecurityToken setAPCBSecurityTokenCmd `cmd:"" help:"sets a APCB security token"`
}

func (s *outputFirmwareCmd) Run(ctx *context) error {
func (s *outputFirmwareCmd) Run(ctx *internal.Context) error {
amdFw, err := parseAMDFirmwareFile(s.FwPath)
if err != nil {
return fmt.Errorf("could not parse firmware image: %w", err)
Expand All @@ -118,7 +114,7 @@ func (s *outputFirmwareCmd) Run(ctx *context) error {
return nil
}

func (s *showKeysCmd) Run(ctx *context) error {
func (s *showKeysCmd) Run(ctx *internal.Context) error {
amdFw, err := parseAMDFirmwareFile(s.FwPath)
if err != nil {
return fmt.Errorf("could not parse firmware image: %w", err)
Expand All @@ -133,7 +129,7 @@ func (s *showKeysCmd) Run(ctx *context) error {
return nil
}

func (v *validatePSPEntriesCmd) Run(ctx *context) error {
func (v *validatePSPEntriesCmd) Run(ctx *internal.Context) error {
directory, err := psb.DirectoryTypeFromString(v.Directory)
if err != nil {
return err
Expand Down Expand Up @@ -169,7 +165,7 @@ func (v *validatePSPEntriesCmd) Run(ctx *context) error {
return nil
}

func (v *validateRTMCmd) Run(ctx *context) error {
func (v *validateRTMCmd) Run(ctx *internal.Context) error {
amdFw, err := parseAMDFirmwareFile(v.FwPath)
if err != nil {
return fmt.Errorf("could not parse firmware image: %w", err)
Expand Down Expand Up @@ -215,13 +211,13 @@ func dumpHelper(fwPath string, entry string, resultFile string,
return nil
}

func (v *dumpPSPEntryCmd) Run(ctx *context) error {
func (v *dumpPSPEntryCmd) Run(ctx *internal.Context) error {
return dumpHelper(v.FwPath, v.Entry, v.EntryFile, func(amdFw *amd_manifest.AMDFirmware, entryID uint32, w io.Writer) (int, error) {
return psb.DumpPSPEntry(amdFw, v.PSPLevel, amd_manifest.PSPDirectoryTableEntryType(entryID), w)
})
}

func (v *dumpBIOSEntryCmd) Run(ctx *context) error {
func (v *dumpBIOSEntryCmd) Run(ctx *internal.Context) error {
return dumpHelper(v.FwPath, v.Entry, v.EntryFile, func(amdFw *amd_manifest.AMDFirmware, entryID uint32, w io.Writer) (int, error) {
return psb.DumpBIOSEntry(amdFw, v.BIOSLevel, amd_manifest.BIOSDirectoryTableEntryType(entryID), v.Instance, w)
})
Expand Down Expand Up @@ -270,19 +266,19 @@ func patchHelper(fwPath string, entry string, entryFile string, resultFile strin
return nil
}

func (v *patchPSPEntryCmd) Run(ctx *context) error {
func (v *patchPSPEntryCmd) Run(ctx *internal.Context) error {
return patchHelper(v.FwPath, v.Entry, v.EntryFile, v.ModifiedFirmwareFile, func(amdFw *amd_manifest.AMDFirmware, entryID uint32, r io.Reader, w io.Writer) (int, error) {
return psb.PatchPSPEntry(amdFw, v.PSPLevel, amd_manifest.PSPDirectoryTableEntryType(entryID), r, w)
})
}

func (v *patchBIOSEntryCmd) Run(ctx *context) error {
func (v *patchBIOSEntryCmd) Run(ctx *internal.Context) error {
return patchHelper(v.FwPath, v.Entry, v.EntryFile, v.ModifiedFirmwareFile, func(amdFw *amd_manifest.AMDFirmware, entryID uint32, r io.Reader, w io.Writer) (int, error) {
return psb.PatchBIOSEntry(amdFw, v.BIOSLevel, amd_manifest.BIOSDirectoryTableEntryType(entryID), v.Instance, r, w)
})
}

func (v *outputAPCBSecurityTokensCmd) Run(ctx *context) error {
func (v *outputAPCBSecurityTokensCmd) Run(ctx *internal.Context) error {
amdFw, err := parseAMDFirmwareFile(v.FwPath)
if err != nil {
return fmt.Errorf("could not parse firmware image: %w", err)
Expand Down Expand Up @@ -332,7 +328,7 @@ func (v *outputAPCBSecurityTokensCmd) Run(ctx *context) error {
return nil
}

func (v *setAPCBSecurityTokenCmd) Run(ctx *context) error {
func (v *setAPCBSecurityTokenCmd) Run(ctx *internal.Context) error {
b, err := ioutil.ReadFile(v.FwPath)
if err != nil {
return fmt.Errorf("unable to read the image '%s': %w", v.FwPath, err)
Expand Down
12 changes: 8 additions & 4 deletions cmd/amd-suite/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package main

import (
"github.com/9elements/converged-security-suite/v2/pkg/internal"
"github.com/9elements/converged-security-suite/v2/pkg/log"
"github.com/alecthomas/kong"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/facebookincubator/go-belt/tool/logger/implementation/dummy"
fianoLog "github.com/linuxboot/fiano/pkg/log"
)

const programName = "amd-suite"
const programDesc = "AMD PSP and PSB management tool"
const (
programName = "amd-suite"
programDesc = "AMD PSP and PSB management tool"
)

var (
gitcommit string
Expand All @@ -28,7 +31,8 @@ func main() {
fianoLog.DefaultLogger = log.NewFianoLogger(dummy.New(), logger.LevelTrace)

// Run commands
err := ctx.Run(&context{
debug: cli.Debug})
err := ctx.Run(&internal.Context{
Debug: cli.Debug,
})
ctx.FatalIfErrorf(err)
}
57 changes: 27 additions & 30 deletions cmd/bg-prov/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ import (

"github.com/linuxboot/fiano/pkg/uefi"

"github.com/9elements/converged-security-suite/v2/internal"
"github.com/9elements/converged-security-suite/v2/pkg/provisioning/bootguard"
"github.com/9elements/converged-security-suite/v2/pkg/tools"
)

type context struct {
Debug bool
}

type versionCmd struct{}

type templateCmdv1 struct {
Expand Down Expand Up @@ -301,12 +298,12 @@ type verifyBPMSigCmd struct {
BPM string `arg:"" required:"" name:"bpm" help:"Path to the Boot Policy Manifest binary file." type:"path"`
}

func (v *versionCmd) Run(ctx *context) error {
func (v *versionCmd) Run(ctx *internal.Context) error {
tools.ShowVersion(programName, gittag, gitcommit)
return nil
}

func (kmp *kmPrintCmd) Run(ctx *context) error {
func (kmp *kmPrintCmd) Run(ctx *internal.Context) error {
file, err := os.Open(kmp.Path)
if err != nil {
return err
Expand All @@ -320,7 +317,7 @@ func (kmp *kmPrintCmd) Run(ctx *context) error {
return nil
}

func (bpmp *bpmPrintCmd) Run(ctx *context) error {
func (bpmp *bpmPrintCmd) Run(ctx *internal.Context) error {
file, err := os.Open(bpmp.Path)
if err != nil {
return err
Expand All @@ -334,7 +331,7 @@ func (bpmp *bpmPrintCmd) Run(ctx *context) error {
return nil
}

func (acmp *acmPrintCmd) Run(ctx *context) error {
func (acmp *acmPrintCmd) Run(ctx *internal.Context) error {
file, err := os.Open(acmp.Path)
if err != nil {
return err
Expand All @@ -348,7 +345,7 @@ func (acmp *acmPrintCmd) Run(ctx *context) error {
return nil
}

func (biosp *biosPrintCmd) Run(ctx *context) error {
func (biosp *biosPrintCmd) Run(ctx *internal.Context) error {
data, err := os.ReadFile(biosp.Path)
if err != nil {
return err
Expand All @@ -365,7 +362,7 @@ func (biosp *biosPrintCmd) Run(ctx *context) error {
return nil
}

func (acme *acmExportCmd) Run(ctx *context) error {
func (acme *acmExportCmd) Run(ctx *internal.Context) error {
data, err := os.ReadFile(acme.BIOS)
if err != nil {
return err
Expand All @@ -381,7 +378,7 @@ func (acme *acmExportCmd) Run(ctx *context) error {
return nil
}

func (kme *kmExportCmd) Run(ctx *context) error {
func (kme *kmExportCmd) Run(ctx *internal.Context) error {
data, err := os.ReadFile(kme.BIOS)
if err != nil {
return err
Expand All @@ -397,7 +394,7 @@ func (kme *kmExportCmd) Run(ctx *context) error {
return nil
}

func (bpme *bpmExportCmd) Run(ctx *context) error {
func (bpme *bpmExportCmd) Run(ctx *internal.Context) error {
data, err := os.ReadFile(bpme.BIOS)
if err != nil {
return err
Expand All @@ -413,7 +410,7 @@ func (bpme *bpmExportCmd) Run(ctx *context) error {
return nil
}

func (g *generateKMCmdv1) Run(ctx *context) error {
func (g *generateKMCmdv1) Run(ctx *internal.Context) error {
var b bootguard.BootGuard
b.Version = bgheader.Version10
if g.Config != "" {
Expand Down Expand Up @@ -481,7 +478,7 @@ func (g *generateKMCmdv1) Run(ctx *context) error {
return nil
}

func (g *generateKMCmdv2) Run(ctx *context) error {
func (g *generateKMCmdv2) Run(ctx *internal.Context) error {
var b bootguard.BootGuard
b.Version = bgheader.Version20
if g.Config != "" {
Expand Down Expand Up @@ -551,7 +548,7 @@ func (g *generateKMCmdv2) Run(ctx *context) error {
return nil
}

func (g *generateBPMCmdv1) Run(ctx *context) error {
func (g *generateBPMCmdv1) Run(ctx *internal.Context) error {
var b bootguard.BootGuard
b.Version = bgheader.Version10
if g.Config != "" {
Expand Down Expand Up @@ -610,7 +607,7 @@ func (g *generateBPMCmdv1) Run(ctx *context) error {
return nil
}

func (g *generateBPMCmdv2) Run(ctx *context) error {
func (g *generateBPMCmdv2) Run(ctx *internal.Context) error {
var b bootguard.BootGuard
b.Version = bgheader.Version20
if g.Config != "" {
Expand Down Expand Up @@ -689,7 +686,7 @@ func (g *generateBPMCmdv2) Run(ctx *context) error {
return nil
}

func (g *generateACMCmdv0) Run(ctx *context) error {
func (g *generateACMCmdv0) Run(ctx *internal.Context) error {
var sACM fit.EntrySACM
var sACMData *fit.EntrySACMData
if g.ConfigIn != "" {
Expand Down Expand Up @@ -764,7 +761,7 @@ func (g *generateACMCmdv0) Run(ctx *context) error {
return nil
}

func (g *generateACMCmdv3) Run(ctx *context) error {
func (g *generateACMCmdv3) Run(ctx *internal.Context) error {
var sACM fit.EntrySACM
var sACMData *fit.EntrySACMData
if g.ConfigIn != "" {
Expand Down Expand Up @@ -839,7 +836,7 @@ func (g *generateACMCmdv3) Run(ctx *context) error {
return nil
}

func (s *signKMCmd) Run(ctx *context) error {
func (s *signKMCmd) Run(ctx *internal.Context) error {
encKey, err := os.ReadFile(s.Key)
if err != nil {
return err
Expand Down Expand Up @@ -867,7 +864,7 @@ func (s *signKMCmd) Run(ctx *context) error {
return nil
}

func (s *signBPMCmd) Run(ctx *context) error {
func (s *signBPMCmd) Run(ctx *internal.Context) error {
encKey, err := os.ReadFile(s.Key)
if err != nil {
return err
Expand Down Expand Up @@ -895,7 +892,7 @@ func (s *signBPMCmd) Run(ctx *context) error {
return nil
}

func (t *templateCmdv2) Run(ctx *context) error {
func (t *templateCmdv2) Run(ctx *internal.Context) error {
var vdata bootguard.VersionedData
vdata.CBNTbpm = cbntbootpolicy.NewManifest()
vdata.CBNTkm = cbntkey.NewManifest()
Expand Down Expand Up @@ -960,7 +957,7 @@ func (t *templateCmdv2) Run(ctx *context) error {
return nil
}

func (t *templateCmdv1) Run(ctx *context) error {
func (t *templateCmdv1) Run(ctx *internal.Context) error {
var vdata bootguard.VersionedData
var err error

Expand Down Expand Up @@ -1003,7 +1000,7 @@ func (t *templateCmdv1) Run(ctx *context) error {
return nil
}

func (rc *readConfigCmd) Run(ctx *context) error {
func (rc *readConfigCmd) Run(ctx *internal.Context) error {
f, err := os.Create(rc.Config)
if err != nil {
return err
Expand All @@ -1015,7 +1012,7 @@ func (rc *readConfigCmd) Run(ctx *context) error {
return nil
}

func (s *stitchingKMCmd) Run(ctx *context) error {
func (s *stitchingKMCmd) Run(ctx *internal.Context) error {
file, err := os.Open(s.KM)
if err != nil {
return err
Expand Down Expand Up @@ -1046,7 +1043,7 @@ func (s *stitchingKMCmd) Run(ctx *context) error {
return nil
}

func (s *stitchingBPMCmd) Run(ctx *context) error {
func (s *stitchingBPMCmd) Run(ctx *internal.Context) error {
file, err := os.Open(s.BPM)
if err != nil {
return err
Expand Down Expand Up @@ -1077,7 +1074,7 @@ func (s *stitchingBPMCmd) Run(ctx *context) error {
return nil
}

func (s *stitchingCmd) Run(ctx *context) error {
func (s *stitchingCmd) Run(ctx *internal.Context) error {
var err error
var bpm, km, acm, me []byte
if s.BPM != "" {
Expand Down Expand Up @@ -1134,7 +1131,7 @@ func (s *stitchingCmd) Run(ctx *context) error {
return nil
}

func (k *keygenCmd) Run(ctx *context) error {
func (k *keygenCmd) Run(ctx *internal.Context) error {
kmPubFile, err := os.Create(k.Path + "km_pub.pem")
if err != nil {
return err
Expand Down Expand Up @@ -1180,7 +1177,7 @@ func (k *keygenCmd) Run(ctx *context) error {
return nil
}

func (p printFITCmd) Run(ctx *context) error {
func (p printFITCmd) Run(ctx *internal.Context) error {
img, err := os.ReadFile(p.BIOS)
if err != nil {
return err
Expand All @@ -1193,7 +1190,7 @@ func (p printFITCmd) Run(ctx *context) error {
return nil
}

func (v *verifyKMSigCmd) Run(ctx *context) error {
func (v *verifyKMSigCmd) Run(ctx *internal.Context) error {
file, err := os.Open(v.KM)
if err != nil {
return err
Expand All @@ -1206,7 +1203,7 @@ func (v *verifyKMSigCmd) Run(ctx *context) error {
return bg.VerifyKM()
}

func (b *verifyBPMSigCmd) Run(ctx *context) error {
func (b *verifyBPMSigCmd) Run(ctx *internal.Context) error {
file, err := os.Open(b.BPM)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion cmd/bg-prov/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/9elements/converged-security-suite/v2/internal"
"github.com/9elements/converged-security-suite/v2/pkg/log"
"github.com/alecthomas/kong"
"github.com/linuxboot/fiano/pkg/intel/metadata/cbnt"
Expand Down Expand Up @@ -28,6 +29,6 @@ func main() {
}))
cbnt.StrictOrderCheck = cli.ManifestStrictOrderCheck
fianoLog.DefaultLogger = log.FianoLogger{}
err := ctx.Run(&context{Debug: cli.Debug})
err := ctx.Run(&internal.Context{Debug: cli.Debug})
ctx.FatalIfErrorf(err)
}
Loading

0 comments on commit 5cd09f8

Please sign in to comment.