4

I have a simple (probably the simplest) bootloader. Very similar to this and this. The code is shown below

[org  0x7c00]

mov ah, 0x0e
mov al, 'h'

int 0x10

jmp  $

times  510-($-$$)  db  0

dw  0xaa55

The code is taken from the video series tutorial from here.

I have tried everything to compile and load this bootloader using bochs. But whatever I do, I get a black screen like the following

enter image description here

Nothing comes on the screen and after a bit I get that bochs is not responding.

My present approach to load the image is -

  1. nasm boot_simple.asm -f bin -o boot.bin

  2. dd if=boot.bin of=boot.img bs=512

  3. bochs -f /dev/null -q 'display_library: sdl2' 'boot:a' 'floppya: 1_44=boot.img, status=inserted'

I tried many other approaches (slight variations of the above) (even tried to compile and load a separate bootloader from here) But nothing worked. Always the same black screen and does not show any other information.

I am becoming mad. What am I missing?

Ubuntu 22.04

Bochs version: Bochs x86 Emulator 2.7 (installed via sudo apt install)

I am super new to OS development and I want to learn it as a side project. Please help me getting started. Could not find suitable information anywhere.


Checking the log I see the following

00000000000i[FLOPPY] fd0: 'boot.img' ro=0, h=2,t=80,spt=18
00000000000i[FLOPPY] Using boot sequence floppy, none, none
00000000000i[FLOPPY] Floppy boot signature check is enabled

Not sure how to interpret this (if this is normal or some kind of error / mistake is hiding in them)


Tried to follow these steps as per here and here which I believe should create a simple 1.44 MB image, place the bin in the first sector and leave it to it. When done in this way (as well not padding it) in both cases qemu loads the image and shows the h each time without fault. Bochs does not load anything. Whatever I do. Always the same black screen and afterwards Not responding

I uninstalled, purged, and then reinstalled bochs. I do not know what else I can do. I can't understand why it fails to load the simple bootloader.


Here are the steps

  1. nasm boot_simple.asm -f bin -o boot.bin

  2. dd if=/dev/zero of=floppy.img bs=1024 count=1440

  3. dd if=boot.bin of=floppy.img seek=0 count=1 conv=notrunc

  4. mkfs.vfat -F 12 ./floppy.img (This step makes the floppy un-bootable in qemu but really does not change anything for bochs)

  5. bochs -f /dev/null -q 'display_library: x' 'boot:floppy' 'floppya: 1_44=floppy.img, status=inserted' 'romimage: file=/usr/share/bochs/BIOS-bochs-latest' 'error: action=report'

But nothing happens. Bochs boots to a black screen.


Ok I have a slight idea. It seems that for some reason bochs halts at the first line of the code.

Printing something like this -

Next at t=0
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0
<bochs:1>

Then waiting at the prompt <bochs:1>. If I do step there it does step through some instructions. Is there something I am missing here? Is there a bug in my code?


Trying to use seabios.bin with the default bochs vgabios brings me to this state

enter image description here

This is my bochs config file -

romimage: file="/usr/share/seabios/bios.bin"
vgaromimage: file ="/usr/share/bochs/VGABIOS-lgpl-latest"
floppya: 1_44=floppy.img, status=inserted
boot: a
display_library: x,
log: bochsout.txt

With this option I am able to get rid of the not-responding part. A small advancement. (using seabios) but it still does not display anything. So, I have checked from the menu and it is detecting the floppy in fd0. I am suspecting the vgabios is also not working And hence I do not see anything? Well I am a bit lost to be frank. But as comments say, I think it is not a normal case.


Debug log when trying to set breakpoint

With seabios

    <bochs:1> b 0x7c00
<bochs:2> c
(0) Breakpoint 1, 0x0000000000007c00 in ?? ()
Next at t=16650931
(0) [0x000000007c00] 0000:7c00 (unk. ctxt): mov ah, 0x0e              ; b40e
<bochs:3> n
Next at t=16650932
(0) [0x000000007c02] 0000:7c02 (unk. ctxt): mov al, 0x68              ; b068
<bochs:4> n
Next at t=16650933
(0) [0x000000007c04] 0000:7c04 (unk. ctxt): int 0x10                  ; cd10
<bochs:5> n
Next at t=16650935
(0) [0x000000007c06] 0000:7c06 (unk. ctxt): jmp .-2  (0x00007c06)     ; ebfe
<bochs:6> n
Next at t=16650936
(0) [0x000000007c06] 0000:7c06 (unk. ctxt): jmp .-2  (0x00007c06)     ; ebfe
<bochs:7> n
Next at t=16650937
(0) [0x000000007c06] 0000:7c06 (unk. ctxt): jmp .-2  (0x00007c06)     ; ebfe
<bochs:8> n
Next at t=16650938
(0) [0x000000007c06] 0000:7c06 (unk. ctxt): jmp .-2  (0x00007c06)     ; ebfe

So the breakpoint works. But, no display

With Bochs default bios

