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

					   -     .

  FAT32-      0x200 = 512 .

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

      ,    LBA,
    use_lba    .
  :
1)  ,   FAT    
  . (       
     MBR  ,   
  (  6  )   
 ).
2)   - 80386.
3)       584K   .

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

   (    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

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

  :
	...-7C00	
	7C00-7E00	 
	7E00-8200	   (kordldr.f32)
	8400-8C00	     FAT: 100h   8
			: 4  (  -   ) 
			 L2-    
			    
			+ 4    ;   
			    ,   ,
			      
	60000-80000	   FAT (100h )
	80000-90000	    
	90000-...	    (  
			2000h  = 100h ,   
			    8 ;
			    
			  -  , 
			 A0000  EBDA, Extended BIOS Data Area)

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

  .
  (start):    BIOS  ,  
	dl   ,    
1.   ss:sp = 0:7C00 (   
	),   ds = 0,   ss:bp  
	 (      [bp+N] -
	  ds     ).   
	     
	 byte [bp-2].
2. LBA-: ,    LBA,   41h
	 13h.  ,      
	   LBA.
CHS-:      8  13h 
	    BPB.    ,
	    .
3.    FAT-,      
	  dword [bp-10].     
	 FAT,         
	dword [bp-6].
4. (    )    dword- -1
	    dword [bp-14] - 
	,   ,    FAT
	(-1        FAT).
5.      kordldr.f32.    -  
	       .
	:         
	   ,      FAT
	(8+3 - 8   , 3   ,   
	 ,      
	,   ,   ).
6.     kordldr.f32   0:7E00  
	 .     eax  
	   kordldr.f32,   cx -   
	(  ).

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

   (read_cluster):
    :
	ss:bp = 0:7C00
	es:bx =    ,    
	eax =  
 : ecx =    ( ),
	es:bx    ,     ,
	eax     32-  
  ecx  ,      
    .

   (read_sectors32  read_sectors2):
    :
	ss:bp = 0:7C00
	es:bx =    ,    
	eax =   (   
		 read_sectors32,   
		 read_sectors2)
	cx =   (   )
 : es:bx    ,     
	  32-   
0.   read_sectors2,      
	     ,   
	 ,     [bp-10].
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.

     (lookup_in_dir):
    :
	ss:bp = 0:7C00
	ds:si =       FAT (. )
	eax =   
	bx = 0
 :  CF ,    ;  , 
	CF   es:di    
          
.        read_clusters,
     -   
get_next_clusters.     ,   
8000:0000,    2000h     ( , ,
   )    
(   ,     kordldr.f32).
      :   ;
    (    );
         FAT.

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

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

   kordldr.f32:
1. ,     CHS-  LBA- .
	       
	.  :  CHS-   err 
	 0xE8 (  call),  LBA-    
	  0x14,    err .
2.      (..   
	 ,   0)  int 12h.   
	      .     
	  ,    592 Kb (94000h ).
	:      0A0000h  
	    ( 1-2 )  -
		   BIOS ""  .
3.   .    - 
	  ;       ,
	     .
4.   FAT.     FAT    
	 ,       ,
	    .      
	  FAT (    -  
	).
5.    ,      
	kordldr.f32,      , 
	     kordldr.f32.
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 ( ).
	(,   FAT32  ?  -  ... 
	  ,    ,  ,
	  ,    BIOS-  0x80?)
	 bx='32' (   - FAT32).
	 si=   .   
	 ds=0,  ds:si   .
9.     1000:0000.

     :
	   .
       .
1.        :
	ss:sp = 0:(7C00-10), bp=7C00:  ss:bp    
	    0:7C00,  -10   , 
	       10  ,
	     . ( [ebp-14],
	" ,    FAT",   
	   kordldr.f32.)
2.        
	 (     ).
3.       .

  kordldr.f32.
     FAT (get_next_cluster):
1.     FAT,     .
	(  0x200 ,    4 .)
2. ,     .  ,   3  4.
3.  ,       .     ,
	     .  , 
	   (,       );
	 ,       
	,  ()      ,
	      ,   
	      .
4.    FAT  .
5.  :       ,
	  ,    . (   
	     ,      .)
6.     FAT,   4 .
7.     :    
	0x0FFFFFF7,        ;
	    .

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

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