feat: reformatted code

This commit is contained in:
2021-02-10 18:56:47 +01:00
parent 2b295db30a
commit 90ef4522ca
16 changed files with 47 additions and 38 deletions

View File

@@ -59,7 +59,8 @@ int init_timer(u32 freq) {
u32 divisor = 1193180 / freq;
u8 low = (u8) (divisor & 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, high);
return 0;

View File

@@ -8,7 +8,9 @@
#include <types.h>
int init_timer(u32 freq);
void print_current_tick();
void sleep(u32 milliseconds);
#endif //MY_KERNEL_TIMER_H

View File

@@ -61,6 +61,7 @@ typedef struct pci_driver pci_driver;
typedef struct pci_device pci_device;
typedef u8 (*pci_driver_validate)(const pci_device *);
typedef u8 (*pci_driver_initialize)(pci_device *);
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_init_bar(pci_device *device, u8 bar_index);
#endif //NEW_KERNEL_PCI_H

View File

@@ -31,6 +31,7 @@ unsigned short port_word_in(unsigned short port) {
void port_word_out(unsigned short port, unsigned short data) {
__asm__("out %%ax, %%dx" : : "a" (data), "d" (port));
}
unsigned int port_double_word_in(unsigned int port) {
unsigned int result;
__asm__("in %%dx, %%eax" : "=a" (result) : "d" (port));

View File

@@ -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_double_word_in_repeat(unsigned short port, unsigned int *data, int buffer_size);
#endif

View File

@@ -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);
}
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)];
if (table->signature[0] != 0x55 && table->signature[1] != 0xAA) { // AA 55 but in little endian
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
@@ -101,9 +101,9 @@ void mbr_register_block_driver() {
}
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);
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);
free(mbr_data);
}

View File

@@ -36,8 +36,11 @@ typedef struct {
} cmd_handler;
void print(const char *arg);
void ide(const char *arg);
void echo(const char *arg);
void help(const char *arg);
cmd_handler cmd_handlers[] = {
@@ -55,7 +58,7 @@ void print_bootinfo() {
(cmdline[0] == 0 ? "NULL" : cmdline));
}
void help(const char* arg) {
void help(const char *arg) {
kprint("Available commands:\n");
int index = 0;
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) {
block_dev_print_info();
} else if (strcmp(arg, "ide_devices") == 0) {

View File

@@ -13,7 +13,7 @@
/*
* 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
char msg[33];
itoa(value, msg, base);
@@ -53,13 +53,13 @@ u32 vasprintf(char *buf, const char *fmt, va_list args) {
break;
}
case 'c':
buf[ptr++] = (char)va_arg(args, int);
buf[ptr++] = (char) va_arg(args, int);
break;
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;
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;
case '%':
buf[ptr++] = '%';
@@ -85,7 +85,7 @@ int printf(const char *fmt, ...) {
return result;
}
int sprintf(char* target, const char*fmt, ...) {
int sprintf(char *target, const char *fmt, ...) {
int result;
va_list args;
va_start(args, fmt);

View File

@@ -32,12 +32,13 @@ int abs(int val) {
// next stolen form https://www.techiedelight.com/implement-itoa-function-in-c/
// inline function to swap two numbers
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]
char* reverse(char *buffer, int i, int j)
{
char *reverse(char *buffer, int i, int j) {
while (i < 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
char* itoa(int value, char* buffer, int base)
{
char *itoa(int value, char *buffer, int base) {
// invalid input
if (base < 2 || base > 32)
return buffer;
@@ -55,8 +55,7 @@ char* itoa(int value, char* buffer, int base)
int n = abs(value);
int i = 0;
while (n)
{
while (n) {
int r = n % base;
if (r >= 10)

View File

@@ -11,12 +11,12 @@
#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);
char* result = malloc(RESULT_SIZE);
char *result = malloc(RESULT_SIZE);
memset(result, 0, RESULT_SIZE);
for (int i = 0; i < RESULT_SIZE - 1; ++i) {
result[i] = getc();

View File

@@ -5,6 +5,6 @@
#ifndef NEW_KERNEL_READLINE_H
#define NEW_KERNEL_READLINE_H
char* readline(const char *prompt);
char *readline(const char *prompt);
#endif //NEW_KERNEL_READLINE_H

View File

@@ -46,7 +46,7 @@ void ring_buffer_put(void *buffer, void *item) {
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;
if (ring_buffer->read_pos == ring_buffer->write_pos) {
// nothing to read

View File

@@ -14,6 +14,6 @@ void free_buffer(void *buffer);
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

View File

@@ -6,9 +6,9 @@
#define NEW_KERNEL_VA_LIST_H
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_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)
#endif //NEW_KERNEL_VA_LIST_H

View File

@@ -9,7 +9,7 @@ void k_wait_for_interrupt() {
__asm__ __volatile__("hlt;");
}
void k_panics(const char* msg) {
void k_panics(const char *msg) {
kprint(msg);
kprint("PANIC!");
__asm__ __volatile__("cli;"

View File

@@ -7,7 +7,7 @@
void k_wait_for_interrupt();
void k_panics(const char* msg);
void k_panics(const char *msg);
void k_panic();