gopkg.in/hugelgupf/u-root.v4@v4.0.0-20180831060141-1d761fb73d50/cmds/bzimage/bzimage.go (about)

     1  // Copyright 2012-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  // bzImage is used to modify bzImage files.
     6  // It reads the image in, applies an operator, and writes a new one out.
     7  //
     8  // Synopsis:
     9  //     bzImage [dump <file>] | [initramfs input-bzimage initramfs output-bzimage]
    10  //
    11  // Description:
    12  //	Read a bzImage in, change it, write it out, or print info.
    13  package main
    14  
    15  import (
    16  	"fmt"
    17  	"io/ioutil"
    18  	"log"
    19  	"strings"
    20  
    21  	flag "github.com/spf13/pflag"
    22  	"github.com/u-root/u-root/pkg/bzimage"
    23  )
    24  
    25  var argcounts = map[string]int{
    26  	"dump":      2,
    27  	"initramfs": 4,
    28  }
    29  
    30  var cmdUsage = "Usage: bzImage  [dump <file>] | [initramfs input-bzimage initramfs output-bzimage]"
    31  
    32  func usage() {
    33  	log.Fatalf(cmdUsage)
    34  }
    35  
    36  func main() {
    37  	flag.Parse()
    38  
    39  	a := flag.Args()
    40  	if len(a) < 2 {
    41  		usage()
    42  	}
    43  	n, ok := argcounts[a[0]]
    44  	if !ok || len(a) != n {
    45  		usage()
    46  	}
    47  
    48  	var br = &bzimage.BzImage{}
    49  	switch a[0] {
    50  	case "dump", "initramfs":
    51  		b, err := ioutil.ReadFile(a[1])
    52  		if err != nil {
    53  			log.Fatal(err)
    54  		}
    55  		if err = br.UnmarshalBinary(b); err != nil {
    56  			log.Fatal(err)
    57  		}
    58  	}
    59  
    60  	switch a[0] {
    61  	case "dump":
    62  		fmt.Printf("%s\n", strings.Join(br.Header.Show(), "\n"))
    63  	case "initramfs":
    64  		if err := br.AddInitRAMFS(a[2]); err != nil {
    65  			log.Fatal(err)
    66  		}
    67  
    68  		b, err := br.MarshalBinary()
    69  		if err != nil {
    70  			log.Fatal(err)
    71  		}
    72  
    73  		if err := ioutil.WriteFile(a[3], b, 0644); err != nil {
    74  			log.Fatal(err)
    75  		}
    76  	}
    77  }