stand/common: Add file_addbuf()

This provides an interface for a memory buffer to be passed from the loader
to the kernel as a "preloaded module".

Reviewed by:	kevans
This commit is contained in:
Colin Percival 2021-05-30 20:17:11 +00:00
parent 4afa62be71
commit 60a978bec9
2 changed files with 52 additions and 0 deletions

View file

@ -260,6 +260,7 @@ void file_addmetadata(struct preloaded_file *, int, size_t, void *);
int file_addmodule(struct preloaded_file *, char *, int,
struct kernel_module **);
void file_removemetadata(struct preloaded_file *fp);
int file_addbuf(const char *name, const char *type, size_t len, void *buf);
vm_offset_t build_font_module(vm_offset_t);

View file

@ -1042,6 +1042,57 @@ file_removemetadata(struct preloaded_file *fp)
fp->f_metadata = NULL;
}
/*
* Add a buffer to the list of preloaded "files".
*/
int
file_addbuf(const char *name, const char *type, size_t len, void *buf)
{
struct preloaded_file *fp;
vm_offset_t dest;
/* We can't load first */
if ((file_findfile(NULL, NULL)) == NULL) {
command_errmsg = "can't load file before kernel";
return (-1);
}
/* Figure out where to load the data. */
dest = loadaddr;
if (archsw.arch_loadaddr != NULL)
dest = archsw.arch_loadaddr(LOAD_RAW, (void *)name, dest);
/* Create & populate control structure */
fp = file_alloc();
if (fp == NULL) {
snprintf(command_errbuf, sizeof (command_errbuf),
"no memory to load %s", name);
return (-1);
}
fp->f_name = strdup(name);
fp->f_type = strdup(type);
fp->f_args = NULL;
fp->f_metadata = NULL;
fp->f_loader = -1;
fp->f_addr = dest;
fp->f_size = len;
if ((fp->f_name == NULL) || (fp->f_type == NULL)) {
snprintf(command_errbuf, sizeof (command_errbuf),
"no memory to load %s", name);
free(fp->f_name);
free(fp->f_type);
return (-1);
}
/* Copy the data in. */
archsw.arch_copyin(buf, fp->f_addr, len);
loadaddr = fp->f_addr + len;
/* Add to the list of loaded files */
file_insert_tail(fp);
return(0);
}
struct file_metadata *
metadata_next(struct file_metadata *md, int type)
{