Feat: lab 4

This commit is contained in:
2025-03-21 03:39:25 +08:00
parent eb7b9199f6
commit a6dc454a4c
35 changed files with 833 additions and 174 deletions

View File

@@ -1,52 +0,0 @@
#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;

View File

@@ -1,6 +1,8 @@
#pragma once
#define ERR_NO_MEM 0x00000001
#define ERR_UNREACHABLE 0x00000002
#define ERR_CONVERSION 0x00000003
#define ERR_VECTOR_OOR 0x00000101
#define ERR_NO_MEM 0x00000001
#define ERR_UNREACHABLE 0x00000002
#define ERR_CONVERSION 0x00000003
#define ERR_OUT_OF_BOUND 0x00000004
#define ERR_INVALID_OP 0x00000005
#define ERR_INVALID_MEM 0x00000006

View File

@@ -1,55 +0,0 @@
#pragma once
#include <stddef.h>
#include <dtb.h>
#include <vector.h>
typedef struct {
char c_magic[6];
char c_ino[8];
char c_mode[8];
char c_uid[8];
char c_gid[8];
char c_nlink[8];
char c_mtime[8];
char c_filesize[8];
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8];
char c_check[8];
}__attribute__((packed)) cpio_newc_header_t;
typedef struct file_node {
struct file_node *l, *r;
int rand;
int node_size;
int ino;
int mode;
int uid;
int gid;
int nlink;
int mtime;
int filesize;
int devmajor;
int devminor;
int rdevmajor;
int rdevminor;
int namesize;
char *filename;
uint8_t *filecontent;
} file_node_t;
typedef void (*initrd_callback_func_t)(file_node_t *);
file_node_t *initrd_init(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;

View File

@@ -1,7 +0,0 @@
#pragma once
#include <stddef.h>
void *simple_alloc(size_t size);
void *kmalloc(size_t size);
void kfree(void *ptr);

95
include/logger.h Normal file
View File

@@ -0,0 +1,95 @@
#pragma once
#include <stddef.h>
#include <uart.h>
#ifndef LOGLEVEL
#define LOGLEVEL 3
#endif
#define LOGGER_BUFLEN 0x100
static inline char *_do_nothing(char *dest, const char *) { return dest; }
static inline char *_logger_string(char *dest, const char *src)
{
while (*src != '\0')
*dest++ = *src++;
*dest = '\0';
return dest;
}
static inline char *_logger_hex(char *dest, uint64_t x)
{
for (int n, c = 28; c >= 0; c -= 4) {
n = (x >> c) & 0xf;
n += (n > 9) ? 0x37 : 0x30;
*dest++ = (char)n;
}
*dest = '\0';
return dest;
}
static inline char *_logger_pointer(char *dest, void *x)
{
*dest++ = '0';
*dest++ = 'x';
return _logger_hex(dest, (uint64_t)x);
}
#define _I_HATE_C_LANG(msg) \
logger_cur = _Generic((msg), \
char * : _do_nothing, \
const char *: _do_nothing, \
default : _logger_string \
)(logger_cur, #msg " = "); \
logger_cur = _Generic((msg), \
char * : _logger_string, \
const char *: _logger_string, \
uint64_t : _logger_hex, \
default : _logger_pointer \
)(logger_cur, msg)
#define LOG(val) { \
if (logger_cur != logger_buf) \
logger_cur = _logger_string(logger_cur, ", "); \
_I_HATE_C_LANG(val); \
}
#define FLUSH(prefix) { \
uart_puts(prefix); \
uart_puts(logger_buf); \
uart_puts(ENDL); \
logger_cur = logger_buf; \
}
#if LOGLEVEL >= 0
#define ERROR(val) { \
LOG(val); \
FLUSH("[ERROR]: "); \
}
#else
#define ERROR(val)
#endif
#if LOGLEVEL >= 1
#define INFOR(val) { \
LOG(val); \
FLUSH("[INFOR]: "); \
}
#else
#define INFOR(val)
#endif
#if LOGLEVEL >= 2
#define DEBUG(val) { \
LOG(val); \
FLUSH("[DEBUG]: "); \
}
#define DEBUG_DTB(val) DEBUG(val)
#define DEBUG_MEM(val) DEBUG(val)
#else // #if LOGLEVEL >= 2
#define DEBUG(val)
#define DEBUG_DTB(val)
#define DEBUG_MEM(val)
#endif // #if LOGLEVEL >= 2
extern char logger_buf[LOGGER_BUFLEN];
extern char *logger_cur;

View File

@@ -1,51 +0,0 @@
/*
* Copyright (C) 2018 bzt (bztsrc@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/* a properly aligned buffer */
extern volatile unsigned int mbox[36];
#define MBOX_REQUEST 0
/* channels */
#define MBOX_CH_POWER 0
#define MBOX_CH_FB 1
#define MBOX_CH_VUART 2
#define MBOX_CH_VCHIQ 3
#define MBOX_CH_LEDS 4
#define MBOX_CH_BTNS 5
#define MBOX_CH_TOUCH 6
#define MBOX_CH_COUNT 7
#define MBOX_CH_PROP 8
/* tags */
#define MBOX_TAG_LAST 0
#define MBOX_TAG_BOARD_REVISION 0x00010002
#define MBOX_TAG_GETSERIAL 0x00010004
#define MBOX_TAG_ARM_MEMORY 0x00010005
int mbox_call(unsigned char ch);
unsigned int get_board_revision(void);
unsigned int get_memory_base_addr(void);
unsigned int get_memory_size(void);

View File

@@ -1,8 +0,0 @@
#pragma once
extern const int _random_a;
extern const int _random_c;
extern const int _random_m;
extern int seed;
int random(void);

View File

@@ -1,15 +0,0 @@
#pragma once
#include <initrd.h>
#include <stddef.h>
void help(void);
void hello(void);
void hwinfo(void);
void memalloc(size_t size);
void ls(file_node_t *root);
void cat(file_node_t *root, const char *filename);
void reboot(void);
int // is continue
shell(file_node_t *);

View File

@@ -2,24 +2,11 @@
#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);
uint8_t uart_getb();
char uart_getc();
void uart_puts(const char *s);
void uart_hex(unsigned int d);
void uart_int(int d);
void uart_hex(uint64_t d);
extern int is_uart_init;

View File

@@ -1,31 +0,0 @@
#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) \
)

View File

@@ -1,18 +0,0 @@
#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)