Subversion Repositories Kolibri OS

Rev

Rev 700 | Rev 732 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 serge 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
709 diamond 3
;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;;
431 serge 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 serge 7
 
593 mikedld 8
$Revision: 709 $
9
 
10
 
465 serge 11
DRV_COMPAT   equ  5  ;minimal required drivers version
12
DRV_CURRENT  equ  5  ;current drivers model version
214 serge 13
 
227 serge 14
DRV_VERSION equ (DRV_COMPAT shl 16) or DRV_CURRENT
15
 
164 serge 16
align 4
17
proc attach_int_handler stdcall, irq:dword, handler:dword
18
 
709 diamond 19
         mov  ebx, [irq]        ;irq num
20
         test ebx, ebx
21
         jz   .err
22
         cmp  ebx, 15           ; hidnplayr says: we only have 16 IRQ's
23
         ja   .err
24
         mov  eax, [handler]
25
         test eax, eax
26
         jz   .err
27
         mov  [irq_tab+ebx*4], eax
28
         stdcall enable_irq, [irq]
29
         ret
164 serge 30
.err:
709 diamond 31
         xor eax, eax
32
         ret
164 serge 33
endp
34
 
35
align 4
36
proc  detach_int_handler
37
 
38
	   ret
39
endp
40
 
41
align 4
42
proc enable_irq stdcall, irq_line:dword
672 hidnplayr 43
	   mov ebx, [irq_line]
44
	   mov edx, 0x21
45
	   cmp ebx, 8
46
	   jb @F
47
	   mov edx, 0xA1
48
	   sub ebx,8
164 serge 49
@@:
672 hidnplayr 50
	   in al,dx
51
	   btr eax, ebx
52
	   out dx, al
53
	   ret
164 serge 54
endp
55
 
56
align 16
57
;; proc irq_serv
58
 
59
irq_serv:
60
 
61
.irq_1:
62
	   push eax
63
	   mov eax, 1
64
	   jmp .main
65
align 4
66
.irq_2:
67
	   push eax
68
	   mov eax, 2
69
	   jmp .main
70
align 4
71
.irq_3:
72
	   push eax
73
	   mov eax, 3
74
	   jmp .main
75
align 4
76
.irq_4:
77
	   push eax
78
	   mov eax, 4
79
	   jmp .main
80
align 4
81
.irq_5:
82
	   push eax
83
	   mov eax, 5
84
	   jmp .main
85
align 4
86
.irq_6:
87
	   push eax
88
	   mov eax, 6
89
	   jmp .main
90
align 4
91
.irq_7:
92
	   push eax
93
	   mov eax, 7
94
	   jmp .main
95
align 4
96
.irq_8:
97
	   push eax
98
	   mov eax, 8
99
	   jmp .main
100
align 4
101
.irq_9:
102
	   push eax
103
	   mov eax, 9
104
	   jmp .main
105
align 4
106
.irq_10:
107
	   push eax
108
	   mov eax, 10
109
	   jmp .main
110
align 4
111
.irq_11:
112
	   push eax
113
	   mov eax, 11
114
	   jmp .main
115
align 4
116
.irq_12:
117
	   push eax
118
	   mov eax, 12
119
	   jmp .main
120
align 4
121
.irq_13:
122
	   push eax
123
	   mov eax, 13
124
	   jmp .main
125
align 4
126
.irq_14:
127
	   push eax
128
	   mov eax, 14
129
	   jmp .main
130
align 4
131
.irq_15:
132
	   push eax
133
	   mov eax, 15
134
	   jmp .main
135
 
136
align 16
137
.main:
138
	   save_ring3_context
672 hidnplayr 139
	   mov	 bx, app_data  ;os_data
164 serge 140
	   mov	 ds, bx
141
	   mov	 es, bx
142
 
709 diamond 143
           cmp   [v86_irqhooks+eax*8], 0
144
           jnz   v86_irq
145
 
164 serge 146
	   mov ebx, [irq_tab+eax*4]
147
	   test ebx, ebx
148
	   jz .exit
149
 
672 hidnplayr 150
	   call ebx
151
	   mov	[check_idle_semaphore],5
164 serge 152
 
153
.exit:
154
	   restore_ring3_context
155
 
672 hidnplayr 156
	   cmp eax, 8
164 serge 157
	   mov al, 0x20
672 hidnplayr 158
	   jb @f
164 serge 159
	   out 0xa0, al
160
@@:
672 hidnplayr 161
	   out 0x20, al
164 serge 162
 
672 hidnplayr 163
	   pop eax
164 serge 164
	   iret
165
 
166
align 4
167
proc get_notify stdcall, p_ev:dword
168
 
169
.wait:
672 hidnplayr 170
	   mov ebx,[current_slot]
171
	   test dword [ebx+APPDATA.event_mask],EVENT_NOTIFY
164 serge 172
	   jz @f
672 hidnplayr 173
	   and dword [ebx+APPDATA.event_mask], not EVENT_NOTIFY
164 serge 174
	   mov edi, [p_ev]
175
	   mov dword [edi], EV_INTR
672 hidnplayr 176
	   mov eax, [ebx+APPDATA.event]
164 serge 177
	   mov dword [edi+4], eax
