| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Lorenz Brun | 80deba5 | 2022-02-24 17:07:13 +0100 | [diff] [blame] | 4 | // This assembles standalone microcode files into the format expected by the |
| 5 | // Linux microcode loader. See |
| 6 | // https://www.kernel.org/doc/html/latest/x86/microcode.html for further |
| 7 | // information. |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "flag" |
| 12 | "io" |
| 13 | "log" |
| 14 | "os" |
| 15 | |
| 16 | "github.com/cavaliergopher/cpio" |
| 17 | "google.golang.org/protobuf/encoding/prototext" |
| 18 | |
| Tim Windelschmidt | c2290c2 | 2024-08-15 19:56:00 +0200 | [diff] [blame] | 19 | "source.monogon.dev/osbase/build/mkucode/spec" |
| Lorenz Brun | 80deba5 | 2022-02-24 17:07:13 +0100 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | var ( |
| Tim Windelschmidt | c2290c2 | 2024-08-15 19:56:00 +0200 | [diff] [blame] | 23 | specPath = flag.String("spec", "", "Path to prototext specification (osbase.build.mkucode.UCode)") |
| Lorenz Brun | 80deba5 | 2022-02-24 17:07:13 +0100 | [diff] [blame] | 24 | outPath = flag.String("out", "", "Output path for cpio to be prepend to initrd") |
| 25 | ) |
| 26 | |
| 27 | // Usage: -spec <ucode.prototxt> -out <ucode.cpio> |
| 28 | func main() { |
| 29 | flag.Parse() |
| 30 | specRaw, err := os.ReadFile(*specPath) |
| 31 | if err != nil { |
| 32 | log.Fatalf("Failed to read spec: %v", err) |
| 33 | } |
| 34 | var ucodeSpec spec.UCode |
| 35 | if err := prototext.Unmarshal(specRaw, &ucodeSpec); err != nil { |
| 36 | log.Fatalf("Failed unmarshaling ucode spec: %v", err) |
| 37 | } |
| 38 | out, err := os.Create(*outPath) |
| 39 | if err != nil { |
| 40 | log.Fatalf("Failed to create cpio: %v", err) |
| 41 | } |
| 42 | defer out.Close() |
| 43 | cpioWriter := cpio.NewWriter(out) |
| 44 | for _, vendor := range ucodeSpec.Vendor { |
| 45 | var totalSize int64 |
| 46 | for _, file := range vendor.File { |
| 47 | data, err := os.Stat(file) |
| 48 | if err != nil { |
| 49 | log.Fatalf("Failed to stat file for vendor %q: %v", vendor.Id, err) |
| 50 | } |
| 51 | totalSize += data.Size() |
| 52 | } |
| 53 | if err := cpioWriter.WriteHeader(&cpio.Header{ |
| 54 | Mode: 0444, |
| 55 | Name: "kernel/x86/microcode/" + vendor.Id + ".bin", |
| 56 | Size: totalSize, |
| 57 | }); err != nil { |
| 58 | log.Fatalf("Failed to write cpio header for vendor %q: %v", vendor.Id, err) |
| 59 | } |
| 60 | for _, file := range vendor.File { |
| 61 | f, err := os.Open(file) |
| 62 | if err != nil { |
| 63 | log.Fatalf("Failed to open file for vendor %q: %v", vendor.Id, err) |
| 64 | } |
| 65 | if _, err := io.Copy(cpioWriter, f); err != nil { |
| 66 | log.Fatalf("Failed to copy data for file %q: %v", file, err) |
| 67 | } |
| 68 | f.Close() |
| 69 | } |
| 70 | } |
| 71 | if err := cpioWriter.Close(); err != nil { |
| 72 | log.Fatalf("Failed writing cpio: %v", err) |
| 73 | } |
| 74 | } |