hw2 done
This commit is contained in:
52
include/dtb.h
Normal file
52
include/dtb.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <vector.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
uint32_t totalsize;
|
||||
uint32_t off_dt_struct;
|
||||
uint32_t off_dt_strings;
|
||||
uint32_t off_mem_rsvmap;
|
||||
uint32_t version;
|
||||
uint32_t last_comp_version;
|
||||
uint32_t boot_cpuid_phys;
|
||||
uint32_t size_dt_strings;
|
||||
uint32_t size_dt_struct;
|
||||
}__attribute__((packed)) fdt_header_t;
|
||||
|
||||
typedef struct {
|
||||
uint64_t address;
|
||||
uint64_t size;
|
||||
}__attribute__((packed)) fdt_reserve_entry_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t len;
|
||||
uint32_t nameoff;
|
||||
}__attribute__((packed)) fdt_prop_header_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t len;
|
||||
const char *name;
|
||||
void *value;
|
||||
} fdt_prop_t;
|
||||
|
||||
#define FDT_BEGIN_NODE 0x00000001
|
||||
#define FDT_END_NODE 0x00000002
|
||||
#define FDT_PROP 0x00000003
|
||||
#define FDT_NOP 0x00000004
|
||||
#define FDT_END 0x00000009
|
||||
|
||||
#define FDT_PATH_BUFFER_LEN 0x10
|
||||
|
||||
typedef void (*fdt_callback_func_t)(const vector_t *props);
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const fdt_callback_func_t func;
|
||||
} fdt_callback_t;
|
||||
|
||||
void fdt_callback(const char *path, const vector_t *cbs, const vector_t *props);
|
||||
void fdt_traverse(const vector_t *cbs);
|
||||
|
||||
extern void *dtb_addr;
|
||||
6
include/errcode.h
Normal file
6
include/errcode.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#define ERR_NO_MEM 0x00000001
|
||||
#define ERR_UNREACHABLE 0x00000002
|
||||
#define ERR_CONVERSION 0x00000003
|
||||
#define ERR_VECTOR_OOR 0x00000101
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <dtb.h>
|
||||
#include <vector.h>
|
||||
|
||||
typedef struct {
|
||||
char c_magic[6];
|
||||
@@ -38,9 +40,16 @@ typedef struct file_node {
|
||||
int namesize;
|
||||
|
||||
char *filename;
|
||||
byte_t *filecontent;
|
||||
uint8_t *filecontent;
|
||||
} file_node_t;
|
||||
|
||||
typedef void (*initrd_callback_func_t)(file_node_t *);
|
||||
|
||||
file_node_t *initrd_init(void);
|
||||
int initrd_ls(void);
|
||||
void initrd_fdt_callback(const vector_t *props);
|
||||
file_node_t *initrd_get(file_node_t *root, const char *filename);
|
||||
void initrd_traverse(file_node_t *tr, initrd_callback_func_t func);
|
||||
|
||||
extern fdt_callback_t initrd_dtb_cb;
|
||||
extern void *initrd_start;
|
||||
extern void *initrd_end;
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <initrd.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void help(void);
|
||||
void hello(void);
|
||||
void hwinfo(void);
|
||||
void ls(file_node_t *root);
|
||||
void cat(file_node_t *root, const char *filename);
|
||||
void reboot(void);
|
||||
|
||||
int // is continue
|
||||
shell(void);
|
||||
shell(file_node_t *);
|
||||
|
||||
@@ -3,7 +3,12 @@
|
||||
#define true 1
|
||||
#define false 0
|
||||
#define ENDL "\r\n"
|
||||
#define NAME_MAX 255
|
||||
#define PATH_MAX 4096
|
||||
|
||||
typedef long unsigned int size_t;
|
||||
typedef unsigned char byte_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef int int32_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef long long int int64_t;
|
||||
typedef unsigned long long int uint64_t;
|
||||
|
||||
@@ -3,5 +3,8 @@
|
||||
#include <stddef.h>
|
||||
|
||||
int strcmp(const char *lhs, const char *rhs);
|
||||
char *strcpy(char *destination, const char *source);
|
||||
size_t strlen(const char *str);
|
||||
|
||||
void *memcpy(void *dest, const void *src, size_t count);
|
||||
void *memzero(void *start, void *end);
|
||||
|
||||
@@ -2,9 +2,24 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define DEBUG(x) { \
|
||||
uart_puts(#x " = "); \
|
||||
uart_hex((unsigned int)(unsigned long)x); \
|
||||
uart_puts(ENDL); \
|
||||
}
|
||||
|
||||
#define DEBUG_s(x) { \
|
||||
uart_puts(#x " = "); \
|
||||
uart_puts(x); \
|
||||
uart_puts(ENDL); \
|
||||
}
|
||||
|
||||
void uart_init();
|
||||
void uart_send(unsigned int c);
|
||||
byte_t uart_getb();
|
||||
uint8_t uart_getb();
|
||||
char uart_getc();
|
||||
void uart_puts(char *s);
|
||||
void uart_puts(const char *s);
|
||||
void uart_hex(unsigned int d);
|
||||
void uart_int(int d);
|
||||
|
||||
extern int is_uart_init;
|
||||
|
||||
31
include/utils.h
Normal file
31
include/utils.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <uart.h>
|
||||
|
||||
uint32_t msb32(uint32_t);
|
||||
|
||||
uint32_t hton32(const uint32_t);
|
||||
uint32_t ntoh32(const uint32_t);
|
||||
|
||||
uint64_t hton64(const uint64_t);
|
||||
uint64_t ntoh64(const uint64_t);
|
||||
|
||||
int isdigit(int);
|
||||
int isxdigit(int);
|
||||
int isupper(int);
|
||||
int isspace(int);
|
||||
|
||||
int32_t atoi32(const char *);
|
||||
uint32_t atoh32(const char *);
|
||||
|
||||
void exit(int);
|
||||
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
#define MAX(x, y) (((x) < (y)) ? (y) : (x))
|
||||
#define ALIGN4(ptr) (void *)(((((uint64_t)(ptr) - 1) >> 2) + 1) << 2)
|
||||
#define ALIGN8(ptr) (void *)(((((uint64_t)(ptr) - 1) >> 3) + 1) << 3)
|
||||
#define BUMP(orig_type, bump_type, ptr) ( \
|
||||
(ptr = (orig_type *)((bump_type *)ptr + 1)), \
|
||||
((bump_type *)ptr - 1) \
|
||||
)
|
||||
18
include/vector.h
Normal file
18
include/vector.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
size_t cap;
|
||||
|
||||
uint64_t *data;
|
||||
} vector_t;
|
||||
|
||||
vector_t *make_vector(size_t size);
|
||||
|
||||
uint64_t vec_at(const vector_t *vec, size_t idx);
|
||||
vector_t *vec_push(vector_t *vec, uint64_t val);
|
||||
|
||||
#define VEC_AT(type, vec, idx) ((type *)vec_at((vec), (idx)))
|
||||
#define VEC_PUSH(vec, val) vec = vec_push(vec, (uint64_t)val)
|
||||
Reference in New Issue
Block a user