xref: /netbsd-src/usr.sbin/sysinst/arch/shark/md.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: md.c,v 1.2 2014/08/03 16:09:40 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 -- shark machine specific routines */
36 
37 #include <stdio.h>
38 #include <curses.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <util.h>
42 #include <sys/types.h>
43 #include <sys/disklabel.h>
44 #include <sys/ioctl.h>
45 #include <sys/param.h>
46 
47 #include "defs.h"
48 #include "md.h"
49 #include "msg_defs.h"
50 #include "menu_defs.h"
51 
52 static int clear_mbr(const char *, char *, size_t);
53 
54 void
55 md_init(void)
56 {
57 }
58 
59 void
60 md_init_set_status(int flags)
61 {
62 	(void)flags;
63 }
64 
65 int
66 md_get_info(void)
67 {
68 	struct disklabel disklabel;
69 	int fd;
70 	char dev_name[100];
71 
72 	if (strncmp(pm->diskdev, "wd", 2) == 0)
73 		pm->disktype = "ST506";
74 	else
75 		pm->disktype = "SCSI";
76 
77 	snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev);
78 
79 	fd = open(dev_name, O_RDONLY, 0);
80 	if (fd < 0) {
81 		endwin();
82 		fprintf(stderr, "Can't open %s\n", dev_name);
83 		exit(1);
84 	}
85 	if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
86 		endwin();
87 		fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
88 		close(fd);
89 		exit(1);
90 	}
91 	close(fd);
92 
93 	pm->dlcyl = disklabel.d_ncylinders;
94 	pm->dlhead = disklabel.d_ntracks;
95 	pm->dlsec = disklabel.d_nsectors;
96 	pm->sectorsize = disklabel.d_secsize;
97 	pm->dlcylsize = disklabel.d_secpercyl;
98 
99 	/*
100 	 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
101 	 * and secperunit,  just in case the disk is already labelled.
102 	 * (If our new label's RAW_PART size ends up smaller than the
103 	 * in-core RAW_PART size  value, updating the label will fail.)
104 	 */
105 	pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec;
106 	if (disklabel.d_secperunit > pm->dlsize)
107 		pm->dlsize = disklabel.d_secperunit;
108 
109 	return 1;
110 }
111 
112 /*
113  * md back-end code for menu-driven BSD disklabel editor.
114  */
115 int
116 md_make_bsd_partitions(void)
117 {
118 	return make_bsd_partitions();
119 }
120 
121 /*
122  * any additional partition validation
123  */
124 int
125 md_check_partitions(void)
126 {
127 	return 1;
128 }
129 
130 /*
131  * hook called before writing new disklabel.
132  */
133 int
134 md_pre_disklabel(void)
135 {
136 	char diskpath[MAXPATHLEN];
137 
138 	if (clear_mbr(pm->diskdev, diskpath, sizeof(diskpath)) == -1) {
139 		msg_display(MSG_badclearmbr, diskpath);
140 		process_menu(MENU_ok, NULL);
141 	}
142 
143 	return 0;
144 }
145 
146 /*
147  * hook called after writing disklabel to new target disk.
148  */
149 int
150 md_post_disklabel(void)
151 {
152 	return 0;
153 }
154 
155 /*
156  * hook called after upgrade() or install() has finished setting
157  * up the target disk but immediately before the user is given the
158  * ``disks are now set up'' message.
159  */
160 int
161 md_post_newfs(void)
162 {
163 	return 0;
164 }
165 
166 int
167 md_post_extract(void)
168 {
169 	msg_display(MSG_setbootdevice);
170 	process_menu(MENU_ok, NULL);
171 
172 	return 0;
173 }
174 
175 void
176 md_cleanup_install(void)
177 {
178 #ifndef DEBUG
179 	enable_rc_conf();
180 	add_rc_conf("wscons=YES\n");
181 
182 	/* Configure a single screen. */
183 	run_program(RUN_CHROOT,
184 		    "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
185 		    "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
186 
187 #endif
188 }
189 
190 int
191 md_pre_update(void)
192 {
193 	return 1;
194 }
195 
196 /* Upgrade support */
197 int
198 md_update(void)
199 {
200 	md_post_newfs();
201 	return 1;
202 }
203 
204 /*
205  * Clears the disk's first sector by writing all zeros over it.
206  * Returns 0 on success, -1 on failure.  Leaves the disk's path in
207  * the output diskpath buffer for further usage in error messages.
208  */
209 static int
210 clear_mbr(const char *disk, char *diskpath, size_t diskpathlen)
211 {
212 	int fd;
213 	char sector[512];
214 
215 	fd = opendisk(disk, O_WRONLY, diskpath, diskpathlen, 0);
216 	if (fd < 0)
217 		return -1;
218 
219 	memset(sector, 0, sizeof(sector));
220 	if (pwrite(fd, &sector, sizeof(sector), 0) < 0) {
221 		close(fd);
222 		return -1;
223 	}
224 
225 	close(fd);
226 	return 0;
227 }
228 
229 
230 int
231 md_pre_mount()
232 {
233 	return 0;
234 }
235