stage0: fix some bugs

main
anna 12 months ago
parent ecf4c94da4
commit 6886d43eef
Signed by: fef
GPG Key ID: 2585C2DC6D79B485

@ -159,6 +159,8 @@ GLOBL _start
rep stosb
mov %di, %sp /* %sp = 0x7800:8000 = 0x80000 */
sti /* we're safe again, i think */
/* say hello */
mov $msg_loader_info, %si
call print
@ -336,52 +338,67 @@ LOCAL start2
and %ebx, %edi /* %edi = 0 (crc32 returns the inverted value) */
jnz err_bad_gpt_csum
/* search the partition table for our GUID */
/*
* Step 5: Search the GPT for stage1
*/
pop %bx /* restore entry size */ /* '1 ^ */
sub $(bussy_guid_end - bussy_guid), %bx /* entry size -= sizeof(bussy_guid) */
/* this loop uses %ax for temporarily storing `entry count` and
* `sizeof(bussy_guid)` because xchg with %ax is only one byte */
xor %si, %si
1: push %cx /* save remaining entry count */ /* '1 v */
mov $bussy_guid, %si
push %si /* save current position */ /* '2 v */
mov $bussy_guid, %di
mov $(bussy_guid_end - bussy_guid), %cx
repe cmpsb %es:(%di), %fs:(%si)
pop %si /* restore current position */ /* '2 ^ */
pop %cx /* restore remaining entry count */ /* '1 ^ */
je 2f /* found it */
add %bx, %di /* %di += entry size */
add %bx, %si /* %si += entry size */
loopne 1b /* only loop if %di didn't wrap around */
jmp err_no_stage1
/* gotcha! now load the first 64 K from the partition */
/*
* Step 6: Load stage1 to the beginning of low memory (0x0500)
*/
2: push %dx /* save drive number */ /* '1 v */
xor %ax, %ax
mov $1, %dl /* %dh is known to be 0x00 */
divw (drive_params + 0x18) /* %ax = 0x10000 / sector size = sector count */
mov $0x8000, %ax
xor %dx, %dx
divw (drive_params + 0x18) /* %ax = 0x8000 / sector size = sector count */
xchg %ax, %cx /* %cx = sector count (xchg saves 1 byte) */
mov %fs:0x20(%di), %eax /* %eax = LBA[31:0] */
mov %fs:0x24(%di), %bx /* %bx = LBA[47:32] */
mov %fs:0x20(%si), %eax /* %eax = LBA[31:0] */
mov %fs:0x24(%si), %bx /* %bx = LBA[47:32] */
xor %di, %di
pop %dx /* restore drive number */ /* '1 ^ */
call read_lba
/* check the stage1 header */
lods %fs:(%di), %ax /* offset 0x00: magic number */
cmp $0xacab, %ax
/* these values are for stage1 */
push %eax /* boot partition LBA[31:0] */ /* '1 v */
push %bx /* boot partition LBA[47:32] */ /* '2 v */
push %dx /* boot drive number */ /* '3 v */
/*
* Step 7: Check stage1's magic number and CRC
*/
xor %si, %si
cmp $0xacab, %fs:(%si) /* offset 0x00: magic number */
jne err_bad_stage1_magic
lods %fs:(%di), %ax /* offset 0x02: byte count for CRC */
xchg %cx, %ax
mov %fs:0x02(%si), %cx /* offset 0x02: byte count for CRC */
xor %eax, %eax
xchg %fs:(%di), %eax /* offset 0x04: CRC (replace with 0) */
push %ax /* save CRC[15:0] (crc32 clobbers %al) */ /* '1 v */
xchg %fs:0x04(%si), %eax /* offset 0x04: CRC (replace with 0) */
push %ax /* save CRC[15:0] (crc32 clobbers %al) */ /* '4 v */
call crc32
pop %ax /* restore CRC[15:0] */ /* '1 ^ */
and %ebx, %eax /* check CRC and clear %eax */
pop %ax /* restore CRC[15:0] */ /* '4 ^ */
xor %ebx, %eax /* check CRC and clear %eax */
jnz err_bad_stage1_csum
/*
* Step 8: Jump to stage1 (finally)
*
* The three remaining items on the stack are for stage1
*/
ljmp $0x0000, $0x0508 /* entry point is right after the 8-byte header */
ljmp $0x0000, $0x0510 /* entry point is right after the 16-byte header */
END start2
/*

Loading…
Cancel
Save