md.c revision 1.3
1/*	$NetBSD: md.c,v 1.3 2019/06/12 06:20:20 martin Exp $	*/
2
3/*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Based on code written by Philip A. Nelson for Piermont Information
8 * Systems Inc.
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 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 *    or promote products derived from this software without specific prior
20 *    written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/* md.c -- ews4800mips machine specific routines */
36
37#include <sys/types.h>
38#include <sys/ioctl.h>
39#include <sys/param.h>
40#include <stdio.h>
41#include <curses.h>
42#include <unistd.h>
43#include <fcntl.h>
44#include <util.h>
45
46#include "defs.h"
47#include "md.h"
48#include "msg_defs.h"
49#include "menu_defs.h"
50
51static int ews4800mips_boot_offset(void);
52
53void
54md_init(void)
55{
56}
57
58void
59md_init_set_status(int flags)
60{
61	(void)flags;
62}
63
64bool
65md_get_info(struct install_partition_desc *install)
66{
67	struct disklabel disklabel;
68	int fd;
69	char dev_name[100];
70
71	snprintf(dev_name, 100, "/dev/r%s%c", pm->diskdev, 'a' + getrawpartition());
72
73	fd = open(dev_name, O_RDONLY, 0);
74	if (fd < 0) {
75		endwin();
76		fprintf (stderr, "Can't open %s\n", dev_name);
77		exit(1);
78	}
79	if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
80		endwin();
81		fprintf (stderr, "Can't read disklabel on %s.\n", dev_name);
82		close(fd);
83		exit(1);
84	}
85	close(fd);
86
87	pm->dlcyl = disklabel.d_ncylinders;
88	pm->dlhead = disklabel.d_ntracks;
89	pm->dlsec = disklabel.d_nsectors;
90	pm->sectorsize = disklabel.d_secsize;
91	pm->dlcylsize = disklabel.d_secpercyl;
92
93	/*
94	 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
95	 * and secperunit,  just in case the disk is already labelled.
96	 * (If our new label's RAW_PART size ends up smaller than the
97	 * in-core RAW_PART size  value, updating the label will fail.)
98	 */
99	pm->dlsize = pm->dlcyl * pm->dlhead * pm->dlsec;
100	if (disklabel.d_secperunit > pm->dlsize)
101		pm->dlsize = disklabel.d_secperunit;
102
103	return true;
104}
105
106/*
107 * md back-end code for menu-driven BSD disklabel editor.
108 */
109bool
110md_make_bsd_partitions(struct install_partition_desc *install)
111{
112
113	return make_bsd_partitions(install);
114}
115
116/*
117 * any additional partition validation
118 */
119bool
120md_check_partitions(struct install_partition_desc *install)
121{
122	return true;
123}
124
125/*
126 * hook called before writing new disklabel.
127 */
128bool
129md_pre_disklabel(struct install_partition_desc *install,
130    struct disk_partitions *parts)
131{
132	part_id part;
133	struct disk_part_info info;
134	daddr_t boot_offset = ews4800mips_boot_offset();
135
136	/* make sure the boot parition is at the right offset */
137	for (part = 0; part < parts->num_part; part++) {
138		if (!parts->pscheme->get_part_info(parts, part, &info))
139			continue;
140		if (info.flags & (PTI_SEC_CONTAINER|PTI_WHOLE_DISK|
141		    PTI_PSCHEME_INTERNAL|PTI_RAW_PART))
142			continue;
143		if (info.fs_type != PART_BOOT_TYPE)
144			continue;
145		if (info.start == boot_offset)
146			continue;
147		info.start = boot_offset;
148		parts->pscheme->set_part_info(parts, part, &info, NULL);
149	}
150
151	return true;
152}
153
154/*
155 * hook called after writing disklabel to new target disk.
156 */
157bool
158md_post_disklabel(struct install_partition_desc *install,
159    struct disk_partitions *parts)
160{
161	return true;
162}
163
164/*
165 * hook called after upgrade() or install() has finished setting
166 * up the target disk but immediately before the user is given the
167 * ``disks are now set up'' message.
168 *
169 * On the ews4800mips, we use this opportunity to install the boot blocks.
170 */
171int
172md_post_newfs(struct install_partition_desc *install)
173{
174	int flags;
175
176	flags = RUN_DISPLAY | RUN_FATAL;
177	cp_to_target("/usr/mdec/boot", "/stand/boot");
178	run_program(flags, "/usr/sbin/installboot /dev/r%s%c %s",
179	    pm->diskdev, 'a' + getrawpartition(),
180	    "/usr/mdec/bootxx_bfs");
181
182	return 0;
183}
184
185int
186md_post_extract(struct install_partition_desc *install)
187{
188	return 0;
189}
190
191void
192md_cleanup_install(struct install_partition_desc *install)
193{
194#ifndef DEBUG
195	enable_rc_conf();
196#endif
197}
198
199int
200md_pre_update(struct install_partition_desc *install)
201{
202	return 1;
203}
204
205/* Upgrade support */
206int
207md_update(struct install_partition_desc *install)
208{
209	md_post_newfs(install);
210	return 1;
211}
212
213static int
214ews4800mips_boot_offset(void)
215{
216	return pm->dlcylsize;
217}
218
219/*
220 * used in bsddisklabel.c as BOOT_SIZE
221 */
222int
223ews4800mips_boot_size(void)
224{
225	int i;
226
227	/*
228	 * pm->dlcylsize	: PDINFO block
229	 * pm->dlcylsize	: 100 block
230	 */
231	i = pm->dlcylsize + 100;
232	if (i >= 1024) /* XXX bsddisklabel.c hack. convert to byte count. */
233		i = pm->dlcylsize * pm->sectorsize * 2;
234
235	return i;
236}
237
238/*
239 * used in bsddisklabel.c as SYSVBFS_SIZE
240 */
241int
242ews4800mips_sysvbfs_size(void)
243{
244	return (8 * 1024 * 1024) / 512;	/* 8MB */
245}
246
247int
248md_pre_mount(struct install_partition_desc *install)
249{
250	return 0;
251}
252
253bool
254md_parts_use_wholedisk(struct disk_partitions *parts)
255{
256	struct disk_part_info boot_part = {
257		.size = PART_BOOT / 512,
258		.fs_type = PART_BOOT_TYPE,
259	};
260
261	boot_part.nat_type = parts->pscheme->get_fs_part_type(
262	    boot_part.fs_type, boot_part.fs_sub_type);
263
264	return parts_use_wholedisk(parts, 1, &boot_part);
265}
266
267#ifdef HAVE_GPT
268bool
269md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
270    bool root_is_new, part_id efi_id, bool efi_is_new)
271{
272	/* no GPT boot support, nothing needs to be done here */
273	return true;
274}
275#endif
276
277