Browse Source

Bincows test environment on unix

master
Mathieu Serandour 1 year ago
parent
commit
50328d9f23
  1. 104
      tests/disk_tb.c
  2. 46
      tests/tb.c
  3. 23
      tests/vfs/Makefile
  4. BIN
      tests/vfs/vfs
  5. 127
      tests/vfs/vfs.c

104
tests/disk_tb.c

@ -0,0 +1,104 @@
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <drivers/storage_interface.h>
#include <drivers/driver.h>
#include <drivers/dev.h>
#include <fs/gpt.h>
#include <lib/dump.h>
struct data {
FILE* f;
struct storage_interface si;
};
static
void dread(struct driver* this,
uint64_t lba,
void* buf,
size_t count
) {
struct data* data = this->data;
size_t size = 1 << data->si.lbashift;
fseek(data->f, lba * size, SEEK_SET);
assert(fread(buf, size, count, data->f) == count);
printf("DREAD %lu %lu\n", size, size*count);
//dump(buf, size*count, 32, DUMP_HEX8);
//printf(buf);
//fwrite(, size, count, stdout);
}
static
void dwrite(struct driver* this,
uint64_t lba,
const void* buf,
size_t count
) {
struct data* data = this->data;
size_t size = 1 << data->si.lbashift;
fseek(data->f, lba * size, SEEK_SET);
assert(fwrite(buf, size, count, data->f) == count);
printf("DWRITE\n");
}
static
void dremove(driver_t* this) {
fclose(this->data);
this->status = DRIVER_STATE_SHUTDOWN;
}
static
int install(driver_t* this) {
FILE* dfile = fopen((void*)(this->device+1), "rb+");
assert(dfile);
struct data* data = this->data = malloc(sizeof(struct data));
this->data_len = sizeof(struct data);
this->remove = dremove;
this->status = DRIVER_STATE_OK;
this->name = (string_t){"tb disk controller", 0},
data->si = (struct storage_interface) {
.capacity = ftell(dfile),
.driver = this,
.lbashift = 9,
.read = dread,
.write = dwrite,
};
data->f = dfile;
gpt_scan(&data->si);
return 1;
}
void disk_tb_install(const char* path) {
struct tmp {
dev_t dev;
char path[128];
}* dev = malloc(sizeof(struct tmp));
*dev = (struct tmp) {
.dev = {
.name = {.ptr = "vdisk", .freeable=0},
.type = 0,
.driver = NULL,
},
};
strcpy(dev->path, path);
register_dev((void*)dev);
driver_register_and_install(install, (void*)dev);
}

46
tests/tb.c

@ -7,8 +7,54 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <drivers/storage_interface.h>
unsigned heap_get_n_allocation(void) {
return 0;
}
void ps2_trigger_CPU_reset() {
printf("CPU RESET");
exit(0);
}
void* get_active_terminal() {
return NULL;
}
void terminal_set_fgcolor(void) {
}
void terminal_set_bgcolor(void) {
}
void terminal_set_colors(void) {
}
size_t heap_get_size() {
return 0;
}
size_t heap_get_brk(void) {
return 0;
}
size_t stack_size = 4096;
void* stack_base = NULL;
void* _rbp(void) {
return __builtin_return_address(0);
}
void ps2kb_poll_wait_for_key(uint8_t key) {
(void) key;
getchar();
}

23
tests/vfs/Makefile

@ -13,17 +13,30 @@ PY := python3
KERNEL=../../kernel/
CFLAGS = -Wall -Wextra -O3 -pipe -I$(KERNEL)
CFLAGS = -Wall -Wextra -O0 -pipe -I$(KERNEL) -DTEST -fno-inline -g
CFILES := $(shell find ./ -type f -name '*.c')
CFILES := ../tb.c
CFILES += ../tb.c
CFILES += ../disk_tb.c
KCFILES += $(shell find $(KERNEL)/fs -type f -name '*.c')
KCFILES += $(KERNEL)/drivers/dev.c
KCFILES += $(KERNEL)/drivers/driver.c
KCFILES += $(KERNEL)/lib/logging.c
KCFILES += $(KERNEL)/lib/dump.c
KCFILES += $(KERNEL)/lib/stacktrace.c
KCFILES += $(KERNEL)/lib/panic.c
KCFILES += $(KERNEL)/lib/utf16le.c
KCFILES += $(KERNEL)/acpi/power.c
OBJ := $(CFILES:.c=.o) $(KCFILES:.c=.test.o)
OBJ := $(CFILES:.c=.test.o) $(KCFILES:.c=.test.o)
debug: all
gdb $(EXE)
run: all
@ -39,9 +52,9 @@ $(EXE): $(OBJ)
%.test.o: %.c
$(CC) -DTEST $(CFLAGS) $(INTERNALCFLAGS) -c $< -o $@
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf *.o
rm -rf $(KERNEL)*.test.o
find $(KERNEL) -name "*.test.o" | rm -rf

