/* Boot sector: load contiguous kernel sectors 1..N to 0x7E00, then jump to 0x7E00 in real mode (VESA stage handles the rest). */ .code16 .section .text .globl _start _start: /* Short jump over the BPB and the fixed header slots to loader_start. * The displacement is computed so the header layout below can change * without leaving the jump inside a header field (a hardcoded 0x40 * landed in pack_lba, where 0x0E decoded as a harmless push cs and * 0x0F as a faulting larw). */ .byte 0xEB .byte loader_start - _start - 2 .byte 0x90 /* OEM name, then a deliberately empty BPB. This sector is not a FAT * volume: the old FAT-shaped BPB made VFS mount the raw kernel * partition as a fake FAT32 volume, and UEFI firmware tried to mount * it. The BIOS only needs the fields below (kernel_sectors at 0x3E, * kernel_lba, pack_lba), so bytes 0x0B..0x3D stay zero. */ .ascii "NEBOS " /* OEM name */ .space 0x3E - 0x0B, 0 .org 0x3E kernel_sectors: .word 1 /* First kernel sector (LBA). Fixed header slot so the image builders * can patch it without depending on the code layout: the floppy image * keeps 1, the USB/HDD image patches the low byte (0x40 = LBA 64). */ .org 0x40 kernel_lba: .word 1 /* Rootfs image LBA (see rootfs.h). Patched by the image builders: * floppy/final.bin point it just past the padded kernel, usb.img at * its rootfs partition. */ .org 0x42 pack_lba: .long 1 .org 0x46 loader_start: xorw %ax, %ax movw %ax, %ds movw %ax, %ss movw $0x7c00, %sp movb %dl, boot_drive cld movw $0x0003, %ax int $0x10 /* First visible sign of life. This is before VBE, paging and the * kernel, and it is the only output a machine without serial has: * if the panel shows this line, the BIOS reached our boot sector. */ movw $boot_msg, %si call print_string /* The kernel load can take a while on a BIOS USB stack; say what is * happening instead of leaving the operator with a blank screen. */ movw $loading_msg, %si call print_string load_kernel: movw kernel_sectors, %cx movw kernel_lba, %si movw $0x07e0, %ax movw %ax, %es /* One INT 13h per run of sectors into ES:0, then ES advances, so the * offset never wraps. EDD reads up to 64 sectors; CHS (floppy) reads * up to the end of the track and never across a 64 KB physical ISA * DMA boundary (the buffer starts 512-byte aligned at 0x7E00). One * call per sector made the kernel load ~700 BIOS calls, each of which * can cost a full floppy revolution. Errors: three tries per run, * then one sector per call (the old behaviour), three tries again, * then "Disk read error". */ load_loop: testw %cx, %cx jz load_done pushw %cx pushw %si movw %cx, %ax cmpw chunk_max, %ax jbe 1f movw chunk_max, %ax 1: movb boot_drive, %dl cmpb $0x80, %dl jb read_chs movw %ax, cur_count movw %ax, 0x502 xorw %ax, %ax movw %ax, 0x504 movw %ax, 0x50a movw %ax, 0x50c movw %ax, 0x50e movb $0x10, 0x500 movb %al, 0x501 movw %es, 0x506 movw %si, 0x508 movw $0x500, %si movb $0x42, %ah jmp do_int13 read_chs: movw %ax, %bx /* bx = sectors to read */ movw %es, %ax shlw $4, %ax /* low 16 bits of the physical address */ negw %ax /* bytes to the next 64 KB boundary */ shrw $9, %ax jz 3f /* 0: a whole 64 KB window is free */ cmpw %ax, %bx jbe 3f movw %ax, %bx 3: movw %si, %ax xorw %dx, %dx movw $18, %di divw %di /* ax = track index, dx = sector - 1 */ subw %dx, %di /* sectors left on this track */ cmpw %di, %bx jbe 4f movw %di, %bx 4: movw %bx, cur_count movb %dl, %cl incb %cl xorw %dx, %dx movw $2, %di divw %di movb %dl, %dh movb %al, %ch movb boot_drive, %dl movb $0x02, %ah movb %bl, %al xorw %bx, %bx do_int13: int $0x13 popw %si popw %cx jnc 2f movb boot_drive, %dl /* reset the drive, then retry */ xorw %ax, %ax int $0x13 incb tries cmpb $3, tries jb load_loop movb $0, tries cmpw $1, chunk_max je read_error movw $1, chunk_max /* multi-sector refused: old behaviour */ jmp load_loop 2: movb $0, tries movw cur_count, %ax addw %ax, %si subw %ax, %cx shlw $5, %ax movw %es, %bx /* not DX: DL carries the boot drive */ addw %ax, %bx /* into the VESA stage */ movw %bx, %es jmp load_loop load_done: /* Second marker: the whole kernel image was read without an INT 13h * error. It stays on screen if the VESA stage fails or hangs before * it switches modes. */ movw $loaded_msg, %si call print_string /* Jump to VESA stage at 0x7E00 (real mode). */ ljmp $0x0000, $0x7E00 boot_drive: .byte 0 chunk_max: .word 64 cur_count: .word 0 tries: .byte 0 /* INT 13h error: say so instead of halting silently. */ read_error: movw $disk_err_msg, %si call print_string halt: cli 1: hlt jmp 1b /* Print the NUL-terminated string at DS:SI with the BIOS teletype service. */ print_string: pushw %ax pushw %bx movb $0x0E, %ah xorw %bx, %bx 1: lodsb testb %al, %al jz 2f int $0x10 jmp 1b 2: popw %bx popw %ax ret boot_msg: .asciz "Nebos starting...\r\n" loading_msg: .asciz "Loading kernel...\r\n" loaded_msg: .asciz "Kernel loaded\r\n" disk_err_msg: .asciz "Disk read error\r\n" .org 0x1BE /* MBR partition table: one active partition at LBA 0 (USB/HDD). */ .byte 0x80 /* bootable */ .byte 0x00, 0x01, 0x00 /* CHS start: C=0, H=0, S=1 */ .byte 0x06 /* type: FAT16 LBA (matches BPB) */ .byte 0xFF, 0xFF, 0xFF /* CHS end: max values */ .long 0 /* LBA start */ .long 131072 /* sector count (64MB) */ .space 48, 0 /* 3 empty partition entries */ .org 0x1FE .word 0xAA55