⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 patchvol.asm

📁 一个朋友写的操作系统源码
💻 ASM
📖 第 1 页 / 共 2 页
字号:

;/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; This file deals with the low leavel diskette I/O performance
; to patch the volume directly, it will alloc Minix zones by
; setting appropriate zone bitmap and copy the Stuix kernel
; loader into these zones blk by blk. at last a table will be
; generated to reflect the right order of the logic-physic
; distribution of kernel loader, this table patchs directly
; to the second halt of the boot block. 
;
; The entry points into this file are
;   patchfs:     the main subroutine
;   rtzbmap:     restore original zone bitmap
;   wrtofs:      it will call wrtzone and updat
;   wrtzone:     almost the same as getzone
;   updat:       it fills an entry on the table
;   alloczone:   alloc a zone from Minix fs volume
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/

include comstruc.inc

sseg segment stack

     db 128 dup (0)

sseg ends

overlap segment para common 'strucinfo'

        ; this is the only way to reference
        ; extern struct

        inodeinfo minixinode <>
        superinfo minixsuper <>
        dent      dirent     <>        

overlap ends

dseg segment para public 'global'

     extrn inodeblk:byte     
     extrn superblk:byte
     extrn zonespace:byte
     extrn newline:byte

     zbmap        db 1024 dup (0)       ; zone bitmap for Minix fs
     orizbmap     db 1024 dup (0)       ; used to restore bitmap
     corename     db 'stuixldr.exe',0   ; load modual name
     zbmapoff     dw 0                  ; word stepping offset
     zbindex      dw 0                  ; inter word offset
     bootblock    db 1024 dup (0)       ; bootblock image

     ; debug alloczone

     allocsuc     db '%d # physical zone has been alloced!','$'
     testmap      db 1024 dup (1)



dseg ends

cseg segment para public 'code'

     assume cs:cseg,ds:dseg,es:dseg,ss:sseg

     public patchfs

     extrn  panic:near
     extrn  printf:near
     extrn  getzone:near
     extrn  getsuper:near

;/============================================================================
;
;                             patchfs
;
;============================================================================/

     patchfs proc near
     
     push ds
     push es
     push bp
     mov bp,sp

     sub sp,12

     ; stack arranges like this
     ; [bp-2]  : total zones available
     ; [bp-4]  : &search counter
     ; [bp-6]  : file handler
     ; [bp-8]  : patched physical blk #
     ; [bp-10] : &current index of patched blk
     ; [bp-12] : number of byte in last blk

     ; we want to the max zone # and check fs magic #

     call getsuper
     push es
     mov ax,overlap
     mov es,ax
     assume es:overlap
     mov ax,es:[superinfo.s_zones]
     pop es
     assume es:dseg
     mov [bp-2],ax           ; how many zones

     ; initialize local variables

     mov word ptr [bp-8],0
     mov word ptr [bp-6],0
     mov word ptr [bp-4],0
     mov word ptr [bp-10],1

     ; initialize global variables

     mov word ptr [zbmapoff],offset zbmap
     mov word ptr [zbindex],0

     ; we a blk is writen, the second half of boot block
     ; will be update according to the index, 0# index
     ; used for keep track of total blk #

     mov ax,0
     push ax
     call getzone              ; get bootblock
     pop cx
     mov si,offset zonespace
     mov di,offset bootblock
     mov cx,1024
     cld
     repnz movsb
     xor ax,ax
     mov di,offset bootblock
     add di,512                
     mov cx,512                
     cld
     repnz stosb               ; clear second half of boot block

     ; if ther's no blk avaiable in file system
     ; we shall reset the zone bitmap to its
     ; original state, and info the user that
     ; patch failed, also, the 0# index in the
     ; second half on bootblock shall set to zero
     ; so that boot program will not try to load it

     mov ax,3            ; get zone bitmap
     push ax
     call getzone
     pop cx
     mov cx,1024
     mov si,offset zonespace
     mov di,offset zbmap
     cld
     repnz movsb         ; load zone map

     mov cx,1024
     mov si,offset zonespace
     mov di,offset orizbmap
     cld
     repnz movsb         ; load backup zone map

     ; open kernel loader executable file

     mov dx,offset corename
     mov al,0
     mov ah,3dh
     int 21h
     jnc pnext2
     push ax                ; push error code
     call panic             ; never return
pnext2:
     mov [bp-6],ax          ; save file handler
     mov bx,ax

     ; read the file blk by blk, read one, alloc
     ; storage on Minix file system, write to it
     ; also we need to updata the second half
     ; of boot block as side effect, when we detect
     ; read count < 1024 we will end reading, if
     ; any error detected during this time, we will
     ; restore the original bitmap, if all process
     ; success, we will add the total number to
     ; the second half of boot blk 0# index

