* remove "\r" nonsense
[mascara-docs.git] / amd64 / bareMetalOS-0.5.2 / baremetal0.5.2 / os / init_hdd.asm
blob4270b4041c2750bd8cb61a09fb7b16e4b944d111
1 ; =============================================================================
2 ; BareMetal -- a 64-bit OS written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2011 Return Infinity -- see LICENSE.TXT
5 ; INIT HDD
6 ; =============================================================================
8 align 16
9 db 'DEBUG: INIT_HDD '
10 align 16
13 hdd_setup:
14 ; Read first sector (MBR) into memory
15 xor rax, rax
16 mov rdi, secbuffer0
17 push rdi
18 mov rcx, 1
19 call readsectors
20 pop rdi
22 cmp byte [0x0000000000005030], 0x01 ; Did we boot from a MBR drive
23 jne hdd_setup_no_mbr ; If not then we already have the correct sector
25 ; Grab the partition offset value for the first partition
26 mov eax, [rdi+0x01C6]
27 mov [fat16_PartitionOffset], eax
29 ; Read the first sector of the first partition
30 mov rdi, secbuffer0
31 push rdi
32 mov rcx, 1
33 call readsectors
34 pop rdi
36 hdd_setup_no_mbr:
37 ; Get the values we need to start using fat16
38 mov ax, [rdi+0x0b]
39 mov [fat16_BytesPerSector], ax ; This will probably be 512
40 mov al, [rdi+0x0d]
41 mov [fat16_SectorsPerCluster], al ; This will be 128 or less (Max cluster size is 64KiB)
42 mov ax, [rdi+0x0e]
43 mov [fat16_ReservedSectors], ax
44 mov [fat16_FatStart], eax
45 mov al, [rdi+0x10]
46 mov [fat16_Fats], al ; This will probably be 2
47 mov ax, [rdi+0x11]
48 mov [fat16_RootDirEnts], ax
49 mov ax, [rdi+0x16]
50 mov [fat16_SectorsPerFat], ax
52 ; Find out how many sectors are on the disk
53 xor eax, eax
54 mov ax, [rdi+0x13]
55 cmp ax, 0x0000
56 jne lessthan65536sectors
57 mov eax, [rdi+0x20]
58 lessthan65536sectors:
59 mov [fat16_TotalSectors], eax
61 ; Calculate the size of the drive in MiB
62 xor rax, rax
63 mov eax, [fat16_TotalSectors]
64 mov [hd1_maxlba], rax
65 shr rax, 11 ; rax = rax * 512 / 1048576
66 mov [hd1_size], eax ; in mebibytes
68 ; Calculate FAT16 info
69 xor rax, rax
70 xor rbx, rbx
71 mov ax, [fat16_SectorsPerFat]
72 shl ax, 1 ; quick multiply by two
73 add ax, [fat16_ReservedSectors]
74 mov [fat16_RootStart], eax
75 mov bx, [fat16_RootDirEnts]
76 shr ebx, 4 ; bx = (bx * 32) / 512
77 add ebx, eax ; BX now holds the datastart sector number
78 mov [fat16_DataStart], ebx
80 ret
83 ; =============================================================================
84 ; EOF