Subversion Repositories Kolibri OS

Rev

Rev 8687 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. format ELF
  2. section '.text' executable
  3. public start
  4. public start as '_start'
  5. ;extrn mf_init
  6. extrn main
  7. include '../../../../../proc32.inc'
  8. include '../../../../../macros.inc'
  9. include '../../../../../dll.inc'
  10. ;include '../../../../../debug.inc'
  11.  
  12. ;start_:
  13. virtual at 0
  14.         db 'MENUET01' ; 1. Magic number (8 bytes)
  15.         dd 0x01       ; 2. Version of executable file
  16.         dd start       ; 3. Start address
  17. imgsz   dd 0x0        ; 4. Size of image
  18.         dd 0x100000   ; 5. Size of needed memory
  19.         dd 0x100000   ; 6. Pointer to stack
  20. hparams dd 0x0        ; 7. Pointer to program arguments
  21. hpath   dd 0x0        ; 8. Pointer to program path
  22. end virtual
  23.  
  24. start:
  25. ;DEBUGF 'Start programm\n'
  26.     ;init heap of memory
  27.     mov eax,68
  28.     mov ebx,11
  29.     int 0x40
  30.  
  31.     mov  [argc], 0
  32.     mov  eax, [hparams]
  33.     test eax, eax
  34.     jz   .without_path
  35.     mov  eax, path
  36.     cmp  word ptr eax, 32fh  ; '/#3'  UTF8
  37.     jne  .without_path
  38.     mov  word ptr eax, 12fh  ; '/#1'  fix to CP866
  39. .without_path:
  40.     mov  esi, eax
  41.     call push_param
  42. ; retrieving parameters
  43.     mov  esi, params
  44.     xor  edx, edx  ; dl - èä¸ò ïàðàìåòð(1) èëè ðàçäåëèòåëè(0)
  45.                    ; dh - ñèìâîë ñ êîòîðîãî íà÷àëñÿ ïàðàìåòð (1 êàâû÷êè, 0 îñòàëüíîå)
  46.     mov  ecx, 1    ; cl = 1
  47.                    ; ch = 0  ïðîñòî íîëü
  48. .parse:
  49.     lodsb
  50.     test al, al
  51.     jz   .run
  52.     test dl, dl
  53.     jnz  .findendparam
  54.                      ;{åñëè áûë ðàçäåëèòåëü
  55.     cmp  al, ' '
  56.     jz   .parse  ;çàãðóæåí ïðîáåë, ãðóçèì ñëåäóþùèé ñèìâîë
  57.     mov  dl, cl  ;íà÷èíàåòñÿ ïàðàìåòð
  58.     cmp  al, '"'
  59.     jz   @f      ;çàãðóæåíû êàâû÷êè
  60.     mov  dh, ch     ;ïàðàìåòð áåç êàâû÷åê
  61.     dec  esi
  62.     call push_param
  63.     inc  esi
  64.     jmp  .parse
  65.  
  66.   @@:  
  67.     mov  dh, cl     ;ïàðàìåòð â êàâû÷åêàõ
  68.     call push_param ;åñëè íå ïðîáåë çíà÷èò íà÷èíàåòñÿ êàêîé òî ïàðàìåòð
  69.     jmp  .parse     ;åñëè áûë ðàçäåëèòåëü}
  70.  
  71. .findendparam:
  72.     test dh, dh
  73.     jz   @f ; áåç êàâû÷åê
  74.     cmp  al, '"'
  75.     jz   .clear
  76.     jmp  .parse
  77.   @@:  
  78.     cmp  al, ' '
  79.     jnz  .parse
  80.  
  81. .clear:
  82.     lea  ebx, [esi - 1]
  83.     mov  [ebx], ch
  84.     mov  dl, ch
  85.     jmp  .parse
  86.  
  87. .run:
  88.     call load_imports
  89.     push argv
  90.     push [argc]
  91.     call main
  92. .exit:
  93.     xor  eax,eax
  94.     dec  eax
  95.     int  0x40
  96.     dd   -1
  97. .crash:
  98.     jmp  .exit
  99. ;============================
  100. push_param:
  101. ;============================
  102. ;parameters
  103. ;  esi - pointer
  104. ;description
  105. ;  procedure increase argc
  106. ;  and add pointer to array argv
  107. ;  procedure changes ebx
  108.     mov  ebx, [argc]
  109.     cmp  ebx, max_parameters
  110.     jae  .dont_add
  111.     mov  [argv+4*ebx], esi
  112.     inc  [argc]
  113. .dont_add:    
  114.     ret
  115. ;==============================
  116.  
  117. ;==============================
  118. load_imports:
  119. ;==============================
  120. ;parameters
  121. ;  none
  122. ;description
  123. ;  imports must be located at end of image (but before BSS sections)
  124. ;  the address of end of imports (next byte after imports) is located in imgsz
  125. ;  look at each import from that address up to illegal import
  126. ;  legal import is such that:
  127. ;    first pointer points to procedure name
  128. ;      and is smaller than imgsz
  129. ;    second pointer points lo library name, starting with 0x55, 0xAA
  130. ;      and is smaller than imgsz
  131. ;  each library should be initialized as appropriate, once
  132. ;  so as library is initialized, its name will be replaced 0x00
  133.     mov ebx, [imgsz]                ; byte after imports
  134. .handle_next_import:
  135.     sub ebx, 4                      ; ebx = pointer to pointer to library name
  136.     mov esi, dword[ebx]             ; esi = pointer to library name
  137.     push ebx
  138.     push esi
  139.     call load_library               ; eax = pointer to library exports
  140.     pop esi
  141.     pop ebx
  142.     test eax, eax
  143.     jz .done
  144.     sub ebx, 4                      ; ebx = pointer to pointer to symbol name
  145.     push ebx
  146.     stdcall dll.GetProcAddress, eax, dword[ebx]
  147.     pop ebx
  148.     test eax, eax
  149.     jz .fail
  150.     mov dword[ebx], eax
  151.     jmp .handle_next_import
  152. .done:
  153.     ;DEBUGF 1, "Library: %s not loaded!\n", esi
  154.     ;mcall -1
  155.     ret
  156. .fail:
  157.     ret
  158.  
  159. ;==============================
  160.  
  161. ;==============================
  162. load_library:
  163. ;==============================
  164. ;parameters
  165. ;  ebx: library name address
  166. ;description
  167. ;  each library should be initialized as appropriate, once
  168. ;  so as library is initialized, its name will be replaced 0x00
  169. ;  and 4 next bytes will be set to address of library
  170.     ; first two bytes of library name must be 0x55, 0xAA (is like a magic)
  171.     cld                ; move esi further, not back
  172.     cmp esi, [imgsz]
  173.     ja .fail
  174.     lodsb              ; al = first byte of library name
  175.     cmp al, 0x55
  176.     jne .fail
  177.     lodsb              ; al = second byte of library name
  178.     cmp al, 0xAA
  179.     jne .fail
  180.     lodsb              ; al = third byte of library name (0x00 if the library is already loaded)
  181.     test al, al
  182.     jnz .load
  183.     lodsd              ; if we here, then third byte is 0x00 => address of library is in next 4 bytes
  184.     ; now eax contains address of library
  185.     ret
  186. .load:
  187.     dec esi ; we checked on 0 before, let's go back
  188.     mov eax, 68
  189.     mov ebx, 19
  190.     mov ecx, esi
  191.     int 0x40           ; eax = address of exports
  192.     mov byte[esi], 0   ; library is loaded, let's place 0 in first byte of name
  193.     mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library
  194.     ; call lib_init
  195.     stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init
  196.     test eax, eax
  197.     jz .ret
  198.     stdcall dll.Init, eax
  199. .ret:
  200.     mov eax, [esi + 1] ; put address of library into eax
  201.     ret
  202. .fail:
  203.     mov eax, 0
  204.     ret
  205.  
  206. ;==============================
  207.  
  208. lib_init_str db 'lib_init', 0
  209.  
  210. public argc as '__argc'
  211. public params as '__argv'
  212. public path as '__path'
  213.  
  214. section '.bss'
  215. buf_len = 0x400
  216. max_parameters=0x20
  217. argc     rd 1
  218. argv     rd max_parameters
  219. path     rb buf_len
  220. params   rb buf_len
  221.  
  222.