Skip to content

Commit

Permalink
Fix initrd loading bug. Renamed ramdisk to initrd to make clear disti…
Browse files Browse the repository at this point in the history
…nction between ramdisks and initrds to prevent future programming errors
  • Loading branch information
tangrs committed Apr 7, 2013
1 parent 3cca837 commit 83945d5
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 54 deletions.
8 changes: 4 additions & 4 deletions atag.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ int atag_build() {
ATAG(ATAG_MEM, settings.phys.size, settings.phys.start);
if (strlen(settings.kernel_cmdline))
ATAG(ATAG_CMDLINE, settings.kernel_cmdline);
if (settings.ramdisk_loaded)
ATAG(ATAG_INITRD2, settings.ramdisk.addr, settings.ramdisk.size);
if (settings.kernel_ramdisk_size)
ATAG(ATAG_RAMDISK, 0, settings.kernel_ramdisk_size, 0);
if (settings.initrd_loaded)
ATAG(ATAG_INITRD2, settings.initrd.addr, settings.initrd.size);
if (settings.ramdisk_size)
ATAG(ATAG_RAMDISK, 0, settings.ramdisk_size, 0);

ATAG(ATAG_SERIAL, settings.serialnr[0], settings.serialnr[1]);
ATAG(ATAG_REVISION, settings.rev);
Expand Down
2 changes: 1 addition & 1 deletion cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int process_cmd(char * cmd) {
pointer to arguments
*/
DEFINE_COMMAND(kernel, load_kernel);
DEFINE_COMMAND(initrd, load_ramdisk);
DEFINE_COMMAND(initrd, load_initrd);
DEFINE_COMMAND(dtb, load_dtb);
DEFINE_COMMAND(dump, dump_settings);
DEFINE_COMMAND(free, show_mem);
Expand Down
14 changes: 7 additions & 7 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void setget_phys(char * arg) {
void setget_rdisksize(char * arg) {
unsigned num;
if ( (num = strtoul(arg, NULL, 16)) ) {
settings.kernel_ramdisk_size = num;
settings.ramdisk_size = num;
}
printl("Kernel RAMDISK size set to %uK" NEWLINE, settings.kernel_ramdisk_size);
printl("Kernel RAMDISK size set to %uK" NEWLINE, settings.ramdisk_size);
}

void peek(char * arg) {
Expand Down Expand Up @@ -105,9 +105,9 @@ void dump_settings(char * ignored __attribute__((unused))) {
DUMP_LEVEL1(settings.kernel, size);
FOOTER_LEVEL0();

HEADER_LEVEL0(ramdisk);
DUMP_LEVEL1(settings.ramdisk, addr);
DUMP_LEVEL1(settings.ramdisk, size);
HEADER_LEVEL0(initrd);
DUMP_LEVEL1(settings.initrd, addr);
DUMP_LEVEL1(settings.initrd, size);
FOOTER_LEVEL0();

HEADER_LEVEL0(atag);
Expand All @@ -127,8 +127,8 @@ void dump_settings(char * ignored __attribute__((unused))) {

DUMP_LEVEL0(settings, machine_id);
DUMP_LEVEL0(settings, rev);
DUMP_LEVEL0(settings, kernel_ramdisk_size);
DUMP_LEVEL0(settings, ramdisk_loaded);
DUMP_LEVEL0(settings, ramdisk_size);
DUMP_LEVEL0(settings, initrd_loaded);
DUMP_LEVEL0(settings, kernel_loaded);
DUMP_LEVEL0(settings, dtb_loaded);

Expand Down
10 changes: 5 additions & 5 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ struct params {
} kernel;

struct {
void* addr; /* Where the ramdisk is loaded (address should be in range of mem_block) */
void* addr; /* Where the initrd is loaded (address should be in range of mem_block) */
size_t size;
} ramdisk;
} initrd;

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

struct {
void* start; /* Memory block for kernel and ramdisk */
void* start; /* Memory block for kernel and initrd */
size_t size;
} mem_block;

Expand All @@ -55,9 +55,9 @@ struct params {

unsigned serialnr[2];
unsigned rev;
unsigned kernel_ramdisk_size;
unsigned ramdisk_size;

unsigned ramdisk_loaded:1;
unsigned initrd_loaded:1;
unsigned kernel_loaded:1;
unsigned dtb_loaded:1;
};
Expand Down
10 changes: 5 additions & 5 deletions fdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int update_fdt()
{
static void* fdt = 0;

if(!fdt)
if(!fdt)
fdt = malloc(FDT_SIZE_MAX + 7);

if(!fdt) {
Expand Down Expand Up @@ -47,10 +47,10 @@ int update_fdt()
}

//UNTESTED (but doesn't hurt)
if(settings.ramdisk_loaded) {
unsigned ramdisk_end = settings.ramdisk.addr + settings.kernel_ramdisk_size;
if(fdt_setprop_cell(fdt, chosen, "linux,initrd-start", (unsigned)settings.ramdisk.addr) < 0
|| fdt_setprop_cell(fdt, chosen, "linux,initrd-end", ramdisk_end) < 0) {
if(settings.initrd_loaded) {
unsigned initrd_end = settings.initrd.addr + settings.initrd.size;
if(fdt_setprop_cell(fdt, chosen, "linux,initrd-start", (unsigned)settings.initrd.addr) < 0
|| fdt_setprop_cell(fdt, chosen, "linux,initrd-end", initrd_end) < 0) {
printl("Failed to set ramdisk location!" NEWLINE);
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void kernel_boot(char * ignored __attribute__((unused))) {
return;
}

/* Kernels and ramdisks should already be loaded to their correct places */
/* Kernels and initrds should already be loaded to their correct places */
/* Build atag next */
if (!settings.dtb_loaded) {
if (atag_build()) return;
Expand Down
46 changes: 23 additions & 23 deletions load.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,53 +62,53 @@ void load_kernel(const char *filename) {
return;
}

void load_ramdisk(const char *filename) {
void load_initrd(const char *filename) {
FILE* f;
size_t ramdisk_size = file_size(filename);
size_t size_free_memory = mem_block_size_free() - settings.ramdisk.size;
void* ramdisk_laddr = ((char*)settings.mem_block.start + settings.mem_block.size - ramdisk_size);
ramdisk_laddr = ROUND_PAGE_BOUND(ramdisk_laddr);
size_t initrd_size = file_size(filename);
size_t size_free_memory = mem_block_size_free() - settings.initrd.size;
void* initrd_laddr = ((char*)settings.mem_block.start + settings.mem_block.size - initrd_size);
initrd_laddr = ROUND_PAGE_BOUND(initrd_laddr);
size_t needed_size = ((char*)settings.mem_block.start + settings.mem_block.size)
- (char*)ramdisk_laddr;
- (char*)initrd_laddr;

if (!strlen(filename) && settings.ramdisk_loaded) {
settings.ramdisk_loaded = 0;
settings.ramdisk.addr = NULL;
settings.ramdisk.size = 0;
printl("Unloaded ramdisk" NEWLINE);
if (!strlen(filename) && settings.initrd_loaded) {
settings.initrd_loaded = 0;
settings.initrd.addr = NULL;
settings.initrd.size = 0;
printl("Unloaded initrd" NEWLINE);
return;
}

if (!ramdisk_size) {
printl("Ramdisk doesn't exist or empty" NEWLINE);
if (!initrd_size) {
printl("Initrd doesn't exist or empty" NEWLINE);
return;
}

if (needed_size > size_free_memory) {
printl( "Ramdisk too large!" NEWLINE
"Tried to load ramdisk needing %u bytes into %u bytes of free space" NEWLINE
"Original ramdisk size was %u bytes" NEWLINE,
needed_size, size_free_memory, ramdisk_size);
printl( "Initrd too large!" NEWLINE
"Tried to load initrd needing %u bytes into %u bytes of free space" NEWLINE
"Original initrd size was %u bytes" NEWLINE,
needed_size, size_free_memory, initrd_size);
return;
}

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

settings.ramdisk.addr = ramdisk_laddr;
settings.ramdisk.size = needed_size;
settings.initrd.addr = initrd_laddr;
settings.initrd.size = needed_size;

if (fread(settings.ramdisk.addr, 1, ramdisk_size, f) != ramdisk_size)
if (fread(settings.initrd.addr, 1, initrd_size, f) != initrd_size)
printl("Warning: read less data from file than expected" NEWLINE);

fclose(f);

settings.ramdisk_loaded = !!(settings.ramdisk.size);
settings.initrd_loaded = !!(settings.initrd.size);

printl("Ramdisk successfully loaded" NEWLINE);
printl("Initrd successfully loaded" NEWLINE);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion load.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#define LOAD_H

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

#endif
12 changes: 6 additions & 6 deletions memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ void show_mem(char *ignored __attribute__((unused))) {
HUMAN(settings.mem_block.size);
printl("" NEWLINE);

printl("Ramdisk: ");
HUMAN(settings.ramdisk.size);
printl("Initrd: ");
HUMAN(settings.initrd.size);
printl("/");
HUMAN(settings.mem_block.size);
printl("" NEWLINE);
Expand All @@ -94,11 +94,11 @@ void free_memory() {
settings.kernel.size = 0;
settings.kernel.addr = 0;

settings.ramdisk.size = 0;
settings.ramdisk.addr = 0;
settings.initrd.size = 0;
settings.initrd.addr = 0;

settings.kernel_loaded = 0;
settings.ramdisk_loaded = 0;
settings.initrd_loaded = 0;
}

void alloc_memory() {
Expand All @@ -113,6 +113,6 @@ void alloc_memory() {

printl( "Allocated memory:" NEWLINE
" DTB/ATAGs: %u bytes" NEWLINE
" Kernel and ramdisk: %u bytes" NEWLINE,
" Kernel and initrd: %u bytes" NEWLINE,
settings.boot_param.size, settings.mem_block.size);
}
2 changes: 1 addition & 1 deletion memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#define MAX_BOOT_PARAM_SIZE (0x4000-0x100)

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


void alloc_memory();
Expand Down

0 comments on commit 83945d5

Please sign in to comment.