github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/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(a[0], a[1:]...) 39 if err != nil { 40 log.Fatalf("Can't open pty: %v", err) 41 } 42 // Make a good faith effort to set up root. This being 43 // a kind of init program, we do our best and keep going. 44 if *setupRoot { 45 util.Rootfs() 46 } 47 48 in, out := io.Reader(os.Stdin), io.Writer(os.Stdout) 49 50 // This switch is kind of hokey, true, but it's also quite convenient for users. 51 switch { 52 // A raw IO port for serial console 53 case []byte(*serial)[0] == '0': 54 u, err := openUART(*serial) 55 if err != nil { 56 log.Fatalf("Sorry, can't get a uart: %v", err) 57 } 58 in, out = u, u 59 case *serial == "i8042": 60 u, err := openi8042() 61 if err != nil { 62 log.Fatalf("Sorry, can't get an i8042: %v", err) 63 } 64 in, out = u, os.Stdout 65 case *serial == "stdio": 66 default: 67 log.Fatalf("console must be one of stdio, i8042, or an IO port with a leading 0 (e.g. 0x3f8)") 68 } 69 70 err = p.Start() 71 if err != nil { 72 fmt.Printf("Can't start %v: %v", a, err) 73 os.Exit(1) 74 } 75 kid := p.C.Process.Pid 76 77 // You need the \r\n as we are now in raw mode! 78 fmt.Printf("Started %d\r\n", kid) 79 80 go io.Copy(out, p.Ptm) 81 82 go func() { 83 var data = make([]byte, 1) 84 for { 85 if _, err := in.Read(data); err != nil { 86 fmt.Printf("kid stdin: done\n") 87 } 88 if data[0] == '\r' { 89 if _, err := out.Write(data); err != nil { 90 log.Printf("error on echo %v: %v", data, err) 91 } 92 data[0] = '\n' 93 } 94 if _, err := p.Ptm.Write(data); err != nil { 95 log.Printf("Error writing input to ptm: %v: give up\n", err) 96 break 97 } 98 } 99 }() 100 101 if err := p.Wait(); err != nil { 102 log.Fatalf("%v", err) 103 } 104 os.Exit(0) 105 }