new initrd format, 2nd layer for keypad

master
Clyne 7 years ago
parent 234beaee5d
commit 769b3dbcea

@ -21,7 +21,7 @@
CROSS = arm-none-eabi- CROSS = arm-none-eabi-
CC = gcc CC = gcc
AS = as AS = as
AR = ar AR = tools/rba
OBJCOPY = objcopy OBJCOPY = objcopy
STRIP = strip STRIP = strip
@ -46,11 +46,10 @@ INITRD = initrd.img
all: $(OUT) all: $(OUT)
#@$(CROSS)$(STRIP) --only-keep-debug $(OUT)
$(OUT): $(OFILES) initrd/init libinterp.a $(OUT): $(OFILES) initrd/init libinterp.a
@echo " INITRD " $(INITRD) @echo " INITRD " $(INITRD)
@rm -f $(INITRD) @rm -f $(INITRD)
@$(CROSS)$(AR) r $(INITRD) initrd/* @$(AR) $(INITRD) initrd/*
@$(CROSS)$(OBJCOPY) -B arm -I binary -O elf32-littlearm $(INITRD) out/initrd.img.o @$(CROSS)$(OBJCOPY) -B arm -I binary -O elf32-littlearm $(INITRD) out/initrd.img.o
@echo " LINK " $(OUT) @echo " LINK " $(OUT)
@$(CROSS)$(CC) $(CFLAGS) $(LFLAGS) out/*.o -o $(OUT) -L. -linterp -lm @$(CROSS)$(CC) $(CFLAGS) $(LFLAGS) out/*.o -o $(OUT) -L. -linterp -lm

@ -26,45 +26,31 @@
#include <stdint.h> #include <stdint.h>
/**
* Structure for the archive's header.
*/
typedef struct
{
char signature[8]; /**< The archive's signature */
} __attribute__ ((packed)) initrd_header;
/**
* Structure for a file entry in the archive.
*/
typedef struct
{
char name[16]; /**< The name of the file */
uint8_t unused[32]; /**< Unused information */
char size[10]; /**< The file's size in bytes (as string) */
char sig[2]; /**< A signature to start file data */
} __attribute__ ((packed)) initrd_file;
/** /**
* Confirms the initrd image is loaded and valid. * Confirms the initrd image is loaded and valid.
* @return non-zero if valid image found * @return non-zero if valid image found
*/ */
uint8_t initrd_validate(void); uint8_t initrd_validate(void);
/**
* Gets the file name of the index'th file in the archive.
* @param index the index of the file
* @return the file's name, or zero if not found
*/
char *initrd_getfile(uint32_t index);
/** /**
* Gets contents of the given file. * Gets contents of the given file.
* @param name the file's name * @param name the file's name
* @return pointer to file data, null if not found * @return pointer to file data, null if not found
*/ */
char *initrd_getfile(const char *name); char *initrd_readfile(const char *name);
/** /**
* Gets the size of the given file. * Gets the size of the given file.
* @param name the file's name * @param name the file's name
* @return the file's size, in bytes * @return the file's size, in bytes
*/ */
uint32_t initrd_getfilesize(const char *name); uint32_t initrd_filesize(const char *name);
char *initrd_getnfile(unsigned int index);
#endif // INITRD_H_ #endif // INITRD_H_

@ -1,42 +0,0 @@
#include <stdint.h>
#include <string.h>
extern uint8_t *initrd_start;
extern uint32_t initrd_size;
char *getfile(uint32_t offset)
{
char *ptr = initrd_start;
for (uint32_t i = 0; i < offset; i++) {
uint32_t len = *((uint32_t *)ptr);
uint32_t datalen = *((uint32_t *)(ptr + 4 + len));
ptr += len + datalen + 8;
if (ptr >= (char *)(initrd_start + initrd_size))
return 0;
}
return ptr;
}
char *readfile(const char *name)
{
char *ptr;
for (uint32_t i = 0; ptr = getfile(i), ptr != 0; i++) {
uint32_t len = *((uint32_t *)ptr);
if (!strncmp(name, ptr + 4, len))
return ptr + len + 8;
}
return 0;
}
uint32_t filesize(const char *name)
{
char *ptr;
for (uint32_t i = 0; ptr = getfile(i), ptr != 0; i++) {
uint32_t len = *((uint32_t *)ptr);
if (!strncmp(name, ptr + 4, len))
return *((uint32_t *)ptr + len + 4);
}
return 0;
}