178
	   ret
179
@@:
180
	   call change_task
181
	   jmp .wait
182
endp
183
 
184
align 4
185
proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword
186
	   xor eax, eax
187
	   xor ebx, ebx
188
	   mov ah, byte [bus]
672 hidnplayr 189
	   mov al, 6
164 serge 190
	   mov bh, byte [devfn]
191
	   mov bl, byte [reg]
192
	   call pci_read_reg
193
	   ret
194
endp
195
 
196
align 4
557 serge 197
proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword
198
	   xor eax, eax
199
	   xor ebx, ebx
200
	   mov ah, byte [bus]
672 hidnplayr 201
	   mov al, 5
557 serge 202
	   mov bh, byte [devfn]
203
	   mov bl, byte [reg]
204
	   call pci_read_reg
205
	   ret
206
endp
207
 
208
align 4
164 serge 209
proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword
210
	   xor eax, eax
211
	   xor ebx, ebx
212
	   mov ah, byte [bus]
672 hidnplayr 213
	   mov al, 4
164 serge 214
	   mov bh, byte [devfn]
215
	   mov bl, byte [reg]
216
	   call pci_read_reg
217
	   ret
218
endp
219
 
220
align 4
221
proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
222
	   xor eax, eax
223
	   xor ebx, ebx
224
	   mov ah, byte [bus]
672 hidnplayr 225
	   mov al, 8
164 serge 226
	   mov bh, byte [devfn]
227
	   mov bl, byte [reg]
672 hidnplayr 228
	   mov ecx, [val]
229
	   call pci_write_reg
164 serge 230
	   ret
231
endp
232
 
557 serge 233
align 4
234
proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
235
	   xor eax, eax
236
	   xor ebx, ebx
237
	   mov ah, byte [bus]
672 hidnplayr 238
	   mov al, 9
557 serge 239
	   mov bh, byte [devfn]
240
	   mov bl, byte [reg]
672 hidnplayr 241
	   mov ecx, [val]
242
	   call pci_write_reg
557 serge 243
	   ret
244
endp
245
 
672 hidnplayr 246
align 4
247
proc pci_write32 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
248
	   xor eax, eax
249
	   xor ebx, ebx
250
	   mov ah, byte [bus]
251
	   mov al, 10
252
	   mov bh, byte [devfn]
253
	   mov bl, byte [reg]
254
	   mov ecx, [val]
255
	   call pci_write_reg
256
	   ret
257
endp
164 serge 258
 
672 hidnplayr 259
handle	   equ	IOCTL.handle
260
io_code    equ	IOCTL.io_code
261
input	   equ	IOCTL.input
262
inp_size   equ	IOCTL.inp_size
263
output	   equ	IOCTL.output
264
out_size   equ	IOCTL.out_size
164 serge 265
 
672 hidnplayr 266
 
164 serge 267
align 4
268
proc srv_handler stdcall, ioctl:dword
672 hidnplayr 269
	   mov esi, [ioctl]
270
	   test esi, esi
271
	   jz .err
164 serge 272
 
672 hidnplayr 273
	   mov edi, [esi+handle]
274
	   cmp [edi+SRV.magic], ' SRV'
164 serge 275
	   jne .fail
276
 
672 hidnplayr 277
	   cmp [edi+SRV.size], SRV_SIZE
164 serge 278
	   jne .fail
279
 
672 hidnplayr 280
	   stdcall [edi+SRV.srv_proc], esi
281
	   ret
164 serge 282
.fail:
672 hidnplayr 283
	   xor eax, eax
284
	   not eax
285
	   mov [esi+output], eax
286
	   mov [esi+out_size], 4
287
	   ret
164 serge 288
.err:
672 hidnplayr 289
	   xor eax, eax
290
	   not eax
291
	   ret
164 serge 292
endp
293
 
377 serge 294
; param
295
;  ebx= io_control
296
;
297
; retval
298
;  eax= error code
299
 
164 serge 300
align 4
377 serge 301
srv_handlerEx:
672 hidnplayr 302
	   cmp ebx, OS_BASE
303
	   jae .fail
164 serge 304
 
672 hidnplayr 305
	   mov eax, [ebx+handle]
306
	   cmp [eax+SRV.magic], ' SRV'
164 serge 307
	   jne .fail
308
 
672 hidnplayr 309
	   cmp [eax+SRV.size], SRV_SIZE
164 serge 310
	   jne .fail
311
 
672 hidnplayr 312
	   stdcall [eax+SRV.srv_proc], ebx
313
	   ret
164 serge 314
.fail:
672 hidnplayr 315
	   or eax, -1
316
	   ret
164 serge 317
 
318
restore  handle
319
restore  io_code
320
restore  input
321
restore  inp_size
322
restore  output
323
restore  out_size
324
 
325
align 4
326
proc get_service stdcall, sz_name:dword
672 hidnplayr 327
	   mov eax, [sz_name]
328
	   test eax, eax
329
	   jnz @F
330
	   ret
164 serge 331
@@:
672 hidnplayr 332
	   mov edx, [srv.fd]
188 serge 333
@@:
672 hidnplayr 334
	   cmp edx, srv.fd-SRV_FD_OFFSET
