     1                                  ; ****************************************************************************
     2                                  ; TRDOS386.ASM (TRDOS 386 Kernel) - v2.0.0 - trfs1fdb.s - TRFS1 BOOT SECTOR
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Turkish Rational SINGLIX File System 1 1.44MB Floppy Disk Boot Sector Code
     5                                  ; ----------------------------------------------------------------------------
     6                                  ; Last Update: 02/02/2018
     7                                  ; ----------------------------------------------------------------------------
     8                                  ; Beginning: 06/01/2018 
     9                                  ; ----------------------------------------------------------------------------
    10                                  ; Assembler: NASM version 2.11  
    11                                  ; ----------------------------------------------------------------------------
    12                                  ;	    ((nasm trfs1fdb.s -l trfs1fdb.lst -o TRFS1FDB.BIN)) 	
    13                                  ; ----------------------------------------------------------------------------
    14                                  ; Turkish Rational DOS
    15                                  ; Operating System Project v2.0 by ERDOGAN TAN (Beginning: 04/01/2016)
    16                                  ;
    17                                  ; Derived from 'trfs1_chs.s' TRDOS 386 TRFS1 boot sector source code
    18                                  ; by Erdogan Tan (04/01/2018).
    19                                  ;
    20                                  ; ****************************************************************************
    21                                  ; incbin "TRFS1FDB.BIN"
    22                                  
    23                                  rts_segment     equ	1000h
    24                                  
    25                                  ; FDT equalities
    26                                  ; 03-01-2018 [ TRFS Issue 2, Revision 14 ]
    27                                  
    28                                  FDT_Sign	equ 0	; 'FDT'
    29                                  FDT_SectorSize	equ 4	; 512
    30                                  FDT_Number	equ 6	; FDT sequence number
    31                                  FDT_FileNumber	equ 8	; 1st FDT address (offset)	
    32                                  FDT_NextFDTNum	equ 12  ; Next FDT address
    33                                  FDT_SectorCount	equ 16	; Sector count in section
    34                                  FDT_ParentDir	equ 20	; Parent directory address
    35                                  FDT_PDirSerial	equ 24	; Parent directory serial number
    36                                  FDT_FileSize	equ 28	; File Size lower 4 bytes
    37                                  FDT_FileSizeHW	equ 32	; File Size higher 2 bytes
    38                                  
    39                                  [BITS 16]
    40                                  [ORG 7C00h]
    41                                  bsjmpBoot:
    42                                                  ;jmp     short loc_41  ; jmp short start
    43 00000000 EB2E                    		jmp	short loc_30
    44                                  bsjmpBoot_nop:
    45 00000002 90                                      nop
    46                                  
    47                                  ; TR-SINGLIX FS1 BootSector Identification (Data) Block
    48                                  ; 03-01-2018 FS1 Boot Sector
    49                                  ; 02-01-2018 [ TRFS Issue 1, Revision 14 ]
    50                                  
    51 00000003 4653                    bsFSystemID:    db 'FS'			; bp+3
    52 00000005 00                                      db 0   
    53 00000006 0002                    bsBytesPerSec:  dw 512			; bp+6
    54 00000008 03                      bsMediaAttrib:  db 3			; bp+8
    55 00000009 A1                      bsPartitionID:  db 0A1h			; bp+9
    56 0000000A 01                      bsFSVersionMaj: db 01h			; bp+10
    57 0000000B 00                      bsFSVersionMin: db 0			; bp+11
    58 0000000C 00000000                bsBeginSector:	dd 0			; bp+12 
    59 00000010 400B0000                bsVolumeSize:   dd 2880			; bp+16
    60 00000014 00000000                bsStartupFDT:	dd 0			; bp+20
    61 00000018 01000000                bsMATLocation:  dd 1			; bp+24
    62 0000001C 03000000                bsRootDirDT:    dd 3			; bp+28
    63 00000020 00000000                bsSysConfFDT:	dd 0			; bp+32
    64 00000024 00000000                bsSwapFD:       dd 0			; bp+36
    65 00000028 00000000                bsUndelDirDT:	dd 0			; bp+40
    66 0000002C 00                      bsDriveNumber:  db 0			; bp+44
    67 0000002D 00                      bs_LBA_Ready:	db 0			; bp+45
    68                                  bsMagicWord:	
    69                                  bs_Disk_SecPerTrack:
    70 0000002E 12                      		db 18			; bp+46
    71                                  bs_Disk_Heads: 
    72 0000002F 02                                      db 2			; bp+47
    73                                  ;bsOperationSys: 
    74                                  ;;		db 'TR-SINGLIX v1.0b'	; bp+48
    75                                  ;		db 'TR-DOS 386 TRFS1'
    76                                  ;terminator:	db 0
    77                                  
    78                                  ; NOTE: This boot sector is valid for < 32MB Hard Disks and Floppy Disks
    79                                  ; [bs_Disk_SecPerTrack] and [bs_Disk_Heads] and
    80                                  ; [bsVolumeSize] and [bsRootDirDT] and [bsBeginSector] and
    81                                  ; [bsMediaAttrib] must be set as correct!
    82                                  ; High word of all double word -dd- parameters must be 0!!!
    83                                  ; Max. volume size = 65535 sectors. 
    84                                  
    85                                  ; !!! ((Default configuration: 1.44MB (3.5") Floppy Disk)) !!!
    86                                  
    87                                  start:
    88                                  ;loc_41:
    89 00000030 BD007C                  loc_30:		mov	bp, 7C00h
    90                                  
    91 00000033 31C0                    		xor	ax, ax
    92 00000035 8ED8                    		mov	ds, ax
    93 00000037 8EC0                    		mov	es, ax
    94 00000039 FA                      		cli
    95 0000003A 8ED0                    		mov	ss, ax
    96 0000003C 89EC                    		mov	sp, bp
    97 0000003E FB                      		sti
    98 0000003F 8A562C                  		mov	dl, [bp+44] ; [bsDriveNumber]
    99                                  
   100 00000042 A3[FC01]                		mov	[bsReserved1], ax 
   101                                  
   102                                  		;; Check File System ID value
   103                                  		;cmp	word [bp+3], 'FS' ; [bsFSystemID]
   104                                  		;jne	short invalid_system_disk
   105                                  
   106                                  		; Check Bytes/Sector value
   107                                  		; It must be 512 !? (at least, for TRDOS386) 
   108                                  		;cmp	word [bp+6], 512 ; [bsBytesPerSec]
   109                                  		;jne	short invalid_system_disk
   110                                  
   111                                  		; Check LBA signature
   112                                  		;cmp	byte [bp+45], 0 ; [bs_LBA_Ready]
   113                                  		;ja	short invalid_system_disk
   114                                  
   115                                  		; Check Volume Size (>= 32MB)
   116                                  		;cmp	word [bp+18], 0
   117                                  		;ja	short invalid_system_disk
   118                                  
   119                                  check_startup_file_address: 
   120 00000045 8B4614                  		mov	ax, [bp+20] ; [bsStartupFDT]
   121 00000048 21C0                    		and	ax, ax
   122 0000004A 7445                    		jz	short invalid_system_disk
   123                                  
   124 0000004C 3B4610                  		cmp	ax, [bp+16] ; [bsVolumeSize]
   125 0000004F 7340                    		jnb	short invalid_system_disk
   126                                  
   127 00000051 83EC04                  		sub	sp, 4 ; 13/01/2018
   128                                  		; SP = 7BFCh
   129                                  
   130 00000054 03460C                  		add	ax, [bp+12] ; [bsBeginSector]
   131 00000057 8946FE                  		mov	[bp-2], ax
   132                                  
   133                                  		; overwrite hd drive number !
   134                                                  ;mov	[bsDriveNumber], dl ; drive number from INT 19h
   135                                  		;mov	[bp+44], dl
   136                                  
   137 0000005A BB007E                  		mov	bx, 7E00h    ; FDT Buffer address
   138                                  		;mov	si, bx
   139                                  
   140 0000005D E8BC00                  		call	read_sector
   141 00000060 723B                    		jc	short disk_io_error
   142                                  
   143                                  check_startup_file_fdt: 
   144                                  		; Check FDT signature
   145                                  		;lodsw
   146                                  		;cmp	ax, 'FD' 
   147                                  		;jne	short invalid_system_disk ; not a valid FDT!
   148                                  		;lodsb
   149                                  		;cmp	al, 'T'
   150                                  		;jne	short invalid_system_disk ; not a valid FDT!
   151                                  		;add	si, 5
   152 00000062 BE087E                  		mov	si, 7E00h+FDT_FileNumber
   153 00000065 AD                      		lodsw	; FDT_FileNumber
   154 00000066 8B560C                  		mov	dx, [bp+12] ; [bsBeginSector]
   155 00000069 8B7EFE                  		mov	di, [bp-2]  ; Current FDT address (LBA) 
   156 0000006C 01D0                    		add	ax, dx
   157 0000006E 39C7                    		cmp	di, ax
   158 00000070 751F                    		jne	short invalid_system_disk ; not a valid FDT!
   159 00000072 AD                      		lodsw ; +2
   160 00000073 AD                      		lodsw	; FDT_NextFDTNum
   161 00000074 01D0                    		add	ax, dx  ; DX = Volume Beginning Sector
   162 00000076 8946FE                  		mov	[bp-2], ax  ; Next FDT address (LBA)
   163 00000079 AD                      		lodsw ; +2
   164                                  		; DI = Current FDT address
   165 0000007A AD                      		lodsw	; FDT_SectorCount
   166 0000007B 09C0                    		or	ax, ax
   167 0000007D 7412                    		jz	short invalid_system_disk ; not a valid FDT!
   168 0000007F 89C1                    		mov	cx, ax
   169 00000081 83C60A                  		add	si, 2+8
   170 00000084 AD                      		lodsw	; FDT_FileSize
   171 00000085 89C2                    		mov	dx, ax
   172 00000087 AD                      		lodsw
   173 00000088 92                      		xchg	dx, ax
   174                                  
   175 00000089 09C0                    		or	ax, ax
   176 0000008B 751E                    		jnz	short bs_02
   177 0000008D 09D2                    		or	dx, dx
   178 0000008F 751A                    		jnz	short bs_02 
   179                                  
   180                                  invalid_system_disk:
   181 00000091 BE[B401]                		mov	si, Inv_disk_Msg
   182 00000094 E8D000                  		call	print_msg
   183                                  getchar_reboot:
   184                                  		; Wait for a keystroke just before reboot
   185 00000097 30E4                    		xor	ah, ah
   186 00000099 CD16                    		int	16h
   187                                  		
   188 0000009B CD19                    		int	19h	; disk boot	
   189                                  				; causes reboot of disk system
   190                                  disk_io_error:
   191 0000009D BE[A701]                		mov	si, Diskio_err_Msg
   192 000000A0 E8C400                  		call	print_msg
   193                                  ;replace_disk:		
   194                                  ;		mov	si, Replace_Msg	
   195                                  replace_disk:	
   196 000000A3 BE[C901]                		mov	si, Disk_err_replace_Msg
   197 000000A6 E8BE00                  		call	print_msg
   198 000000A9 EBEC                    		jmp	short getchar_reboot
   199                                  
   200                                  bs_02:	
   201 000000AB BBFF01                  		mov	bx, 511
   202 000000AE 01D8                    		add	ax, bx
   203 000000B0 83D200                  		adc	dx, 0
   204 000000B3 43                      		inc	bx
   205 000000B4 F7F3                    		div	bx
   206 000000B6 BB7F04                  		mov	bx, 1151
   207 000000B9 39D8                    		cmp	ax, bx		; Maximum 1151 sectors
   208                                  					; (Segment 1000h to 9FE0h)
   209                                  					; ((512 bytes must be reserved for
   210                                  					; stack just before segment A000h))	
   211 000000BB 7602                    		jna	short bs_03
   212 000000BD 89D8                    		mov	ax, bx
   213                                  bs_03:
   214                                  		; CX = sector count (in section)
   215                                  load_startup_file:
   216 000000BF 39C8                    		cmp	ax, cx
   217 000000C1 7302                    		jnb	short bs_04
   218 000000C3 89C1                    		mov	cx, ax		; sector count (in section)
   219                                  					; must not be greater
   220                                  					; remain sectors (for file) to read
   221                                  bs_04:
   222 000000C5 29C8                    		sub	ax, cx
   223 000000C7 8946FC                  		mov	[bp-4], ax	; Remain sector count (for next read)	
   224                                  		;mov	ax, di ; FDT address
   225 000000CA 97                      		xchg	ax, di
   226 000000CB 40                      		inc	ax	 ; +1 (section data)
   227                                  		; CX = sector count (<= 1151) in section
   228 000000CC 8B1E[7501]              		mov	bx, [next_segment]
   229 000000D0 06                      		push	es
   230 000000D1 8EC3                    		mov	es, bx ; segment = 1000h +
   231 000000D3 31DB                    		xor	bx, bx ; offset = 0 
   232                                  		; CX = num of sectors to read (= sectors/cluster)
   233 000000D5 E84700                  		call	disk_read
   234 000000D8 07                      		pop	es
   235 000000D9 72C2                    		jc	short disk_io_error
   236 000000DB C1EB04                  		shr	bx, 4 ; from byte count to paragraph count
   237 000000DE 011E[7501]              		add	[next_segment], bx
   238                                  
   239                                  		;mov	di, [bp-4]	; Remain sector count
   240 000000E2 09FF                    		or	di, di
   241 000000E4 0F84A300                		jz	bs_07		; none
   242                                  		
   243 000000E8 8B46FE                  		mov	ax, [bp-2]	; Next FDT address
   244 000000EB BB007E                  		mov	bx, 7E00h	; FDT Buffer address
   245 000000EE 89DE                    		mov	si, bx
   246                                  
   247 000000F0 E82900                  		call	read_sector
   248 000000F3 72A8                    		jc	short disk_io_error
   249                                  		
   250 000000F5 AD                      		lodsw
   251 000000F6 3D4644                  		cmp	ax, 'FD' 
   252 000000F9 7596                    		jne	short invalid_system_disk ; not a valid FDT!
   253 000000FB AC                      		lodsb
   254 000000FC 3C54                    		cmp	al, 'T'
   255 000000FE 7591                    		jne	short invalid_system_disk ; not a valid FDT!
   256 00000100 83C609                  		add	si, 9
   257 00000103 AD                      		lodsw	; FDT_NextFDTNum
   258 00000104 03460C                  		add	ax, [bp+12] ; [bsBeginSector]
   259 00000107 8746FE                  		xchg	[bp-2], ax  ; Next FDT address (LBA)
   260 0000010A 89C2                    		mov	dx, ax ; Current FDT address
   261                                  		;xchg	dx, ax
   262 0000010C AD                      		lodsw	; +2
   263 0000010D AD                      		lodsw	; FDT_SectorCount
   264 0000010E 09C0                    		or	ax, ax
   265 00000110 0F847DFF                		jz	invalid_system_disk ; not a valid FDT!
   266 00000114 89C1                    		mov	cx, ax ; sector count (in section)
   267                                  		;xchg	cx, ax
   268 00000116 89F8                    		mov	ax, di ; [bp-4] ; remain sectors to read
   269                                  		;xchg	ax, di
   270 00000118 89D7                    		mov	di, dx
   271                                  		;xchg	di, dx
   272                                  		; DI = Current FDT address
   273 0000011A EBA3                    		jmp	short load_startup_file
   274                                  
   275                                  read_sector:	; 25/12/2017 (Read 1 sector)
   276 0000011C B90100                  		mov	cx, 1
   277                                  disk_read:
   278                                  		;mov	byte [bp+retry_count-7C00h], 4
   279 0000011F B204                    		mov	dl, 4 ; retry count
   280                                  disk_read_0:
   281 00000121 60                      		pusha
   282                                  chs_read:	
   283                                  		; Convert LBA to CHS
   284 00000122 31D2                    		xor	dx, dx
   285 00000124 30ED                    		xor	ch, ch
   286                                  		;mov	cl, byte [bs_Disk_SecPerTrack] ; [bp+46]
   287                                  				; sectors per track (18 or 17 or 63)
   288 00000126 8A4E2E                  		mov	cl, [bp+46]
   289 00000129 F7F1                    		div	cx
   290 0000012B FEC2                    		inc	dl	; sector number (1 based)
   291 0000012D 52                      		push	dx
   292                                  		; ax = (heads * cylinder) + head number
   293 0000012E 29D2                    		sub	dx, dx
   294                                  		;mov	cl, [bs_Disk_Heads] ; [bp+47]
   295 00000130 8A4E2F                  		mov	cl, [bp+47] ; number of heads (2 to 255)	
   296 00000133 F7F1                    		div	cx 	
   297                                  		; AX = cylinder (0 to 1023)
   298                                  		; DX = head number (in DL)
   299 00000135 88D6                    		mov	dh, dl	 ; head number in DH
   300                                  		;mov	dl, [bsDriveNumber] ; [bp+2Ch] ; Drive number (80h)
   301 00000137 8A562C                  		mov	dl, [bp+44]
   302 0000013A 59                      		pop	cx
   303 0000013B 88C5                    		mov	ch, al ; Low 8 bits of cylinder number (0 to 7)
   304 0000013D C0E406                  		shl	ah, 6  ; High 2 bits of cylinder is in bit 7&8	
   305 00000140 08E1                    		or	cl, ah ; High two bits of CL is cylinder bits 8&9 
   306 00000142 B80102                  		mov	ax, 201h ; Read 1 sector
   307 00000145 CD13                    		int	13h
   308                                  disk_read_1:
   309 00000147 61                      		popa
   310 00000148 7305                    		jnc	short disk_read_2
   311                                  		; cf = 1
   312                                  		;dec	byte [retry_count]
   313                                  		;dec	byte [bp+retry_count-7C00h]
   314 0000014A FECA                    		dec	dl ; Retry count
   315 0000014C 75D3                    		jnz	short disk_read_0 ; Retry
   316                                  		; cf = 1
   317 0000014E C3                      		retn
   318                                  disk_read_2:
   319                                  		;add	bx, [bp+6] ; [bsBytesPerSec] ; 512
   320 0000014F 81C30002                		add	bx, 512
   321                                  		;add	bh, 2 ; **
   322 00000153 730D                    		jnc	short disk_read_3
   323                                  		;mov	bx, [next_segment]
   324 00000155 8CC3                    		mov	bx, es
   325 00000157 80C710                  		add	bh, 10h	 
   326 0000015A 891E[7501]              		mov	[next_segment], bx
   327 0000015E 8EC3                    		mov	es, bx
   328 00000160 31DB                    		xor	bx, bx
   329                                  disk_read_3:
   330 00000162 40                      		inc	ax
   331 00000163 49                      		dec	cx
   332 00000164 75B9                    		jnz	short disk_read
   333                                  		;clc 	; ** (128 sectors/cluster!?)
   334                                  bs_05:
   335 00000166 C3                      		retn
   336                                  
   337                                  print_msg:
   338                                  		; DS:SI = Error message address (ASCIIZ string)	
   339 00000167 B40E                    		mov	ah, 0Eh
   340 00000169 BB0700                  		mov	bx, 7
   341                                  bs_06:
   342 0000016C AC                      		lodsb
   343 0000016D 84C0                    		test	al, al
   344 0000016F 74F5                    		jz	short bs_05
   345 00000171 CD10                    		int	10h
   346 00000173 EBF7                    		jmp	short bs_06
   347                                  
   348                                  next_segment:
   349 00000175 0010                    		dw	rts_segment
   350                                  
   351                                  		; Filler (Magic Number)
   352 00000177 04                      		db	4
   353 00000178 01                      		db	1
   354 00000179 07                      		db	7
   355                                  
   356                                  		; Filler (File System)
   357 0000017A 5452465331              		db	'TRFS1'
   358 0000017F 00                      		db	0
   359                                  
   360                                  		; Filler (Boot Code Date)
   361 00000180 30322F30322F323031-     		db	'02/02/2018'
   361 00000189 38                 
   362 0000018A 00                      		db	0
   363                                  
   364                                  bs_07:
   365                                  		; Set TRDOS 386 kernel specific parameters (& signs)
   366                                  		; and
   367                                  		; Launch TRDOS 386 Kernel (Startup/RTS file)
   368                                  
   369 0000018B A1[7501]                		mov	ax, [next_segment] ; 16 paragraphs after the
   370                                  					  ; start of the last segment
   371                                  					  ; of the kernel file loading
   372                                  					  ; space.
   373                                  					  ; So, (top of) stack will have
   374                                  					  ; 256 bytes or more distance
   375                                  					  ; from the last byte
   376                                  					  ; of the kernel file.	 							
   377                                  					  ; (This will be enough for
   378                                  					  ; TRDOS 386 kernel before 
   379                                  					  ; entering protected mode.)
   380 0000018E FA                      		cli
   381 0000018F 8ED0                    		mov	ss, ax
   382 00000191 BCFEFF                  		mov	sp, 0FFFEh			
   383 00000194 FB                      		sti
   384                                  
   385 00000195 BB0010                  		mov     bx, rts_segment ; 1000h
   386                                  
   387 00000198 8EDB                    		mov	ds, bx
   388 0000019A 8EC3                    		mov	es, bx
   389                                  		;mov	fs, bx
   390                                  		;mov	gs, bx
   391                                  
   392                                  		;xor	bx, bx
   393                                  		;xor	cx, cx
   394                                  		;xor	dx, dx
   395                                  		;xor	si, si
   396                                  		;xor	di, di
   397                                  		;xor	bp, bp
   398                                  
   399                                  		; bp = 7C00h
   400                                  
   401                                  		;mov	dl, [bsDriveNumber]
   402 0000019C 8B562C                                  mov	dx, [bp+44] ; DL = Drive number, DH = 0 (CHS)
   403                                  
   404 0000019F B8A101                  		mov	ax, 417 ; TRDOS boot sector sign for TRDOS386.SYS
   405                                  
   406 000001A2 EA00000010              		jmp	rts_segment:0
   407                                  
   408                                  Diskio_err_Msg:
   409 000001A7 0D0A                    		db	0Dh, 0Ah
   410 000001A9 4469736B206572726F-     		db	'Disk error'
   410 000001B2 72                 
   411                                  		;db	'!'
   412 000001B3 00                      		db	0
   413                                  Inv_disk_Msg:   
   414 000001B4 0D0A                    		db	0Dh, 0Ah
   415 000001B6 496E76616C69642073-     		db	'Invalid system disk'
   415 000001BF 797374656D20646973-
   415 000001C8 6B                 
   416                                  Disk_err_replace_Msg:
   417 000001C9 21                      		db	'!'
   418                                  Replace_Msg:    
   419 000001CA 0D0A                    		db	0Dh, 0Ah
   420 000001CC 5265706C6163652074-     		db	'Replace the disk and press any key to reboot.'
   420 000001D5 6865206469736B2061-
   420 000001DE 6E6420707265737320-
   420 000001E7 616E79206B65792074-
   420 000001F0 6F207265626F6F742E 
   421 000001F9 0D0A00                  		db	0Dh, 0Ah, 0
   422                                  
   423                                  		times	508 - ($ - $$) db 0
   424                                  bsReserved1:
   425 000001FC 5452                    		db	'TR'  ; 'Turkish Rational DOS' feature identifier.
   426                                  bootsignature1:
   427 000001FE 55AA                    		db	55h, 0AAh
