This repository was archived by the owner on Jun 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_c.c
More file actions
382 lines (291 loc) · 7.79 KB
/
lib_c.c
File metadata and controls
382 lines (291 loc) · 7.79 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "lib_asm.h"
#include "lib_c.h"
#include "string.h"
#define TTY 0x03F8
#define DLLB 0
#define IER 1
#define DLHB 1
#define IIR 2
#define FCR 2
#define LCR 3
#define MCR 4
#define LSR 5
#define MSR 6
#define SCRATCH 7
typedef struct __attribute__((packed)) {
unsigned short handler_15_0;
unsigned short selector;
unsigned char always_zero;
unsigned char type;
unsigned short handler_31_16;
} IDT_Entry;
IDT_Entry idt[ 256 ];
extern const unsigned char ISR_Template_Start;
extern const unsigned char ISR_Template_Error_Start;
extern const unsigned char ISR_Template_Error_End;
extern const unsigned char ISR_Template_Num;
extern const unsigned char ISR_Template_CFunc_Start;
extern const unsigned char ISR_Template_CFunc_Addr;
extern const unsigned char ISR_Template_CFunc_End;
extern const unsigned char ISR_Template_End;
//void putstring(char * str);
//void _printInt(int val, int base);
//void _printf(char * fmt, ...);
// 0x40 is bad!! Re-code to handle changing size of ISR stub code
// based on ( ISR_template_end - ISR_template_start )
#define ISR_STUB_LENGTH 0x40
unsigned char irq_handler[ 256 * ISR_STUB_LENGTH ];
// get a character from the serial port console
// echoes back to the console if echo is non-zero
char getcc( int echo ) {
char c;
while ( ( inb( TTY + LSR ) & 0x01 ) == 0x00 )
;
c = inb( TTY );
if ( echo )
putcc( c );
return c;
}
// gets up to 8 hex digits & stores in num
// returns 0 on success and non-zero on invalid input
int gethex( unsigned long *num, int digits, int echo ) {
char buf;
int i;
if ( echo )
printss( " " );
*num = 0;
for ( i = 0; i < digits; i++ ) {
buf = lowercase( getcc( echo ) );
if ( ( buf < '0' ) || ( buf > 'f' ) || ( ( buf > '9' ) && ( buf < 'a' ) ) )
return -1;
if ( buf > '9' )
buf -= 'a' - 10;
else
buf -= '0';
*num = ( *num << 4 ) + buf;
}
return 0;
}
void idt_install() {
printss( "\nidt_install()" );
printss( "\n&irq_handler = " );
printhex( (unsigned long) &irq_handler, 8 );
printss( "\nsizeof( irq_handler ) = " );
printhex( sizeof( irq_handler ), 8 );
printss( "\n&irq_count = " );
printhex( (unsigned long) &irq_count, 8 );
printss( "\nsizeof( irq_count ) = " );
printhex( sizeof( irq_count ), 8 );
printss( "\n&idt = " );
printhex( (unsigned long) &idt, 8 );
printss( "\nsizeof( idt ) = " );
printhex( sizeof( idt ), 8 );
printss( "\n&idt = " );
printhex( (unsigned long) &idt, 8 );
IDTR idtr;
idtr.limit = sizeof( idt ) - 1;
idtr.base = &idt;
lidt( &idtr );
// Initialize IDT and by extension, IRQ count and ISR stubs
int i;
for ( i = 0; i < 256; i++ )
idt_set_gate( i, NULL, 0x0008, 0x8E );
// Re-map BIOS-mapped IRQs so as not to conflict with reserved
// IDT mappings < 32...specifically, IRQ0-15 are re-mapped to
// IDT entries 32-47. Otherwise, exceptions, such as double
// fault (IDT 8) would be a problem!
outb( 0x20, 0x11 );
outb( 0xA0, 0x11 );
outb( 0x21, 0x20 );
outb( 0xA1, 0x28 );
outb( 0x21, 0x04 );
outb( 0xA1, 0x02 );
outb( 0x21, 0x01 );
outb( 0xA1, 0x01 );
outb( 0x21, 0x00 );
outb( 0xA1, 0x00 );
}
void idt_set_gate( unsigned char num, void ( *handler )( unsigned char num, ISR_Stack_Frame isf ), unsigned short selector, unsigned char type ) {
idt[ num ].handler_15_0 = ( unsigned long ) &irq_handler[ num * ISR_STUB_LENGTH ];
idt[ num ].selector = selector;
idt[ num ].always_zero = 0x00;
idt[ num ].type = type;
idt[ num ].handler_31_16 = ( unsigned long ) &irq_handler[ num * ISR_STUB_LENGTH ] >> 16;
irq_count[ num ] = 0;
const unsigned char *src = &ISR_Template_Start;
const unsigned char *cfunc = ( unsigned char * ) &handler;
unsigned char *dst = &irq_handler[ num * ISR_STUB_LENGTH ];
while ( src < &ISR_Template_Start + ISR_STUB_LENGTH ) {
// Blank memory if entire ISR stub is already copied
if ( src >= &ISR_Template_End )
*dst = 0x00;
// CPU pushes error code automatically; replace error push code with no-op's
else if ( ( src >= &ISR_Template_Error_Start ) && ( src < &ISR_Template_Error_End ) &&
( ( num == 8 ) || ( ( num >= 10 ) && ( num <= 14 ) ) ) )
*dst = 0x90; // No-op
// Insert ISR number in code
else if ( src == &ISR_Template_Num )
*dst = num;
// Have hook to C language function? Copy function pointer to call code
else if ( ( src >= &ISR_Template_CFunc_Addr ) && ( src < &ISR_Template_CFunc_Addr + 4 ) &&
( handler != NULL ) )
*dst = *cfunc++;
// No hook to C language function, replace call code with no-op's
else if ( ( src >= &ISR_Template_CFunc_Start ) && ( src < &ISR_Template_CFunc_End ) &&
( handler == NULL ) )
*dst = 0x90; // No-op
// Otherwise, just copy the code byte-for-byte
else
*dst = *src;
src++;
dst++;
}
}
// converts to lowercase
char lowercase( char c ) {
if ( ( c >= 'A' ) && ( c <= 'Z' ) )
c += 'a' - 'A';
return c;
}
char uppercase( char c ) {
if ( ( c >= 'a' ) && ( c <= 'z' ) )
c -= 'a' - 'A';
return c;
}
// print a number in hexadecimal format
void printhex( unsigned long num, int digits ) {
int i, buf;
for ( i = 0; i < digits; i++ ) {
buf = ( num >> ( ( digits - i - 1 ) * 4 ) ) & 0xF;
if ( buf < 10 )
buf += '0';
else
buf += 'A' - 10;
putcc( buf );
}
}
// print a null-terminated string
void printss( char *s ) {
while ( *s ) {
putcc( *s );
s++;
}
}
// print n characters from the string pointed to by s
// protect against control characters < 32 and > 127
void printsss( char *s, int n ) {
while ( n-- ) {
if ( *s > ' ' )
putcc( *s );
else
putcc( '.' );
s++;
}
}
// send a character to the serial port console
void putcc( char c ) {
if ( c == 0x0A )
putcc( 0x0D );
while ( ( inb( TTY + LSR ) & 0x20 ) == 0x00 )
;
outb( TTY, c );
}
// send a string
void putstring(char * str)
{
while (*str != '\0')
{
putcc(*str);
str++;
}
}
void _printInt(int val, int base)
{
char buff[32];
char * ptr;
int n;
ptr = &buff[sizeof(buff)-1];
*ptr = '\0';
for(; val != 0; val /= base)
{
ptr--;
n = val%base;
if (n < 10)
{
*ptr = n + '0';
}
else
{
*ptr = n - 10 + 'A';
}
}
putstring(ptr);
}
void _printf(char * fmt, ...)
{
int i = 0;
char *s, *p = (char *)fmt;
unsigned long *argptr = (unsigned long *)&fmt;
argptr++;
while (*p != '\0') // is not NULL
{
if (*p != '%')
{
putcc(*p);
p++;
continue;
}
else if (*p == '%')
{
switch (*(p+1))
{
case 'd':
i = *(int *)argptr;
_printInt(i, 10);
argptr++;
p+=2;
break;
case 's':
s = (char *)*argptr;
putstring(s);
argptr++;
p+=2;
break;
case 'x':
i = *(int *)argptr;
_printInt(i, 16);
argptr++;
p+=2;
break;
default:
putcc(*p);
p++;
break;
}
}
}
}
char* uppercase_str(char* input)
{
unsigned int counter = 0;
unsigned int inputLength = strlen(input);
while (counter < inputLength) //iterate through input, converting the characters to uppercase
{
if ((short)input[counter] >= 97 && (short)input[counter] <= 122)
input[counter] -= 32;
counter++;
}
return input;
}
char* lowercase_str(char* input)
{
unsigned int counter = 0;
unsigned int inputLength = strlen(input);
while (counter < inputLength) //iterate through input, converting the characters to uppercase
{
if ((short)input[counter] >= 65 && (short)input[counter] <= 90)
input[counter] += 32;
counter++;
}
return input;
}