     1                                  ; ****************************************************************************
     2                                  ; TRDOS386.ASM (TRDOS 386 Kernel) - v2.0.2 - trhdboot.s
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Last Update: 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.lst -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[CF0D]                	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[B40E]                	mov	[sectors], al
   203 0000008B C0E906                  	shr	cl, 6 
   204 0000008E 86E9                    	xchg	ch, cl
   205 00000090 41                      	inc	cx
   206 00000091 890E[B60E]              	mov	[cylinders], cx
   207 00000095 FEC6                    	inc	dh
   208 00000097 8836[B50E]              	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[B00E]                	mov	[CHS_limit], ax
   214 000000A2 8916[B20E]              	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[C00E]                	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[AE0E]              	dec	byte [RetryCount]
   236 000000BD 75F3                    	jnz	short T_11
   237                                  T_12:
   238 000000BF C606[340E]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[BE10]55AA          	cmp	word [MBR+510], 0AA55h 
   244 000000D0 75ED                            jne	short T_12
   245                                  
   246 000000D2 BE[8210]                	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[C210]              	cmp	si, MBR+partition_table+ptFileSystemID+64
   253 000000E1 72F2                    	jb	short T_14
   254                                  
   255 000000E3 BE[7C0E]                	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[AD0E]                	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[6D0E]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[6E0E]32            	mov	byte [fattype_str+1],'2' ; 'FAT12'
   278                                  T_17:	
   279 0000010E C606[AE0E]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[B80E]                	mov	[dosp_start], ax
   285 0000011E 8916[BA0E]              	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[BC0E]              	mov	[dosp_size], cx
   290 0000012E 891E[BE0E]              	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[B20E]              	cmp	bx, [CHS_limit+2]	
   296 0000013C BB[C00E]                	mov	bx, bootsector
   297 0000013F 772C                    	ja	short T_20 ; LBA read/write
   298 00000141 7206                    	jb	short T_18
   299 00000143 3B0E[B00E]              	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[AE0E]              	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[AF0E]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[AE0E]                      dec	byte [RetryCount]
   355 00000191 0F842AFF                	jz	T_12
   356                                  
   357 00000195 A1[B80E]                	mov	ax, [dosp_start]
   358 00000198 8B16[BA0E]              	mov	dx, [dosp_start+2]
   359 0000019C EBD4                    	jmp	T_21	 
   360                                  
   361                                  T_22:
   362 0000019E 813E[BE10]55AA          	cmp	word [bootsector+510], 0AA55h
   363 000001A4 7536                    	jne	short T_23
   364                                  
   365 000001A6 813E[CB0E]0002          	cmp	word [bootsector+bsBytesPerSec], 512
   366 000001AC 752E                    	jne	short T_23
   367                                  
   368 000001AE C606[D50E]F8            	mov	byte [bootsector+bsMedia], 0F8h
   369 000001B3 7527                    	jne	short T_23
   370                                  
   371 000001B5 803E[AD0E]02            	cmp	byte [fattype], 2
   372 000001BA 7729                    	ja	short T_24
   373                                  
   374 000001BC 803E[E60E]29            	cmp	byte [bootsector+bsBpbSignature], 29h
   375 000001C1 7519                    	jne	short T_23
   376 000001C3 66813E[F60E]464154-     	cmp	dword [bootsector+bsFileSysType], 'FAT1'
   376 000001CB 31                 
   377 000001CC 750E                    	jne	short T_23
   378                                  
   379 000001CE B92B00                  	mov	cx, 54-11 ; byte count to be copied 
   380                                  	
   381 000001D1 A0[FA0E]                	mov	al, [bootsector+bsFileSysType+4]
   382 000001D4 3C36                    	cmp	al, '6'
   383 000001D6 7429                    	je	short T_25
   384                                  
   385 000001D8 3C32                    	cmp	al, '2'
   386 000001DA 7425                    	je	short T_25
   387                                  T_23:
   388 000001DC BE[470E]                	mov	si, TrDOS_invalid_bootsector
   389 000001DF E8F100                  	call	print_msg
   390 000001E2 E9D700                  	jmp	T_35
   391                                  T_24:
   392 000001E5 803E[020F]29            	cmp	byte [bootsector+BS_BootSig], 29h
   393 000001EA 75F0                    	jne	short T_23
   394 000001EC 66813E[120F]464154-     	cmp	dword [bootsector+BS_FilSysType], 'FAT3'
   394 000001F4 33                 
   395 000001F5 75E5                    	jne	short T_23
   396 000001F7 803E[160F]32            	cmp	byte [bootsector+BS_FilSysType+4], '2'
   397 000001FC 75DE                    	jne	short T_23
   398                                  
   399 000001FE B94700                  	mov	cx, 82-11 ; byte count to be copied 
   400                                  T_25:
   401 00000201 BE[F80C]                	mov	si, TrDOS_Do_you_want
   402 00000204 E8CC00                  	call	print_msg
   403                                  T_26:
   404                                  	;xor	ax, ax
   405                                  	;int	16h			; wait for keyboard command
   406                                  	;cmp	al, 'y'
   407                                  	;je	short T_27		; retry
   408                                  	;cmp	al, 'Y'
   409                                  	;je	short T_27
   410                                  	;cmp	al, 'n'
   411                                  	;je	short T_35 		; exit
   412                                  	;cmp	al, 'N'
   413                                  	;je	short T_35
   414                                  	;cmp	al, 'C'-40h
   415                                  	;je	short T_35                   
   416                                  	;cmp	al, 27
   417                                  	;je	short T_35
   418                                  	;jmp	short T_26
   419                                  
   420 00000207 E8D800                  	call	get_answer
   421 0000020A 3C59                    	cmp	al, 'Y'
   422 0000020C 7409                    	je	short T_27
   423                                  
   424 0000020E BE[9C0D]                	mov	si, _no_str
   425 00000211 E8BF00                  	call	print_msg
   426                                  
   427 00000214 E9A500                  	jmp	T_35
   428                                  T_27:
   429 00000217 BE[950D]                	mov	si, _yes_str
   430 0000021A E8B600                  	call	print_msg
   431                                  
   432                                  	;mov	si, TrDOS_CRLF
   433                                  	;call	print_msg
   434                                  
   435                                  	; set 'TRDOS386' as OEM name
   436 0000021D 8B3E[9403]              	mov	di, [trdos386bs]
   437 00000221 83C703                  	add	di, bsOemName
   438 00000224 B85452                  	mov	ax, 'TR'
   439 00000227 AB                      	stosw
   440 00000228 B8444F                  	mov	ax, 'DO'
   441 0000022B AB                      	stosw
   442 0000022C B85333                  	mov	ax, 'S3'
   443 0000022F AB                      	stosw
   444 00000230 B83836                  	mov	ax, '86'
   445 00000233 AB                      	stosw
   446                                  	
   447                                  	; DI points to trdos386bs+bsBytesPerSec
   448 00000234 BE[CB0E]                	mov	si, bootsector+bsBytesPerSec
   449                                  	
   450 00000237 F3A4                    	rep	movsb
   451                                  
   452 00000239 BE[A20D]                	mov	si, TrDOS_PressKeyWhenReady
   453 0000023C E89400                  	call	print_msg
   454                                  T_28:
   455 0000023F 31C0                    	xor	ax, ax
   456 00000241 CD16                    	int	16h			; wait for keyboard command
   457 00000243 3C0D                    	cmp	al, 'M'-40h		; Enter (OK) key
   458 00000245 740A                    	je	short T_29		; write
   459 00000247 3C03                    	cmp	al, 'C'-40h
   460 00000249 7471                    	je	short T_35		; no write (exit)
   461 0000024B 3C1B                    	cmp	al, 27
   462 0000024D 746D                    	je	short T_35
   463 0000024F EBEE                    	jmp	short T_28
   464                                  
   465                                  T_29:
   466 00000251 BE[0F0E]                	mov	si, TrDOS_CRLF
   467 00000254 E87C00                  	call	print_msg
   468                                  T_30:
   469                                  	;xor	ax, ax
   470                                  	;int	1Ah			; get time of day
   471                                  	
   472                                  	;mov	si, volume_id
   473                                  	
   474                                  	;mov	[si], dx
   475                                  	;mov	[si+2], cx		; set unique volume ID
   476                                  	
   477                                  	;mov	ah, 02h			; Return Current Time
   478                                  	;int	1Ah
   479                                  	;xchg	ch, cl
   480                                  	;xchg	dh, dl
   481                                  	
   482                                  	;add	cx, dx  
   483                                  	;add	[si+2], cx
   484                                  		
   485                                  	;mov	ah, 04h			; Return Current Date
   486                                  	;int	1Ah
   487                                  	;xchg	ch, cl
   488                                  	;xchg	dh, dl
   489                                  	
   490                                  	;add	cx, dx  
   491                                  	;add	[si+2], cx
   492                                  
   493                                  	;mov	ax, [vol_id]
   494                                  	;mov	dx, [vol_id+2]
   495                                  
   496 00000257 8B1E[9403]              	mov	bx, [trdos386bs]	; location of boot code
   497                                  	; es: bx = boot sector buffer address
   498                                  	
   499                                  	;mov	si, bx	
   500                                  	;add	si, bsVolumeID
   501                                  
   502                                  	;cmp	byte [fattype], 3
   503                                  	;jne	short T_31
   504                                  
   505                                  	;add	si, BS_VolID-bsVolumeID
   506                                  ;T_31:
   507                                  	;mov	[si], ax
   508                                  	;mov	[si+2], dx
   509                                  
   510 0000025B 803E[AF0E]01            	cmp	byte [lba], 1
   511 00000260 731E                    	jnb	short T_32 ; LBA write 		
   512                                  
   513 00000262 8A16[980B]              	mov	dl, [drv] ; drive
   514 00000266 8A36[990B]              	mov	dh, [_dh] ; head
   515 0000026A 8B0E[9A0B]              	mov	cx, [_cx] ; cl = sector, ch = cylinder (low 8 bits)
   516                                  T_31:
   517 0000026E B80103                  	mov	ax, 0301h		; write to disk
   518                                  	; es: bx = boot sector buffer address
   519                                  
   520 00000271 CD13                    	int	13h
   521 00000273 733A                    	jnc	short T_34		; ok
   522                                  
   523                                  	; error
   524                                  
   525 00000275 FE0E[AE0E]              	dec	byte [RetryCount]
   526 00000279 75F3                    	jnz	short T_31
   527 0000027B E85200                  	call	T_37
   528 0000027E EB47                    	jmp	short T_36
   529                                  
   530                                  T_32:
   531 00000280 A1[B80E]                	mov	ax, [dosp_start]
   532 00000283 8B16[BA0E]              	mov	dx, [dosp_start+2]
   533                                  T_33:
   534                                  	;pusha				; db 60h
   535 00000287 60                      	db	60h
   536                                  	;push 	0                       ; db 6Ah, 00h
   537 00000288 6A00                    	db	6Ah, 0
   538                                  	;push	0                       ; db 6Ah, 00h
   539 0000028A 6A00                    	db	6Ah, 0
   540 0000028C 52                      	push    dx
   541 0000028D 50                      	push    ax
   542 0000028E 06                      	push    es
   543 0000028F 53                      	push    bx
   544                                  	;push	1			; db 6Ah, 01h
   545 00000290 6A01                    	db	6Ah, 01h                     
   546                                  	;push	10h                     ; db 6Ah, 10h
   547 00000292 6A10                    	db	6Ah, 10h
   548                                  
   549 00000294 8A16[980B]              	mov     dl, [drv]
   550 00000298 B443                    	mov     ah, 43h ; LBA write
   551 0000029A 30C0                    	xor	al, al ; verify off 
   552 0000029C 89E6                    	mov     si, sp
   553 0000029E CD13                    	int     13h
   554                                  
   555                                  	;popa
   556 000002A0 61                      	db	61h
   557                                  	;popa
   558 000002A1 61                      	db	61h
   559 000002A2 730B                    	jnc     short T_34
   560                                  
   561 000002A4 FE0E[AE0E]              	dec	byte [RetryCount]
   562 000002A8 75D6                    	jnz	short T_32
   563 000002AA E82300                  	call	T_37
   564 000002AD EB18                    	jmp	short T_36
   565                                  
   566                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   567                                  ; success. try again ?
   568                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   569                                  
   570                                  T_34:
   571 000002AF 803E[AD0E]03            	cmp	byte [fattype], 3 ; FAT32
   572 000002B4 744D                    	je	short T_40		
   573                                  
   574 000002B6 BE[D30D]                	mov	si, TrDOS_disk_WrittenSuccesfully
   575 000002B9 E81700                  	call	print_msg
   576                                  T_35:
   577 000002BC BE[0F0E]                	mov	si, TrDOS_CRLF
   578 000002BF E81100                  	call	print_msg
   579 000002C2 B8004C                  	mov	ax, 4C00h		; terminate
   580 000002C5 CD21                    	int	21h
   581                                  T_36:
   582                                  	;xor	ax, ax
   583                                  	;int	16h			; wait for keyboard command
   584                                  	;cmp	al, 'y'
   585                                  	;je	short TX_15		; retry
   586                                  	;cmp	al, 'Y'
   587                                  	;je	short TX_15
   588                                  	;cmp	al, 'n'
   589                                  	;je	short T_35 		; exit
   590                                  	;cmp	al, 'N'
   591                                  	;je	short T_35
   592                                  	;cmp	al, 'C'-40h
   593                                  	;je	short T_35                   
   594                                  	;cmp	al, 27
   595                                  	;je	short T_35
   596                                  	;jmp	short T_36
   597                                  
   598 000002C7 E81800                  	call	get_answer
   599 000002CA 3C59                    	cmp	al, 'Y'
   600 000002CC 7483                    	je	short T_29
   601 000002CE EBEC                    	jmp	short T_35
   602                                  
   603                                  T_37:
   604 000002D0 BE[120E]                	mov	si, TrDOS_disk_NotReadyOrError
   605                                  	;;call	print_msg
   606                                  	;;jmp	short T_36
   607                                  	;jmp	short print_msg
   608                                  
   609                                  print_msg:
   610                                  T_38:
   611 000002D3 AC                      	lodsb				; Load byte at DS:SI to AL
   612 000002D4 20C0                    	and	al, al            
   613 000002D6 7409                    	jz	short T_39       
   614 000002D8 B40E                    	mov	ah, 0Eh			
   615 000002DA BB0700                  	mov	bx, 07h             
   616 000002DD CD10                    	int	10h			; BIOS Service func ( ah ) = 0Eh
   617                                  					; Write char as TTY
   618                                  					; AL-char BH-page BL-color
   619 000002DF EBF2                    	jmp     short T_38          
   620                                  T_39:
   621                                  _NO_:
   622 000002E1 C3                      	retn
   623                                  		 
   624                                  get_answer:
   625 000002E2 31C0                    	xor	ax, ax
   626 000002E4 CD16                    	int	16h			; wait for keyboard command
   627 000002E6 3C79                    	cmp	al, 'y'
   628 000002E8 7416                    	je	short _yes		; retry
   629 000002EA 3C59                    	cmp	al, 'Y'
   630 000002EC 7414                    	je	short _YES_
   631 000002EE 3C6E                    	cmp	al, 'n'
   632 000002F0 74EF                    	je	short _NO_ 		; exit
   633 000002F2 3C4E                    	cmp	al, 'N'
   634 000002F4 74EB                    	je	short _NO_
   635 000002F6 3C03                    	cmp	al, 'C'-40h
   636 000002F8 74E7                    	je	short _NO_                 
   637 000002FA 3C1B                    	cmp	al, 27
   638 000002FC 74E3                    	je	short _NO_
   639 000002FE EBE2                    	jmp	short get_answer
   640                                  _yes:
   641 00000300 B059                    	mov	al, 'Y'
   642                                  _YES_:
   643 00000302 C3                      	retn
   644                                  
   645                                  T_40:
   646                                  	; write 2nd sector of FAT32 bs code (1024 bytes)
   647                                  
   648 00000303 C606[AE0E]04            	mov	byte [RetryCount], 4
   649 00000308 C606[AD0E]00            	mov	byte [fattype], 0
   650                                  
   651 0000030D BB[9805]                	mov	bx, TRDOS_FAT32_hd_bs+512
   652                                  
   653 00000310 803E[AF0E]00            	cmp	byte [lba], 0
   654 00000315 7610                    	jna	short T_41
   655                                  
   656 00000317 A1[B80E]                	mov	ax, [dosp_start]
   657 0000031A 8B16[BA0E]              	mov	dx, [dosp_start+2]
   658 0000031E 83C002                  	add	ax, 2 ; sector 2 in the partition (after FSINFO sector)
   659 00000321 83D200                  	adc	dx, 0
   660 00000324 E960FF                  	jmp	T_33
   661                                  T_41:
   662                                  	; convert FAT32 bootsector+2 address to CHS
   663                                  	;mov	dl, [drv] ; drive
   664                                  	;mov	dh, [_dh] ; head
   665                                  	;mov	cx, [_cx] ; cl = sector, ch = cylinder (low 8 bits)	
   666 00000327 89C8                    	mov	ax, cx
   667 00000329 243F                    	and	al, 63
   668 0000032B 3A06[B40E]              	cmp	al, [sectors]
   669 0000032F 7313                    	jnb	short T_43
   670 00000331 FEC0                    	inc	al
   671 00000333 3A06[B40E]               	cmp	al, [sectors]
   672 00000337 7307                    	jnb	short T_42
   673 00000339 FEC1                    	inc	cl
   674 0000033B FEC1                    	inc	cl
   675 0000033D E92EFF                  	jmp	T_31
   676                                  T_42:
   677 00000340 B001                    	mov	al, 1	; sector 1
   678 00000342 EB02                    	jmp	short T_44
   679                                  T_43:
   680 00000344 B002                    	mov	al, 2	; sector 2
   681                                  T_44:
   682 00000346 FEC6                    	inc	dh
   683 00000348 3A36[B50E]              	cmp	dh, [heads]
   684 0000034C 0F821EFF                	jb	T_31
   685 00000350 28F6                    	sub	dh, dh  ; head 0
   686 00000352 C0E906                  	shr	cl, 6
   687 00000355 86E9                    	xchg	ch, cl
   688 00000357 41                      	inc	cx	; next cylinder
   689                                  	;and	cx, 1023
   690                                  	;cmp	cx, [cylinders]
   691                                  	;jnb	short T_37
   692 00000358 86CD                    	xchg	cl, ch
   693 0000035A C0E106                  	shl	cl, 6
   694 0000035D 08C1                    	or	cl, al	
   695 0000035F E90CFF                  	jmp	T_31
   696                                  
   697                                  validate_primary_dos_partition:
   698                                  	
   699                                  	; INPUT:
   700                                  	;   si = partition table entry offset + file system ID 
   701                                  	; OUTPUT:
   702                                  	;   cf = 0 -> ah = primary DOS partition ID
   703                                  	;			 (01h,04h,06h,0Bh,0Ch,0Eh)		
   704                                  	;	      al = FAT type 
   705                                  	;			1 = FAT12
   706                                  	;			2 = FAT16
   707                                  	;			3 = FAT32
   708                                  	;
   709                                  	;   cf = 1 -> not a primary DOS partition	
   710                                  
   711 00000362 28C0                    	sub	al, al ; mov al, 0
   712                                  
   713 00000364 8A24                    	mov 	ah, [si]
   714                                  
   715 00000366 80FC01                  	cmp	ah, 01h	; FAT12 partition
   716 00000369 7228                    	jb	short V_5 ; 0
   717 0000036B 741E                    	je	short V_3
   718                                  V_0:
   719 0000036D FEC0                    	inc	al  ; mov al, 1
   720                                  
   721 0000036F 80FC06                  	cmp 	ah, 06h ; FAT16 CHS partition (>=32MB)
   722 00000372 7709                    	ja	short V_2
   723 00000374 7415                    	je	short V_3
   724                                  
   725 00000376 80FC04                  	cmp	ah, 04h	; FAT16 CHS partition (< 32MB)
   726 00000379 7410                    	je	short V_3
   727                                  V_1:
   728 0000037B F9                      	stc
   729 0000037C C3                      	retn
   730                                  V_2:
   731 0000037D FEC0                    	inc	al ; mov al, 2
   732                                  
   733 0000037F 80FC0C                  	cmp	ah, 0Ch	; FAT32 LBA partition
   734 00000382 7407                    	je	short V_3
   735 00000384 7708                    	ja	short V_4
   736                                  
   737 00000386 80FC0B                  	cmp	ah, 0Bh	; FAT32 CHS partition 
   738 00000389 7208                    	jb	short V_5
   739                                  V_3:
   740 0000038B FEC0                    	inc	al ; 0->1, 1->2, 2->3
   741 0000038D C3                      	retn 
   742                                  V_4:
   743 0000038E 80FC0E                  	cmp	ah, 0Eh	; FAT16 LBA partition
   744 00000391 75E8                    	jne	short V_1
   745                                  	;mov	al, 2
   746                                  V_5:
   747 00000393 C3                      	retn
   748                                  
   749                                  ; ----------------------------------------------------------------------------
   750                                  ; initialized data
   751                                  ; ----------------------------------------------------------------------------
   752                                  
   753                                  align 2
   754                                  
   755                                  trdos386bs:
   756 00000394 [9807]                  	dw TRDOS_FAT16_hd_bs
   757 00000396 0000                    	dw 0
   758                                  
   759                                  ;volume_id:
   760                                  ;	dd 0
   761                                  
   762                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   763                                  ;  FAT boot sector code
   764                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   765                                  
   766                                  TRDOS_FAT32_hd_bs:
   767 00000398 <incbin>                	incbin	'FAT32_BS.BIN'
   768                                  TRDOS_FAT16_hd_bs: 
   769 00000798 <incbin>                	incbin	'FAT16_BS.BIN'
   770                                  TRDOS_FAT12_hd_bs: 
   771 00000998 <incbin>                	incbin	'FAT12_BS.BIN'
   772                                  
   773                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   774                                  ;  messages
   775                                  ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   776                                  
   777 00000B98 00                      drv:	db 0
   778                                  
   779                                  ;sectors: db 0
   780                                  ;heads	: db 0
   781                                  ;cylinders: dw 0
   782                                  
   783 00000B99 00                      _dh:	db 0
   784 00000B9A 0000                    _cx:	dw 0
   785                                  
   786                                  TrDOS_Welcome:
   787 00000B9C 0D0A                    	db 0Dh, 0Ah
   788 00000B9E 54522D444F53203338-     	db 'TR-DOS 386 v2 Hard Disk Boot Sector Update Utility '
   788 00000BA7 362076322048617264-
   788 00000BB0 204469736B20426F6F-
   788 00000BB9 7420536563746F7220-
   788 00000BC2 557064617465205574-
   788 00000BCB 696C69747920       
   789 00000BD1 0D0A                    	db 0Dh, 0Ah
   790 00000BD3 286329204572646F67-     	db '(c) Erdogan TAN - 2020'
   790 00000BDC 616E2054414E202D20-
   790 00000BE5 32303230           
   791 00000BE9 0D0A                    	db 0Dh,0Ah
   792 00000BEB 0D0A                    	db 0Dh,0Ah
   793 00000BED 55736167653A207472-     	db 'Usage: trhdboot <drive> '
   793 00000BF6 6864626F6F74203C64-
   793 00000BFF 726976653E20       
   794 00000C05 0D0A0D0A                	db 0Dh,0Ah, 0Dh, 0Ah
   795 00000C09 4472697665206E616D-     	db 'Drive names: '
   795 00000C12 65733A20           
   796 00000C16 0D0A                    	db 0Dh, 0Ah
   797 00000C18 20686430206F722043-     	db ' hd0 or C: ..for primary dos partition on 1st disk '
   797 00000C21 3A202E2E666F722070-
   797 00000C2A 72696D61727920646F-
   797 00000C33 732070617274697469-
   797 00000C3C 6F6E206F6E20317374-
   797 00000C45 206469736B20       
   798 00000C4B 0D0A                    	db 0Dh, 0Ah
   799 00000C4D 20686431206F722044-     	db ' hd1 or D: ..for primary dos partition on 2nd disk '
   799 00000C56 3A202E2E666F722070-
   799 00000C5F 72696D61727920646F-
   799 00000C68 732070617274697469-
   799 00000C71 6F6E206F6E20326E64-
   799 00000C7A 206469736B20       
   800 00000C80 0D0A                    	db 0Dh, 0Ah
   801 00000C82 206864322020202020-     	db ' hd2       ..for primary dos partition on 3rd disk '
   801 00000C8B 20202E2E666F722070-
   801 00000C94 72696D61727920646F-
   801 00000C9D 732070617274697469-
   801 00000CA6 6F6E206F6E20337264-
   801 00000CAF 206469736B20       
   802 00000CB5 0D0A                    	db 0Dh, 0Ah
   803 00000CB7 206864332020202020-     	db ' hd3       ..for primary dos partition on 4th disk '
   803 00000CC0 20202E2E666F722070-
   803 00000CC9 72696D61727920646F-
   803 00000CD2 732070617274697469-
   803 00000CDB 6F6E206F6E20347468-
   803 00000CE4 206469736B20       
   804 00000CEA 0D0A00                  	db 0Dh, 0Ah, 0
   805                                  
   806 00000CED 31322F30392F323032-     	db '12/09/2020'
   806 00000CF6 30                 
   807 00000CF7 00                      	db 0
   808                                  
   809                                  TrDOS_Do_you_want:
   810 00000CF8 0D0A                    	db 0Dh, 0Ah
   811 00000CFA 5741524E494E472021-     	db "WARNING ! ", 0Dh, 0Ah 
   811 00000D03 200D0A             
   812 00000D06 28496620796F752073-     	db "(If you say 'Yes', MSDOS or WINDOWS will not be bootable on this disk !) "
   812 00000D0F 61792027596573272C-
   812 00000D18 204D53444F53206F72-
   812 00000D21 2057494E444F575320-
   812 00000D2A 77696C6C206E6F7420-
   812 00000D33 626520626F6F746162-
   812 00000D3C 6C65206F6E20746869-
   812 00000D45 73206469736B202129-
   812 00000D4E 20                 
   813 00000D4F 0D0A0D0A                	db 0Dh, 0Ah, 0Dh, 0Ah
   814 00000D53 446F20796F75207761-     	db "Do you want to update boot sector to TRDOS 386 v2 format ? (Y/N) "
   814 00000D5C 6E7420746F20757064-
   814 00000D65 61746520626F6F7420-
   814 00000D6E 736563746F7220746F-
   814 00000D77 205452444F53203338-
   814 00000D80 3620763220666F726D-
   814 00000D89 6174203F2028592F4E-
   814 00000D92 2920               
   815 00000D94 00                      	db 0
   816                                  
   817                                  _yes_str:
   818 00000D95 59455320                	db 'YES '
   819 00000D99 0D0A00                  	db 0Dh, 0Ah, 0
   820                                  _no_str:
   821 00000D9C 4E4F20                  	db 'NO '
   822 00000D9F 0D0A00                  	db 0Dh, 0Ah, 0
   823                                  
   824                                  TrDOS_PressKeyWhenReady:
   825 00000DA2 0D0A                    	db 0Dh, 0Ah
   826 00000DA4 507265737320456E74-     	db 'Press Enter to write boot sector on disk hd'
   826 00000DAD 657220746F20777269-
   826 00000DB6 746520626F6F742073-
   826 00000DBF 6563746F72206F6E20-
   826 00000DC8 6469736B206864     
   827                                  TrDOS_Drive:
   828 00000DCF 3F2E2000                	db '?. ', 0
   829                                  
   830                                  TrDOS_disk_WrittenSuccesfully:
   831 00000DD3 0D0A                    	db 0Dh, 0Ah
   832 00000DD5 426F6F742073656374-     	db 'Boot sector successfully updated to TRDOS 386 v2 format...'
   832 00000DDE 6F7220737563636573-
   832 00000DE7 7366756C6C79207570-
   832 00000DF0 646174656420746F20-
   832 00000DF9 5452444F5320333836-
   832 00000E02 20763220666F726D61-
   832 00000E0B 742E2E2E           
   833                                  TrDOS_CRLF:
   834 00000E0F 0D0A00                  	db 0Dh, 0Ah, 0
   835                                  
   836                                  TrDOS_disk_NotReadyOrError:
   837 00000E12 0D0A                    	db 0Dh, 0Ah
   838 00000E14 4469736B206572726F-     	db 'Disk error or drive not ready ! '
   838 00000E1D 72206F722064726976-
   838 00000E26 65206E6F7420726561-
   838 00000E2F 6479202120         
   839 00000E34 54727920616761696E-     zbyte:	db 'Try again ? (Y/N) '
   839 00000E3D 203F2028592F4E2920 
   840 00000E46 00                      	db 0
   841                                  
   842                                  TrDOS_invalid_bootsector:
   843 00000E47 0D0A                    	db 0Dh, 0Ah
   844 00000E49 496E76616C69642062-     	db 'Invalid boot sector (not a valid FAT'
   844 00000E52 6F6F7420736563746F-
   844 00000E5B 7220286E6F74206120-
   844 00000E64 76616C696420464154 
   845                                  fattype_str:
   846 00000E6D 313620667320646973-     	db '16 fs disk) ! '
   846 00000E76 6B29202120         
   847 00000E7B 00                      	db 0
   848                                  
   849                                  TRDOS_fatp_notfound:
   850 00000E7C 0D0A                    	db 0Dh, 0Ah
   851 00000E7E 4D425220646F657320-     	db 'MBR does not contain a primary DOS partition ! '	
   851 00000E87 6E6F7420636F6E7461-
   851 00000E90 696E2061207072696D-
   851 00000E99 61727920444F532070-
   851 00000EA2 6172746974696F6E20-
   851 00000EAB 2120               
   852                                  fattype:
   853 00000EAD 00                      	db 0
   854                                  RetryCount:
   855 00000EAE 04                      	db 4
   856                                  
   857 00000EAF 00                      lba:	db 0
   858                                  
   859                                  align 4
   860                                  
   861                                  CHS_limit: 
   862 00000EB0 0000                    	dw 0
   863                                  	;dw 0
   864                                  
   865 00000EB2 A101                    sign:	dw 417
   866                                  
   867                                  ; ----------------------------------------------------------------------------
   868                                  ; uninitialized data
   869                                  ; ----------------------------------------------------------------------------
   870                                  
   871                                  bss_start:
   872                                  
   873                                  ABSOLUTE bss_start
   874                                  
   875                                  alignb 4
   876                                  
   877 00000EB4 <res 00000001>          sectors: resb 1
   878 00000EB5 <res 00000001>          heads:	 resb 1
   879 00000EB6 <res 00000002>          cylinders: resw 1
   880                                  
   881 00000EB8 <res 00000004>          dosp_start: resd 1
   882 00000EBC <res 00000004>          dosp_size:  resd 1	 
   883                                  
   884                                  MBR:
   885                                  bootsector:
   886 00000EC0 <res 00000200>          	resb 512
   887                                  
   888                                  end_bss:
