Page 1 of 1

Compression of bootsector code

Posted: Wed Oct 21, 2015 10:04 am
by snasim2002
So far, I have only used GRUB as my bootloader, but for the cause of adventure, I am intended to write my own boot loader. And, as I expected, the code that I want to put in the bootsector exceedes 512 bytes.
My question is , is it possible to compress raw binary data, without actually hampering its purpose ?

Re: Compression of bootsector code

Posted: Wed Oct 21, 2015 10:21 am
by bluemoon
To overcome the 512 byte barrier, there are 3 sane choices:
1. compress the boot code as you suggested
2. extend the capacity by chain loading more sectors.
3. a mix of the above.

Compress the boot code (1) while remain restricted in 512 byte does not give you much benefit, even with an extraordinary 10% compression and zero byte footprint.
Many people is doing (2), for example:
a) the 512 byte contain code to fetch more sectors (as fixed location on disk),
b) the loaded code contain file system logic, and loads the system files (e.g. kernel & initrd)
c) those system files are optionally compressed / packed.

Or otherwise, just use ready-made boot loader like GRUB.

Re: Compression of bootsector code

Posted: Wed Oct 21, 2015 10:23 am
by TightCoderEx
Probably a more effective thing to do would be this and in essence extends your boot sector to 4K

Code: Select all

                   BDA  equ  0x0040     ; BDA segment
                   MEM  equ  0x0013     ; Offset to memsize.

                ReBoot  equ  0x0019
       SYSTEM_SERVICES  equ  0x0015

        DISK_IO    equ  0x0013
              D_STATUS     equ  0x0001
                D_READ     equ  0x0200
               D_WRITE     equ  0x0300
                GET_DP     equ  0x0048

        jmp     0x7c0:Begin             ; For quirky BIOS's so we know CS = 7C0H
        
  Begin:

        mov     ax, BDA                 ; Point to BIOS DATA AREA
        mov     ds, ax
        mov     ax, [MEM]               ; Get number of 1k pages. Probably 27FH
        shl     ax, 6                   ; Convert to segment address
        sub     ah, 16

        cli
        mov     ss, ax
        xor     sp, sp
        sti

        mov     ax, cs
        add     ax, 32
        mov     es, ax
        xor     bx, bx                  ; ES:BX = 0x7c0:0

        mov     ax, D_READ + 8          ; AH = 2, AL = 8 sectors to read
        mov     cx, 2                   ; Begin @ sector 2
        mov     dh, 0                   ; Head 0, probably was that already
        int     DISK_IO
        jc      Failed

        jmp     0:0x7e00                ; Jump to second stage.

       times   506 - $  db 90H
                
  Failed:
       int     0x16
       int     ReBoot
               dw      0xAA55
This example sets up a stack just below EBDA and then loads another 8 sectors in @ 7E00H.