     1                                  ; ****************************************************************************
     2                                  ; TRDOS386.ASM (TRDOS 386 Kernel) - v2.0.7 - trhdboot.s
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Last Update: 03/05/2024 (Previous: 12/09/2020)
     5                                  ; ----------------------------------------------------------------------------
     6                                  ; Beginning: 07/09/2020
     7                                  ; ----------------------------------------------------------------------------
     8                                  ; Assembler: NASM version 2.15 (trhdboot.s)
     9                                  ; ----------------------------------------------------------------------------
    10                                  ; Turkish Rational DOS
    11                                  ; Operating System Project v2.0 by ERDOGAN TAN (Beginning: 04/01/2016)
    12                                  ;
    13                                  ; Derived from TRDOS Operating System v2.0 (80386) source code by Erdogan Tan
    14                                  ; trfdboot.s (06/09/2020)
    15                                  ; ****************************************************************************
    16                                  ; Derived from rdhdboot.s (19/05/2018), Retro DOS v2 Hard Disk BS Utility
    17                                  
    18                                  ; TRHDBOOT.ASM
    19                                  ; Turkish Rational DOS
    20                                  ; Disk Operation System v2.0 Project
    21                                  ; Hard Disk (FAT12, FAT16, FAT32 fs) Boot Code
    22                                  ;
    23                                  ; Copyright (C) 2020  Erdogan TAN 
    24                                  ; ****************************************************************************
    25                                  ; assembling: nasm trhdboot.s -l trhdboot.txt -o TRHDBOOT.COM -Z error.txt
    26                                  
    27                                  ; previous version: trhdboot.s v1 -- 18/04/2000
    28                                  
    29                                  ; ----------------------------------------------------------------------------
    30                                  ; equations
    31                                  ; ----------------------------------------------------------------------------
    32                                  
    33                                  ; boot sector parameters
    34                                  
    35                                  bsOemName	equ 3	; ('MSWIN4.1') --> 'TRDOS386'       
    36                                  bsBytesPerSec	equ 11	; 512 (word)
    37                                  bsSecPerClust	equ 13
    38                                  bsResSectors	equ 14
    39                                  bsFATs		equ 16
    40                                  bsRootDirEnts	equ 17
    41                                  bsSectors	equ 19
    42                                  bsMedia		equ 21	; 0F8h
    43                                  bsFATsecs	equ 22
    44                                  bsSecPerTrack	equ 24
    45                                  bsHeads		equ 26
    46                                  bsHidden1	equ 28
    47                                  bsHidden2	equ 30
    48                                  bsHugeSectors	equ 32
    49                                  ; FAT 16 bs & FAT 12 bs
    50                                  bsDriveNumber	equ 36	; 80h
    51                                  bsReserved1	equ 37
    52                                  bsBpbSignature	equ 38	; 29h (byte)                 
    53                                  bsVolumeID	equ 39
    54                                  bsVolumeLabel	equ 43
    55                                  bsFileSysType	equ 54	; 'FAT16   '  (8 bytes)
    56                                  ; FAT 32 bs
    57                                  BPB_FATSz32	equ 36
    58                                  BPB_ExtFlags	equ 40
    59                                  BPB_FSVer	equ 42
    60                                  BPB_RootClus	equ 44
    61                                  BPB_FSInfo	equ 48
    62                                  BPB_BkBootSec	equ 50
    63                                  BPB_Reserved	equ 52
    64                                  BS_DrvNum	equ 64	; 80h
    65                                  BS_Reserved1	equ 65
    66                                  BS_BootSig	equ 66	; 29h (byte)
    67                                  BS_VolID	equ 67
    68                                  BS_VolLab	equ 71
    69                                  BS_FilSysType	equ 82	; 'FAT32   '  (8 bytes)         
    70                                  
    71                                  ; Masterboot / Partition Table at Beginning+1BEh
    72                                  ptBootable      equ 0
    73                                  ptBeginHead     equ 1
    74                                  ptBeginSector   equ 2
    75                                  ptBeginCylinder equ 3
    76                                  ptFileSystemID	equ 4
    77                                  ptEndHead       equ 5
    78                                  ptEndSector     equ 6
    79                                  ptEndCylinder   equ 7
    80                                  ptStartSector   equ 8
    81                                  ptSectors       equ 12
    82                                  
    83                                  partition_table equ 1BEh    
    84                                  
    85                                  ; ----------------------------------------------------------------------------
    86                                  ; code
    87                                  ; ----------------------------------------------------------------------------
    88                                  
    89                                  [BITS 16]
    90                                  [ORG 100h]
    91                                  
    92 00000000 FA                      	cli
    93 00000001 FC                      	cld
    94 00000002 0E                      	push	cs
    95 00000003 17                      	pop	ss
    96 00000004 BCFEFF                  	mov	sp, 0FFFEh
    97 00000007 FB                      	sti
    98                                  
    99                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   100                                  ; see if drive specified
   101                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   102                                  
   103 00000008 BE8000                  	mov	si, 80h			; PSP command tail
   104 0000000B 8A0C                    	mov	cl, [si]
   105 0000000D 08C9                    	or	cl, cl                               
   106 0000000F 7457                    	jz	short T_9		; jump if zero
   107                                  T_1:
   108 00000011 46                      	inc	si
   109                                  
   110 00000012 8A04                    	mov	al, [si]
   111 00000014 3C20                    	cmp	al, ' '			; is it SPACE ?
   112 00000016 7506                    	jne	short T_2
   113                                  
   114 00000018 FEC9                    	dec	cl                                  
   115 0000001A 75F5                    	jnz	short T_1                  
   116 0000001C EB4A                    	jmp	short T_9
   117                                  T_2:
   118 0000001E 46                      	inc	si
   119 0000001F 803C3A                  	cmp	byte [si], ':'
   120 00000022 741D                    	je	short T_3
   121 00000024 803C20                  	cmp	byte [si], ' '
   122 00000027 7618                    	jna	short T_3
   123                                  
   124 00000029 3C68                    	cmp	al, 'h'
   125 0000002B 753B                    	jne	short T_9  	
   126 0000002D 803C64                  	cmp	byte [si], 'd'
   127 00000030 7536                    	jne	short T_9
   128 00000032 46                      	inc	si
   129 00000033 8A04                    	mov	al, [si]
   130 00000035 3C30                    	cmp	al, '0'
   131 00000037 7429                    	je	short T_8
   132 00000039 722D                    	jb	short T_9
   133 0000003B 3C33                    	cmp	al, '3'
   134 0000003D 7623                    	jna	short T_8
   135 0000003F EB27                    	jmp	short T_9
   136                                  T_3:
   137 00000041 3C43                    	cmp	al, 'C'
   138 00000043 7223                    	jb	short T_9
   139 00000045 7414                    	je	short T_6
   140                                  	;cmp	al, 'Z'			; A - Z
   141                                  	;jna	short T_6                   
   142 00000047 3C44                    	cmp	al, 'D'
   143 00000049 7610                    	jna	short T_6
   144 0000004B 3C5A                    	cmp	al, 'Z'
   145 0000004D 7619                    	jna	short T_9
   146                                  T_4:	
   147 0000004F 3C63                    	cmp	al, 'c'			; a - z 
   148 00000051 7215                    	jb	short T_9                  
   149 00000053 7404                    	je	short T_5
   150                                  	;cmp	al, 'z'                           
   151                                  	;ja	short T_9     
   152 00000055 3C64                    	cmp	al, 'd'
   153 00000057 770F                    	ja	short T_9
   154                                  T_5:
   155 00000059 2C20                    	sub	al, 'a'-'A'		; to upper case
   156                                  
   157                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   158                                  ; get drive code
   159                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   160                                  
   161                                  T_6:
   162 0000005B 2C13                    	sub	al, 'C'-'0'
   163                                  T_7:
   164 0000005D A2[D20D]                	mov	[TrDOS_Drive], al	; '0' .. '4'
   165 00000060 EB0F                    	jmp	short T_10
   166                                  T_8:
   167 00000062 46                      	inc	si
   168 00000063 803C20                  	cmp	byte [si], ' '
   169 00000066 76F5                    	jna	short T_7		
   170                                  
   171                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   172                                  ; Write message
   173                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   174                                  
   175                                  T_9:
   176 00000068 BE[9C0B]                	mov	si, TrDOS_Welcome
   177 0000006B E86502                  	call	print_msg
   178                                  	;cmp	cl, 0
   179                                          ;ja	short T_35
   180 0000006E E94B02                  	jmp	T_35
   181                                  
   182                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   183                                  ; get drive parameters
   184                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   185                                  
   186                                  T_10:
   187 00000071 B408                    	mov	ah, 08h
   188                                  	;mov	dl, [TrDOS_Drive]	; drive
   189 00000073 88C2                    	mov	dl, al
   190 00000075 80C250                  	add	dl, 80h -'0'		; make it 80h based 
   191 00000078 8816[980B]              	mov	[drv], dl
   192 0000007C CD13                    	int	13h			; return disk parameters
   193                                  
   194 0000007E 0E                      	push	cs
   195 0000007F 07                      	pop	es			; restore es
   196                                  
   197 00000080 08E4                    	or	ah, ah
   198 00000082 753B                    	jnz	short T_12		; error
   199                                  	
   200 00000084 88C8                    	mov	al, cl
   201 00000086 243F                    	and	al, 63
   202 00000088 A2[B80E]                	mov	[sectors], al
   203 0000008B C0E906                  	shr	cl, 6 
   204 0000008E 86E9                    	xchg	ch, cl
   205 00000090 41                      	inc	cx
   206 00000091 890E[BA0E]              	mov	[cylinders], cx
   207 00000095 FEC6                    	inc	dh
   208 00000097 8836[B90E]              	mov	[heads], dh
   209 0000009B F6E6                    	mul	dh
   210                                  		; ax = heads * spt
   211 0000009D F7E1                    	mul	cx ; * cylinders
   212                                  		; dx:ax = chs limit
   213 0000009F A3[B40E]                	mov	[CHS_limit], ax
   214 000000A2 8916[B60E]              	mov	[CHS_limit+2], dx
   215                                  
   216                                  	; check for (valid) primary dos partition
   217                                  
   218                                  	;mov	byte [RetryCount], 4
   219                                  
   220                                  	;mov	ax, 0201h		; read disk
   221 000000A6 BB[C40E]                	mov	bx, MBR			; location of masterboot code
   222                                  
   223 000000A9 B90100                  	mov	cx, 1			; cylinder = 0
   224                                  					; sector = 1
   225 000000AC B600                    	mov	dh, 0			; head = 0
   226                                  	;mov	dl, [TrDOS_Drive]	; drive 
   227                                  	;add	dl, 80h -'0'		; make it 80h based 
   228 000000AE 8A16[980B]              	mov	dl, [drv]
   229                                  T_11:
   230 000000B2 B80102                  	mov	ax, 0201h
   231 000000B5 CD13                    	int	13h
   232                                  	;jc	short T_12	
   233 000000B7 7311                    	jnc	short T_13		; read masterboot sector, OK
   234                                  
   235 000000B9 FE0E[B10E]              	dec	byte [RetryCount]
   236 000000BD 75F3                    	jnz	short T_11
   237                                  T_12:
   238 000000BF C606[370E]00            	mov	byte [zbyte], 0
   239 000000C4 E80902                  	call	T_37			; write error message
   240 000000C7 E9F201                  	jmp	T_35 			; terminate
   241                                  
   242                                  T_13:
   243 000000CA 813E[C210]55AA          	cmp	word [MBR+510], 0AA55h 
   244 000000D0 75ED                            jne	short T_12
   245                                  
   246 000000D2 BE[8610]                	mov	si, MBR+(partition_table+ptFileSystemID)
   247                                  T_14:
   248 000000D5 E88A02                  	call	validate_primary_dos_partition
   249 000000D8 7312                    	jnc	short T_15	
   250                                   
   251 000000DA 83C610                  	add	si, 16
   252 000000DD 81FE[C610]              	cmp	si, MBR+partition_table+ptFileSystemID+64
   253 000000E1 72F2                    	jb	short T_14
   254                                  
   255 000000E3 BE[7F0E]                	mov	si, TRDOS_fatp_notfound
   256 000000E6 E8EA01                  	call	print_msg
   257 000000E9 E9D001                  	jmp	T_35
   258                                  	
   259                                  T_15:
   260                                  	; valid primary dos partition
   261                                  	; al = FAT type (1,2,3)
   262                                  	; ah = partition type
   263                                  
   264 000000EC A2[B00E]                	mov	byte [fattype], al
   265 000000EF 3C02                    	cmp	al, 2
   266 000000F1 741B                    	je	short T_17 ; FAT16 BS (default offset addr)
   267 000000F3 720E                    	jb	short T_16
   268                                  	; set TRDOS 386 BS pointer for FAT32 BS
   269 000000F5 C706[9403][9803]        	mov	word [trdos386bs], TRDOS_FAT32_hd_bs
   270                                  	; set FS type string
   271 000000FB C706[700E]3332          	mov	word [fattype_str],'32'	; 'FAT32'
   272                                  	; ok.. read boot sector
   273 00000101 EB0B                    	jmp	short T_17
   274                                  T_16:
   275                                  	; set TRDOS 386 BS pointer for FAT12 BS
   276 00000103 C706[9403][9809]        	mov	word [trdos386bs], TRDOS_FAT12_hd_bs
   277 00000109 C606[710E]32            	mov	byte [fattype_str+1],'2' ; 'FAT12'
   278                                  T_17:	
   279 0000010E C606[B10E]05            	mov	byte [RetryCount], 5
   280                                  
   281 00000113 83C604                  	add	si, ptStartSector-ptFileSystemID
   282 00000116 8B04                    	mov	ax, [si]
   283 00000118 8B5402                  	mov	dx, [si+2]
   284 0000011B A3[BC0E]                	mov	[dosp_start], ax
   285 0000011E 8916[BE0E]              	mov	[dosp_start+2], dx
   286 00000122 83C604                  	add	si, ptSectors-ptStartSector
   287 00000125 8B0C                    	mov	cx, [si]
   288 00000127 8B5C02                  	mov	bx, [si+2]
   289 0000012A 890E[C00E]              	mov	[dosp_size], cx
   290 0000012E 891E[C20E]              	mov	[dosp_size+2], bx	
   291 00000132 01C1                    	add	cx, ax
   292 00000134 11D3                    	adc	bx, dx
   293 00000136 7287                    	jc	short T_12
   294                                  
   295 00000138 3B1E[B60E]              	cmp	bx, [CHS_limit+2]	
   296 0000013C BB[C40E]                	mov	bx, bootsector
   297 0000013F 772C                    	ja	short T_20 ; LBA read/write
   298 00000141 7206                    	jb	short T_18
   299 00000143 3B0E[B40E]              	cmp	cx, [CHS_limit]
   300 00000147 7724                    	ja	short T_20
   301                                  T_18:
   302                                  	; CHS read
   303                                  
   304 00000149 83EE0B                  	sub	si, ptSectors-ptBeginHead
   305                                  
   306 0000014C 8A34                    	mov	dh, [si] ; head
   307 0000014E 46                      	inc	si
   308 0000014F 8B0C                    	mov	cx, [si] ; sector
   309                                  		; cl = sector, ch = cylinder
   310                                  	;mov	bx, bootsector
   311 00000151 8A16[980B]              	mov	dl, [drv]
   312                                  
   313 00000155 8836[990B]              	mov	[_dh], dh
   314 00000159 890E[9A0B]              	mov	[_cx], cx
   315                                  T_19:
   316 0000015D B80102                  	mov	ax, 0201h ; read one sector
   317 00000160 CD13                    	int	13h
   318 00000162 733A                    	jnc	short T_22 ; OK
   319 00000164 FE0E[B10E]              	dec	byte [RetryCount]
   320 00000168 75F3                    	jnz	short T_19
   321 0000016A E952FF                  	jmp	T_12
   322                                  T_20:
   323                                  	; LBA read
   324 0000016D C606[B20E]01            	mov	byte [lba], 1
   325                                  	;mov	ax, [dosp_start]
   326                                  	;mov	dx, [dosp_start+2]
   327                                  T_21:
   328                                  	;pusha				; db 60h
   329 00000172 60                      	db	60h
   330                                  	;push 	0                       ; db 6Ah, 00h
   331 00000173 6A00                    	db	6Ah, 0
   332                                  	;push	0                       ; db 6Ah, 00h
   333 00000175 6A00                    	db	6Ah, 0
   334 00000177 52                      	push    dx
   335 00000178 50                      	push    ax
   336 00000179 06                      	push    es
   337 0000017A 53                      	push    bx
   338                                  	;push	1			; db 6Ah, 01h
   339 0000017B 6A01                    	db	6Ah, 01h                     
   340                                  	;push	10h                     ; db 6Ah, 10h
   341 0000017D 6A10                    	db	6Ah, 10h
   342                                  
   343 0000017F 8A16[980B]              	mov     dl, [drv]
   344 00000183 B442                    	mov     ah, 42h
   345 00000185 89E6                    	mov     si, sp
   346 00000187 CD13                    	int     13h
   347                                  
   348                                  	;popa
   349 00000189 61                      	db	61h
   350                                  	;popa
   351 0000018A 61                      	db	61h
   352 0000018B 7311                    	jnc     short T_22
   353                                                  
   354 0000018D FE0E[B10E]                      dec	byte [RetryCount]
   355 00000191 0F842AFF                	jz	T_12
   356                                  
   357 00000195 A1[BC0E]                	mov	ax, [dosp_start]
   358 00000198 8B16[BE0E]              	mov	dx, [dosp_start+2]
   359 0000019C EBD4                    	jmp	short T_21	 
   360                                  
   361                                  T_22:
   362 0000019E 813E[C210]55AA          	cmp	word [bootsector+510], 0AA55h
   363 000001A4 7536                    	jne	short T_23
   364                                  
   365 000001A6 813E[CF0E]0002          	cmp	word [bootsector+bsBytesPerSec], 512
   366 000001AC 752E                    	jne	short T_23
   367                                  
   368                                  	; 03/05/2024 (BugFix)
   369 000001AE 803E[D90E]F8            	cmp	byte [bootsector+bsMedia], 0F8h
   370 000001B3 7527                    	jne	short T_23
   371                                  
   372 000001B5 803E[B00E]02            	cmp	byte [fattype], 2
   373 000001BA 7729                    	ja	short T_24
   374                                  
   375 000001BC 803E[EA0E]29            	cmp	byte [bootsector+bsBpbSignature], 29h
   376 000001C1 7519                    	jne	short T_23
   377 000001C3 66813E[FA0E]464154-     	cmp	dword [bootsector+bsFileSysType], 'FAT1'
   377 000001CB 31                 
   378 000001CC 750E                    	jne	short T_23
   379                                  
   380 000001CE B92B00                  	mov	cx, 54-11 ; byte count to be copied 
   381                                  	
   382 000001D1 A0[FE0E]                	mov	al, [bootsector+bsFileSysType+4]
   383 000001D4 3C36                    	cmp	al, '6'
   384 000001D6 7429                    	je	short T_25
   385                                  
   386 000001D8 3C32                    	cmp	al, '2'
   387 000001DA 7425                    	je	short T_25
   388                                  T_23:
   389 000001DC BE[4A0E]                	mov	si, TrDOS_invalid_bootsector
   390 000001DF E8F100                  	call	print_msg
   391 000001E2 E9D700                  	jmp	T_35
   392                                  
   393                                  T_24:
   394 000001E5 803E[060F]29            	cmp	byte [bootsector+BS_BootSig], 29h
   395 000001EA 75F0                    	jne	short T_23
   396 000001EC 66813E[160F]464154-     	cmp	dword [bootsector+BS_FilSysType], 'FAT3'
   396 000001F4 33                 
   397 000001F5 75E5                    	jne	short T_23
   398 000001F7 803E[1A0F]32            	cmp	byte [bootsector+BS_FilSysType+4], '2'
   399 000001FC 75DE                    	jne	short T_23
   400                                  
   401 000001FE B94700                  	mov	cx, 82-11 ; byte count to be copied 
   402                                  T_25:
   403 00000201 BE[FB0C]                	mov	si, TrDOS_Do_you_want
   404 00000204 E8CC00                  	call	print_msg
   405                                  T_26:
   406                                  	;xor	ax, ax
   407                                  	;int	16h			; wait for keyboard command
   408                                  	;cmp	al, 'y'
   409                                  	;je	short T_27		; retry
   410                                  	;cmp	al, 'Y'
   411                                  	;je	short T_27
   412                                  	;cmp	al, 'n'
   413                                  	;je	short T_35 		; exit
   414                                  	;cmp	al, 'N'
   415                                  	;je	short T_35
   416                                  	;cmp	al, 'C'-40h
   417                                  	;je	short T_35                   
   418                                  	;cmp	al, 27
   419                                  	;je	short T_35
   420                                  	;jmp	short T_26
   421                                  
   422 00000207 E8D800                  	call	get_answer
   423 0000020A 3C59                    	cmp	al, 'Y'
   424 0000020C 7409                    	je	short T_27
   425                                  
   426 0000020E BE[9F0D]                	mov	si, _no_str
   427 00000211 E8BF00                  	call	print_msg
   428 00000214 E9A500                  	jmp	T_35
   429                                  
   430                                  T_27:
   431 00000217 BE[980D]                	mov	si, _yes_str
   432 0000021A E8B600                  	call	print_msg
   433                                  
   434                                  	;mov	si, TrDOS_CRLF
   435                                  	;call	print_msg
   436                                  
   437                                  	; set 'TRDOS386' as OEM name
   438 0000021D 8B3E[9403]              	mov	di, [trdos386bs]
   439 00000221 83C703                  	add	di, bsOemName
   440 00000224 B85452                  	mov	ax, 'TR'
   441 00000227 AB                      	stosw
   442 00000228 B8444F                  	mov	ax, 'DO'
   443 0000022B AB                      	stosw
   444 0000022C B85333                  	mov	ax, 'S3'
   445 0000022F AB                      	stosw
   446 00000230 B83836                  	mov	ax, '86'
   447 00000233 AB                      	stosw
   448                                  	
   449                                  	; DI points to trdos386bs+bsBytesPerSec
   450 00000234 BE[CF0E]                	mov	si, bootsector+bsBytesPerSec
   451                                  	
   452 00000237 F3A4                    	rep	movsb
   453                                  
   454 00000239 BE[A50D]                	mov	si, TrDOS_PressKeyWhenReady
   455 0000023C E89400                  	call	print_msg
   456                                  T_28:
   457 0000023F 31C0                    	xor	ax, ax
   458 00000241 CD16                    	int	16h			; wait for keyboard command
   459 00000243 3C0D                    	cmp	al, 'M'-40h		; Enter (OK) key
   460 00000245 740A                    	je	short T_29		; write
   461 00000247 3C03                    	cmp	al, 'C'-40h
   462 00000249 7471                    	je	short T_35		; no write (exit)
   463 0000024B 3C1B                    	cmp	al, 27
   464 0000024D 746D                    	je	short T_35
   465 0000024F EBEE                    	jmp	short T_28
   466                                  
   467                                  T_29:
   468 00000251 BE[120E]                	mov	si, TrDOS_CRLF
   469 00000254 E87C00                  	call	print_msg
   470                                  T_30:
   471                                  	;xor	ax, ax
   472                                  	;int	1Ah			; get time of day
   473                                  	
   474                                  	;mov	si, volume_id
   475                                  	
   476                                  	;mov	[si], dx
   477                                  	;mov	[si+2], cx		; set unique volume ID
   478                                  	
   479                                  	;mov	ah, 02h			; Return Current Time
   480                                  	;int	1Ah
   481                                  	;xchg	ch, cl
   482                                  	;xchg	dh, dl
   483                                  	
   484                                  	;add	cx, dx  
   485                                  	;add	[si+2], cx
   486                                  		
   487                                  	;mov	ah, 04h			; Return Current Date
   488                                  	;int	1Ah
   489                                  	;xchg	ch, cl
   490                                  	;xchg	dh, dl
   491                                  	
   492                                  	;add	cx, dx  
   493                                  	;add	[si+2], cx
   494                                  
   495                                  	;mov	ax, [vol_id]
   496                                  	;mov	dx, [vol_id+2]
   497                                  
   498 00000257 8B1E[9403]              	mov	bx, [trdos386bs]	; location of boot code
   499                                  	; es: bx = boot sector buffer address
   500                                  	
   501                                  	;mov	si, bx	
   502                                  	;add	si, bsVolumeID
   503                                  
   504                                  	;cmp	byte [fattype], 3
   505                                  	;jne	short T_31
   506                                  
   507                                  	;add	si, BS_VolID-bsVolumeID
   508                                  ;T_31:
   509                                  	;mov	[si], ax
   510                                  	;mov	[si+2], dx
   511                                  
   512 0000025B 803E[B20E]01            	cmp	byte [lba], 1
   513 00000260 731E                    	jnb	short T_32 ; LBA write 		
   514                                  
   515 00000262 8A16[980B]              	mov	dl, [drv] ; drive
   516 00000266 8A36[990B]              	mov	dh, [_dh] ; head
   517 0000026A 8B0E[9A0B]              	mov	cx, [_cx] ; cl = sector, ch = cylinder (low 8 bits)
   518                                  T_31:
   519 0000026E B80103                  	mov	ax, 0301h		; write to disk
   520                                  	; es: bx = boot sector buffer address
   521                                  
   522 00000271 CD13                    	int	13h
   523 00000273 733A                    	jnc	short T_34		; ok
   524                                  
   525                                  	; error
   526                                  
   527 00000275 FE0E[B10E]              	dec	byte [RetryCount]
   528 00000279 75F3                    	jnz	short T_31
   529 0000027B E85200                  	call	T_37
   530 0000027E EB47                    	jmp	short T_36
   531                                  
   532                                  T_32:
   533 00000280 A1[BC0E]                	mov	ax, [dosp_start]
   534 00000283 8B16[BE0E]              	mov	dx, [dosp_start+2]
   535                                  T_33:
   536                                  	;pusha				; db 60h
   537 00000287 60                      	db	60h
   538                                  	;push 	0                       ; db 6Ah, 00h
   539 00000288 6A00                    	db	6Ah, 0
   540                                  	;push	0                       ; db 6Ah, 00h
   541 0000028A 6A00                    	db	6Ah, 0
   542 0000028C 52                      	push    dx
   543 0000028D 50                      	push    ax
   544 0000028E 06                      	push    es
   545 0000028F 53                      	push    bx
   546                                  	;push	1			; db 6Ah, 01h
   547 00000290 6A01                    	db	6Ah, 01h                     
   548                                  	;push	10h                     ; db 6Ah, 10h
   549 00000292 6A10                    	db	6Ah, 10h
   550                                  
   551 00000294 8A16[980B]              	mov     dl, [drv]
   552 00000298 B443                    	mov     ah, 43h ; LBA write
   553 0000029A 30C0                    	xor	al, al ; verify off 
   554 0000029C 89E6                    	mov     si, sp
   555 0000029E CD13                    	int     13h
   556                                  
   557                                  	;popa
   558 000002A0 61                      	db	61h
   559                                  	;popa
   560 000002A1 61                      	db	61h
   561 000002A2 730B                    	jnc     short T_34
   562                                  
   563 000002A4 FE0E[B10E]              	dec	byte [RetryCount]
   564 000002A8 75D6                    	jnz	short T_32
   565 000002AA E82300                  	call	T_37
   566 000002AD EB18                    	jmp	short T_36
   567                                  
   568                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   569                                  ; success. try again ?
   570                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   571                                  
   572                                  T_34:
   573 000002AF 803E[B00E]03            	cmp	byte [fattype], 3 ; FAT32
   574 000002B4 744D                    	je	short T_40		
   575                                  
   576 000002B6 BE[D60D]                	mov	si, TrDOS_disk_WrittenSuccesfully
   577 000002B9 E81700                  	call	print_msg
   578                                  T_35:
   579 000002BC BE[120E]                	mov	si, TrDOS_CRLF
   580 000002BF E81100                  	call	print_msg
   581 000002C2 B8004C                  	mov	ax, 4C00h		; terminate
   582 000002C5 CD21                    	int	21h
   583                                  T_36:
   584                                  	;xor	ax, ax
   585                                  	;int	16h			; wait for keyboard command
   586                                  	;cmp	al, 'y'
   587                                  	;je	short TX_15		; retry
   588                                  	;cmp	al, 'Y'
   589                                  	;je	short TX_15
   590                                  	;cmp	al, 'n'
   591                                  	;je	short T_35 		; exit
   592                                  	;cmp	al, 'N'
   593                                  	;je	short T_35
   594                                  	;cmp	al, 'C'-40h
   595                                  	;je	short T_35                   
   596                                  	;cmp	al, 27
   597                                  	;je	short T_35
   598                                  	;jmp	short T_36
   599                                  
   600 000002C7 E81800                  	call	get_answer
   601 000002CA 3C59                    	cmp	al, 'Y'
   602 000002CC 7483                    	je	short T_29
   603 000002CE EBEC                    	jmp	short T_35
   604                                  
   605                                  T_37:
   606 000002D0 BE[150E]                	mov	si, TrDOS_disk_NotReadyOrError
   607                                  	;;call	print_msg
   608                                  	;;jmp	short T_36
   609                                  	;jmp	short print_msg
   610                                  
   611                                  print_msg:
   612                                  T_38:
   613 000002D3 AC                      	lodsb				; Load byte at DS:SI to AL
   614 000002D4 20C0                    	and	al, al            
   615 000002D6 7409                    	jz	short T_39       
   616 000002D8 B40E                    	mov	ah, 0Eh			
   617 000002DA BB0700                  	mov	bx, 07h             
   618 000002DD CD10                    	int	10h			; BIOS Service func ( ah ) = 0Eh
   619                                  					; Write char as TTY
   620                                  					; AL-char BH-page BL-color
   621 000002DF EBF2                    	jmp     short T_38          
   622                                  T_39:
   623                                  _NO_:
   624 000002E1 C3                      	retn
   625                                  		 
   626                                  get_answer:
   627 000002E2 31C0                    	xor	ax, ax
   628 000002E4 CD16                    	int	16h			; wait for keyboard command
   629 000002E6 3C79                    	cmp	al, 'y'
   630 000002E8 7416                    	je	short _yes		; retry
   631 000002EA 3C59                    	cmp	al, 'Y'
   632 000002EC 7414                    	je	short _YES_
   633 000002EE 3C6E                    	cmp	al, 'n'
   634 000002F0 74EF                    	je	short _NO_ 		; exit
   635 000002F2 3C4E                    	cmp	al, 'N'
   636 000002F4 74EB                    	je	short _NO_
   637 000002F6 3C03                    	cmp	al, 'C'-40h
   638 000002F8 74E7                    	je	short _NO_                 
   639 000002FA 3C1B                    	cmp	al, 27
   640 000002FC 74E3                    	je	short _NO_
   641 000002FE EBE2                    	jmp	short get_answer
   642                                  _yes:
   643 00000300 B059                    	mov	al, 'Y'
   644                                  _YES_:
   645 00000302 C3                      	retn
   646                                  
   647                                  T_40:
   648                                  	; write 2nd sector of FAT32 bs code (1024 bytes)
   649                                  
   650 00000303 C606[B10E]04            	mov	byte [RetryCount], 4
   651 00000308 C606[B00E]00            	mov	byte [fattype], 0
   652                                  
   653 0000030D BB[9805]                	mov	bx, TRDOS_FAT32_hd_bs+512
   654                                  
   655 00000310 803E[B20E]00            	cmp	byte [lba], 0
   656 00000315 7610                    	jna	short T_41
   657                                  
   658 00000317 A1[BC0E]                	mov	ax, [dosp_start]
   659 0000031A 8B16[BE0E]              	mov	dx, [dosp_start+2]
   660 0000031E 83C002                  	add	ax, 2 ; sector 2 in the partition (after FSINFO sector)
   661 00000321 83D200                  	adc	dx, 0
   662 00000324 E960FF                  	jmp	T_33
   663                                  T_41:
   664                                  	; convert FAT32 bootsector+2 address to CHS
   665                                  	;mov	dl, [drv] ; drive
   666                                  	;mov	dh, [_dh] ; head
   667                                  	;mov	cx, [_cx] ; cl = sector, ch = cylinder (low 8 bits)	
   668 00000327 89C8                    	mov	ax, cx
   669 00000329 243F                    	and	al, 63
   670 0000032B 3A06[B80E]              	cmp	al, [sectors]
   671 0000032F 7313                    	jnb	short T_43
   672 00000331 FEC0                    	inc	al
   673 00000333 3A06[B80E]               	cmp	al, [sectors]
   674 00000337 7307                    	jnb	short T_42
   675 00000339 FEC1                    	inc	cl
   676 0000033B FEC1                    	inc	cl
   677 0000033D E92EFF                  	jmp	T_31
   678                                  T_42:
   679 00000340 B001                    	mov	al, 1	; sector 1
   680 00000342 EB02                    	jmp	short T_44
   681                                  T_43:
   682 00000344 B002                    	mov	al, 2	; sector 2
   683                                  T_44:
   684 00000346 FEC6                    	inc	dh
   685 00000348 3A36[B90E]              	cmp	dh, [heads]
   686 0000034C 0F821EFF                	jb	T_31
   687 00000350 28F6                    	sub	dh, dh  ; head 0
   688 00000352 C0E906                  	shr	cl, 6
   689 00000355 86E9                    	xchg	ch, cl
   690 00000357 41                      	inc	cx	; next cylinder
   691                                  	;and	cx, 1023
   692                                  	;cmp	cx, [cylinders]
   693                                  	;jnb	short T_37
   694 00000358 86CD                    	xchg	cl, ch
   695 0000035A C0E106                  	shl	cl, 6
   696 0000035D 08C1                    	or	cl, al	
   697 0000035F E90CFF                  	jmp	T_31
   698                                  
   699                                  validate_primary_dos_partition:
   700                                  	
   701                                  	; INPUT:
   702                                  	;   si = partition table entry offset + file system ID 
   703                                  	; OUTPUT:
   704                                  	;   cf = 0 -> ah = primary DOS partition ID
   705                                  	;			 (01h,04h,06h,0Bh,0Ch,0Eh)		
   706                                  	;	      al = FAT type 
   707                                  	;			1 = FAT12
   708                                  	;			2 = FAT16
   709                                  	;			3 = FAT32
   710                                  	;
   711                                  	;   cf = 1 -> not a primary DOS partition	
   712                                  
   713 00000362 28C0                    	sub	al, al ; mov al, 0
   714                                  
   715 00000364 8A24                    	mov 	ah, [si]
   716                                  
   717 00000366 80FC01                  	cmp	ah, 01h	; FAT12 partition
   718 00000369 7228                    	jb	short V_5 ; 0
   719 0000036B 741E                    	je	short V_3
   720                                  V_0:
   721 0000036D FEC0                    	inc	al  ; mov al, 1
   722                                  
   723 0000036F 80FC06                  	cmp 	ah, 06h ; FAT16 CHS partition (>=32MB)
   724 00000372 7709                    	ja	short V_2
   725 00000374 7415                    	je	short V_3
   726                                  
   727 00000376 80FC04                  	cmp	ah, 04h	; FAT16 CHS partition (< 32MB)
   728 00000379 7410                    	je	short V_3
   729                                  V_1:
   730 0000037B F9                      	stc
   731 0000037C C3                      	retn
   732                                  V_2:
   733 0000037D FEC0                    	inc	al ; mov al, 2
   734                                  
   735 0000037F 80FC0C                  	cmp	ah, 0Ch	; FAT32 LBA partition
   736 00000382 7407                    	je	short V_3
   737 00000384 7708                    	ja	short V_4
   738                                  
   739 00000386 80FC0B                  	cmp	ah, 0Bh	; FAT32 CHS partition 
   740 00000389 7208                    	jb	short V_5
   741                                  V_3:
   742 0000038B FEC0                    	inc	al ; 0->1, 1->2, 2->3
   743 0000038D C3                      	retn 
   744                                  V_4:
   745 0000038E 80FC0E                  	cmp	ah, 0Eh	; FAT16 LBA partition
   746 00000391 75E8                    	jne	short V_1
   747                                  	;mov	al, 2
   748                                  V_5:
   749 00000393 C3                      	retn
   750                                  
   751                                  ; ----------------------------------------------------------------------------
   752                                  ; initialized data
   753                                  ; ----------------------------------------------------------------------------
   754                                  
   755                                  align 2
   756                                  
   757                                  trdos386bs:
   758 00000394 [9807]                  	dw TRDOS_FAT16_hd_bs
   759 00000396 0000                    	dw 0
   760                                  
   761                                  ;volume_id:
   762                                  ;	dd 0
   763                                  
   764                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   765                                  ;  FAT boot sector code
   766                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   767                                  
   768                                  TRDOS_FAT32_hd_bs:
   769 00000398 <bin 400h>              	incbin	'FAT32_BS.BIN' ; 27/04/2024
   770                                  TRDOS_FAT16_hd_bs: 
   771 00000798 <bin 200h>              	incbin	'FAT16_BS.BIN' ; 26/12/2017
   772                                  TRDOS_FAT12_hd_bs: 
   773 00000998 <bin 200h>              	incbin	'FAT12_BS.BIN' ; 26/12/2017
   774                                  
   775                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   776                                  ;  messages
   777                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   778                                  
   779 00000B98 00                      drv:	db 0
   780                                  
   781                                  ;sectors: db 0
   782                                  ;heads	: db 0
   783                                  ;cylinders: dw 0
   784                                  
   785 00000B99 00                      _dh:	db 0
   786 00000B9A 0000                    _cx:	dw 0
   787                                  
   788                                  TrDOS_Welcome:
   789 00000B9C 0D0A                    	db 0Dh, 0Ah
   790 00000B9E 54522D444F53203338-     	db 'TR-DOS 386 v2 Hard Disk Boot Sector Update Utility '
   790 00000BA7 362076322048617264-
   790 00000BB0 204469736B20426F6F-
   790 00000BB9 7420536563746F7220-
   790 00000BC2 557064617465205574-
   790 00000BCB 696C69747920       
   791 00000BD1 0D0A                    	db 0Dh, 0Ah
   792 00000BD3 286329204572646F67-     	db '(c) Erdogan TAN 2020-2024'
   792 00000BDC 616E2054414E203230-
   792 00000BE5 32302D32303234     
   793 00000BEC 0D0A                    	db 0Dh,0Ah
   794 00000BEE 0D0A                    	db 0Dh,0Ah
   795 00000BF0 55736167653A207472-     	db 'Usage: trhdboot <drive> '
   795 00000BF9 6864626F6F74203C64-
   795 00000C02 726976653E20       
   796 00000C08 0D0A0D0A                	db 0Dh,0Ah, 0Dh, 0Ah
   797 00000C0C 4472697665206E616D-     	db 'Drive names: '
   797 00000C15 65733A20           
   798 00000C19 0D0A                    	db 0Dh, 0Ah
   799 00000C1B 20686430206F722043-     	db ' hd0 or C: ..for primary dos partition on 1st disk '
   799 00000C24 3A202E2E666F722070-
   799 00000C2D 72696D61727920646F-
   799 00000C36 732070617274697469-
   799 00000C3F 6F6E206F6E20317374-
   799 00000C48 206469736B20       
   800 00000C4E 0D0A                    	db 0Dh, 0Ah
   801 00000C50 20686431206F722044-     	db ' hd1 or D: ..for primary dos partition on 2nd disk '
   801 00000C59 3A202E2E666F722070-
   801 00000C62 72696D61727920646F-
   801 00000C6B 732070617274697469-
   801 00000C74 6F6E206F6E20326E64-
   801 00000C7D 206469736B20       
   802 00000C83 0D0A                    	db 0Dh, 0Ah
   803 00000C85 206864322020202020-     	db ' hd2       ..for primary dos partition on 3rd disk '
   803 00000C8E 20202E2E666F722070-
   803 00000C97 72696D61727920646F-
   803 00000CA0 732070617274697469-
   803 00000CA9 6F6E206F6E20337264-
   803 00000CB2 206469736B20       
   804 00000CB8 0D0A                    	db 0Dh, 0Ah
   805 00000CBA 206864332020202020-     	db ' hd3       ..for primary dos partition on 4th disk '
   805 00000CC3 20202E2E666F722070-
   805 00000CCC 72696D61727920646F-
   805 00000CD5 732070617274697469-
   805 00000CDE 6F6E206F6E20347468-
   805 00000CE7 206469736B20       
   806 00000CED 0D0A00                  	db 0Dh, 0Ah, 0
   807                                  
   808 00000CF0 30332F30352F323032-     	db '03/05/2024'
   808 00000CF9 34                 
   809 00000CFA 00                      	db 0
   810                                  
   811                                  TrDOS_Do_you_want:
   812 00000CFB 0D0A                    	db 0Dh, 0Ah
   813 00000CFD 5741524E494E472021-     	db "WARNING ! ", 0Dh, 0Ah 
   813 00000D06 200D0A             
   814 00000D09 28496620796F752073-     	db "(If you say 'Yes', MSDOS or WINDOWS will not be bootable on this disk !) "
   814 00000D12 61792027596573272C-
   814 00000D1B 204D53444F53206F72-
   814 00000D24 2057494E444F575320-
   814 00000D2D 77696C6C206E6F7420-
   814 00000D36 626520626F6F746162-
   814 00000D3F 6C65206F6E20746869-
   814 00000D48 73206469736B202129-
   814 00000D51 20                 
   815 00000D52 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   816 00000D56 446F20796F75207761-     	db "Do you want to update boot sector to TRDOS 386 v2 format ? (Y/N) "
   816 00000D5F 6E7420746F20757064-
   816 00000D68 61746520626F6F7420-
   816 00000D71 736563746F7220746F-
   816 00000D7A 205452444F53203338-
   816 00000D83 3620763220666F726D-
   816 00000D8C 6174203F2028592F4E-
   816 00000D95 2920               
   817 00000D97 00                      	db 0
   818                                  
   819                                  _yes_str:
   820 00000D98 59455320                	db 'YES '
   821 00000D9C 0D0A00                  	db 0Dh, 0Ah, 0
   822                                  _no_str:
   823 00000D9F 4E4F20                  	db 'NO '
   824 00000DA2 0D0A00                  	db 0Dh, 0Ah, 0
   825                                  
   826                                  TrDOS_PressKeyWhenReady:
   827 00000DA5 0D0A                    	db 0Dh, 0Ah
   828 00000DA7 507265737320456E74-     	db 'Press Enter to write boot sector on disk hd'
   828 00000DB0 657220746F20777269-
   828 00000DB9 746520626F6F742073-
   828 00000DC2 6563746F72206F6E20-
   828 00000DCB 6469736B206864     
   829                                  TrDOS_Drive:
   830 00000DD2 3F2E2000                	db '?. ', 0
   831                                  
   832                                  TrDOS_disk_WrittenSuccesfully:
   833 00000DD6 0D0A                    	db 0Dh, 0Ah
   834 00000DD8 426F6F742073656374-     	db 'Boot sector successfully updated to TRDOS 386 v2 format...'
   834 00000DE1 6F7220737563636573-
   834 00000DEA 7366756C6C79207570-
   834 00000DF3 646174656420746F20-
   834 00000DFC 5452444F5320333836-
   834 00000E05 20763220666F726D61-
   834 00000E0E 742E2E2E           
   835                                  TrDOS_CRLF:
   836 00000E12 0D0A00                  	db 0Dh, 0Ah, 0
   837                                  
   838                                  TrDOS_disk_NotReadyOrError:
   839 00000E15 0D0A                    	db 0Dh, 0Ah
   840 00000E17 4469736B206572726F-     	db 'Disk error or drive not ready ! '
   840 00000E20 72206F722064726976-
   840 00000E29 65206E6F7420726561-
   840 00000E32 6479202120         
   841 00000E37 54727920616761696E-     zbyte:	db 'Try again ? (Y/N) '
   841 00000E40 203F2028592F4E2920 
   842 00000E49 00                      	db 0
   843                                  
   844                                  TrDOS_invalid_bootsector:
   845 00000E4A 0D0A                    	db 0Dh, 0Ah
   846 00000E4C 496E76616C69642062-     	db 'Invalid boot sector (not a valid FAT'
   846 00000E55 6F6F7420736563746F-
   846 00000E5E 7220286E6F74206120-
   846 00000E67 76616C696420464154 
   847                                  fattype_str:
   848 00000E70 313620667320646973-     	db '16 fs disk) ! '
   848 00000E79 6B29202120         
   849 00000E7E 00                      	db 0
   850                                  
   851                                  TRDOS_fatp_notfound:
   852 00000E7F 0D0A                    	db 0Dh, 0Ah
   853 00000E81 4D425220646F657320-     	db 'MBR does not contain a primary DOS partition ! '	
   853 00000E8A 6E6F7420636F6E7461-
   853 00000E93 696E2061207072696D-
   853 00000E9C 61727920444F532070-
   853 00000EA5 6172746974696F6E20-
   853 00000EAE 2120               
   854                                  fattype:
   855 00000EB0 00                      	db 0
   856                                  RetryCount:
   857 00000EB1 04                      	db 4
   858                                  
   859 00000EB2 00                      lba:	db 0
   860                                  
   861 00000EB3 90                      align 4
   862                                  
   863                                  CHS_limit: 
   864 00000EB4 0000                    	dw 0
   865                                  	;dw 0
   866                                  
   867 00000EB6 A101                    sign:	dw 417
   868                                  
   869                                  ; ----------------------------------------------------------------------------
   870                                  ; uninitialized data
   871                                  ; ----------------------------------------------------------------------------
   872                                  
   873                                  bss_start:
   874                                  
   875                                  ABSOLUTE bss_start
   876                                  
   877                                  alignb 4
   878                                  
   879 00000EB8 ??                      sectors: resb 1
   880 00000EB9 ??                      heads:	 resb 1
   881 00000EBA ????                    cylinders: resw 1
   882                                  
   883 00000EBC ????????                dosp_start: resd 1
   884 00000EC0 ????????                dosp_size:  resd 1	 
   885                                  
   886                                  MBR:
   887                                  bootsector:
   888 00000EC4 <res 200h>              	resb 512
   889                                  
   890                                  end_bss:
