Add externs to avoid multiple definitions, and then add missing definitions.
[rrq/maintain_lilo.git] / src / mapper.S
1 ;  mapper.S  -  LILO chain loader subroutine
2 ;
3 ;  Copyright 2003-2004 John Coffman
4 ;  Copyright 2009-2011 Joachim Wiedorn
5 ;  All rights reserved.
6 ;
7 ;  Licensed under the terms contained in the file 'COPYING'
8 ;  in the source directory.
9 ;
10
11
12 ;  Check for presence of existing drive mapper
13 ;
14 ;       Enter with  DS == CS,  SS == 0000         (chain loader)
15 ;       Enter with  DS == CS == ES,  SS != 0000   (second stage)
16 ;
17 ;       If a previous drive mapper exists, ES:DI points at the  drvmap
18 ;               and ZF=0        (DI != 0)
19 ;
20 ;       If no recognizable drive map exists, DI == 0  and  ZF==1
21 ;               ES is indeterminate
22 ;
23 ;
24 ;
25 is_prev_mapper:
26         push    cx
27         push    si
28
29 #ifndef CHAIN_LOADER
30         push    #0
31         pop     es
32         seg es
33 #else
34         seg ss
35 #endif
36           les   di,[4*0x13]     ; vector to int 0x13
37         or      di,di
38         jnz     is_p_no_mapper  ; our mappers start at offset 0
39
40         mov     di,es
41         cmp     di,#0xA000      ; start of system reserved locations
42         jae     is_p_no_mapper
43         cmp     di,#0x0060      ; VERY conservative
44         jb      is_p_no_mapper
45
46 ; first test for new mapper
47         xor     di,di
48         mov     cx,#new13_length
49         mov     si,#new13
50         repe
51           cmpsb
52         jne     is_p_try_old
53
54 ; found new (v.22) mapper
55         seg es
56           mov   di,[new13_drvmap_offset]
57 #if defined CHAIN_LOADER && defined DEBUG_NEW
58         mov     si,#msg_new
59         call    say
60 #endif
61         jmp     is_prev_ret
62
63 is_p_try_old:
64         xor     di,di
65         mov     cx,#new13_old_length
66         mov     si,#new13_old
67         repe
68           cmpsb
69         jne     is_p_no_mapper
70
71 ; likely old (<=v.21) mapper
72         seg es
73           mov   di,(di) 
74         cmp     di,#new13_old_min_offs  ; validate the range of values
75         jb      is_p_no_mapper
76         cmp     di,#new13_old_max_offs  ; validate the range of values
77 #if defined CHAIN_LOADER && defined DEBUG_NEW
78         ja      is_p_no_mapper
79         mov     si,#msg_old
80         call    say
81         jmp     is_prev_ret
82 #else
83         jbe     is_prev_ret
84 #endif
85
86 is_p_no_mapper:
87         xor     di,di           ; set DI = 0, ZF=1
88 is_prev_ret:
89         or      di,di           ; set ZF by DI
90         pop     si
91         pop     cx
92         ret
93
94
95 /* LILO version 21 (and maybe earlier) drive map header signature code */
96 new13_old:
97         push    ax              ! save AX (contains function code in AH)
98         push    bp              ! need BP to mess with stack
99         mov     bp,sp
100         pushf                   ! push flags (to act like interrupt)
101         push    si
102         mov     si,#drvmap-new13
103
104 new13_old_drvmap_offs   =       * - new13_old - 2
105 new13_old_length        =       new13_old_drvmap_offs
106 new13_old_min_offs      =       0x46    ; min seen in old code is 0x49
107 new13_old_max_offs      =       0x50    ; maxed out at  21.7.5 at 0x4d
108
109         .even                   ! this is very important
110
111 new13:  push    ax              ! save AX (contains function code in AH)
112         push    bp              ! need BP to mess with stack
113         mov     bp,sp
114         jmp     new13a          ! make space for signature
115
116         .org    new13+6
117         .ascii  "LILO"
118         .word   STAGE_DRIVE_MAP
119 new13_length            =       *-new13         ; max compare length
120         .word   VERSION
121 new13_drvmap_offset     =       * - new13
122         .word   drvmap-new13    ! relative pointer to drive map
123 new13a:
124 #ifdef CHAIN_LOADER
125         ! Stack layout:
126         !
127         !   +8  INT flags
128         !   +6  INT CS
129         !   +4  INT IP
130         !   +2  AX
131         ! BP+0 BP
132         pushf                   ! push flags (to act like interrupt)
133         push    si
134         mov     si,#drvmap-new13
135 mapfl:  seg     cs              ! get next entry
136          mov    ax,(si)         ! do not depend on DIRECTION flag
137         lea     si,(si+2)       ! **
138         or      ax,ax           ! at end ?
139         jz      nomap           ! yes -> do not map
140         cmp     dl,al           ! match ?
141         jne     mapfl           ! no -> continue
142         mov     dl,ah           ! map drive
143 nomap:  pop     si              ! restore SI
144         mov     8(bp),ax        ! overwrite old flags (to remember mapping)
145         mov     ax,2(bp)        ! restore AX
146         mov     bp,(bp)         ! restore BP
147 old13of =       *+1
148 old13sg =       *+3
149         callf   0,0
150
151         push    bp              ! save BP again
152         mov     bp,sp
153         ! New stack layout:
154         !
155         !   +10 mapping (was flags)
156         !   +8  INT CS
157         !   +6  INT IP
158         !   +4  AX
159         !   +2  obsolete BP
160         ! BP+0  BP
161         xchg    ax,4(bp)        ! save AX and get command
162         pushf                   ! fix driver number, if necessary
163         cmp     ah,#8 ! do not fix
164         je      done13
165         cmp     ah,#0x15 ! do not fix
166         je      done13
167         mov     ax,10(bp)       ! no mapping ?
168         or      ax,ax
169         jz      done13
170         mov     dl,al           ! fix mapping
171 done13: mov     ax,4(bp)        ! restore AX
172         pop     10(bp)          ! restore flags
173         pop     bp              ! get BP
174         add     sp,#4           ! fix SP
175         iret                    ! done
176         
177         .even
178 drvmap: .blkw   DRVMAP_SIZE+1
179
180 new13end:
181 #endif
182 #ifdef SECOND_STAGE_LOADER
183 drvmap:
184 #endif
185
186 /* end of mapper.S  */