0

I have coded the minimal realization of OS booting on assembly:

    [BITS 16]
[ORG 0x7C00]
start:
mov ax, cs 
mov ds, ax  
mov ss, ax 
mov sp, start 

mov ax, 0x1100
mov es, ax
mov bx, 0x0000
mov dl, 1 
mov dh, 0 
mov ch, 0 
mov cl, 2 
mov al, 1 
mov ah, 0x02 
int 0x13 

mov ax, 0x1200
mov es, ax
mov bx, 0x0000
mov dl, 1
mov dh, 0
mov ch, 0
mov cl, 3
mov al, 1
mov ah, 0x02
int 0x13


cli
lgdt [gdt_info]
in al, 0x92
or al, 2
out 0x92, al    
mov eax, cr0
or al, 1
mov cr0, eax
jmp 0x8:protected_mode 
gdt:
db 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
;base=0, size=4Gb, P=1, DPL=0, S=1(user),
;Type=1(code), Access=00A, G=1, B=32bit
db 0xff, 0xff, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00
;base=0, size=4Gb, P=1, DPL=0, S=1(user),
;Type=0(data), Access=0W0, G=1, B=32bit
db 0xff, 0xff, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
gdt_info: 
dw gdt_info - gdt
dw gdt, 0
[BITS 32]
protected_mode:
mov ax, 0x10
mov es, ax
mov ds, ax
mov ss, ax
call 0x11000
times (512 - ($ - start) - 2) db 0 
db 0x55, 0xAA

And minimal realization of Kernel in C:

extern "C" int kmain();
__declspec(naked) void startup()
{
    __asm {
        call kmain;
    }
}
#define VIDEO_BUF_PTR (0xb8000)
void out_str(int color, const char* ptr, unsigned int strnum)
{
    unsigned char* video_buf = (unsigned char*)VIDEO_BUF_PTR;
    video_buf += 80 * 2 * strnum;
    while (*ptr)
    {
        video_buf[0] = (unsigned char)*ptr;
        video_buf[1] = color;
        video_buf += 2;
        ptr++;
    }
}
extern "C" int kmain()
{
    const char* hello = "Welcome to HelloWorldOS (gcc edition)!";
    out_str(0x07, hello, 0);
    while (1)
    {
        __asm hlt;
    }
    return 0;
}

Compile kernel with Microsoft C complier Transaltor of boot: Yasm To get the data from PE-kernel used dumpbin and get:

Microsoft (R) COFF/PE Dumper Version 14.28.29335.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file kernel.bin

PE signature found

File Type: EXECUTABLE IMAGE

FILE HEADER VALUES
             14C machine (x86)
               2 number of sections
        604FC5B3 time date stamp Mon Mar 15 23:38:11 2021
               0 file pointer to symbol table
               0 number of symbols
              E0 size of optional header
             103 characteristics
                   Relocations stripped
                   Executable
                   32 bit word machine

OPTIONAL HEADER VALUES
             10B magic # (PE32)
           14.28 linker version
             200 size of code
             200 size of initialized data
               0 size of uninitialized data
            1000 entry point (00011000)
            1000 base of code
            2000 base of data
           10000 image base (00010000 to 00012FFF)
            1000 section alignment
             200 file alignment
            6.00 operating system version
            0.00 image version
            6.00 subsystem version
               0 Win32 version
            3000 size of image
             200 size of headers
             636 checksum
               1 subsystem (Native)
             500 DLL characteristics
                   NX compatible
                   No structured exception handler
          100000 size of stack reserve
            1000 size of stack commit
          100000 size of heap reserve
            1000 size of heap commit
               0 loader flags
              10 number of directories
               0 [       0] RVA [size] of Export Directory
               0 [       0] RVA [size] of Import Directory
               0 [       0] RVA [size] of Resource Directory
               0 [       0] RVA [size] of Exception Directory
               0 [       0] RVA [size] of Certificates Directory
               0 [       0] RVA [size] of Base Relocation Directory
            2044 [      1C] RVA [size] of Debug Directory
               0 [       0] RVA [size] of Architecture Directory
               0 [       0] RVA [size] of Global Pointer Directory
               0 [       0] RVA [size] of Thread Storage Directory
               0 [       0] RVA [size] of Load Configuration Directory
               0 [       0] RVA [size] of Bound Import Directory
               0 [       0] RVA [size] of Import Address Table Directory
               0 [       0] RVA [size] of Delay Import Directory
               0 [       0] RVA [size] of COM Descriptor Directory
               0 [       0] RVA [size] of Reserved Directory


