xref: /netbsd-src/sys/dev/md_root.c (revision 5e3f7c3b6df02896b2fc0396b2c67dea22a3365a)
1*5e3f7c3bSuebayasi /*	$NetBSD: md_root.c,v 1.19 2015/08/30 05:24:03 uebayasi Exp $	*/
210da4aa9Stsutsui 
310da4aa9Stsutsui /*-
410da4aa9Stsutsui  * Copyright (c) 1996 The NetBSD Foundation, Inc.
510da4aa9Stsutsui  * All rights reserved.
610da4aa9Stsutsui  *
710da4aa9Stsutsui  * This code is derived from software contributed to The NetBSD Foundation
810da4aa9Stsutsui  * by Gordon W. Ross.
910da4aa9Stsutsui  *
1010da4aa9Stsutsui  * Redistribution and use in source and binary forms, with or without
1110da4aa9Stsutsui  * modification, are permitted provided that the following conditions
1210da4aa9Stsutsui  * are met:
1310da4aa9Stsutsui  * 1. Redistributions of source code must retain the above copyright
1410da4aa9Stsutsui  *    notice, this list of conditions and the following disclaimer.
1510da4aa9Stsutsui  * 2. Redistributions in binary form must reproduce the above copyright
1610da4aa9Stsutsui  *    notice, this list of conditions and the following disclaimer in the
1710da4aa9Stsutsui  *    documentation and/or other materials provided with the distribution.
1810da4aa9Stsutsui  *
1910da4aa9Stsutsui  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2010da4aa9Stsutsui  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2110da4aa9Stsutsui  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2210da4aa9Stsutsui  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2310da4aa9Stsutsui  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2410da4aa9Stsutsui  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2510da4aa9Stsutsui  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2610da4aa9Stsutsui  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2710da4aa9Stsutsui  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2810da4aa9Stsutsui  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2910da4aa9Stsutsui  * POSSIBILITY OF SUCH DAMAGE.
3010da4aa9Stsutsui  */
3110da4aa9Stsutsui 
322bbe2de6Slukem #include <sys/cdefs.h>
33*5e3f7c3bSuebayasi __KERNEL_RCSID(0, "$NetBSD: md_root.c,v 1.19 2015/08/30 05:24:03 uebayasi Exp $");
342bbe2de6Slukem 
35fd2c055cSuch #include "opt_md.h"
36*5e3f7c3bSuebayasi #include "opt_memory_disk_image.h"
37fd2c055cSuch 
3810da4aa9Stsutsui #include <sys/param.h>
3910da4aa9Stsutsui #include <sys/systm.h>
4010da4aa9Stsutsui #include <sys/reboot.h>
4110da4aa9Stsutsui 
4210da4aa9Stsutsui #include <dev/md.h>
4310da4aa9Stsutsui 
4471376e07Slukem #ifdef MEMORY_DISK_DYNAMIC
45*5e3f7c3bSuebayasi #ifdef makeoptions_MEMORY_DISK_IMAGE
46*5e3f7c3bSuebayasi #error MEMORY_DISK_DYNAMIC is not compatible with MEMORY_DISK_IMAGE
47*5e3f7c3bSuebayasi #endif
48fd2c055cSuch size_t md_root_size;
49fd2c055cSuch char *md_root_image;
50fd2c055cSuch #else /* MEMORY_DISK_DYNAMIC */
51fd2c055cSuch 
52*5e3f7c3bSuebayasi #ifdef makeoptions_MEMORY_DISK_IMAGE
53*5e3f7c3bSuebayasi #ifdef MEMORY_DISK_ROOT_SIZE
54*5e3f7c3bSuebayasi #error MEMORY_DISK_ROOT_SIZE is not compatible with MEMORY_DISK_IMAGE
55*5e3f7c3bSuebayasi #endif
56*5e3f7c3bSuebayasi char md_root_image[] = {
57*5e3f7c3bSuebayasi #include "md_root_image.h"
58*5e3f7c3bSuebayasi };
59*5e3f7c3bSuebayasi uint32_t md_root_size = sizeof(md_root_image) & ~(DEV_BSIZE - 1);
60*5e3f7c3bSuebayasi 
61*5e3f7c3bSuebayasi #else /* makeoptions_MEMORY_DISK_IMAGE */
62b81d443eSthorpej 
63d213d804Slukem #ifndef MEMORY_DISK_ROOT_SIZE
64d213d804Slukem #define MEMORY_DISK_ROOT_SIZE 512
6510da4aa9Stsutsui #endif
66d213d804Slukem #define ROOTBYTES (MEMORY_DISK_ROOT_SIZE << DEV_BSHIFT)
6710da4aa9Stsutsui 
6810da4aa9Stsutsui /*
6910da4aa9Stsutsui  * This array will be patched to contain a file-system image.
7010da4aa9Stsutsui  * See the program mdsetimage(8) for details.
7110da4aa9Stsutsui  */
7289de08c2Stsutsui uint32_t md_root_size = ROOTBYTES;
7310da4aa9Stsutsui char md_root_image[ROOTBYTES] = "|This is the root ramdisk!\n";
74*5e3f7c3bSuebayasi #endif /* makeoptions_MEMORY_DISK_IMAGE */
75fd2c055cSuch #endif /* MEMORY_DISK_DYNAMIC */
76fd2c055cSuch 
7792ae85d1Sjym #ifndef MEMORY_DISK_RBFLAGS
7892ae85d1Sjym #define MEMORY_DISK_RBFLAGS	RB_AUTOBOOT	/* default boot mode */
796d78dc7cSbriggs #endif
806d78dc7cSbriggs 
8171376e07Slukem #ifdef MEMORY_DISK_DYNAMIC
82fd2c055cSuch void
md_root_setconf(char * addr,size_t size)83fd2c055cSuch md_root_setconf(char *addr, size_t size)
84fd2c055cSuch {
8589de08c2Stsutsui 
861253c2caSad 	md_is_root = 1;
87fd2c055cSuch 	md_root_image = addr;
88fd2c055cSuch 	md_root_size = size;
89fd2c055cSuch }
90fd2c055cSuch #endif /* MEMORY_DISK_DYNAMIC */
9110da4aa9Stsutsui 
9210da4aa9Stsutsui /*
9310da4aa9Stsutsui  * This is called during pseudo-device attachment.
9410da4aa9Stsutsui  */
9589de08c2Stsutsui #define PBUFLEN	sizeof("99999 KB")
9689de08c2Stsutsui 
9710da4aa9Stsutsui void
md_attach_hook(int unit,struct md_conf * md)98fd2c055cSuch md_attach_hook(int unit, struct md_conf *md)
9910da4aa9Stsutsui {
10089de08c2Stsutsui 	char pbuf[PBUFLEN];
10110da4aa9Stsutsui 
1021253c2caSad 	if (unit == 0 && md_is_root) {
10310da4aa9Stsutsui 		/* Setup root ramdisk */
10453524e44Schristos 		md->md_addr = (void *)md_root_image;
10510da4aa9Stsutsui 		md->md_size = (size_t)md_root_size;
10610da4aa9Stsutsui 		md->md_type = MD_KMEM_FIXED;
107fd2c055cSuch 		format_bytes(pbuf, sizeof(pbuf), md->md_size);
1081253c2caSad 		aprint_verbose("md%d: internal %s image area\n", unit, pbuf);
10910da4aa9Stsutsui 	}
11010da4aa9Stsutsui }
11110da4aa9Stsutsui 
11210da4aa9Stsutsui /*
11310da4aa9Stsutsui  * This is called during open (i.e. mountroot)
11410da4aa9Stsutsui  */
11510da4aa9Stsutsui void
md_open_hook(int unit,struct md_conf * md)116168cd830Schristos md_open_hook(int unit, struct md_conf *md)
11710da4aa9Stsutsui {
11810da4aa9Stsutsui 
1191253c2caSad 	if (unit == 0 && md_is_root) {
12092ae85d1Sjym 		boothowto |= MEMORY_DISK_RBFLAGS;
12110da4aa9Stsutsui 	}
12210da4aa9Stsutsui }
123