-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
159 lines (145 loc) · 3.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// +build !wasm
package main
import (
// "encoding/hex"
"flag"
"fmt"
// "github.com/hajimehoshi/ebiten"
// "github.com/hajimehoshi/ebiten/ebitenutil"
"image"
"io/ioutil"
// "log"
// "math/rand"
"os"
"os/exec"
"strconv"
"strings"
// "path/filepath"
"runtime"
// "time"
)
const (
height = 120
width = 160
)
var (
vram = image.NewRGBA(image.Rect(0, 0, width, height))
)
func printf(format string, a ...interface{}) {
fmt.Printf(format, a...)
}
// func update(screen *ebiten.Image) error {
// for i := 0; i < width*height; i++ {
// vram.Pix[4*i] = uint8(rand.Int() & 0xFF)
// vram.Pix[4*i+1] = uint8(rand.Int() & 0xFF)
// vram.Pix[4*i+2] = uint8(rand.Int() & 0xFF)
// vram.Pix[4*i+3] = 0xff
// }
// if ebiten.IsRunningSlowly() {
// }
// screen.ReplacePixels(vram.Pix)
// ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS()))
// return nil
// }
func main() {
runtime.LockOSThread()
filename := flag.String("f", "", "binary filename (*.bin)")
// enableGUI := flag.Bool("gui", false, "gui mode")
silent := flag.Bool("silent", false, "silent mode")
flag.Parse()
// load binary
if *filename == "" {
printf("Please set filename\n")
os.Exit(1)
}
// disasm binary
exec.Command("sh", "-c", "head -c 49 "+*filename+" | ndisasm -b 16 -o 0x7c00 - | tee disasm16.txt").Run() // 16 bit mode
exec.Command("sh", "-c", "tail -c +50 "+*filename+" | ndisasm -b 32 -o 0x7c31 - | head -n 5000 | tee disasm32.txt").Run() // 32 bit mode
b, err := exec.Command("sh", "-c", "cat disasm16.txt disasm32.txt").CombinedOutput()
disasm := map[uint64]string{}
if err != nil {
panic(err)
}
for _, line := range strings.Split(string(b), "\n") {
row := strings.Fields(line)
if len(row) < 3 {
continue
}
ix, _ := strconv.ParseUint(row[0][2:], 16, 64)
disasm[ix] = strings.Join(row[1:], " ")
}
// disasm binary of ./xv6-public/kernel
b, err = exec.Command("sh", "-c", "objdump -d ./xv6-public/kernel | tail -n +7 | grep -E \"[0-9a-f]{8}:\"").CombinedOutput()
if err != nil {
panic(err)
}
for _, line := range strings.Split(string(b), "\n") {
if len(line) < 7 {
continue
}
ix, err := strconv.ParseUint(line[:8], 16, 64)
if err != nil {
panic(err)
}
disasm[ix] = line[10:]
disasm[ix-0x80000000] = line[10:]
}
bytes, err := LoadFile(*filename)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
// printf("enable GUI = %#v\n", *enableGUI)
printf("len(bytes) = %d\n", len(bytes))
// printf("bytes =\n%s", hex.Dump(bytes))
// setup emulator
e := NewEmulator(0x7c00+0x10240000, 0x7c00, 0x6f04, false, *silent, os.Stdin, os.Stdout, disasm)
for i := 0; i < len(bytes); i++ {
e.memory[uint32(i+0x7c00)] = bytes[i]
}
e.io.hdds[0], _ = os.Open(*filename)
// emulate
// chFinished := make(chan bool)
// go func(chFinished chan bool) {
// time.Sleep(3000 * time.Millisecond)
// for e.eip < 0x7c00+0x200000 {
i := 0
for {
// if !*silent && 0x8010376c < e.eip && e.eip < 0x801037d1 {
if false {
// if !*silent && i > 3635000 {
e.dump(i)
}
err := e.execInst()
if err != nil {
printf(err.Error())
os.Exit(1)
}
if e.eip == 0 || e.eip == 0x7c00 {
break
}
i++
}
if !*silent {
e.dump(i)
}
printf("End of program\n")
// chFinished <- true
// }(chFinished)
// setup gui
// if *enableGUI {
// err := ebiten.Run(update, width, height, 2, "x86 emulator")
// if err != nil {
// log.Fatal(err.Error())
// }
// }
// <-chFinished
}
// LoadFile by filename
func LoadFile(filename string) ([]byte, error) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return bytes, nil
}