pw1loop:
     mov bx,[bp-6]
     mov cx,1024
     mov dx,offset zonespace
     mov ah,3fh
     int 21h
     jnc pnext3

     ; failed to read file, restore original
     ; bitmap and panic

     push ax                ; accept error code
     call rtzbmap           ; reset and panic
finish:
     jmp over               ; file size is just multiple of blk byte
pnext4:
     jmp pnext42            ; scale the relative jump arrage
pnext3:
     mov [bp-12],ax         ; ax != 1024, it is the last blk
     cmp ax,0
     jz finish 
     cmp ax,1024
     je pnext4

     ; these blocks reading has no error
     ; we will set the rest bytes in last
     ; block to 0 and write to Minix fs
     ; set the total blk # we have patched
     ; to the second halt of bootblk

     mov si,[bp-2]
     mov di,bp
     sub di,4               ; &[bp-4]

     ; must be stack address, so that
     ; alloczone can modify it

     push di
     push si
     call alloczone
     pop cx
     pop cx                 ; leave ax = physical blk # available
     mov [bp-8],ax
     push ax
     mov ax,[bp-12]
     push ax
     mov si,bp
     sub si,10
     push si
     call wrtofs
     pop cx
     pop cx
     pop cx

     ; wrtofs do a lot things, it determines the last
     ; block and give it special treatment, it calls
     ; wrtzone to write the whole buf to the Minix fs
     ; zone and calls updat to patch the second half
     ; of boot block, at last we put debug information
     ; tell the logic number n has been writen to
     ; physical block sucessful

     ; also we should write back the zbmap to file system

over:
     mov si,offset testmap
     mov di,offset zonespace
     cld
     mov cx,1024
     repnz movsb
     mov ax,3
     push ax




     call wrtzone
     pop cx

     mov sp,bp
     pop bp
     pop es
     pop ds
     ret
pnext42:
     mov si,[bp-2]
     mov di,bp
     sub di,4
     push di
     push si
     call alloczone
     pop cx
     pop cx
     mov [bp-8],ax
     push ax
     mov ax,[bp-12]
     push ax
     mov si,bp
     sub si,10
     push si
     call wrtofs
     pop cx
     pop cx
     pop cx
     jmp pw1loop          ; read if any

     patchfs endp


;/============================================================================
;
;                             alloczone
;
;============================================================================/

     alloczone proc near

     push ds
     push es
     push bp
     mov bp,sp

     sub sp,6

     ; the stack arranges as follows

     ; [bp-2] : first data zone
     ; [bp-4] : zone to be searched
     ; [bp-6] : &count been seached

     ; it reads zbmap one time 16bit, and use
     ; first fit algorithm to find the free zone
     ; we will read superinode info to get the
     ; first data zone and add to bit position
     ; to calculate the physical disk zones

     mov ax,8[bp]
     mov [bp-4],ax

     mov ax,10[bp]
     mov [bp-6],ax

     push es
     mov ax,overlap
     mov es,ax
     assume es:overlap
     mov ax,es:[superinfo.s_firstdatazone]
     pop es
     assume es:dseg
     mov [bp-2],ax          ; get first data zone

     ; fetch word from bitmap, and try to alloc zone
     ; manipulate bit map, we use two pointer, one is
     ; word pointer [zbmapoff], other is word bit pointer
     ; [zbindex], these pointers can indentify the bit
     ; position uniquely. note, no matter current bit
     ; search failed or not, word pointer and word bit
     ; pointer will both increase by 1, but next call
     ; of this subroutine, the initialize code will
     ; set word pointer properly according to the value
     ; of word bit pointer only word bit pointer >= 16
     ; the word pointer will remain its value, other wise
     ; it will decrease by 1 and reload the bitmap to ax
     ; at last set dx to word bit pointer value, and jump
     ; to anext2 to resume search procedure. be careful
     ; to properly shift the bit word if previous alloced
     ; bit is not on word boundary

     mov si,[zbmapoff]
     cmp word ptr [zbindex],16
     jae pw2loop             ; last sucess bit alloc at word boundary
     mov di,[bp-6]
     cmp word ptr ss:[di],0
     je fstsrch
     dec si                  ; this word bit has not been searched out yet
     dec si
fstsrch:                     ; when the subroutine was called first time
;     mov [zbmapoff],si       
     cld
     lodsw
     mov dx,[zbindex]
     cmp dx,0
     je noshift
     mov cx,dx
     xor ch,ch

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -