     1                                  ; ****************************************************************************
     2                                  ; mount8086.s (mount0.s) - by Erdogan Tan - 08/05/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - mount -- mount file system
     5                                  ;
     6                                  ; [ Last Modification: 09/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                                  ;           %if	%0 >= 4 ; 11/03/2022
    67                                  ;		mov edx, %4
    68                                  ;           %endif
    69                                  ;       %endif
    70                                  ;   %endif
    71                                  ;   mov eax, %1
    72                                  ;   int 30h
    73                                  ;%endmacro
    74                                  
    75                                  %macro sys 1-4
    76                                      ; Retro UNIX 8086 v1 system call.
    77                                      %if %0 >= 2   
    78                                          mov bx, %2
    79                                          %if %0 >= 3
    80                                              mov cx, %3
    81                                              %if %0 >= 4
    82                                                 mov dx, %4
    83                                              %endif
    84                                          %endif
    85                                      %endif
    86                                      mov ax, %1
    87                                      int 20h
    88                                  %endmacro
    89                                  
    90                                  ;; Retro UNIX 386 v1 system call format:
    91                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    92                                  
    93                                  ;; 11/03/2022
    94                                  ;; Note: Above 'sys' macro has limitation about register positions;
    95                                  ;;	ebx, ecx, edx registers must not be used after their
    96                                  ;;	positions in sys macro.
    97                                  ;; for example:
    98                                  ;;	'sys _write, 1, msg, ecx' is defective, because
    99                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
   100                                  ;; correct order may be:
   101                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   102                                  
   103                                  ; Retro UNIX 8086 v1 system call format:
   104                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   105                                  
   106                                  ; ----------------------------------------------------------------------------
   107                                  
   108                                  [BITS 16] ; 16-bit intructions (8086/8088 - Real Mode)
   109                                  
   110                                  [ORG 0] 
   111                                  
   112                                  START_CODE:
   113                                  	; 09/05/2022 (16 bit code)
   114                                  	; 08/05/2022
   115 00000000 59                      	pop	cx ; cx = number of arguments
   116                                  	;
   117 00000001 58                      	pop	ax ; ax = argument 0 = executable file name
   118                                  	;
   119                                  	;cmp	cx, 3
   120 00000002 80F903                  	cmp	cl, 3
   121 00000005 7504                    	jne	short mnt_0
   122                                  
   123 00000007 5F                      	pop	di ; argument 1 = device name
   124 00000008 5D                      	pop	bp ; argument 2 = mounting directory
   125                                  
   126 00000009 89EE                    	mov	si, bp
   127                                  
   128                                  	; 09/05/2022
   129                                  
   130                                  ;; 'pwd' command compatibility restriction !
   131                                  ;;	; ---
   132                                  ;;	; Only '/usr' or '/mnt' directories can be used
   133                                  ;;	;  with current mount version (otherwise current
   134                                  ;;	; 'pwd' will not recognize mounting directory
   135                                  ;;	;  as correct).
   136                                  ;
   137                                  ;	; short way...
   138                                  ;
   139                                  ;	lodsw
   140                                  ;	cmp	al, '/'
   141                                  ;	jne	short mnt_5
   142                                  ;	cmp	ah, 'u'
   143                                  ;	jne	short mnt_m
   144                                  ;mnt_u:
   145                                  ;	lodsw
   146                                  ;	cmp	ax, 'sr'
   147                                  ;	je	short mnt_1
   148                                  ;	jmp	short mnt_2
   149                                  ;mnt_m:
   150                                  ;	cmp	ah, 'm'
   151                                  ;	jne	short mnt_2
   152                                  ;	lodsw
   153                                  ;	cmp	ax, 'nt'
   154                                  ;	jne	short mnt_2
   155                                  ;mnt_1:
   156                                  ;	cmp	byte [si], 0 ;
   157                                  ;	jna	short mnt_5 ; '/usr' or '/mnt', continue
   158                                  ;
   159                                  ;	; neither '/usr' or '/mnt'
   160                                  
   161                                  	; print usage message and then exit
   162                                  mnt_0:
   163 0000000B B8[7200]                	mov	ax, usage_msg
   164                                  
   165 0000000E EB38                    	jmp	short mnt_7
   166                                  
   167                                  ;p_msg_exit:
   168                                  ;	call	print_msg
   169                                  ;exit:
   170                                  	sys	_exit
    76                              <1> 
    77                              <1>  %if %0 >= 2
    78                              <1>  mov bx, %2
    79                              <1>  %if %0 >= 3
    80                              <1>  mov cx, %3
    81                              <1>  %if %0 >= 4
    82                              <1>  mov dx, %4
    83                              <1>  %endif
    84                              <1>  %endif
    85                              <1>  %endif
    86 00000010 B80100              <1>  mov ax, %1
    87 00000013 CD20                <1>  int 20h
   171                                  ;hang:
   172                                  ;	nop
   173                                  ;	jmp	short hang
   174                                  
   175                                  mnt_2:
   176                                  	; long way...
   177                                  	sys	_stat, bp, stbuf
    76                              <1> 
    77                              <1>  %if %0 >= 2
    78 00000015 89EB                <1>  mov bx, %2
    79                              <1>  %if %0 >= 3
    80 00000017 B9[2401]            <1>  mov cx, %3
    81                              <1>  %if %0 >= 4
    82                              <1>  mov dx, %4
    83                              <1>  %endif
    84                              <1>  %endif
    85                              <1>  %endif
    86 0000001A B81200              <1>  mov ax, %1
    87 0000001D CD20                <1>  int 20h
   178 0000001F 722F                    	jc	short mnt_dir_err
   179                                  	; ax = device number
   180                                  
   181                                  	; is directory ? (check inode mode)
   182 00000021 F606[2701]40            	test	byte [stbuf+3], 40h ; directory
   183 00000026 7428                    	jz	short mnt_dir_err	
   184                                  
   185                                  	; is already mounted ?
   186 00000028 09C0                    	or	ax, ax
   187 0000002A 7524                    	jnz	short mnt_dir_err	
   188                                  
   189                                  ;	mov	dx, [stbuf] ; inode number
   190                                  ;	
   191                                  ;	sys	_stat, usr, stbuf
   192                                  ;	jc	short mnt_4
   193                                  ;	
   194                                  ;	; check if it is '/usr' inode
   195                                  ;	cmp	dx, [stbuf] ; same inode number
   196                                  ;	je	short mnt_5
   197                                  ;mnt_4:
   198                                  ;	sys	_stat, mnt, stbuf
   199                                  ;	jc	short mnt_dir_err
   200                                  ;
   201                                  ;	; check if it is '/mnt' inode
   202                                  ;	cmp	dx, [stbuf] ; same inode number
   203                                  ;	jne	short mnt_dir_err
   204                                  
   205                                  mnt_5:
   206                                  	; only superuser (root) can do mount/umount
   207                                  	sys	_getuid
    76                              <1> 
    77                              <1>  %if %0 >= 2
    78                              <1>  mov bx, %2
    79                              <1>  %if %0 >= 3
    80                              <1>  mov cx, %3
    81                              <1>  %if %0 >= 4
    82                              <1>  mov dx, %4
    83                              <1>  %endif
    84                              <1>  %endif
    85                              <1>  %endif
    86 0000002C B81800              <1>  mov ax, %1
    87 0000002F CD20                <1>  int 20h
   208 00000031 21C0                    	and	ax, ax ; uid = 0 -> root
   209 00000033 7405                    	jz	short mnt_6
   210 00000035 B8[BF00]                	mov	ax, deny_msg
   211 00000038 EB0E                    	jmp	short mnt_7
   212                                  mnt_6:
   213                                  	; di = device name address
   214                                  	; bp = mounting directory path/name address
   215                                   	;
   216                                  	; 09/05/2022
   217                                  	; Note: Retro UNIX 8086 v1 kernel 
   218                                  	; (and original unix v1 kernel, sysmount)
   219                                  	; mounts devices without checking superblock
   220                                  	; validity. So, sysmount does not return
   221                                  	; with 'invalid file system' error.
   222                                  	; (but Retro UNIX 386 sysmount returns with
   223                                  	; 'invalid file system' error)
   224                                  	;
   225                                  	; At sysmount system call return,
   226                                  	; if cf=1 it is user permission error
   227                                  	; or disk r/w error or 'already mounted' error.
   228                                  
   229                                  	sys	_mount, di, bp
    76                              <1> 
    77                              <1>  %if %0 >= 2
    78 0000003A 89FB                <1>  mov bx, %2
    79                              <1>  %if %0 >= 3
    80 0000003C 89E9                <1>  mov cx, %3
    81                              <1>  %if %0 >= 4
    82                              <1>  mov dx, %4
    83                              <1>  %endif
    84                              <1>  %endif
    85                              <1>  %endif
    86 0000003E B81500              <1>  mov ax, %1
    87 00000041 CD20                <1>  int 20h
   230 00000043 7210                    	jc	short mnt_err
   231                                  	;
   232 00000045 B8[1801]                	mov	ax, ok_msg
   233                                  mnt_7:
   234 00000048 E80F00                  	call	print_msg
   235                                  exit:
   236                                  	sys	_exit
    76                              <1> 
    77                              <1>  %if %0 >= 2
    78                              <1>  mov bx, %2
    79                              <1>  %if %0 >= 3
    80                              <1>  mov cx, %3
    81                              <1>  %if %0 >= 4
    82                              <1>  mov dx, %4
    83                              <1>  %endif
    84                              <1>  %endif
    85                              <1>  %endif
    86 0000004B B80100              <1>  mov ax, %1
    87 0000004E CD20                <1>  int 20h
   237                                  ;hang:
   238                                  ;	nop
   239                                  ;	jmp	short hang
   240                                  
   241                                  mnt_dir_err:
   242 00000050 B8[FD00]                	mov	ax, dir_err_msg
   243 00000053 EBF3                    	jmp	short mnt_7
   244                                  
   245                                  mnt_err:
   246                                  	; 09/05/2022
   247 00000055 B8[F100]                	mov	ax, err_msg
   248 00000058 EBEE                    	jmp	short mnt_7
   249                                  
   250                                  ;-----------------------------------------------------------------
   251                                  
   252                                  print_msg:
   253                                  	; ax = asciiz string address
   254 0000005A 89C6                    	mov	si, ax
   255 0000005C 4E                      	dec	si
   256                                  nextchr:
   257 0000005D 46                      	inc	si
   258 0000005E 803C00                  	cmp	byte [si], 0
   259 00000061 77FA                    	ja	short nextchr
   260                                  	;cmp	[si], 0Dh
   261                                  	;ja	short nextchr
   262 00000063 29C6                    	sub	si, ax
   263                                  	; si = asciiz string length
   264                                  	;
   265                                  	sys	_write, 1, ax, si
    76                              <1> 
    77                              <1>  %if %0 >= 2
    78 00000065 BB0100              <1>  mov bx, %2
    79                              <1>  %if %0 >= 3
    80 00000068 89C1                <1>  mov cx, %3
    81                              <1>  %if %0 >= 4
    82 0000006A 89F2                <1>  mov dx, %4
    83                              <1>  %endif
    84                              <1>  %endif
    85                              <1>  %endif
    86 0000006C B80400              <1>  mov ax, %1
    87 0000006F CD20                <1>  int 20h
   266                                  	;
   267 00000071 C3                      	retn
   268                                  
   269                                  ;-----------------------------------------------------------------
   270                                  ;  data - initialized data
   271                                  ;-----------------------------------------------------------------
   272                                  
   273                                  ; default mounting directories (for current retro unix version)
   274                                  ;usr:	db '/usr', 0
   275                                  ;mnt:	db '/mnt', 0
   276                                  
   277                                  usage_msg:
   278 00000072 0D0A                    	db 0Dh, 0Ah
   279                                  	;db "Usage: mount <block device name> </usr> "
   280                                  	;db 0Dh, 0Ah
   281                                  	;db "    or mount <block device name> </mnt> "	
   282 00000074 55736167653A206D6F-     	db "Usage: mount <block device name> <dir> "
   282 0000007D 756E74203C626C6F63-
   282 00000086 6B2064657669636520-
   282 0000008F 6E616D653E203C6469-
   282 00000098 723E20             
   283 0000009B 0D0A                    	db 0Dh, 0Ah
   284 0000009D 0D0A                    	db 0Dh, 0Ah
   285 0000009F 4578616D706C653A20-     	db "Example: mount /dev/fd1 /usr "
   285 000000A8 6D6F756E74202F6465-
   285 000000B1 762F666431202F7573-
   285 000000BA 7220               
   286 000000BC 0D0A00                  	db 0Dh, 0Ah, 0
   287                                  
   288                                  deny_msg:
   289 000000BF 0D0A                    	db 0Dh, 0Ah	
   290 000000C1 4D6F756E743A204F6E-     	db "Mount: Only superuser/root can mount devices!"	
   290 000000CA 6C7920737570657275-
   290 000000D3 7365722F726F6F7420-
   290 000000DC 63616E206D6F756E74-
   290 000000E5 206465766963657321 
   291 000000EE 0D0A00                   	db 0Dh, 0Ah, 0  		
   292                                  err_msg:
   293 000000F1 0D0A                    	db 0Dh, 0Ah
   294 000000F3 4572726F722120          	db 'Error! '
   295 000000FA 0D0A00                  	db 0Dh, 0Ah, 0
   296                                  dir_err_msg:
   297 000000FD 0D0A                    	db 0Dh, 0Ah
   298 000000FF 4D6F756E7420446972-     	db 'Mount Directory Error!'
   298 00000108 6563746F7279204572-
   298 00000111 726F7221           
   299                                  	;db 0Dh, 0Ah
   300                                  	;db "(Mounting directory must exist "
   301                                  	;db "and be '/usr' or '/mnt'.)",
   302 00000115 0D0A00                  	db 0Dh, 0Ah, 0
   303                                  ok_msg:
   304 00000118 0D0A                    	db 0Dh, 0Ah
   305 0000011A 4F4B2E20                	db 'OK. '
   306                                  nextline:
   307 0000011E 0D0A00                  	db 0Dh, 0Ah, 0
   308                                  
   309                                  ;-----------------------------------------------------------------
   310                                  ;  bss - uninitialized data
   311                                  ;-----------------------------------------------------------------	
   312                                  
   313 00000121 90<rep 3h>              align 4
   314                                  
   315                                  bss_start:
   316                                  
   317                                  ABSOLUTE bss_start
   318                                  
   319                                  ; sysstat buffer
   320 00000124 <res 22h>               stbuf: resb 34 ; (Retro UNIX 8086/386 v1 & Retro UNIX 386 v1.1)
   321                                  
   322                                  ;-----------------------------------------------------------------
