Skip to content

Commit

Permalink
[FREELDR][BTRFS] Fixed VBR build with GAS. Removed some redundant cod…
Browse files Browse the repository at this point in the history
…e and variables
  • Loading branch information
Extravert-ir committed Jul 15, 2018
1 parent f44eabb commit 239eec2
Showing 1 changed file with 114 additions and 119 deletions.
233 changes: 114 additions & 119 deletions boot/freeldr/bootsect/btrfs.S
Expand Up @@ -60,17 +60,15 @@ ChunkMapSize:
.byte 0
BootDrive:
.byte 0
PartitionStartSector:
.byte 0
PartitionStartLBA:
.quad HEX(3f) // hardcoded! usetup has to write proper value here during installation

TreeRootAddress:
.double 0
.quad 0
ChunkRootAddress:
.double 0
.quad 0
FsRootAddress:
.double 0
ExtentRootAddress:
.double 0
.quad 0

SectorSize:
.long 0
Expand All @@ -89,8 +87,6 @@ ChunkRootLevel:
.byte 0
FsRootLevel:
.byte 0
ExtentRootLevel:
.byte 0

main:
xor eax, eax // Setup segment registers
Expand All @@ -101,7 +97,6 @@ main:
mov bp, sp

mov byte ptr [BootDrive], dl
mov byte ptr [PartitionStartSector], HEX(3f) // hardcoded! need to get partition start address somehow

// Now check if this computer supports extended reads. This boot sector will not work without it
CheckInt13hExtensions:
Expand Down Expand Up @@ -134,7 +129,7 @@ LoadExtraBootCode:
xor ax, ax
mov es, ax // ReadSectors spoils es
mov si, DATA_BUFFER_OFFSET + HEX(40)
mov di, btrfsSignature
lea di, [btrfsSignature]
mov cx, 4 // magic string size (words)
repe cmpsw
je SignatureOk
Expand All @@ -145,12 +140,12 @@ LoadExtraBootCode:
SignatureOk:
// signature is ok
add si, 8 // logical address of the root tree root
mov di, TreeRootAddress
lea di, [TreeRootAddress]
mov cx, 8 // read both root tree root and chunk tree root
rep movsw
// read sizes
add si, HEX(30)
mov di, SectorSize // read all 5 values
lea di, [SectorSize] // read all 5 values
mov cx, 10
rep movsw
// read both levels
Expand All @@ -176,72 +171,6 @@ ReadSysChunk:

jmp SetTreeRoots

// ax - key addr, bx - chunk addr
InsertChunk:
push bx
push ax
xor ecx, ecx // index
ic_loop:
std // numbers are little-engian, going right-to-left
mov si, CHUNK_MAP_OFFSET
lea si, [esi+ecx*8]
lea si, [esi+ecx*8]
lea si, [esi+ecx*8] // shift by 24 bytes

inc cx
cmp cl, byte ptr [ChunkMapSize]
jg ic_insert

lea si, [di+4] // set to the high dword of the 8-byte number
lea di, [eax+KEY_SIZE-4]

cmpsd
jl ic_loop
cmpsd
jl ic_loop
lea si, [si+4]

// numbers are greater - need to insert Here
// shifting all to right by one element
ic_insert:
dec cx
push si
movzx eax, byte ptr [ChunkMapSize] // number of elements

mov si, CHUNK_MAP_OFFSET
lea si, [esi+eax*8]
lea si, [esi+eax*8]
lea si, [esi+eax*8-4] // setting to the high dword of the last element

mov di, si
add di, 20 // last byte of the last + 1 element (-4 bytes)

sub ax, cx
mov bx, 6
mul bx // 24/4 because of dwords
mov cx, ax // number of elements to shift
rep movsd

// finally we can write the element
cld
pop di

pop si // key address
add si, 9 // move to 'offset' field
movsd
movsd // logical address loaded!

// time for stripes
pop si // chunk item address, length is the first field
movsd
movsd

add si, 48 // move to offset of the first stripe (we read only first one)
movsd
movsd
inc byte ptr [ChunkMapSize]
ret


// Reads logical sectors into ES:[BX]
// EDX:EAX has logical sector number to read
Expand All @@ -251,9 +180,9 @@ ReadSectors:
push bp
mov bp, sp
sub sp, 2
movzx esi, byte ptr [PartitionStartSector]
add eax, esi
adc edx, 0

add eax, dword ptr [PartitionStartLBA]
adc edx, dword ptr [PartitionStartLBA+4]
ReadSectors_loop:
pushad // Save logical sector number & sector count

Expand Down Expand Up @@ -305,7 +234,7 @@ PrintCustomError:
call PutChars // Display it

Reboot:
mov si,msgAnyKey // Press any key message
lea si, [msgAnyKey] // Press any key message
call PutChars // Display it
xor ax,ax
int HEX(16) // Wait for a keypress
Expand Down Expand Up @@ -344,18 +273,16 @@ SetTreeRoots:
shr dword ptr [LeafSize], 9 // leafsize is deprecated

// finding FS_TREE root
mov si, TreeRootAddress
lea si, [TreeRootAddress]
mov cl, byte ptr [TreeRootLevel]

push si // safe ROOT_TREE data for future
push cx

