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