github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/stboot/config.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 stboot
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/u-root/u-root/pkg/boot/jsonboot"
    12  )
    13  
    14  // Stconfig contains multiple u-root BootConfig stucts and additional
    15  // information for stboot
    16  type Stconfig struct {
    17  	// configs is an array of u-root BootConfigs
    18  	BootConfigs []jsonboot.BootConfig `json:"boot_configs"`
    19  	// rootCertPath is the path to root certificate of the signing
    20  	RootCertPath string `json:"root_cert"`
    21  }
    22  
    23  // StconfigFromBytes parses a Stcinfig from a byte slice
    24  func stconfigFromBytes(data []byte) (*Stconfig, error) {
    25  	var config Stconfig
    26  	if err := json.Unmarshal(data, &config); err != nil {
    27  		return nil, err
    28  	}
    29  	return &config, nil
    30  }
    31  
    32  // Bytes serializes a Stconfig stuct into a byte slice
    33  func (cfg *Stconfig) bytes() ([]byte, error) {
    34  	buf, err := json.Marshal(cfg)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return buf, nil
    39  }
    40  
    41  // IsValid returns true if all BootConfig structs inside the config has valid
    42  // content.
    43  func (cfg *Stconfig) IsValid() bool {
    44  	if len(cfg.BootConfigs) == 0 {
    45  		return false
    46  	}
    47  	for _, config := range cfg.BootConfigs {
    48  		if !config.IsValid() {
    49  			return false
    50  		}
    51  	}
    52  	return cfg.RootCertPath != ""
    53  }
    54  
    55  // GetBootConfig returns the i-th boot configuration from the manifest, or an
    56  // error if an invalid index is passed.
    57  func (cfg *Stconfig) getBootConfig(index int) (*jsonboot.BootConfig, error) {
    58  	if index < 0 || index >= len(cfg.BootConfigs) {
    59  		return nil, fmt.Errorf("invalid index: not in range: %d", index)
    60  	}
    61  	return &cfg.BootConfigs[index], nil
    62  }