initial commit
This commit is contained in:
88
lib/initrd.c
Normal file
88
lib/initrd.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <stddef.h>
|
||||
#include <initrd.h>
|
||||
#include <kmalloc.h>
|
||||
#include <random.h>
|
||||
#include <string.h>
|
||||
|
||||
#define nullnode ((file_node_t *)0)
|
||||
|
||||
void _init_node(file_node_t *node) {
|
||||
node->l = nullnode;
|
||||
node->r = nullnode;
|
||||
node->rand = random();
|
||||
node->node_size = 1;
|
||||
}
|
||||
|
||||
void _pull_from(file_node_t *to, file_node_t *from)
|
||||
{
|
||||
if (!from)
|
||||
return;
|
||||
|
||||
to->node_size += from->node_size;
|
||||
}
|
||||
|
||||
void _pull(file_node_t *node)
|
||||
{
|
||||
node->node_size = 1;
|
||||
_pull_from(node, node->l);
|
||||
_pull_from(node, node->r);
|
||||
}
|
||||
|
||||
file_node_t *_merge(file_node_t *a, file_node_t *b)
|
||||
{
|
||||
if (!a || !b)
|
||||
return a ?: b;
|
||||
|
||||
if (a->rand < b->rand) {
|
||||
a->r = _merge(a->r, b);
|
||||
_pull(a);
|
||||
return a;
|
||||
}
|
||||
b->l = _merge(a, b->l);
|
||||
_pull(b);
|
||||
return b;
|
||||
}
|
||||
|
||||
void _split(file_node_t *rt, const char *s, file_node_t **a, file_node_t **b)
|
||||
{
|
||||
if (!rt) {
|
||||
*a = *b = nullnode;
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp(s, rt->filename) < 0) {
|
||||
*a = rt;
|
||||
_split((*a)->r, s, &(*a)->r, b);
|
||||
_pull(*a);
|
||||
} else {
|
||||
*b = rt;
|
||||
_split((*b)->l, s, a, &(*b)->l);
|
||||
_pull(*b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
file_node_t *initrd_init(void)
|
||||
{
|
||||
// TODO
|
||||
|
||||
return nullnode;
|
||||
}
|
||||
|
||||
file_node_t *_node_bs(file_node_t *cur, const char *s)
|
||||
{
|
||||
if (!cur)
|
||||
return nullnode;
|
||||
|
||||
int cmp = strcmp(s, cur->filename);
|
||||
if (cmp < 0)
|
||||
return _node_bs(cur->r, s);
|
||||
if (cmp > 0)
|
||||
return _node_bs(cur->l, s);
|
||||
return cur; // cmp == 0
|
||||
}
|
||||
|
||||
file_node_t *initrd_get(file_node_t *root, const char *filename)
|
||||
{
|
||||
return _node_bs(root, filename);
|
||||
}
|
||||
37
lib/kmalloc.c
Normal file
37
lib/kmalloc.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <kmalloc.h>
|
||||
#include <stddef.h>
|
||||
|
||||
extern void *__heap_start;
|
||||
extern void *__heap_end;
|
||||
|
||||
void *_heap_top = (void *)0;
|
||||
|
||||
// simple 8-byte aligned linear allocation
|
||||
void *simple_alloc(size_t size)
|
||||
{
|
||||
if (!_heap_top) {
|
||||
_heap_top = __heap_start;
|
||||
}
|
||||
|
||||
if (size & 0xff)
|
||||
size = (size & ~0xff) + 0x100;
|
||||
|
||||
if ((uint64_t)_heap_top + size >= (uint64_t)__heap_end)
|
||||
return (void *)0;
|
||||
|
||||
void *ret = _heap_top;
|
||||
_heap_top = (void *)((uint64_t)_heap_top + size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *kmalloc(size_t size)
|
||||
{
|
||||
return simple_alloc(size);
|
||||
}
|
||||
|
||||
void kfree(void *ptr)
|
||||
{
|
||||
// not implemented for now
|
||||
return;
|
||||
}
|
||||
89
lib/mbox.c
Normal file
89
lib/mbox.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <gpio.h>
|
||||
#include <mbox.h>
|
||||
|
||||
/* mailbox message buffer */
|
||||
volatile unsigned int __attribute__((aligned(16))) mbox[36];
|
||||
|
||||
#define VIDEOCORE_MBOX (MMIO_BASE+0x0000B880)
|
||||
#define MBOX_READ ((volatile unsigned int*)(VIDEOCORE_MBOX+0x0))
|
||||
#define MBOX_POLL ((volatile unsigned int*)(VIDEOCORE_MBOX+0x10))
|
||||
#define MBOX_SENDER ((volatile unsigned int*)(VIDEOCORE_MBOX+0x14))
|
||||
#define MBOX_STATUS ((volatile unsigned int*)(VIDEOCORE_MBOX+0x18))
|
||||
#define MBOX_CONFIG ((volatile unsigned int*)(VIDEOCORE_MBOX+0x1C))
|
||||
#define MBOX_WRITE ((volatile unsigned int*)(VIDEOCORE_MBOX+0x20))
|
||||
#define MBOX_RESPONSE 0x80000000
|
||||
#define MBOX_FULL 0x80000000
|
||||
#define MBOX_EMPTY 0x40000000
|
||||
|
||||
#define MAILBOX_BASE MMIO_BASE + 0xb880
|
||||
|
||||
#define REQUEST_CODE 0x00000000
|
||||
#define REQUEST_SUCCEED 0x80000000
|
||||
#define REQUEST_FAILED 0x80000001
|
||||
#define TAG_REQUEST_CODE 0x00000000
|
||||
#define END_TAG 0x00000000
|
||||
|
||||
/**
|
||||
* Make a mailbox call. Returns 0 on failure, non-zero on success
|
||||
*/
|
||||
int mbox_call(unsigned char ch)
|
||||
{
|
||||
unsigned int r = (((unsigned int)((unsigned long)&mbox)&~0xF) | (ch&0xF));
|
||||
/* wait until we can write to the mailbox */
|
||||
do{asm volatile("nop");}while(*MBOX_STATUS & MBOX_FULL);
|
||||
/* write the address of our message to the mailbox with channel identifier */
|
||||
*MBOX_WRITE = r;
|
||||
/* now wait for the response */
|
||||
while(1) {
|
||||
/* is there a response? */
|
||||
do{asm volatile("nop");}while(*MBOX_STATUS & MBOX_EMPTY);
|
||||
/* is it a response to our message? */
|
||||
if(r == *MBOX_READ)
|
||||
/* is it a valid successful response? */
|
||||
return mbox[1]==MBOX_RESPONSE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int get_board_revision(void)
|
||||
{
|
||||
mbox[0] = 36 * 4;
|
||||
mbox[1] = REQUEST_CODE;
|
||||
|
||||
// tags
|
||||
mbox[2] = MBOX_TAG_BOARD_REVISION;
|
||||
mbox[3] = 4;
|
||||
mbox[4] = TAG_REQUEST_CODE;
|
||||
mbox[5] = 0; // value buffer
|
||||
|
||||
mbox[6] = END_TAG;
|
||||
|
||||
mbox_call(MBOX_CH_PROP);
|
||||
|
||||
return mbox[5];
|
||||
}
|
||||
12
lib/random.c
Normal file
12
lib/random.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <random.h>
|
||||
|
||||
const int _random_a = 100003;
|
||||
const int _random_c = 114514 + 33;
|
||||
const int _random_m = 1000000007;
|
||||
int seed = 0;
|
||||
|
||||
int random(void)
|
||||
{
|
||||
seed = _random_a * (seed + _random_c) % _random_m;
|
||||
return seed;
|
||||
}
|
||||
75
lib/shell.c
Normal file
75
lib/shell.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <uart.h>
|
||||
#include <mbox.h>
|
||||
#include <shell.h>
|
||||
|
||||
#define INPUT_BUFLEN 1000
|
||||
|
||||
void help (void)
|
||||
{
|
||||
uart_puts(
|
||||
"help : print this help menu" ENDL
|
||||
"hello : print Hello World!" ENDL
|
||||
"hwinfo: print hardware info" ENDL
|
||||
"reboot: reboot the device" ENDL
|
||||
);
|
||||
}
|
||||
|
||||
void hello (void)
|
||||
{
|
||||
uart_puts("hello, world" ENDL);
|
||||
}
|
||||
|
||||
void hwinfo (void)
|
||||
{
|
||||
unsigned int val = get_board_revision();
|
||||
uart_puts("hwinfo: ");
|
||||
uart_hex(val);
|
||||
uart_puts(ENDL);
|
||||
}
|
||||
|
||||
#define PM_PASSWORD 0x5a000000
|
||||
#define PM_RSTC 0x3F10001c
|
||||
#define PM_WDOG 0x3F100024
|
||||
|
||||
void set(long addr, unsigned int value) {
|
||||
volatile unsigned int* point = (unsigned int*)addr;
|
||||
*point = value;
|
||||
}
|
||||
|
||||
void reset(int tick) { // reboot after watchdog timer expire
|
||||
set(PM_RSTC, PM_PASSWORD | 0x20); // full reset
|
||||
set(PM_WDOG, PM_PASSWORD | tick); // number of watchdog tick
|
||||
}
|
||||
|
||||
void reboot(void)
|
||||
{
|
||||
reset(1 << 16);
|
||||
}
|
||||
|
||||
int shell (void)
|
||||
{
|
||||
uart_puts("# ");
|
||||
|
||||
char buf[INPUT_BUFLEN], ch;
|
||||
int sz = 0;
|
||||
while ((ch = uart_getc()) != '\n') {
|
||||
buf[sz++] = ch;
|
||||
uart_send(ch);
|
||||
}
|
||||
uart_puts(ENDL);
|
||||
buf[sz] = '\0';
|
||||
|
||||
if (strcmp(buf, "help") == 0) {
|
||||
help();
|
||||
} else if (strcmp(buf, "hello") == 0) {
|
||||
hello();
|
||||
} else if (strcmp(buf, "hwinfo") == 0) {
|
||||
hwinfo();
|
||||
} else if (strcmp(buf, "reboot") == 0) {
|
||||
reboot();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
35
lib/string.c
Normal file
35
lib/string.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
int strcmp(const char *a, const char *b)
|
||||
{
|
||||
while (*a != '\0' && *b != '\0') {
|
||||
if (*a != *b)
|
||||
return (*a > *b) - (*a < *b);
|
||||
a++, b++;
|
||||
}
|
||||
if (*a == '\0' && *b == '\0')
|
||||
return 0;
|
||||
|
||||
if (*a == '\0')
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *memcpy(void *dest, const void *src, size_t count)
|
||||
{
|
||||
void *ret = dest;
|
||||
for (const byte_t *p = src; count > 0; --count, ++p, ++dest)
|
||||
*(byte_t *)dest = *p;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void *memzero(void *start, void *end)
|
||||
{
|
||||
void *ret = start;
|
||||
for (byte_t *p = start; p < (byte_t *)end; ++p)
|
||||
*p = 0;
|
||||
|
||||
return ret;
|
||||
}
|
||||
103
lib/uart.c
Normal file
103
lib/uart.c
Normal file
@@ -0,0 +1,103 @@
|
||||
#include <uart.h>
|
||||
#include <gpio.h>
|
||||
|
||||
/* Auxilary mini UART registers */
|
||||
#define AUX_ENABLE ((volatile unsigned int*)(MMIO_BASE+0x00215004))
|
||||
#define AUX_MU_IO ((volatile unsigned int*)(MMIO_BASE+0x00215040))
|
||||
#define AUX_MU_IER ((volatile unsigned int*)(MMIO_BASE+0x00215044))
|
||||
#define AUX_MU_IIR ((volatile unsigned int*)(MMIO_BASE+0x00215048))
|
||||
#define AUX_MU_LCR ((volatile unsigned int*)(MMIO_BASE+0x0021504C))
|
||||
#define AUX_MU_MCR ((volatile unsigned int*)(MMIO_BASE+0x00215050))
|
||||
#define AUX_MU_LSR ((volatile unsigned int*)(MMIO_BASE+0x00215054))
|
||||
#define AUX_MU_MSR ((volatile unsigned int*)(MMIO_BASE+0x00215058))
|
||||
#define AUX_MU_SCRATCH ((volatile unsigned int*)(MMIO_BASE+0x0021505C))
|
||||
#define AUX_MU_CNTL ((volatile unsigned int*)(MMIO_BASE+0x00215060))
|
||||
#define AUX_MU_STAT ((volatile unsigned int*)(MMIO_BASE+0x00215064))
|
||||
#define AUX_MU_BAUD ((volatile unsigned int*)(MMIO_BASE+0x00215068))
|
||||
|
||||
/**
|
||||
* Set baud rate and characteristics (115200 8N1) and map to GPIO
|
||||
*/
|
||||
void uart_init()
|
||||
{
|
||||
register unsigned int r;
|
||||
|
||||
/* initialize UART */
|
||||
*AUX_ENABLE |=1; // enable UART1, AUX mini uart
|
||||
*AUX_MU_IER = 0;
|
||||
*AUX_MU_CNTL = 0;
|
||||
*AUX_MU_LCR = 3; // 8 bits
|
||||
*AUX_MU_MCR = 0;
|
||||
*AUX_MU_IER = 0;
|
||||
*AUX_MU_IIR = 0xc6; // disable interrupts
|
||||
*AUX_MU_BAUD = 270; // 115200 baud
|
||||
/* map UART1 to GPIO pins */
|
||||
r=*GPFSEL1;
|
||||
r&=~((7<<12)|(7<<15)); // gpio14, gpio15
|
||||
r|=(2<<12)|(2<<15); // alt5
|
||||
*GPFSEL1 = r;
|
||||
*GPPUD = 0; // enable pins 14 and 15
|
||||
r=150; while(r--) { asm volatile("nop"); }
|
||||
*GPPUDCLK0 = (1<<14)|(1<<15);
|
||||
r=150; while(r--) { asm volatile("nop"); }
|
||||
*GPPUDCLK0 = 0; // flush GPIO setup
|
||||
*AUX_MU_CNTL = 3; // enable Tx, Rx
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a character
|
||||
*/
|
||||
void uart_send(unsigned int c) {
|
||||
/* wait until we can send */
|
||||
do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x20));
|
||||
/* write the character to the buffer */
|
||||
*AUX_MU_IO=c;
|
||||
}
|
||||
|
||||
byte_t uart_getb()
|
||||
{
|
||||
byte_t r;
|
||||
/* wait until something is in the buffer */
|
||||
do{asm volatile("nop");}while(!(*AUX_MU_LSR&0x01));
|
||||
/* read it and return */
|
||||
r=(byte_t)(*AUX_MU_IO);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a character
|
||||
*/
|
||||
char uart_getc() {
|
||||
char r = (char)uart_getb();
|
||||
|
||||
/* convert carrige return to newline */
|
||||
return r=='\r'?'\n':r;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a string
|
||||
*/
|
||||
void uart_puts(char *s) {
|
||||
while(*s) {
|
||||
/* convert newline to carrige return + newline */
|
||||
if(*s=='\n')
|
||||
uart_send('\r');
|
||||
uart_send(*s++);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a binary value in hexadecimal
|
||||
*/
|
||||
void uart_hex(unsigned int d) {
|
||||
unsigned int n;
|
||||
int c;
|
||||
for(c=28;c>=0;c-=4) {
|
||||
// get highest tetrad
|
||||
n=(d>>c)&0xF;
|
||||
// 0-9 => '0'-'9', 10-15 => 'A'-'F'
|
||||
n+=n>9?0x37:0x30;
|
||||
uart_send(n);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user