|
| 1 | +// Copyright 2023 the LinuxBoot 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 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "encoding/json" |
| 10 | + "flag" |
| 11 | + "fmt" |
| 12 | + "os" |
| 13 | + |
| 14 | + "github.com/linuxboot/fiano/pkg/intel/metadata/bg/bgbootpolicy" |
| 15 | + "github.com/linuxboot/fiano/pkg/intel/metadata/bg/bgkey" |
| 16 | + "github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntbootpolicy" |
| 17 | + "github.com/linuxboot/fiano/pkg/intel/metadata/cbnt/cbntkey" |
| 18 | + "github.com/linuxboot/fiano/pkg/intel/metadata/fit" |
| 19 | + "github.com/linuxboot/fiano/pkg/log" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + flagJSON = flag.Bool("j", false, "Output as JSON") |
| 24 | +) |
| 25 | + |
| 26 | +type Manifest interface {} |
| 27 | + |
| 28 | +type Meta struct { |
| 29 | + Keym Manifest |
| 30 | + Polm Manifest |
| 31 | +} |
| 32 | + |
| 33 | +func main() { |
| 34 | + flag.Parse() |
| 35 | + if flag.Arg(0) == "" { |
| 36 | + log.Fatalf("missing file name") |
| 37 | + } |
| 38 | + filePath := flag.Arg(0) |
| 39 | + data, err := os.ReadFile(filePath) |
| 40 | + if err != nil { |
| 41 | + log.Fatalf("cannot read input file: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + entries, err := fit.GetEntries(data) |
| 45 | + if err != nil { |
| 46 | + log.Fatalf("cannot parse input file: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + var bme fit.Entry |
| 50 | + var kme fit.Entry |
| 51 | + for idx, entry := range entries { |
| 52 | + // if entry.GetEntryBase().Headers.Type() == fit.EntryTypeStartupACModuleEntry { |
| 53 | + if entry.GetEntryBase().Headers.Type() == fit.EntryTypeKeyManifestRecord { |
| 54 | + kme = entry |
| 55 | + fmt.Fprintf(os.Stderr, "key manifest @ %v\n", idx) |
| 56 | + } |
| 57 | + if entry.GetEntryBase().Headers.Type() == fit.EntryTypeBootPolicyManifest { |
| 58 | + bme = entry |
| 59 | + fmt.Fprintf(os.Stderr, "boot policy manifest @ %v\n", idx) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + var meta Meta |
| 64 | + |
| 65 | + if bme == nil { |
| 66 | + fmt.Fprintf(os.Stderr, "no boot manifest entry\n") |
| 67 | + } else { |
| 68 | + bpm := bgbootpolicy.Manifest{} |
| 69 | + _, err = bpm.ReadFrom(bytes.NewReader(bme.GetEntryBase().DataSegmentBytes)) |
| 70 | + if err == nil { |
| 71 | + if !*flagJSON { |
| 72 | + fmt.Print(bpm.PrettyString(0, true)) |
| 73 | + } |
| 74 | + meta.Polm = bpm |
| 75 | + } else { |
| 76 | + // log.Fatalf("%v", err) |
| 77 | + bpm := cbntbootpolicy.Manifest{} |
| 78 | + _, err = bpm.ReadFrom(bytes.NewReader(bme.GetEntryBase().DataSegmentBytes)) |
| 79 | + if err == nil { |
| 80 | + if !*flagJSON { |
| 81 | + fmt.Print(bpm.PrettyString(0, true)) |
| 82 | + } |
| 83 | + meta.Polm = bpm |
| 84 | + } else { |
| 85 | + // log.Fatalf("%v", err) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if kme == nil { |
| 91 | + fmt.Fprintf(os.Stderr, "no key manifest entry\n") |
| 92 | + } else { |
| 93 | + km := bgkey.Manifest{} |
| 94 | + _, err = km.ReadFrom(bytes.NewReader(kme.GetEntryBase().DataSegmentBytes)) |
| 95 | + if err == nil { |
| 96 | + if !*flagJSON { |
| 97 | + fmt.Print(km.PrettyString(0, true)) |
| 98 | + } |
| 99 | + meta.Keym = km |
| 100 | + km := cbntkey.Manifest{} |
| 101 | + _, err = km.ReadFrom(bytes.NewReader(kme.GetEntryBase().DataSegmentBytes)) |
| 102 | + if err == nil { |
| 103 | + if !*flagJSON { |
| 104 | + fmt.Print(km.PrettyString(0, true)) |
| 105 | + } |
| 106 | + meta.Keym = km |
| 107 | + } else { |
| 108 | + // log.Fatalf("%v", err) |
| 109 | + } |
| 110 | + } else { |
| 111 | + // log.Fatalf("%v", err) |
| 112 | + km := cbntkey.Manifest{} |
| 113 | + _, err = km.ReadFrom(bytes.NewReader(kme.GetEntryBase().DataSegmentBytes)) |
| 114 | + if err == nil { |
| 115 | + if !*flagJSON { |
| 116 | + fmt.Print(km.PrettyString(0, true)) |
| 117 | + } |
| 118 | + meta.Keym = km |
| 119 | + } else { |
| 120 | + // log.Fatalf("%v", err) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if *flagJSON { |
| 126 | + j, err := json.MarshalIndent(meta, "", " ") |
| 127 | + if err != nil { |
| 128 | + log.Fatalf("cannot marshal JSON: %v", err) |
| 129 | + } |
| 130 | + if err != nil { |
| 131 | + log.Fatalf("cannot marshal JSON: %v", err) |
| 132 | + } |
| 133 | + fmt.Println(string(j)) |
| 134 | + } |
| 135 | +} |
0 commit comments