     1                                  ; ****************************************************************************
     2                                  ; mount386.s (mount1.s) - by Erdogan Tan - 08/05/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 386 v1 - mount -- mount file system
     5                                  ;
     6                                  ; [ Last Modification: 08/05/2022 ]
     7                                  ;
     8                                  ; ****************************************************************************
     9                                  ; (/etc/mount)
    10                                  
    11                                  ; mount0.s - Retro UNIX 8086 v1 (16 bit version of 'mount1.s')
    12                                  ; mount1.s - Retro UNIX 386 v1 (& v1.1 & v1.2)
    13                                  
    14                                  ; UNIX v1 system calls
    15                                  _rele 	equ 0
    16                                  _exit 	equ 1
    17                                  _fork 	equ 2
    18                                  _read 	equ 3
    19                                  _write	equ 4
    20                                  _open	equ 5
    21                                  _close 	equ 6
    22                                  _wait 	equ 7
    23                                  _creat 	equ 8
    24                                  _link 	equ 9
    25                                  _unlink	equ 10
    26                                  _exec	equ 11
    27                                  _chdir	equ 12
    28                                  _time 	equ 13
    29                                  _mkdir 	equ 14
    30                                  _chmod	equ 15
    31                                  _chown	equ 16
    32                                  _break	equ 17
    33                                  _stat	equ 18
    34                                  _seek	equ 19
    35                                  _tell 	equ 20
    36                                  _mount	equ 21
    37                                  _umount	equ 22
    38                                  _setuid	equ 23
    39                                  _getuid	equ 24
    40                                  _stime	equ 25
    41                                  _quit	equ 26	
    42                                  _intr	equ 27
    43                                  _fstat	equ 28
    44                                  _emt 	equ 29
    45                                  _mdate 	equ 30
    46                                  _stty 	equ 31
    47                                  _gtty	equ 32
    48                                  _ilgins	equ 33
    49                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    50                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    51                                  _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
    52                                  
    53                                  ;;;
    54                                  ESCKey equ 1Bh
    55                                  EnterKey equ 0Dh
    56                                  
    57                                  %macro sys 1-4
    58                                      ; 03/09/2015	
    59                                      ; 13/04/2015
    60                                      ; Retro UNIX 386 v1 system call.		
    61                                      %if %0 >= 2   
    62                                  	mov ebx, %2
    63                                  	%if %0 >= 3    
    64                                  	    mov ecx, %3
    65                                  	    %if %0 = 4
    66                                  	       mov edx, %4   
    67                                  	    %endif
    68                                  	%endif
    69                                      %endif
    70                                      mov eax, %1
    71                                      int 30h	   
    72                                  %endmacro
    73                                  
    74                                  ; Retro UNIX 386 v1 system call format:
    75                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    76                                  
    77                                  [BITS 32] ; 32-bit intructions (for 80386 protected mode)
    78                                  
    79                                  [ORG 0] 
    80                                  
    81                                  START_CODE:
    82                                  	; 08/05/2022
    83 00000000 59                      	pop	ecx ; ecx = number of arguments
    84                                  	;
    85 00000001 58                      	pop	eax ; eax = argument 0 = executable file name
    86                                  	;
    87                                  	;cmp	ecx, 3
    88 00000002 80F903                  	cmp	cl, 3
    89 00000005 752D                    	jne	short mnt_0
    90                                  
    91 00000007 5F                      	pop	edi ; argument 1 = device name
    92 00000008 5D                      	pop	ebp ; argument 2 = mounting directory
    93                                  
    94 00000009 89EE                    	mov	esi, ebp
    95                                  	; 08/05/2022
    96                                  	;lodsd	; lodsd gives gpf if dir name length < 4
    97 0000000B AC                      	lodsb  ; *!
    98 0000000C 3C2F                    	cmp	al, '/'
    99 0000000E 7532                    	jne	short mnt_2
   100                                  mnt_u:
   101 00000010 AC                      	lodsb
   102 00000011 3C75                    	cmp	al, 'u'
   103 00000013 750C                    	jne	short mnt_m
   104 00000015 AC                      	lodsb
   105 00000016 3C73                    	cmp	al, 's'
   106 00000018 7528                    	jne	short mnt_2
   107 0000001A AC                      	lodsb
   108 0000001B 3C72                    	cmp	al, 'r'
   109 0000001D 7410                    	je	short mnt_1
   110 0000001F EB21                    	jmp	short mnt_2
   111                                  mnt_m:
   112 00000021 3C6D                    	cmp	al, 'm'
   113 00000023 751D                    	jne	short mnt_2
   114 00000025 AC                      	lodsb
   115 00000026 3C6E                    	cmp	al, 'n'
   116 00000028 7518                    	jne	short mnt_2
   117 0000002A AC                      	lodsb
   118 0000002B 3C74                    	cmp	al, 't'
   119 0000002D 7513                    	jne	short mnt_2
   120                                  
   121                                  ;	; 08/05/2022
   122                                  ;	; 'pwd' command compatibility restriction !
   123                                  ;	; ---
   124                                  ;	; Only '/usr' or '/mnt' directories can be used
   125                                  ;	;  with current mount version (otherwise current
   126                                  ;	; 'pwd' will not recognize mounting directory
   127                                  ;	;  as correct).
   128                                  ;
   129                                  ;	; short way...
   130                                  ;	cmp	eax, '/usr'
   131                                  ;	je	short mnt_1
   132                                  ;	cmp	eax, '/mnt'
   133                                  ;	jne	short mnt_2
   134                                  mnt_1:
   135 0000002F 803E00                  	cmp	byte [esi], 0 ;
   136 00000032 765D                    	jna	short mnt_5 ; '/usr' or '/mnt', continue
   137                                  
   138                                  	; neither '/usr' or '/mnt'
   139                                  
   140                                  	; print usage message and then exit
   141                                  mnt_0:
   142 00000034 B8[03010000]            	mov	eax, usage_msg
   143                                  
   144 00000039 EB7A                    	jmp	mnt_7
   145                                  
   146                                  ;p_msg_exit:
   147                                  ;	call	print_msg
   148                                  ;exit:
   149                                  	sys	_exit
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62                              <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64                              <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 0000003B B801000000          <1>  mov eax, %1
    71 00000040 CD30                <1>  int 30h
   150                                  ;hang:
   151                                  ;	nop
   152                                  ;	jmp	short hang
   153                                  
   154                                  mnt_2:
   155                                  	; long way...
   156                                  	sys	_stat, ebp, stbuf
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62 00000042 89EB                <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64 00000044 B9[1C020000]        <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 00000049 B812000000          <1>  mov eax, %1
    71 0000004E CD30                <1>  int 30h
   157 00000050 726F                    	jc	short mnt_dir_err
   158                                  
   159 00000052 668B15[1C020000]        	mov	dx, [stbuf] ; inode number
   160                                  	
   161                                  	sys	_stat, usr, stbuf
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62 00000059 BB[F9000000]        <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64 0000005E B9[1C020000]        <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 00000063 B812000000          <1>  mov eax, %1
    71 00000068 CD30                <1>  int 30h
   162 0000006A 7209                    	jc	short mnt_4
   163                                  	
   164                                  	; check if it is '/usr' inode
   165 0000006C 663B15[1C020000]        	cmp	dx, [stbuf] ; same inode number
   166 00000073 741C                    	je	short mnt_5
   167                                  mnt_4:
   168                                  	sys	_stat, mnt, stbuf
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62 00000075 BB[FE000000]        <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64 0000007A B9[1C020000]        <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 0000007F B812000000          <1>  mov eax, %1
    71 00000084 CD30                <1>  int 30h
   169 00000086 7239                    	jc	short mnt_dir_err
   170                                  
   171                                  	; check if it is '/mnt' inode
   172 00000088 663B15[1C020000]        	cmp	dx, [stbuf] ; same inode number
   173 0000008F 7530                    	jne	short mnt_dir_err
   174                                  mnt_5:
   175                                  	; only superuser (root) can do mount/umount
   176                                  	sys	_getuid
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62                              <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64                              <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 00000091 B818000000          <1>  mov eax, %1
    71 00000096 CD30                <1>  int 30h
   177 00000098 21C0                    	and	eax, eax ; uid = 0 -> root
   178 0000009A 7407                    	jz	short mnt_6
   179 0000009C B8[7B010000]            	mov	eax, deny_msg
   180 000000A1 EB12                    	jmp	short mnt_7
   181                                  mnt_6:
   182                                  	; edi = device name address
   183                                  	; ebp = mounting directory path/name address
   184                                   	;
   185                                  	sys	_mount, edi, ebp
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62 000000A3 89FB                <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64 000000A5 89E9                <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 000000A7 B815000000          <1>  mov eax, %1
    71 000000AC CD30                <1>  int 30h
   186 000000AE 7218                    	jc	short mnt_err
   187                                  	;
   188 000000B0 B8[12020000]            	mov	eax, ok_msg
   189                                  mnt_7:
   190 000000B5 E825000000              	call	print_msg
   191                                  exit:
   192                                  	sys	_exit
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62                              <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64                              <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 000000BA B801000000          <1>  mov eax, %1
    71 000000BF CD30                <1>  int 30h
   193                                  ;hang:
   194                                  ;	nop
   195                                  ;	jmp	short hang
   196                                  
   197                                  mnt_dir_err:
   198 000000C1 B8[B9010000]            	mov	eax, dir_err_msg
   199 000000C6 EBED                    	jmp	short mnt_7
   200                                  
   201                                  mnt_err:
   202                                  	; Temporary! 08/05/2022
   203                                  	; Lets do not recognize 'invalid file system'
   204                                  	; error for now! (for current version and for
   205                                  	; current kernel response to sysmount system call)
   206                                  	;
   207                                  	; ((DOS FAT file system disk or another/new/old
   208                                  	;   type retro unix fs or invalid free map size
   209                                  	;   or invalid/unexpected superblock content))
   210                                  	;
   211                                  	; Note: (08/05/2022)
   212                                  	;   [drv.size] is calculated with cylinders-1
   213                                    	;   for floppy disks ('diskinit.s' ,'set_disk_parms')
   214                                  	;   So, free map size and [drv.size]/8 is not same.
   215                                  	;   This is sysmount error return reason
   216                                  	;   (as a bug in current Retro Unix 386 kernel).
   217                                  	
   218 000000C8 29DB                    	sub	ebx, ebx
   219                                  	sys	_geterr	; (get last error in eax)
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62                              <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64                              <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 000000CA B824000000          <1>  mov eax, %1
    71 000000CF CD30                <1>  int 30h
   220                                  	
   221                                  	; (eax = error code from sysmount system call)
   222 000000D1 83F81C                  	cmp	eax, 28 ; ERR_INV_FS ('sysmnt_5:', 'u7.s')
   223 000000D4 7502                    	jne	short mnt_8
   224                                  	;'invalid fs/superblock !' error
   225                                  	; (exit without 'oK.' message) 
   226 000000D6 EBE2                    	jmp	exit
   227                                  mnt_8:
   228 000000D8 B8[AD010000]            	mov	eax, err_msg
   229 000000DD EBD6                    	jmp	short mnt_7
   230                                  
   231                                  ;-----------------------------------------------------------------
   232                                  
   233                                  print_msg:
   234                                  	; 08/05/2022
   235                                  	; eax = asciiz string address
   236 000000DF 89C2                    	mov	edx, eax
   237 000000E1 4A                      	dec	edx
   238                                  nextchr:
   239 000000E2 42                      	inc	edx
   240 000000E3 803A00                  	cmp	byte [edx], 0
   241 000000E6 77FA                    	ja	short nextchr
   242                                  	;cmp	[edx], 0Dh
   243                                  	;ja	short nextchr
   244 000000E8 29C2                    	sub	edx, eax
   245                                  	; edx = asciiz string length
   246                                  	;
   247                                  	sys	_write, 1, eax
    58                              <1> 
    59                              <1> 
    60                              <1> 
    61                              <1>  %if %0 >= 2
    62 000000EA BB01000000          <1>  mov ebx, %2
    63                              <1>  %if %0 >= 3
    64 000000EF 89C1                <1>  mov ecx, %3
    65                              <1>  %if %0 = 4
    66                              <1>  mov edx, %4
    67                              <1>  %endif
    68                              <1>  %endif
    69                              <1>  %endif
    70 000000F1 B804000000          <1>  mov eax, %1
    71 000000F6 CD30                <1>  int 30h
   248                                  	;
   249 000000F8 C3                      	retn
   250                                  
   251                                  ;-----------------------------------------------------------------
   252                                  ;  data - initialized data
   253                                  ;-----------------------------------------------------------------
   254                                  
   255                                  ; 08/05/2022
   256                                  
   257                                  ; default mounting directories (for current retro unix version)
   258 000000F9 2F75737200              usr:	db '/usr', 0
   259 000000FE 2F6D6E7400              mnt:	db '/mnt', 0
   260                                  
   261                                  usage_msg:
   262 00000103 0D0A                    	db 0Dh, 0Ah
   263 00000105 55736167653A206D6F-     	db "Usage: mount <block device name> </usr> "
   263 0000010E 756E74203C626C6F63-
   263 00000117 6B2064657669636520-
   263 00000120 6E616D653E203C2F75-
   263 00000129 73723E20           
   264 0000012D 0D0A                    	db 0Dh, 0Ah
   265 0000012F 202020206F72206D6F-     	db "    or mount <block device name> </mnt> "
   265 00000138 756E74203C626C6F63-
   265 00000141 6B2064657669636520-
   265 0000014A 6E616D653E203C2F6D-
   265 00000153 6E743E20           
   266 00000157 0D0A                    	db 0Dh, 0Ah
   267 00000159 0D0A                    	db 0Dh, 0Ah
   268 0000015B 4578616D706C653A20-     	db "Example: mount /dev/fd1 /usr "
   268 00000164 6D6F756E74202F6465-
   268 0000016D 762F666431202F7573-
   268 00000176 7220               
   269 00000178 0D0A00                  	db 0Dh, 0Ah, 0
   270                                  
   271                                  deny_msg:
   272 0000017B 0D0A                    	db 0Dh, 0Ah	
   273 0000017D 4D6F756E743A204F6E-     	db "Mount: Only superuser/root can mount devices!"	
   273 00000186 6C7920737570657275-
   273 0000018F 7365722F726F6F7420-
   273 00000198 63616E206D6F756E74-
   273 000001A1 206465766963657321 
   274 000001AA 0D0A00                   	db 0Dh, 0Ah, 0  		
   275                                  err_msg:
   276 000001AD 0D0A                    	db 0Dh, 0Ah
   277 000001AF 4572726F722120          	db 'Error! '
   278 000001B6 0D0A00                  	db 0Dh, 0Ah, 0
   279                                  dir_err_msg:
   280 000001B9 0D0A                    	db 0Dh, 0Ah
   281 000001BB 4D6F756E7420446972-     	db 'Mount Directory Error! '
   281 000001C4 6563746F7279204572-
   281 000001CD 726F722120         
   282 000001D2 0D0A                    	db 0Dh, 0Ah
   283 000001D4 284D6F756E74696E67-     	db "(Mounting directory must exist "
   283 000001DD 206469726563746F72-
   283 000001E6 79206D757374206578-
   283 000001EF 69737420           
   284 000001F3 616E6420626520272F-     	dd "and be '/usr' or '/mnt'.) ",
   284 000001FC 75737227206F722027-
   284 00000205 2F6D6E74272E292000-
   284 0000020E 00                 
   285 0000020F 0D0A00                  	db 0Dh, 0Ah, 0
   286                                  ok_msg:
   287 00000212 0D0A                    	db 0Dh, 0Ah
   288 00000214 4F4B2E20                	db 'OK. '
   289                                  nextline:
   290 00000218 0D0A00                  	db 0Dh, 0Ah, 0
   291                                  
   292                                  ;-----------------------------------------------------------------
   293                                  ;  bss - uninitialized data
   294                                  ;-----------------------------------------------------------------	
   295                                  
   296 0000021B 90                      align 4
   297                                  
   298                                  bss_start:
   299                                  
   300                                  ABSOLUTE bss_start
   301                                  
   302                                  ; 08/05/2022
   303                                  
   304                                  ;stbuf: resb 34 ; (Retro UNIX v1 and v1.1)
   305 0000021C <res 42h>               stbuf:	resb 66	; stat(us) buffer
   306                                  		; (Retro UNIX v1, v1.1 and v1.2)
   307                                  
   308                                  ;-----------------------------------------------------------------
