2018-04-12 18:59:07 -04:00
|
|
|
package bootcommand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-04-13 02:05:05 -04:00
|
|
|
"os"
|
2018-04-12 18:59:07 -04:00
|
|
|
"strings"
|
2018-04-13 02:05:05 -04:00
|
|
|
"time"
|
2018-04-12 18:59:07 -04:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2018-04-13 02:05:05 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/common"
|
2018-04-12 18:59:07 -04:00
|
|
|
)
|
|
|
|
|
2018-04-12 19:32:22 -04:00
|
|
|
// SendCodeFunc will be called to send codes to the VM
|
2018-04-12 18:59:07 -04:00
|
|
|
type SendCodeFunc func([]string) error
|
2018-04-19 20:02:39 -04:00
|
|
|
type scMap map[string]*scancode
|
2018-04-12 18:59:07 -04:00
|
|
|
|
2018-04-14 00:06:37 -04:00
|
|
|
type pcXTDriver struct {
|
2018-04-13 02:05:05 -04:00
|
|
|
interval time.Duration
|
2018-04-12 20:00:09 -04:00
|
|
|
sendImpl SendCodeFunc
|
2018-04-19 20:02:39 -04:00
|
|
|
specialMap scMap
|
2018-04-12 18:59:07 -04:00
|
|
|
scancodeMap map[rune]byte
|
2018-04-12 20:00:09 -04:00
|
|
|
buffer [][]string
|
|
|
|
// TODO: set from env
|
|
|
|
scancodeChunkSize int
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 20:02:39 -04:00
|
|
|
type scancode struct {
|
|
|
|
make []string
|
|
|
|
break_ []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *scancode) makeBreak() []string {
|
|
|
|
return append(sc.make, sc.break_...)
|
|
|
|
}
|
|
|
|
|
2018-04-14 00:06:37 -04:00
|
|
|
// NewPCXTDriver creates a new boot command driver for VMs that expect PC-XT
|
2018-04-13 02:11:14 -04:00
|
|
|
// keyboard codes. `send` should send its argument to the VM. `chunkSize` should
|
|
|
|
// be the maximum number of keyboard codes to send to `send` at one time.
|
2018-08-22 14:25:43 -04:00
|
|
|
func NewPCXTDriver(send SendCodeFunc, chunkSize int, interval int) *pcXTDriver {
|
2018-04-13 02:05:05 -04:00
|
|
|
// We delay (default 100ms) between each input event to allow for CPU or
|
|
|
|
// network latency. See PackerKeyEnv for tuning.
|
|
|
|
keyInterval := common.PackerKeyDefault
|
|
|
|
if delay, err := time.ParseDuration(os.Getenv(common.PackerKeyEnv)); err == nil {
|
|
|
|
keyInterval = delay
|
|
|
|
}
|
2018-08-22 14:25:43 -04:00
|
|
|
// Override interval based on builder-specific override
|
|
|
|
if interval >= 0 {
|
|
|
|
keyInterval = time.Duration(interval) * time.Millisecond
|
|
|
|
}
|
2018-04-14 00:06:37 -04:00
|
|
|
// Scancodes reference: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
|
|
|
|
// https://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html
|
2018-04-12 19:32:22 -04:00
|
|
|
//
|
|
|
|
// Scancodes are recorded here in pairs. The first entry represents
|
|
|
|
// the key press and the second entry represents the key release and is
|
|
|
|
// derived from the first by the addition of 0x80.
|
2018-04-19 20:02:39 -04:00
|
|
|
sMap := make(scMap)
|
|
|
|
sMap["bs"] = &scancode{[]string{"0e"}, []string{"8e"}}
|
|
|
|
sMap["del"] = &scancode{[]string{"e0", "53"}, []string{"e0", "d3"}}
|
|
|
|
sMap["down"] = &scancode{[]string{"e0", "50"}, []string{"e0", "d0"}}
|
|
|
|
sMap["end"] = &scancode{[]string{"e0", "4f"}, []string{"e0", "cf"}}
|
|
|
|
sMap["enter"] = &scancode{[]string{"1c"}, []string{"9c"}}
|
|
|
|
sMap["esc"] = &scancode{[]string{"01"}, []string{"81"}}
|
|
|
|
sMap["f1"] = &scancode{[]string{"3b"}, []string{"bb"}}
|
|
|
|
sMap["f2"] = &scancode{[]string{"3c"}, []string{"bc"}}
|
|
|
|
sMap["f3"] = &scancode{[]string{"3d"}, []string{"bd"}}
|
|
|
|
sMap["f4"] = &scancode{[]string{"3e"}, []string{"be"}}
|
|
|
|
sMap["f5"] = &scancode{[]string{"3f"}, []string{"bf"}}
|
|
|
|
sMap["f6"] = &scancode{[]string{"40"}, []string{"c0"}}
|
|
|
|
sMap["f7"] = &scancode{[]string{"41"}, []string{"c1"}}
|
|
|
|
sMap["f8"] = &scancode{[]string{"42"}, []string{"c2"}}
|
|
|
|
sMap["f9"] = &scancode{[]string{"43"}, []string{"c3"}}
|
|
|
|
sMap["f10"] = &scancode{[]string{"44"}, []string{"c4"}}
|
|
|
|
sMap["f11"] = &scancode{[]string{"57"}, []string{"d7"}}
|
|
|
|
sMap["f12"] = &scancode{[]string{"58"}, []string{"d8"}}
|
|
|
|
sMap["home"] = &scancode{[]string{"e0", "47"}, []string{"e0", "c7"}}
|
|
|
|
sMap["insert"] = &scancode{[]string{"e0", "52"}, []string{"e0", "d2"}}
|
|
|
|
sMap["left"] = &scancode{[]string{"e0", "4b"}, []string{"e0", "cb"}}
|
|
|
|
sMap["leftalt"] = &scancode{[]string{"38"}, []string{"b8"}}
|
|
|
|
sMap["leftctrl"] = &scancode{[]string{"1d"}, []string{"9d"}}
|
|
|
|
sMap["leftshift"] = &scancode{[]string{"2a"}, []string{"aa"}}
|
|
|
|
sMap["leftsuper"] = &scancode{[]string{"e0", "5b"}, []string{"e0", "db"}}
|
|
|
|
sMap["menu"] = &scancode{[]string{"e0", "5d"}, []string{"e0", "dd"}}
|
|
|
|
sMap["pagedown"] = &scancode{[]string{"e0", "51"}, []string{"e0", "d1"}}
|
|
|
|
sMap["pageup"] = &scancode{[]string{"e0", "49"}, []string{"e0", "c9"}}
|
|
|
|
sMap["return"] = &scancode{[]string{"1c"}, []string{"9c"}}
|
|
|
|
sMap["right"] = &scancode{[]string{"e0", "4d"}, []string{"e0", "cd"}}
|
|
|
|
sMap["rightalt"] = &scancode{[]string{"e0", "38"}, []string{"e0", "b8"}}
|
|
|
|
sMap["rightctrl"] = &scancode{[]string{"e0", "1d"}, []string{"e0", "9d"}}
|
|
|
|
sMap["rightshift"] = &scancode{[]string{"36"}, []string{"b6"}}
|
|
|
|
sMap["rightsuper"] = &scancode{[]string{"e0", "5c"}, []string{"e0", "dc"}}
|
|
|
|
sMap["spacebar"] = &scancode{[]string{"39"}, []string{"b9"}}
|
|
|
|
sMap["tab"] = &scancode{[]string{"0f"}, []string{"8f"}}
|
|
|
|
sMap["up"] = &scancode{[]string{"e0", "48"}, []string{"e0", "c8"}}
|
2018-04-12 18:59:07 -04:00
|
|
|
|
|
|
|
scancodeIndex := make(map[string]byte)
|
|
|
|
scancodeIndex["1234567890-="] = 0x02
|
|
|
|
scancodeIndex["!@#$%^&*()_+"] = 0x02
|
|
|
|
scancodeIndex["qwertyuiop[]"] = 0x10
|
|
|
|
scancodeIndex["QWERTYUIOP{}"] = 0x10
|
|
|
|
scancodeIndex["asdfghjkl;'`"] = 0x1e
|
|
|
|
scancodeIndex[`ASDFGHJKL:"~`] = 0x1e
|
|
|
|
scancodeIndex[`\zxcvbnm,./`] = 0x2b
|
|
|
|
scancodeIndex["|ZXCVBNM<>?"] = 0x2b
|
|
|
|
scancodeIndex[" "] = 0x39
|
|
|
|
|
|
|
|
scancodeMap := make(map[rune]byte)
|
|
|
|
for chars, start := range scancodeIndex {
|
|
|
|
var i byte = 0
|
|
|
|
for len(chars) > 0 {
|
|
|
|
r, size := utf8.DecodeRuneInString(chars)
|
|
|
|
chars = chars[size:]
|
|
|
|
scancodeMap[r] = start + i
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 00:06:37 -04:00
|
|
|
return &pcXTDriver{
|
2018-04-13 02:05:05 -04:00
|
|
|
interval: keyInterval,
|
2018-04-12 20:00:09 -04:00
|
|
|
sendImpl: send,
|
|
|
|
specialMap: sMap,
|
|
|
|
scancodeMap: scancodeMap,
|
|
|
|
scancodeChunkSize: chunkSize,
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:44:50 -04:00
|
|
|
// Flush send all scanecodes.
|
|
|
|
func (d *pcXTDriver) Flush() error {
|
2018-04-12 20:00:09 -04:00
|
|
|
defer func() {
|
|
|
|
d.buffer = nil
|
|
|
|
}()
|
|
|
|
sc, err := chunkScanCodes(d.buffer, d.scancodeChunkSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, b := range sc {
|
|
|
|
if err := d.sendImpl(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-13 02:05:05 -04:00
|
|
|
time.Sleep(d.interval)
|
2018-04-12 20:00:09 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-14 00:06:37 -04:00
|
|
|
func (d *pcXTDriver) SendKey(key rune, action KeyAction) error {
|
2018-04-12 18:59:07 -04:00
|
|
|
keyShift := unicode.IsUpper(key) || strings.ContainsRune(shiftedChars, key)
|
|
|
|
|
2018-04-19 20:02:39 -04:00
|
|
|
var sc []string
|
2018-04-12 18:59:07 -04:00
|
|
|
|
|
|
|
if action&(KeyOn|KeyPress) != 0 {
|
2018-04-19 20:02:39 -04:00
|
|
|
scInt := d.scancodeMap[key]
|
2018-04-12 18:59:07 -04:00
|
|
|
if keyShift {
|
2018-04-19 20:02:39 -04:00
|
|
|
sc = append(sc, "2a")
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
2018-04-19 20:02:39 -04:00
|
|
|
sc = append(sc, fmt.Sprintf("%02x", scInt))
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if action&(KeyOff|KeyPress) != 0 {
|
2018-04-19 20:02:39 -04:00
|
|
|
scInt := d.scancodeMap[key] + 0x80
|
2018-04-12 18:59:07 -04:00
|
|
|
if keyShift {
|
2018-04-19 20:02:39 -04:00
|
|
|
sc = append(sc, "aa")
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
2018-04-19 20:02:39 -04:00
|
|
|
sc = append(sc, fmt.Sprintf("%02x", scInt))
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
|
|
|
|
2018-04-19 19:09:38 -04:00
|
|
|
log.Printf("Sending char '%c', code '%s', shift %v",
|
2018-04-19 20:02:39 -04:00
|
|
|
key, strings.Join(sc, ""), keyShift)
|
2018-04-12 18:59:07 -04:00
|
|
|
|
2018-04-19 20:02:39 -04:00
|
|
|
d.send(sc)
|
2018-04-12 20:00:09 -04:00
|
|
|
return nil
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
|
|
|
|
2018-04-14 00:06:37 -04:00
|
|
|
func (d *pcXTDriver) SendSpecial(special string, action KeyAction) error {
|
2018-04-12 18:59:07 -04:00
|
|
|
keyCode, ok := d.specialMap[special]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("special %s not found.", special)
|
|
|
|
}
|
2018-04-19 19:44:50 -04:00
|
|
|
log.Printf("Special code '%s' '<%s>' found, replacing with: %v", action.String(), special, keyCode)
|
2018-04-12 18:59:07 -04:00
|
|
|
|
|
|
|
switch action {
|
|
|
|
case KeyOn:
|
2018-04-19 20:02:39 -04:00
|
|
|
d.send(keyCode.make)
|
2018-04-12 18:59:07 -04:00
|
|
|
case KeyOff:
|
2018-04-19 20:02:39 -04:00
|
|
|
d.send(keyCode.break_)
|
2018-04-12 18:59:07 -04:00
|
|
|
case KeyPress:
|
2018-04-19 20:02:39 -04:00
|
|
|
d.send(keyCode.makeBreak())
|
2018-04-12 20:00:09 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:44:50 -04:00
|
|
|
// send stores the codes in an internal buffer. Use Flush to send them.
|
2018-04-14 00:06:37 -04:00
|
|
|
func (d *pcXTDriver) send(codes []string) {
|
2018-04-12 20:00:09 -04:00
|
|
|
d.buffer = append(d.buffer, codes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func chunkScanCodes(sc [][]string, size int) (out [][]string, err error) {
|
|
|
|
var running []string
|
|
|
|
for _, codes := range sc {
|
|
|
|
if size > 0 {
|
|
|
|
if len(codes) > size {
|
|
|
|
return nil, fmt.Errorf("chunkScanCodes: size cannot be smaller than sc width.")
|
|
|
|
}
|
|
|
|
if len(running)+len(codes) > size {
|
|
|
|
out = append(out, running)
|
|
|
|
running = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
running = append(running, codes...)
|
|
|
|
}
|
|
|
|
if running != nil {
|
|
|
|
out = append(out, running)
|
2018-04-12 18:59:07 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|