Simple CD/USB boot-loader

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
0xIvan32
Posts: 6
Joined: Fri Jul 17, 2009 11:16 pm

Simple CD/USB boot-loader

Post by 0xIvan32 »

I never seen any CD or USB boot-loader without FDD emulation. I dont even found any documentation about how BIOS loads such devices. Can somebody show me example of this code. Im just a beginner of system programming and i just need the simpleest code that could be I most forget. Can someone explain how to interract with USB in real mode? If there is no way to boot from USB without emulation, i guess i would be at least able to read my kernel(by the way i still didnt even proected - just want to have at least a valid boot-loader) from it. As i heard, FDD makes a lot of error while reading - so procedure of load would be very paintfull for boot-loader. I cant write a fully functional boot-loader what would excepting this kind of troubles because i just studing.
PS: I have already tryed to write a FDD boot-loader - so i understand how it works, but i used only Int in it - no ports.. Can anybody recommend me a book for study a port-based device programming?
PSS: Sorry for my english, have no time yet to improve it=(
PSSS: I use FASM.
User avatar
xvedejas
Member
Member
Posts: 168
Joined: Thu Jun 04, 2009 5:01 pm

Re: Simple CD/USB boot-loader

Post by xvedejas »

You pretty much depend on your BIOS to boot from various mediums, so you're stuck with floppy emulation. The BIOS contains a floppy driver that you call with interrupts to load a kernel or stage2 of your bootloader. Since it (probably) is impossible to write your own drivers in a bootsector to load something off a USB drive without this emulation, just forget about it. If you're writing your own bootloader, you need to use BIOS interrupts.

Now, if you're talking about USB drivers AFTER your kernel has been loaded (ie not in the bootloader) then that's a whole different story, where I recommend you start with the wiki.

Also, you mean PPS, not PSS (also PPPS, not PSSS). PS stands for "Post Scriptum" (After Writing), and PPS stands for "Post post scriptum" (after after writing).
User avatar
Thor
Member
Member
Posts: 51
Joined: Mon Jul 06, 2009 12:55 am
Location: Kamloops, BC, Canada

Re: Simple CD/USB boot-loader

Post by Thor »

xvedejas wrote:Since it (probably) is impossible to write your own drivers in a bootsector to load something off a USB drive without this emulation, just forget about it. If you're writing your own bootloader, you need to use BIOS interrupts.
I don't see any reason you can't boot from CD without emulation. It would be quite possible if all you did was load a stage 2. You wouldn't need an entire device driver either. Just one routine to load the necessary sectors into memory and you would be set.
Vancouver Canucks fan for life
0xIvan32
Posts: 6
Joined: Fri Jul 17, 2009 11:16 pm

Re: Simple CD/USB boot-loader

Post by 0xIvan32 »

Thor, can you explain with more details? Maybe some articles are exist about this topic? I need a quite simple sample of that kind of boot-loader, it would be innaf to understand:) Thank you.
PS: PSS and more S - it's some kind of habit in russian segment of intrenet. We talk so only because we like this form of it:)
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Simple CD/USB boot-loader

Post by kop99 »

Can anybody recommend me a book for study a port-based device programming?
here is related wiki.
http://wiki.osdev.org/Floppy_Disk_Controller

And related books.
"The Indispensable PC Hardware Book"
User avatar
f2
Member
Member
Posts: 311
Joined: Mon Jun 15, 2009 10:01 am
Location: France

Re: Simple CD/USB boot-loader

Post by f2 »

0xIvan32 wrote:Thor, can you explain with more details? Maybe some articles are exist about this topic? I need a quite simple sample of that kind of boot-loader, it would be innaf to understand:) Thank you.
PS: PSS and more S - it's some kind of habit in russian segment of intrenet. We talk so only because we like this form of it:)
Hi 0xIvan32,

You may be happy, I wrote a boot sector for CD-ROM to one of my old OS project. As the boot sector does not serve me in my current project, I will give the source of this boot sector to all those who need them.

Features:
- supports ISO-9660 filesystem ("El Torito");
- no floppy emulation;
- supports multiples burning session.

Limitations:
- supports only Joliet format.

