fs work, initrd/stdio

master
Clyne Sullivan 6 years ago
parent e88b4f9040
commit 22615096de

@ -25,11 +25,9 @@ OBJCOPY = objcopy
MCUFLAGS = -mthumb -mcpu=cortex-m4 #-mfloat-abi=hard -mfpu=fpv4-sp-d16 MCUFLAGS = -mthumb -mcpu=cortex-m4 #-mfloat-abi=hard -mfpu=fpv4-sp-d16
AFLAGS = $(MCUFLAGS) AFLAGS = $(MCUFLAGS)
CFLAGS = $(MCUFLAGS) -ggdb --specs=nosys.specs \ CFLAGS = $(MCUFLAGS) -ggdb -ffreestanding -nostdlib -fsigned-char -I.. \
-I.. \ -Wall -Werror -Wextra -pedantic
-fno-builtin -fsigned-char -ffreestanding \ #-Wno-overlength-strings -Wno-discarded-qualifiers
-Wall -Werror -Wextra -pedantic \
-Wno-overlength-strings -Wno-discarded-qualifiers
LFLAGS = -T link.ld LFLAGS = -T link.ld
OUT = main.elf OUT = main.elf

@ -1,4 +1,6 @@
all: all:
@arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -fsigned-char -Os -nostdinc \ @arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -fsigned-char -Os \
-nostdinc -nostdlib \
-fdata-sections -ffunction-sections -Wl,--gc-sections \
-I../src/pdclib/include -I../src/pdclib/platform/stmos/include \ -I../src/pdclib/include -I../src/pdclib/platform/stmos/include \
test.c ../src/pdclib/pdclib.a -s -o test -pie test.c ../src/pdclib/pdclib.a -s -o test

Binary file not shown.

