     1                                  ; ****************************************************************************
     2                                  ; TRDOS386.ASM (TRDOS 386 Kernel) - v2.0.0 - trfs1_bs.s - TRFS1 BOOT SECTOR
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Turkish Rational SINGLIX File System 1 Hard Disk (LBA) Boot Sector Code
     5                                  ; ----------------------------------------------------------------------------
     6                                  ; Last Update: 02/02/2018
     7                                  ; ----------------------------------------------------------------------------
     8                                  ; Beginning: 03/01/2018 
     9                                  ; ----------------------------------------------------------------------------
    10                                  ; Assembler: NASM version 2.11  
    11                                  ; ----------------------------------------------------------------------------
    12                                  ;	    ((nasm trfs1lba.s -l trfs1lba.lst -o TRFS1LBA.BIN)) 	
    13                                  ; ----------------------------------------------------------------------------
    14                                  ; Turkish Rational DOS
    15                                  ; Operating System Project v2.0 by ERDOGAN TAN (Beginning: 04/01/2016)
    16                                  ;
    17                                  ; Derived from 'fat32_bs.s' TRDOS 386 (v2) FAT32 boot sector source code
    18                                  ; by Erdogan Tan (25/12/2017).
    19                                  ;
    20                                  ; Derived from 'FS1_HDBS.ASM' TRDOS 8086 (v1) Singlix FS1 boot sector
    21                                  ; source code by Erdogan Tan (21/02/2010).
    22                                  ; ****************************************************************************
    23                                  ; incbin "TRFS1LBA.BIN" (in 'hdimage.s' & 'hdformat.s')
    24                                  
    25                                  rts_segment     equ	1000h
    26                                  
    27                                  ; FDT equalities
    28                                  ; 03-01-2018 [ TRFS Issue 2, Revision 14 ]
    29                                  
    30                                  FDT_Sign	equ 0	; 'FDT'
    31                                  FDT_SectorSize	equ 4	; 512
    32                                  FDT_Number	equ 6	; FDT sequence number
    33                                  FDT_FileNumber	equ 8	; 1st FDT address (offset)	
    34                                  FDT_NextFDTNum	equ 12  ; Next FDT address
    35                                  FDT_SectorCount	equ 16	; Sector count in section
    36                                  FDT_ParentDir	equ 20	; Parent directory address
    37                                  FDT_PDirSerial	equ 24	; Parent directory serial number
    38                                  FDT_FileSize	equ 28	; File Size lower 4 bytes
    39                                  FDT_FileSizeHW	equ 32	; File Size higher 2 bytes
    40                                  
    41                                  [BITS 16]
    42                                  [ORG 7C00h]
    43                                  bsjmpBoot:
    44 00000000 EB3F                                    jmp     short loc_41  ; jmp short start
    45                                  bsjmpBoot_nop:
    46 00000002 90                                      nop
    47                                  
    48                                  ; TR-SINGLIX FS1 BootSector Identification (Data) Block
    49                                  ; 03-01-2018 FS1 HD Partition Boot Sector
    50                                  ; 02-01-2018 [ TRFS Issue 1, Revision 14 ]
    51                                  
    52 00000003 4653                    bsFSystemID:    db 'FS'			; bp+3
    53 00000005 00                                      db 0   
    54 00000006 0002                    bsBytesPerSec:  dw 512			; bp+6
    55 00000008 01                      bsMediaAttrib:  db 1			; bp+8
    56 00000009 A1                      bsPartitionID:  db 0A1h			; bp+9
    57 0000000A 01                      bsFSVersionMaj: db 01h			; bp+10
    58 0000000B 00                      bsFSVersionMin: db 0			; bp+11
    59 0000000C 00000000                bsBeginSector:	dd 0			; bp+12 
    60 00000010 00000000                bsVolumeSize:   dd 0			; bp+16
    61 00000014 00000000                bsStartupFDT:	dd 0			; bp+20
    62 00000018 01000000                bsMATLocation:  dd 1			; bp+24
    63 0000001C 00000000                bsRootDirDT:    dd 0			; bp+28
    64 00000020 00000000                bsSysConfFDT:	dd 0			; bp+32
    65 00000024 00000000                bsSwapFD:       dd 0			; bp+36
    66 00000028 00000000                bsUndelDirDT:	dd 0			; bp+40
    67 0000002C 00                      bsDriveNumber:  db 0			; bp+44
    68 0000002D 01                      bs_LBA_Ready:	db 01h			; bp+45
    69                                  bsMagicWord:	
    70                                  bs_Disk_SecPerTrack:
    71 0000002E A1                      		db 0A1h			; bp+46
    72                                  bs_Disk_Heads: 
    73 0000002F 01                                      db 01h			; bp+47
    74                                  bsOperationSys: 
    75                                  ;		db 'TR-SINGLIX v1.0b'	; bp+48
    76 00000030 54522D444F53203338-     		db 'TR-DOS 386 TRFS1' 
    76 00000039 36205452465331     
    77 00000040 00                      terminator:     db 0
    78                                   
    79                                  start:
    80                                  loc_41:
    81 00000041 BD007C                  		mov	bp, 7C00h
    82                                  
    83 00000044 3DA101                  		cmp	ax, 417  ; If AX=417, the masterboot sector
    84                                  				 ; has a SINGLIX FS (& TRDOS 386)
    85                                  				 ; masterboot code; and...  
    86                                  				 ; DX=ES=SS=0, BP=7C00h
    87                                  				 ; SP=7C00h ... masterboot sector has
    88                                  				 ; been loaded at 0:600h, it has
    89                                  				 ; CHS parameters at offset 600h+420.
    90                                  				 ; (There is a 01A1h in offset 600h+417)
    91                                  	
    92 00000047 740F                    		je	short bs_01 ; no need to following assignments !
    93                                  
    94 00000049 31C0                    		xor	ax, ax
    95 0000004B 8ED8                    		mov	ds, ax
    96 0000004D 8EC0                    		mov	es, ax
    97 0000004F FA                      		cli
    98 00000050 8ED0                    		mov	ss, ax
    99 00000052 89EC                    		mov	sp, bp
   100 00000054 FB                      		sti
   101 00000055 8A562C                  		mov	dl, [bp+44] ; [bsDriveNumber]
   102                                  bs_01:
   103 00000058 8936[FC01]              		mov	[bsReserved1], si ; Partition entry address..
   104                                  		; 24/12/2017		  ; (from Singlix FS MBR)
   105                                  					  ; (7BEh,7CEh,7DEh,7EEh) 
   106                                  
   107                                  		;; Check File System ID value
   108                                  		;cmp	word [bp+3], 'FS' ; [bsFSystemID]
   109                                  		;jne	short invalid_system_disk
   110                                  
   111                                  		; Check Bytes/Sector value
   112                                  		; It must be 512 !? (at least, for TRDOS386) 
   113                                  		;cmp	word [bp+6], 512 ; [bsBytesPerSec]
   114                                  		;jne	short invalid_system_disk
   115                                  
   116                                  
   117                                  		; Check LBA signature
   118                                  		;;cmp	byte [bp+45], 1 ; [bs_LBA_Ready]
   119                                  		;;jne	short invalid_system_disk
   120                                  		;cmp	byte [bp+45], 0 ; [bs_LBA_Ready]
   121                                  		;jna	short invalid_system_disk
   122                                  
   123                                  
   124                                  check_startup_file_address: 
   125 0000005C 668B4614                		mov	eax, [bp+20] ; [bsStartupFDT]
   126 00000060 6621C0                  		and	eax, eax
   127 00000063 745B                    		jz	short invalid_system_disk
   128                                  
   129 00000065 663B4610                		cmp	eax, [bp+16] ; [bsVolumeSize]
   130 00000069 7355                    		jnb	short invalid_system_disk
   131                                  
   132 0000006B 83EC08                  		sub	sp, 8 ; 13/01/2018
   133                                  		; SP = 7BF8h
   134                                  
   135 0000006E 6603460C                		add	eax, [bp+12] ; [bsBeginSector]
   136 00000072 668946FC                		mov	[bp-4], eax
   137                                  
   138                                  		; overwrite hd drive number !
   139                                                  ;mov	[bsDriveNumber], dl ; drive number from INT 19h
   140 00000076 88562C                  		mov	[bp+44], dl
   141                                  
   142 00000079 BB007E                  		mov	bx, 7E00h    ; FDT Buffer address
   143 0000007C 89DE                    		mov	si, bx
   144                                  
   145 0000007E E8D400                  		call	read_sector
   146 00000081 7249                    		jc	short disk_io_error
   147                                  
   148                                  check_startup_file_fdt: 
   149                                  		; Check FDT signature
   150 00000083 AD                      		lodsw
   151 00000084 3D4644                  		cmp	ax, 'FD' 
   152 00000087 7537                    		jne	short invalid_system_disk ; not a valid FDT!
   153 00000089 AC                      		lodsb
   154 0000008A 3C54                    		cmp	al, 'T'
   155 0000008C 7532                    		jne	short invalid_system_disk ; not a valid FDT!
   156 0000008E 83C605                  		add	si, 5
   157 00000091 66AD                    		lodsd	; FDT_FileNumber
   158 00000093 668B560C                		mov	edx, [bp+12] ; [bsBeginSector]
   159 00000097 668B7EFC                		mov	edi, [bp-4] ; Current FDT address (LBA) 
   160 0000009B 6601D0                  		add	eax, edx
   161 0000009E 6639C7                  		cmp	edi, eax
   162 000000A1 751D                    		jne	short invalid_system_disk ; not a valid FDT!
   163 000000A3 66AD                    		lodsd	; FDT_NextFDTNum
   164 000000A5 6601D0                  		add	eax, edx
   165 000000A8 668946FC                		mov	[bp-4], eax  ; Next FDT address (LBA)
   166                                  		; EDI = Current FDT address
   167 000000AC 66AD                    		lodsd	; FDT_SectorCount
   168 000000AE 6609C0                  		or	eax, eax
   169 000000B1 740D                    		jz	short invalid_system_disk ; not a valid FDT!
   170 000000B3 6689C1                  		mov	ecx, eax
   171 000000B6 83C608                  		add	si, 8
   172 000000B9 66AD                    		lodsd	; FDT_FileSize
   173                                  		;mov	edx, eax
   174                                  		;lodsw	; FDT_FileSizeHW
   175                                  		;movzx	eax, ax		
   176                                  		;mov	ecx, edx
   177                                  		;or	ecx, eax
   178                                  		;jz	short invalid_system_disk ; not a valid FDT!
   179 000000BB 6609C0                  		or	eax, eax
   180 000000BE 7514                    		jnz	short bs_02
   181                                  
   182                                  invalid_system_disk:
   183 000000C0 BE[D301]                		mov	si, Inv_disk_Msg
   184 000000C3 E8EE00                  		call	print_msg
   185                                  getchar_reboot:
   186                                  		; Wait for a keystroke just before reboot
   187 000000C6 30E4                    		xor	ah, ah
   188 000000C8 CD16                    		int	16h
   189                                  		
   190 000000CA CD19                    		int	19h	; disk boot	
   191                                  				; causes reboot of disk system
   192                                  disk_io_error:
   193 000000CC BE[C301]                		mov	si, Diskio_err_Msg
   194 000000CF E8E200                  		call	print_msg
   195 000000D2 EBF2                    		jmp	short getchar_reboot
   196                                  
   197                                  bs_02:	
   198                                  		;xchg	eax, edx
   199                                  		;mov	ebx, 511
   200                                  		;add	eax, ebx
   201                                  		;adc	edx, 0
   202                                  		;inc	ebx
   203 000000D4 6631D2                  		xor	edx, edx
   204 000000D7 66BB00020000            		mov	ebx, 512
   205 000000DD 6601D8                  		add	eax, ebx
   206 000000E0 6648                    		dec	eax
   207 000000E2 66F7F3                  		div	ebx
   208 000000E5 BB7F04                  		mov	bx, 1151
   209 000000E8 6639D8                  		cmp	eax, ebx	; Maximum 1151 sectors
   210                                  					; (Segment 1000h to 9FE0h)
   211                                  					; ((512 bytes must be reserved for
   212                                  					; stack just before segment A000h))	
   213 000000EB 7603                    		jna	short bs_03
   214 000000ED 6689D8                  		mov	eax, ebx
   215                                  bs_03:
   216                                  		; ECX = sector count (in section)
   217                                  load_startup_file:
   218 000000F0 6639C8                  		cmp	eax, ecx
   219 000000F3 7303                    		jnb	short bs_04
   220 000000F5 6689C1                  		mov	ecx, eax	; sector count (in section)
   221                                  					; must not be greater
   222                                  					; remain sectors (for file) to read
   223                                  bs_04:
   224 000000F8 6629C8                  		sub	eax, ecx
   225 000000FB 668946F8                		mov	[bp-8], eax	; Remain sector count (for next read)	
   226 000000FF 6689F8                  		mov	eax, edi ; FDT address
   227 00000102 6640                    		inc	eax	 ; +1 (section data)
   228                                  		; CX = Sector count (<= 1151) in section
   229 00000104 8B1E[EF01]              		mov	bx, [next_segment]
   230 00000108 06                      		push	es
   231 00000109 8EC3                    		mov	es, bx ; segment = 1000h +
   232 0000010B 31DB                    		xor	bx, bx ; offset = 0 
   233                                  		; CX = num of sectors to read (= sectors/cluster)
   234 0000010D E84800                  		call	disk_read
   235 00000110 07                      		pop	es
   236 00000111 C1EB04                  		shr	bx, 4 ; from byte count to paragraph count
   237 00000114 011E[EF01]              		add	[next_segment], bx
   238                                  
   239 00000118 668B7EF8                		mov	edi, [bp-8]	; Remain sector count
   240 0000011C 6609FF                  		or	edi, edi
   241 0000011F 7474                    		jz	short bs_05	; none
   242                                  		
   243 00000121 668B46FC                		mov	eax, [bp-4]	; Next FDT address
   244 00000125 BB007E                  		mov	bx, 7E00h	; FDT Buffer address
   245 00000128 89DE                    		mov	si, bx
   246                                  
   247 0000012A E82800                  		call	read_sector
   248 0000012D 729D                    		jc	short disk_io_error
   249                                  		
   250 0000012F AD                      		lodsw
   251 00000130 3D4644                  		cmp	ax, 'FD' 
   252 00000133 758B                    		jne	short invalid_system_disk ; not a valid FDT!
   253 00000135 AC                      		lodsb
   254 00000136 3C54                    		cmp	al, 'T'
   255 00000138 7586                    		jne	short invalid_system_disk ; not a valid FDT!
   256 0000013A 83C609                  		add	si, 9
   257 0000013D 66AD                    		lodsd	; FDT_NextFDTNum
   258 0000013F 6603460C                		add	eax, [bp+12] ; [bsBeginSector]
   259 00000143 668946FC                		mov	[bp-4], eax  ; Next FDT address (LBA)
   260                                  		;mov	edx, eax
   261                                  		;lodsd	; FDT_SectorCount
   262                                  		;or	eax, eax
   263                                  		;jz	invalid_system_disk ; not a valid FDT!
   264                                  		;mov	ecx, eax ; sector count (in section)
   265                                  		;mov	eax, edi ; [bp-8] ; remain sectors to read
   266                                  		;mov	edi, edx
   267 00000147 66678B0E                		mov	ecx, [esi]
   268 0000014B 09C9                    		or	cx, cx		
   269 0000014D 0F846FFF                		jz	invalid_system_disk
   270 00000151 6697                    		xchg	eax, edi
   271                                  		; EDI = Current FDT address
   272                                  		; EAX = Sector count
   273 00000153 EB9B                    		jmp	short load_startup_file
   274                                  
   275                                  read_sector:	; 25/12/2017 (Read 1 sector)
   276 00000155 B90100                  		mov	cx, 1
   277                                  disk_read:
   278                                  		;mov	byte [bp+retry_count-7C00h], 4
   279 00000158 B204                    		mov	dl, 4 ; retry count
   280                                  disk_read_0:
   281 0000015A 6660                    		pushad
   282                                  lba_read:
   283                                  		;pushad
   284                                  
   285                                  		;mov	di, sp
   286                                  		
   287 0000015C 666A00                  		push	dword 0
   288 0000015F 6650                    		push	eax
   289 00000161 06                      		push	es
   290 00000162 53                      		push	bx
   291 00000163 6A01                    		push 	byte 1
   292 00000165 6A10                    		push	byte 16 ; 10h
   293 00000167 B442                    		mov	ah, 42h
   294                                  		;mov	dl, [bsDriveNumber]
   295 00000169 8A562C                  		mov	dl, [bp+44]
   296 0000016C 89E6                    		mov	si, sp
   297 0000016E CD13                    		int	13h
   298                                  
   299                                  		;pop	eax
   300                                  		;pop	eax
   301                                  		;pop	eax
   302                                  		;pop	eax
   303                                  		;mov	sp, di
   304                                  		
   305                                  		; 30/01/2018
   306 00000170 61                      		popa
   307                                  disk_read_1:
   308 00000171 6661                    		popad
   309 00000173 7305                    		jnc	short disk_read_2
   310                                  		; cf = 1
   311                                  		;dec	byte [retry_count]
   312                                  		;dec	byte [bp+retry_count-7C00h]
   313 00000175 FECA                    		dec	dl ; Retry count
   314 00000177 75E1                    		jnz	short disk_read_0 ; Retry
   315                                  		; cf = 1		
   316 00000179 C3                      		retn
   317                                  disk_read_2:
   318                                  		;add	bx, [bp+6] ; [bsBytesPerSec] ; 512
   319 0000017A 81C30002                		add	bx, 512
   320                                  		;add	bh, 2 ; **
   321 0000017E 730E                    		jnc	short disk_read_3
   322 00000180 8CC3                    		mov	bx, es
   323 00000182 81C30010                		add	bx, 1000h	 
   324 00000186 891E[EF01]              		mov	[next_segment], bx
   325 0000018A 8EC3                    		mov	es, bx
   326 0000018C 31DB                    		xor	bx, bx
   327                                  disk_read_3:
   328 0000018E 6640                    		inc	eax
   329 00000190 49                      		dec	cx
   330 00000191 75C5                    		jnz	short disk_read
   331                                  		;clc 	; ** (128 sectors/cluster!?)
   332 00000193 C3                      		retn
   333                                  
   334                                  		; Filler
   335 00000194 90                      		db	90h
   336                                  
   337                                  bs_05:
   338                                  		; Set TRDOS 386 kernel specific parameters (& signs)
   339                                  		; and
   340                                  		; Launch TRDOS 386 Kernel (Startup/RTS file)
   341                                  
   342 00000195 A1[EF01]                		mov	ax, [next_segment] ; 16 paragraphs after the
   343                                  					  ; start of the last segment
   344                                  					  ; of the kernel file loading
   345                                  					  ; space.
   346                                  					  ; So, (top of) stack will have
   347                                  					  ; 256 bytes or more distance
   348                                  					  ; from the last byte
   349                                  					  ; of the kernel file.	 							
   350                                  					  ; (This will be enough for
   351                                  					  ; TRDOS 386 kernel before 
   352                                  					  ; entering protected mode.)
   353                                  
   354 00000198 FA                      		cli
   355 00000199 8ED0                    		mov	ss, ax
   356 0000019B BCFEFF                  		mov	sp, 0FFFEh			
   357 0000019E FB                      		sti
   358                                  
   359 0000019F 66B8A1010000            		mov     eax, 417
   360                                  
   361 000001A5 BB0010                  		mov     bx, rts_segment ; 1000h
   362                                  
   363 000001A8 8EDB                                    mov     ds, bx
   364 000001AA 8EC3                                    mov     es, bx
   365                                  		;mov	fs, bx
   366                                  		;mov	gs, bx 
   367                                  
   368                                  		;xor	ebx, ebx
   369                                  		;xor	ecx, ecx
   370                                  		;xor	edx, edx
   371                                  		;xor	esi, esi
   372                                  		;xor	edi, edi
   373                                  		;xor	ebp, ebp
   374                                  
   375                                  		; bp = 7C00h
   376                                  
   377                                  		;mov	dl, [bsDriveNumber]
   378 000001AC 8B562C                                  mov	dx, [bp+44] ; DL = Drive number, DH = 0 (CHS)
   379                                  				    ;			 DH = 1 (LBA)
   380                                  
   381                                  		;mov	eax, 417 ; TRDOS boot sector sign for TRDOS386.SYS
   382                                  
   383 000001AF EA00000010              		jmp	rts_segment:0
   384                                  
   385                                  print_msg:
   386                                  		; DS:SI = Error message address (ASCIIZ string)	
   387 000001B4 B40E                    		mov	ah, 0Eh
   388 000001B6 BB0700                  		mov	bx, 7
   389                                  bs_06:
   390 000001B9 AC                      		lodsb
   391 000001BA 84C0                    		test	al, al
   392 000001BC 7404                    		jz	short bs_07
   393 000001BE CD10                    		int	10h
   394 000001C0 EBF7                    		jmp	short bs_06
   395                                  bs_07:
   396 000001C2 C3                      		retn
   397                                  
   398                                  Diskio_err_Msg:
   399 000001C3 0D0A                    		db	0Dh, 0Ah
   400 000001C5 4469736B206572726F-     		db	'Disk error!'
   400 000001CE 7221               
   401 000001D0 0D0A00                  		db	0Dh, 0Ah, 0
   402                                  Inv_disk_Msg:   
   403 000001D3 0D0A                    		db	0Dh, 0Ah
   404 000001D5 496E76616C69642073-     		db	'Invalid system disk!'
   404 000001DE 797374656D20646973-
   404 000001E7 6B21               
   405 000001E9 0D0A00                  		db	0Dh, 0Ah, 0
   406                                  
   407                                  		; Filler
   408 000001EC 04                      		db	4
   409 000001ED 01                      		db	1
   410 000001EE 07                      		db	7
   411                                  
   412                                  next_segment:
   413 000001EF 0010                    		dw	rts_segment
   414                                  
   415                                  		; Filler (Boot Code Date)
   416 000001F1 30322F30322F323031-     		db	'02/02/2018'
   416 000001FA 38                 
   417 000001FB 00                      		db	0
   418                                  
   419                                  		times	508 - ($ - $$) db 0
   420                                  bsReserved1:
   421 000001FC 5452                    		db	'TR'  ; 'Turkish Rational DOS' feature identifier.
   422                                  bootsignature1:
   423 000001FE 55AA                    		db	55h, 0AAh
