github.hscsec.cn/u-root/u-root@v7.0.0+incompatible/cmds/exp/console/console.go (about)

     1  // Copyright 2015-2020 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  // sh. 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/libinit"
    21  	"github.com/u-root/u-root/pkg/pty"
    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", false, "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{"/bin/sh"}
    36  	}
    37  
    38  	p, err := pty.New()
    39  	if err != nil {
    40  		log.Fatalf("Console exits: 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  		libinit.SetEnv()
    47  		libinit.CreateRootfs()
    48  	}
    49  
    50  	in, out, err := console(*serial)
    51  
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  
    56  	err = p.Start()
    57  	if err != nil {
    58  		log.Fatalf("Console exits: can't start %v: %v", a, err)
    59  	}
    60  	kid := p.C.Process.Pid
    61  
    62  	// You need the \r\n as we are now in raw mode!
    63  	fmt.Printf("Started %d\r\n", kid)
    64  
    65  	go io.Copy(out, p.Ptm)
    66  
    67  	go func() {
    68  		var data = make([]byte, 1)
    69  		for {
    70  			if _, err := in.Read(data); err != nil {
    71  				fmt.Printf("kid stdin: done\n")
    72  			}
    73  			if data[0] == '\r' {
    74  				if _, err := out.Write(data); err != nil {
    75  					log.Printf("error on echo %v: %v", data, err)
    76  				}
    77  				data[0] = '\n'
    78  			}
    79  			if _, err := p.Ptm.Write(data); err != nil {
    80  				log.Printf("Error writing input to ptm: %v: give up\n", err)
    81  				break
    82  			}
    83  		}
    84  	}()
    85  
    86  	if err := p.Wait(); err != nil {
    87  		log.Fatalf("Console exits: %v", err)
    88  	}
    89  	log.Printf("Console all done")
    90  	os.Exit(0)
    91  }