<bochs:1> b 0x7c00
<bochs:2> c
bx_dbg_read_linear: physical memory read error (phy=0x0000322f3130, lin=0x00000000322f3130)
Next at t=688972
(0) [0x0000fffffff0] f000:fff0 (unk. ctxt): jmpf 0xf000:e05b          ; ea5be000f0
<bochs:3> n
Next at t=688973
(0) [0x0000000fe05b] f000:e05b (unk. ctxt): xor ax, ax                ; 31c0
<bochs:4> n
Next at t=688974
(0) [0x0000000fe05d] f000:e05d (unk. ctxt): out 0x0d, al              ; e60d
<bochs:5> n
Next at t=688975
(0) [0x0000000fe05f] f000:e05f (unk. ctxt): out 0xda, al              ; e6da
<bochs:6> n
Next at t=688976
(0) [0x0000000fe061] f000:e061 (unk. ctxt): mov al, 0xc0              ; b0c0
<bochs:7> n
Next at t=688977
(0) [0x0000000fe063] f000:e063 (unk. ctxt): out 0xd6, al              ; e6d6
<bochs:8> n
Next at t=688978
(0) [0x0000000fe065] f000:e065 (unk. ctxt): mov al, 0x00              ; b000
<bochs:9> n
Next at t=688979
(0) [0x0000000fe067] f000:e067 (unk. ctxt): out 0xd4, al              ; e6d4
<bochs:10> n
Next at t=688980
(0) [0x0000000fe069] f000:e069 (unk. ctxt): mov al, 0x0f              ; b00f
<bochs:11>

I DID IT!! I downloaded and compiled bochs from source on my platform. I could not make sdl compile (--with-sdl did not work, even after installing sdl2-dev) but rest is good. So I guess the apt-get install is broken for Ubuntu 22.04 (at least)

Thanks a lot for all your help. It was a great learning session.

