Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Vincent Whitchurch and committed by
Richard Weinberger
361640b4 8bb227ac

+75 -43
+1
arch/um/kernel/Makefile
··· 18 18 physmem.o process.o ptrace.o reboot.o sigio.o \ 19 19 signal.o sysrq.o time.o tlb.o trap.o \ 20 20 um_arch.o umid.o maccess.o kmsg_dump.o capflags.o skas/ 21 + obj-y += load_file.o 21 22 22 23 obj-$(CONFIG_BLK_DEV_INITRD) += initrd.o 23 24 obj-$(CONFIG_GPROF) += gprof_syms.o
+5 -43
arch/um/kernel/initrd.c
··· 10 10 #include <init.h> 11 11 #include <os.h> 12 12 13 + #include "um_arch.h" 14 + 13 15 /* Changed by uml_initrd_setup, which is a setup */ 14 16 static char *initrd __initdata = NULL; 15 - static int load_initrd(char *filename, void *buf, int size); 16 17 17 18 int __init read_initrd(void) 18 19 { 20 + unsigned long long size; 19 21 void *area; 20 - long long size; 21 - int err; 22 22 23 - if (initrd == NULL) 23 + if (!initrd) 24 24 return 0; 25 25 26 - err = os_file_size(initrd, &size); 27 - if (err) 28 - return 0; 29 - 30 - /* 31 - * This is necessary because alloc_bootmem craps out if you 32 - * ask for no memory. 33 - */ 34 - if (size == 0) { 35 - printk(KERN_ERR "\"%s\" is a zero-size initrd\n", initrd); 36 - return 0; 37 - } 38 - 39 - area = memblock_alloc(size, SMP_CACHE_BYTES); 26 + area = uml_load_file(initrd, &size); 40 27 if (!area) 41 - panic("%s: Failed to allocate %llu bytes\n", __func__, size); 42 - 43 - if (load_initrd(initrd, area, size) == -1) 44 28 return 0; 45 29 46 30 initrd_start = (unsigned long) area; ··· 43 59 " This is used to boot UML from an initrd image. The argument is the\n" 44 60 " name of the file containing the image.\n\n" 45 61 ); 46 - 47 - static int load_initrd(char *filename, void *buf, int size) 48 - { 49 - int fd, n; 50 - 51 - fd = os_open_file(filename, of_read(OPENFLAGS()), 0); 52 - if (fd < 0) { 53 - printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename, 54 - -fd); 55 - return -1; 56 - } 57 - n = os_read_file(fd, buf, size); 58 - if (n != size) { 59 - printk(KERN_ERR "Read of %d bytes from '%s' failed, " 60 - "err = %d\n", size, 61 - filename, -n); 62 - return -1; 63 - } 64 - 65 - os_close_file(fd); 66 - return 0; 67 - }
+61
arch/um/kernel/load_file.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 4 + */ 5 + #include <linux/memblock.h> 6 + #include <os.h> 7 + 8 + #include "um_arch.h" 9 + 10 + static int __init __uml_load_file(const char *filename, void *buf, int size) 11 + { 12 + int fd, n; 13 + 14 + fd = os_open_file(filename, of_read(OPENFLAGS()), 0); 15 + if (fd < 0) { 16 + printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename, 17 + -fd); 18 + return -1; 19 + } 20 + n = os_read_file(fd, buf, size); 21 + if (n != size) { 22 + printk(KERN_ERR "Read of %d bytes from '%s' failed, " 23 + "err = %d\n", size, 24 + filename, -n); 25 + return -1; 26 + } 27 + 28 + os_close_file(fd); 29 + return 0; 30 + } 31 + 32 + void *uml_load_file(const char *filename, unsigned long long *size) 33 + { 34 + void *area; 35 + int err; 36 + 37 + *size = 0; 38 + 39 + if (!filename) 40 + return NULL; 41 + 42 + err = os_file_size(filename, size); 43 + if (err) 44 + return NULL; 45 + 46 + if (*size == 0) { 47 + printk(KERN_ERR "\"%s\" is empty\n", filename); 48 + return NULL; 49 + } 50 + 51 + area = memblock_alloc(*size, SMP_CACHE_BYTES); 52 + if (!area) 53 + panic("%s: Failed to allocate %llu bytes\n", __func__, *size); 54 + 55 + if (__uml_load_file(filename, area, *size)) { 56 + memblock_free(area, *size); 57 + return NULL; 58 + } 59 + 60 + return area; 61 + }
+8
arch/um/kernel/um_arch.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + 3 + #ifndef __UML_ARCH_H__ 4 + #define __UML_ARCH_H__ 5 + 6 + extern void * __init uml_load_file(const char *filename, unsigned long long *size); 7 + 8 + #endif