Having trouble booting from CD?

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.
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Having trouble booting from CD?

Post by zehawk »

So I've been developing a sort of mini OS (Go figure xD) and wanted to try booting from a CD. I used VirtualBox of course, and I've decided to use VirtualBox to develop it for the most part. But the end goal IS to create something bootable from a CD, even if it only works on my machine.

So I decided to try a simple bootloader and burning it to a CD as an iso, and running it. You know, as a simple 'Hello World'.

Boot for some reason, when I go into BIOS, and click on boot from CD, it goes directly to FreeDOS. I'm not entirely sure if this is an intermediate step to actually booting from CD, or if somehow the CD burner isn't working right, but I am just completely lost as to what I do next.

If you know why it's doing this, you can skip the next section.

Here's the installation method I went through:

Code: Select all

BITS 16

start:
	mov sp, 4096

	mov ax, 0x07C0	        ; Set data segment to where we're loaded
	mov ds, ax
        
keyb_in:
        mov    ah, 00h	; keyboard input subprogram
        int    16h	; character input
        ; character is stored in al
        mov    [keyboard_in], al	; copy character from al to keyboard in.
        
        mov al, [keyboard_in]  ;I know this step is redundant, but i did it for practice sake.
        mov ah, 0Eh
        int 10h
        
        cmp al, 0
        je keyb_in
        
        cmp al, '.'  ;Will shutdown pc if user presses '.' on the keyboard.
        je shutdown
          
        jmp keyb_in

        keyboard_in: db 0

shutdown:
	;Connect to APM API  ;WILL ATTEMPT TO TURN COMPUTER OFF
        ;mov     AX,5301h
        ;xor     BX,BX
        ;int     0x15
        
        ;Try to set APM version (to 1.2)
        ;MOV     ax,530Eh
        ;XOR     bx,bx
        ;MOV     cx,0x0102
        ;INT     0x15
        
        ;Turn off the system
       ; MOV     ax,5307h
       ;MOV     bx,0001h
        ;MOV     cx,0003h
        ;int     0x15
        mov ah, 0Eh
        mov al, '1'
        int 10h
        
        ;perform an installation check
        mov ah,53h            ;this is an APM command
        mov al,00h            ;installation check command
        xor bx,bx             ;device id (0 = APM BIOS)
        int 15h               ;call the BIOS function through interrupt 15h
        jc APM_error          ;if the carry flag is set there was an error
                      ;the function was successful
                      ;AX = APM version number
                          ;AH = Major revision number (in BCD format)
                          ;AL = Minor revision number (also BCD format)
                      ;BX = ASCII characters "P" (in BH) and "M" (in BL)
                      ;CX = APM flags (see the official documentation for more details)
       
        mov ah, 0Eh
        mov al, '2'
        int 10h
         
        ;connect to an APM interface
        mov ah,53h               ;this is an APM command
        mov al,01;see above description
        xor bx,bx                ;device id (0 = APM BIOS)
        int 15h                  ;call the BIOS function through interrupt 15h
        jc APM_error             ;if the carry flag is set there was an error
                         ;otherwise, the function was successful
                         ;The return values are different for each interface.
                         ;The Real Mode Interface returns nothing.
 
                         ;16-Bit Protected Mode Interface
                         ;AX = Code Segment
                         ;BX = Entry Point (Offset)
                         ;CX = Data Segment
                         ;SI = Code Segment Length
                         ;DI = Data Segment Length
 
                         ;32-Bit Protected Mode Interface
                         ;AX = 32-Bit Code Segment
                         ;EBX = 32-Bit Entry Point (Offset)
                         ;CX = 16-Bit Code Segment
                         ;DX = Data Segment
                         ;ESI 0:15 = 32-Bit Code Segment Length
                         ;ESI 16:31 = 16-Bit Code Segment Length
                         ;DI = Data Segment Length
        mov ah, 0Eh
        mov al, '3'
        int 10h
                                 
        ;disconnect from any APM interface
        mov ah,53h               ;this is an APM command
        mov al,04h               ;interface disconnect command
        xor bx,bx                ;device id (0 = APM BIOS)
        int 15h                  ;call the BIOS function through interrupt 15h
        jc .disconnect_error            ;if the carry flag is set see what the fuss is about. 
        
        jmp .no_error
 
   .disconnect_error:       ;the error code is in ah.
        cmp ah,03h               ;if the error code is anything but 03h there was an error.
        jne APM_error            ;the error code 03h means that no interface was connected in the first place.
        jmp shutdown
         
    .no_error:
        mov ah, 0Eh
        mov al, '4'
        int 10h
        
    ;Enable power management for all devices
        mov ah,53h              ;this is an APM command
        mov al,08h              ;Change the state of power management...
        mov bx,0001h            ;...on all devices to...
        mov cx,0003h            ;...power management on.
        int 15h                 ;call the BIOS function through interrupt 15h
        jc APM_error            ;if the carry flag is set there was an error
        
        mov ah, 0Eh
        mov al, '5'
        int 10h
        ;Set the power state for all devices
        mov ah,53h              ;this is an APM command
        mov al,07h              ;Set the power state...
        mov bx,0001h            ;...on all devices to...
        mov cx,03h                ;off
        int 15h                 ;call the BIOS function through interrupt 15h
        jc APM_error            ;if the carry flag is set there was an error    
        
