Custom bootloader output to terminal not working

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.
User avatar
turdus
Member
Member
Posts: 496
Joined: Tue Feb 08, 2011 1:58 pm

Re: Custom bootloader output to terminal not working

Post by turdus »

For kputc() it should be:

Code: Select all

where = (unsigned short*)0xb8000 + (y * 160 + x * 2);
If bochs output does not show up for kputs(), you need to debug why. About bochs debugger this may help: http://bochs.sourceforge.net/doc/docboo ... ugger.html
After started, you can enable trace to see what instructions are executed at what offsets in which order:

Code: Select all

<bochs:1> trace on
<bochs:2> continue
You'll see if an irq or exception happens (invalid opcode, gpf etc.). You can also set a breakpoint at address 0x1000 and examine the stack if it's right.
BetaWar
Posts: 12
Joined: Thu May 28, 2009 11:23 pm

Re: Custom bootloader output to terminal not working

Post by BetaWar »

Okay, so I have narrowed the problem a bit further, by going through the debugger output (and trace makes for quite a bit of that), and trying a few things in the source I found that the problem seems to deal with global variables (though not global constants), constant pointers, and pointers; setting them all to null.

I can successfully pass ints and chars around, but can't get the const char* to work and am unable to get the global variables to work at all (even primitives).

Here is my main, bootloader, and loader sources:

Code: Select all

[BITS 16]
[ORG 0x7c00]

checkENine:
	; check if e9 hack is available
	in al, 0xe9
	cmp al, 0xe9
	jne .noENine
	jmp .ENine
.noENine:
	mov ah, 0x0e
	mov al, 'N'
	mov bh, 0x0f
	mov bl, 0
	int 0x10
	jmp reset_drive
.ENine:
	mov ah, 0x0e
	mov al, 'Y'
	mov bh, 0x0f
	mov bl, 0
	int 0x10

reset_drive:

	mov al, '1'
	out 0xe9, al

	mov ah, 0 ; reset command
	int 0x13 ; call interrupt 13
	or ah, ah ; check for error
	jnz reset_drive ; try again if ah != 0

	xor ax, ax
	mov ss, ax
	mov sp, 0x600

	mov ax, 0
	mov es, ax
	mov bx, 0x1000 ; destination address = 0000:1000

	mov ah, 0x02 ; read sector command
	mov al, 0x1c; 28 ; number of 512 byte sectors to read
	mov ch, 0 ; cylinder = 0
	mov cl, 0x02 ; sector = 2
	mov dh, 0 ; head = 0
	int 0x13 ; call int 13
	or ah, ah ; check for error
	jnz reset_drive ; try again

	cli ; disable interrupts

	xor ax, ax
	mov ds, ax

	lgdt [gdt_desc] ; load the initial gdt descriptor - we will redo 
  ; this in the kernel
	
	mov eax, cr0
	or eax, 1 ; set bit 0 to get to protected mode
	mov cr0, eax ; set the values

	jmp 0x08:clear_pipe

[BITS 32]
clear_pipe:
	mov al, '2'
	out 0x0e9, al

	mov ax, 0x10 ; save data segment identifier
	mov ds, ax ; move a valid data segment into the data segment register
	mov ss, ax ; move a valid data segment into the stack segment register
	mov esp, 0x090000 ; move the stack pointer to 90000h

	mov al, '3'
	out 0x0e9, al

	jmp 0x08:0x01000

gdt:
gdt_null:
	dd 0
	dd 0
gdt_code:
	dw 0x0ffff
	dw 0
	db 0
	db 10011010b
	db 11001111b
	db 0
gdt_data:
	dw 0x0ffff
	dw 0
	db 0
	db 10010010b
	db 11001111b
	db 0
gdt_end:

gdt_desc:
	dw gdt_end - gdt - 1 ; limit (size)
	dd gdt ; address of the gdt

times 510-($-$$) db 0
	dw 0x0aa55

Code: Select all

global loader						   ; making entry point visible to linker
 
extern main							; kmain is defined in kmain.cpp
 
extern start_ctors					  ; beginning and end
extern end_ctors						; of the respective
extern start_dtors					  ; ctors and dtors section,
extern end_dtors						; declared by the linker script

section .text
 
; reserve initial kernel stack space
STACKSIZE equ 0x4000					; that's 16k.
 
loader:
	mov al, '4'
	out 0x0e9, al

	mov esp, stack + STACKSIZE		 ; set up the stack
	mov ebp, esp

	;push eax							; Multiboot magic number
	;push ebx							; Multiboot info structure
 
	mov  ebx, start_ctors			   ; call the constructors
	jmp  .ctors_until_end
.call_constructor:
	call [ebx]
	add  ebx,4
.ctors_until_end:
	cmp  ebx, end_ctors
	jb   .call_constructor
 
	call main						  ; call kernel proper
 
	mov  ebx, end_dtors				 ; call the destructors
	jmp $
;	jmp  .text
.call_destructor:
	sub  ebx, 4
	call [ebx]
.dtors_until_end:
	cmp  ebx, start_dtors
	jb   .call_destructor
 
	cli
.hang:
	hlt								 ; halt machine should kernel return
	jmp  .hang

section .bss
 
align 4
stack:
	resb STACKSIZE					  ; reserve 16k stack on a doubleword boundary

Code: Select all

#include "system.h"

