Dev: small fixes in programs + new programs #4

Merged
xamidev merged 3 commits from dev into main 2024-08-25 18:49:20 +02:00
10 changed files with 192 additions and 230 deletions

View File

@@ -1,5 +1,12 @@
# Blank OS Developer's Manual # Blank OS Developer's Manual
## Table of Contents
- [Getting Started](#getting-started)
- [Writing programs for BlankOS](#writing-programs)
- [Changing the TTY font](#changing-font)
<a name="getting-started"/>
## Getting Started ## Getting Started
### System description ### System description
@@ -27,7 +34,10 @@ gdb kernel.elf
(gdb) target remote localhost:1234 (gdb) target remote localhost:1234
``` ```
## Making programs for the OS <a name="getting-started"/>
## Writing programs for BlankOS
Be warned, these are not actual programs in the sense you'd expect. These are indeed functions that are called from the shell, and embedded in the kernel ELF binary. Real programs apart from the kernel are not yet a thing here, but might be one day.
### Step 1 - Making the program and the entry point ### Step 1 - Making the program and the entry point
@@ -86,4 +96,19 @@ The linking process should be taken care of by the appropriate Linker script `li
If you're proud of what you've made, you can clone the repo, make your changes, open a pull request and maybe your program will be added to the main BlankOS repo, and later distributed in the new ISOs! If you're proud of what you've made, you can clone the repo, make your changes, open a pull request and maybe your program will be added to the main BlankOS repo, and later distributed in the new ISOs!
<a name="changing-font"/>
## Changing the TTY font
In order to change the default font, first get your hands on a 8x16 `.psf` (PC Screen Font 2) formatted font. Then, put it in `include/fonts` and remove the default one (`UniCyr_8x16.psf`).
Go ahead and run `make` one time. The compilation/linking will fail because of unresolved symbols, but an object file should have been created in `build/fonts` with your custom font's name.
Read the symbols in that object file:
```
readelf -s -W build/fonts/YOUR_FONT_8x16.o
```
Get the symbol name that ends with `_start` and replace all occurences of it in the `src/drivers/framebuffer.c` file.
Then, run `make` again and the font should have changed properly.

View File

@@ -8,7 +8,6 @@
#include "gdt.h" #include "gdt.h"
#include "idt.h" #include "idt.h"
#include "system.h" #include "system.h"
#include "paging.h"
#include "../drivers/ata.h" #include "../drivers/ata.h"
#include <stdint.h> #include <stdint.h>
#include "../drivers/framebuffer.h" #include "../drivers/framebuffer.h"
@@ -62,10 +61,7 @@ void kmain(multiboot2_info *mb_info)
irq_install(); irq_install();
__asm__ __volatile__("sti"); __asm__ __volatile__("sti");
//init_paging();
//test_read_sector(); //test_read_sector();
//uint32_t *ptr = (uint32_t*)0xA0000000;
//uint32_t do_page_fault = *ptr;
timer_install(); timer_install();
keyboard_install(); keyboard_install();

View File

@@ -26,7 +26,7 @@ CHECKSUM equ -(MAGIC_NUMBER + FLAGS + HEADER_LEN)
; Tags? (28bytes) ; Tags? (28bytes)
; Tag 1 : set graphics mode (only recommended, can be overriden by GRUB) ; Tag 1 : set graphics mode (only recommended, can be overriden by GRUB)
align 8
dw 5 ; 2 dw 5 ; 2
dw 0 ; 2 dw 0 ; 2
dd 20 ; 4 dd 20 ; 4
@@ -36,8 +36,9 @@ CHECKSUM equ -(MAGIC_NUMBER + FLAGS + HEADER_LEN)
; End of tags ; End of tags
align 8
dw 0 ; 2
dw 0 ; 2 dw 0 ; 2
;dw 0 ; 2
dd 8 ; 4 dd 8 ; 4
; End of Multiboot 2 header ; End of Multiboot 2 header

View File

@@ -1,151 +0,0 @@
// Paging kernel module
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include <stdint.h>
#include "paging.h"
#include "../libc/stdio.h"
#include "system.h"
#include "kheap.h"
uint32_t *frames;
uint32_t nframes;
extern uint32_t placement_address;
static void set_frame(uint32_t frame_addr)
{
uint32_t frame = frame_addr/0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
frames[idx] |= (0x1 << off);
}
static void clear_frame(uint32_t frame_addr)
{
uint32_t frame = frame_addr/0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
frames[idx] &= ~(0x1 << off);
}
/*
static uint32_t test_frame(uint32_t frame_addr)
{
uint32_t frame = frame_addr/0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
return (frames[idx] & (0x1 << off));
}
*/
static uint32_t first_frame()
{
uint32_t i, j;
for (i=0; i<INDEX_FROM_BIT(nframes); i++)
{
if (frames[i] != 0xFFFFFFFF)
{
for (j=0; j<32; j++)
{
uint32_t toTest = 0x1 << j;
if (!(frames[i]&toTest))
{
return i*4*8+j;
}
}
}
}
return 0;
}
void alloc_frame(page_t *page, int is_kernel, int is_writeable)
{
if (page->frame != 0)
{
return;
} else {
uint32_t idx = first_frame();
if (idx == (uint32_t)-1)
{
panic();
}
set_frame(idx*0x1000);
page->present = 1;
page->rw = (is_writeable)?1:0;
page->user = (is_kernel)?0:1;
page->frame = idx;
}
}
void free_frame(page_t *page)
{
uint32_t frame;
if (!(frame=page->frame))
{
return;
} else {
clear_frame(frame);
page->frame = 0x0;
}
}
void init_paging()
{
uint32_t mem_end_page = 0x10000000;
nframes = mem_end_page / 0x1000;
frames = (uint32_t*)kmalloc(INDEX_FROM_BIT(nframes));
memset(frames, 0, INDEX_FROM_BIT(nframes));
page_directory_t* kernel_directory = (page_directory_t*)kmalloc_a(sizeof(page_directory_t));
memset(kernel_directory, 0, sizeof(page_directory_t));
//page_directory_t* current_directory = kernel_directory;
unsigned int i = 0;
while (i < placement_address)
{
alloc_frame(get_page(i, 1, kernel_directory), 0, 0);
i += 0x1000;
}
irq_install_handler(14, page_fault);
switch_page_directory(kernel_directory);
}
void switch_page_directory(page_directory_t *dir)
{
//page_directory_t* current_directory = dir;
asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical));
uint32_t cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 |= 0x80000000;
asm volatile("mov %0, %%cr0":: "r"(cr0));
}
page_t *get_page(uint32_t address, int make, page_directory_t *dir)
{
address /= 0x1000;
uint32_t table_idx = address / 1024;
if (dir->tables[table_idx])
{
return &dir->tables[table_idx]->pages[address%1024];
} else if (make)
{
uint32_t tmp;
dir->tables[table_idx] = (page_table_t*)kmalloc_ap(sizeof(page_table_t), &tmp);
memset(dir->tables[table_idx], 0, 0x1000);
dir->tablesPhysical[table_idx] = tmp | 0x7;
return &dir->tables[table_idx]->pages[address%1024];
} else {
return 0;
}
return 0;
}
void page_fault()
{
puts("Page fault");
panic();
}

View File

@@ -1,43 +0,0 @@
// Paging kernel module header
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef PAGING_H
#define PAGING_H
#include "system.h"
#include <stdint.h>
#define INDEX_FROM_BIT(a) (a/(8*4))
#define OFFSET_FROM_BIT(a) (a%(8*4))
typedef struct
{
uint32_t present : 1;
uint32_t rw : 1;
uint32_t user : 1;
uint32_t accessed : 1;
uint32_t dirty : 1;
uint32_t unused : 7;
uint32_t frame : 20;
} page_t;
typedef struct
{
page_t pages[1024];
} page_table_t;
typedef struct
{
page_table_t *tables[1024];
uint32_t tablesPhysical[1024];
uint32_t physicalAsddr;
} page_directory_t;
void init_paging();
void switch_page_directory(page_directory_t *new);
page_t *get_page(uint32_t address, int make, page_directory_t *dir);
void page_fault();
#endif

View File

@@ -12,7 +12,7 @@
#include "../drivers/rtc.h" #include "../drivers/rtc.h"
#define BUFFER_SIZE 256 #define BUFFER_SIZE 256
#define MAX_COMMANDS 16 #define MAX_COMMANDS 64
#define MAX_ARGS 64 #define MAX_ARGS 64
// Splash screen: esthetic stuff. // Splash screen: esthetic stuff.
@@ -25,6 +25,8 @@ char* motd[] =
}; };
int motd_size = sizeof(motd)/sizeof(motd[0]); int motd_size = sizeof(motd)/sizeof(motd[0]);
bool do_splash = true;
void splash() void splash()
{ {
int random = randint(time_seed()); int random = randint(time_seed());
@@ -87,7 +89,11 @@ int parse_input(char* input, char* argv[], int max_args)
void shell_install() void shell_install()
{ {
splash(); if (do_splash == true)
{
do_splash = false;
splash();
}
register_command("help", program_help); register_command("help", program_help);
register_command("panic", program_panic); register_command("panic", program_panic);
@@ -104,6 +110,9 @@ void shell_install()
register_command("rot13", program_rot13); register_command("rot13", program_rot13);
register_command("morse", program_morse); register_command("morse", program_morse);
register_command("cowsay", program_cowsay); register_command("cowsay", program_cowsay);
register_command("time", program_time);
register_command("read", program_read);
register_command("reboot", program_reboot);
for (;;) for (;;)
{ {

View File

@@ -56,12 +56,35 @@ void dtostrf(double val, char *buffer, int precision);
enum Colors enum Colors
{ {
// AARRGGBB? // AARRGGBB?
white = 0xFFFFFFFF, white = 0xFFFFFFFF,
black = 0x00000000, black = 0x00000000,
red = 0x00FF0000, red = 0x00FF0000,
green = 0x0000FF00, green = 0x0000FF00,
blue = 0x000000FF, blue = 0x000000FF,
yellow = 0x00FFFF00, yellow = 0x00FFFF00,
cyan = 0x0000FFFF,
magenta = 0x00FF00FF,
orange = 0x00FFA500,
purple = 0x00800080,
brown = 0x00A52A2A,
gray = 0x00808080,
pink = 0x00FFC0CB,
lime = 0x00BFFF00,
navy = 0x00000080,
teal = 0x00008080,
maroon = 0x00800000,
olive = 0x00808000,
silver = 0x00C0C0C0,
gold = 0x00FFD700,
indigo = 0x004B0082,
violet = 0x00EE82EE,
coral = 0x00FF7F50,
turquoise = 0x0040E0D0,
salmon = 0x00FA8072,
chocolate = 0x00D2691E,
khaki = 0x00F0E68C,
lavender = 0x00E6E6FA,
beige = 0x00F5F5DC
}; };
#endif #endif

View File

@@ -29,14 +29,27 @@ void rot13(char* input, char* output)
output[i] = '\0'; output[i] = '\0';
} }
void program_rot13() void program_rot13(int argc, char* argv[])
{ {
char input_buffer[BUFFER_SIZE]; if (argc < 2)
char output[BUFFER_SIZE]; {
puts("String? "); printf("Usage: %s <string>\n", argv[0]);
get_input(input_buffer, BUFFER_SIZE); return;
}
char input_buffer[BUFFER_SIZE] = {0};
char output[BUFFER_SIZE] = {0};
for (int i=1; i<argc; i++)
{
strcat(input_buffer, argv[i]);
if (i<argc-1)
{
strcat(input_buffer, " ");
}
}
rot13(input_buffer, output); rot13(input_buffer, output);
printf("\n%s\n", output); printf("%s\n", output);
} }
const char* morse_alphabet[] = { const char* morse_alphabet[] = {
@@ -121,12 +134,27 @@ void to_morse(const char* input, char* output) {
} }
} }
void program_morse() { void program_morse(int argc, char* argv[]) {
if (argc < 2)
{
printf("Usage: %s <string>\n", argv[0]);
return;
}
char output[512]; char output[512];
char input_buffer[BUFFER_SIZE]; char message[BUFFER_SIZE];
puts("String? ");
get_input(input_buffer, BUFFER_SIZE); for (int i=1; i<argc; i++)
to_morse(input_buffer, output); {
printf("\n%s\n", output); strcat(message, argv[i]);
if (i < argc-1)
{
strcat(message, " ");
}
}
to_morse(message, output);
printf("%s\n", output);
} }

View File

@@ -7,22 +7,46 @@
#include "../kernel/system.h" #include "../kernel/system.h"
#include "../libc/string.h" #include "../libc/string.h"
#include "../drivers/framebuffer.h" #include "../drivers/framebuffer.h"
#include "../drivers/ata.h"
#include "../drivers/rtc.h"
#include "../kernel/io.h"
// Print a rainbow colorful text for testing // Print a rainbow colorful text for testing
#define BUF_SIZE 256 #define BUF_SIZE 256
#define COLORS 20 #define COLORS 20
void program_rainbow() void program_rainbow(int argc, char* argv[])
{ {
char input_buffer[BUF_SIZE]; if (argc < 2)
puts("What to print? ");
get_input(input_buffer, BUF_SIZE);
puts("\n");
for (int i=0; i<COLORS; i++)
{ {
//colorputs(input_buffer, i); printf("Usage: %s <string>\n", argv[0]);
return;
}
char input_buffer[BUF_SIZE] = {0};
for (int i=1; i<argc; i++)
{
strcat(input_buffer, argv[i]);
if (i<argc-1)
{
strcat(input_buffer, " ");
}
}
enum Colors colors[] = {
white, black, red, green, blue, yellow,
cyan, magenta, orange, purple, brown,
gray, pink, lime, navy, teal, maroon,
olive, silver, gold, indigo, violet,
coral, turquoise, salmon, chocolate,
khaki, lavender, beige
};
int colors_count = sizeof(colors)/sizeof(colors[0]);
for (int i=0; i<colors_count-1; i++)
{
colorputs(input_buffer, colors[i], colors[i+1]);
puts("\n"); puts("\n");
} }
} }
@@ -49,7 +73,7 @@ void program_uptime()
void program_help() void program_help()
{ {
printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\tconway\nrot13 morse\tcowsay\n"); printf("help\tpanic\twords\tprimes\trainbow\tclear\nmath\tbf\t uptime echo\t sysinfo\tconway\nrot13 morse\tcowsay time\t read\t reboot\n");
} }
// Panic // Panic
@@ -72,3 +96,50 @@ void program_echo(int argc, char* argv[])
} }
puts("\n"); puts("\n");
} }
// Get current RTC time
void program_time()
{
rtc_time_t time;
rtc_read_time(&time);
puts("Current RTC time: ");
print_time(&time);
puts("\n");
}
// Read a sector
void program_read(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: %s <sector>\n", argv[0]);
} else if (argc == 2)
{
uint8_t buffer[512];
ata_read_sector(atoi(argv[1]), buffer);
for (int i=0; i<512; i++)
{
if (i%50==0) puts("\n"); // hardcoded = bad
printf("%02x ", buffer[i]);
}
puts("\n");
} else
{
puts("Invalid argument number\n");
}
}
// Reboots the machine (might just shutdown)
void program_reboot()
{
puts("Rebooting...\n");
while(inb(0x64) & 0x02);
outb(0x64, 0xFE);
while (1) asm volatile("hlt");
}

View File

@@ -29,5 +29,8 @@ void program_uptime();
void program_panic(); void program_panic();
void program_help(); void program_help();
void program_echo(); void program_echo();
void program_time();
void program_read();
void program_reboot();
#endif #endif