| // Copyright 2026 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| package cosboot |
| |
| import ( |
| "bytes" |
| "fmt" |
| "log" |
| "os" |
| "os/exec" |
| "path/filepath" |
| |
| "cos.googlesource.com/cos/tools.git/src/pkg/tools/partutil" |
| ) |
| |
| func readEFIFileOnCOS(path string) ([]byte, error) { |
| efiPath, err := partutil.MountEFIPartition() |
| if err != nil { |
| return nil, err |
| } |
| data, err := os.ReadFile(filepath.Join(efiPath, path)) |
| if err != nil { |
| unmountErr := partutil.UnmountEFIPartition() |
| if unmountErr != nil { |
| log.Printf("ERROR: failed to unmount EFI partition: %v", unmountErr) |
| } |
| return nil, fmt.Errorf("failed to read EFI file %q: %v", path, err) |
| } |
| if err := partutil.UnmountEFIPartition(); err != nil { |
| return nil, fmt.Errorf("failed to unmount EFI partition: %v", err) |
| } |
| return data, nil |
| } |
| |
| func readEFIFileOffline(disk, path string) ([]byte, error) { |
| sfdiskTable, err := partutil.ReadPartitionTableJSON(disk) |
| if err != nil { |
| return nil, fmt.Errorf("failed to get GPT from %q: %v", disk, err) |
| } |
| partTable := sfdiskTable.PartitionTable |
| var efiPart *partutil.Partition |
| for i := range partTable.Partitions { |
| if partTable.Partitions[i].Name == "EFI-SYSTEM" { |
| efiPart = &partTable.Partitions[i] |
| break |
| } |
| } |
| if efiPart == nil { |
| return nil, fmt.Errorf("failed to find EFI partition on %q", disk) |
| } |
| tmpDir, err := os.MkdirTemp("", "cos-efi") |
| if err != nil { |
| return nil, err |
| } |
| defer os.RemoveAll(tmpDir) |
| start := efiPart.Start * partTable.SectorSize |
| size := efiPart.Size * partTable.SectorSize |
| buf := make([]byte, size) |
| in, err := os.Open(disk) |
| if err != nil { |
| return nil, err |
| } |
| defer in.Close() |
| if _, err := in.ReadAt(buf, int64(start)); err != nil { |
| return nil, err |
| } |
| efiImage := filepath.Join(tmpDir, "efi.bin") |
| if err := os.WriteFile(efiImage, buf, 0644); err != nil { |
| return nil, err |
| } |
| var stdout bytes.Buffer |
| var stderr bytes.Buffer |
| cmd := exec.Command("mcopy", "-i", efiImage, "::"+path, "-") |
| cmd.Stdout = &stdout |
| cmd.Stderr = &stderr |
| if err := cmd.Run(); err != nil { |
| log.Printf("Command %q failed with stderr: %v", cmd.String(), string(stderr.Bytes())) |
| return nil, fmt.Errorf("cannot mcopy from EFI image sourced from %q: %v", disk, err) |
| } |
| return stdout.Bytes(), nil |
| } |
| |
| func ReadEFIFile(disk, path string) ([]byte, error) { |
| if disk == "" { |
| return readEFIFileOnCOS(path) |
| } else { |
| return readEFIFileOffline(disk, path) |
| } |
| } |