diff options
author | Vincent Whitchurch <vincent.whitchurch@axis.com> | 2021-12-08 18:11:22 +0300 |
---|---|---|
committer | Richard Weinberger <richard@nod.at> | 2021-12-22 22:32:36 +0300 |
commit | 361640b4fdc86167b0c25d8e73c08dcaa4ecd28a (patch) | |
tree | 36af31f11c70be3c0e135c6c56a5bc569acef444 /arch/um/kernel/load_file.c | |
parent | 8bb227ac34c062b466a0d5fd21f060010375880b (diff) | |
download | linux-361640b4fdc86167b0c25d8e73c08dcaa4ecd28a.tar.xz |
um: Extract load file helper from initrd.c
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>
Diffstat (limited to 'arch/um/kernel/load_file.c')
-rw-r--r-- | arch/um/kernel/load_file.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/arch/um/kernel/load_file.c b/arch/um/kernel/load_file.c new file mode 100644 index 000000000000..5cecd0e291fb --- /dev/null +++ b/arch/um/kernel/load_file.c @@ -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; +} |