1/*	$NetBSD: md_root.c,v 1.16 2009/02/06 18:50:29 jym 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.16 2009/02/06 18:50:29 jym 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#ifdef MEMORY_DISK_DYNAMIC
44#ifdef MEMORY_DISK_IMAGE
45#error MEMORY_DISK_DYNAMIC is not compatible with MEMORY_DISK_IMAGE
46#endif
47size_t md_root_size;
48char *md_root_image;
49#else /* MEMORY_DISK_DYNAMIC */
50
51#ifdef MEMORY_DISK_IMAGE
52#ifdef MEMORY_DISK_ROOT_SIZE
53#error MEMORY_DISK_ROOT_SIZE is not compatible with MEMORY_DISK_IMAGE
54#endif
55char md_root_image[] = {
56#include "md_root_image.h"
57};
58uint32_t md_root_size = sizeof(md_root_image) & ~(DEV_BSIZE - 1);
59
60#else /* MEMORY_DISK_IMAGE */
61
62#ifndef MEMORY_DISK_ROOT_SIZE
63#define MEMORY_DISK_ROOT_SIZE 512
64#endif
65#define ROOTBYTES (MEMORY_DISK_ROOT_SIZE << DEV_BSHIFT)
66
67/*
68 * This array will be patched to contain a file-system image.
69 * See the program mdsetimage(8) for details.
70 */
71uint32_t md_root_size = ROOTBYTES;
72char md_root_image[ROOTBYTES] = "|This is the root ramdisk!\n";
73#endif /* MEMORY_DISK_IMAGE */
74#endif /* MEMORY_DISK_DYNAMIC */
75
76#ifndef MEMORY_DISK_RBFLAGS
77#define MEMORY_DISK_RBFLAGS	RB_AUTOBOOT	/* default boot mode */
78#endif
79
80#ifdef MEMORY_DISK_DYNAMIC
81void
82md_root_setconf(char *addr, size_t size)
83{
84
85	md_is_root = 1;
86	md_root_image = addr;
87	md_root_size = size;
88}
89#endif /* MEMORY_DISK_DYNAMIC */
90
91/*
92 * This is called during pseudo-device attachment.
93 */
94#define PBUFLEN	sizeof("99999 KB")
95
96void
97md_attach_hook(int unit, struct md_conf *md)
98{
99	char pbuf[PBUFLEN];
100
101	if (unit == 0 && md_is_root) {
102		/* Setup root ramdisk */
103		md->md_addr = (void *)md_root_image;
104		md->md_size = (size_t)md_root_size;
105		md->md_type = MD_KMEM_FIXED;
106		format_bytes(pbuf, sizeof(pbuf), md->md_size);
107		aprint_verbose("md%d: internal %s image area\n", unit, pbuf);
108	}
109}
110
111/*
112 * This is called during open (i.e. mountroot)
113 */
114void
115md_open_hook(int unit, struct md_conf *md)
116{
117
118	if (unit == 0 && md_is_root) {
119		boothowto |= MEMORY_DISK_RBFLAGS;
120	}
121}
122