Binary file not shown.

@ -1,9 +1,6 @@
/** /**
* @file initrd.c * @file initrd.c
* Initrd image support * Initrd image support
* An archive file (made with ar) can be linked into the final executable to
* allow files to be loaded in memory on boot. See mkinitrd.sh or the Makefile
* for more info.
* *
* Copyright (C) 2018 Clyne Sullivan * Copyright (C) 2018 Clyne Sullivan
* *
@ -21,110 +18,57 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/**
* The format of the initrd file is custom; made with the 'rba' utility.
*/
#include <initrd.h> #include <initrd.h>
#include <string.h>
extern uint8_t _binary_initrd_img_start[]; extern uint8_t _binary_initrd_img_start[];
extern uint8_t _binary_initrd_img_size[]; extern uint8_t _binary_initrd_img_size[];
static const void *initrd_start = (void *)_binary_initrd_img_start; static const uint8_t *initrd_start = (uint8_t *)_binary_initrd_img_start;
static const uint32_t initrd_size = (uint32_t)_binary_initrd_img_size; static const uint32_t initrd_size = (uint32_t)_binary_initrd_img_size;
static const char *initrd_sig = "!<arch>\n";
uint8_t initrd_validate(void) uint8_t initrd_validate(void)
{ {
initrd_header *header = (initrd_header *)initrd_start; return 1; // TODO maybe add header/signature to archiver?
for (uint8_t i = 0; i < 8; i++) {
if (header->signature[i] != initrd_sig[i])
return 0;
}
return 1;
} }
uint8_t initrd_nametest(char *file, const char *want) char *initrd_getfile(uint32_t offset)
{ {
for (uint8_t i = 0; i < 16; i++) { char *ptr = initrd_start;
if (want[i] == '\0') for (uint32_t i = 0; i < offset; i++) {
return (file[i] == '/'); uint32_t len = *((uint32_t *)ptr);
else if (want[i] != file[i]) uint32_t datalen = *((uint32_t *)(ptr + 4 + len));
ptr += len + datalen + 8;
if (ptr >= (char *)(initrd_start + initrd_size))
return 0; return 0;
} }
return 0; return ptr;
}
uint32_t ipow10(uint8_t n)
{
uint32_t i = 1;
while (n--)
i *= 10;
return i;
}
uint32_t initrd_getsize(initrd_file *file)
{
uint32_t size = 0;
char *p = file->size + 10;
while (*--p == ' ');
for (int8_t i = p - file->size, j = 0; i >= 0; i--, j++)
size += (*p-- - '0') * ipow10(j);
return size;
} }
initrd_file *initrd_getfileptr(const char *name) char *initrd_readfile(const char *name)
{ {
initrd_file *file = (initrd_file *)((uint8_t *)initrd_start + sizeof(initrd_header)); char *ptr;
uint32_t offset = sizeof(initrd_header); for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
uint32_t len = *((uint32_t *)ptr);
while (offset < initrd_size) { if (!strncmp(name, ptr + 4, len))
if (initrd_nametest(file->name, name)) return ptr + len + 8;
return file;
uint32_t size = initrd_getsize(file) + sizeof(initrd_file);
offset += size;
file = (initrd_file *)((uint8_t *)file + size + 1);
} }
return 0; return 0;
} }
char *initrd_getnfile(unsigned int index) uint32_t initrd_filesize(const char *name)
{ {
initrd_file *file = (initrd_file *)((uint8_t *)initrd_start + sizeof(initrd_header)); char *ptr;
uint32_t offset = sizeof(initrd_header); for (uint32_t i = 0; ptr = initrd_getfile(i), ptr != 0; i++) {
uint32_t len = *((uint32_t *)ptr);
for (unsigned int i = 0; i < index; i++) { if (!strncmp(name, ptr + 4, len))
uint32_t size = initrd_getsize(file) + sizeof(initrd_file); return *((uint32_t *)(ptr + len + 4));
offset += size;
file = (initrd_file *)((uint8_t *)file + size);
if (file->name[0] == '\n')
file = (initrd_file *)((uint32_t)file + 1);
} }
if ((uint32_t)file >= (uint32_t)initrd_start + initrd_size)
return 0;
return file->name;
}
char *initrd_getfile(const char *name)
{
initrd_file *file = initrd_getfileptr(name);
if (file == 0)
return 0; return 0;
char *ptr = (char *)file + sizeof(initrd_file);
ptr[initrd_getsize(file) - 1] = 0;
return ptr;
}
uint32_t initrd_getfilesize(const char *name)
{
initrd_file *file = initrd_getfileptr(name);
if (file == 0)
return 0;
return initrd_getsize(file);
} }

