Subversion Repositories Kolibri OS

Rev

Rev 732 | Rev 747 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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