-
Notifications
You must be signed in to change notification settings - Fork 59
Add replaceraw #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add replaceraw #451
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2026 the LinuxBoot Authors. All rights reserved | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package visitors | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
|
|
||
| "github.com/linuxboot/fiano/pkg/uefi" | ||
| ) | ||
|
|
||
| // ReplaceRaw replaces EFI_SECTION_RAW sections with NewRaw for all files matching Predicate. | ||
| type ReplaceRaw struct { | ||
| // Input | ||
| Predicate func(f uefi.Firmware) bool | ||
| NewRaw []byte | ||
|
|
||
| // Output | ||
| Matches []uefi.Firmware | ||
| } | ||
|
|
||
| // Run wraps Visit and performs some setup and teardown tasks. | ||
| func (v *ReplaceRaw) Run(f uefi.Firmware) error { | ||
| // Run "find" to generate a list of matches to replace. | ||
| find := Find{ | ||
| Predicate: v.Predicate, | ||
| } | ||
| if err := find.Run(f); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| v.Matches = find.Matches | ||
| if len(find.Matches) == 0 { | ||
| return errors.New("no matches found for replacement") | ||
| } | ||
| if len(find.Matches) > 1 { | ||
| return errors.New("multiple matches found! There can be only one. Use find to list all matches") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This errors.New should be |
||
| } | ||
|
|
||
| for _, m := range v.Matches { | ||
| if err := m.Apply(v); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // Visit applies the ReplaceRaw visitor to any Firmware type. | ||
| func (v *ReplaceRaw) Visit(f uefi.Firmware) error { | ||
| switch f := f.(type) { | ||
|
|
||
| case *uefi.File: | ||
| return f.ApplyChildren(v) | ||
|
|
||
| case *uefi.Section: | ||
| if f.Header.Type == uefi.SectionTypeRaw { | ||
| f.SetBuf(v.NewRaw) | ||
| f.Encapsulated = nil // Raw sections have no encapsulated children | ||
| if err := f.GenSecHeader(); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return f.ApplyChildren(v) | ||
|
|
||
| default: | ||
| // Must be applied to a File to have any effect. | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func init() { | ||
| RegisterCLI("replace_raw", "replace a raw section given a GUID or name and new file", 2, func(args []string) (uefi.Visitor, error) { | ||
| pred, err := FindFilePredicate(args[0]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| filename := args[1] | ||
| newRaw, err := os.ReadFile(filename) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &ReplaceRaw{ | ||
| Predicate: pred, | ||
| NewRaw: newRaw, | ||
| }, nil | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright 2026 the LinuxBoot Authors. All rights reserved | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package visitors | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/linuxboot/fiano/pkg/uefi" | ||
| ) | ||
|
|
||
| func TestReplaceRaw(t *testing.T) { | ||
| f := parseImage(t) | ||
|
|
||
| replace := &ReplaceRaw{ | ||
| Predicate: FindFileGUIDPredicate(*microcodeRawGUID), | ||
| NewRaw: []byte("banana"), | ||
| } | ||
| if err := replace.Run(f); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if len(replace.Matches) != 1 { | ||
| t.Fatalf("got %d matches; expected 1", len(replace.Matches)) | ||
| } | ||
|
|
||
| results := find(t, f, microcodeRawGUID) | ||
| if len(results) != 1 { | ||
| t.Fatalf("got %d matches; expected 1", len(results)) | ||
| } | ||
| want := []byte{0x0a, 0x00, 0x00, byte(uefi.SectionTypeRaw), 'b', 'a', 'n', 'a', 'n', 'a'} | ||
| file, ok := results[0].(*uefi.File) | ||
| if !ok { | ||
| t.Fatalf("did not match a file, got type :%T", file) | ||
| } | ||
| got := file.Sections[0].Buf() | ||
| if !reflect.DeepEqual(want, got) { | ||
| t.Fatalf("want %v; got %v", want, got) | ||
| } | ||
| } | ||
|
|
||
| func TestReplaceRawErrors(t *testing.T) { | ||
| f := parseImage(t) | ||
|
|
||
| var tests = []struct { | ||
| name string | ||
| newRaw []byte | ||
| match string | ||
| err string | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we first wrote fiano, Go error handling picked up some of the nice bits from Rust. |
||
| }{ | ||
| {"No Matches", []byte("banana"), "no-match-string", | ||
| "no matches found for replacement"}, | ||
| {"Multiple Matches", []byte("banana"), ".*", | ||
| "multiple matches found! There can be only one. Use find to list all matches"}, | ||
| } | ||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| pred, err := FindFilePredicate(test.match) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| replace := &ReplaceRaw{ | ||
| Predicate: pred, | ||
| NewRaw: test.newRaw, | ||
| } | ||
| err = replace.Run(f) | ||
| if err == nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be ! errors.Is(err, test.err) and you can avoid the else in that case. |
||
| t.Fatalf("Expected Error (%v), got nil", test.err) | ||
| } else if err.Error() != test.err { | ||
| t.Fatalf("Mismatched Error: Expected %v, got %v", test.err, err.Error()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want to avoid errors.New in anything but a variable declaration, and you want to wrap standard errors for tests. And you might was well be as informative as possible.
return fmt.Errorf("no matches found for %v:%w", theThingYouAreTryingToMatchWhichICan'tSee, os.ErrNotexist)
then in your test, below, you can test for errors.Is(test.err), and test.err can be initialized to os.ErrNotExist, and now you're not string matching any more.
Thanks for this useful contribution!