feat: reformatted code
This commit is contained in:
@@ -59,7 +59,8 @@ int init_timer(u32 freq) {
|
|||||||
u32 divisor = 1193180 / freq;
|
u32 divisor = 1193180 / freq;
|
||||||
u8 low = (u8) (divisor & 0xFF);
|
u8 low = (u8) (divisor & 0xFF);
|
||||||
u8 high = (u8) ((divisor >> 8) & 0xFF);
|
u8 high = (u8) ((divisor >> 8) & 0xFF);
|
||||||
port_byte_out(PORT_PIT_COMMAND, PIT_MODE_BIN | PIT_MODE_HARDWARE_SQUARE_WAVE_GENERATOR | PIT_ACCESS_MODE_LH | PIT_CHANNEL_0);
|
port_byte_out(PORT_PIT_COMMAND,
|
||||||
|
PIT_MODE_BIN | PIT_MODE_HARDWARE_SQUARE_WAVE_GENERATOR | PIT_ACCESS_MODE_LH | PIT_CHANNEL_0);
|
||||||
port_byte_out(PORT_PIT_DATA_0, low);
|
port_byte_out(PORT_PIT_DATA_0, low);
|
||||||
port_byte_out(PORT_PIT_DATA_0, high);
|
port_byte_out(PORT_PIT_DATA_0, high);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -8,7 +8,9 @@
|
|||||||
#include <types.h>
|
#include <types.h>
|
||||||
|
|
||||||
int init_timer(u32 freq);
|
int init_timer(u32 freq);
|
||||||
|
|
||||||
void print_current_tick();
|
void print_current_tick();
|
||||||
|
|
||||||
void sleep(u32 milliseconds);
|
void sleep(u32 milliseconds);
|
||||||
|
|
||||||
#endif //MY_KERNEL_TIMER_H
|
#endif //MY_KERNEL_TIMER_H
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ typedef struct pci_driver pci_driver;
|
|||||||
typedef struct pci_device pci_device;
|
typedef struct pci_device pci_device;
|
||||||
|
|
||||||
typedef u8 (*pci_driver_validate)(const pci_device *);
|
typedef u8 (*pci_driver_validate)(const pci_device *);
|
||||||
|
|
||||||
typedef u8 (*pci_driver_initialize)(pci_device *);
|
typedef u8 (*pci_driver_initialize)(pci_device *);
|
||||||
|
|
||||||
typedef struct pci_driver {
|
typedef struct pci_driver {
|
||||||
@@ -152,4 +153,5 @@ void pci_config_write_word(u8 bus, u8 slot, u8 func, u8 offset, u16 value);
|
|||||||
void pci_config_write_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 value);
|
void pci_config_write_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 value);
|
||||||
|
|
||||||
void pci_init_bar(pci_device *device, u8 bar_index);
|
void pci_init_bar(pci_device *device, u8 bar_index);
|
||||||
|
|
||||||
#endif //NEW_KERNEL_PCI_H
|
#endif //NEW_KERNEL_PCI_H
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ unsigned short port_word_in(unsigned short port) {
|
|||||||
void port_word_out(unsigned short port, unsigned short data) {
|
void port_word_out(unsigned short port, unsigned short data) {
|
||||||
__asm__("out %%ax, %%dx" : : "a" (data), "d" (port));
|
__asm__("out %%ax, %%dx" : : "a" (data), "d" (port));
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int port_double_word_in(unsigned int port) {
|
unsigned int port_double_word_in(unsigned int port) {
|
||||||
unsigned int result;
|
unsigned int result;
|
||||||
__asm__("in %%dx, %%eax" : "=a" (result) : "d" (port));
|
__asm__("in %%dx, %%eax" : "=a" (result) : "d" (port));
|
||||||
|
|||||||
@@ -61,4 +61,5 @@ void port_word_out_repeat(unsigned short port, unsigned short *data, int buffer_
|
|||||||
void port_word_in_repeat(unsigned short port, unsigned short *data, int buffer_size);
|
void port_word_in_repeat(unsigned short port, unsigned short *data, int buffer_size);
|
||||||
|
|
||||||
void port_double_word_in_repeat(unsigned short port, unsigned int *data, int buffer_size);
|
void port_double_word_in_repeat(unsigned short port, unsigned int *data, int buffer_size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 se
|
|||||||
return info->device->access(info->device, direction, actual_lba, sectors, target);
|
return info->device->access(info->device, direction, actual_lba, sectors, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
u8 mbr_check_device(const block_device *device, u8* first_sector) {
|
u8 mbr_check_device(const block_device *device, u8 *first_sector) {
|
||||||
mbr_table *table = (mbr_table *) &first_sector[512 - sizeof(mbr_table)];
|
mbr_table *table = (mbr_table *) &first_sector[512 - sizeof(mbr_table)];
|
||||||
if (table->signature[0] != 0x55 && table->signature[1] != 0xAA) { // AA 55 but in little endian
|
if (table->signature[0] != 0x55 && table->signature[1] != 0xAA) { // AA 55 but in little endian
|
||||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||||
@@ -90,10 +90,10 @@ u8 mbr_check_device(const block_device *device, u8* first_sector) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
block_dev_driver mbr_driver = {
|
block_dev_driver mbr_driver = {
|
||||||
.name = "mbr",
|
.name = "mbr",
|
||||||
.flags.root_only = 1,
|
.flags.root_only = 1,
|
||||||
.check_device = mbr_check_device,
|
.check_device = mbr_check_device,
|
||||||
.free_device = NULL, // todo
|
.free_device = NULL, // todo
|
||||||
};
|
};
|
||||||
|
|
||||||
void mbr_register_block_driver() {
|
void mbr_register_block_driver() {
|
||||||
@@ -101,9 +101,9 @@ void mbr_register_block_driver() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void mbr_read_from_ide(u8 ide_drive) {
|
void mbr_read_from_ide(u8 ide_drive) {
|
||||||
char* mbr_data = malloc(0x200);
|
char *mbr_data = malloc(0x200);
|
||||||
ide_access(0, ide_drive, 0, 1, mbr_data);
|
ide_access(0, ide_drive, 0, 1, mbr_data);
|
||||||
mbr_partition_table_entry* entry = (mbr_partition_table_entry *) (mbr_data + 0x1BE);
|
mbr_partition_table_entry *entry = (mbr_partition_table_entry *) (mbr_data + 0x1BE);
|
||||||
printf("Start at %d, count: %d\n", entry->start_lba, entry->num_lbas);
|
printf("Start at %d, count: %d\n", entry->start_lba, entry->num_lbas);
|
||||||
free(mbr_data);
|
free(mbr_data);
|
||||||
}
|
}
|
||||||
@@ -22,8 +22,8 @@
|
|||||||
#define CMDLINE_MAX_LENGTH 256
|
#define CMDLINE_MAX_LENGTH 256
|
||||||
|
|
||||||
const int version_major = 0,
|
const int version_major = 0,
|
||||||
version_minor = 0,
|
version_minor = 0,
|
||||||
version_patch = 1;
|
version_patch = 1;
|
||||||
|
|
||||||
char bootloader_name[BOOTLOADER_NAME_MAX_LENGTH];
|
char bootloader_name[BOOTLOADER_NAME_MAX_LENGTH];
|
||||||
char cmdline[CMDLINE_MAX_LENGTH];
|
char cmdline[CMDLINE_MAX_LENGTH];
|
||||||
@@ -36,16 +36,19 @@ typedef struct {
|
|||||||
} cmd_handler;
|
} cmd_handler;
|
||||||
|
|
||||||
void print(const char *arg);
|
void print(const char *arg);
|
||||||
|
|
||||||
void ide(const char *arg);
|
void ide(const char *arg);
|
||||||
|
|
||||||
void echo(const char *arg);
|
void echo(const char *arg);
|
||||||
|
|
||||||
void help(const char *arg);
|
void help(const char *arg);
|
||||||
|
|
||||||
cmd_handler cmd_handlers[] = {
|
cmd_handler cmd_handlers[] = {
|
||||||
{"help", help},
|
{"help", help},
|
||||||
{"echo", echo},
|
{"echo", echo},
|
||||||
{"print", print},
|
{"print", print},
|
||||||
{"ide", ide},
|
{"ide", ide},
|
||||||
{NULL, NULL},
|
{NULL, NULL},
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_bootinfo() {
|
void print_bootinfo() {
|
||||||
@@ -55,7 +58,7 @@ void print_bootinfo() {
|
|||||||
(cmdline[0] == 0 ? "NULL" : cmdline));
|
(cmdline[0] == 0 ? "NULL" : cmdline));
|
||||||
}
|
}
|
||||||
|
|
||||||
void help(const char* arg) {
|
void help(const char *arg) {
|
||||||
kprint("Available commands:\n");
|
kprint("Available commands:\n");
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -90,7 +93,7 @@ void print(const char *arg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ide(const char* arg) {
|
void ide(const char *arg) {
|
||||||
if (strcmp(arg, "block_devices") == 0) {
|
if (strcmp(arg, "block_devices") == 0) {
|
||||||
block_dev_print_info();
|
block_dev_print_info();
|
||||||
} else if (strcmp(arg, "ide_devices") == 0) {
|
} else if (strcmp(arg, "ide_devices") == 0) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
/*
|
/*
|
||||||
* Integer to string
|
* Integer to string
|
||||||
*/
|
*/
|
||||||
void print_int(u32 value, u32 width, char* buf, u32 *ptr, u8 base) {
|
void print_int(u32 value, u32 width, char *buf, u32 *ptr, u8 base) {
|
||||||
// u32 in binary is 32 digits, so never longer than 33 digits
|
// u32 in binary is 32 digits, so never longer than 33 digits
|
||||||
char msg[33];
|
char msg[33];
|
||||||
itoa(value, msg, base);
|
itoa(value, msg, base);
|
||||||
@@ -53,13 +53,13 @@ u32 vasprintf(char *buf, const char *fmt, va_list args) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'c':
|
case 'c':
|
||||||
buf[ptr++] = (char)va_arg(args, int);
|
buf[ptr++] = (char) va_arg(args, int);
|
||||||
break;
|
break;
|
||||||
case 'x':
|
case 'x':
|
||||||
print_int((u32)va_arg(args, u32), arg_width, buf, &ptr, 16);
|
print_int((u32) va_arg(args, u32), arg_width, buf, &ptr, 16);
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
print_int((u32)va_arg(args, u32), arg_width, buf, &ptr, 10);
|
print_int((u32) va_arg(args, u32), arg_width, buf, &ptr, 10);
|
||||||
break;
|
break;
|
||||||
case '%':
|
case '%':
|
||||||
buf[ptr++] = '%';
|
buf[ptr++] = '%';
|
||||||
@@ -85,7 +85,7 @@ int printf(const char *fmt, ...) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sprintf(char* target, const char*fmt, ...) {
|
int sprintf(char *target, const char *fmt, ...) {
|
||||||
int result;
|
int result;
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
|
|||||||
@@ -32,12 +32,13 @@ int abs(int val) {
|
|||||||
// next stolen form https://www.techiedelight.com/implement-itoa-function-in-c/
|
// next stolen form https://www.techiedelight.com/implement-itoa-function-in-c/
|
||||||
// inline function to swap two numbers
|
// inline function to swap two numbers
|
||||||
void swap(char *x, char *y) {
|
void swap(char *x, char *y) {
|
||||||
char t = *x; *x = *y; *y = t;
|
char t = *x;
|
||||||
|
*x = *y;
|
||||||
|
*y = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
// function to reverse buffer[i..j]
|
// function to reverse buffer[i..j]
|
||||||
char* reverse(char *buffer, int i, int j)
|
char *reverse(char *buffer, int i, int j) {
|
||||||
{
|
|
||||||
while (i < j)
|
while (i < j)
|
||||||
swap(&buffer[i++], &buffer[j--]);
|
swap(&buffer[i++], &buffer[j--]);
|
||||||
|
|
||||||
@@ -45,8 +46,7 @@ char* reverse(char *buffer, int i, int j)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterative function to implement itoa() function in C
|
// Iterative function to implement itoa() function in C
|
||||||
char* itoa(int value, char* buffer, int base)
|
char *itoa(int value, char *buffer, int base) {
|
||||||
{
|
|
||||||
// invalid input
|
// invalid input
|
||||||
if (base < 2 || base > 32)
|
if (base < 2 || base > 32)
|
||||||
return buffer;
|
return buffer;
|
||||||
@@ -55,8 +55,7 @@ char* itoa(int value, char* buffer, int base)
|
|||||||
int n = abs(value);
|
int n = abs(value);
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (n)
|
while (n) {
|
||||||
{
|
|
||||||
int r = n % base;
|
int r = n % base;
|
||||||
|
|
||||||
if (r >= 10)
|
if (r >= 10)
|
||||||
|
|||||||
@@ -11,12 +11,12 @@
|
|||||||
|
|
||||||
#define RESULT_SIZE 256
|
#define RESULT_SIZE 256
|
||||||
|
|
||||||
const char* default_msg = "> ";
|
const char *default_msg = "> ";
|
||||||
|
|
||||||
char* readline(const char *prompt) {
|
char *readline(const char *prompt) {
|
||||||
kprint(prompt == NULL ? default_msg : prompt);
|
kprint(prompt == NULL ? default_msg : prompt);
|
||||||
|
|
||||||
char* result = malloc(RESULT_SIZE);
|
char *result = malloc(RESULT_SIZE);
|
||||||
memset(result, 0, RESULT_SIZE);
|
memset(result, 0, RESULT_SIZE);
|
||||||
for (int i = 0; i < RESULT_SIZE - 1; ++i) {
|
for (int i = 0; i < RESULT_SIZE - 1; ++i) {
|
||||||
result[i] = getc();
|
result[i] = getc();
|
||||||
|
|||||||
@@ -5,6 +5,6 @@
|
|||||||
#ifndef NEW_KERNEL_READLINE_H
|
#ifndef NEW_KERNEL_READLINE_H
|
||||||
#define NEW_KERNEL_READLINE_H
|
#define NEW_KERNEL_READLINE_H
|
||||||
|
|
||||||
char* readline(const char *prompt);
|
char *readline(const char *prompt);
|
||||||
|
|
||||||
#endif //NEW_KERNEL_READLINE_H
|
#endif //NEW_KERNEL_READLINE_H
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ void ring_buffer_put(void *buffer, void *item) {
|
|||||||
ring_buffer->write_pos++;
|
ring_buffer->write_pos++;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool ring_buffer_get(void *buffer, void* target) {
|
bool ring_buffer_get(void *buffer, void *target) {
|
||||||
ring_buffer_t *ring_buffer = (ring_buffer_t *) buffer;
|
ring_buffer_t *ring_buffer = (ring_buffer_t *) buffer;
|
||||||
if (ring_buffer->read_pos == ring_buffer->write_pos) {
|
if (ring_buffer->read_pos == ring_buffer->write_pos) {
|
||||||
// nothing to read
|
// nothing to read
|
||||||
|
|||||||
@@ -14,6 +14,6 @@ void free_buffer(void *buffer);
|
|||||||
|
|
||||||
void ring_buffer_put(void *buffer, void *item);
|
void ring_buffer_put(void *buffer, void *item);
|
||||||
|
|
||||||
bool ring_buffer_get(void *buffer, void* target);
|
bool ring_buffer_get(void *buffer, void *target);
|
||||||
|
|
||||||
#endif //NEW_KERNEL_RINGQUEUE_H
|
#endif //NEW_KERNEL_RINGQUEUE_H
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
#define NEW_KERNEL_VA_LIST_H
|
#define NEW_KERNEL_VA_LIST_H
|
||||||
|
|
||||||
typedef __builtin_va_list va_list;
|
typedef __builtin_va_list va_list;
|
||||||
#define va_start(ap,last) __builtin_va_start(ap, last)
|
#define va_start(ap, last) __builtin_va_start(ap, last)
|
||||||
#define va_end(ap) __builtin_va_end(ap)
|
#define va_end(ap) __builtin_va_end(ap)
|
||||||
#define va_arg(ap,type) __builtin_va_arg(ap,type)
|
#define va_arg(ap, type) __builtin_va_arg(ap,type)
|
||||||
#define va_copy(dest, src) __builtin_va_copy(dest,src)
|
#define va_copy(dest, src) __builtin_va_copy(dest,src)
|
||||||
|
|
||||||
#endif //NEW_KERNEL_VA_LIST_H
|
#endif //NEW_KERNEL_VA_LIST_H
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ void k_wait_for_interrupt() {
|
|||||||
__asm__ __volatile__("hlt;");
|
__asm__ __volatile__("hlt;");
|
||||||
}
|
}
|
||||||
|
|
||||||
void k_panics(const char* msg) {
|
void k_panics(const char *msg) {
|
||||||
kprint(msg);
|
kprint(msg);
|
||||||
kprint("PANIC!");
|
kprint("PANIC!");
|
||||||
__asm__ __volatile__("cli;"
|
__asm__ __volatile__("cli;"
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
void k_wait_for_interrupt();
|
void k_wait_for_interrupt();
|
||||||
|
|
||||||
void k_panics(const char* msg);
|
void k_panics(const char *msg);
|
||||||
|
|
||||||
void k_panic();
|
void k_panic();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user