I want to create arm os like MSDOS.
i have bootloader (strap.s)
.global _start
_start:
ldr x0, =0x20000
mov sp, x0
bl notmain
hang:
b hang
.global PUT32
PUT32:
str w1,[x0]
ret
This is 64bit booyloader which load C function from notmain.c:
#include <stdint.h>
#include "uart.h"
#include "commands.h"
#define MAX_INPUT_LENGTH 64
void notmain(void) {
char c;
char input_buffer[MAX_INPUT_LENGTH + 1]; // +1 для нуль-термінатора
int input_pos = 0;
uart_puts("UART test...n");
uart_puts(">>>");
while (1) {
c = uart_getc();
// Виведення кожного зчитаного символу для відладки
uart_puts("Received char: ");
uart_putc(c);
uart_putc('n');
if (c == 'r' || c == 'n') {
uart_putc('r');
uart_putc('n');
input_buffer[input_pos] = ''; // Завершення рядка нуль-термінатором
// Діагностика: виведення буфера перед передачею в process_command
uart_puts("Input buffer: ");
uart_puts(input_buffer);
uart_putc('n');
process_command(input_buffer); // Виклик команди
input_pos = 0; // Скидання буфера
uart_puts(">>>");
} else if (c == 'b' || c == 127) { // Backspace
if (input_pos > 0) {
input_pos--;
uart_puts("b b"); // Видалення останнього символу з консолі
}
} else {
if (input_pos < MAX_INPUT_LENGTH) {
input_buffer[input_pos++] = c;
uart_putc(c); // Виведення символу на екран
}
}
}
}
The provided code snippet implements the main input handling loop for a simple command-line interface in an ARM-based operating system using UART for communication.
in this code we have heade files: uart.h
and commands.h
// uart.h
#ifndef UART_H
#define UART_H
#include <stdint.h>
void uart_putc(char c);
char uart_getc();
void uart_puts(const char *str);
#endif // UART_H
The uart.h header file provides the necessary declarations for UART communication functions, enabling other parts of the operating system to utilize these functions without needing to know the details of their implementation.
and i have also uart.c file
// uart.c
#include "uart.h"
#define UART0_BASE 0x09000000
#define UART0_DR (*(volatile uint32_t *)(UART0_BASE + 0x00))
#define UART0_FR (*(volatile uint32_t *)(UART0_BASE + 0x18))
void uart_putc(char c){
while(UART0_FR & (1 << 5)) {}
UART0_DR = c;
}
char uart_getc(){
while(UART0_FR & (1 << 4)) {}
return UART0_DR & 0xFF;
}
void uart_puts(const char *str){
while (*str){
if(*str == 'n'){
uart_putc('r');
uart_putc('n');
str++;
} else {
uart_putc(*str++);
}
}
}
This code provides essential functions for UART communication in the operating system, enabling the sending and receiving of individual characters and strings. This functionality is crucial for user interaction and debugging, allowing the system to communicate with users through a serial interface.
come back to commands.h:
// commands.h
#ifndef COMMANDS_H
#define COMMANDS_H
void process_command(const char *cmd);
#endif // COMMANDS_H
and i have commands.c file:
// commands.c
#include "commands.h"
#include "uart.h"
// Remove #include <string.h>
// Implement strcmp
int strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
void process_command(const char *cmd) {
int i = 0;
// Skip '>' and spaces
while (cmd[i] == '>' || cmd[i] == ' ') {
i++;
}
// Extract command word
char command[65];
int j = 0;
while (cmd[i] != '' && cmd[i] != ' ' && j < 64) {
command[j++] = cmd[i++];
}
command[j] = '';
// Check for "version" command
if (strcmp(command, "version") == 0) {
uart_puts("Version 0.1n");
} else {
uart_puts("Unknown commandn");
}
}
The commands.c file provides the functionality to interpret user commands in the operating system. It includes a custom string comparison function and processes input to recognize a specific command (“version”). When the command is received, it communicates back to the user through UART, enabling interaction in a command-line interface. This design allows for easy expansion to include additional commands in the future.
i run code using wsl and use this commands:
aarch64-linux-gnu-as --warn --fatal-warnings -march=armv8-a strap.s -o strap.o
aarch64-linux-gnu-gcc -c -Wall -O2 -nostdlib -nostartfiles -ffreestanding -march=armv8-a notmain.c -o notmain.o
aarch64-linux-gnu-gcc -c -Wall -O2 -nostdlib -nostartfiles -ffreestanding -march=armv8-a uart.c -o uart.o
aarch64-linux-gnu-gcc -c -Wall -O2 -nostdlib -nostartfiles -ffreestanding -march=armv8-a commands.c -o commands.o
aarch64-linux-gnu-ld strap.o notmain.o uart.o commands.o -T memmap -o notmain.elf
aarch64-linux-gnu-objcopy notmain.elf -O binary os.bin
qemu-system-aarch64 -M virt -cpu cortex-a53 -m 128 -serial mon:stdio -kernel os.bin
but when i laucnhed code console shows me that command version is uncorrect:
How i tryed to fix it: just rewrite commands.c for debuging:
#include "commands.h"
#include "uart.h"
// Реалізація strcmp
int strcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++;
s2++;
}
return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}
void process_command(const char *cmd) {
// Виведення введеного рядка для діагностики
uart_puts("Received command: ");
uart_puts(cmd);
uart_putc('n');
// Пропуск пробілів на початку команди
while (*cmd == ' ') {
cmd++;
}
// Порівняння команди
if (strcmp(cmd, "version") == 0) {
uart_puts("Version 0.1n");
} else {
uart_puts("Unknown commandn");
}
}
But i see that buffer is not setuped:
How i can fixed proublem whis scanning text from console and how i can set up buffer correctly?