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