; 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.
;*****************************************************************************

					   FAT.
					- ,  ?
					- ? .
					- A  AFT,   TAF,   FTA, ,  ... 

  FAT12/FAT16-      0x200 = 512 .

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

      ,    LBA,
    use_lba    .
  :
1)  ,   FAT    
  .
2)   - 80186.
3)       592K   .

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

   (      , 15.05.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
	   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

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

    FAT12- - 0xFF4 = 4084;  
 12    FAT,      
0x17EE = 6126 .     .
    FAT16- - 0xFFF4 = 65524; 
  16    FAT,      
0x1FFE8 = 131048 .      ,  
      , 
      .    
,      ,   
  .

  :
	...-7C00	
	7C00-7E00	 
	7E00-8200	   (kordldr.f1x)
	8200-8300	    FAT16
			(1 =   )
	60000-80000	  FAT12 /    FAT16
	80000-90000	    
	90000-92000	   
	92000-...	    (  
			2000h  = 100h ,   
			    7 ;
			    
			  -  , 
			 A0000  EBDA, Extended BIOS Data Area)

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

  .
  (start):    BIOS  ,  
	dl   ,    
1.   ss:sp = 0:7C00 (   
	),   ds = 0,   ss:bp  
	 (      [bp+N] -
	  ds     ).
2. LBA-: ,    LBA,   41h
	 13h.  ,      
	   LBA.
CHS-:      8  13h 
	    BPB.    ,
	    .
3.    FAT-:    
	   .    ;  
	        bp.
4.       9000:0000.  
	 -     ,   BPB,  16
	(     - 2000h  = 16 ).
5.      kordldr.f1x.   ,  
	  ,       -
	       
	 .
	:         
	   ,      FAT
	(8+3 - 8   , 3   ,   
	 ,      
	,   ,   ).
6.     kordldr.f1x   0:7E00  
	 .     dx:ax  
	   kordldr.f1x,   cx -   
	(  ).

  .
   (err):
1.      .
2.   "Press any key...".
3.   any key.
4.  int 18h,   BIOS   - .
5.   .

   (read_sectors  read_sectors2):
    :
	ss:bp = 0:7C00
	es:bx =    ,    
	dx:ax =   (   
		 read_sectors,     read_sectors2)
	cx =   (   )
 : es:bx    ,     
0.   read_sectors2,      
	     ,   
	 ,     [bp-8].
1.    (   )   
	,      BPB.
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.

         
	(scan_for_filename):
    :
	ds:si =       FAT (11 , 8  ,
		3  ,   ,  /
		,     )
	es =   
	cx =     
 : ZF ,      
	(ZF=1,     ,  
	 ); CF ,       
	(CF=1,   );  ,  es:di   .
scan_for_filename ,       es:0.
    di.       
 .

      (lookup_in_root_dir):
    :
	ss:bp = 0:7C00
	ds:si =       FAT (. )
 :  CF ,    ;  , 
	CF   es:di    
    ()   .  
	 ;     ,
	    ,     0x10000 = 64K
	 (    : -,  
	     , -, 
	,       
	)   .
    :   ;
	    (   ,   BPB);
	      (  ).

    ASCIIZ- (out_string):
 : ds:si -> 
 ,     ,   int 10h/ah=0Eh.

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

   kordldr.f1x:
1. ,     CHS-  LBA- .
	       
	.  : scan_for_filename  
	  'xor di,di'   31 FF (-   
	      33 FF,  fasm 
	  ).
2.      (..   
	 ,   0)  int 12h.   
	      .     
	  ,    592 Kb (94000h ).
	:      0A0000h  
	    ( 1-2 )  -
		   BIOS ""  .
3.    : FAT12  FAT16.  
	  Microsoft ( 1.03  ,
	 , 06  2000 ),  FAT 
	  :    
	FAT12-  4094 = 0xFF4.   ,  FAT12
	  0xFF5 ,   :    2,
	  0xFF7      .
	Win95/98/Me   :  FAT12/16 
	  0xFF5.  FAT  WinNT/2k/XP/Vista  
	 , ,  0xFF6 ( )  
	FAT12-,   ,   
	(  0xFF6) .   osloader.exe
	[  ntldr]  NT/2k/XP   .  
	[ FAT12/16    ntldr,   FAT-
	  ]  NT/2k    .  XP   
	   . Linux   FAT12/FAT16
	  .
	      . 9x ,    NT
	Microsoft     ,   
	.
4.  FAT12:       FAT   6000:0000.
	 ,   BPB,  12 ,
	 ,      (  
	  ),    12  ( FAT12
	     ).
 FAT16:   , ,   
	FAT   (   ,  
	  ,  ).
5.    ,      
	kordldr.f1x,      , 
	     kordldr.f1x.
6.    kord/loader   1000:0000.   
	,	  ,    ,  
	       
	"Fatal error: cannot load the secondary loader".
	:           
	   ASCIIZ,       
	- .
7.          hooked_err.
	 ,        
	       
	,     ,   
	-   .
8.       0x80,
	  al='f' ("floppy"), ah= ,
	 al='h' ("hard"), ah= -0x80 ( ).
	 bx='12',     - FAT12, 
	bx='16'   FAT16.	 si=  
	.     ds=0,  ds:si   .
9.     1000:0000.

     :
	   .
       .
1.        :
	ss:sp = 0:(7C00-8), bp=7C00:  ss:bp    
	    0:7C00,  -8   , 
	       2  ,
	     .
2.   , ,   ,
	    .
3.       .

  kordldr.f1x.
     FAT (get_next_cluster):
1.   FAT,  .
 FAT12:
2.  ds = 0x6000 - ,    
	  FAT.
3.  si = () + ()/2 -    
	,   .     .
4.     ,    
	   12  ,     
	 4 ;    -   12 ,    
	.
5.     12 .     0xFF7:
	   ,   CF ;
	  EOF  BadClus   CF.
 FAT16:
2.   ,     
	  FAT.
3.     ,   .
4.        
	.
5.    ax  ,    1  3.
6.     0xFFF7:    ,  
	CF ;   EOF  BadClus  CF.

   (load_file):
1.    - .     2-4.
2.       (
	  '/')  FAT- 8+3.   
	( 8   ,  3    
	  ),   .
3.         .  
	    .   :
	a) ,        .
	(      .)
	    ,    ;   ,
	 ,       . (
	      0  ( )-1,
	        .
	   -     ,
	  ,    ,   .)
	)         ,
	   .    ,
	   4.    ,  
	  .
	)     .    
	,         . 
	    ,     ,
	     . ,     
	 :   ;   ( 
	   FAT);      
	(  ).       .
4.     (/):   
	    ,   - .
	    - ,  
	      2.
5.      FAT      
	      ;
	        
	,       .
	  ,       
	    .

    (continue_load_file): 
	  5 load_file;      (
	  load_file)    5.