335
	   je .not_load
278 serge 336
 
672 hidnplayr 337
	   stdcall strncmp, edx, [sz_name], 16
338
	   test eax, eax
339
	   je .ok
164 serge 340
 
672 hidnplayr 341
	   mov edx, [edx+SRV.fd]
342
	   jmp @B
164 serge 343
.not_load:
672 hidnplayr 344
	   pop ebp
345
	   jmp load_driver
164 serge 346
.ok:
672 hidnplayr 347
	   mov eax, edx
348
	   ret
164 serge 349
endp
350
 
351
align 4
278 serge 352
reg_service:
353
.sz_name equ esp+4
354
.handler equ esp+8
672 hidnplayr 355
	   mov eax, [.sz_name]
164 serge 356
	   test eax, eax
357
	   jz .fail
358
 
672 hidnplayr 359
	   mov ebx, [.handler]
164 serge 360
	   test ebx, ebx
361
	   jz .fail
362
 
672 hidnplayr 363
	   mov eax, SRV_SIZE
364
	   call malloc		 ;call alloc_service
164 serge 365
	   test eax, eax
366
	   jz .fail
367
 
368
	   mov edi, eax
672 hidnplayr 369
	   mov esi, [.sz_name]
370
	   mov ecx, 16/4
371
	   rep movsd
164 serge 372
 
672 hidnplayr 373
	   mov [eax+SRV.magic], ' SRV'
374
	   mov [eax+SRV.size], SRV_SIZE
278 serge 375
 
672 hidnplayr 376
	   mov ebx, srv.fd-SRV_FD_OFFSET
377
	   mov edx, [ebx+SRV.fd]
378
	   mov [eax+SRV.fd], edx
379
	   mov [eax+SRV.bk], ebx
380
	   mov [ebx+SRV.fd], eax
381
	   mov [edx+SRV.bk], eax
278 serge 382
 
672 hidnplayr 383
	   mov ecx, [.handler]
384
	   mov [eax+SRV.srv_proc], ecx
385
	   ret 8
164 serge 386
.fail:
387
	   xor eax, eax
672 hidnplayr 388
	   ret 8
164 serge 389
 
390
align 4
391
proc get_proc stdcall, exp:dword, sz_name:dword
392
 
672 hidnplayr 393
	   mov edx, [exp]
164 serge 394
.next:
672 hidnplayr 395
	   mov eax, [edx]
396
	   test eax, eax
397
	   jz .end
164 serge 398
 
672 hidnplayr 399
	   push edx
400
	   stdcall strncmp, eax, [sz_name], 16
401
	   pop edx
402
	   test eax, eax
403
	   jz .ok
164 serge 404
 
672 hidnplayr 405
	   add edx,8
406
	   jmp .next
164 serge 407
.ok:
672 hidnplayr 408
	   mov eax, [edx+4]
164 serge 409
.end:
672 hidnplayr 410
	   ret
164 serge 411
endp
412
 
413
align 4
414
proc get_coff_sym stdcall, pSym:dword,count:dword, sz_sym:dword
415
 
416
@@:
417
	   stdcall strncmp, [pSym], [sz_sym], 8
418
	   test eax,eax
419
	   jz .ok
420
	   add [pSym], 18
421
	   dec [count]
422
	   jnz @b
423
	   xor eax, eax
424
	   ret
425
.ok:
426
	   mov ebx, [pSym]
427
	   mov eax, [ebx+8]
428
	   ret
429
endp
430
 
431
align 4
188 serge 432
proc get_curr_task
672 hidnplayr 433
	   mov eax,[CURRENT_TASK]
434
	   shl eax, 8
435
	   ret
188 serge 436
endp
164 serge 437
 
188 serge 438
align 4
439
proc get_fileinfo stdcall, file_name:dword, info:dword
672 hidnplayr 440
	   locals
441
	     cmd     dd ?
442
	     offset  dd ?
443
		     dd ?
444
	     count   dd ?
445
	     buff    dd ?
446
		     db ?
447
	     name    dd ?
448
	   endl
164 serge 449
 
672 hidnplayr 450
	   xor eax, eax
451
	   mov ebx, [file_name]
452
	   mov ecx, [info]
164 serge 453
 
672 hidnplayr 454
	   mov [cmd], 5
455
	   mov [offset], eax
456
	   mov [offset+4], eax
457
	   mov [count], eax
458
	   mov [buff], ecx
459
	   mov byte [buff+4], al
460
	   mov [name], ebx
164 serge 461
 
672 hidnplayr 462
	   mov eax, 70
463
	   lea ebx, [cmd]
464
	   int 0x40
465
	   ret
188 serge 466
endp
164 serge 467
 
188 serge 468
align 4
469
proc read_file stdcall,file_name:dword, buffer:dword, off:dword,\
672 hidnplayr 470
				     bytes:dword
471
	   locals
472
	     cmd     dd ?
473
	     offset  dd ?
474
		     dd ?
475
	     count   dd ?
476
	     buff    dd ?
477
		     db ?
478
	     name    dd ?
479
	   endl
164 serge 480
 
672 hidnplayr 481
	   xor eax, eax
482
	   mov ebx, [file_name]
