Creating image of OS to run with qemu.

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
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Creating image of OS to run with qemu.

Post by bilsch01 »

I have executable code for my simple OS in a binary file (jsec2.bin) created using nasm assembler. I have been running it from a flash drive with a boot sector that loads the executable to memory and jumps to it. File jsec2.bin is not bootable by itself. I want to start running it with qemu - no flash drive involved. I do not want qemu to use a virtual flash drive either. I want to use the qemu floppy image switch, -fda. However the -fda switch will only work if there is a aa55 boot sector mark at the end of the first sector of the image.

qemu-system-i386 -fda jsec2.bin will not work. Here's some other possibilities:

1) I need an image of a bootable floppy with a bootsector that jumps to a file named jsec2.bin just like an msdos boot sector jumps to the file named IO.SYS contained in the file system on the disk. Is there a tool that I can adapt to this purpose?

2) possibly there is some way to make a grub floppy image or iso that boots the executable in jsec2.bin. If there is a way using a grub tool?

Actually, both of these ideas suck because I want to use qemu for running the code as I develop it and these ideas are slow and cumbersome. Any suggestions will be appreciated. Thanks Bill S.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Creating image of OS to run with qemu.

Post by iansjack »

Make your file a multiboot file and then qemu can run it directly.
Octocontrabass
Member
Member
Posts: 5580
Joined: Mon Mar 25, 2013 7:01 pm

Re: Creating image of OS to run with qemu.

Post by Octocontrabass »

bilsch01 wrote:1) I need an image of a bootable floppy with a bootsector that jumps to a file named jsec2.bin just like an msdos boot sector jumps to the file named IO.SYS contained in the file system on the disk. Is there a tool that I can adapt to this purpose?
You can use dd to create a blank image of the correct size, mkdosfs to format it, and mtools to copy your file. You'll have to come up with your own boot sector (or customize someone else's), but once you have that you can use dd to install it.
bilsch01 wrote:2) possibly there is some way to make a grub floppy image or iso that boots the executable in jsec2.bin. If there is a way using a grub tool?
No. Your file needs to be compatible with multiboot for GRUB to load it.
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Creating image of OS to run with qemu.

Post by MichaelPetch »

Is it correct you already have a bootloader that loads jsec2.bin when booted from the flash drive? I'm at present uncertain exactly how your existing one works. Can you post the code for the bootloader you are using to boot from the flash drive?
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Re: Creating image of OS to run with qemu.

Post by bilsch01 »

The OS is 16 sectors starting at sector 1 of the flashdrive partition /sdb1, which is sector 2 of the flashdrive.


bits 16
org 0x7c00

mov ax,0
mov ss,ax
mov sp,0x7b00
mov ds,ax

mov si,0x7b00 ;packet location
mov word[si],0x10 ;packet size
mov word[si+2],16 ;# of secs to transfer
mov word[si+4],0x0000 ;offset where to load
mov word[si+6],0x07e0 ;segment where to load
mov word[si+8],2 ;start sector# - mbr=0, this=1
mov word[si+10],0
mov word[si+12],0
mov word [si+14],0
mov ah,0x42 ;extended read
int 0x13
jc berr

mov ax,0x0e31 ;CF=0=success, print 1
int 0x10

jmp 0:0x7e00

berr:
mov ax,0x0e45 ;CF=1=failed, print E
int 0x10
jmp $

times 510-($-$$) db 'k'
dw 0xAA55
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Creating image of OS to run with qemu.

Post by MichaelPetch »

Your boot sector doesn't have partition data in it. Any reason why you just don't create a disk image with that boot sector and kernel? If you were willing to boot with QEMU using -hda then your boot sector wouldn't even need to change. If you want to use -fda you'd have to convert your bootloader to use CHS and Int 0x13/AH=2. If you were willing to simply boot as a HD image then you could do something like:

Code: Select all

# Create a 10MB disk image
dd if=/dev/zero of=disk.img bs=1M count=10

# Place boot sector boot.bin at LBA=0
dd if=boot.bin of=disk.img conv=notrunc

# Place kernel file kernel.bin at LBA=2
dd if=kernel.bin of=disk.img conv=notrunc seek=2
It's unclear if you want to read from a partition like FAT by looking for a file or whether you are willing to accept reading directly from the disk image like you are doing with the flash drive.
bilsch01
Member
Member
Posts: 42
Joined: Sat Dec 19, 2015 10:48 am

Re: Creating image of OS to run with qemu.

Post by bilsch01 »

OK. Thanks. I did something very similar to what you said. It is working partly, but the int 0x13 operation performed in the boot sector is returning an error code: 0Ch unsupported track or invalid media. I have never seen this before when I was using real hardware (flash drives).

My HD image is 17 sectors as follows:
sector 1: boot sector very similar to what I posted but with error reporting capability. See listing below.
sectors 2 thru 17: my operating system, jsec2.bin

Please note: I include the format=raw in command line: qemu-system-i386 -hda js1.bin format=raw
but it won't run unless I remove format=raw. Then it runs and reports the 0Ch error. Qemu says:
WRITE OPERATIONS ON BLOCK 0 WILL BE RESTRICTED - because format=raw was not specified.
I wonder if that is what is causing the 0Ch error.

Here is the revised boot sector:

Code: Select all

bits 16
org 0x7c00

mov ax,0
mov ss,ax
mov sp,0x7aff
mov ds,ax

mov si,0x7b00 ;packet location
mov word[si],0x10 ;packet size
mov word[si+2],16 ;# of secs to transfer
mov word[si+4],0x0000 ;offset  where to load
mov word[si+6],0x07e0 ;segment where to load
mov word[si+8],2 ;start sector# - mbr=0, this=1
mov word[si+10],0   
mov word[si+12],0
mov word [si+14],0
mov ah,0x42 ;extended read
mov dl,0x80
int 0x13
jc berr

mov ax,0x0e31 ;CF=0=success, print 1
int 0x10

	
jmp 0:0x7e00 ;long jump

berr:
push ax		;save error code from int 13
mov ax,0x0e45 ;CF=1=failed, print E
int 0x10
pop ax

mov	cx,2			;print the 2 digits in ah
hexout:
	rol ax,4		;put hi hex digit on right
	push ax			;save rol'd ax
	and al,0x0f
	add al,'0'		;0 thru 9
	cmp al,'9'
	jbe .prnhx
	add al,0x7		;A thru F
.prnhx: 
	mov ah,0xe			
	int 0x10		;write
	pop ax			;rol'd version of ax
	dec cx
	jnz hexout		;1 digit per loop

jmp $ 

times 510-($-$$) db 'k'
dw 0xAA55
Last edited by JAAman on Sun Nov 03, 2019 8:48 am, edited 1 time in total.
Reason: added code tags
MichaelPetch
Member
Member
Posts: 797
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Creating image of OS to run with qemu.

Post by MichaelPetch »

I suspect you are getting error 0x0c is because you haven't created a disk image with 18 full sectors. If you ask QEMU to read a sector beyond the edge of the disk image file then it will usually fail. That was one of the reasons that in my example I used DD to make a 10MB disk image (far more than just 18 sectors but you could have created a 9216 byte image file for 18 complete sectors). After making a 10MB image I then used DD again to insert the bootsector at the beginning of the image (LBA=0) and the kernel starting at LBA=2.

As for using raw=format you should be able to do this to boot as the first hard drive:

Code: Select all

qemu-system-i386 -drive file=disk.img,format=raw
Post Reply