@ -53,16 +53,29 @@ static const port_t keypad_cols[COLS] = {
{ COL_3 }, { COL_4 } { COL_3 }, { COL_4 }
}; };
#define K_2ND 0x000000FF
#define K_HOLD 0x000001FF
static const char keypad_map[ROWS * COLS * 4] = { static const char keypad_map[ROWS * COLS * 4] = {
"&\0\0\0" "|\0\0\0" "pi\0\0" "==\0\0" "!=\0\0" "\xFF\0\0\0" ">\0\0\0" ">=\0\0" "==\0\0" "=\0\0\0"
"x\0\0\0" "y\0\0\0" "z\0\0\0" "=\0\0\0" "sin\0" "x\0\0\0" "<\0\0\0" "<=\0\0" "!=\0\0" "%\0\0\0"
"7\0\0\0" "8\0\0\0" "9\0\0\0" "(\0\0\0" ")\0\0\0" "7\0\0\0" "8\0\0\0" "9\0\0\0" "(\0\0\0" ")\0\0\0"
"4\0\0\0" "5\0\0\0" "6\0\0\0" "/\0\0\0" "%\0\0\0" "4\0\0\0" "5\0\0\0" "6\0\0\0" "/\0\0\0" "*\0\0\0"
"1\0\0\0" "2\0\0\0" "3\0\0\0" "*\0\0\0" "-\0\0\0" "1\0\0\0" "2\0\0\0" "3\0\0\0" "-\0\0\0" "+\0\0\0"
".\0\0\0" "0\0\0\0" "\b\0\0\0" "\n\0\0\0" "+\0\0\0" ".\0\0\0" "0\0\0\0" "\0\0\0\0" "\b\0\0\0" "\n\0\0\0"
};
static const char keypad_map_2nd[ROWS * COLS * 4] = {
"a\0\0\0" "b\0\0\0" "c\0\0\0" "d\0\0\0" "e\0\0\0"
"f\0\0\0" "g\0\0\0" "h\0\0\0" "i\0\0\0" "j\0\0\0"
"k\0\0\0" "l\0\0\0" "m\0\0\0" "n\0\0\0" "o\0\0\0"
"p\0\0\0" "q\0\0\0" "r\0\0\0" "s\0\0\0" "t\0\0\0"
"u\0\0\0" "v\0\0\0" "w\0\0\0" "x\0\0\0" "y\0\0\0"
"z\0\0\0" "\0\0\0\0" "\0\0\0\0" "\0\0\0\0" "\xFF\x01\0\0"
}; };
#define KEY(r, c, i) keypad_map[r * COLS * 4 + c * 4 + i] #define KEY(r, c, i) map[r * COLS * 4 + c * 4 + i]
#define KEYCODE(r, c) *((uint32_t *)(map + (r * COLS * 4 + c * 4)))
#define BUFFER_SIZE 8 #define BUFFER_SIZE 8
static char keypad_buffer[BUFFER_SIZE]; static char keypad_buffer[BUFFER_SIZE];
@ -71,12 +84,24 @@ static int keypad_buffer_pos = -1;
void keypad_task(void) void keypad_task(void)
{ {
unsigned int col = 0; unsigned int col = 0;
unsigned char use2nd = 0;
unsigned char hold = 0;
while (1) { while (1) {
gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1); gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1);
delay(10); delay(10);
for (unsigned int row = 0; row < ROWS; row++) { for (unsigned int row = 0; row < ROWS; row++) {
if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) { if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) {
if (keypad_buffer_pos < BUFFER_SIZE) { char *map = (use2nd == 0) ? keypad_map : keypad_map_2nd;
if (KEYCODE(row, col) == K_2ND) {
use2nd = 1;
} else if (KEYCODE(row, col) == K_HOLD) {
if (use2nd != 0) {
if ((hold ^= 1) == 0)
use2nd = 0;
}
} else if (keypad_buffer_pos < BUFFER_SIZE) {
if (use2nd != 0 && hold == 0)
use2nd = 0;
for (unsigned int i = 0; KEY(row, col, i) != '\0'; i++) for (unsigned int i = 0; KEY(row, col, i) != '\0'; i++)
keypad_buffer[++keypad_buffer_pos] = KEY(row, col, i); keypad_buffer[++keypad_buffer_pos] = KEY(row, col, i);
} }

@ -90,7 +90,7 @@ void task_interpreter(void)
instance *it = inewinstance(); instance *it = inewinstance();
script_loadlib(it); script_loadlib(it);
char *s = initrd_getfile("init"); char *s = initrd_readfile("init");
if (s == 0) { if (s == 0) {
dsp_puts("can't find init"); dsp_puts("can't find init");
goto end; goto end;
@ -98,7 +98,7 @@ void task_interpreter(void)
char *linebuf = (char *)malloc(120); char *linebuf = (char *)malloc(120);
uint32_t i = 0, prev = 0, lc; uint32_t i = 0, prev = 0, lc;
uint32_t size = initrd_getfilesize("init"); uint32_t size = initrd_filesize("init");
int ret = 0; int ret = 0;
while (i < size) { while (i < size) {
for (; s[i] != '\n' && s[i] != '\0'; i++); for (; s[i] != '\n' && s[i] != '\0'; i++);

@ -107,7 +107,7 @@ int script_filemenu(instance *it)
char *fname; char *fname;
strncpy(listbuf, " : \0", 4); strncpy(listbuf, " : \0", 4);
dsp_puts("Choose a file: \n"); dsp_puts("Choose a file: \n");
for (unsigned int i = 0; (fname = initrd_getnfile(i)) != 0; i++) { for (unsigned int i = 0; (fname = initrd_getfile(i)) != 0; i++) {
listbuf[0] = i + '0'; listbuf[0] = i + '0';
dsp_puts(listbuf); dsp_puts(listbuf);
dsp_puts(strncpy(buf, fname, 16)); dsp_puts(strncpy(buf, fname, 16));

@ -23,8 +23,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <heap.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h>
#include <string.h> #include <string.h>
extern char *itoa(int, char *, int); extern char *itoa(int, char *, int);
@ -61,7 +61,7 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...)
itoa(va_arg(args, unsigned int), nbuf, 16); itoa(va_arg(args, unsigned int), nbuf, 16);
break; break;
case 'f': case 'f':
itoa((int)va_arg(args, double), nbuf, 10); itoa(va_arg(args, double), nbuf, 10);
break; break;
default: default:
buf[off++] = format[i]; buf[off++] = format[i];

Loading…
Cancel
Save