BIN
tests/vfs/vfs

Binary file not shown.

127
tests/vfs/vfs.c

@ -1,6 +1,133 @@
#include <stdio.h>
#include <fs/vfs.h>
#include <lib/assert.h>
#include <lib/panic.h>
#include <lib/logging.h>
#include <lib/dump.h>
#include <stdlib.h>
void disk_tb_install(const char* path);
static void log_tree(const char *path, int level)
{
struct DIR *dir = vfs_opendir(path);
struct dirent *dirent;
assert(dir);
while ((dirent = vfs_readdir(dir)))
{
for (int i = 0; i < level + 1; i++)
puts("-");
printf(" %d - %s (size %u) \n", dirent->type, dirent->name, dirent->file_size);
if (dirent->type == DT_DIR)
{
char *pathb = malloc(strlen(path) + strlen(dirent->name) + 2);
strcpy(pathb, path);
strcat(pathb, "/");
strcat(pathb, dirent->name);
log_tree(pathb, level + 1);
free(pathb);
}
}
vfs_closedir(dir);
}
// read & seek test function
static inline
void test_file_read_seek(void) {
file_handle_t* f = vfs_open_file("/fs/file.dat");
assert(f);
assert(!vfs_seek_file(f, 512, SEEK_SET));
log_warn("FILE SIZE = %u", vfs_tell_file(f));
vfs_seek_file(f, 50, SEEK_CUR);
char rd[513];
assert(vfs_read_file(rd, 512, 1, f) == 1);
rd[512] = 0;
printf("READ: %512s", rd);
dump(rd, 512, 32, DUMP_HEX8);
vfs_seek_file(f, 50, SEEK_CUR);
char buf[512] =
"Une balle pourtant, mieux ajustée ou plus traître que les autres, finit par atteindre "
"l'enfant feu follet. On vit Gavroche chanceler, puis il s'affaissa. Toute la barricade "
"poussa un cri ; mais il y avait de l'Antée dans ce pygmée ; pour le gamin toucher le "
"pavé, c'est comme pour le géant toucher la terre ; Gavroche n'était tombé que pour se "
"redresser ; il resta assis sur son séant, un long filet de sang rayait son visage,"
"il éleva ses deux bras en l'air, regarda du côté d'où était venu le coup, et se mit à";
vfs_write_file(buf, 512, 1, f);
vfs_close_file(f);
return;
#define SIZE 512
/*
vfs_seek_file(f, 0, SEEK_END);
vfs_seek_file(f, 0, SEEK_SET);
char* buf = malloc(SIZE+1);
int read = 0;
int x = 531;
for(int i = 0; i < 200; i++)
{
x = (x * 411 + 1431) % (1024 * 1024 / 4 - SIZE);
size_t y = x*4;
log_info("test %u", y);
vfs_seek_file(f, y, SEEK_SET);
read = vfs_read_file(buf, 1, SIZE, f);
assert(read);
buf[read] = 0;
uint32_t* chunk = (uint32_t*)buf;
for(unsigned j = 0; j < SIZE / 4; j++) {
if(chunk[j] != j + x) {
log_warn("chunk[j]=%x, j + x=%x", chunk[j], j+x);
panic("no :(");
}
assert(chunk[j] == j + x);
}
//dump(buf, SIZE, 8, DUMP_DEC32);
}
vfs_close_file(f);
*/
}
int main() {
vfs_init();
disk_tb_install("../../disk.bin");
disk_part_t* part = search_partition("Bincows");
assert(part);
printf("MOUNT");
vfs_mount(part, "/fs");
test_file_read_seek();
}

Loading…
Cancel
Save