xref: /openbmc/linux/arch/um/kernel/initrd.c (revision 82e6fdd6)
1 /*
2  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3  * Licensed under the GPL
4  */
5 
6 #include <linux/init.h>
7 #include <linux/bootmem.h>
8 #include <linux/initrd.h>
9 #include <asm/types.h>
10 #include <init.h>
11 #include <os.h>
12 
13 /* Changed by uml_initrd_setup, which is a setup */
14 static char *initrd __initdata = NULL;
15 static int load_initrd(char *filename, void *buf, int size);
16 
17 int __init read_initrd(void)
18 {
19 	void *area;
20 	long long size;
21 	int err;
22 
23 	if (initrd == NULL)
24 		return 0;
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 = alloc_bootmem(size);
40 
41 	if (load_initrd(initrd, area, size) == -1)
42 		return 0;
43 
44 	initrd_start = (unsigned long) area;
45 	initrd_end = initrd_start + size;
46 	return 0;
47 }
48 
49 static int __init uml_initrd_setup(char *line, int *add)
50 {
51 	initrd = line;
52 	return 0;
53 }
54 
55 __uml_setup("initrd=", uml_initrd_setup,
56 "initrd=<initrd image>\n"
57 "    This is used to boot UML from an initrd image.  The argument is the\n"
58 "    name of the file containing the image.\n\n"
59 );
60 
61 static int load_initrd(char *filename, void *buf, int size)
62 {
63 	int fd, n;
64 
65 	fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
66 	if (fd < 0) {
67 		printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename,
68 		       -fd);
69 		return -1;
70 	}
71 	n = os_read_file(fd, buf, size);
72 	if (n != size) {
73 		printk(KERN_ERR "Read of %d bytes from '%s' failed, "
74 		       "err = %d\n", size,
75 		       filename, -n);
76 		return -1;
77 	}
78 
79 	os_close_file(fd);
80 	return 0;
81 }
82