@ -1,4 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
int main(void);
void _start(void)
{
exit(main());
}
int main(void) int main(void)
{ {

@ -56,26 +56,6 @@ SECTIONS {
. = ALIGN(8); . = ALIGN(8);
} > FLASH } > FLASH
/* ARM stuff */
.ARM.exidx : {
*(.ARM.exidx)
} > FLASH
/* init_array/fini_array (TODO understand this) */
.init_array : {
__init_array_start = .;
KEEP(*(.init_array.*))
KEEP(*(.init_array*))
__init_array_end = .;
} > FLASH
.fini_array : {
__fini_array_start = .;
KEEP(*(.fini_array.*))
KEEP(*(.fini_array*))
__fini_array_end = .;
} > FLASH
/* initialized data */ /* initialized data */
_sidata = LOADADDR(.data); _sidata = LOADADDR(.data);
.data : { .data : {

@ -1,5 +1,4 @@
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <kernel/heap.h> #include <kernel/heap.h>
#include <kernel/vfs.h> #include <kernel/vfs.h>
@ -30,6 +29,16 @@ static const vfs_volume_funcs initrd_funcs = {
0 // readdir 0 // readdir
}; };
int initrd_strncmp(const char *a, const char *b, unsigned int n)
{
for (unsigned int i = 0; i < n; i++) {
if (a[i] != b[i])
return 1;
}
return 0;
}
void initrd_init(void) void initrd_init(void)
{ {
vfs_mount(&initrd_funcs, VFS_READONLY); vfs_mount(&initrd_funcs, VFS_READONLY);
@ -40,7 +49,7 @@ void *initrd_open(const char *file)
char *ptr; char *ptr;
for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) { for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
uint32_t len = *((uint32_t *)ptr); uint32_t len = *((uint32_t *)ptr);
if (!strncmp(file, ptr + 4, len)) { if (!initrd_strncmp(file, ptr + 4, len)) {
initrd_info *file = (initrd_info *)malloc( initrd_info *file = (initrd_info *)malloc(
sizeof(initrd_info)); sizeof(initrd_info));
file->address = ptr + len + 8; file->address = ptr + len + 8;

@ -20,7 +20,7 @@
#include "gpio.h" #include "gpio.h"
static const GPIO_TypeDef *gpio_ports[8] = { static GPIO_TypeDef *gpio_ports[8] = {
GPIOA, GPIOB, GPIOC, GPIOD, GPIOA, GPIOB, GPIOC, GPIOD,
GPIOE, GPIOF, GPIOG, GPIOH GPIOE, GPIOF, GPIOG, GPIOH
}; };

@ -105,7 +105,7 @@ LoopFillZerobss:
bcc FillZerobss bcc FillZerobss
/* Call static constructors */ /* Call static constructors */
bl __libc_init_array //bl __libc_init_array
/* Call the application's entry point.*/ /* Call the application's entry point.*/
bl main bl main

@ -87,7 +87,7 @@ void task_sleep(uint32_t ms)
pid_t task_getpid(void) pid_t task_getpid(void)
{ {
return task_current->pid; return task_current != 0 ? task_current->pid : 0;
} }
pid_t task_waitpid(pid_t pid, int *wstatus, int options) pid_t task_waitpid(pid_t pid, int *wstatus, int options)

@ -48,7 +48,7 @@ void vfs_init(void)
vfs_open(" err", VFS_FILE_WRITE); vfs_open(" err", VFS_FILE_WRITE);
} }
int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags) int vfs_mount(const vfs_volume_funcs *funcs, uint32_t flags)
{ {
for (int i = 0; i < VFS_MAX_VOLS; i++) { for (int i = 0; i < VFS_MAX_VOLS; i++) {
if (!(vfs_volumes[i].flags && VFS_MOUNTED)) { if (!(vfs_volumes[i].flags && VFS_MOUNTED)) {
@ -64,10 +64,14 @@ int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags)
int vfs_get_drive(const char *path) int vfs_get_drive(const char *path)
{ {
// Validate parameters // Validate parameters
if (path[0] == '\0' || path[1] == '\0' || path[1] != ':' || if (path[0] == '\0')
path[2] == '\0' || path[2] != '/')
return -1; return -1;
// Default to 'A' if no drive specified (A gives stdio)
if (path[1] == '\0' || path[1] != ':' ||
path[2] == '\0' || path[2] != '/')
return 0;
// Find chosen drive // Find chosen drive
int drive = -1; int drive = -1;
for (int i = 0; i < VFS_MAX_VOLS; i++) { for (int i = 0; i < VFS_MAX_VOLS; i++) {
@ -103,11 +107,14 @@ int vfs_open(const char *path, uint32_t flags)
if (file == -1) if (file == -1)
return -1; return -1;
void *fsinfo = vfs_volumes[drive].funcs->open(path + 3);
if (fsinfo == 0)
return -1;
vfs_files[file].flags = VFS_FILE_OPEN | flags; vfs_files[file].flags = VFS_FILE_OPEN | flags;
vfs_files[file].vol = drive; vfs_files[file].vol = drive;
vfs_files[file].pid = task_getpid(); vfs_files[file].pid = task_getpid();
vfs_files[file].fsinfo = vfs_files[file].fsinfo = fsinfo;
vfs_volumes[drive].funcs->open(path + 3);
return file; return file;
} }

@ -22,7 +22,7 @@ typedef struct vfs_volume_funcs_t {
typedef struct { typedef struct {
uint32_t flags; uint32_t flags;
vfs_volume_funcs *funcs; const vfs_volume_funcs *funcs;
} vfs_volume; } vfs_volume;
// Indicates an opened file // Indicates an opened file
@ -43,7 +43,7 @@ typedef struct {
void vfs_init(void); void vfs_init(void);
int vfs_mount(vfs_volume_funcs *funcs, uint32_t flags); int vfs_mount(const vfs_volume_funcs *funcs, uint32_t flags);
int vfs_open(const char *path, uint32_t flags); int vfs_open(const char *path, uint32_t flags);
int vfs_close(int fd); int vfs_close(int fd);
uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer); uint32_t vfs_read(int fd, uint32_t count, uint8_t *buffer);

@ -6,6 +6,11 @@
// //
// Task-related calls // Task-related calls
#define WEXITSTATUS(s) ((s) & 0xFF)
#define WIFEXITED(s) ((s) & (1 << 8))
#define WIFSIGNALED(s) ((s) & (1 << 9))
#define WTERMSIG(s) ((s) & (1 << 10))
void _exit(int code); void _exit(int code);
int fork(void); int fork(void);
@ -35,7 +40,9 @@ unsigned int ticks(void);
// Set if EOF has been reached // Set if EOF has been reached
#define VFS_EOF (1 << 3) #define VFS_EOF (1 << 3)
#ifndef EOF
#define EOF (-1) #define EOF (-1)
#endif // EOF
struct dirent { struct dirent {
char name[32]; char name[32];

@ -19,10 +19,11 @@ void user_main(void)
{ {
gpio(GPIO_MODE, 5, OUTPUT); gpio(GPIO_MODE, 5, OUTPUT);
int test = vfs_open("A:/hello", VFS_FILE_READ); int test = vfs_open("B:/hello", VFS_FILE_READ);
char *buf = malloc(20); char *buf = malloc(20);
int count = vfs_read(test, 20, (uint8_t *)buf); int count = vfs_read(test, 20, (uint8_t *)buf);
(void)count; buf[count] = '\0';
vfs_close(test);
// if (fork() == 0) { // if (fork() == 0) {
// while (1) { // while (1) {

Loading…
Cancel
Save