483
	   mov ecx, [off]
484
	   mov edx, [bytes]
485
	   mov esi, [buffer]
164 serge 486
 
672 hidnplayr 487
	   mov [cmd], eax
488
	   mov [offset], ecx
489
	   mov [offset+4], eax
490
	   mov [count], edx
491
	   mov [buff], esi
492
	   mov byte [buff+4], al
493
	   mov [name], ebx
188 serge 494
 
672 hidnplayr 495
	   pushad
496
	   push eax
497
	   lea eax, [cmd]
498
	   call file_system_lfn
499
	   pop eax
500
	   popad
501
	   ret
188 serge 502
endp
503
 
363 serge 504
; description
505
;  allocate kernel memory and loads the specified file
506
;
507
; param
508
;  file_name= full path to file
509
;
510
; retval
511
;  eax= file image in kernel memory
512
;  ebx= size of file
513
;
514
; warging
515
;  You mast call kernel_free() to delete each file
516
;  loaded by the load_file() function
517
 
188 serge 518
align 4
519
proc load_file stdcall, file_name:dword
672 hidnplayr 520
	   locals
521
	     attr	dd ?
522
	     flags	dd ?
523
	     cr_time	dd ?
524
	     cr_date	dd ?
525
	     acc_time	dd ?
526
	     acc_date	dd ?
527
	     mod_time	dd ?
528
	     mod_date	dd ?
529
	     file_size	dd ?
188 serge 530
 
672 hidnplayr 531
	     file	dd ?
532
	     file2	dd ?
533
	   endl
188 serge 534
 
672 hidnplayr 535
	   push esi
536
	   push edi
662 serge 537
 
672 hidnplayr 538
	   lea eax, [attr]
539
	   stdcall get_fileinfo, [file_name], eax
540
	   test eax, eax
541
	   jnz .fail
164 serge 542
 
672 hidnplayr 543
	   mov eax, [file_size]
544
	   cmp eax, 1024*1024*16
545
	   ja .fail
206 serge 546
 
672 hidnplayr 547
	   stdcall kernel_alloc, [file_size]
548
	   mov [file], eax
164 serge 549
 
672 hidnplayr 550
	   stdcall read_file, [file_name], eax, dword 0, [file_size]
551
	   cmp ebx, [file_size]
552
	   jne .cleanup
211 serge 553
 
672 hidnplayr 554
	   mov eax, [file]
555
	   cmp dword [eax], 0x4B43504B
556
	   jne .exit
557
	   mov ebx, [eax+4]
558
	   mov [file_size], ebx
559
	   stdcall kernel_alloc, ebx
211 serge 560
 
672 hidnplayr 561
	   test eax, eax
562
	   jz .cleanup
211 serge 563
 
672 hidnplayr 564
	   mov [file2], eax
565
	   stdcall unpack, [file], eax
566
	   stdcall kernel_free, [file]
567
	   mov eax, [file2]
568
	   mov ebx, [file_size]
211 serge 569
.exit:
672 hidnplayr 570
	   push eax
571
	   lea edi, [eax+ebx]	  ;cleanup remain space
572
	   mov ecx, 4096	  ;from file end
573
	   and ebx, 4095
574
	   jz  @f
575
	   sub ecx, ebx
576
	   xor eax, eax
577
	   cld
578
	   rep stosb
521 diamond 579
@@:
672 hidnplayr 580
	   mov ebx, [file_size]
581
	   pop eax
582
	   pop edi
583
	   pop esi
584
	   ret
188 serge 585
.cleanup:
672 hidnplayr 586
	   stdcall kernel_free, [file]
188 serge 587
.fail:
672 hidnplayr 588
	   xor eax, eax
589
	   xor ebx, ebx
590
	   pop edi
591
	   pop esi
592
	   ret
188 serge 593
endp
164 serge 594
 
188 serge 595
align 4
596
proc get_proc_ex stdcall, proc_name:dword, imports:dword
597
 
598
.look_up:
672 hidnplayr 599
	   mov edx, [imports]
600
	   test edx, edx
601
	   jz .end
602
	   mov edx, [edx]
603
	   test edx, edx
604
	   jz .end
188 serge 605
.next:
672 hidnplayr 606
	   mov eax, [edx]
607
	   test eax, eax
608
	   jz .next_table
164 serge 609
 
672 hidnplayr 610
	   push edx
611
	   stdcall strncmp, eax, [proc_name], 16
612
	   pop edx
613
	   test eax, eax
614
	   jz .ok
164 serge 615
 
672 hidnplayr 616
	   add edx,8
617
	   jmp .next
188 serge 618
.next_table:
672 hidnplayr 619
	   add [imports], 4
620
	   jmp .look_up
188 serge 621
.ok:
672 hidnplayr 622
	   mov eax, [edx+4]
623
	   ret
188 serge 624
.end:
672 hidnplayr 625
	   xor eax, eax
626
	   ret
188 serge 627
endp
164 serge 628
 
188 serge 629
align 4
630
proc fix_coff_symbols stdcall, sec:dword, symbols:dword,\
672 hidnplayr 631
		      sym_count:dword, strings:dword, imports:dword
632
	   locals
