github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/bootvars/main.go (about)

     1  // Copyright 2020 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  // SPDX-License-Identifier: BSD-3-Clause
     6  //
     7  
     8  // Command bootvars reads the current UEFI boot variables. Given -m arg, it
     9  // mounts the filesystem pointed to by the current boot variable and prints the
    10  // location.
    11  //
    12  // Note - does not check whether the fs is already mounted anywhere. User is
    13  // responsible for unmounting and for removing the temp dir.
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"io/ioutil"
    19  	"log"
    20  	"os"
    21  	fp "path/filepath"
    22  
    23  	"github.com/u-root/u-root/pkg/uefivars/boot"
    24  )
    25  
    26  //must run as root, as efi vars are not accessible otherwise
    27  func main() {
    28  	m := flag.Bool("m", false, "Mount FS containing boot file, print path.")
    29  	flag.Parse()
    30  
    31  	bv, err := boot.ReadCurrentBootVar()
    32  	if err != nil {
    33  		log.Fatalf("Reading current boot var: %s", err)
    34  	}
    35  	if bv == nil {
    36  		log.Fatalf("Unable to read var... are you root?")
    37  	}
    38  	if *m {
    39  		//actually mount FS, locate file
    40  		exe := fp.Base(os.Args[0])
    41  		tmp, err := ioutil.TempDir("", exe)
    42  		if err != nil {
    43  			log.Fatalf("creating temp dir: %s", err)
    44  		}
    45  		path := tmp
    46  		for _, element := range bv.FilePathList {
    47  			res, err := element.Resolver()
    48  			if err != nil {
    49  				log.Fatalf("%s", err)
    50  			}
    51  			path, err = res.Resolve(path)
    52  			if err != nil {
    53  				log.Printf("Resolving element %s: %s", element, err)
    54  			}
    55  		}
    56  		log.Printf("file corresponding to CurrentBoot var can be found at %s\n", path)
    57  		log.Printf("you will need to unmount the filesystem, remove temp dir, etc when done.\n")
    58  	} else {
    59  		//just print out elements in variable
    60  		log.Printf("%s", bv)
    61  		for _, element := range bv.FilePathList {
    62  			res, err := element.Resolver()
    63  			if err != nil {
    64  				log.Fatalf("%s", err)
    65  			}
    66  			log.Printf("%s", res.String())
    67  		}
    68  	}
    69  }