APM_error:
        mov ah, 0Eh
        
        mov al, 'L'  ;If system prints L, we can assume shutdown did not work and force shutdown is neccessary.
        int 10h
        
        ;Exit (for good measure and in case of failure)
        ret

	times 510-($-$$) db 0	; Pad remainder of boot sector with 0s
	dw 0xAA55		; The standard PC boot signature
Compiled with nasm flags: nasm C:/CustomOs/Bootloader.asm -f bin -o C:/CustomOs/Image/cdcontents/Bootloader.img

Then, using mkisofs, I turn it into a burnable iso image as so:
mkisofs/ -no-emul-boor -boot-load-size 4 -o Test_OS.iso -V Test_OS -b "Bootloader.img" C:/CustomOs/Image/cdcontents"

Don't know if it's worth mentioning but the iso size comes out to be 354 kb. Saw somewhere that it should be 1.44 Megabytes or something, don't know if my iso size being less could have anything to do with it.

Afterwards, I burn the iso to the disc and it burns successfully (As in, the windows disc burner recognizes my iso as a burnable image.)

I go back to boot through the BIOS, and I have to set Secure Boot off on my machine. Only way for it to detect the CD. I click on running the CD and-----

It goes to FreeDOS.

Again, it is entirely possible that this is intentional, and that FreeDos is just a median step to actually booting from a cd. If it is, then I would like to know how to go from FreeDos and run my test system.

If it is not supposed to go to FreeDos, then I am unsure as to what I am doing wrong. If you guys have any more questions about my system specs or anything, I'll respond promptly.

Finally, I am new to OS development, but I read theory and consider myself competent at assembly and c. I'm not trying to make the next Windows, just a for fun thing to show my friends. Also, I already use VirtualBox to debug. I would just like the capability to boot from a cd for stable 'releases' to test on actual hardware.

Thanks!
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: Having trouble booting from CD?

Post by Octocontrabass »

zehawk wrote:FreeDOS
I think you have a drive connected to your computer with FreeDOS installed on it. I don't think your CD is booting at all.
zehawk wrote:

Code: Select all

BITS 16

start:
	mov sp, 4096
You have made many mistakes, and many of them are explained here. In addition, you also have to take into account some CD-specific things, which are explained here.
zehawk wrote:Don't know if it's worth mentioning but the iso size comes out to be 354 kb. Saw somewhere that it should be 1.44 Megabytes or something, don't know if my iso size being less could have anything to do with it.
The 1440 kilobyte size is required for floppy disk emulation mode, but you're using no emulation.
zehawk wrote:I read theory and consider myself competent at assembly and c.
You may feel different after you realize the mistakes in your code. :roll: (But you'll learn from your mistakes, and that's a good thing.)
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Re: Having trouble booting from CD?

Post by zehawk »

Octocontrabass wrote: You may feel different after you realize the mistakes in your code. :roll: (But you'll learn from your mistakes, and that's a good thing.)

Thanks for your response! I suppose the right thing to say was I'm fairly competent at writing assembly and c in user mode, not real mode. xD So I double checked and it looks like you're right, it would appear that the BIOS doesn't detect my cd drive AT ALL and that when setting Secure Boot to off, it detected FreeDos and that's what I made it run. I might have to go out and get a new cd drive.

