problem with using printk in tftpbootable code

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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: problem with using printk in tftpbootable code

Post by Gigasoft »

The reason that it was crashing was obvious. He was returning from the entry function, when there was no return address present.

@aloktiagi: Okay, so your first main requirement is initializing the ethernet card, you say. Well, I don't know if you want to make a system that has one specific function and will work with exactly 1 configuration with a specific type of ethernet card, or if you want to make something general. Either way, you should definitely have an IDT and exception handlers so that you can print information when the program crashes, which it will do a lot of the time. You should probably have an IRQ handling system sooner or later, since most devices including network cards cause IRQs. If you intend the system to be multithreaded, you should implement thread switching and have a way for threads to wait for events and be resumed on signaling of the events. Whether the system is multithreaded or not will affect how you perform I/O operations. And I really hope you're going to test things in Bochs from now on, because things will become increasingly complicated and with the problems you had with even getting into your C code, just think of how hard it will be to figure out what's wrong when your program actually does something.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: problem with using printk in tftpbootable code

Post by neon »

Hello,
Gigasoft wrote:The reason that it was crashing was obvious. He was returning from the entry function, when there was no return address present.
I know, I was expecting the OP to give me the answer.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: problem with using printk in tftpbootable code

Post by aloktiagi »

Hi,

Was struggling with a problem but could not find the solution. Here from the main code when i do a jump to k_clear_screen() the machine reboots. I've tried putting loops and concluded that when i make this jump the reboot happens. Any idea why??

Following are my bootloader and c code.

bootloader.asm

Code: Select all

bits 16
org 7c00h

cli
xor ax,ax
mov ds,ax
mov es,ax
lgdt [gdt_desc]
mov si,7e00h ; if your kernel has been placed at offset 200h, like you did
mov di,1000h ; the address where the kernel should be
mov cx,512 ; whatever the size of the kernel is
rep movsb
mov eax,cr0
or al,1
mov cr0,eax
jmp 08h:clear_pipe
bits 32
clear_pipe:
mov ax,10h
mov ds,eax
mov es,eax
mov ss,eax
mov esp,90000h
jmp 08h:00001000h
;mov byte [ds:0B8000h], 'P'      ; Move the ASCII-code of 'P' into first video memory
;mov byte [ds:0B8001h], 1Bh      ; Assign a color code

jmp $
gdt dd 0ffffh,0cf9a00h,0ffffh,0cf9200h
gdt_desc dw 23
dd gdt-8
main.c

Code: Select all

#define WHITE_TXT 0x07 // white on black text

int k_clear_screen();
int k_printf(char *message, unsigned int line);


int main() // like main in a normal C program
{
        //for(;;);
        k_clear_screen();
        k_printf("Hi!\nHow's this for a starter OS?", 0);
        for(;;);
}

int k_clear_screen() // clear the entire text screen
{
        for(;;);
        char *vidmem = (char *) 0xb8000;
        unsigned int i=0;
        while(i < (80*25*2))
        {
                vidmem[i]=' ';
                i++;
                vidmem[i]=WHITE_TXT;
                i++;
        }
        for(;;);
        return 0;
}

int k_printf(char *message, unsigned int line) // the message and then the line #
{
        char *vidmem = (char *) 0xb8000;
        unsigned int i=0;

        i=(line*80*2);

        while(*message!=0)
        {
                if(*message=='\n') // check for a new line
                {
                        line++;
                        i=(line*80*2);
                        *message++;
                } else {
                        vidmem[i]=*message;
                        *message++;
                        i++;
                        vidmem[i]=WHITE_TXT;
                        i++;
                }
        }

        return 0;
}
Compilation steps:
nasm -f bin bootloader.asm -o bootloader.bin
gcc -ffreestanding -c main.c -o main.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
dd if=bootsect.bin of=os1.img bs=512 count=1
dd if=kernel.bin of=os1.img bs=512 seek=1
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: problem with using printk in tftpbootable code

Post by Gigasoft »

What is the size of kernel.bin?
Looking at bootloader.asm, is there some line you think has to do with this?
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: problem with using printk in tftpbootable code

Post by aloktiagi »

the size of kernel.bin is 4.3kb. Is that the problem? I put 5000 instead of 512 in bootloader.asm but it still didn't work!
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: problem with using printk in tftpbootable code

Post by Combuster »

mov si,7e00h ; if your kernel has been placed at offset 200h,
Did you actually do that?
"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 ]
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: problem with using printk in tftpbootable code

