Boot floppy without BIOS disk services

Programming, for all ages and all languages.
Post Reply
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Boot floppy without BIOS disk services

Post 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
User avatar
DavidCooper
Member
Member
Posts: 1150
Joined: Wed Oct 27, 2010 4:53 pm
Location: Scotland

Re: Boot floppy without BIOS disk services

Post 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.
Last edited by DavidCooper on Wed Dec 11, 2019 11:21 am, edited 1 time in total.
Help the people of Laos by liking - https://www.facebook.com/TheSBInitiative/?ref=py_c

MSB-OS: http://www.magicschoolbook.com/computing/os-project - direct machine code programming
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Boot floppy without BIOS disk services

Post 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
azblue
Member
Member
Posts: 147
Joined: Sat Feb 27, 2010 8:55 pm

Re: Boot floppy without BIOS disk services

Post 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. :)
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Boot floppy without BIOS disk services

Post by BenLunt »

Ya, you are probably correct. I rushed through that part. Either way, it does show that it can be done.

Thanks,
Ben
Post Reply