OSDev.org

The Place to Start for Operating System Developers
It is currently Wed Aug 22, 2018 2:06 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: My OS boots in qemu, but will not work on a real computer
PostPosted: Tue Jun 05, 2018 4:46 pm 
Offline

Joined: Tue Jun 05, 2018 4:43 pm
Posts: 6
I've been working on an operating system for a week or two, very lightly as a hobby, but can't get it to boot on a real computer. Specifically, the first stage of the bootloader starts correctly, and loads the second stage and kernel, but it is unable to start the second stage.

Here is the first stage of my bootloader: https://git.scd31.com/laptopdude90/Stev ... otsect.asm

The screen fills up with "sector loaded" messages, and then afterward fills up with "Starting bootloader second stage..." followed by "Loading SteveOS..." one after another in an infinite loop. I'm guessing there's some kind of problem when the program tries to "jmp" to the memory address holding the second stage. I'm not sure why it isn't working, and especially why it only has problems on a real machine.


Top
 Profile  
 
 Post subject: Re: My OS boots in qemu, but will not work on a real compute
PostPosted: Tue Jun 05, 2018 6:42 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 412
Location: USA
This is probably the single most "why not" question here, with the answer being almost the same every time.

You don't set you DS segment register at all. QEMU probably sets it to 0x0000, which makes your code work, while your real hardware probably sets it to 0x0040 or something else.

Set the DS (and ES, SS, etc) registers before you access memory.

i.e.: Before

Code:
    mov [BOOT_DRIVE], dl


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


Top
 Profile  
 
 Post subject: Re: My OS boots in qemu, but will not work on a real compute
PostPosted: Wed Jun 06, 2018 3:37 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3236
Location: Chichester, UK
Another common mistake is to assume that uninitialised variables are 0, which can cause all manner of strange errors. (Although I agree that in this case assumptions about the segment registers are more likely.)


Top
 Profile  
 
 Post subject: Re: My OS boots in qemu, but will not work on a real compute
PostPosted: Wed Jun 06, 2018 3:03 pm 
Offline

Joined: Tue Jun 05, 2018 4:43 pm
Posts: 6
BenLunt wrote:
This is probably the single most "why not" question here, with the answer being almost the same every time.

You don't set you DS segment register at all. QEMU probably sets it to 0x0000, which makes your code work, while your real hardware probably sets it to 0x0040 or something else.

Set the DS (and ES, SS, etc) registers before you access memory.

i.e.: Before

Code:
    mov [BOOT_DRIVE], dl


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


Thank you very much. I managed to break my OS, so I can't test your solution, but I'll let you know as soon as I do.


Top
 Profile  
 
 Post subject: Re: My OS boots in qemu, but will not work on a real compute
PostPosted: Wed Jun 06, 2018 3:39 pm 
Offline

Joined: Tue Jun 05, 2018 4:43 pm
Posts: 6
Okay, so I tried setting the registers mentioned to 0. However, the system hangs at "Starting bootloader second stage...", which is different from before, but the system still refuses to boot.


Top
 Profile  
 
 Post subject: Re: My OS boots in qemu, but will not work on a real compute
PostPosted: Fri Jun 08, 2018 5:43 pm 
Offline
Member
Member
User avatar

Joined: Sat Nov 22, 2014 6:33 pm
Posts: 412
Location: USA
I quickly looked over your code a bit. You have a lot of redundant code as well as other minor errors.

For example, you pusha/popa before you call the disk code, which also pusha/popa as well.

Anyway, I think you need to eliminate the use of CHS and use LBA addressing. This way, you only have to increment a single 16-bit (32-bit) value for each sector. Then, and only then, at the time you call the INT 13h service, if needed, you convert LBA to CHS. Besides, if you are on a hard drive, you can eliminate the CHS altogether.

Code:
  mov  cur_lba, 2  ; starting LBA of (2nd stage) boot code
  ...
  ...
  call  load_boot_code
  ...
  ...


load_boot_code:
  mov  ax,cur_lba
  cmp  dl,80h
  jae   load_use_ext_services
  call  convert_ax_to_chs
  mov  es, ... address_to_load ...
  jmp  do_call_the_call

load_use_ext_services:
  mov  ah,42h
  mov  si,  ... packet ...

do_call_the_call:
  int    13h

Get the idea? I think this will help you eliminate errors and problems.

For another point, a boot sector gets written to the media once, and never changes until a clean format. Either way, it will always be for that format; floppy, hard drive, etc., and will always know that it is a floppy, hard drive, etc. Therefore, write your boot sector for that specific media type only. Then write another for the other media types.

Now, before someone gets all out of shape, you can write all of the media independent code in one file, then use %if/#if/.if directives to assembly the boot sector for a specific media.

For example:
Code:
   ... media independent code here ....
   ...
   mov  ax,starting_lba
   call load_sectors
   ...
   ...

#ifdef  FOR_FLOPPY
load_sectors:
   ; write code for floppy media here

   ret
#endif

#ifdef  FOR_HARD_DRIVE
load_sectors:
   ; write code for floppy media here

   ret
#endif

This is what all of my boot sectors do. I have a single (some times two) file(s) for each file system format, but a single file for each media format. If I change how the floppy is read, all I have to do is re-assemble all other files...i.e.: re-build the project.

For an older version of my boot code, go to https://github.com/fysnet/FYSOS/tree/master/boot for an example.

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


Top
 Profile  
 
 Post subject: Re: My OS boots in qemu, but will not work on a real compute
PostPosted: Fri Jun 08, 2018 7:58 pm 
Offline
User avatar

Joined: Wed Jun 06, 2018 9:15 pm
Posts: 15
I looked at your boot loading code, and if it's crashing, hanging, or getting stuck at or after the 'Starting bootloader second stage...' message, then it's either when you are calling the 'load_bootloader' routine, or when you are far jumping to the boot loaders' second stage main entry point.

If it is this:
Code:
mov bx, STARTED_LOADING
   call print
   call print_nl
   call load_bootloader


Then you need to check your disk code. There could be either possibly be an loop somewhere, or there could be some code that is getting the computer struck. I can't see the 'disk.asm' file, so only you can know what's inside of it.

If it is this:
Code:
mov bx, STARTING_STAGE
   call print
   call print_nl
   jmp BOOTLOADER_OFFSET
   jmp $


Then your jump to the second stage of your boot loader is broken. One possibly is it's actually, somehow, hitting the
Code:
jmp $
instruction; that's probably unlikely, though.

It could also be that your
Code:
BOOTLOADER_OFFSET
is faulty, and is actually set to a inaccurate value. I've had that happen to me also, when I was trying to write a Protected Mode boot loader program. I completely failed at far jumping, and far jumped to the wrong place, then I got nothing but a black screen.

All in all, if it's getting stuck at that specific point, then it can only be something that was past that point, and most likely very close to that point. Hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group