     1                                  ; ****************************************************************************
     2                                  ; umount8086.s (umount0.s) - by Erdogan Tan - 08/05/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - umount -- dismount file system
     5                                  ;
     6                                  ; [ Last Modification: 09/05/2022 ]
     7                                  ;
     8                                  ; ****************************************************************************
     9                                  ; (/etc/umount)
    10                                  
    11                                  ; umount0.s - Retro UNIX 8086 v1 (16 bit version of 'umount1.s')
    12                                  ; umount1.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                                  
    52                                  ;;;
    53                                  ESCKey equ 1Bh
    54                                  EnterKey equ 0Dh
    55                                  
    56                                  ;%macro sys 1-4
    57                                  ;   ; 03/09/2015
    58                                  ;   ; 13/04/2015
    59                                  ;   ; Retro UNIX 386 v1 system call.
    60                                  ;   %if %0 >= 2   
    61                                  ;       mov ebx, %2
    62                                  ;       %if %0 >= 3
    63                                  ;           mov ecx, %3
    64                                  ;           ;%if %0 = 4
    65                                  ;           %if	%0 >= 4 ; 11/03/2022
    66                                  ;		mov edx, %4
    67                                  ;           %endif
    68                                  ;       %endif
    69                                  ;   %endif
    70                                  ;   mov eax, %1
    71                                  ;   int 30h
    72                                  ;%endmacro
    73                                  
    74                                  %macro sys 1-4
    75                                      ; Retro UNIX 8086 v1 system call.
    76                                      %if %0 >= 2   
    77                                          mov bx, %2
    78                                          %if %0 >= 3
    79                                              mov cx, %3
    80                                              %if %0 >= 4
    81                                                 mov dx, %4
    82                                              %endif
    83                                          %endif
    84                                      %endif
    85                                      mov ax, %1
    86                                      int 20h
    87                                  %endmacro
    88                                  
    89                                  ;; Retro UNIX 386 v1 system call format:
    90                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    91                                  
    92                                  ;; 11/03/2022
    93                                  ;; Note: Above 'sys' macro has limitation about register positions;
    94                                  ;;	ebx, ecx, edx registers must not be used after their
    95                                  ;;	positions in sys macro.
    96                                  ;; for example:
    97                                  ;;	'sys _write, 1, msg, ecx' is defective, because
    98                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
    99                                  ;; correct order may be:
   100                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   101                                  
   102                                  ; Retro UNIX 8086 v1 system call format:
   103                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   104                                  
   105                                  ; ----------------------------------------------------------------------------
   106                                  
   107                                  [BITS 16] ; 16-bit intructions (8086/8088 - Real Mode)
   108                                  
   109                                  [ORG 0] 
   110                                  
   111                                  START_CODE:
   112                                  	; 09/05/2022 (16 bit code)
   113                                  	; 08/05/2022
   114 00000000 59                      	pop	cx ; cx = number of arguments
   115                                  	;
   116 00000001 58                      	pop	ax ; ax = argument 0 = executable file name
   117                                  	;
   118                                  	;cmp	cx, 2
   119 00000002 80F902                  	cmp	cl, 2
   120 00000005 740B                    	je	short umnt_1
   121                                  
   122                                  	; print usage message and then exit
   123                                  umnt_0:
   124 00000007 B8[4F00]                	mov	ax, usage_msg
   125                                  p_msg_exit:
   126 0000000A E82A00                  	call	print_msg
   127                                  exit:
   128                                  	sys	_exit
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov bx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov cx, %3
    80                              <1>  %if %0 >= 4
    81                              <1>  mov dx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000000D B80100              <1>  mov ax, %1
    86 00000010 CD20                <1>  int 20h
   129                                  ;hang:
   130                                  ;	nop
   131                                  ;	jmp	short hang
   132                                  
   133                                  umnt_1:
   134 00000012 5E                      	pop	si ; argument 1 = device name
   135                                  
   136                                  	; only superuser (root) can do mount/umount
   137                                  	sys	_getuid
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov bx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov cx, %3
    80                              <1>  %if %0 >= 4
    81                              <1>  mov dx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000013 B81800              <1>  mov ax, %1
    86 00000016 CD20                <1>  int 20h
   138 00000018 21C0                    	and	ax, ax ; uid = 0 -> root
   139 0000001A 7405                    	jz	short umnt_2
   140 0000001C B8[9100]                	mov	ax, deny_msg
   141 0000001F EBE9                    	jmp	short p_msg_exit
   142                                  umnt_2:
   143                                  	; si = device name address
   144                                  	sys	_umount, si
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000021 89F3                <1>  mov bx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov cx, %3
    80                              <1>  %if %0 >= 4
    81                              <1>  mov dx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000023 B81600              <1>  mov ax, %1
    86 00000026 CD20                <1>  int 20h
   145 00000028 7205                    	jc	short umnt_err
   146                                  
   147                                  
   148                                  	; 09/05/2022
   149                                  	; Following -temporray- code is not needed..
   150                                  	; (sysumount system call of
   151                                  	; Retro UNIX 8086 v1 kernel returns with
   152                                  	; error -cf=1- if /dev/fd0 is not mounted.)
   153                                  
   154                                  	; ----
   155                                  	
   156                                  ;	; Temporary! 08/05/2022 
   157                                  ;	; device number of /dev/fd0 is 0 and this
   158                                  ;	; affects sysumount system call return 
   159                                  ;	; because [mdev] = 0 when there is not
   160                                  ;	; a mounted device and sysumount checks this!.
   161                                  ;	;
   162                                  ;	; (It does not cause an error in file system
   163                                  ;	;  but sysumount returns to user with wrong
   164                                  ;	; 'ok'. For now, till sysumount system call
   165                                  ;	; modification, 'ok.' message must/will not
   166                                  ;	; be written for /dev/fd0.) 
   167                                  ;
   168                                  ;	lodsw
   169                                  ;	cmp	ax, '/d'
   170                                  ;	jne	short umnt_3 ; (ax = 'fd', [si] = '0')
   171                                  ;	; word [si] = 'ev'
   172                                  ;	add	si, 5
   173                                  ;	; byte [si] = '0' ; '/dev/'+'fd'+'0'
   174                                  ;umnt_3:
   175                                  ;	lodsb	; byte [si] = '0'
   176                                  ;	cmp	al, '0' ; 'fd0'
   177                                  ;	je	short exit ; do not write 'OK.'	
   178                                  
   179 0000002A B8[D300]                	mov	ax, ok_msg
   180 0000002D EBDB                    	jmp	short p_msg_exit
   181                                  umnt_err:
   182 0000002F 66B8[C7000000]          	mov	eax, err_msg
   183 00000035 EBD3                    	jmp	short p_msg_exit
   184                                  
   185                                  ;-----------------------------------------------------------------
   186                                  
   187                                  print_msg:
   188                                  	; ax = asciiz string address
   189 00000037 89C6                    	mov	si, ax
   190 00000039 4E                      	dec	si
   191                                  nextchr:
   192 0000003A 46                      	inc	si
   193 0000003B 803C00                  	cmp	byte [si], 0
   194 0000003E 77FA                    	ja	short nextchr
   195                                  	;cmp	[si], 0Dh
   196                                  	;ja	short nextchr
   197 00000040 29C6                    	sub	si, ax
   198                                  	; si = asciiz string length
   199                                  	;
   200                                  	sys	_write, 1, ax, si
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000042 BB0100              <1>  mov bx, %2
    78                              <1>  %if %0 >= 3
    79 00000045 89C1                <1>  mov cx, %3
    80                              <1>  %if %0 >= 4
    81 00000047 89F2                <1>  mov dx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000049 B80400              <1>  mov ax, %1
    86 0000004C CD20                <1>  int 20h
   201                                  	;
   202 0000004E C3                      	retn
   203                                  
   204                                  ;-----------------------------------------------------------------
   205                                  ;  data - initialized data
   206                                  ;-----------------------------------------------------------------
   207                                  
   208                                  usage_msg:
   209 0000004F 0D0A                    	db 0Dh, 0Ah
   210 00000051 55736167653A20756D-     	db "Usage: umount <block device name> "
   210 0000005A 6F756E74203C626C6F-
   210 00000063 636B20646576696365-
   210 0000006C 206E616D653E20     
   211 00000073 0D0A                    	db 0Dh, 0Ah
   212 00000075 4578616D706C653A20-     	db "Example: umount /dev/fd1 "
   212 0000007E 756D6F756E74202F64-
   212 00000087 65762F66643120     
   213 0000008E 0D0A00                  	db 0Dh, 0Ah, 0
   214                                  
   215                                  deny_msg:
   216 00000091 0D0A                    	db 0Dh, 0Ah	
   217 00000093 556D6F756E743A204F-     	db "Umount: Only superuser/root can dismount devices!"	
   217 0000009C 6E6C79207375706572-
   217 000000A5 757365722F726F6F74-
   217 000000AE 2063616E206469736D-
   217 000000B7 6F756E742064657669-
   217 000000C0 63657321           
   218 000000C4 0D0A00                   	db 0Dh, 0Ah, 0  		
   219                                  err_msg:
   220 000000C7 0D0A                    	db 0Dh, 0Ah
   221 000000C9 4572726F722120          	db 'Error! '
   222 000000D0 0D0A00                  	db 0Dh, 0Ah, 0
   223                                  ok_msg:
   224 000000D3 0D0A                    	db 0Dh, 0Ah
   225 000000D5 4F4B2E20                	db 'OK. '
   226                                  nextline:
   227 000000D9 0D0A00                  	db 0Dh, 0Ah, 0
   228                                  
   229                                  ;-----------------------------------------------------------------
