Subversion Repositories Kolibri OS

Rev

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

  1. ; Kolibri kernel packer
  2. ; (C) copyright diamond 2006, 2007
  3. ;
  4. ;   This program is free software; you can redistribute it and/or modify
  5. ;   it under the terms of the GNU General Public License as published by
  6. ;   the Free Software Foundation; either version 2 of the License, or
  7. ;   (at your option) any later version.
  8. ;
  9. ;   This program is distributed in the hope that it will be useful,
  10. ;   but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;   GNU General Public License for more details.
  13.  
  14. ; Uses LZMA compression library by Igor Pavlov
  15. ; (for more information on LZMA and 7-Zip visit http://www.7-zip.org)
  16. ; (plain-C packer is ported by diamond)
  17.  
  18. format MS COFF
  19.  
  20. extrn '_lzma_compress@16' as lzma_compress
  21. extrn '_lzma_set_dict_size@4' as lzma_set_dict_size
  22.  
  23. section '.text' code executable readable
  24.  
  25. die_with_err:
  26.         pop     esi
  27. @@:
  28.         lodsb
  29.         test    al, al
  30.         jz      @f
  31.         mov     cl, al
  32.         push    63
  33.         pop     eax
  34.         push    1
  35.         pop     ebx
  36.         int     40h
  37.         jmp     @b
  38. @@:
  39.         mov     al, 63
  40.         mov     cl, 13
  41.         int     40h
  42.         mov     cl, 10
  43.         int     40h
  44.         or      eax, -1
  45.         int     40h
  46.  
  47. public _start
  48. _start:
  49.         push    70
  50.         pop     eax
  51.         mov     ebx, fn70_read
  52.         int     40h
  53.         cmp     eax, 6
  54.         jz      read_ok
  55. read_err:
  56.         call    die_with_err
  57.         db      'KerPack: cannot load kernel.mnt',0
  58. read_ok:
  59.         push    18
  60.         call    lzma_set_dict_size
  61. ; find jump to 32-bit code
  62.         mov     edi, infile - 1
  63. @@:
  64.         inc     edi
  65.         cmp     dword [edi], 0E88EE08Eh         ; mov fs,ax/mov gs,ax
  66.         jnz     @b
  67.         cmp     dword [edi+4], 00BCD08Eh      ; mov ss,ax/mov esp,00xxxxxx
  68.         jnz     @b
  69.         add     edi, 11
  70.         mov     [inptr], edi
  71.         sub     edi, infile
  72.         mov     [indelta], edi
  73.         lea     eax, [ebx+0x10000]
  74.         mov     [..loader_patch3+2], eax
  75.         sub     ebx, edi
  76.         mov     [insize], ebx
  77.         call    preprocess_calltrick2
  78.         mov     al, [cti]
  79.         mov     [loader_patch5-1], al
  80.         mov     eax, [ctn]
  81.         mov     [loader_patch4+1], eax
  82.         mov     eax, [inptr]
  83.         add     eax, outfile - infile + loader_size - 5
  84.         push    workmem
  85.         push    [insize]
  86.         push    eax
  87.         push    [inptr]
  88.         call    lzma_compress
  89.         add     eax, loader_size-5
  90.         mov     [loader_patch1+6], eax
  91.         add     eax, [indelta]
  92.         mov     [outsize], eax
  93.         mov     eax, [indelta]
  94.         mov     ecx, dword [eax + outfile + loader_size - 4]
  95.         bswap   ecx
  96.         mov     [loader_patch2+4], ecx
  97.         add     eax, 0x10000
  98.         mov     [loader_patch1+1], eax
  99.         mov     esi, infile
  100.         mov     edi, outfile
  101.         mov     ecx, [indelta]
  102.         rep     movsb
  103.         mov     esi, loader_start
  104.         mov     ecx, loader_size
  105.         rep     movsb
  106.         push    70
  107.         pop     eax
  108.         mov     ebx, fn70_write
  109.         int     40h
  110.         test    eax, eax
  111.         jz      @f
  112.         call    die_with_err
  113.         db      'KerPack: cannot save kernel.mnt',0
  114. @@:
  115.         call    die_with_err
  116.         db      'KerPack: all is OK',0
  117.  
  118. preprocess_calltrick2:
  119. ; input preprocessing
  120.         mov     edi, ct1
  121.         xor     eax, eax
  122.         push    edi
  123.         mov     ecx, 256/4
  124.         rep     stosd
  125.         pop     edi
  126.         mov     ecx, ebx
  127.         mov     esi, [inptr]
  128.         mov     ebx, inbuftmp
  129.         xchg    eax, edx
  130. input_pre2:
  131.         lodsb
  132. @@:
  133.         cmp     al, 0Fh
  134.         jnz     ip1
  135.         dec     ecx
  136.         jz      input_pre_done2
  137.         lodsb
  138.         cmp     al, 80h
  139.         jb      @b
  140.         cmp     al, 90h
  141.         jb      @f
  142. ip1:
  143.         sub     al, 0E8h
  144.         cmp     al, 1
  145.         ja      input_pre_cont2
  146. @@:
  147.         cmp     ecx, 5
  148.         jb      input_pre_done2
  149.         lodsd
  150.         add     eax, esi
  151.         sub     eax, [inptr]
  152.         cmp     eax, [insize]
  153.         jae     xxx2
  154.         cmp     eax, 1000000h
  155.         jae     xxx2
  156.         sub     ecx, 4
  157.         xchg    al, ah
  158.         rol     eax, 16
  159.         xchg    al, ah
  160.         mov     [esi-4], eax
  161.         inc     edx
  162.         mov     [ebx], esi
  163.         add     ebx, 4
  164.         jmp     input_pre_cont2
  165. xxx2:   sub     esi, 4
  166.         movzx   eax, byte [esi]
  167.         mov     byte [eax+edi], 1
  168. input_pre_cont2:
  169.         loop    input_pre2
  170. input_pre_done2:
  171.         mov     [ctn], edx
  172.         xor     eax, eax
  173.         mov     ecx, 256
  174.         repnz   scasb
  175.         jnz     pack_calltrick_done
  176.         not     cl
  177.         mov     [cti], cl
  178. @@:
  179.         cmp     ebx, inbuftmp
  180.         jz      pack_calltrick_done
  181.         sub     ebx, 4
  182.         mov     eax, [ebx]
  183.         mov     [eax-4], cl
  184.         jmp     @b
  185. pack_calltrick_done:
  186.         ret
  187.  
  188. include 'loader_lzma.asm'
  189.  
  190. section '.data' data readable writeable
  191.         db      'MENUET01'
  192.         dd      1
  193.         dd      _start
  194.         dd      bss_start       ; i_end
  195.         dd      bss_end         ; memory
  196.         dd      mtstack_end     ; esp
  197.         dd      0               ; params
  198.         dd      0               ; icon
  199. fn70_read:
  200.         dd      0
  201.         dd      0
  202.         dd      0
  203.         dd      200*1024
  204.         dd      infile
  205. filename db      '/rd/1/kernel.mnt',0
  206. fn70_write:
  207.         dd      2
  208.         dd      0
  209.         dd      0
  210. outsize dd      ?
  211.         dd      outfile
  212.         db      0
  213.         dd      filename
  214. section '.bss' readable writeable
  215. bss_start:
  216.         align   4
  217. inptr           dd      ?
  218. indelta         dd      ?
  219. insize          dd      ?
  220. ct1             rb      256
  221. ctn             dd      ?
  222. cti             db      ?
  223.  
  224. align 4
  225. mtstack         rb      1000h
  226. mtstack_end:
  227.  
  228. infile          rb      200*1024
  229. inbuftmp        rb      200*1024
  230. outfile         rb      200*1024
  231. workmem         rb      6A8000h
  232. bss_end:
  233.