Another option was to try getting it onto a usb and installing off of there, as the BIOS does recognize the USB as a bootable device, but so far had little luck actually implementing this. Tried Rufus, but it won't copy my iso over because it's not a supported image. (As in, not a windows or linux image because it's custom.) Will have to try using windows commands to do it.

Another option I heard of was creating a hard drive partition and putting my bootloader in the first bytes of that, but I'm not even sure that would work. (If it does, any idea on how to do that?)

I will look at the assembly mistakes I made and fix them promptly, would you mind if I posted my revisions here after so you could take a look at it again?


EDIT: What about booting from an external hard drive? Is that easier or harder than trying to boot from a USB,(which I had very little luck with)?

EDITx2:

Code: Select all

BITS 16
org 0x7C00

start:
   	mov ax, 0
	mov ss, ax ;Puts 0 into the segment pointer, we are using real memory.
	mov sp, 0x7C00 ;Moves 7C00 into the stack pointer, so that everything from 6C00 - 7BFF will be stack.

keyb_in:
        mov    ah, 00h   ; keyboard input subprogram
        int    16h   ; character input
        ; character is stored in al
        mov    [keyboard_in], al   ; copy character from al to keyboard in.
       
        mov al, [keyboard_in]  ;I know this step is redundant, but i did it for practice sake.
        mov ah, 0Eh
        int 10h
       
        cmp al, 0
         
        jmp keyb_in

        keyboard_in: db 0

        times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
        dw 0xAA55      ; The standard PC boot signature
How's this?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Having trouble booting from CD?

Post by Combuster »

Code: Select all

mov [undefined_segment:keyboard_in], al
You forgot DS (and ES)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Re: Having trouble booting from CD?

Post by zehawk »

You're right. Seems the tutorials I've been looking through have generally not been well written. (I've followed quite a bit.) ds and es are, I'm assuming, going to be 0 as well because org 7C00 sets it to address 7C00 in sector 0, so ds and es would be in sector 0 as well when they call the memory of the data I have. Or so I am assuming.

With these fixes, it still begs the question, how am I going to boot the bootloader if my cd drive isn't being detected by my BIOS? Also thinking about moving to Bochs instead of VirtualBox as the code I ran without ds and es worked in VB, which obviously would not work in real life xD

If I had an external hard drive and booted off that, would that work? Would I have to reformat it everytime I want to try my bootloader/OS on real hardware?

If anyone else has ways to do this I am more than happy to hear them! I'm still learning and want to thank you guys for your patience.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Having trouble booting from CD?

Post by kzinti »

zehawk wrote:I'm assuming
When programming, never assume. Verify.

zehawk wrote: , going to be 0 as well because org 7C00 sets it to address 7C00 in sector 0, so ds and es would be in sector 0 as well when they call the memory of the data I have. Or so I am assuming.
"org" is a compile-time directive to let the assembler know at which address your code will reside. It doesn't set any runtime registers. In fact, it doesn't exist at runtime. "ds" and "es" are CPU registers. How can they be in a CDROM sector?

Since you didn't initialize DS and ES, you don't know what values they have.
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Re: Having trouble booting from CD?

Post by zehawk »

Sorry, meant I'm assuming should be set to 0. I should've specified a bit better. That one's on me. (And I used the words 'I assume' hoping somebody would correct me if I'm wrong, assuming in real practice is definitely wrong xD).

I also know that org is just a directive, and that it basically makes sure that when I call keyboard_in for example, it calls it at keyboard_in's address + 7C00. So when I said ds and es should be set to 0, I was verifying that it would correctly point to that data when I call [keyboard_in]. Of course the cd doesn't have registers. It's just memory. The bios loads the bootloader into 7C00 and that's why org is needed. Then the BIOS sets the instruction pointer to 7C00 to start executing the code. The only thing I am having troubles with now, are figuring out how to make sure that the bootloader is successfully loaded into 7C00. As of now, my BIOS doesn't see my drive, and I only have a few more options. A good tutorial or link on how to set the bootloader onto an external hard drive/usb and boot it would be very helpful! :D
Last edited by zehawk on Thu Jun 11, 2015 12:37 pm, edited 2 times in total.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Having trouble booting from CD?

Post by kzinti »

Reading comprehension problems?

Read this:
zehawk wrote:"ds and es are, I'm assuming, going to be 0 as well because org 7C00 sets it to address 7C00 in sector 0"
Read my reply again.

ORG doesn't set DS and ES. It's impossible.
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Re: Having trouble booting from CD?

Post by zehawk »

I said I realize org doesn't set them. You set them through mov ax, 0 mov ds, ax and the same for es. I was just asking to make sure that 0 is the correct value being sent into the registers.
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Having trouble booting from CD?

Post by kzinti »

Well you edited you post to say that after I replied.

Anyhow... Yes in your case 0 seems to be the right value. If you have to ask, you probably don't understand how real mode addressing works yet.
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Re: Having trouble booting from CD?

Post by zehawk »

Well, I was editing it while you were probably typing it.

Anyways, I was just verifying. I know how segmenting addressing works, I was just verifying with more experienced people. The ss and ds and es registers were how the original 20 bit address systems worked with only a 16 bit processor. You set your segment, then access the data off that segment. So basically, it is Accessed Memory = segment register * 16 + address. I'm simplifying, but that;s how it works.

Anyways, it still doesn't answer what I'm trying to get at. Actually booting. Again, even if it's just a link on how to boot without the aid of a cd drive or something, I'm down for it. I did look, but believe it or not, resources on booting custom os's are scarce.
Octocontrabass
Member
Member
Posts: 5588
Joined: Mon Mar 25, 2013 7:01 pm

Re: Having trouble booting from CD?

Post by Octocontrabass »

zehawk wrote:I might have to go out and get a new cd drive.
I can honestly say I've never tried to boot an external CD drive, so I have no idea if that would work. I do know that booting an internal CD drive should work on anything reasonably modern, so you may just need to adjust the boot order.
zehawk wrote:Another option was to try getting it onto a usb and installing off of there, as the BIOS does recognize the USB as a bootable device, but so far had little luck actually implementing this.
The BIOS will usually automatically choose to emulate the USB flash drive as either a floppy drive or a hard drive, depending on the contents of the flash drive. If there's a valid partition table, the BIOS will choose hard disk mode. If there's a valid BPB, the BIOS will choose floppy disk mode. If there's neither, most BIOSes choose floppy disk mode by default; if there's both, you'll get a mix of results.
zehawk wrote:Another option I heard of was creating a hard drive partition and putting my bootloader in the first bytes of that, but I'm not even sure that would work. (If it does, any idea on how to do that?)
You mean on the disk that contains Windows? Don't do that.
zehawk wrote:What about booting from an external hard drive? Is that easier or harder than trying to boot from a USB,(which I had very little luck with)?
It should be a bit easier, as long as the external drive uses 512-byte sectors. (BIOS can't boot from 4096-byte sectors. UEFI can, but you're writing a BIOS bootloader.)
zehawk wrote:Would I have to reformat it everytime I want to try my bootloader/OS on real hardware?
You don't have to reformat it at all, but if you overwrite parts of the filesystem, it will appear as an unformatted disk when you plug it in. The filesystem (and the MBR) will have some space dedicated for bootloaders; if you install your bootloader in that space and don't overwrite anything else, the filesystem will remain usable.

The dedicated space is very small, so I use it to load a simple binary file from the disk. That binary file contains the "second stage" bootloader that does all of the real work to load the operating system. The second stage is just an ordinary file on the disk, so whenever I need to update it, I can just drag and drop the replacement file onto the disk. It's very convenient!
zehawk wrote:A good tutorial or link on how to set the bootloader onto an external hard drive/usb and boot it would be very helpful! :D
I just use HxD to copy from the binary file to the disk. The "physical disk" shows you all sectors on the disk, starting with sector 0. The "logical disk" shows you all sectors in the partition, with "sector 0" being whichever sector is at the beginning of the partition.
zehawk
Member
Member
Posts: 35
Joined: Thu Jun 11, 2015 7:26 am
Libera.chat IRC: zehawk

Re: Having trouble booting from CD?

Post by zehawk »

Thank you, Octocontrabass! The information you provided is very helpful!
Octocontrabass wrote: I can honestly say I've never tried to boot an external CD drive, so I have no idea if that would work. I do know that booting an internal CD drive should work on anything reasonably modern, so you may just need to adjust the boot order.
Yeah, I went through the bios boot order, and it's not even being detected by the BIOS. This could be due to a faulty BIOS, as my BIOS does have a few bugs. (Changing boot order does not save. Yeah, that means a manual rearrange each time.) So I'm thinking if I decide to go down the CD path, it has to be external.
Octocontrabass wrote: The BIOS will usually automatically choose to emulate the USB flash drive as either a floppy drive or a hard drive, depending on the contents of the flash drive. If there's a valid partition table, the BIOS will choose hard disk mode. If there's a valid BPB, the BIOS will choose floppy disk mode. If there's neither, most BIOSes choose floppy disk mode by default; if there's both, you'll get a mix of results.
Any reason why I'd want to choose one or the either? Worth noting is that most ISO to USB programs have failed to link the image to the USB. This could be due to a lack of either a Partition Table or a BPB. I'm guessing the partition table and BPB are included somewhere in the bootloader itself? (Again, wild guess. I'm probably wrong and accept that.)
Octocontrabass wrote: You mean on the disk that contains Windows? Don't do that.
Yeah, don't even know where I heard this from. But definitely taking that advice! xD
Octocontrabass wrote: It should be a bit easier, as long as the external drive uses 512-byte sectors. (BIOS can't boot from 4096-byte sectors. UEFI can, but you're writing a BIOS bootloader.)
Hmm, when I go into my BIOS screen, it is UEFI enabled. Not sure what to call that. Thank you for this warning though, I will definitely go out and get the correct HD!
Octocontrabass wrote: You don't have to reformat it at all, but if you overwrite parts of the filesystem, it will appear as an unformatted disk when you plug it in. The filesystem (and the MBR) will have some space dedicated for bootloaders; if you install your bootloader in that space and don't overwrite anything else, the filesystem will remain usable.

The dedicated space is very small, so I use it to load a simple binary file from the disk. That binary file contains the "second stage" bootloader that does all of the real work to load the operating system. The second stage is just an ordinary file on the disk, so whenever I need to update it, I can just drag and drop the replacement file onto the disk. It's very convenient!
That sounds exactly like what I want to accomplish! And you use HxD for this?
I guess because this most closely relates to what I want to accomplish, I'm going to ask some more questions about this. This small space dedicated for bootloaders, does it reside at 0x0000? You know, the very front of the drive? Or is it some arbitrary location? (And if so, is it drive dependent or independent? Another way to say it would be standardized, which I am quickly realizing that most things at this level aren't.)
If so, all I have to do is load in the boatloader into this small area, and it will load it into RAM (0000:7C00) for me, assuming BIOS boot order is configured? I know you mentioned partition tables earlier, I'm guessing that this where those come into play. And, you mentioned certain caveats with CD booting, any sort of caveats with, say, booting from an external HD? Special formatting, etc?

Octocontrabass wrote: I just use HxD to copy from the binary file to the disk. The "physical disk" shows you all sectors on the disk, starting with sector 0. The "logical disk" shows you all sectors in the partition, with "sector 0" being whichever sector is at the beginning of the partition.
Going to assume that you use "physical disk" to access the place you want to put in the bootloader?

With all this in mind, I'm thinking maybe I can somehow use HxD to move the bootloader to the very front of an external drive, and implement a partition table. Any links on how to make partition table then? (To my understanding, on a hard drive, it has to be within certain bytes and contain certain data just like the last two bytes 0x55AA.) And then, once i have this done, it should be considered 'bootable', or am I missing something? And HxD will be the only program I need? No iso burners like for a CD or anything as well. Or formatting tools? Just a simple copy paste binary into the Hard drive's bootsector? This sounds much easier than booting from a CD anyways! xD

Thank you guys very much with your help so far. I know I am asking a lot of questions, but I really appreciate the help you've given me!
Last edited by zehawk on Thu Jun 11, 2015 4:51 pm, edited 2 times in total.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Having trouble booting from CD?

Post by Combuster »

For all your booting problems, try it in a virtual machine or emulator first. ;)

Only the truly ancient BIOSes can't boot from CD. In fact, no-emulation El-Torito is better supported than no-emulation which is generally better supported than USB boot. But each mechanism is different, and you can't simply use code for one method to do the other. However, test cycles using a CD are pretty much the slowest option available - from experience they are worse than floppies.

I have a working external CD drive that can be booted from - I even installed my computer with it.

If you want to do the CD way, your best bet is to make the .iso using mkisofs/genisoimage and specifying the bootcode properly. You can then also post that command for scrutiny if either the VM or real hardware doesn't grok it.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Having trouble booting from CD?

Post by gerryg400 »

kiznit wrote:
zehawk wrote:I'm assuming
When programming, never assume. Verify.
This is poor advice. Assumptions are important in all forms of engineering. Without making assumptions, often no progress can be reasonably made. As long as assumptions are considered and stated they should present no problem.
If a trainstation is where trains stop, what is a workstation ?
Post Reply