forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bzimage.go
77 lines (65 loc) · 1.47 KB
/
bzimage.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
// Copyright 2012-2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// bzImage is used to modify bzImage files.
// It reads the image in, applies an operator, and writes a new one out.
//
// Synopsis:
// bzImage [dump <file>] | [initramfs input-bzimage initramfs output-bzimage]
//
// Description:
// Read a bzImage in, change it, write it out, or print info.
package main
import (
"fmt"
"io/ioutil"
"log"
"strings"
flag "github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/bzimage"
)
var argcounts = map[string]int{
"dump": 2,
"initramfs": 4,
}
var cmdUsage = "Usage: bzImage [dump <file>] | [initramfs input-bzimage initramfs output-bzimage]"
func usage() {
log.Fatalf(cmdUsage)
}
func main() {
flag.Parse()
a := flag.Args()
if len(a) < 2 {
usage()
}
n, ok := argcounts[a[0]]
if !ok || len(a) != n {
usage()
}
var br = &bzimage.BzImage{}
switch a[0] {
case "dump", "initramfs":
b, err := ioutil.ReadFile(a[1])
if err != nil {
log.Fatal(err)
}
if err = br.UnmarshalBinary(b); err != nil {
log.Fatal(err)
}
}
switch a[0] {
case "dump":
fmt.Printf("%s\n", strings.Join(br.Header.Show(), "\n"))
case "initramfs":
if err := br.AddInitRAMFS(a[2]); err != nil {
log.Fatal(err)
}
b, err := br.MarshalBinary()
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(a[3], b, 0644); err != nil {
log.Fatal(err)
}
}
}