633
	     retval dd ?
634
	   endl
164 serge 635
 
672 hidnplayr 636
	   mov edi, [symbols]
637
	   mov [retval], 1
188 serge 638
.fix:
672 hidnplayr 639
	   movzx ebx, [edi+CSYM.SectionNumber]
640
	   test ebx, ebx
641
	   jnz .internal
642
	   mov eax, dword [edi+CSYM.Name]
643
	   test eax, eax
644
	   jnz @F
164 serge 645
 
672 hidnplayr 646
	   mov edi, [edi+4]
647
	   add edi, [strings]
188 serge 648
@@:
672 hidnplayr 649
	   push edi
650
	   stdcall get_proc_ex, edi,[imports]
651
	   pop edi
164 serge 652
 
672 hidnplayr 653
	   xor ebx, ebx
654
	   test eax, eax
655
	   jnz @F
164 serge 656
 
672 hidnplayr 657
	   mov esi, msg_unresolved
658
	   call sys_msg_board_str
659
	   mov esi, edi
660
	   call sys_msg_board_str
661
	   mov esi, msg_CR
662
	   call sys_msg_board_str
164 serge 663
 
672 hidnplayr 664
	   mov [retval],0
188 serge 665
@@:
672 hidnplayr 666
	   mov edi, [symbols]
667
	   mov [edi+CSYM.Value], eax
668
	   jmp .next
188 serge 669
.internal:
672 hidnplayr 670
	   cmp bx, -1
671
	   je .next
672
	   cmp bx, -2
673
	   je .next
541 serge 674
 
672 hidnplayr 675
	   dec ebx
676
	   shl ebx, 3
677
	   lea ebx, [ebx+ebx*4]
678
	   add ebx, [sec]
188 serge 679
 
672 hidnplayr 680
	   mov eax, [ebx+CFS.VirtualAddress]
681
	   add [edi+CSYM.Value], eax
188 serge 682
.next:
672 hidnplayr 683
	   add edi, CSYM_SIZE
684
	   mov [symbols], edi
685
	   dec [sym_count]
686
	   jnz .fix
687
	   mov eax, [retval]
688
	   ret
164 serge 689
endp
690
 
691
align 4
188 serge 692
proc fix_coff_relocs stdcall, coff:dword, sec:dword, sym:dword
164 serge 693
	   locals
672 hidnplayr 694
	     n_sec     dd ?
164 serge 695
	   endl
696
 
672 hidnplayr 697
	   mov eax, [coff]
698
	   movzx ebx, [eax+CFH.nSections]
699
	   mov [n_sec], ebx
188 serge 700
.fix_sec:
672 hidnplayr 701
	   mov esi, [sec]
164 serge 702
	   mov edi, [esi+CFS.PtrReloc]
672 hidnplayr 703
	   add edi, [coff]
164 serge 704
 
672 hidnplayr 705
	   movzx ecx, [esi+CFS.NumReloc]
706
	   test ecx, ecx
707
	   jz .next
188 serge 708
.next_reloc:
164 serge 709
	   mov ebx, [edi+CRELOC.SymIndex]
710
	   add ebx,ebx
711
	   lea ebx,[ebx+ebx*8]
672 hidnplayr 712
	   add ebx, [sym]
164 serge 713
 
672 hidnplayr 714
	   mov edx, [ebx+CSYM.Value]
164 serge 715
 
672 hidnplayr 716
	   cmp [edi+CRELOC.Type], 6
717
	   je .dir_32
164 serge 718
 
672 hidnplayr 719
	   cmp [edi+CRELOC.Type], 20
720
	   jne .next_reloc
188 serge 721
.rel_32:
164 serge 722
	   mov eax, [edi+CRELOC.VirtualAddress]
672 hidnplayr 723
	   add eax, [esi+CFS.VirtualAddress]
724
	   sub edx, eax
725
	   sub edx, 4
726
	   jmp .fix
188 serge 727
.dir_32:
728
	   mov eax, [edi+CRELOC.VirtualAddress]
672 hidnplayr 729
	   add eax, [esi+CFS.VirtualAddress]
188 serge 730
.fix:
672 hidnplayr 731
	   add [eax], edx
732
	   add edi, 10
733
	   dec ecx
734
	   jnz .next_reloc
188 serge 735
.next:
672 hidnplayr 736
	   add [sec], COFF_SECTION_SIZE
737
	   dec [n_sec]
738
	   jnz .fix_sec
164 serge 739
.exit:
740
	   ret
741
endp
742
 
188 serge 743
align 4
346 diamond 744
proc load_driver stdcall, driver_name:dword
672 hidnplayr 745
	   locals
746
	     coff      dd ?
747
	     sym       dd ?
748
	     strings   dd ?
749
	     img_size  dd ?
750
	     img_base  dd ?
751
	     start     dd ?
188 serge 752
 
672 hidnplayr 753
	     exports   dd ?   ;fake exports table
754
		       dd ?
755
	     file_name rb 13+16+4+1	 ; '/sys/drivers/.obj'
756
	   endl
188 serge 757
 
672 hidnplayr 758
	   lea	   edx, [file_name]
759
	   mov	   dword [edx], '/sys'
