mbr.S 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 1.
  4. */
  5. .code16
  6. .section .text
  7. .globl _start
  8. _start:
  9. /* Standard MBR jump */
  10. .byte 0xEB, 0x44, 0x90
  11. .space 0x44 - 3, 0
  12. .org 0x44
  13. mbr_start:
  14. cli
  15. xorw %ax, %ax
  16. movw %ax, %ds
  17. movw %ax, %ss
  18. movw $0x7c00, %sp
  19. sti
  20. /* Save boot drive */
  21. movb %dl, boot_drive
  22. /* Find active partition */
  23. movw $0x1BE, %si
  24. movb $4, %cl
  25. find_active:
  26. cmpb $0x80, (%si)
  27. je found_active
  28. addw $16, %si
  29. decb %cl
  30. jnz find_active
  31. /* No active partition found */
  32. movw $no_boot_msg, %si
  33. call print_string
  34. jmp halt
  35. found_active:
  36. /* Load boot sector from partition start LBA */
  37. movl 8(%si), %eax /* LBA start of partition */
  38. movl %eax, part_lba
  39. /* Read boot sector to 0x7C00 */
  40. pushw %ds
  41. popw %es
  42. movw $0x7C00, %bx
  43. read_boot_sector:
  44. movl part_lba, %eax
  45. call read_sector_lba
  46. jc read_error
  47. /* Check boot signature */
  48. movw $0x7DFE, %si
  49. cmpw $0xAA55, (%si)
  50. jne read_error
  51. /* Jump to boot sector */
  52. movb boot_drive, %dl
  53. ljmp $0x0000, $0x7C00
  54. read_error:
  55. movw $read_err_msg, %si
  56. call print_string
  57. jmp halt
  58. /* Read one sector from LBA using INT 0x13 AH=0x42 */
  59. read_sector_lba:
  60. /* %eax = LBA, %es:%bx = buffer */
  61. pushw %ds
  62. movw $0x500, %si
  63. movw %si, %ds
  64. xorw %di, %di
  65. movb $0x10, (%di) /* packet size */
  66. movb $1, 1(%di) /* count = 1 */
  67. movw %bx, 4(%di) /* buffer offset */
  68. movw %es, 6(%di) /* buffer segment */
  69. movl %eax, 8(%di) /* LBA */
  70. xorl 12(%di), %eax /* LBA high = 0 */
  71. popw %ds
  72. movb $0x42, %ah
  73. movb boot_drive, %dl
  74. int $0x13
  75. ret
  76. /* Print null-terminated string at %si */
  77. print_string:
  78. pushw %ax
  79. pushw %bx
  80. movb $0x0E, %ah
  81. movb $0x00, %bh
  82. 1: lodsb
  83. testb %al, %al
  84. jz 2f
  85. int $0x10
  86. jmp 1b
  87. 2: popw %bx
  88. popw %ax
  89. ret
  90. halt:
  91. cli
  92. 1: hlt
  93. jmp 1b
  94. boot_drive:
  95. .byte 0
  96. part_lba:
  97. .long 0
  98. no_boot_msg:
  99. .asciz "No bootable partition"
  100. read_err_msg:
  101. .asciz "Boot sector read failed"
  102. /* Pad to partition table at offset 0x1BE (446) */
  103. .space 0x1BE - (. - _start), 0
  104. /* Partition table: one active partition at LBA 1 */
  105. .byte 0x80 /* bootable */
  106. .byte 0x00, 0x01, 0x00 /* CHS start */
  107. .byte 0x06 /* type: FAT16 LBA */
  108. .byte 0xFF, 0xFF, 0xFF /* CHS end */
  109. .long 1 /* LBA start (boot sector) */
  110. .long 131071 /* sector count */
  111. .space 48, 0 /* 3 empty entries */
  112. /* Boot signature */
  113. .section .bootsig
  114. .word 0xAA55