xref: /freebsd/sys/powerpc/ofw/ofw_initrd.c (revision 5ab1c5846ff41be24b1f6beb0317bf8258cd4409)
1 /*-
2  * Copyright (C) 2018 Breno Leitao
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32 #include <sys/types.h>
33 #include <sys/proc.h>
34 
35 #include <vm/vm.h>
36 #include <vm/pmap.h>
37 
38 #include <machine/bus.h>
39 
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include "opt_md.h"
45 
46 extern u_char *mfs_root;
47 extern int mfs_root_size;
48 
49 static void ofw_initrd_probe_and_attach(void *junk);
50 
51 SYSINIT(ofw_initrd_probe_and_attach, SI_SUB_KMEM, SI_ORDER_ANY,
52     ofw_initrd_probe_and_attach, NULL);
53 
54 static void
55 ofw_initrd_probe_and_attach(void *junk)
56 {
57 	phandle_t chosen;
58 	vm_paddr_t start, end;
59 	pcell_t cell[2];
60 	ssize_t size;
61 
62 	if (!hw_direct_map)
63 		return;
64 
65 	chosen = OF_finddevice("/chosen");
66 	if (chosen <= 0)
67 		return;
68 
69 	if (!OF_hasprop(chosen, "linux,initrd-start") ||
70 	    !OF_hasprop(chosen, "linux,initrd-end"))
71 		return;
72 
73 	size = OF_getencprop(chosen, "linux,initrd-start", cell, sizeof(cell));
74 	if (size == 4)
75 		start = cell[0];
76 	else if (size == 8)
77 		start = (uint64_t)cell[0] << 32 | cell[1];
78 	else {
79 		printf("ofw_initrd: Wrong linux,initrd-start size\n");
80 		return;
81 	}
82 
83 	size = OF_getencprop(chosen, "linux,initrd-end", cell, sizeof(cell));
84 	if (size == 4)
85 		end = cell[0];
86 	else if (size == 8)
87 		end = (uint64_t)cell[0] << 32 | cell[1];
88 	else{
89 		printf("ofw_initrd: Wrong linux,initrd-end size\n");
90 		return;
91 	}
92 
93 	if (end - start > 0) {
94 		mfs_root = (u_char *) PHYS_TO_DMAP(start);
95 		mfs_root_size = end - start;
96 		printf("ofw_initrd: initrd loaded at 0x%08lx-0x%08lx\n",
97 			start, end);
98 	}
99 }
100 
101