Skip to content

Commit

Permalink
um: Extract load file helper from initrd.c
Browse files Browse the repository at this point in the history
The file loading support in initrd.c can be re-used for
loading devicetrees.  Move it out of initrd.c.

Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
Signed-off-by: Richard Weinberger <richard@nod.at>
  • Loading branch information
Vincent Whitchurch authored and Richard Weinberger committed Dec 22, 2021
1 parent 8bb227a commit 361640b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 43 deletions.
1 change: 1 addition & 0 deletions arch/um/kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ obj-y = config.o exec.o exitcode.o irq.o ksyms.o mem.o \
physmem.o process.o ptrace.o reboot.o sigio.o \
signal.o sysrq.o time.o tlb.o trap.o \
um_arch.o umid.o maccess.o kmsg_dump.o capflags.o skas/
obj-y += load_file.o

obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o
obj-$(CONFIG_GPROF) += gprof_syms.o
Expand Down
48 changes: 5 additions & 43 deletions arch/um/kernel/initrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,21 @@
#include <init.h>
#include <os.h>

#include "um_arch.h"

/* Changed by uml_initrd_setup, which is a setup */
static char *initrd __initdata = NULL;
static int load_initrd(char *filename, void *buf, int size);

int __init read_initrd(void)
{
unsigned long long size;
void *area;
long long size;
int err;

if (initrd == NULL)
return 0;

err = os_file_size(initrd, &size);
if (err)
if (!initrd)
return 0;

/*
* This is necessary because alloc_bootmem craps out if you
* ask for no memory.
*/
if (size == 0) {
printk(KERN_ERR "\"%s\" is a zero-size initrd\n", initrd);
return 0;
}

area = memblock_alloc(size, SMP_CACHE_BYTES);
area = uml_load_file(initrd, &size);
if (!area)
panic("%s: Failed to allocate %llu bytes\n", __func__, size);

if (load_initrd(initrd, area, size) == -1)
return 0;

initrd_start = (unsigned long) area;
Expand All @@ -59,25 +43,3 @@ __uml_setup("initrd=", uml_initrd_setup,
" This is used to boot UML from an initrd image. The argument is the\n"
" name of the file containing the image.\n\n"
);

static int load_initrd(char *filename, void *buf, int size)
{
int fd, n;

fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
if (fd < 0) {
printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename,
-fd);
return -1;
}
n = os_read_file(fd, buf, size);
if (n != size) {
printk(KERN_ERR "Read of %d bytes from '%s' failed, "
"err = %d\n", size,
filename, -n);
return -1;
}

os_close_file(fd);
return 0;
}
61 changes: 61 additions & 0 deletions arch/um/kernel/load_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
*/
#include <linux/memblock.h>
#include <os.h>

#include "um_arch.h"

static int __init __uml_load_file(const char *filename, void *buf, int size)
{
int fd, n;

fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
if (fd < 0) {
printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename,
-fd);
return -1;
}
n = os_read_file(fd, buf, size);
if (n != size) {
printk(KERN_ERR "Read of %d bytes from '%s' failed, "
"err = %d\n", size,
filename, -n);
return -1;
}

os_close_file(fd);
return 0;
}

void *uml_load_file(const char *filename, unsigned long long *size)
{
void *area;
int err;

*size = 0;

if (!filename)
return NULL;

err = os_file_size(filename, size);
if (err)
return NULL;

if (*size == 0) {
printk(KERN_ERR "\"%s\" is empty\n", filename);
return NULL;
}

area = memblock_alloc(*size, SMP_CACHE_BYTES);
if (!area)
panic("%s: Failed to allocate %llu bytes\n", __func__, *size);

if (__uml_load_file(filename, area, *size)) {
memblock_free(area, *size);
return NULL;
}

return area;
}
8 changes: 8 additions & 0 deletions arch/um/kernel/um_arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0-only */

#ifndef __UML_ARCH_H__
#define __UML_ARCH_H__

extern void * __init uml_load_file(const char *filename, unsigned long long *size);

#endif

0 comments on commit 361640b

Please sign in to comment.