-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttyredirect.c
211 lines (138 loc) · 3.8 KB
/
ttyredirect.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "ttyredirect.h"
#include "littlelibc.h"
#include "utility.h"
#include "drawing.h"
#define SIO_DATA 0x1F801050
#define SIO_STAT 0x1F801054
#define SIO_MODE 0x1F801058
#define SIO_CTRL 0x1F80105E
// File control block structure
struct SIOFCB{
ulong FCB_STATUS;
ulong FCB_DISKID;
ulong FCB_TADDR;
ulong FCB_TLEN;
ulong FCB_FPOS;
ulong FCB_DFLAGS;
ulong FCB_ERROR;
ulong FCB_DCB;
ulong FCB_FSIZE;
ulong FCB_LBN;
ulong FCB_FCBID;
};
#pragma GCC push options
#pragma GCC optimize ("-O0")
#define MODE_READ 1
#define MODE_WRITE 2
void __attribute__((section(".kttytext"))) KTTYAction( struct SIOFCB * fcb, ulong inMode ){
ulong i = 0;
ulong bailout = 0;
if( inMode != MODE_WRITE )
return;
ulong transferLength = fcb->FCB_TLEN;
ulong readAddr = fcb->FCB_TADDR;
for( i = readAddr; i < readAddr + transferLength; i++ ){
bailout = 0;
// SR_TXU | SR_TXRDY
while( (*(uchar*)SIO_STAT & 0x05) == 0 ){
if ( bailout++ > 8000 )
break;
}
*(uchar*)SIO_DATA = *(uchar*)readAddr;
}
}
void __attribute__((section(".kttytext"))) KTTYNull(){
}
ulong __attribute__((section(".kttytext"))) KTTYReturn0(){
return 0;
}
#pragma GCC pop options
char deviceName[] = "tty";
char deviceDesc[] = "SIO TTY";
struct ttyDevice{
char * deviceName;
ulong flags;
ulong blockSize;
char * deviceDesc;
void * init;
void * open;
void * action;
void * close;
void * ioctl;
void * read;
void * write;
void * erase;
void * undelete;
void * firstFile;
void * nextFile;
void * format;
void * chdir;
void * rename;
void * deinit;
void * check;
} defaultTTYDevice = {
(char*)deviceName,
3,
0,
(char*)deviceDesc,
&KTTYNull, // init
&KTTYReturn0, // open
&KTTYAction, // action
&KTTYReturn0, // close
&KTTYReturn0, // ioctl
&KTTYNull, // read
&KTTYNull, // write
&KTTYNull, // erase
&KTTYNull, // undelete
&KTTYNull, // firstfile
&KTTYNull, // nextfile
&KTTYNull, // format
&KTTYNull, // chdir
&KTTYNull, // rename
&KTTYNull, // deinit/remove
&KTTYNull, // check
};
char ttyInstalled = 0;
ulong IsTTYInstalled(){
return ttyInstalled;
}
void RemoveTTY(){
char* namePointer = (char*)&deviceName;
if ( !ttyInstalled )
return;
// Close stdin
CloseFile( 0 );
// Close stdout
CloseFile( 1 );
// Remove the dummy TTY device
RemoveDevice( (char*)&namePointer );
}
void InstallTTY(){
if ( ttyInstalled )
return;
ulong wasCritical = EnterCritical();
RemoveTTY();
NewMemcpy( (char*)&__ktty_dest_start, (char*)&__ktty_src, (ulong)&__ktty_length );
// need a wee bit more stack for these ones...
__asm__ volatile( "addiu $sp, $sp, -0x08\n\t" );
AddDevice( (void*)&defaultTTYDevice );
// Open stdin and stdout again
OpenFile( deviceName, 2 );
OpenFile( deviceName, 1 );
__asm__ volatile( "addiu $sp, $sp, 0x08\n\t" );
if ( !wasCritical )
ExitCritical();
ttyInstalled = 1;
}
void TTYViewMemoryAllocation(){
ClearScreenText();
Blah( "\n\n\n\n\n\n" );
Blah( "__ktty_src %x\n", (ulong)&__ktty_src );
Blah( "__ktty_length %x\n", (ulong)&__ktty_length );
Blah( "__ktty_dest_start %x\n", (ulong)&__ktty_dest_start );
Blah( "__ktty_dest_end %x\n", (ulong)&__ktty_dest_end );
HoldMessage();
}