127 lines
2.6 KiB
C
127 lines
2.6 KiB
C
#include "tar.h"
|
|
|
|
#include <launch.h>
|
|
#include <mango/log.h>
|
|
#include <mango/msg.h>
|
|
#include <mango/task.h>
|
|
#include <mango/types.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define INIT_PATH "/usr/bin/test"
|
|
|
|
static enum launch_status resolve_dependency(
|
|
struct launch_ctx *ctx,
|
|
const char *name,
|
|
kern_handle_t *out,
|
|
void *arg)
|
|
{
|
|
char s[128];
|
|
while (*name == '/') {
|
|
name++;
|
|
}
|
|
|
|
snprintf(s, sizeof s, "searching for library %s", name);
|
|
kern_log(s);
|
|
|
|
struct tar *fs = arg;
|
|
struct tar_file file = {0};
|
|
if (tar_open(fs, name, &file) != 0) {
|
|
snprintf(s, sizeof s, "cannot find library %s", name);
|
|
kern_log(s);
|
|
return LAUNCH_ERR_CANNOT_RESOLVE_DEPENDENCY;
|
|
}
|
|
|
|
kern_handle_t image = KERN_HANDLE_INVALID;
|
|
int err = tar_file_create_vm_object(&file, &image);
|
|
if (err != 0) {
|
|
snprintf(s, sizeof s, "cannot load library %s", name);
|
|
kern_log(s);
|
|
return -1;
|
|
}
|
|
|
|
*out = image;
|
|
return LAUNCH_OK;
|
|
}
|
|
|
|
int main(
|
|
int argc,
|
|
const char **argv,
|
|
kern_handle_t task,
|
|
kern_handle_t address_space,
|
|
uintptr_t bsp_base)
|
|
{
|
|
struct tar bsp = {0};
|
|
if (tar_init(&bsp, bsp_base) != 0) {
|
|
kern_log("cannot access bsp");
|
|
return -1;
|
|
}
|
|
|
|
struct tar_file init = {0};
|
|
if (tar_open(&bsp, INIT_PATH, &init) != 0) {
|
|
kern_log("cannot find init program " INIT_PATH);
|
|
return -1;
|
|
}
|
|
|
|
kern_trace("loading " INIT_PATH);
|
|
|
|
kern_handle_t image = KERN_HANDLE_INVALID;
|
|
int err = tar_file_create_vm_object(&init, &image);
|
|
if (err != 0) {
|
|
kern_log("cannot load executable " INIT_PATH);
|
|
return -1;
|
|
}
|
|
|
|
kern_trace("loaded executable vm-object " INIT_PATH);
|
|
|
|
kern_tracef("task=%x, region=%x", task, address_space);
|
|
|
|
struct launch_ctx launch;
|
|
struct launch_result result;
|
|
struct launch_parameters params = {
|
|
.p_executable = image,
|
|
.p_parent_task = task,
|
|
.p_task_name = "init",
|
|
.p_local_address_space = address_space,
|
|
.p_resolver_arg = &bsp,
|
|
};
|
|
|
|
kern_handle_t channel;
|
|
channel_create(0, 0, &channel);
|
|
|
|
launch_ctx_init(&launch);
|
|
launch.ctx_resolve_library = resolve_dependency;
|
|
|
|
enum launch_status status
|
|
= launch_ctx_execute(&launch, ¶ms, LAUNCH_F_NONE, &result);
|
|
kern_logf("launch result: %d", status);
|
|
|
|
#if 1
|
|
char msg_buf[512];
|
|
struct iovec vec = {
|
|
.io_base = (virt_addr_t)msg_buf,
|
|
.io_len = sizeof msg_buf,
|
|
};
|
|
struct msg msg = {.msg_data = &vec, .msg_data_count = 1};
|
|
|
|
while (1) {
|
|
msgid_t id = 0;
|
|
kern_status_t kstatus = msg_recv(channel, 0, &id, &msg);
|
|
if (kstatus != KERN_OK) {
|
|
kern_logf("msg_recv failed %d", kstatus);
|
|
break;
|
|
}
|
|
|
|
kern_logf("received message %zx: %s", id, msg_buf);
|
|
|
|
size_t len = snprintf(msg_buf, sizeof msg_buf, "Goodbye!");
|
|
vec.io_len = len + 1;
|
|
|
|
msg_reply(channel, 0, id, &msg);
|
|
}
|
|
#endif
|
|
|
|
return 102;
|
|
}
|