Compare commits

4 Commits

23 changed files with 709 additions and 188 deletions

View File

@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
CFLAGS = -ffreestanding -O2 -Wall -Wextra
OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/PhysicalMemoryManager.o $(BUILD_DIR)/io.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o
OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/window.o $(BUILD_DIR)/cursor.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/PhysicalMemoryManager.o $(BUILD_DIR)/io.o $(BUILD_DIR)/vesa.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o
SRC_DIR = src
BUILD_DIR = build
@ -36,7 +36,9 @@ iso: clean_iso clean build
grub-mkrescue -o build/barinkOS.iso root
test:
$(EMULATOR) -kernel $(BUILD_DIR)/myos.bin -serial stdio -vga std -display gtk -m 2G -cpu core2duo
$(EMULATOR) -kernel $(BUILD_DIR)/myos.bin -serial mon:stdio -vga std -display gtk -m 2G -cpu core2duo # -monitor stdio
test_iso:
$(EMULATOR) -cdrom build/barinkOS.iso -serial mon:stdio -vga std -display gtk -m 2G -cpu core2duo
build_kernel: $(OBJ_LINK_LIST)
$(CC) -T $(SRC_DIR)/kernel//linker.ld -o $(BUILD_DIR)/myos.bin \
@ -50,7 +52,7 @@ clean:
rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS)
$(BUILD_DIR)/kernel.o:
$(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti
$(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti -fpermissive
$(BUILD_DIR)/kterm.o:
$(CPP) -c $(SRC_DIR)/kernel/tty/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti
@ -86,5 +88,11 @@ $(BUILD_DIR)/string.o:
$(BUILD_DIR)/PhysicalMemoryManager.o:
$(CPP) -c $(SRC_DIR)/kernel/memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/PhysicalMemoryManager.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/pci.o:
$(CPP) -c $(SRC_DIR)/kernel/pci.cpp -o $(BUILD_DIR)/pci.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/vesa.o:
$(CPP) -c $(SRC_DIR)/kernel/drivers/vesa/vesa.cpp -o $(BUILD_DIR)/vesa.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/window.o:
$(CPP) -c $(SRC_DIR)/gui/window.cpp -o $(BUILD_DIR)/window.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/cursor.o:
$(CPP) -c $(SRC_DIR)/gui/cursor.cpp -o $(BUILD_DIR)/cursor.o $(CFLAGS) -fno-exceptions -fno-rtti

View File

@ -6,6 +6,9 @@
________________________
### Screenshot(s)
![Graphics mode](screenshots/BarinkOS_VBE_GRAPHICS.png) \
It may not look like much but I am proud of it! We are in graphics mode.
![Scrolling the terminal](screenshots/Screenshot1.png) \
The first scrolling boot screen. 😲

BIN
screenshots/BarinkOS_VBE_GRAPHICS.png (Stored with Git LFS) Normal file

Binary file not shown.

5
src/gui/Graphics.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
class Graphics {
};

7
src/gui/Widget.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
class Widget{
virtual void draw();
};

17
src/gui/cursor.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "cursor.h"
void Cursor::draw(){
for(int i = 0; i < this->width; i++){
for(int j = 0; j < this->height; j++){
if(this->bitmap[j * this->width + i] == 1 ){
putPixel(i + this->x,j + this->y, 0xFF000000);
}
}
}
}
Cursor::Cursor(int x, int y){
this->x = x;
this->y = y;
}

38
src/gui/cursor.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include "../kernel/drivers/vesa/vesa.h"
class Cursor{
public:
void draw();
Cursor(int x, int y);
private:
int x;
int y;
const int width= 16;
const int height= 10;
const int bitmap [160] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,
0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
};

33
src/gui/window.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "window.h"
int Window::getWidth(){
return this->rect.width;
}
int Window::getHeight(){
return this->rect.height;
}
void Window::setWidth(int& width){
this->rect.width = width;
}
void Window::setHeight(int& height){
this->rect.height = height;
}
int Window::getX(){
return this->rect.x;
}
int Window::getY(){
return this->rect.y;
}
Window::Window (Rect& rect , uint32_t colour){
this->rect = rect;
this->Background_colour = colour;
}
void Window::draw(){
drawRect(this->getX() , this->getY() , this->getWidth() , this->getHeight() ,this->Background_colour );
}

37
src/gui/window.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include "../kernel/drivers/vesa/vesa.h"
#include "Widget.h"
struct Rect {
int width;
int height;
int x;
int y;
};
class Window : Widget{
public:
int getX();
int getY();
int getWidth();
int getHeight();
void setWidth(int&);
void setHeight(int&);
Window (Rect& rect , uint32_t colour);
void draw();
private:
Rect rect;
uint32_t Background_colour;
};

View File

@ -3,7 +3,8 @@
*/
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
.set MEMINFO, 1<<1 /* provide memory map */
.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */
.set VIDEO, 1<<2 /* provide video mode */
.set FLAGS, ALIGN | MEMINFO | VIDEO /* this is the Multiboot 'flag' field */
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */
@ -12,7 +13,15 @@
.long MAGIC
.long FLAGS
.long CHECKSUM
.long 0 # unused
.long 0 # .
.long 0 # .
.long 0 # .
.long 0 # unused
.long 0 # set graphics mode
.long 800 # screen witdh
.long 600 # screen height
.long 32 # bpp
.section .bss
.align 16

View File

@ -1,17 +1,17 @@
#pragma once
#include "bootloader/multiboot.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
#include "../gui/window.h"
#include "../gui/cursor.h"
#include "tty/kterm.h"
#include "drivers/vesa/vesa.h"
void CheckMBT ( multiboot_info_t* mbt ){
/* Set MBI to the addresss of the multiboot information structure*/
multiboot_info_t * mbi = (multiboot_info_t *) mbt;
/* Print out the flags */
printf("flags = 0x%x\n", (unsigned) mbi->flags);
printf("flags = 0x%8x\n", (unsigned) mbi->flags);
/* Are mem_* valid? */
if ( CHECK_FLAG(mbi->flags,0)){
@ -68,9 +68,41 @@ void CheckMBT ( multiboot_info_t* mbt ){
}
/* Draw diagonal blue line */
if (CHECK_FLAG (mbt->flags, 12)){
if (CHECK_FLAG (mbt->flags, 11)){
printf("Can draw!");
}
// Init vesa driver
initVBEDevice(mbt);
// Fill screen with blue
// colours AARRGGBB
drawRect(0, 0 , VbeModeInfo->width,VbeModeInfo->height, 0xFF0000FF);
// Create two windows
Rect rect_window1 {};
Rect rect_window2 {};
rect_window1.height =200;
rect_window1.width = 300;
rect_window1.x = 50;
rect_window1.y = 50;
rect_window2.height =200;
rect_window2.width = 300;
rect_window2.x = 300;
rect_window2.y = 200;
Window window_1 ( rect_window1, 0xFF00F0FF);
Window window_2 (rect_window2, 0xFFAACCDD);
window_1.draw();
window_2.draw();
Cursor cursor (70,100);
cursor.draw();
}
}

View File

@ -0,0 +1,136 @@
#include "vesa.h"
VbeInfoBlock* vbeInfo;
vbe_mode_info_structure* VbeModeInfo;
void initVBEDevice(multiboot_info_t* mbt){
print_serial("initVBEDevice");
vbeInfo = (VbeInfoBlock*) mbt->vbe_control_info;
printf_serial("Signature: %s, V0x%x\n", vbeInfo->VbeSignature, vbeInfo->VbeVersion);
VbeModeInfo = (vbe_mode_info_structure*) mbt->vbe_mode_info;
printf_serial("VESA video mode info: Width: %d Height: %d BPP: %d\n", VbeModeInfo->width, VbeModeInfo->height , VbeModeInfo->bpp);
printf_serial("VideoMemory Location: 0x%x \n", VbeModeInfo->framebuffer );
}
void putPixel( int x, int y , uint32_t colour){
// printf_serial("putPixel x: %d, y: %d\n", x, y);
///fb + mbt->framebuffer_pitch * y + 4 * x ,NOTE: this calculation is very important
*(uint32_t*) ( VbeModeInfo->framebuffer + VbeModeInfo->pitch * y + 4 * x ) = colour;
}
void drawLine(int x1, int y1, int x2, int y2, uint32_t colour ){
print_serial("drawline\n");
// See Bresenham's line algorithm
int deltaX = x2 - x1;
int deltaY = y2 - y1;
int D = 2 * deltaY - deltaX;
int y = y1;
for ( int x = x1; x < x2; x++){
putPixel(x,y, colour);
if( D > 0){
y +=1;
D = D - 2 * deltaX;
}
D = D + 2 * deltaY;
}
}
void drawRect ( int x, int y, int width, int height, uint32_t colour ){
print_serial("drawRect\n");
for ( int i = x; i < x + width; i ++)
{
for(int j = y; j < y + height; j++){
putPixel(i,j, colour);
}
}
}
void blueDiagnalLineTest(multiboot_info_t* mbt){
multiboot_uint32_t color;
unsigned i;
void *fb = (void *) (unsigned long) mbt->framebuffer_addr;
switch (mbt->framebuffer_type)
{
case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED:
{
unsigned best_distance, distance;
struct multiboot_color *palette;
palette = (struct multiboot_color *) mbt->framebuffer_palette_addr;
color = 0;
best_distance = 4*256*256;
for (i = 0; i < mbt->framebuffer_palette_num_colors; i++)
{
distance = (0xff - palette[i].blue) * (0xff - palette[i].blue)
+ palette[i].red * palette[i].red
+ palette[i].green * palette[i].green;
if (distance < best_distance)
{
color = i;
best_distance = distance;
}
}
}
break;
case MULTIBOOT_FRAMEBUFFER_TYPE_RGB:
color = ((1 << mbt->framebuffer_blue_mask_size) - 1)
<< mbt->framebuffer_blue_field_position;
break;
case MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT:
color = '\\' | 0x0100;
break;
default:
color = 0xffffffff;
break;
}
for (i = 0; i < mbt->framebuffer_width
&& i < mbt->framebuffer_height; i++)
{
switch (mbt->framebuffer_bpp)
{
case 8:
{
multiboot_uint8_t *pixel = (multiboot_uint8_t*)fb + mbt->framebuffer_pitch * i + i;
*pixel = color;
}
break;
case 15:
case 16:
{
multiboot_uint16_t *pixel
= (multiboot_uint16_t*)fb + mbt->framebuffer_pitch * i + 2 * i;
*pixel = color;
}
break;
case 24:
{
multiboot_uint32_t *pixel
= (multiboot_uint32_t*)fb + mbt->framebuffer_pitch * i + 3 * i;
*pixel = (color & 0xffffff) | (*pixel & 0xff000000);
}
break;
case 32:
{
multiboot_uint32_t *pixel
= (multiboot_uint32_t*)fb + mbt->framebuffer_pitch * i + 4 * i;
*pixel = color;
}
break;
}
}
}

View File

@ -0,0 +1,66 @@
#pragma once
#include <stdint.h>
#include "../../bootloader/multiboot.h"
#include "../../serial.h"
struct vbe_mode_info_structure{
uint16_t attributes;
uint8_t window_a;
uint8_t window_b;
uint16_t granularity;
uint16_t window_size;
uint16_t segment_a;
uint16_t segment_b;
uint32_t win_func_ptr;
uint16_t pitch;
uint16_t width;
uint16_t height;
uint8_t w_char;
uint8_t y_char;
uint8_t planes;
uint8_t bpp;
uint8_t banks;
uint8_t memory_model;
uint8_t bank_size;
uint8_t image_pages;
uint8_t reserved0;
uint8_t red_mask;
uint8_t red_position;
uint8_t green_mask;
uint8_t green_position;
uint8_t blue_mask;
uint8_t blue_position;
uint8_t reserved_mask;
uint8_t reserved_position;
uint8_t direct_color_attributes;
uint32_t framebuffer;
uint32_t off_screen_mem_off;
uint16_t off_screen_mem_size;
uint8_t reserved1[206];
}__attribute__((packed));
struct VbeInfoBlock {
char VbeSignature[4];
uint16_t VbeVersion;
uint16_t OemStringPtr;
uint8_t Capabilities;
uint16_t VideoModePtr;
uint16_t TotalMemory;
}__attribute__((packed));
extern VbeInfoBlock* vbeInfo;
extern vbe_mode_info_structure* VbeModeInfo;
void initVBEDevice(multiboot_info_t* mbt);
void blueDiagnalLineTest(multiboot_info_t* mbt);
// Primitive drawing functions
void putPixel( int x, int y , uint32_t colour);
void drawLine(int x1, int y1, int x2, int y2, uint32_t colour );
void drawRect ( int x, int y, int width, int height, uint32_t colour );

View File

@ -48,10 +48,15 @@ void initGDT(){
gdtDescriptor.limit = ((sizeof(SegmentDescriptor ) * 5 ) - 1);
gdtDescriptor.base = (unsigned int) &GlobalDescriptorTable;
printf("Hello GDT!\n");
LoadGlobalDescriptorTable();
// while (true)
// asm volatile("hlt");
}

View File

@ -12,10 +12,9 @@ unsigned short inw_p(unsigned short ){
// TODO: implement me!
return 0;
}
uint32_t inl( int port ){
unsigned int data;
asm volatile ("inl %w1, %0": "=a" (data): "d" (port));
return data;
unsigned int inl(unsigned short ){
// TODO: implement me!
return 0;
}
unsigned int inl_p(unsigned short ){
// TODO: implement me!
@ -32,12 +31,9 @@ void outw(unsigned short , unsigned short ){
void outw_p(unsigned short , unsigned short ){
}
void outl(unsigned int , unsigned short ){
void outl( int port , uint32_t data ){
asm volatile ("outl %0, %1" :: "a" (data), "dn"(port));
}
void outl_p(unsigned int , unsigned short ){
}

View File

@ -12,17 +12,21 @@ static inline uint8_t inb(uint16_t port)
unsigned char inb_p(unsigned short port);
unsigned short inw(unsigned short port);
unsigned short inw_p(unsigned short port);
uint32_t inl( int port );
unsigned int inl(unsigned short port);
unsigned int inl_p(unsigned short port);
static inline void outb(uint16_t port, uint8_t val)
{
asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
/* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint).
* Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint).
* The outb %al, %dx encoding is the only option for all other cases.
* %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */
}
void outb_p(unsigned char value, unsigned short port);
void outw(unsigned short value, unsigned short port);
void outw_p(unsigned short value, unsigned short port);
void outl( int port , uint32_t data );
void outl(unsigned int value, unsigned short port);
void outl_p(unsigned int value, unsigned short port);
void insb(unsigned short port, void *addr,

View File

@ -1,17 +1,18 @@
#include "kernel.h"
#define GB4 524288
#define GB2 262144
int memcmp( const void* ptr1, const void* ptr2, size_t num);
extern "C" void kernel_main (void);
extern "C" void putPixel(int pos_x, int pos_y, unsigned char VGA_COLOR , unsigned char addr , unsigned char pixelWidth, unsigned pitch );
extern "C" void early_main(unsigned long magic, unsigned long addr){
/** initialize terminal interface */
kterm_init();
// kterm_init();
printf("Magic flag 0x%8x\n", magic);
printf("Magic must be 0x%8x\n", MULTIBOOT_BOOTLOADER_MAGIC);
if (magic != MULTIBOOT_BOOTLOADER_MAGIC){
printf("Invalid magic number: 0x%x\n", magic);
printf("Invalid magic number: 0x%8x\n", magic);
return;
}
@ -34,25 +35,8 @@ extern "C" void kernel_main (void);
initGDT();
kernel_main();
}
int memcmp( const void* ptr1, const void* ptr2, size_t num)
{
const unsigned char * cs = (const unsigned char*) ptr1;
const unsigned char * ct = (const unsigned char*) ptr2;
for (int i = 0 ; i < num ; i++, cs++, ct++ ){
if( *cs < *ct){
return -1;
} else if( *cs > *ct){
return 1;
}
}
return 0;
}
@ -60,55 +44,12 @@ extern "C" void kernel_main (void);
printf("call to init serial\n");
init_serial();
print_serial("Serial port initialized!");
// Enumerate the PCI bus
int devicesFound = 0;
// loop through all possible busses, devices and their functions;
for( int bus = 0 ; bus < 256 ; bus++)
{
for(int device = 0; device < 32 ; device ++)
{
for ( int function = 0; function < 8; function++)
{
uint64_t DeviceIdentify = ConfigReadWord(bus, device, function,0x0);
uint32_t VendorID = DeviceIdentify & 0xFFFF;
uint32_t DeviceID = DeviceIdentify >> 16;
if( DeviceID != 0xFFFF){
printf("bus: %d, device: %d, function %d \n");
printf("Device found!\n");
printf("DeviceID: 0x%x, VendorID: 0x%x\n", DeviceID, VendorID);
uint32_t classcodes = ConfigReadWord(bus, device, function, 0x8);
uint32_t classData = classcodes >> 16; // We only care for the last 2 bytes!
uint32_t deviceClass = classData >> 8;
uint32_t subclass = classData & 0xFF;
printf(" class: %d, subClass: %d\n\n", deviceClass, subclass);
devicesFound++;
}
}
}
}
printf("Found %d devices!", devicesFound);
while (true){
//Read time indefinetely
//read_rtc();
//printf( "UTC time: %02d-%02d-%02d %02d:%02d:%02d [ Formatted as YY-MM-DD h:mm:ss]\r" ,year, month, day, hour, minute, second);
read_rtc();
printf( "UTC time: %02d-%02d-%02d %02d:%02d:%02d [ Formatted as YY-MM-DD h:mm:ss]\r" ,year, month, day, hour, minute, second);
delay(1000);
}

View File

@ -16,8 +16,6 @@ extern "C"{
#include "time.h"
#include "cpu.h"
#include "serial.h"
#include "pci.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
#define PANIC(message) { return; }

View File

@ -1,28 +1,108 @@
#include "pci.h"
#include "tty/kterm.h"
#define PCI_BUS_ADDR_SHIFT 16
#define PCI_DEVICE_ADDR_SHIFT 11
#define PCI_FUNCTION_ADDR_SHIFT 8
#define PCI_ENABLE_ADDR_SHIFT 31
uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset){
uint16_t ConfigReadWord (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset){
uint32_t address;
uint32_t lbus = (uint32_t) bus;
uint32_t lslot = (uint32_t) slot;
uint32_t lfunc = (uint32_t) func;
uint16_t tmp = 0;
address = (uint32_t) (
((uint32_t) 1 << PCI_ENABLE_ADDR_SHIFT) |
((uint32_t)bus << PCI_BUS_ADDR_SHIFT) |
((uint32_t)device << PCI_DEVICE_ADDR_SHIFT) |
((uint32_t)func << PCI_FUNCTION_ADDR_SHIFT) |
offset );
// printf("PCI address read 0x%x", address);
/* Create configuration address as per Figure 1 */
address = (uint32_t) ((lbus << 16) | (lslot << 11) | (lfunc << 8) | (offset & 0xFC) |((uint32_t) 0x80000000) );
/*write out the address */
outl(CONFIG_ADDRESS, address);
return inl(CONFIG_DATA);
/* read in the data */
/* (offset & 2 ) * 8 ) = o will choosse the first word of the 32 bits register*/
tmp = (uint16_t)((inl(CONFIG_DATA)) >> ((offset & 2) * 8) & 0xFFFF);
return (tmp);
}
uint16_t CheckVendor (uint8_t bus, uint8_t slot) {
uint16_t vendor, device;
/*
Try and read the first configuration register. Since there ar no
vendors that == 0xFFFF, it must be a non-existent device.
*/
if((vendor = ConfigReadWord(bus, slot, 0,0)) != 0xFFFF) {
device = ConfigReadWord(bus, slot, 0,2);
// Possible read more config values ...
} return (vendor);
}
void checkDevice (uint8_t bus, uint8_t device ) {
uint8_t function = 0;
uint16_t vendorID = CheckVendor(bus, device);
if (vendorID == 0xFFFF) {
return;
}
checkFunction (bus, device, function );
headerType = getHeaderType(bus, device, function );
if( (headerType & 0x80) != 0) {
/* It is a multi-function device, so check remaining functions */
for (function = 1; function < 8; function++){
if (CheckVendor(bus, device)!= 0xFFFF){
checkFunction(bus, device, function );
}
}
}
}
void checkFunction (uint8_t bus, uint8_t device, uint8_t function ){
uint8_t baseClass;
uint8_t subClass;
uint8_t secondaryBus;
baseClass = getBaseClass(bus, device, function);
subClass = getSubClass (bus, device, function );
if ( (baseClass == 0x06) && (subClass == 0x04)){
secondaryBus = getSecondaryBus(bus,device, function);
checkBus(secondaryBus);
}
}
// Brute-force scan
void checkAllBuses (){
uint16_t bus;
uint8_t device;
for(bus = 0; bus < 256; bus++){
for(device = 0; device < 32; device++){
checkDevice(bus,device);
}
}
}
// Recursive scan
void checkBus (uint8_t bus){
uint8_t device;
for(device = 0; device < 32; device ++){
checkDevice(bus,device);
}
}
void checkAllBuses(){
uint8_t function;
uint8_t bus;
headerType = getHeaderType(0,0,0);
if ( (headerType & 0x80) == 0 ){
/* Single PCI host controller */
checkBus(0);
} else{
/* Multiple PCI host controllers */
for (function = 0; function < 8; function++){
if( CheckVendor(0,0) != 0xFFFF) {
break;
}
bus = function;
checkBus(bus);
}
}
}

View File

@ -5,13 +5,54 @@
#define CONFIG_ADDRESS 0xCF8 // Configuration adress that is to be accessed
#define CONFIG_DATA 0xCFC // Will do the actual configuration operation
uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset);
/*
CONFIG_ADDRESS
32 bit register
bit 31 Enable bit (Should CONFIG_DATA be translatedc to configuration cycles)
bit 30 - 24 Reserved
bit 23 - 16 Bus Number (Choose a specific PCI BUS)
bit 15 - 11 Device Number (Selects specific device one the pci bus)
bit 10 - 8 Function Number (Selects a specific function in a device)
bit 7 - 0 Register Offset (Offset in the configuration space of 256 Bytes ) NOTE: lowest two bits will always be zero
*/
inline uint16_t getVendorID(uint8_t bus, uint8_t device, uint8_t function ){
return ConfigReadWord ( bus , device, function, 0);
}
/*
PCI Device structure
inline uint16_t getDeviceID(uint8_t bus, uint8_t device, uint8_t function ){
return ConfigReadWord(bus, device, function , 16);
}
Register offset bits 31-24 bits 23-16 bits 15-8 bits 7-0
00 00 Device ID <---- Vendor ID <-------
01 04 Status <---- Command <-------
02 08 Class code Sub class Prog IF Revision ID
03 0C BIST Header Type Ltncy Timer Cache line Size
04 10 Base address #0 (BAR0)
05 14 Base address #1 (BAR1)
06 18 Base address #2 (BAR2)
07 1C Base address #3 (BAR3)
08 20 Base address #4 (BAR4)
09 24 Base address #5 (BAR5)
0A 28 Cardbus CIS Pointer
0B 2C Subsystem ID <------ Subsystem Vendor ID <-------
0C 30 Expansion ROM base address
0D 34 Reserved <------- Capabilities Pointer <------
0E 38 Reserved <------- <-------- <--------
0F 3C Max ltncy Min Grant Interrupt PIN Interrupt Line
*/
/*
The idea for now is to support the minimal things necessary to find ATA supported drives
*/
// Lets write some boiler plate configuration code
uint16_t ConfigReadWord (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
uint16_t CheckVendor (uint8_t bus, uint8_t slot);
void checkDevice (uint8_t bus, uint8_t device );

View File

@ -1,9 +1,10 @@
#pragma once
#include "tty/kterm.h"
#include "io.h"
#define PORT 0x3f8
static int init_serial() {
inline static int init_serial() {
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
@ -25,33 +26,95 @@ static int init_serial() {
return 0;
}
int is_transmit_empty() {
inline int is_transmit_empty() {
return inb(PORT + 5) & 0x20;
}
void write_serial(char a) {
inline void write_serial(char a) {
while (is_transmit_empty() == 0);
outb(PORT,a);
}
int serial_received() {
inline int serial_received() {
return inb(PORT + 5) & 1;
}
char read_serial() {
inline char read_serial() {
while (serial_received() == 0);
return inb(PORT);
}
void print_serial(const char* string ){
inline void print_serial(const char* string ){
for(size_t i = 0; i < strlen(string); i ++){
write_serial(string[i]);
}
}
void test_serial(){
inline void printf_serial ( const char *format, ...) {
char **arg = (char **)&format;
int c;
char buf[20];
arg++;
while ((c = *format++) != 0){
if( c != '%')
write_serial(c);
else{
char *p, *p2;
int pad0 = 0, pad = 0;
c = *format++;
if(c =='0'){
pad0 = 1;
c = *format++;
}
if ( c >= '0' && c <= '9'){
pad = c - '0';
c = *format++;
}
switch (c)
{
case 'd':
case 'u':
case 'x':
itoa(buf, c, *((int *) arg++));
p = buf;
goto string;
break;
case 's':
p = *arg++;
if(!p)
p = "(null)";
string:
for (p2 = p; *p2; p2++);
for (; p2 < p + pad; p2++)
write_serial(pad0 ? '0': ' ');
while (*p)
write_serial(*p++);
break;
default:
write_serial(*((int *)arg++));
break;
}
}
}
}
inline void test_serial(){
/** Serial test **/
kterm_writestring("Writing to COM1 serial port:");
init_serial();

View File

@ -137,7 +137,7 @@ void kterm_writestring(const char* data ){
}
static void itoa (char *buf, int base, int d) {
void itoa (char *buf, int base, int d) {
char *p = buf;
char *p1, *p2;
unsigned long ud = d;
@ -174,7 +174,7 @@ static void itoa (char *buf, int base, int d) {
}
void printf ( const char *format, ...) {
return;
char **arg = (char **)&format;
int c;
char buf[20];

View File

@ -33,10 +33,9 @@ uint16_t get_cursor_position();
int get_cursor_x (uint16_t cursor_pos);
int get_cursor_y (uint16_t cursor_pos);
extern "C" void itoa (char *buf, int base, int d);
void printf ( const char *format, ...);
//static void itoa (char *buf, int base, int d);
#define KernelTag "[Kernel]: "
#define AS_KERNEL() ( kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\