github.com/linuxboot/fiano@v1.2.0/pkg/utk/utk.go (about)

     1  // Copyright 2018 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 utk is where the implementation of the utk command lives.
     6  package utk
     7  
     8  import (
     9  	"errors"
    10  	"os"
    11  
    12  	"github.com/linuxboot/fiano/pkg/uefi"
    13  	"github.com/linuxboot/fiano/pkg/visitors"
    14  )
    15  
    16  // Run runs the utk command with the given arguments.
    17  func Run(args ...string) error {
    18  	if len(args) == 0 {
    19  		return errors.New("at least one argument is required")
    20  	}
    21  
    22  	v, err := visitors.ParseCLI(args[1:])
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	// Load and parse the image.
    28  	path := args[0]
    29  	f, err := os.Stat(path)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	var parsedRoot uefi.Firmware
    34  	if m := f.Mode(); m.IsDir() {
    35  		// Call ParseDir
    36  		pd := visitors.ParseDir{BasePath: path}
    37  		if parsedRoot, err = pd.Parse(); err != nil {
    38  			return err
    39  		}
    40  		// Assemble the tree from the bottom up
    41  		a := visitors.Assemble{}
    42  		if err = a.Run(parsedRoot); err != nil {
    43  			return err
    44  		}
    45  	} else {
    46  		// Regular file
    47  		image, err := os.ReadFile(path)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		parsedRoot, err = uefi.Parse(image)
    52  		if err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	// Execute the instructions from the command line.
    58  	return visitors.ExecuteCLI(parsedRoot, v)
    59  }