Skip to content

Commit

Permalink
Untested DTB support
Browse files Browse the repository at this point in the history
  • Loading branch information
tangrs committed Apr 5, 2013
1 parent dd742a8 commit be0595b
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 21 deletions.
8 changes: 4 additions & 4 deletions atag.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static void* atag_begin(void *head) {
}

#define ATAG(...) do { \
if (current > (char*)settings.atag.start + settings.atag.size) { \
if (current > (char*)settings.boot_param.start + settings.boot_param.size) { \
printl("Internal error. ATAG buffer overrun" NEWLINE); \
return -1; \
} \
Expand All @@ -123,11 +123,11 @@ static void* atag_begin(void *head) {

int atag_build() {
char *current;
if (!settings.atag.start) {
printl("Internal error. settings.atag.start = NULL" NEWLINE);
if (!settings.boot_param.start) {
printl("Internal error. settings.boot_param.start = NULL" NEWLINE);
return -1;
}
current = atag_begin(settings.atag.start);
current = atag_begin(settings.boot_param.start);
/*
Begin building list of ATAGs
*/
Expand Down
1 change: 1 addition & 0 deletions cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ int process_cmd(char * cmd) {
*/
DEFINE_COMMAND(kernel, load_kernel);
DEFINE_COMMAND(initrd, load_ramdisk);
DEFINE_COMMAND(dtb, load_dtb);
DEFINE_COMMAND(dump, dump_settings);
DEFINE_COMMAND(free, show_mem);
DEFINE_COMMAND(mach, setget_mach);
Expand Down
4 changes: 2 additions & 2 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ void dump_settings(char * ignored __attribute__((unused))) {
FOOTER_LEVEL0();

HEADER_LEVEL0(atag);
DUMP_LEVEL1(settings.atag, start);
DUMP_LEVEL1(settings.atag, size);
DUMP_LEVEL1(settings.boot_param, start);
DUMP_LEVEL1(settings.boot_param, size);
FOOTER_LEVEL0();

HEADER_LEVEL0(mem_block);
Expand Down
8 changes: 5 additions & 3 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stddef.h>

#define PAGE_SIZE 0x1000
#define DTB_MACH_ID 3503

struct params {
struct {
Expand All @@ -34,9 +35,9 @@ struct params {
} ramdisk;

struct {
void* start; /* A separate section of memory for writing ATAGs */
void* start; /* A separate section of memory for writing ATAG and DTBs */
size_t size;
} atag;
} boot_param;

struct {
void* start; /* Memory block for kernel and ramdisk */
Expand All @@ -58,6 +59,7 @@ struct params {

unsigned ramdisk_loaded:1;
unsigned kernel_loaded:1;
unsigned dtb_loaded:1;
};

extern struct params settings;
Expand All @@ -70,4 +72,4 @@ void setget_rdisksize(char * arg);
void poke(char *arg);
void peek(char *arg);

#endif
#endif
6 changes: 4 additions & 2 deletions kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ void kernel_boot(char * ignored __attribute__((unused))) {

/* Kernels and ramdisks should already be loaded to their correct places */
/* Build atag next */
if (atag_build()) return;
if (!settings.dtb_loaded) {
if (atag_build()) return;
}

clear_cache();
/* Disable D-Cache and MMU */
Expand All @@ -48,6 +50,6 @@ void kernel_boot(char * ignored __attribute__((unused))) {
"mcr p15, 0, r0, c1, c0,0 \n"
: : : "r0" );
/* Bye bye */
entry(0, settings.machine_id, settings.atag.start);
entry(0, settings.machine_id, settings.boot_param.start);
__builtin_unreachable();
}
27 changes: 27 additions & 0 deletions load.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,30 @@ void load_ramdisk(const char *filename) {
printl("Ramdisk successfully loaded" NEWLINE);
return;
}

void load_dtb(const char *filename) {
FILE* f;
size_t dtb_size = file_size(filename);

if (dtb_size > settings.boot_param.size) {
printl("DTB image too large!" NEWLINE);
return;
}

f = fopen(filename, "rb");
if (!f) {
printl("Failed to open dtb image %s" NEWLINE, filename);
return;
}

if (fread(settings.boot_param.start, 1, dtb_size, f) != dtb_size)
printl("Warning: read less data from file than expected" NEWLINE);

fclose(f);

settings.dtb_loaded = 1;
settings.machine_id = DTB_MACH_ID;

printl("DTB successfully loaded" NEWLINE);
return;
}
3 changes: 2 additions & 1 deletion load.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

void load_kernel(const char *filename);
void load_ramdisk(const char *filename);
void load_dtb(const char *filename);

#endif
#endif
5 changes: 4 additions & 1 deletion mach.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ int detect_machine() {
settings.machine_id = 4443;
break;
default:
printl("No machine ID for this platform" NEWLINE);
printl( "No machine ID for this platform." NEWLINE
"Defaulting to DTB machine number %d." NEWLINE,
DTB_MACH_ID);
settings.machine_id = DTB_MACH_ID;
return -1;
}
return 0;
Expand Down
13 changes: 7 additions & 6 deletions memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ void show_mem(char *ignored __attribute__((unused))) {
#undef HUMAN

void free_memory() {
free(settings.atag.start);
free(settings.boot_param.start);
free(settings.mem_block.start);

settings.atag.start = NULL;
settings.boot_param.start = NULL;
settings.mem_block.start = NULL;

settings.atag.size = 0;
settings.boot_param.size = 0;
settings.mem_block.size = 0;

settings.kernel.size = 0;
Expand All @@ -98,14 +98,15 @@ void free_memory() {
void alloc_memory() {
free_memory();

settings.atag.start = malloc(MAX_ATAG_SIZE);
if (settings.atag.start) settings.atag.size = MAX_ATAG_SIZE;
settings.boot_param.start = malloc(MAX_BOOT_PARAM_SIZE);
if (settings.boot_param.start)
settings.boot_param.size = MAX_BOOT_PARAM_SIZE;

/* Allocate as much remaining memory as possible */
settings.mem_block.start = max_malloc(&settings.mem_block.size);

printl( "Allocated memory:" NEWLINE
" ATAGs: %u bytes" NEWLINE
" Kernel and ramdisk: %u bytes" NEWLINE,
settings.atag.size, settings.mem_block.size);
settings.boot_param.size, settings.mem_block.size);
}
4 changes: 2 additions & 2 deletions memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "common.h"

#define MAX_ATAG_SIZE (0x8000-0x100)
#define MAX_BOOT_PARAM_SIZE (0x4000-0x100)

#define mem_block_size_free() (settings.mem_block.size - settings.kernel.size - settings.ramdisk.size)

Expand All @@ -31,4 +31,4 @@ void alloc_memory();
void free_memory();
void show_mem(char*);

#endif
#endif

0 comments on commit be0595b

Please sign in to comment.