github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/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  	"log"
    19  	"os"
    20  	fp "path/filepath"
    21  
    22  	"github.com/mvdan/u-root-coreutils/pkg/uefivars/boot"
    23  )
    24  
    25  // must run as root, as efi vars are not accessible otherwise
    26  func main() {
    27  	m := flag.Bool("m", false, "Mount FS containing boot file, print path.")
    28  	flag.Parse()
    29  
    30  	bv, err := boot.ReadCurrentBootVar()
    31  	if err != nil {
    32  		log.Fatalf("Reading current boot var: %s", err)
    33  	}
    34  	if bv == nil {
    35  		log.Fatalf("Unable to read var... are you root?")
    36  	}
    37  	if *m {
    38  		// actually mount FS, locate file
    39  		exe := fp.Base(os.Args[0])
    40  		tmp, err := os.MkdirTemp("", exe)
    41  		if err != nil {
    42  			log.Fatalf("creating temp dir: %s", err)
    43  		}
    44  		path := tmp
    45  		for _, element := range bv.FilePathList {
    46  			res, err := element.Resolver()
    47  			if err != nil {
    48  				log.Fatalf("%s", err)
    49  			}
    50  			path, err = res.Resolve(path)
    51  			if err != nil {
    52  				log.Printf("Resolving element %s: %s", element, err)
    53  			}
    54  		}
    55  		log.Printf("file corresponding to CurrentBoot var can be found at %s\n", path)
    56  		log.Printf("you will need to unmount the filesystem, remove temp dir, etc when done.\n")
    57  	} else {
    58  		// just print out elements in variable
    59  		log.Printf("%s", bv)
    60  		for _, element := range bv.FilePathList {
    61  			res, err := element.Resolver()
    62  			if err != nil {
    63  				log.Fatalf("%s", err)
    64  			}
    65  			log.Printf("%s", res.String())
    66  		}
    67  	}
    68  }