// put the key on stack
data32 push 0
data32 push 0
xor eax, eax
push eax
push eax
mov byte ptr [esp-1], ROOT_ITEM_KEY
dec sp
data32 push 0
push eax
data32 push FS_TREE_OBJECTID
call SearchTree
add sp, 17 // setting stack back
Expand All @@ -370,39 +297,39 @@ SetTreeRoots:
movsd

// finding EXTENT_TREE root
pop cx
pop si
//pop cx
//pop si

// put the key on stack
data32 push 0
data32 push 0
mov byte ptr [esp-1], ROOT_ITEM_KEY
dec sp
data32 push 0
data32 push EXTENT_TREE_OBJECTID
call SearchTree
add sp, 17 // setting stack back
//data32 push 0
//data32 push 0
//mov byte ptr [esp-1], ROOT_ITEM_KEY
//dec sp
//data32 push 0
//data32 push EXTENT_TREE_OBJECTID
//call SearchTree
//add sp, 17 // setting stack back

// bx - pointer to ROOT_ITEM
mov al, byte ptr [bx+238] // moving level
mov byte ptr [ExtentRootLevel], al
cld
lea si, [bx+176] // moving address
lea di, [ExtentRootAddress]
movsd
movsd
//mov al, byte ptr [bx+238] // moving level
//mov byte ptr [ExtentRootLevel], al
//cld
//lea si, [bx+176] // moving address
//lea di, [ExtentRootAddress]
//movsd
//movsd

// now we need to find DIR_ITEM_KEY with freeldr.sys hash
mov si, FsRootAddress
lea si, [FsRootAddress]
mov cl, byte ptr [FsRootLevel]

data32 push 0
xor eax, eax
push eax
push dword ptr [filenameCrc]
mov byte ptr [esp-1], DIR_ITEM_KEY
dec sp
data32 push 0
push 0
push 256 // root dir objectid
push eax
data32 push 256 // root dir objectid
call SearchTree
add sp, 17 // setting stack back

Expand Down Expand Up @@ -430,16 +357,17 @@ ParseDirItem_loop_2:
jmp ParseDirItem_loop

FreeLdrNotFound:
mov si, notFoundError
lea si, [notFoundError]
call PrintCustomError

FreeLdrFound:
// freeldr objectid is the first qword of DIR_ITEM structure
mov si, FsRootAddress
lea si, [FsRootAddress]
mov cl, byte ptr [FsRootLevel]

data32 push 0
data32 push 0
xor eax, eax
push eax
push eax
mov byte ptr [esp-1], EXTENT_DATA_KEY
dec sp
push dword ptr [bx+4]
Expand Down Expand Up @@ -481,6 +409,74 @@ FreeLdrFound:
/* Transfer execution to the bootloader */
ljmp16 0, FREELDR_BASE


// ax - key addr, bx - chunk addr
InsertChunk:
push bx
push ax
xor ecx, ecx // index
ic_loop:
std // numbers are little-engian, going right-to-left
mov si, CHUNK_MAP_OFFSET
lea si, [esi+ecx*8]
lea si, [esi+ecx*8]
lea si, [esi+ecx*8] // shift by 24 bytes

inc cx
cmp cl, byte ptr [ChunkMapSize]
jg ic_insert

lea si, [di+4] // set to the high dword of the 8-byte number
lea di, [eax+KEY_SIZE-4]

cmpsd
jl ic_loop
cmpsd
jl ic_loop
lea si, [si+4]

// numbers are greater - need to insert Here
// shifting all to right by one element
ic_insert:
dec cx
push si
movzx eax, byte ptr [ChunkMapSize] // number of elements

mov si, CHUNK_MAP_OFFSET
lea si, [esi+eax*8]
lea si, [esi+eax*8]
lea si, [esi+eax*8-4] // setting to the high dword of the last element

mov di, si
add di, 20 // last byte of the last + 1 element (-4 bytes)

sub ax, cx
mov bx, 6
mul bx // 24/4 because of dwords
mov cx, ax // number of elements to shift
rep movsd

// finally we can write the element
cld
pop di

pop si // key address
add si, 9 // move to 'offset' field
movsd
movsd // logical address loaded!

// time for stripes
pop si // chunk item address, length is the first field
movsd
movsd

add si, 48 // move to offset of the first stripe (we read only first one)
movsd
movsd
inc byte ptr [ChunkMapSize]
ret


// si - logical addr of the root, cl - root level, [bp+4] - key to find
// returns ebx - pointer to item, ecx - item length
SearchTree:
Expand Down Expand Up @@ -657,7 +653,7 @@ ConvertAddress_notFound:
movsd
movsd

mov si, ChunkRootAddress
lea si, [ChunkRootAddress]
mov cl, byte ptr [ChunkRootLevel]

// put the key on stack
Expand All @@ -666,8 +662,7 @@ ConvertAddress_notFound:
mov byte ptr [esp-1], CHUNK_ITEM_KEY
dec sp
data32 push 0
push 0
push FIRST_CHUNK_TREE_OBJECTID
data32 push FIRST_CHUNK_TREE_OBJECTID
call SearchTree
add sp, 17 // setting stack back

Expand Down

0 comments on commit 239eec2

Please sign in to comment.