1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
;------------------------------------------------------------------------------
;*
;* Copyright 2006 - 2007, Intel Corporation
;* All rights reserved. This program and the accompanying materials
;* are licensed and made available under the terms and conditions of the BSD License
;* which accompanies this distribution. The full text of the license may be found at
;* http://opensource.org/licenses/bsd-license.php
;*
;* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
;* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
;*
;* gpt.asm
;*
;* Abstract:
;*
;------------------------------------------------------------------------------
.model small
; .dosseg
.stack
.486p
.code
BLOCK_SIZE EQU 0200h
BLOCK_MASK EQU 01ffh
BLOCK_SHIFT EQU 9
; ****************************************************************************
; Code loaded by BIOS at 0x0000:0x7C00
; ****************************************************************************
org 0h
Start:
; ****************************************************************************
; Start Print
; ****************************************************************************
mov ax,0b800h
mov es,ax
mov ax, 07c0h
mov ds, ax
lea si, cs:[StartString]
mov cx, 10
mov di, 160
rep movsw
; ****************************************************************************
; Print over
; ****************************************************************************
; ****************************************************************************
; Initialize segment registers and copy code at 0x0000:0x7c00 to 0x0000:0x0600
; ****************************************************************************
xor ax, ax ; AX = 0x0000
mov bx, 07c00h ; BX = 0x7C00
mov bp, 0600h ; BP = 0x0600
mov si, OFFSET RelocatedStart ; SI = Offset(RelocatedStart)
mov cx, 0200h ; CX = 0x0200
sub cx, si ; CS = 0x0200 - Offset(RelocatedStart)
lea di, [bp+si] ; DI = 0x0600 + Offset(RelocatedStart)
lea si, [bx+si] ; BX = 0x7C00 + Offset(RelocatedStart)
mov ss, ax ; SS = 0x0000
mov sp, bx ; SP = 0x7C00
mov es,ax ; ES = 0x0000
mov ds,ax ; DS = 0x0000
push ax ; PUSH 0x0000
push di ; PUSH 0x0600 + Offset(RelocatedStart)
cld ; Clear the direction flag
rep movsb ; Copy 0x0200 bytes from 0x7C00 to 0x0600
retf ; JMP 0x0000:0x0600 + Offset(RelocatedStart)
; ****************************************************************************
; Code relocated to 0x0000:0x0600
; ****************************************************************************
RelocatedStart:
; ****************************************************************************
; Get Driver Parameters to 0x0000:0x7BFC
; ****************************************************************************
xor ax,ax ; ax = 0
mov ss,ax ; ss = 0
add ax,1000h
mov ds,ax
mov sp,07c00h ; sp = 0x7c00
mov bp,sp ; bp = 0x7c00
mov ah,8 ; ah = 8 - Get Drive Parameters Function
mov byte ptr [bp+PhysicalDrive],dl ; BBS defines that BIOS would pass the booting driver number to the loader through DL
int 13h ; Get Drive Parameters
xor ax,ax ; ax = 0
mov al,dh ; al = dh
inc al ; MaxHead = al + 1
push ax ; 0000:7bfe = MaxHead
mov al,cl ; al = cl
and al,03fh ; MaxSector = al & 0x3f
push ax ; 0000:7bfc = MaxSector
; ****************************************************************************
; Read GPT Header from hard disk to 0x0000:0x0800
; ****************************************************************************
xor ax, ax
mov es, ax ; Read to 0x0000:0x0800
mov di, 0800h ; Read to 0x0000:0x0800
mov eax, 1 ; Read LBA #1
mov edx, 0 ; Read LBA #1
mov bx, 1 ; Read 1 Block
push es
call ReadBlocks
pop es
; ****************************************************************************
; Read Target GPT Entry from hard disk to 0x0000:0x0A00
; ****************************************************************************
cmp dword ptr es:[di], 020494645h ; Check for "EFI "
jne BadGpt
cmp dword ptr es:[di + 4], 054524150h ; Check for "PART"
jne BadGpt
cmp dword ptr es:[di + 8], 000010000h ; Check Revision - 0x10000
jne BadGpt
mov eax, dword ptr es:[di + 84] ; EAX = SizeOfPartitionEntry
mul byte ptr [bp+GptPartitionIndicator] ; EAX = SizeOfPartitionEntry * GptPartitionIndicator
mov edx, eax ; EDX = SizeOfPartitionEntry * GptPartitionIndicator
shr eax, BLOCK_SHIFT ; EAX = (SizeOfPartitionEntry * GptPartitionIndicator) / BLOCK_SIZE
and edx, BLOCK_MASK ; EDX = Targer PartitionEntryLBA Offset
; = (SizeOfPartitionEntry * GptPartitionIndicator) % BLOCK_SIZE
push edx
mov ecx, dword ptr es:[di + 72] ; ECX = PartitionEntryLBA (Low)
mov ebx, dword ptr es:[di + 76] ; EBX = PartitionEntryLBA (High)
add eax, ecx ; EAX = Target PartitionEntryLBA (Low)
; = (PartitionEntryLBA +
; (SizeOfPartitionEntry * GptPartitionIndicator) / BLOCK_SIZE)
adc edx, ebx ; EDX = Target PartitionEntryLBA (High)
mov di, 0A00h ; Read to 0x0000:0x0A00
mov bx, 1 ; Read 1 Block
push es
call ReadBlocks
pop es
; ****************************************************************************
; Read Target DBR from hard disk to 0x0000:0x7C00
; ****************************************************************************
pop edx ; EDX = (SizeOfPartitionEntry * GptPartitionIndicator) % BLOCK_SIZE
add di, dx ; DI = Targer PartitionEntryLBA Offset
cmp dword ptr es:[di], 0C12A7328h ; Check for EFI System Partition "C12A7328-F81F-11d2-BA4B-00A0C93EC93B"
jne BadGpt
cmp dword ptr es:[di + 4], 011d2F81Fh ;
jne BadGpt
cmp dword ptr es:[di + 8], 0A0004BBAh ;
jne BadGpt
cmp dword ptr es:[di + 0ch], 03BC93EC9h ;
jne BadGpt
mov eax, dword ptr es:[di + 32] ; EAX = StartingLBA (Low)
mov edx, dword ptr es:[di + 36] ; EDX = StartingLBA (High)
mov di, 07C00h ; Read to 0x0000:0x7C00
mov bx, 1 ; Read 1 Block
call ReadBlocks
; ****************************************************************************
; Transfer control to BootSector - Jump to 0x0000:0x7C00
; ****************************************************************************
xor ax, ax
push ax ; PUSH 0x0000
mov di, 07c00h
push di ; PUSH 0x7C00
retf ; JMP 0x0000:0x7C00
; ****************************************************************************
; ReadBlocks - Reads a set of blocks from a block device
;
; EDX:EAX = Start LBA
; BX = Number of Blocks to Read (must < 127)
; ES:DI = Buffer to store sectors read from disk
; ****************************************************************************
; si = DiskAddressPacket
ReadBlocks:
pushad
push ds
xor cx, cx
mov ds, cx
mov bp, 0600h ; bp = 0x600
lea si, [bp + OFFSET AddressPacket] ; DS:SI = Disk Address Packet
mov BYTE PTR ds:[si+2],bl ; 02 = Number Of Block transfered
mov WORD PTR ds:[si+4],di ; 04 = Transfer Buffer Offset
mov WORD PTR ds:[si+6],es ; 06 = Transfer Buffer Segment
mov DWORD PTR ds:[si+8],eax ; 08 = Starting LBA (Low)
mov DWORD PTR ds:[si+0ch],edx ; 0C = Starting LBA (High)
mov ah, 42h ; ah = Function 42
mov dl,byte ptr [bp+PhysicalDrive] ; dl = Drive Number
int 13h
jc BadGpt
pop ds
popad
ret
; ****************************************************************************
; Address Packet used by ReadBlocks
; ****************************************************************************
AddressPacket:
db 10h ; Size of address packet
db 00h ; Reserved. Must be 0
db 01h ; Read blocks at a time (To be fixed each times)
db 00h ; Reserved. Must be 0
dw 0000h ; Destination Address offset (To be fixed each times)
dw 0000h ; Destination Address segment (To be fixed each times)
AddressPacketLba:
dd 0h, 0h ; Start LBA (To be fixed each times)
AddressPacketEnd:
; ****************************************************************************
; ERROR Condition:
; ****************************************************************************
BadGpt:
mov ax,0b800h
mov es,ax
mov ax, 060h
mov ds, ax
lea si, cs:[ErrorString]
mov cx, 10
mov di, 320
rep movsw
Halt:
jmp Halt
StartString:
db 'G', 0ch, 'P', 0ch, 'T', 0ch, ' ', 0ch, 'S', 0ch, 't', 0ch, 'a', 0ch, 'r', 0ch, 't', 0ch, '!', 0ch
ErrorString:
db 'G', 0ch, 'P', 0ch, 'T', 0ch, ' ', 0ch, 'E', 0ch, 'r', 0ch, 'r', 0ch, 'o', 0ch, 'r', 0ch, '!', 0ch
; ****************************************************************************
; PhysicalDrive - Used to indicate which disk to be boot
; Can be patched by tool
; ****************************************************************************
org 01B6h
PhysicalDrive db 80h
; ****************************************************************************
; GptPartitionIndicator - Used to indicate which GPT partition to be boot
; Can be patched by tool
; ****************************************************************************
org 01B7h
GptPartitionIndicator db 0
; ****************************************************************************
; Unique MBR signature
; ****************************************************************************
org 01B8h
db 'DUET'
; ****************************************************************************
; Unknown
; ****************************************************************************
org 01BCh
dw 0
; ****************************************************************************
; PMBR Entry - Can be patched by tool
; ****************************************************************************
org 01BEh
db 0 ; Boot Indicator
db 0ffh ; Start Header
db 0ffh ; Start Sector
db 0ffh ; Start Track
db 0eeh ; OS Type
db 0ffh ; End Header
db 0ffh ; End Sector
db 0ffh ; End Track
dd 1 ; Starting LBA
dd 0FFFFFFFFh ; End LBA
org 01CEh
dd 0, 0, 0, 0
org 01DEh
dd 0, 0, 0, 0
org 01EEh
dd 0, 0, 0, 0
; ****************************************************************************
; Sector Signature
; ****************************************************************************
org 01FEh
SectorSignature:
dw 0aa55h ; Boot Sector Signature
end
|