github.com/jlowellwofford/u-root@v1.0.0/cmds/console/console.go (about)

     1  // Copyright 2015-2017 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  // console implements a basic console. It establishes a pair of files
     6  // to read from, the default being a UART at 0x3f8, but an alternative
     7  // being just stdin and stdout. It will also set up a root file system
     8  // using util.Rootfs, although this can be disabled as well.
     9  // Console uses a Go version of fork_pty to start up a shell, default
    10  // /ubin/rush. Console runs until the shell exits and then exits itself.
    11  package main
    12  
    13  import (
    14  	"flag"
    15  	"fmt"
    16  	"io"
    17  	"log"
    18  	"os"
    19  
    20  	"github.com/u-root/u-root/pkg/pty"
    21  	"github.com/u-root/u-root/pkg/uroot/util"
    22  )
    23  
    24  var (
    25  	serial    = flag.String("serial", "0x3f8", "which IO device: stdio, i8042, or serial port starting with 0")
    26  	setupRoot = flag.Bool("setuproot", true, "Set up a root file system")
    27  )
    28  
    29  func main() {
    30  	fmt.Printf("console -- starting")
    31  	flag.Parse()
    32  
    33  	a := flag.Args()
    34  	if len(a) == 0 {
    35  		a = []string{"/ubin/rush"}
    36  	}
    37  
    38  	p, err := pty.New()
    39  	if err != nil {
    40  		log.Fatalf("Can't open pty: %v", err)
    41  	}
    42  	p.Command(a[0], a[1:]...)
    43  	// Make a good faith effort to set up root. This being
    44  	// a kind of init program, we do our best and keep going.
    45  	if *setupRoot {
    46  		util.Rootfs()
    47  	}
    48  
    49  	in, out := io.Reader(os.Stdin), io.Writer(os.Stdout)
    50  
    51  	// This switch is kind of hokey, true, but it's also quite convenient for users.
    52  	switch {
    53  	// A raw IO port for serial console
    54  	case []byte(*serial)[0] == '0':
    55  		u, err := openUART(*serial)
    56  		if err != nil {
    57  			log.Fatalf("Sorry, can't get a uart: %v", err)
    58  		}
    59  		in, out = u, u
    60  	case *serial == "i8042":
    61  		u, err := openi8042()
    62  		if err != nil {
    63  			log.Fatalf("Sorry, can't get an i8042: %v", err)
    64  		}
    65  		in, out = u, os.Stdout
    66  	case *serial == "stdio":
    67  	default:
    68  		log.Fatalf("console must be one of stdio, i8042, or an IO port with a leading 0 (e.g. 0x3f8)")
    69  	}
    70  
    71  	err = p.Start()
    72  	if err != nil {
    73  		fmt.Printf("Can't start %v: %v", a, err)
    74  		os.Exit(1)
    75  	}
    76  	kid := p.C.Process.Pid
    77  
    78  	// You need the \r\n as we are now in raw mode!
    79  	fmt.Printf("Started %d\r\n", kid)
    80  
    81  	go io.Copy(out, p.Ptm)
    82  
    83  	go func() {
    84  		var data = make([]byte, 1)
    85  		for {
    86  			if _, err := in.Read(data); err != nil {
    87  				fmt.Printf("kid stdin: done\n")
    88  			}
    89  			if data[0] == '\r' {
    90  				if _, err := out.Write(data); err != nil {
    91  					log.Printf("error on echo %v: %v", data, err)
    92  				}
    93  				data[0] = '\n'
    94  			}
    95  			if _, err := p.Ptm.Write(data); err != nil {
    96  				log.Printf("Error writing input to ptm: %v: give up\n", err)
    97  				break
    98  			}
    99  		}
   100  	}()
   101  
   102  	if err := p.Wait(); err != nil {
   103  		log.Fatalf("%v", err)
   104  	}
   105  	os.Exit(0)
   106  }