760
	   mov	   dword [edx+4], '/dri'
761
	   mov	   dword [edx+8], 'vers'
762
	   mov	   byte [edx+12], '/'
763
	   mov	   esi, [driver_name]
764
	   lea	   edi, [edx+13]
765
	   mov	   ecx, 16
346 diamond 766
@@:
672 hidnplayr 767
	   lodsb
768
	   test    al, al
769
	   jz	   @f
770
	   stosb
771
	   loop    @b
346 diamond 772
@@:
672 hidnplayr 773
	   mov	   dword [edi], '.obj'
774
	   mov	   byte [edi+4], 0
775
	   stdcall load_file, edx
214 serge 776
 
672 hidnplayr 777
	   test eax, eax
778
	   jz .exit
188 serge 779
 
672 hidnplayr 780
	   mov [coff], eax
188 serge 781
 
672 hidnplayr 782
	   movzx ecx, [eax+CFH.nSections]
783
	   xor ebx, ebx
188 serge 784
 
672 hidnplayr 785
	   lea edx, [eax+20]
188 serge 786
@@:
672 hidnplayr 787
	   add ebx, [edx+CFS.SizeOfRawData]
788
	   add ebx, 15
789
	   and ebx, not 15
790
	   add edx, COFF_SECTION_SIZE
791
	   dec ecx
792
	   jnz @B
793
	   mov [img_size], ebx
188 serge 794
 
672 hidnplayr 795
	   stdcall kernel_alloc, ebx
796
	   test eax, eax
797
	   jz .fail
798
	   mov [img_base], eax
188 serge 799
 
672 hidnplayr 800
	   mov edi, eax
801
	   xor eax, eax
802
	   mov ecx, [img_size]
803
	   add ecx, 4095
804
	   and ecx, not 4095
805
	   shr ecx, 2
806
	   cld
807
	   rep stosd
188 serge 808
 
672 hidnplayr 809
	   mov edx, [coff]
810
	   movzx ebx, [edx+CFH.nSections]
811
	   mov edi, [img_base]
812
	   lea eax, [edx+20]
188 serge 813
@@:
672 hidnplayr 814
	   mov [eax+CFS.VirtualAddress], edi
815
	   mov esi, [eax+CFS.PtrRawData]
816
	   test esi, esi
817
	   jnz .copy
818
	   add edi, [eax+CFS.SizeOfRawData]
819
	   jmp .next
188 serge 820
.copy:
672 hidnplayr 821
	   add esi, edx
822
	   mov ecx, [eax+CFS.SizeOfRawData]
823
	   cld
824
	   rep movsb
188 serge 825
.next:
672 hidnplayr 826
	   add edi, 15
827
	   and edi, not 15
828
	   add eax, COFF_SECTION_SIZE
829
	   dec ebx
830
	   jnz @B
188 serge 831
 
672 hidnplayr 832
	   mov ebx, [edx+CFH.pSymTable]
833
	   add ebx, edx
834
	   mov [sym], ebx
835
	   mov ecx, [edx+CFH.nSymbols]
836
	   add ecx,ecx
837
	   lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE
838
	   add ecx, [sym]
839
	   mov [strings], ecx
188 serge 840
 
672 hidnplayr 841
	   lea ebx, [exports]
842
	   mov dword [ebx], kernel_export
843
	   mov dword [ebx+4], 0
844
	   lea eax, [edx+20]
188 serge 845
 
672 hidnplayr 846
	   stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\
847
				     [strings], ebx
848
	   test eax, eax
849
	   jz .link_fail
188 serge 850
 
672 hidnplayr 851
	   mov ebx, [coff]
852
	   add ebx, 20
853
	   stdcall fix_coff_relocs, [coff], ebx, [sym]
188 serge 854
 
672 hidnplayr 855
	   mov ebx, [coff]
856
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szVersion
857
	   test eax, eax
858
	   jz .link_fail
227 serge 859
 
672 hidnplayr 860
	   mov eax, [eax]
861
	   shr eax, 16
862
	   cmp eax, DRV_COMPAT
863
	   jb .ver_fail
227 serge 864
 
672 hidnplayr 865
	   cmp eax, DRV_CURRENT
866
	   ja .ver_fail
227 serge 867
 
672 hidnplayr 868
	   mov ebx, [coff]
869
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szSTART
870
	   mov [start], eax
188 serge 871
 
672 hidnplayr 872
	   stdcall kernel_free, [coff]
188 serge 873
 
672 hidnplayr 874
	   mov ebx, [start]
875
	   stdcall ebx, DRV_ENTRY
876
	   test eax, eax
877
	   jnz .ok
188 serge 878
 
672 hidnplayr 879
	   stdcall kernel_free, [img_base]
880
	   xor eax, eax
881
	   ret
188 serge 882
.ok:
672 hidnplayr 883
	   mov ebx, [img_base]
884
	   mov [eax+SRV.base], ebx
885
	   mov ecx, [start]
886
	   mov [eax+SRV.entry], ecx
887
	   ret
227 serge 888
 
889
.ver_fail:
672 hidnplayr 890
	   mov esi, msg_CR
891
	   call sys_msg_board_str
892
	   mov esi, [driver_name]