Post by aloktiagi »

Sorry for asking this. How do you do that?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: problem with using printk in tftpbootable code

Post by gerryg400 »

aloktiagi,

My advice is to look inside your image file. Use a disassembler. Follow through the disassembled code step by step and you will surely be able to see what's wrong.

Take the time to learn to do this. It is absolutely an essential skill.

BTW, I don't know what's wrong.

- gerryg400
If a trainstation is where trains stop, what is a workstation ?
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: problem with using printk in tftpbootable code

Post by aloktiagi »

the disassembled part of main ( my comments in the code below)

Code: Select all

00001000 <main>:
    1000:       55                      push   %ebp
    1001:       89 e5                   mov    %esp,%ebp
    1003:       83 ec 08                sub    $0x8,%esp
    1006:       83 e4 f0                and    $0xfffffff0,%esp
    1009:       b8 00 00 00 00          mov    $0x0,%eax
    100e:       83 c0 0f                add    $0xf,%eax
    1011:       83 c0 0f                add    $0xf,%eax
    1014:       c1 e8 04                shr    $0x4,%eax
    1017:       c1 e0 04                shl    $0x4,%eax
    101a:       29 c4                   sub    %eax,%esp
    101c:       c7 45 fc 00 80 0b 00    movl   $0xb8000,0xfffffffc(%ebp)
    1023:       8b 45 fc                mov    0xfffffffc(%ebp),%eax
    1026:       c6 00 41                movb   $0x41,(%eax)
    1029:       8b 45 fc                mov    0xfffffffc(%ebp),%eax
    102c:       40                      inc    %eax
    102d:       c6 00 07                movb   $0x7,(%eax)
    1030:       e8 fc ff ff ff          call   1031 <main+0x31>  this is the jump to clear screen(should this be 1049 instead of 1031)
    1035:       83 ec 08                sub    $0x8,%esp
    1038:       6a 00                   push   $0x0
    103a:       68 00 00 00 00          push   $0x0
    103f:       e8 fc ff ff ff          call   1040 <main+0x40>
    1044:       83 c4 10                add    $0x10,%esp
    1047:       eb fe                   jmp    1047 <main+0x47>

00001049 <k_clear_screen>:
    1049:       55                      push   %ebp
    104a:       89 e5                   mov    %esp,%ebp
    104c:       83 ec 08                sub    $0x8,%esp
    104f:       eb fe                   jmp    104f <k_clear_screen+0x6>
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: problem with using printk in tftpbootable code

Post by Gigasoft »

To be sure, disassemble the final os.img instead of kernel.o or main.o. If you attach os.img here, I may be able to check its correctness.
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: problem with using printk in tftpbootable code

Post by aloktiagi »

I've attached all the files i.e os1.img, main.c and bootsect.asm and the compilation steps that i followed

nasm -f bin bootloader.asm -o bootloader.bin
gcc -ffreestanding -c main.c -o main.o
ld -i -e _main -Ttext 0x1000 -o kernel.o main.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
dd if=bootsect.bin of=os1.img bs=512 count=1
dd if=kernel.bin of=os1.img bs=512 seek=1

I was not able to disassemble os1.img!!

Couldn't attach the os1.img so attaching it as os1.txt
Attachments
os1.txt
(701 Bytes) Downloaded 124 times
main.c
(1.05 KiB) Downloaded 147 times
bootsect.asm
(736 Bytes) Downloaded 62 times
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: problem with using printk in tftpbootable code

Post by gerryg400 »

ld -i -e _main -Ttext 0x1000 -o kernel.o main.o
aloktiagi,
Don't use -i. Check the binutils doc.

- gerryg400
If a trainstation is where trains stop, what is a workstation ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: problem with using printk in tftpbootable code

Post by gerryg400 »

To disassemble your image try

Code: Select all

 objdump --target=binary --architecture=i386  -D os1.img
Use these tools, read the manuals, never guess what an option does, take your time.
- gerryg400
If a trainstation is where trains stop, what is a workstation ?
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: problem with using printk in tftpbootable code

Post by aloktiagi »

Hi,

Thanks it worked :D :D .

-i option incremental linking will have to read abt it

Thanks a lot
Alok
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: problem with using printk in tftpbootable code

Post by Gigasoft »

Your OS image that you posted hasn't had relocations applied correctly. The instruction at 0x1030 is still call 0x1031 in the final os.img. Did it become the correct address when you linked without -i?
Post Reply