; Copyright (c) 2008-2009, diamond
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;       * Redistributions of source code must retain the above copyright
;       notice, this list of conditions and the following disclaimer.
;       * Redistributions in binary form must reproduce the above copyright
;       notice, this list of conditions and the following disclaimer in the
;       documentation and/or other materials provided with the distribution.
;       * Neither the name of the <organization> nor the
;       names of its contributors may be used to endorse or promote products
;       derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY Alexey Teplov aka <Lrz> ''AS IS'' AND ANY
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;*****************************************************************************

						    ,
						    Reset'...

  FAT-  NTFS-  ,    
Windows,      512 .

=====================================================================

  :
1)      .
2)   - 80386.
3)       592K   .
4)          NTFS
	(  ).
5)         
	(  NTFS,  FAT  ).

=====================================================================

   (    08.08.2008):
	  FAT: http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
		  PDF: http://staff.washington.edu/dittrich/misc/fatgen103.pdf
		 : http://wasm.ru/docs/11/fatgen103-rus.zip
	 NTFS: file://C:/windows/system32/drivers/ntfs.sys
		 file://C:/ntldr  file://C:/bootmgr
	  NTFS: http://sourceforge.net/project/showfiles.php?group_id=13956&package_id=16543
	   EDD BIOS 3.0: http://www.phoenix.com/NR/rdonlyres/19FEBD17-DB40-413C-A0B1-1F3F560E222F/0/specsedd30.pdf
		 ,  1.1: http://www.phoenix.com/NR/rdonlyres/9BEDED98-6B3F-4DAC-BBB7-FA89FA5C30F0/0/specsedd11.pdf
	  BIOS: Interrupt List by Ralf Brown: http://www.cs.cmu.edu/~ralf/files.html
	  Boot BIOS: http://www.phoenix.com/NR/rdonlyres/56E38DE2-3E6F-4743-835F-B4A53726ABED/0/specsbbs101.pdf
	  bcdedit  Vista: http://www.microsoft.com/whdc/system/platform/firmware/bcdedit_reff.mspx
	       Vista: http://www.microsoft.com/whdc/system/platform/firmware/bcd.mspx
	    : http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/prork/prcb_dis_qxql.mspx

=====================================================================

  :
	600-2000	  ( )
	2000-3000	
	3000-3200	 MBR
	3200-3400	  
	3400-3C00	     FAT16/FAT32:
			 FAT16 -   0x100 ,   
			0  1    ,  
				  FAT16;
			 FAT32 - 100h   8 : 4 
			(  -   )   L2-
			     
			   + 4   
			;      
			 ,   ,    
			  
	3400-3440	      NTFS 
			  ,     FAT32,   8 
	3480-34C0	     NTFS
	3500-3D00	     NTFS:  
			     
			 
	4000-8000	      NTFS
	60000-80000	 FAT12 /    FAT16 /
				   FAT32 /    NTFS
	80000-90000	  
	90000-92000	FAT:    
	92000-...	FAT:     (  
			2000h  = 100h ,   
			    7 ;
			    
			  -  , 
			 A0000  EBDA, Extended BIOS Data Area)

=====================================================================

  .
0a.  - DOS  Win9x:  kordldr.win 
	  install=c:\kordldr.win    config.sys;
	      kordldr.win  
	com-,  -    100h   
	   (xxxx:0100).
0.  - WinNT/2000/XP:  kordldr.win 
	   c:\kordldr.win="KordOS"  
	[operating systems]  boot.ini;     
	  8  (0x2000 )    3   'NTFS'
	(  kordldr.win   ),     
	   kordldr.win   0D00:0000  
	   0D00:0256.
0.  - Vista:  kordldr.win  
	      bcdedit    
	  kordldr.win;    
	kordldr.win   0000:7C00      .
1.   - DOS/9x    ,  
	      ,    
	kordldr.win   ,    
	 - ,  ,  
	.      
	    . (  
	NT-    ,   
	     .)   
	 KordOS   - DOS/9x 
	 .   kordldr ,  
	 0  0   ( 0  ,  
	    ):   ip ( call
	      call ,  pop si
	    si),     100h,  kordldr
	  com- - DOS/9x.    
	  (    kordldr  ,
	      DOS/9x). 
	    , kordldr .
	   ,     
	int 19h       BIOS
	,       int 19h  BIOS.  
	kordldr     ,
	      DOS
	.      
	  ,      int 19h, 
	        KordOS.
	  BIOS    ,
	 ,   int 8, , , 
	  jmp far   .    
	  .
2.       0000:0600.
3. ( real_entry)     ds = es = 0,
	  ss:sp = 0000:3000   bp , 
	      [bp+N]   N
	(         ds 
	   ).    , 
	  .     , 
	   (  ASCII- 2).
4.    ,   
	:   LBA ( 41h  13h),
	 LBA  ,    -  
	     ( 8  13h),  
	    .
