Page 1 of 1

Boot floppy without BIOS disk services

Posted: Sat Dec 07, 2019 3:11 pm
by BenLunt
Over at Stack Overflow, someone asked about loading additional sectors from a floppy without using the BIOS disk services. Well, curiosity got me and I had to try. (Actual code is here)

The trick wasn't to simply read more sectors from the disk, this would have been easy. The trick was to do it without assuming anything about the hardware. Granted, I have to assume the first controller and the first drive on the controller, but other than this, all remaining must be not assumed.

For example:
- I reset the controller
- Receive/Send all four Sense Interrupts
- Send the Specify command
- Seek to Cylinder 0
- Set up the DMA, and finally:
- Read a sector from the disk
All within the first 512 bytes, all without using the BIOS disk services.

I had to try it, and found out that it is actually possible. The current code needs a little work. For example, I currently don't wait 2 seconds for the disk to spin up, but it should already be spinning since the BIOS just loaded the first sector. The code needs to be cleaned up a little and a more accurate delay needs to be found, but it does show that it can be done.

It runs on Bochs and QEMU just fine. (So that you don't have to build it, the 1.44meg image is available on the github link shown above.)

It was a fun little experiment and I thought others here might be interested in seeing it.

Ben
- http://www.fysnet.net/osdesign_book_series.htm

Re: Boot floppy without BIOS disk services

Posted: Sun Dec 08, 2019 12:20 pm
by DavidCooper
My OS used to work in a similar way, though without using DMA and with interrupts disabled: I wrote a minimal mPD765A device driver in the boot sector which could load the OS and save it back to disk after modification, and it was used for all loading and saving of files too. It worked fine on 486s but refused to boot on later machines so I nearly got stuck when my last 486 stopped working. Bochs then came to the rescue, letting me change the code to use the BIOS instead so that I could use a USB floppy drive. I've often wondered why my old code worked on 486s but not Pentiums but I've never had time to look into why. I'll grab a copy of your code in the hope that it contains the answer, though I won't have time to study it for a while. That device driver is still sitting in my OS, but relocated and no longer used.

Edit: I forgot to mention that my floppy driver ran in 32-bit mode, so the first move in my boot sector was a switch to protected mode.

Re: Boot floppy without BIOS disk services

Posted: Wed Dec 11, 2019 12:06 am
by bzt
Hi,

You could also take a look at Xv6's boot sector.
- bootasm.S is a small prologue that sets up protected mode and calls bootmain()
- bootmain.c then loads the kernel without using BIOS calls (no DMA, extremely simple code).

Cheers,
bzt

Re: Boot floppy without BIOS disk services

Posted: Wed Jan 08, 2020 7:51 am
by azblue
I believe I found a bug in Ben's code. The .inc file has:

Code: Select all

IRQ_NUM             equ  0x0E   ; Interrupt Vector Table index for the floppy disk
OK, so far so good, the floppy is IRQ 6, interrupt vector 0xe

Code: Select all

.if (IRQ_NUM < 8)
           mov  dx,0x21
           in   al,dx
           or   al,(1<<IRQ_NUM)
           out  dx,al
.else
           mov  dx,0xA1
           in   al,dx
           or   al,(1<<(IRQ_NUM-8))
           out  dx,al
.endif
OK, IRQs < 8 go to 1st PIC, >8 go to 2nd PIC. Makes sense. But IRQ_NUM had been defined as the interrupt vector number, not the IRQ number. That means this code will incorrectly enable the IRQ on the 2nd PIC. The code works because the BIOS already has interrupts enabled, but it's not technically correct.

Anyway, thank you for sharing this Ben! I'm using it to learn how to program the floppy. :)

Re: Boot floppy without BIOS disk services

Posted: Wed Jan 08, 2020 5:21 pm
by BenLunt
Ya, you are probably correct. I rushed through that part. Either way, it does show that it can be done.

Thanks,
Ben