13
  • Well, did you see it boot before it got stuck at black screen? Did you test with other image? Also, bochs is sometimes picky about image formats so you should probably pad your image to the proper 1.44M size. Even though that should not cause it to stop responding. Could also be that everything is working properly just that you of course have an endless busy loop.
    – Jester
    Commented Jul 21, 2022 at 14:01
  • I never saw it booting. I at least tried to boot using the last link I posted (did not try with grub yet). How do I make it pad to make it 1.44? According to the FIRST link in my post at least something should be shown. (also the video tutorial I mentioned shows that there were some prints to show it is booting from a floppy.
    – SRC
    Commented Jul 21, 2022 at 14:15
  • So I followed word to word the instructions of little book on OS development (here - littleosbook.github.io) and still all I see is a black screen and then not responding. I am starting to think if there is an issue with Ubuntu 22.04 / bochs version / the one that is available via apt install ... ? Starting to become a bit crazy on this.
    – SRC
    Commented Jul 21, 2022 at 14:30
  • 1
    Try qemu -fda boot.img instead of bochs.
    – Jester
    Commented Jul 21, 2022 at 14:33
  • 4
    No bug, bochs just stops at the first instruction. You should type c to let it run.
    – Jester
    Commented Jul 22, 2022 at 10:42

3 Answers 3

4

BOCHS will always stop at the first instruction in the BIOS when launched. The first instruction is at 0xf000:0xfff0 which is what you see in the output. This gives you a chance to set breakpoints ahead of time. For example b 0x7c00 would break at the first instruction of the bootloader if you wished. To start running just use the command c to continue.

34
  • Thank you so much for this tip. Unfortunately, nowhere this is documented. All the videos and tutorials I have seen so far never talks about this behavior. But when I saw that prompt I start to have an idea. I used c with the image made using the process I mentioned above. When using c I get this error - bx_dbg_read_linear: physical memory read error (phy=0x0000322f3130, lin=0x00000000322f3130) The same image runs without error in qemu.
    – SRC
    Commented Jul 23, 2022 at 5:10
  • 1
    @SRC QEMU for purposes of speed doesn't do a number of checks and can allow bad code to execute seemingly correctly. BOCHs is more stringent about things. This includes reading and writing beyond a segment limit. Without seeing your exact code answering your question is going to be difficult. From the error message I am curious - do you enter protected mode and enable paging? To fix the problem you are having is a different issue than the one your question is really about. Commented Jul 23, 2022 at 5:14
  • 1
    I have managed to make it work. Please the updated part of the question. @Michael Petch. Thanks a lot for all your input. It was great to learn things from you.
    – SRC
    Commented Jul 24, 2022 at 6:02
  • 2
    @SRC : I can confirm that pre-built BOCHS in a fully updated Ubuntu 22.04 fresh install exhibits similar behaviour to what you are seeing. It might be worth filing a bug with Ubuntu as building it from source (as you have also noticed) produces a workable BOCHS. I didn't have time to find out why/where this bug exists in BOCHS when built on Ubuntu 22.04. Commented Jul 24, 2022 at 22:15
  • 3
    @SRC @Michael Petch: On Ubuntu 22.04 I also get bx_dbg_read_linear: physical memory read error (phy=0x0000322f3130, lin=0x00000000322f3130). This is due to a bad BIOS-bochs-latest ROM image. Replacing this with either BIOS-bochs-legacy from the Ubuntu package, or BIOS-bochs-latest from a more recent
    – Bogatyr
    Commented Jan 9 at 12:52
2

I encountered the same problem with you in Debian. The latest bochs(2.7+dfsg-4+b2) in the Debian or Ubuntu software repository is the culprit of this wired situation.

The solution is what you did before - downloading and compiling bochs 2.7 by yourself.

./configure --with-sdl2 --enable-debugger
make
sudo make install

Note that you need to use --with-sdl2 other than --with-sdl [1], or you will encounter the compilation error - "SDL.h is not found". Because bochs 2.7 uses sdl2, meanwhile, bochs 2.6.11 uses sdl1.2.

Another way to solve this problem is to use bochs 2.6.11, tested in Debian and Ubuntu.

[1] https://bochs.sourceforge.io/doc/docbook/user/compiling.html

6
  • What's wrong with Debian's package? Did they replace a non-free BIOS image or something to meet the DFSG (Debian Free Software Guidelines), resulting in a Bochs that works differently from mainline? Or do they not enable the built-in debugger? Commented Feb 21, 2023 at 3:32
  • I am not sure, so I am using reportbug to report this problem to the bochs maintainers.
    – 慕冬亮
    Commented Feb 21, 2023 at 3:38
  • 3
    @PeterCordes I did some testing of this a while back (under my answer the length comments suggest a problem with the build on Debian). Since then I discovered the problem is specific to the ROM image file BIOS-bochs-latest produced by Debian for that release. They do apply patches related to it. If you change the configuration to point at a working build of BIOS-bochs-latest from a 2.7 release it seems to work. I'd suggest the problem should be directed at the Debian maintainers. Commented Feb 21, 2023 at 5:55
  • 2
    This problem has also been mentioned on Reddit: reddit.com/r/osdev/comments/11353ws/bochs_black_screen using Cinnamon 21.1(which uses the the same files and config as Ubuntu 22.04). All are based on Debian bookworm/sid . Note: The Debian package builder applies a number of patches and one is for the ROM BIOS. Commented Feb 21, 2023 at 6:17
  • 1
    @MichaelPetch OMG I wasted SO much time on this! The windows release of bochs from source forge works right out of the box. The Ubuntu 22.04 bochs package (which doesn't seem to work without installing all the other bochs- packages) has a BAD ROM image BIOS-bochs-latest. I switched to BIOS-bochs-legacy and my emulated OS boots! The BIOS-bochs-latest from the windows release (different checksum) works. How many people have bashed their heads against this, the package maintainers really need to step up here and fix this!
    – Bogatyr
    Commented Jan 8 at 9:38
1

I solved the issue. recompile the bochs-2.7 with Bochs-REL_2_7_FINAL

when config, append --with-sdl2 --enable-debugger

./configure **--with-sdl2 \
        --enable-debugger \**
            ......
nvidia@nvidia-PC:~/Downloads/Bochs-REL_2_7_FINAL/bochs$ cat .conf.linux 
#!/bin/sh
#
# .conf.linux
#

#which_config=normal
which_config=plugins

CC="gcc"
CXX="c++"
CFLAGS="-Wall -O3 -fomit-frame-pointer -pipe"    # for speed
#CFLAGS="-Wall -g -pipe"                         # for development
CXXFLAGS="$CFLAGS"

export CC
export CXX
export CFLAGS
export CXXFLAGS


case $which_config in

  normal)

    #######################################################################
    # configuration 1 for release binary RPMs
    # Include a few standard options, speed optimizations, X11 only.
    #######################################################################
    ./configure --with-sdl2 \
            --enable-debugger \
            --enable-sb16 \
                --enable-ne2000 \
                --enable-all-optimizations \
                --enable-cpu-level=6 \
                --enable-x86-64 \
                --enable-vmx=2 \
                --enable-pci \
                --enable-clgd54xx \
                --enable-voodoo \
                --enable-usb \
                --enable-usb-ohci \
                --enable-usb-ehci \
                --enable-usb-xhci \
                --enable-busmouse \
                --enable-es1370 \
                --enable-e1000 \
                --enable-show-ips \
                ${CONFIGURE_ARGS}
    ;;

  plugins)
    #######################################################################
    # configuration 2 for release binary RPMs
    # Include plugins, every possible gui.
    #######################################################################
    ./configure -with-sdl2 \
            --enable-debugger \
            --enable-sb16 \
                --enable-ne2000 \
                --enable-all-optimizations \
                --enable-cpu-level=6 \
                --enable-x86-64 \
                --enable-vmx=2 \
                --enable-pci \
                --enable-clgd54xx \
                --enable-voodoo \
                --enable-usb \
                --enable-usb-ohci \
                --enable-usb-ehci \
                --enable-usb-xhci \
                --enable-busmouse \
                --enable-es1370 \
                --enable-e1000 \
                --enable-plugins \
                --enable-show-ips \
                --with-all-libs \
                ${CONFIGURE_ARGS}
    ;;

esac

nvidia@nvidia-PC:~/Downloads/Bochs-REL_2_7_FINAL/bochs$ . .conf.linux 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.