1/* $NetBSD: md_root.c,v 1.19 2015/08/30 05:24:03 uebayasi 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.19 2015/08/30 05:24:03 uebayasi Exp $");
34
35#include "opt_md.h"
36#include "opt_memory_disk_image.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/reboot.h>
41
42#include <dev/md.h>
43
44#ifdef MEMORY_DISK_DYNAMIC
45#ifdef makeoptions_MEMORY_DISK_IMAGE
46#error MEMORY_DISK_DYNAMIC is not compatible with MEMORY_DISK_IMAGE
47#endif
48size_t md_root_size;
49char *md_root_image;
50#else /* MEMORY_DISK_DYNAMIC */
51
52#ifdef makeoptions_MEMORY_DISK_IMAGE
53#ifdef MEMORY_DISK_ROOT_SIZE
54#error MEMORY_DISK_ROOT_SIZE is not compatible with MEMORY_DISK_IMAGE
55#endif
56char md_root_image[] = {
57#include "md_root_image.h"
58};
59uint32_t md_root_size = sizeof(md_root_image) & ~(DEV_BSIZE - 1);
60
61#else /* makeoptions_MEMORY_DISK_IMAGE */
62
63#ifndef MEMORY_DISK_ROOT_SIZE
64#define MEMORY_DISK_ROOT_SIZE 512
65#endif
66#define ROOTBYTES (MEMORY_DISK_ROOT_SIZE << DEV_BSHIFT)
67
68/*
69 * This array will be patched to contain a file-system image.
70 * See the program mdsetimage(8) for details.
71 */
72uint32_t md_root_size = ROOTBYTES;
73char md_root_image[ROOTBYTES] = "|This is the root ramdisk!\n";
74#endif /* makeoptions_MEMORY_DISK_IMAGE */
75#endif /* MEMORY_DISK_DYNAMIC */
76
77#ifndef MEMORY_DISK_RBFLAGS
78#define MEMORY_DISK_RBFLAGS RB_AUTOBOOT /* default boot mode */
79#endif
80
81#ifdef MEMORY_DISK_DYNAMIC
82void
83md_root_setconf(char *addr, size_t size)
84{
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#define PBUFLEN sizeof("99999 KB")
96
97void
98md_attach_hook(int unit, struct md_conf *md)
99{
100 char pbuf[PBUFLEN];
101
102 if (unit == 0 && md_is_root) {
103 /* Setup root ramdisk */
104 md->md_addr = (void *)md_root_image;
105 md->md_size = (size_t)md_root_size;
106 md->md_type = MD_KMEM_FIXED;
107 format_bytes(pbuf, sizeof(pbuf), md->md_size);
108 aprint_verbose("md%d: internal %s image area\n", unit, pbuf);
109 }
110}
111
112/*
113 * This is called during open (i.e. mountroot)
114 */
115void
116md_open_hook(int unit, struct md_conf *md)
117{
118
119 if (unit == 0 && md_is_root) {
120 boothowto |= MEMORY_DISK_RBFLAGS;
121 }
122}
123