xref: /netbsd-src/sys/dev/md_root.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: md_root.c,v 1.15 2008/05/02 13:02:31 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: md_root.c,v 1.15 2008/05/02 13:02:31 ad Exp $");
34 
35 #include "opt_md.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/reboot.h>
40 
41 #include <dev/md.h>
42 
43 extern int boothowto;
44 
45 #ifdef MEMORY_DISK_DYNAMIC
46 #ifdef MEMORY_DISK_IMAGE
47 #error MEMORY_DISK_DYNAMIC is not compatible with MEMORY_DISK_IMAGE
48 #endif
49 size_t md_root_size;
50 char *md_root_image;
51 #else /* MEMORY_DISK_DYNAMIC */
52 
53 #ifdef MEMORY_DISK_IMAGE
54 #ifdef MEMORY_DISK_ROOT_SIZE
55 #error MEMORY_DISK_ROOT_SIZE is not compatible with MEMORY_DISK_IMAGE
56 #endif
57 char md_root_image[] = {
58 #include "md_root_image.h"
59 };
60 u_int32_t md_root_size = sizeof(md_root_image) & ~(DEV_BSIZE - 1);
61 
62 #else /* MEMORY_DISK_IMAGE */
63 
64 #ifndef MEMORY_DISK_ROOT_SIZE
65 #define MEMORY_DISK_ROOT_SIZE 512
66 #endif
67 #define ROOTBYTES (MEMORY_DISK_ROOT_SIZE << DEV_BSHIFT)
68 
69 /*
70  * This array will be patched to contain a file-system image.
71  * See the program mdsetimage(8) for details.
72  */
73 u_int32_t md_root_size = ROOTBYTES;
74 char md_root_image[ROOTBYTES] = "|This is the root ramdisk!\n";
75 #endif /* MEMORY_DISK_IMAGE */
76 #endif /* MEMORY_DISK_DYNAMIC */
77 
78 #ifndef MEMORY_RBFLAGS
79 #define MEMORY_RBFLAGS	RB_SINGLE	/* force single user */
80 #endif
81 
82 #ifdef MEMORY_DISK_DYNAMIC
83 void
84 md_root_setconf(char *addr, size_t size)
85 {
86 	md_is_root = 1;
87 	md_root_image = addr;
88 	md_root_size = size;
89 }
90 #endif /* MEMORY_DISK_DYNAMIC */
91 
92 /*
93  * This is called during pseudo-device attachment.
94  */
95 void
96 md_attach_hook(int unit, struct md_conf *md)
97 {
98 	char pbuf[9];
99 
100 	if (unit == 0 && md_is_root) {
101 		/* Setup root ramdisk */
102 		md->md_addr = (void *)md_root_image;
103 		md->md_size = (size_t)md_root_size;
104 		md->md_type = MD_KMEM_FIXED;
105 		format_bytes(pbuf, sizeof(pbuf), md->md_size);
106 		aprint_verbose("md%d: internal %s image area\n", unit, pbuf);
107 	}
108 }
109 
110 /*
111  * This is called during open (i.e. mountroot)
112  */
113 void
114 md_open_hook(int unit, struct md_conf *md)
115 {
116 
117 	if (unit == 0 && md_is_root) {
118 		/* The root ramdisk only works single-user. */
119 		boothowto |= MEMORY_RBFLAGS;
120 	}
121 }
122