2015-04-22 01:31:31 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <generated/csr.h>
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
#include <dyld.h>
|
|
|
|
|
|
|
|
#include "kloader.h"
|
2015-04-22 01:31:31 +08:00
|
|
|
#include "log.h"
|
2015-05-01 13:49:26 +08:00
|
|
|
#include "flash_storage.h"
|
2015-04-22 01:31:31 +08:00
|
|
|
#include "mailbox.h"
|
2015-07-25 16:26:04 +08:00
|
|
|
#include "messages.h"
|
2015-04-22 01:31:31 +08:00
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
static void start_kernel_cpu(struct msg_load_request *msg)
|
|
|
|
{
|
|
|
|
// Stop kernel CPU before messing with its code.
|
|
|
|
kernel_cpu_reset_write(1);
|
|
|
|
|
|
|
|
// Load kernel support code.
|
|
|
|
extern void _binary_ksupport_elf_start, _binary_ksupport_elf_end;
|
|
|
|
memcpy((void *)(KERNELCPU_EXEC_ADDRESS - KSUPPORT_HEADER_SIZE),
|
|
|
|
&_binary_ksupport_elf_start,
|
|
|
|
&_binary_ksupport_elf_end - &_binary_ksupport_elf_start);
|
|
|
|
|
|
|
|
// Start kernel CPU.
|
|
|
|
mailbox_send(msg);
|
|
|
|
kernel_cpu_reset_write(0);
|
|
|
|
}
|
2015-04-22 01:31:31 +08:00
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
void kloader_start_bridge()
|
2015-04-22 01:31:31 +08:00
|
|
|
{
|
2015-08-02 11:41:05 +08:00
|
|
|
start_kernel_cpu(NULL);
|
2015-04-22 01:31:31 +08:00
|
|
|
}
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
static int load_or_start_kernel(void *library, const char *kernel)
|
2015-04-22 01:31:31 +08:00
|
|
|
{
|
2015-08-02 11:41:05 +08:00
|
|
|
static struct dyld_info library_info;
|
|
|
|
struct msg_load_request request = {
|
|
|
|
.library = library,
|
|
|
|
.library_info = &library_info,
|
|
|
|
.kernel = kernel,
|
|
|
|
};
|
|
|
|
start_kernel_cpu(&request);
|
|
|
|
|
|
|
|
struct msg_load_reply *reply = mailbox_wait_and_receive();
|
|
|
|
if(reply != NULL && reply->type == MESSAGE_TYPE_LOAD_REPLY) {
|
|
|
|
log("cannot load/run kernel: %s", reply->error);
|
2015-04-22 01:31:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
int kloader_load_library(void *library)
|
2015-04-22 01:31:31 +08:00
|
|
|
{
|
|
|
|
if(!kernel_cpu_reset_read()) {
|
2015-08-02 11:41:05 +08:00
|
|
|
log("BUG: attempted to load kernel library while kernel CPU is running");
|
2015-04-22 01:31:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
return load_or_start_kernel(library, NULL);
|
2015-04-22 01:31:31 +08:00
|
|
|
}
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
int kloader_start_kernel(const char *name)
|
2015-04-22 01:31:31 +08:00
|
|
|
{
|
2015-08-02 11:41:05 +08:00
|
|
|
return load_or_start_kernel(NULL, name);
|
2015-04-22 01:31:31 +08:00
|
|
|
}
|
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
int kloader_start_idle_kernel(void)
|
2015-04-22 01:31:31 +08:00
|
|
|
{
|
2015-08-02 11:41:05 +08:00
|
|
|
#if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE)
|
2015-05-01 13:49:26 +08:00
|
|
|
char buffer[32*1024];
|
2015-08-02 11:41:05 +08:00
|
|
|
int length;
|
2015-05-01 13:49:26 +08:00
|
|
|
|
2015-08-02 11:41:05 +08:00
|
|
|
length = fs_read("idle_kernel", buffer, sizeof(buffer), NULL);
|
|
|
|
if(length <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return load_or_start_kernel(buffer, "test.__modinit__");
|
|
|
|
#else
|
|
|
|
return 0;
|
2015-05-02 12:20:20 +08:00
|
|
|
#endif
|
2015-04-22 01:31:31 +08:00
|
|
|
}
|
|
|
|
|
2015-05-09 17:11:34 +08:00
|
|
|
void kloader_stop(void)
|
2015-04-22 01:31:31 +08:00
|
|
|
{
|
|
|
|
kernel_cpu_reset_write(1);
|
2015-04-29 12:57:09 +08:00
|
|
|
mailbox_acknowledge();
|
2015-04-22 01:31:31 +08:00
|
|
|
}
|
2015-07-25 16:26:04 +08:00
|
|
|
|
|
|
|
int kloader_validate_kpointer(void *p)
|
|
|
|
{
|
|
|
|
unsigned int v = (unsigned int)p;
|
2015-08-02 11:41:05 +08:00
|
|
|
if((v < KERNELCPU_EXEC_ADDRESS) || (v > KERNELCPU_LAST_ADDRESS)) {
|
2015-07-25 16:26:04 +08:00
|
|
|
log("Received invalid pointer from kernel CPU: 0x%08x", v);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kloader_is_essential_kmsg(int msgtype)
|
|
|
|
{
|
|
|
|
switch(msgtype) {
|
|
|
|
case MESSAGE_TYPE_NOW_INIT_REQUEST:
|
|
|
|
case MESSAGE_TYPE_NOW_SAVE:
|
|
|
|
case MESSAGE_TYPE_LOG:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long long int now;
|
|
|
|
|
|
|
|
void kloader_service_essential_kmsg(void)
|
|
|
|
{
|
|
|
|
struct msg_base *umsg;
|
|
|
|
|
|
|
|
umsg = mailbox_receive();
|
|
|
|
if(umsg) {
|
|
|
|
if(!kloader_validate_kpointer(umsg))
|
|
|
|
return;
|
|
|
|
switch(umsg->type) {
|
|
|
|
case MESSAGE_TYPE_NOW_INIT_REQUEST: {
|
|
|
|
struct msg_now_init_reply reply;
|
|
|
|
|
|
|
|
reply.type = MESSAGE_TYPE_NOW_INIT_REPLY;
|
|
|
|
reply.now = now;
|
|
|
|
mailbox_send_and_wait(&reply);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_TYPE_NOW_SAVE: {
|
|
|
|
struct msg_now_save *msg = (struct msg_now_save *)umsg;
|
|
|
|
|
|
|
|
now = msg->now;
|
|
|
|
mailbox_acknowledge();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MESSAGE_TYPE_LOG: {
|
|
|
|
struct msg_log *msg = (struct msg_log *)umsg;
|
|
|
|
|
2015-08-02 11:33:12 +08:00
|
|
|
if(msg->no_newline) {
|
|
|
|
lognonl_va(msg->fmt, msg->args);
|
|
|
|
} else {
|
|
|
|
log_va(msg->fmt, msg->args);
|
|
|
|
}
|
2015-07-25 16:26:04 +08:00
|
|
|
mailbox_acknowledge();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
/* handled elsewhere */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|