#define BOCHSOUT(c) outportb(0xe9, c)
#define BOCHSOUTS(s) for(int i = 0; (s)[i] != '\0'; i++){\
	BOCHSOUT((s)[i]);\
}

int x = 0;
int y = 0;
char attrib = 0xf;

void scroll(void){
  unsigned blank = 0x20 | (attrib << 8);
  unsigned tmp;
  if(y >= 25){
    tmp = y - 24;
    memcpy((unsigned char*)0xb8000, (unsigned char*)(0xb8000 + tmp * 80), (25 - tmp) * 80 * 2);
    memsetw((unsigned short*)0xb8000 + (25 - tmp) * 80, blank, 80);
    y = 24;
  }
}

void move(void){
  unsigned tmp = y * 80 + x;
  outportb(0x3D4, 14);
  outportb(0x3D5, tmp >> 8);
  outportb(0x3D4, 15);
  outportb(0x3D5, tmp);
}

void clear(void){
  unsigned short blank = 0x20 | (attrib << 8);
  int i;
  for(i = 0; i < 25; i++){
    memsetw((unsigned short*)0xb8000 + i * 80, blank, 80);
  }
  x = y = 0;
  move();
}

void kputc(char c){
  unsigned short* where;
  unsigned short att = attrib << 8;
  if(c == '\b' && x != 0){
    x--;
    where = (unsigned short*)((unsigned char*)0xb8000 + (y * 80 + x * 2));
		*where = ' ' | att;
  }
  else if(c == 0x09){
    x = (x + 8) & !7;
  }
  else if(c == '\r'){
    x = 0;
  }
  else if(c == '\n'){
    x = 0;
    y++;
  }
  else if(c >= ' '){
    where = (unsigned short*)((unsigned char*)0xb8000 + (y * 80 + x * 2));
		BOCHSOUT(c);
    *where = c | att;
    x++;
  }
  if(x >= 80){
    x = 0;
    y++;
  }
  scroll();
  move();
}

void kputs(const char* s){
  for(int i = 0; s[i] != '\0'; i++){
		BOCHSOUT(s[i]);
    kputc(s[i]);
  }
}

void print(const char* s){
	unsigned char* vidmem = (unsigned char*)0xb8000;
	unsigned short offset;
	unsigned int i;
	outportb(0x3d4, 14);
	offset = inportb(0x3d5) << 8;
	outportb(0x3d4, 15);
	offset |= inportb(0x3d5);
	for(i = 0; s[i] != 0; i++){
		*vidmem = s[i];
		*(vidmem + 1) = 0x7;
		vidmem += 2;
	}
	offset += i;
	outportb(0x3d5, (unsigned char)offset);
	outportb(0x3d4, 14);
	outportb(0x3d5, (unsigned char)(offset >> 8));
}


#define PORT 0x3f8   /* COM1 */
 
void init_serial() {
	outportb(PORT + 1, 0x00);    // Disable all interrupts
	outportb(PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
	outportb(PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
	outportb(PORT + 1, 0x00);    //                  (hi byte)
	outportb(PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
	outportb(PORT + 2, 0xC7);    // Enable FIFO, clear them, with 14-byte threshold
	outportb(PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
}

bool isTransmitEmpty(void){
	return inportb(PORT + 5) & 0x20;
}

void write_serial(const char* s){
	for(int i = 0; s[i] != '\0'; i++){
		while(!isTransmitEmpty());
		outportb(PORT, s[i]);
	}
	while(!isTransmitEmpty());
	outportb(PORT, '\0');
}

int main(){
	BOCHSOUTS("This is a test\n\n");
	kputc('h');
	kputc('e');
	kputc('l');
	kputc('l');
	kputc('l');
	kputc('o');
	kputc(' ');
	kputc('w');
	kputc('o');
	kputc('r');
	kputc('l');
	kputc('d');
	kputc('\n');
	BOCHSOUT('5');
	BOCHSOUT('\n');
	init_serial();
	write_serial("Entered main\n");
	BOCHSOUT('6');
	//clear();
	//attrib = 0x0f;
	kputs("Hello world\n");
	write_serial("Should have output 'Hello World'\n");
	//kputs("Hello world\n");
	//kputs("Hello world\n");
	print("THIS IS A TEST\n");
	write_serial("Should have output 'THIS IS A TEST'\n");
	for(;;); // infinite loop
	return 0;
}
I am personally thinking that the problem is in the loader.s file, but I am not sure what could be causing it.
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: Custom bootloader output to terminal not working

Post by Combuster »

I can successfully pass ints and chars around, but can't get the const char* to work
FAQ?
"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 ]
BetaWar
Posts: 12
Joined: Thu May 28, 2009 11:23 pm

Re: Custom bootloader output to terminal not working

Post by BetaWar »

From what I read on the FAQs it looks like the most promising thing was the "strings don't work" page, but that just tells me that I need the rodata section, which I already have (I am using a copy of the C++ barebones linker script, which I have also posted on the previous page -- last post, last code segment). I quickly attempted to modify my linker script to this:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(loader)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    start_ctors = .;
    *(.ctor*)
    end_ctors = .;
    start_dtors = .;
    *(.dtor*)
    end_dtors = .;
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code)) {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code)) {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}
Basically the one that was provided in the FAQ with a few modifications (mainly the addition of the ctor and dtor code and changing it to loader instead of start. As could probably be expected; that worked no better.
Post Reply