893
	   call sys_msg_board_str
894
	   mov esi, msg_CR
895
	   call sys_msg_board_str
896
	   mov esi, msg_version
897
	   call sys_msg_board_str
898
	   mov esi, msg_www
899
	   call sys_msg_board_str
900
	   jmp .cleanup
227 serge 901
 
902
.link_fail:
672 hidnplayr 903
	   mov esi, msg_module
904
	   call sys_msg_board_str
905
	   mov esi, [driver_name]
906
	   call sys_msg_board_str
907
	   mov esi, msg_CR
908
	   call sys_msg_board_str
227 serge 909
.cleanup:
672 hidnplayr 910
	   stdcall kernel_free,[img_base]
188 serge 911
.fail:
672 hidnplayr 912
	   stdcall kernel_free, [coff]
227 serge 913
.exit:
672 hidnplayr 914
	   xor eax, eax
915
	   ret
164 serge 916
endp
917
 
198 serge 918
align 4
919
proc load_library stdcall, file_name:dword
672 hidnplayr 920
	   locals
921
	     coff      dd ?
922
	     sym       dd ?
923
	     strings   dd ?
924
	     img_size  dd ?
925
	     img_base  dd ?
926
	     exports   dd ?
927
	   endl
198 serge 928
 
672 hidnplayr 929
	   cli
198 serge 930
 
672 hidnplayr 931
	   stdcall load_file, [file_name]
932
	   test eax, eax
933
	   jz .fail
198 serge 934
 
672 hidnplayr 935
	   mov [coff], eax
936
	   movzx ecx, [eax+CFH.nSections]
937
	   xor ebx, ebx
198 serge 938
 
672 hidnplayr 939
	   lea edx, [eax+20]
198 serge 940
@@:
672 hidnplayr 941
	   add ebx, [edx+CFS.SizeOfRawData]
942
	   add ebx, 15
943
	   and ebx, not 15
944
	   add edx, COFF_SECTION_SIZE
945
	   dec ecx
946
	   jnz @B
947
	   mov [img_size], ebx
198 serge 948
 
672 hidnplayr 949
	   call init_heap
950
	   stdcall user_alloc, [img_size]
198 serge 951
 
672 hidnplayr 952
	   test eax, eax
953
	   jz .fail
954
	   mov [img_base], eax
198 serge 955
 
672 hidnplayr 956
	   mov edx, [coff]
957
	   movzx ebx, [edx+CFH.nSections]
958
	   mov edi, [img_base]
959
	   lea eax, [edx+20]
198 serge 960
@@:
672 hidnplayr 961
	   mov [eax+CFS.VirtualAddress], edi
962
	   mov esi, [eax+CFS.PtrRawData]
963
	   test esi, esi
964
	   jnz .copy
965
	   add edi, [eax+CFS.SizeOfRawData]
966
	   jmp .next
198 serge 967
.copy:
672 hidnplayr 968
	   add esi, edx
465 serge 969
    ;       add edi, new_app_base
672 hidnplayr 970
	   mov ecx, [eax+CFS.SizeOfRawData]
971
	   cld
972
	   rep movsb
198 serge 973
.next:
672 hidnplayr 974
	   add edi, 15 ;-new_app_base
975
	   and edi, -16
976
	   add eax, COFF_SECTION_SIZE
977
	   dec ebx
978
	   jnz @B
198 serge 979
 
672 hidnplayr 980
	   mov ebx, [edx+CFH.pSymTable]
981
	   add ebx, edx
982
	   mov [sym], ebx
983
	   mov ecx, [edx+CFH.nSymbols]
984
	   add ecx,ecx
985
	   lea ecx,[ecx+ecx*8] ;ecx*=18 = nSymbols*CSYM_SIZE
986
	   add ecx, [sym]
987
	   mov [strings], ecx
198 serge 988
 
672 hidnplayr 989
	   lea eax, [edx+20]
198 serge 990
 
672 hidnplayr 991
	   stdcall fix_coff_symbols, eax, [sym], [edx+CFH.nSymbols],\
992
				     [strings], dword 0
993
	   test eax, eax
994
	   jnz @F
198 serge 995
 
996
@@:
672 hidnplayr 997
	   mov edx, [coff]
998
	   movzx ebx, [edx+CFH.nSections]
999
	   mov edi, new_app_base
1000
	   lea eax, [edx+20]
198 serge 1001
@@:
672 hidnplayr 1002
	   add [eax+CFS.VirtualAddress], edi  ;patch user space offset
1003
	   add eax, COFF_SECTION_SIZE
1004
	   dec ebx
1005
	   jnz @B
198 serge 1006
 
672 hidnplayr 1007
	   add edx, 20
1008
	   stdcall fix_coff_relocs, [coff], edx, [sym]
198 serge 1009
 
672 hidnplayr 1010
	   mov ebx, [coff]
1011
	   stdcall get_coff_sym,[sym],[ebx+CFH.nSymbols],szEXPORTS
1012
	   mov [exports], eax
198 serge 1013
 
672 hidnplayr 1014
	   stdcall kernel_free, [coff]
1015
	   mov eax, [exports]
1016
	   ret
198 serge 1017
.fail:
672 hidnplayr 1018
	   xor eax, eax