5. ( new_partition_ex)      .
	  -       
	 (       
	  not_extended),     
	   ( next_partition),    
	.      partition_start,
	      ,
	    -      
	. cur_partition_ofs -   , 
	       . 
	        3000h.
	        .
	       4   .
	   ,    
	 ,   ,     
	     , 
	   .     
	,          
	 .    
	  ,       
	   ,   -   
	 .
	         
	  .      
	,    :   
	   ,   
	     
	.     ,   
	.    : extended_part_start -
	  ; extended_parent -  
	  ; extended_part_cur - 
	   .
	  :   ,   
	(  )  ;   
	(   )    not_extended, 
	 partition_start    
	( );    ,   
	  (5  0xF),     
	(        ,
	  -      ,
	           );
	     ,   , 
	  ,  ,       
	   .     , , 
	      ,  
	,       
	(jmp $).
	  ,       
	         
	   ,   .  , 
	        
	,       
	   (      ,
	        
	kordldr); ,     
	Windows-,     , 
	   Windows,  
	,         
	 .  kordldr   ,  
	   Windows- (   NT/2000/XP 
	    C:\).     
	      ,    
	,     : -,  
	,          C:\,  
	  ; -,  C:      
	 - Vista      
	,    C:   .
6.    ,      
	 .
7.         .
	  FAT,   NTFS    +11     
	      ,  
	200h .   FAT,   NTFS    +13  
	       .
	 NTFS:    +3   NTFS   
	 +16  ( FAT     FAT  
	 ).
	 FAT:    , 
	  (FAT12/FAT16/FAT32)     
	+38  FAT12/16, +66  FAT32 (    0x29).
	       
	 .     , 
	       .
8a.  FAT12-:       -
	 '12';      
	  FAT   FAT12-;    
	 FAT12 (   0x1800  = 6 ),  
	     FAT.
8.  FAT16-:       -
	 '16';      
	  FAT   FAT16-;  
	   FAT (     0  1,
	,       -  
	 FAT16   0x100 ) -     
	,   .
8.  FAT32-:       -
	 '32';      
	  FAT   FAT16-;  
	   FAT (   ,  
	  ) -      .
8.   FAT-:    
	root_start (     FAT12/16, 
	  FAT32-), data_start (   ,
	  ,   N   
	N*sectors_per_cluster+data_start), root_clus (  
	  FAT32, 0  FAT12/16);    
	   FAT-.
8.  NTFS-:       -
	 'nt';     frs_size
	(    , File Record Segment),  
	 ,    ( 0x400   
	 NTFS- -     
	     )   0x1000
	    0x200 ;   
	 -    ;    $MFT
	         $MFT
	( 0x80, $Data);     
	  NTFS-.
9. ( load_secondary)       
	.        
	 .
10.     : al='h' ( ),
	ah=  (   - 0 (BIOS- 80h),
	        
	 ), bx=   (
	 ,       8), ds:si= 
	callback-.
11.        1000:0000.

     :
	   .
       .
 :
1.        :
	ss:sp = 0:3000, bp=dat:  ss:bp    
	    0:dat.
2.        .
3.       .

 .
   (read):
    :
	ss:bp = 0:dat
	es:bx =    ,    
	eax =   (   )
	cx =   (   )
 : es:bx    ,     ,
	 CF ,    
1.    (   )   
	,      ,
	   .
2.   ( 3-6)  ,   ,    
	CHS-:       .
	LBA-:      7Fh (
	 EDD BIOS).
CHS-:
3.      CHS-:   
	         
	 ;       ,
	   ,   ,   - 
	    .      ,
	     ,    
	.
4.     int 13h (ah=2 - , al= ,
	dh=, ( 6  cl)=,
	( 2  cl   ch)=, dl=, es:bx->).
5.  BIOS.  BIOS   ,   
	   ,      
	(        , 
	 ).       ,
	       "Read error".
6.         
	  ,      
	 (  es:bx  es).   , 
	,     3.
LBA-:
3.       7Fh,   ( 
	)  7Fh.
4.      int 13h (    
	push,    :  -  LIFO,   
	        ,   
	).
5.  BIOS.  BIOS   ,    
	   "Read error".    ,
	   .
6.         
	  ,      
	 (  es:bx  es).   , 
	,     3.

   (find_error_si  find_error_sp):
 :       si    
0.   find_error_si,      .
1.       callback-, 
	( error_in_callback)    
	 ,    .
2.         ,
	    "Error: < >: <>"
	 ( )     .

  /     
	(read_file_chunk):
    :
	ds:si =     
	es:bx =    ,    
	ecx =     ,     0
 : es:bx    ,     ,
	 CF ,    
1. ,     (   NTFS
	 ,   /     
	   )   (,  
	 -  ,     ,  ).
2.    ( read_file_chunk.resident)  
	    (   ).
3.        < 
	   ,   >; 
	 ,         
	 .

   (cache_lookup):
    :
	eax =  
	ss:si =   - 
 : ss:di =     ;  CF ,  
	   ,  ,      .
1.      .   
	(   CF  ),    4.
2.    ,       (  
	  ),       .
3.      .  
	CF,      .    5.
4.    .
5.      (  ).
