github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/bootloader/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package bootloader
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/bootloader/lkenv"
    30  	"github.com/snapcore/snapd/bootloader/ubootenv"
    31  )
    32  
    33  // creates a new Androidboot bootloader object
    34  func NewAndroidBoot(rootdir string) Bootloader {
    35  	return newAndroidBoot(rootdir, nil)
    36  }
    37  
    38  func MockAndroidBootFile(c *C, rootdir string, mode os.FileMode) {
    39  	f := &androidboot{rootdir: rootdir}
    40  	err := os.MkdirAll(f.dir(), 0755)
    41  	c.Assert(err, IsNil)
    42  	err = ioutil.WriteFile(f.ConfigFile(), nil, mode)
    43  	c.Assert(err, IsNil)
    44  }
    45  
    46  func NewUboot(rootdir string, blOpts *Options) ExtractedRecoveryKernelImageBootloader {
    47  	return newUboot(rootdir, blOpts).(ExtractedRecoveryKernelImageBootloader)
    48  }
    49  
    50  func MockUbootFiles(c *C, rootdir string, blOpts *Options) {
    51  	u := &uboot{rootdir: rootdir}
    52  	u.setDefaults()
    53  	u.processBlOpts(blOpts)
    54  	err := os.MkdirAll(u.dir(), 0755)
    55  	c.Assert(err, IsNil)
    56  
    57  	// ensure that we have a valid uboot.env too
    58  	env, err := ubootenv.Create(u.envFile(), 4096)
    59  	c.Assert(err, IsNil)
    60  	err = env.Save()
    61  	c.Assert(err, IsNil)
    62  }
    63  
    64  func NewGrub(rootdir string, opts *Options) RecoveryAwareBootloader {
    65  	return newGrub(rootdir, opts).(RecoveryAwareBootloader)
    66  }
    67  
    68  func MockGrubFiles(c *C, rootdir string) {
    69  	err := os.MkdirAll(filepath.Join(rootdir, "/boot/grub"), 0755)
    70  	c.Assert(err, IsNil)
    71  	err = ioutil.WriteFile(filepath.Join(rootdir, "/boot/grub/grub.cfg"), nil, 0644)
    72  	c.Assert(err, IsNil)
    73  }
    74  
    75  func NewLk(rootdir string, opts *Options) Bootloader {
    76  	if opts == nil {
    77  		opts = &Options{}
    78  	}
    79  	return newLk(rootdir, opts)
    80  }
    81  
    82  func MockLkFiles(c *C, rootdir string, opts *Options) {
    83  	if opts == nil {
    84  		opts = &Options{}
    85  	}
    86  	l := &lk{rootdir: rootdir, inRuntimeMode: !opts.PrepareImageTime}
    87  	err := os.MkdirAll(l.dir(), 0755)
    88  	c.Assert(err, IsNil)
    89  
    90  	// first create empty env file
    91  	buf := make([]byte, 4096)
    92  	err = ioutil.WriteFile(l.envFile(), buf, 0660)
    93  	c.Assert(err, IsNil)
    94  	// now write env in it with correct crc
    95  	env := lkenv.NewEnv(l.envFile())
    96  	env.ConfigureBootPartitions("boot_a", "boot_b")
    97  	err = env.Save()
    98  	c.Assert(err, IsNil)
    99  }
   100  
   101  func LkRuntimeMode(b Bootloader) bool {
   102  	lk := b.(*lk)
   103  	return lk.inRuntimeMode
   104  }
   105  
   106  var (
   107  	EditionFromDiskConfigAsset           = editionFromDiskConfigAsset
   108  	EditionFromConfigAsset               = editionFromConfigAsset
   109  	ConfigAssetFrom                      = configAssetFrom
   110  	StaticCommandLineForGrubAssetEdition = staticCommandLineForGrubAssetEdition
   111  )