github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/boot/multiboot.go (about)

     1  // Copyright 2018 the u-root 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 boot
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  	"strings"
    11  
    12  	"github.com/mvdan/u-root-coreutils/pkg/boot/ibft"
    13  	"github.com/mvdan/u-root-coreutils/pkg/boot/multiboot"
    14  )
    15  
    16  // MultibootImage is a multiboot-formated OSImage, such as ESXi, Xen, Akaros,
    17  // tboot.
    18  type MultibootImage struct {
    19  	Name string
    20  
    21  	Kernel   io.ReaderAt
    22  	Cmdline  string
    23  	Modules  []multiboot.Module
    24  	IBFT     *ibft.IBFT
    25  	BootRank int
    26  }
    27  
    28  var _ OSImage = &MultibootImage{}
    29  
    30  // Label returns either Name or a short description.
    31  func (mi *MultibootImage) Label() string {
    32  	if len(mi.Name) > 0 {
    33  		return mi.Name
    34  	}
    35  	return fmt.Sprintf("Multiboot(kernel=%s cmdline=%s iBFT=%s)", stringer(mi.Kernel), mi.Cmdline, mi.IBFT)
    36  }
    37  
    38  // Rank for the boot menu order
    39  func (mi *MultibootImage) Rank() int {
    40  	return mi.BootRank
    41  }
    42  
    43  // Edit the kernel command line.
    44  func (mi *MultibootImage) Edit(f func(cmdline string) string) {
    45  	mi.Cmdline = f(mi.Cmdline)
    46  }
    47  
    48  // Load implements OSImage.Load.
    49  func (mi *MultibootImage) Load(verbose bool) error {
    50  	return multiboot.Load(verbose, mi.Kernel, mi.Cmdline, mi.Modules, mi.IBFT)
    51  }
    52  
    53  // String implements fmt.Stringer.
    54  func (mi *MultibootImage) String() string {
    55  	modules := make([]string, len(mi.Modules))
    56  	for i, mod := range mi.Modules {
    57  		modules[i] = mod.Cmdline
    58  	}
    59  	return fmt.Sprintf("MultibootImage(\n  Name: %s\n  Kernel: %s\n  Cmdline: %s\n  iBFT: %s\n  Modules: %s\n)",
    60  		mi.Name, stringer(mi.Kernel), mi.Cmdline, mi.IBFT, strings.Join(modules, ", "))
    61  }