1019
	   ret
198 serge 1020
endp
1021
 
214 serge 1022
align 4
1023
proc stop_all_services
278 serge 1024
 
672 hidnplayr 1025
	   mov edx, [srv.fd]
214 serge 1026
.next:
672 hidnplayr 1027
	   cmp edx,  srv.fd-SRV_FD_OFFSET
1028
	   je .done
1029
	   cmp [edx+SRV.magic], ' SRV'
1030
	   jne .next
1031
	   cmp [edx+SRV.size], SRV_SIZE
1032
	   jne .next
1033
	   mov ebx, [edx+SRV.entry]
1034
	   mov edx, [edx+SRV.fd]
1035
	   push edx
1036
	   stdcall ebx, dword -1
1037
	   pop edx
1038
	   jmp .next
278 serge 1039
.done:
672 hidnplayr 1040
	   ret
214 serge 1041
endp
198 serge 1042
 
281 serge 1043
; param
291 serge 1044
;  eax= size
1045
;  ebx= pid
214 serge 1046
 
281 serge 1047
align 4
1048
create_kernel_object:
1049
 
672 hidnplayr 1050
	   push ebx
1051
	   call malloc
1052
	   pop ebx
1053
	   test eax, eax
1054
	   jz .fail
281 serge 1055
 
672 hidnplayr 1056
	   mov ecx,[current_slot]
1057
	   add ecx, APP_OBJ_OFFSET
281 serge 1058
 
672 hidnplayr 1059
	   pushfd
1060
	   cli
1061
	   mov edx, [ecx+APPOBJ.fd]
1062
	   mov [eax+APPOBJ.fd], edx
1063
	   mov [eax+APPOBJ.bk], ecx
1064
	   mov [eax+APPOBJ.pid], ebx
281 serge 1065
 
672 hidnplayr 1066
	   mov [ecx+APPOBJ.fd], eax
1067
	   mov [edx+APPOBJ.bk], eax
1068
	   popfd
281 serge 1069
.fail:
672 hidnplayr 1070
	   ret
281 serge 1071
 
1072
; param
1073
;  eax= object
1074
 
1075
align 4
1076
destroy_kernel_object:
1077
 
672 hidnplayr 1078
	   pushfd
1079
	   cli
1080
	   mov ebx, [eax+APPOBJ.fd]
1081
	   mov ecx, [eax+APPOBJ.bk]
1082
	   mov [ebx+APPOBJ.bk], ecx
1083
	   mov [ecx+APPOBJ.fd], ebx
1084
	   popfd
281 serge 1085
 
672 hidnplayr 1086
	   xor edx, edx        ;clear common header
1087
	   mov [eax], edx
1088
	   mov [eax+4], edx
1089
	   mov [eax+8], edx
1090
	   mov [eax+12], edx
1091
	   mov [eax+16], edx
281 serge 1092
 
672 hidnplayr 1093
	   call free	       ;release object memory
1094
	   ret
281 serge 1095
 
1096
 
164 serge 1097
 
465 serge 1098
if 0
164 serge 1099
 
465 serge 1100
irq:
188 serge 1101
 
465 serge 1102
.irq0:
672 hidnplayr 1103
	   pusfd
1104
	   pushad
1105
	   push IRQ_0
1106
	   jmp .master
465 serge 1107
.irq_1:
672 hidnplayr 1108
	   pusfd
1109
	   pushad
1110
	   push IRQ_1
1111
	   jmp .master
465 serge 1112
 
1113
.master:
672 hidnplayr 1114
	   mov ax, app_data
1115
	   mov ds, eax
1116
	   mov es, eax
1117
	   mov ebx, [esp+4]  ;IRQ_xx
1118
	   mov eax, [irq_handlers+ebx+4]
1119
	   call intr_handler
1120
	   mov ecx, [esp+4]
1121
	   cmp [irq_actids+ecx*4], 0
1122
	   je @F
1123
	   in al, 0x21
1124
	   bts eax, ecx
1125
	   out 0x21, al
1126
	   mov al, 0x20
1127
	   out 0x20, al
1128
	   jmp .restart
465 serge 1129
 
1130
.slave:
672 hidnplayr 1131
	   mov ax, app_data
1132
	   mov ds, eax
1133
	   mov es, eax
1134
	   mov ebx, [esp+4]  ;IRQ_xx
1135
	   mov eax, [irq_handlers+ebx+4]
1136
	   call intr_handler
1137
	   mov ecx, [esp+4]
1138
	   sub ecx, 8
1139
	   cmp [irq_actids+ecx*4], 0
1140
	   je @F
1141
	   in al, 0xA1
1142
	   bts eax, ecx
1143
	   out 0xA1, al
1144
	   mov al, 0x20
1145
	   out 0xA0, al
1146
	   out 0x20, al
465 serge 1147
.restart:
672 hidnplayr 1148
	   mov ebx, [next_slot]
1149
	   test ebx, ebx
1150
	   jz @F
1151
	   mov [next_task],0
1152
	   mov esi, [prev_slot]
1153
	   call do_change_task
1154
	   add esp, 4
1155
	   iretd
465 serge 1156
 
1157
end if
1158