mbr.S 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * MBR boot code: finds active partition, loads its boot sector, jumps to it.
  3. * Placed at sector 0 of usb.img. Partition table points to boot sector at sector 63.
  4. *
  5. * The boot sector is loaded to 0x7C00, i.e. over this code, so the MBR first
  6. * relocates its whole sector to 0x0600 and continues from there. Data
  7. * references (scratch, partition table, messages) are relative to the
  8. * relocated copy via reloc_shift.
  9. */
  10. .code16
  11. .section .text
  12. .equ reloc_base, 0x0600
  13. .set reloc_shift, reloc_base - 0x7C00
  14. .globl _start
  15. _start:
  16. /* Standard MBR jump to mbr_start at offset 0x44 */
  17. .byte 0xEB, 0x42, 0x90
  18. .space 0x44 - 3, 0
  19. .org 0x44
  20. mbr_start:
  21. cli
  22. xorw %ax, %ax
  23. movw %ax, %ds
  24. movw %ax, %es
  25. movw %ax, %ss
  26. movw $0x7c00, %sp
  27. sti
  28. /* Relocate this sector to 0x0600 and continue there. */
  29. movw $0x7c00, %si
  30. movw $reloc_base, %di
  31. movw $256, %cx
  32. cld
  33. rep movsw
  34. ljmp $0x0000, $relocated+reloc_shift
  35. relocated:
  36. /* Save boot drive */
  37. movb %dl, boot_drive+reloc_shift
  38. /* Find active partition */
  39. movw $part_table+reloc_shift, %si
  40. movb $4, %cl
  41. find_active:
  42. cmpb $0x80, (%si)
  43. je found_active
  44. addw $16, %si
  45. decb %cl
  46. jnz find_active
  47. /* No active partition found */
  48. movw $no_boot_msg+reloc_shift, %si
  49. call print_string
  50. jmp halt
  51. found_active:
  52. /* Load boot sector from partition start LBA */
  53. movl 8(%si), %eax /* LBA start of partition */
  54. movl %eax, part_lba+reloc_shift
  55. /* Read boot sector to 0x7C00 */
  56. pushw %ds
  57. popw %es
  58. movw $0x7C00, %bx
  59. read_boot_sector:
  60. movl part_lba+reloc_shift, %eax
  61. call read_sector_lba
  62. jc read_error
  63. /* Check boot signature */
  64. movw $0x7DFE, %si
  65. cmpw $0xAA55, (%si)
  66. jne read_error
  67. /* Jump to boot sector */
  68. movb boot_drive+reloc_shift, %dl
  69. ljmp $0x0000, $0x7C00
  70. read_error:
  71. movw $read_err_msg+reloc_shift, %si
  72. call print_string
  73. jmp halt
  74. /* Read one sector from LBA using INT 0x13 AH=0x42 (DS = 0) */
  75. read_sector_lba:
  76. /* %eax = LBA, %es:%bx = buffer */
  77. movw $0x500, %di
  78. movb $0x10, (%di) /* packet size */
  79. movb $0, 1(%di) /* reserved */
  80. movw $1, 2(%di) /* count = 1 */
  81. movw %bx, 4(%di) /* buffer offset */
  82. movw %es, 6(%di) /* buffer segment */
  83. movl %eax, 8(%di) /* LBA */
  84. movl $0, 12(%di) /* LBA high = 0 */
  85. movw $0x500, %si /* DS:SI -> disk address packet */
  86. movb $0x42, %ah
  87. movb boot_drive+reloc_shift, %dl
  88. int $0x13
  89. ret
  90. /* Print null-terminated string at %si */
  91. print_string:
  92. pushw %ax
  93. pushw %bx
  94. movb $0x0E, %ah
  95. movb $0x00, %bh
  96. 1: lodsb
  97. testb %al, %al
  98. jz 2f
  99. int $0x10
  100. jmp 1b
  101. 2: popw %bx
  102. popw %ax
  103. ret
  104. halt:
  105. cli
  106. 1: hlt
  107. jmp 1b
  108. boot_drive:
  109. .byte 0
  110. part_lba:
  111. .long 0
  112. no_boot_msg:
  113. .asciz "No bootable partition"
  114. read_err_msg:
  115. .asciz "Boot sector read failed"
  116. /* Pad to partition table at offset 0x1BE (446) */
  117. .space 0x1BE - (. - _start), 0
  118. part_table:
  119. /* Partition 1: active, starts at LBA 63 (CHS 0/1/1, the classic DOS
  120. * start) and holds the boot sector (LBA 63) and kernel (LBA 64+).
  121. * The CHS values match the LBA values for the usual 255 heads /
  122. * 63 sectors geometry; BIOSes are known to validate that. */
  123. .byte 0x80 /* bootable */
  124. .byte 0x01, 0x01, 0x00 /* CHS start: C=0 H=1 S=1 (LBA 63) */
  125. .byte 0x06 /* type: FAT16 */
  126. .byte 0xFE, 0x3F, 0x06 /* CHS end: C=6 H=254 S=63 (LBA 112454) */
  127. .long 63 /* LBA start (boot sector) */
  128. .long 112392 /* sector count */
  129. /* Partition 2: dummy in the bare MBR. usb.img replaces this entry
  130. * with the rootfs partition (same start, LBA 112455); keeping a second
  131. * valid entry makes the stick look like a USB hard disk to old AMI
  132. * BIOSes (e.g. the ASUS AT3IONT-I), which classify a single-partition
  133. * MBR as a USB-ZIP disk and never offer it. Only partition 1 is
  134. * active. */
  135. .byte 0x00 /* not bootable */
  136. .byte 0x00, 0x01, 0x07 /* CHS start: C=7 H=0 S=1 (LBA 112455) */
  137. .byte 0x21 /* type: dummy */
  138. .byte 0x28, 0x20, 0x08 /* CHS end: C=8 H=40 S=32 (LBA 131071) */
  139. .long 112455
  140. .long 18617
  141. .space 32, 0 /* 2 empty entries */
  142. /* Boot signature at offset 510 */
  143. .space 510 - (. - _start), 0
  144. .word 0xAA55