|
@@ -1,15 +1,23 @@
|
|
|
/*
|
|
/*
|
|
|
* MBR boot code: finds active partition, loads its boot sector, jumps to it.
|
|
* MBR boot code: finds active partition, loads its boot sector, jumps to it.
|
|
|
- * Placed at sector 0 of usb.img. Partition table points to boot sector at sector 1.
|
|
|
|
|
|
|
+ * Placed at sector 0 of usb.img. Partition table points to boot sector at sector 63.
|
|
|
|
|
+ *
|
|
|
|
|
+ * The boot sector is loaded to 0x7C00, i.e. over this code, so the MBR first
|
|
|
|
|
+ * relocates its whole sector to 0x0600 and continues from there. Data
|
|
|
|
|
+ * references (scratch, partition table, messages) are relative to the
|
|
|
|
|
+ * relocated copy via reloc_shift.
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
.code16
|
|
.code16
|
|
|
.section .text
|
|
.section .text
|
|
|
|
|
|
|
|
|
|
+ .equ reloc_base, 0x0600
|
|
|
|
|
+ .set reloc_shift, reloc_base - 0x7C00
|
|
|
|
|
+
|
|
|
.globl _start
|
|
.globl _start
|
|
|
_start:
|
|
_start:
|
|
|
- /* Standard MBR jump */
|
|
|
|
|
- .byte 0xEB, 0x44, 0x90
|
|
|
|
|
|
|
+ /* Standard MBR jump to mbr_start at offset 0x44 */
|
|
|
|
|
+ .byte 0xEB, 0x42, 0x90
|
|
|
.space 0x44 - 3, 0
|
|
.space 0x44 - 3, 0
|
|
|
|
|
|
|
|
.org 0x44
|
|
.org 0x44
|
|
@@ -17,15 +25,25 @@ mbr_start:
|
|
|
cli
|
|
cli
|
|
|
xorw %ax, %ax
|
|
xorw %ax, %ax
|
|
|
movw %ax, %ds
|
|
movw %ax, %ds
|
|
|
|
|
+ movw %ax, %es
|
|
|
movw %ax, %ss
|
|
movw %ax, %ss
|
|
|
movw $0x7c00, %sp
|
|
movw $0x7c00, %sp
|
|
|
sti
|
|
sti
|
|
|
|
|
|
|
|
|
|
+ /* Relocate this sector to 0x0600 and continue there. */
|
|
|
|
|
+ movw $0x7c00, %si
|
|
|
|
|
+ movw $reloc_base, %di
|
|
|
|
|
+ movw $256, %cx
|
|
|
|
|
+ cld
|
|
|
|
|
+ rep movsw
|
|
|
|
|
+ ljmp $0x0000, $relocated+reloc_shift
|
|
|
|
|
+
|
|
|
|
|
+relocated:
|
|
|
/* Save boot drive */
|
|
/* Save boot drive */
|
|
|
- movb %dl, boot_drive
|
|
|
|
|
|
|
+ movb %dl, boot_drive+reloc_shift
|
|
|
|
|
|
|
|
/* Find active partition */
|
|
/* Find active partition */
|
|
|
- movw $0x1BE, %si
|
|
|
|
|
|
|
+ movw $part_table+reloc_shift, %si
|
|
|
movb $4, %cl
|
|
movb $4, %cl
|
|
|
|
|
|
|
|
find_active:
|
|
find_active:
|
|
@@ -36,14 +54,14 @@ find_active:
|
|
|
jnz find_active
|
|
jnz find_active
|
|
|
|
|
|
|
|
/* No active partition found */
|
|
/* No active partition found */
|
|
|
- movw $no_boot_msg, %si
|
|
|
|
|
|
|
+ movw $no_boot_msg+reloc_shift, %si
|
|
|
call print_string
|
|
call print_string
|
|
|
jmp halt
|
|
jmp halt
|
|
|
|
|
|
|
|
found_active:
|
|
found_active:
|
|
|
/* Load boot sector from partition start LBA */
|
|
/* Load boot sector from partition start LBA */
|
|
|
movl 8(%si), %eax /* LBA start of partition */
|
|
movl 8(%si), %eax /* LBA start of partition */
|
|
|
- movl %eax, part_lba
|
|
|
|
|
|
|
+ movl %eax, part_lba+reloc_shift
|
|
|
|
|
|
|
|
/* Read boot sector to 0x7C00 */
|
|
/* Read boot sector to 0x7C00 */
|
|
|
pushw %ds
|
|
pushw %ds
|
|
@@ -51,7 +69,7 @@ found_active:
|
|
|
movw $0x7C00, %bx
|
|
movw $0x7C00, %bx
|
|
|
|
|
|
|
|
read_boot_sector:
|
|
read_boot_sector:
|
|
|
- movl part_lba, %eax
|
|
|
|
|
|
|
+ movl part_lba+reloc_shift, %eax
|
|
|
call read_sector_lba
|
|
call read_sector_lba
|
|
|
jc read_error
|
|
jc read_error
|
|
|
|
|
|
|
@@ -61,31 +79,29 @@ read_boot_sector:
|
|
|
jne read_error
|
|
jne read_error
|
|
|
|
|
|
|
|
/* Jump to boot sector */
|
|
/* Jump to boot sector */
|
|
|
- movb boot_drive, %dl
|
|
|
|
|
|
|
+ movb boot_drive+reloc_shift, %dl
|
|
|
ljmp $0x0000, $0x7C00
|
|
ljmp $0x0000, $0x7C00
|
|
|
|
|
|
|
|
read_error:
|
|
read_error:
|
|
|
- movw $read_err_msg, %si
|
|
|
|
|
|
|
+ movw $read_err_msg+reloc_shift, %si
|
|
|
call print_string
|
|
call print_string
|
|
|
jmp halt
|
|
jmp halt
|
|
|
|
|
|
|
|
-/* Read one sector from LBA using INT 0x13 AH=0x42 */
|
|
|
|
|
|
|
+/* Read one sector from LBA using INT 0x13 AH=0x42 (DS = 0) */
|
|
|
read_sector_lba:
|
|
read_sector_lba:
|
|
|
/* %eax = LBA, %es:%bx = buffer */
|
|
/* %eax = LBA, %es:%bx = buffer */
|
|
|
- pushw %ds
|
|
|
|
|
- movw $0x500, %si
|
|
|
|
|
- movw %si, %ds
|
|
|
|
|
- xorw %di, %di
|
|
|
|
|
|
|
+ movw $0x500, %di
|
|
|
movb $0x10, (%di) /* packet size */
|
|
movb $0x10, (%di) /* packet size */
|
|
|
- movb $1, 1(%di) /* count = 1 */
|
|
|
|
|
|
|
+ movb $0, 1(%di) /* reserved */
|
|
|
|
|
+ movw $1, 2(%di) /* count = 1 */
|
|
|
movw %bx, 4(%di) /* buffer offset */
|
|
movw %bx, 4(%di) /* buffer offset */
|
|
|
movw %es, 6(%di) /* buffer segment */
|
|
movw %es, 6(%di) /* buffer segment */
|
|
|
movl %eax, 8(%di) /* LBA */
|
|
movl %eax, 8(%di) /* LBA */
|
|
|
- xorl 12(%di), %eax /* LBA high = 0 */
|
|
|
|
|
- popw %ds
|
|
|
|
|
|
|
+ movl $0, 12(%di) /* LBA high = 0 */
|
|
|
|
|
|
|
|
|
|
+ movw $0x500, %si /* DS:SI -> disk address packet */
|
|
|
movb $0x42, %ah
|
|
movb $0x42, %ah
|
|
|
- movb boot_drive, %dl
|
|
|
|
|
|
|
+ movb boot_drive+reloc_shift, %dl
|
|
|
int $0x13
|
|
int $0x13
|
|
|
ret
|
|
ret
|
|
|
|
|
|
|
@@ -122,15 +138,31 @@ read_err_msg:
|
|
|
/* Pad to partition table at offset 0x1BE (446) */
|
|
/* Pad to partition table at offset 0x1BE (446) */
|
|
|
.space 0x1BE - (. - _start), 0
|
|
.space 0x1BE - (. - _start), 0
|
|
|
|
|
|
|
|
-/* Partition table: one active partition at LBA 1 */
|
|
|
|
|
|
|
+part_table:
|
|
|
|
|
+ /* Partition 1: active, starts at LBA 63 (CHS 0/1/1, the classic DOS
|
|
|
|
|
+ * start) and holds the boot sector (LBA 63) and kernel (LBA 64+).
|
|
|
|
|
+ * The CHS values match the LBA values for the usual 255 heads /
|
|
|
|
|
+ * 63 sectors geometry; BIOSes are known to validate that. */
|
|
|
.byte 0x80 /* bootable */
|
|
.byte 0x80 /* bootable */
|
|
|
- .byte 0x00, 0x01, 0x00 /* CHS start */
|
|
|
|
|
- .byte 0x06 /* type: FAT16 LBA */
|
|
|
|
|
- .byte 0xFF, 0xFF, 0xFF /* CHS end */
|
|
|
|
|
- .long 1 /* LBA start (boot sector) */
|
|
|
|
|
- .long 131071 /* sector count */
|
|
|
|
|
- .space 48, 0 /* 3 empty entries */
|
|
|
|
|
-
|
|
|
|
|
- /* Boot signature */
|
|
|
|
|
-.section .bootsig
|
|
|
|
|
|
|
+ .byte 0x01, 0x01, 0x00 /* CHS start: C=0 H=1 S=1 (LBA 63) */
|
|
|
|
|
+ .byte 0x06 /* type: FAT16 */
|
|
|
|
|
+ .byte 0xFE, 0x3F, 0x06 /* CHS end: C=6 H=254 S=63 (LBA 112454) */
|
|
|
|
|
+ .long 63 /* LBA start (boot sector) */
|
|
|
|
|
+ .long 112392 /* sector count */
|
|
|
|
|
+
|
|
|
|
|
+ /* Partition 2: dummy. Old AMI BIOSes (e.g. the ASUS AT3IONT-I)
|
|
|
|
|
+ * classify a single-partition MBR as a USB-ZIP disk, which is never
|
|
|
|
|
+ * offered as a hard-disk boot option; a second valid entry makes the
|
|
|
|
|
+ * stick look like a USB hard disk (same trick RMPrepUSB/Easy2Boot
|
|
|
|
|
+ * use). It is never booted: only partition 1 is active. */
|
|
|
|
|
+ .byte 0x00 /* not bootable */
|
|
|
|
|
+ .byte 0x00, 0x01, 0x07 /* CHS start: C=7 H=0 S=1 (LBA 112455) */
|
|
|
|
|
+ .byte 0x21 /* type: dummy */
|
|
|
|
|
+ .byte 0x28, 0x20, 0x08 /* CHS end: C=8 H=40 S=32 (LBA 131071) */
|
|
|
|
|
+ .long 112455
|
|
|
|
|
+ .long 18617
|
|
|
|
|
+ .space 32, 0 /* 2 empty entries */
|
|
|
|
|
+
|
|
|
|
|
+ /* Boot signature at offset 510 */
|
|
|
|
|
+ .space 510 - (. - _start), 0
|
|
|
.word 0xAA55
|
|
.word 0xAA55
|