github.com/transparency-dev/armored-witness-boot@v0.1.0/cmd/armored-witness-image/armored-witness-image.go (about)

     1  // Copyright 2022 The Armored Witness Boot authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/gob"
    20  	"flag"
    21  	"io/fs"
    22  	"io/ioutil"
    23  	"log"
    24  	"os"
    25  
    26  	"github.com/transparency-dev/armored-witness-boot/config"
    27  )
    28  
    29  type Flags struct {
    30  	kernel string
    31  	sig1   string
    32  	sig2   string
    33  	output string
    34  }
    35  
    36  var flags *Flags
    37  
    38  func init() {
    39  	log.SetFlags(0)
    40  	log.SetOutput(os.Stdout)
    41  
    42  	flags = &Flags{}
    43  
    44  	flag.StringVar(&flags.kernel, "k", "", "kernel image")
    45  	flag.StringVar(&flags.sig1, "1", "", "signature #1 file")
    46  	flag.StringVar(&flags.sig2, "2", "", "signature #2 file")
    47  	flag.StringVar(&flags.output, "o", "", "output image")
    48  }
    49  
    50  func main() {
    51  	var err error
    52  
    53  	flag.Parse()
    54  
    55  	if len(flags.kernel) <= 0 || len(flags.sig1) <= 0 || len(flags.sig2) <= 0 || len(flags.output) <= 0 {
    56  		flag.PrintDefaults()
    57  		return
    58  	}
    59  
    60  	elf, err := ioutil.ReadFile(flags.kernel)
    61  
    62  	if err != nil {
    63  		log.Fatal(err)
    64  	}
    65  
    66  	sig1, err := ioutil.ReadFile(flags.sig1)
    67  
    68  	if err != nil {
    69  		log.Fatal(err)
    70  	}
    71  
    72  	sig2, err := ioutil.ReadFile(flags.sig2)
    73  
    74  	if err != nil {
    75  		log.Fatal(err)
    76  	}
    77  
    78  	conf := &config.Config{
    79  		Offset:     config.Offset + config.MaxLength,
    80  		Size:       int64(len(elf)),
    81  		Signatures: [][]byte{sig1, sig2},
    82  	}
    83  
    84  	buf := new(bytes.Buffer)
    85  
    86  	if err = gob.NewEncoder(buf).Encode(conf); err != nil {
    87  		log.Fatal(err)
    88  	}
    89  
    90  	pad := config.MaxLength - int64(buf.Len())
    91  
    92  	buf.Write(make([]byte, pad))
    93  	buf.Write(elf)
    94  
    95  	if err = os.WriteFile(flags.output, buf.Bytes(), fs.ModeExclusive|0600); err != nil {
    96  		log.Fatal(err)
    97  	}
    98  
    99  	log.Printf("written config gob and kernel (off:%d, len:%d) to %s", conf.Offset, conf.Size, flags.output)
   100  }