Verified Commit 59d98c79 authored by Allie Sargente's avatar Allie Sargente 😑

Refactoring + Makefile improvements

parent 2c128fb4
# Build Tree
build*/
# Scorch OS 2008-2010 Scorch OS Developers
# Makefile for Compiling the Base Kernel
# Requirements are nasm, and a i586 cross compiler
# Directions to download is at http://wiki.osdev.org/GCC_Cross-Compiler_on_Debian_Linux
C_COMPILER=i686-elf-gcc
ASM_COMPILER=nasm
LINKER=i686-elf-ld
# These aren't "real" targets
.PHONY: all clean link
# This can be overridden by a user
BUILD_DIR ?= build
CWARNINGS := -Wall -Wextra
CFLAGS := -I./src/include -std=gnu99 -fleading-underscore -O -finline-functions-called-once
# In Case we ever wanna make for another platform
TARGET ?= x86
# We need lowercase for the name, just be sure with a `tr`
TARGETL != echo $(TARGET) | tr '[:upper:]' '[:lower:]'
BUILD := $(BUILD_DIR)-$(TARGETL)
CFLAGS=-I./src/include -std=gnu99 -fleading-underscore -O -ffreestanding -finline-functions-called-once -nostdlib -Werror=implicit-function-declaration
LDFLAGS=-T./src/link.ld
ASFLAGS=-felf -I$(CURDIR)/src/
# Include the sub-makefile
-include target-$(TARGETL).mk
C_SOURCES = $(shell ls src/*.c)
ASM_SOURCES = $(shell ls src/*.s)
OBJECTS = $(ASM_SOURCES:.s=.o) $(C_SOURCES:.c=.o)
# Rewrite paths into the build directory
C_SOURCES != find src/core -type f -name '*.c'
C_SOURCES += $(TARGET_CSOURCES)
C_OBJECTS := $(patsubst %.c,$(BUILD)/%.c.o,$(C_SOURCES))
ASM_OBJECTS := $(patsubst %.s,$(BUILD)/%.s.o,$(ASM_SOURCES))
OBJECTS := $(C_OBJECTS) $(ASM_OBJECTS)
# All of the directories we need for the build
SRC_DIR != find src/ -type d | tr '\n' ' '
all: $(OBJECTS) link
clean:
-rm ./src/*.o
link:
$(LINKER) $(LDFLAGS) -o bin/kernel.x $(OBJECTS)
.c.o:
$(C_COMPILER) $(CFLAGS) -c $< -o [email protected]
.s.o:
$(ASM_COMPILER) $(ASFLAGS) $< -o [email protected]
@printf "\033[1mCLEAN\033[0m \n"
@find $(BUILD) -type f -name '*.o' -exec rm {} +
# $(CC) is a perfectly good linker, no need to use ld
link: $(OBJECTS)
@printf "\033[1mLINK\033[0m [email protected]\n"
@$(CC) $(TARGET_CFLAGS) $(CFLAGS) $(CWARNINGS) $(LDFLAGS) -o./bin/$(BIN) $(OBJECTS)
# Set up build directories
$(BUILD)/%.c.o: %.c Makefile | setup_build_dir
@printf "\033[1mCC\033[0m [email protected]\n"
@$(CC) $(TARGET_CFLAGS) $(CWARNINGS) $(CFLAGS) -c $< -o [email protected]
setup_build_dir:
@mkdir -p $(BUILD)
@cd $(BUILD) && mkdir -p `echo $(SRC_DIR)`
No preview for this file type
File deleted
//
// ahci.c
//
//
//
// Created by Mike Evans on 5/25/15.
//
......@@ -9,8 +9,8 @@
#include <system.h>
#include <string.h>
#include "ahci.h"
#include "vmm.h"
#include <ahci.h>
#include <vmm.h>
......@@ -53,15 +53,15 @@ uint32_t portbase[32];
static int check_type(HBA_PORT *port)
{
DWORD ssts = port->ssts;
BYTE ipm = (ssts >> 8) & 0x0F;
BYTE det = ssts & 0x0F;
if (det != HBA_PORT_DET_PRESENT) // Check drive status
return AHCI_DEV_NULL;
if (ipm != HBA_PORT_IPM_ACTIVE)
return AHCI_DEV_NULL;
switch (port->sig)
{
case SATA_SIG_ATAPI:
......@@ -86,8 +86,8 @@ int find_cmdslot(HBA_PORT *m_port)
DWORD slots = (m_port->sact | m_port->ci);
int num_of_slots= (mabar->cap & 0x0f00)>>8 ; // Bit 8-12
//puts("NUMSLOTS: "); putd(num_of_slots); puts(" ");
for (int i=0; i<num_of_slots; i++)
{
if ((slots&1) == 0)
......@@ -102,49 +102,49 @@ int find_cmdslot(HBA_PORT *m_port)
// Start command engine
void start_cmd(HBA_PORT *port)
{
// Wait until CR (bit15) is cleared
while (port->cmd & HBA_PxCMD_CR);
// Set FRE (bit4) and ST (bit0)
port->cmd |= HBA_PxCMD_FRE;
port->cmd |= HBA_PxCMD_ST;
}
// Stop command engine
void stop_cmd(HBA_PORT *port)
{
// Clear ST (bit0)
port->cmd &= ~HBA_PxCMD_ST;
// Wait until FR (bit14), CR (bit15) are cleared
while(1)
{
if (port->cmd & HBA_PxCMD_FR)
{
continue;
}
if (port->cmd & HBA_PxCMD_CR)
{
continue;
}
break;
}
// Clear FRE (bit4)
port->cmd &= ~HBA_PxCMD_FRE;
}
......@@ -162,13 +162,13 @@ void stop_cmd(HBA_PORT *port)
void port_rebase(HBA_PORT *port, int portno)
{
mabar->ghc=(DWORD)(1<<31);
mabar->ghc=(DWORD)(1<<0);
mabar->ghc=(DWORD)(1<<31);
mabar->ghc=(DWORD)(1<<1);
if(portbase[portno]==0)
{
portbase[portno]=AHCI_BASE + (AHCI_PORT_SIZE*portno);
......@@ -179,39 +179,39 @@ void port_rebase(HBA_PORT *port, int portno)
// puts(" BASE: ");
// putlx(portbase[portno]);
// portbase[portno]=0;
//
//
// return;
// }
}
//puts("ALLOCED REBASE\n");
stop_cmd(port); // Stop command engine
// Command list offset: 1K*portno
// Command list entry size = 32
// Command list entry maxim count = 32
// Command list maxim size = 32*32 = 1K per port
port->clb = portbase[portno] & 0xFFFFFFFF;
port->clbu = 0;
memset((void*)(port->clb), 0, 1024);
//puts("CLB SET\n");
// FIS offset: 32K+256*portno
// FIS entry size = 256 bytes per port
port->fb = portbase[portno] + (1<<10) & 0xFFFFFFFF;
port->fbu = 0;
memset((void*)(port->fb), 0, 256);
//puts("FB SET\n");
// Command table offset: 40K + 8K*portno
// Command table size = 256*32 = 8K per port
HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER*)(port->clb);
......@@ -225,43 +225,43 @@ void port_rebase(HBA_PORT *port, int portno)
memset((void*)cmdheader[i].ctba, 0, 256);
}
//puts("HEADER SET\n");
start_cmd(port); // Start command engine
//puts("LEAVING REBASE");
}
bool identify(HBA_PORT *port)
{
//puts("IDENTIFY\n");
port->is = (DWORD)-1; // Clear pending interrupt bits
int spin = 0; // Spin lock timeout counter
int slot = find_cmdslot(port);
memset(AHCI_BUFFER,0,2048);
//puts("SLOT: "); putd(slot); puts("\n");
if (slot == -1)
return false;
HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER*)port->clb;
cmdheader += slot;
cmdheader->cfl = sizeof(FIS_REG_H2D)/sizeof(DWORD); // Command FIS size
cmdheader->w = 0; // Read from device
cmdheader->prdtl = 2; // PRDT entries count
HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL*)(cmdheader->ctba);
memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) +
(cmdheader->prdtl-1)*sizeof(HBA_PRDT_ENTRY));
cmdtbl->prdt_entry[0].dba = (DWORD)AHCI_BUFFER;
cmdtbl->prdt_entry[0].dbc = 512-1; // 512 bytes per sector
cmdtbl->prdt_entry[0].i = 1;
FIS_REG_H2D *cmdfis = (FIS_REG_H2D*)(&cmdtbl->cfis);
cmdfis->fis_type= FIS_TYPE_REG_H2D;
cmdfis->pmport=0;
......@@ -269,7 +269,7 @@ bool identify(HBA_PORT *port)
cmdfis->command = 0xEC; // 0xEC
cmdfis->device = 0; // Master device
cmdfis->c = 1;
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000)
{
spin++;
......@@ -279,9 +279,9 @@ bool identify(HBA_PORT *port)
puts("Port is hung\n");
return false;
}
port->ci = 1<<slot; // Issue command
while (1)
{
// In some longer duration reads, it may be helpful to spin on the DPS bit
......@@ -294,23 +294,23 @@ bool identify(HBA_PORT *port)
return false;
}
}
// Check again
if (port->is & HBA_PxIS_TFES)
{
puts("Read disk error\n");
return false;
}
hdparam * hdp = (hdparam *) AHCI_BUFFER;
return true;
}
#define LOBYTE(x) ((unsigned char) ((x) & 0xff))
#define HIBYTE(x) ((unsigned char) ((x) >> 8 & 0xff))
......@@ -324,20 +324,20 @@ bool read(HBA_PORT *port, DWORD startl, DWORD starth, DWORD count, WORD *buf)
int slot = find_cmdslot(port);
if (slot == -1)
return false;
HBA_CMD_HEADER *cmdheader = (HBA_CMD_HEADER*)port->clb;
cmdheader += slot;
cmdheader->cfl = sizeof(FIS_REG_H2D)/sizeof(DWORD); // Command FIS size
cmdheader->w = 0; // Read from device
cmdheader->prdtl = (WORD)((count-1)>>4) + 1; // PRDT entries count
HBA_CMD_TBL *cmdtbl = (HBA_CMD_TBL*)(cmdheader->ctba);
memset(cmdtbl, 0, sizeof(HBA_CMD_TBL) +
(cmdheader->prdtl-1)*sizeof(HBA_PRDT_ENTRY));
// 8K bytes (16 sectors) per PRDT
int i;
for (i=0; i<cmdheader->prdtl-1; i++)
{
cmdtbl->prdt_entry[i].dba = (DWORD)buf;
......@@ -350,26 +350,26 @@ bool read(HBA_PORT *port, DWORD startl, DWORD starth, DWORD count, WORD *buf)
cmdtbl->prdt_entry[i].dba = (DWORD)buf;
cmdtbl->prdt_entry[i].dbc = count<<9; // 512 bytes per sector
cmdtbl->prdt_entry[i].i = 1;
// Setup command
FIS_REG_H2D *cmdfis = (FIS_REG_H2D*)(&cmdtbl->cfis);
cmdfis->fis_type = FIS_TYPE_REG_H2D;
cmdfis->c = 1; // Command
cmdfis->command = ATA_CMD_READ_DMA_EXT;
cmdfis->lba0 = (BYTE)startl;
cmdfis->lba1 = (BYTE)(startl>>8);
cmdfis->lba2 = (BYTE)(startl>>16);
cmdfis->device = 1<<6; // LBA mode
cmdfis->lba3 = (BYTE)(startl>>24);
cmdfis->lba4 = (BYTE)starth;
cmdfis->lba5 = (BYTE)(starth>>8);
cmdfis->countl = LOBYTE(count);
cmdfis->counth = HIBYTE(count);
// The below loop waits until the port is no longer busy before issuing a new command
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000)
{
......@@ -380,9 +380,9 @@ bool read(HBA_PORT *port, DWORD startl, DWORD starth, DWORD count, WORD *buf)
puts("Port is hung\n");
return false;
}
port->ci = 1<<slot; // Issue command
// Wait for completion
while (1)
{
......@@ -396,14 +396,14 @@ bool read(HBA_PORT *port, DWORD startl, DWORD starth, DWORD count, WORD *buf)
return false;
}
}
// Check again
if (port->is & HBA_PxIS_TFES)
{
puts("Read disk error\n");
return false;
}
return true;
}
......@@ -415,35 +415,35 @@ void probe_port(HBA_MEM *abar)
// Search disk in impelemented ports
DWORD pi = abar->pi;
DWORD ver = abar->vs;
int i = 0;
while (i<32)
{
if (pi & 1)
{
int dt = check_type(&abar->ports[i]);
if (dt == AHCI_DEV_SATA)
{
puts("SATA drive found at port ");
putd(i);
puts("\n");
port_rebase(&abar->ports[i],i);
identify(&abar->ports[i]);
//read(&abar->ports[i],0,0,512,(WORD *)AHCI_BUFFER);
}
else if (dt == AHCI_DEV_SATAPI)
{
......@@ -470,7 +470,7 @@ void probe_port(HBA_MEM *abar)
// puts(" ");
}
}
pi >>= 1;
i ++;
}
......@@ -479,22 +479,22 @@ void probe_port(HBA_MEM *abar)
void ahci_init(uint32_t _abar)
{
for(int i=0;i<32;i++)
{
portbase[i]=0;
}
mabar=(HBA_MEM*) _abar;
probe_port(mabar);
}
......@@ -8,7 +8,7 @@
#include <system.h>
#include "dma.h"
#include <dma.h>
//! masks a channel
......
......@@ -9,8 +9,8 @@
#include <system.h>
#include <stdbool.h>
#include <stdint.h>
#include "dma.h"
#include "flpydsk.h"
#include <dma.h>
#include <flpydsk.h>
/*
......@@ -197,7 +197,7 @@ bool dma_initialize_floppy(uint8_t* buffer, unsigned length){
dma_set_address( FDC_DMA_CHANNEL, a.byte[0],a.byte[1]);//Buffer address
dma_set_external_page_register(FDC_DMA_CHANNEL,a.byte[3]);
dma_reset_flipflop( 1 );//Flipflop reset on DMA 1
dma_set_count( FDC_DMA_CHANNEL, c.byte[0],c.byte[1]);//Set count
......@@ -270,12 +270,12 @@ static void flpydsk_wait_irq () {
//! floppy disk irq handler
void i86_flpy_irq(struct regs *r)
{
//! irq fired
_FloppyDiskIRQ = 1;
}
/**
......@@ -390,11 +390,11 @@ void flpydsk_reset () {
//! reset the controller
flpydsk_disable_controller ();
flpydsk_enable_controller ();
flpydsk_wait_irq ();
//! send CHECK_INT/SENSE INTERRUPT command to all drives
//for (int i=0; i<4; i++)
......@@ -426,7 +426,7 @@ void flpydsk_read_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
if(!dma_initialize_floppy ((uint8_t*) DMA_BUFFER, 512 ))
{
puts("DMA INIT FAILED!");
}
//! set the DMA for read transfer
......@@ -446,7 +446,7 @@ void flpydsk_read_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
//! wait for irq
flpydsk_wait_irq ();
unsigned char st0, st1, st2, rcy, rhe, rse, bps;
//! read status info
......@@ -457,16 +457,16 @@ void flpydsk_read_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
rhe=flpydsk_read_data();
rse=flpydsk_read_data();
bps=flpydsk_read_data();
int error = 0;
if(st0 & 0xC0) {
static const char * status[] =
{ 0, "error", "invalid command", "drive not ready" };
puts("floppy_do_sector: status =");
puts(status[st0 >> 6]);
puts("\n");
error = 1;
}
if(st1 & 0x80) {
......@@ -523,7 +523,7 @@ void flpydsk_read_sector_imp (uint8_t head, uint8_t track, uint8_t sector) {
error = 2;
}
//! let FDC know we handled interrupt
flpydsk_check_int (&mst0,&cyl);
......@@ -568,18 +568,18 @@ void flpydsk_lba_to_chs (int lba,int *head,int *track,int *sector) {
void flpydsk_install (int irq) {
//! install irq handler
irq_install_handler(irq,i86_flpy_irq);
//! reset the fdc
flpydsk_reset ();
//! set drive information
flpydsk_drive_data (13, 1, 0xf, true);
}
//! set current working drive
......
//
// pci.c
//
//
//
// Created by Mike Evans on 5/10/15.
//
......@@ -8,11 +8,11 @@
#include <system.h>
#include "vmm.h"