Fix a couple of problems with FreeLDR portability.
[reactos.git] / reactos / boot / freeldr / bootsect / dosmbr.asm
1 ;
2 ; normal DOS boot sector
3 ;
4 ; Ported to nasm from FreeDOS fdisk 1.2.0 by:
5 ; Casper Hornstrup (chorns@users.sourceforge.net)
6 ;
7
8 align 2, db 0
9
10 global _bootnormal_code
11 _bootnormal_code:
12
13 ;-----------------------------------------------------------------------
14 ; ENTRY (copied from freedos bootsector)
15 ;
16 ; IN: DL = boot drive
17 ;OUT: DL = boot drive
18 ;
19 ;-----------------------------------------------------------------------
20
21 real_start: cli
22 cld
23 xor ax, ax
24 mov ss, ax ; initialize stack
25 mov ds, ax
26 mov bp, 0x7c00
27 lea sp, [bp-0x20]
28 sti
29
30 mov ax, 0x1FE0
31 mov es, ax
32 mov si, bp
33 mov di, bp
34 mov cx, 0x0100
35 rep movsw
36
37 jmp word 0x1FE0:0x7c00+ cont-real_start
38
39 cont: mov ds, ax
40 mov ss, ax
41 xor ax,ax
42 mov es,ax
43
44
45 ; search for active partition
46 lea di, [bp+0x1be] ; start of partition table
47 test_next_for_active:
48 test byte [di],0x80
49 jne active_partition_found
50 add di,0x10 ; next table
51 cmp di, 07c00h+0x1fe; scanned beyond end of table ??
52 jb test_next_for_active
53
54 ;*****************************************************************
55 call print
56 db 'no active partition found',0
57
58 WAIT_FOR_REBOOT:
59 jmp $
60
61
62 ;*****************************************************************
63 trouble_reading_drive:
64 call print
65 db 'read error while reading drive',0
66 jmp WAIT_FOR_REBOOT
67
68 ;*****************************************************************
69
70 invalid_partition_code:
71 call print
72 db 'partition signature != 55AA',0
73
74 jmp WAIT_FOR_REBOOT
75
76
77 ;*****************************************************************
78
79 active_partition_found:
80 ; call print
81 ; db 'loading active partition',0
82
83 call read_boot_sector
84
85 jc trouble_reading_drive
86
87 cmp word [es:0x7c00+0x1fe],0xaa55
88 jne invalid_partition_code
89
90 jmp word 0x0:0x7c00 ; and jump to boot sector code
91
92
93 ;*****************************
94 ; read_boot_sector
95 ;
96 ; IN: DI--> partition info
97 ;OUT:CARRY
98 ;*****************************
99
100 read_boot_sector:
101 ; /* check for LBA support */
102 mov bx,0x55aa
103 mov ah,0x41
104 int 0x13
105
106 jc StandardBios ; if (regs.b.x != 0xaa55 || (regs.flags & 0x01))
107 cmp bx,0xaa55 ; goto StandardBios;
108 jne StandardBios
109
110 ; /* if DAP cannot be used, don't use LBA */
111 ; if ((regs.c.x & 1) == 0)
112 ; goto StandardBios;
113 test cl,1
114 jz StandardBios
115
116 jmp short LBABios
117
118
119 _bios_LBA_address_packet:
120 db 0x10
121 db 0
122 db 4 ; read four sectors - why not
123 db 0
124 dw 0x7c00 ; fixed boot address for DOS sector
125 dw 0x0000
126 _bios_LBA_low dw 0
127 _bios_LBA_high dw 0
128 dw 0,0
129
130
131 LBABios:
132 ; copy start address of partition to DAP
133 mov ax,[di+8]
134 mov [0x7c00+ (_bios_LBA_low-real_start)],ax
135 mov ax,[di+8+2]
136 mov [0x7c00+ (_bios_LBA_high-real_start)],ax
137
138 mov ax,0x4200 ; regs.a.x = LBA_READ;
139 mov si,0x7c00+ (_bios_LBA_address_packet-real_start); regs.si = FP_OFF(&dap);
140
141 int 0x13
142 ret
143
144 ;*****************************************************************
145 ; read disk, using standard BIOS
146 ;
147 StandardBios:
148 mov ax,0x0204 ; regs.a.x = 0x0201;
149 mov bx,0x7c00 ; regs.b.x = FP_OFF(buffer);
150 mov cx,[di+2] ; regs.c.x =
151 ; ((chs.Cylinder & 0xff) << 8) + ((chs.Cylinder & 0x300) >> 2) +
152 ; chs.Sector;
153 ; that was easy ;-)
154 mov dh,[di+1] ; regs.d.b.h = chs.Head;
155 ; regs.es = FP_SEG(buffer);
156 int 0x13
157 ret
158
159
160
161 ;****** PRINT
162 ; prints text after call to this function.
163
164 print_1char:
165 xor bx, bx ; video page 0
166 mov ah, 0x0E ; else print it
167 int 0x10 ; via TTY mode
168 print: pop si ; this is the first character
169 print1: lodsb ; get token
170 push si ; stack up potential return address
171 cmp al, 0 ; end of string?
172 jne print_1char ; until done
173 ret ; and jump to it
174
175
176
177 times 0x1fe-$+$$ db 0
178 db 0x55,0xaa
179