SECTION HEADER #1
   .text name
      AE virtual size
    1000 virtual address (00011000 to 000110AD)
     200 size of raw data
     200 file pointer to raw data (00000200 to 000003FF)
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
60000020 flags
         Code
         Execute Read

SECTION HEADER #2
   .data name
      B0 virtual size
    2000 virtual address (00012000 to 000120AF)
     200 size of raw data
     400 file pointer to raw data (00000400 to 000005FF)
       0 file pointer to relocation table
       0 file pointer to line numbers
       0 number of relocations
       0 number of line numbers
C0000040 flags
         Initialized Data
         Read Write

  Debug Directories

        Time Type        Size      RVA  Pointer
    -------- ------- -------- -------- --------
    604FC5B3 coffgrp       50 00002060      460

  Summary

        1000 .data
        1000 .text

As emulator use Qemu As the result I get:

Booting from Hard disk...
Boot failed: could not read the boot disk

Booting from from Floppy...

And nothing more. So why my OS doesn't work? I think that the problem is it bootloader, but I'm not sure. (I only started to learn opeation systems, so I don't know much in it). I have to run it with all my upper conditions

12
  • 2
    First, you set DS to CS, assuming that CS == 0. If you use an [ORG 0x7C00], you must assume that CS is 0. This may not be the case. Don't set your segment registers using the current CS value. Directly set them using known values. Second, you set the DL register to 1 for your call to the BIOS read sector. A value of 1 is the second floppy. Do you have 2 floppy drives? Bochs says it is booting from the Hard disk, though you are reading from the second floppy. Commented Mar 16, 2021 at 19:57
  • Thanks for your answer. So to boot os I use 2 floppy disk, on the first one is bootloader and on the second one is kernel. (run with commnad: qemu-system-x86_64 -fda boot.BIN -fdb kernel.BIN -L "C:\Program Files\qemu") Commented Mar 16, 2021 at 20:31
  • 1
    That's right, QEMU, not Bochs. Anyway, first tell QEMU you are booting a floppy and the first error will go way. Second, how are you putting your boot code and kernel file onto the floppy images? Can you verify that they are in the correct spots? Have you single-stepped through the code to see where the error is, to watch what happens? Also, a PE file has a header on it. Are you writing that header to the floppy image? Do you need to skip the header? Commented Mar 17, 2021 at 0:20
  • Just following up. Did you find the error? If not, can you post the two floppy images somewhere for us to look at? Commented Mar 17, 2021 at 23:27
  • Sorry for avoiding, I just need a lot of time to understand all it( So, firstly, I think that I tell QEMU that I'm booting from floppy with the comand ( -fda -fdb), and I cannot find any more information about it. Also I convinced that they locate in the correct slots, booter in the frist (0), and kernel in the second (0). Btw I really don't know how to debbug assembly codes, I've just output symbols and try to notice where's the error occurs, but I didn't find anything. Commented Mar 18, 2021 at 20:03

1 Answer 1

1

Just input

qemu-system-x86_64 -boot order=ab -drive file=boot.BIN,format=raw,index=0,if=floppy -drive file=kernel.BIN,format=raw,index=1,if=floppy -L "C:\Program Files\qemu"

instead of

qemu-system-x86_64 -fda boot.BIN -fdb kernel.BIN -L "C:\Program Files\qemu"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.