The boot sector is written with the GAS assembler. Those who use NASM or FASM will have to translate it.
Attachments

[The extension s has been deactivated and can no longer be displayed.]

"Open source seems to embrace the dark side of human nature." - Ville Turjanmaa
0xIvan32
Posts: 6
Joined: Fri Jul 17, 2009 11:16 pm

Re: Simple CD/USB boot-loader

Post by 0xIvan32 »

Thank you very much all:)
movl $16, %eax
I've seen before code like this but never understand how it works? I may mistake, but if i right - real mode doesn't support operations with 32-bit registers/memory_blocks?
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Simple CD/USB boot-loader

Post by kop99 »

real mode doesn't support operations with 32-bit registers/memory_blocks?
No, it support!
User avatar
xvedejas
Member
Member
Posts: 168
Joined: Thu Jun 04, 2009 5:01 pm

Re: Simple CD/USB boot-loader

Post by xvedejas »

I don't see any reason you can't boot from CD without emulation. It would be quite possible if all you did was load a stage 2.
Yeah, exactly; it takes emulation to load the stage 2 in the first place.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Simple CD/USB boot-loader

Post by Brendan »

Hi,
xvedejas wrote:
I don't see any reason you can't boot from CD without emulation. It would be quite possible if all you did was load a stage 2.
Yeah, exactly; it takes emulation to load the stage 2 in the first place.
For bootable CDs (El Torito), there's 3 different methods:
  • The BIOS uses a floppy image on the CD and emulates a floppy disk for you
  • The BIOS uses a hard disk image on the CD and emulates a hard disk for you
  • The BIOS lets you access the CD directly (without any emulation); where you use extended disk services to read (any number of) 2048 byte sectors from a starting LBA address
I would assume that Xvedejas was talking about the third option (no emulation). In this case the boot loader can be several (2048-byte) sectors and the BIOS will load all the sectors into RAM for you - e.g. the boot loader can be up to about 512 KiB, which is more than enough for a generic ATA/ATAPI driver.

However, you'd have problems to support all CD drives: a generic ATA/ATAPI driver, plus three separate USB controller drivers (AHCI, OHCI and EHCI) and a generic mass storage device driver, plus about 30 different drivers for each type of SCSI controller, plus code to scan PCI configuration space (to figure out which drivers you *might* need to use). It adds up to far too much for a boot loader...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
smwikipedia
Member
Member
Posts: 49
Joined: Tue Apr 20, 2010 1:11 am

Re: Simple CD/USB boot-loader

Post by smwikipedia »

f2 wrote:
0xIvan32 wrote:Thor, can you explain with more details? Maybe some articles are exist about this topic? I need a quite simple sample of that kind of boot-loader, it would be innaf to understand:) Thank you.
PS: PSS and more S - it's some kind of habit in russian segment of intrenet. We talk so only because we like this form of it:)
Hi 0xIvan32,

You may be happy, I wrote a boot sector for CD-ROM to one of my old OS project. As the boot sector does not serve me in my current project, I will give the source of this boot sector to all those who need them.

Features:
- supports ISO-9660 filesystem ("El Torito");
- no floppy emulation;
- supports multiples burning session.

Limitations:
- supports only Joliet format.

The boot sector is written with the GAS assembler. Those who use NASM or FASM will have to translate it.
I roughly go through the code, it is neatly written. Basically it relies on INT 13h AH=42h: Extended Read Sectors From Drive http://en.wikipedia.org/wiki/INT_13H#IN ... From_Drive to load data from CD.

I think we can also implement our own CD Drive driver. Anyway, if we want to use CD-ROM, we have to implement the CD Drive driver, sooner or later once we entered the Protected Mode.

However, I am not quite sure how BIOS treat the floppy boot sector and CD boot sector differently. Could anyone elabrate the details about it? Thanks.
M2004
Member
Member
Posts: 65
Joined: Sun Mar 07, 2010 2:12 am

Re: Simple CD/USB boot-loader

Post by M2004 »

Here's an example which may be of a help.

http://board.flatassembler.net/topic.php?t=12389

Regards
Mac2004
Post Reply