md.c revision 1.7 1 1.7 christos /* $NetBSD: md.c,v 1.7 2019/06/20 00:43:58 christos Exp $ */
2 1.1 dholland
3 1.1 dholland /*
4 1.1 dholland * Copyright 1997 Piermont Information Systems Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * Based on code written by Philip A. Nelson for Piermont Information
8 1.1 dholland * Systems Inc. Modified by Minoura Makoto for x68k.
9 1.1 dholland *
10 1.1 dholland * Redistribution and use in source and binary forms, with or without
11 1.1 dholland * modification, are permitted provided that the following conditions
12 1.1 dholland * are met:
13 1.1 dholland * 1. Redistributions of source code must retain the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer.
15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dholland * notice, this list of conditions and the following disclaimer in the
17 1.1 dholland * documentation and/or other materials provided with the distribution.
18 1.1 dholland * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 1.1 dholland * or promote products derived from this software without specific prior
20 1.1 dholland * written permission.
21 1.1 dholland *
22 1.1 dholland * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 1.1 dholland * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 1.1 dholland * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 dholland * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland /* md.c -- x68k machine specific routines */
36 1.1 dholland /* This file is in close sync with pmax, sparc, and vax md.c */
37 1.1 dholland
38 1.1 dholland #include <stdio.h>
39 1.1 dholland #include <unistd.h>
40 1.1 dholland #include <sys/ioctl.h>
41 1.1 dholland #include <sys/param.h>
42 1.1 dholland #include <util.h>
43 1.1 dholland
44 1.1 dholland #include "defs.h"
45 1.1 dholland #include "md.h"
46 1.1 dholland #include "msg_defs.h"
47 1.1 dholland #include "menu_defs.h"
48 1.1 dholland
49 1.1 dholland #ifdef notyet
50 1.1 dholland #undef NDOSPART 8
51 1.1 dholland #define NDOSPART 16
52 1.1 dholland typedef struct parttab {
53 1.1 dholland struct dos_partition dosparts[NDOSPART];
54 1.1 dholland } parttab;
55 1.1 dholland
56 1.1 dholland parttab md_disklabel;
57 1.1 dholland int md_freepart;
58 1.1 dholland int md_nfreepart;
59 1.1 dholland #endif /* notyet */
60 1.1 dholland
61 1.1 dholland int md_need_newdisk = 0;
62 1.1 dholland
63 1.1 dholland /* prototypes */
64 1.1 dholland static int md_newdisk(void);
65 1.1 dholland
66 1.1 dholland void
67 1.1 dholland md_init(void)
68 1.1 dholland {
69 1.1 dholland }
70 1.1 dholland
71 1.1 dholland void
72 1.1 dholland md_init_set_status(int flags)
73 1.1 dholland {
74 1.1 dholland (void)flags;
75 1.1 dholland }
76 1.1 dholland
77 1.6 martin bool
78 1.6 martin md_get_info(struct install_partition_desc *install)
79 1.1 dholland {
80 1.1 dholland char buf[1024];
81 1.1 dholland int fd;
82 1.1 dholland char dev_name[100];
83 1.1 dholland struct disklabel disklabel;
84 1.1 dholland
85 1.2 martin snprintf(dev_name, 100, "/dev/r%sc", pm->diskdev);
86 1.1 dholland
87 1.1 dholland fd = open(dev_name, O_RDONLY, 0);
88 1.1 dholland if (fd < 0) {
89 1.1 dholland if (logfp)
90 1.1 dholland (void)fprintf(logfp, "Can't open %s\n", dev_name);
91 1.1 dholland endwin();
92 1.1 dholland fprintf(stderr, "Can't open %s\n", dev_name);
93 1.1 dholland exit(1);
94 1.1 dholland }
95 1.1 dholland if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
96 1.1 dholland if (logfp)
97 1.1 dholland (void)fprintf(logfp, "Can't read disklabel on %s.\n",
98 1.1 dholland dev_name);
99 1.1 dholland endwin();
100 1.1 dholland fprintf(stderr, "Can't read disklabel on %s.\n", dev_name);
101 1.1 dholland close(fd);
102 1.1 dholland exit(1);
103 1.1 dholland }
104 1.1 dholland if (disklabel.d_secsize != 512) {
105 1.1 dholland endwin();
106 1.1 dholland fprintf(stderr, "Non-512byte/sector disk is not supported.\n");
107 1.1 dholland close(fd);
108 1.1 dholland exit(1);
109 1.1 dholland }
110 1.1 dholland
111 1.2 martin pm->dlcyl = disklabel.d_ncylinders;
112 1.2 martin pm->dlhead = disklabel.d_ntracks;
113 1.2 martin pm->dlsec = disklabel.d_nsectors;
114 1.2 martin pm->sectorsize = disklabel.d_secsize;
115 1.2 martin pm->dlcylsize = disklabel.d_secpercyl;
116 1.2 martin pm->dlsize = pm->dlcyl*pm->dlhead*pm->dlsec;
117 1.1 dholland
118 1.1 dholland if (read(fd, buf, 1024) < 0) {
119 1.1 dholland endwin();
120 1.1 dholland fprintf(stderr, "Can't read %s\n", dev_name);
121 1.1 dholland close(fd);
122 1.1 dholland exit(1);
123 1.1 dholland }
124 1.1 dholland if (memcmp(buf, "X68SCSI1", 8) != 0)
125 1.1 dholland md_need_newdisk = 1;
126 1.1 dholland #ifdef notyet
127 1.1 dholland else
128 1.1 dholland if (read(fd, md_disklabel, sizeof(md_disklabel)) < 0) {
129 1.1 dholland endwin();
130 1.1 dholland fprintf(stderr, "Can't read %s\n", dev_name);
131 1.1 dholland close(fd);
132 1.1 dholland exit(1);
133 1.1 dholland }
134 1.1 dholland #endif
135 1.1 dholland /* preserve first 64 sectors for system. */
136 1.2 martin pm->ptstart = 64;
137 1.1 dholland
138 1.1 dholland /* preserve existing partitions? */
139 1.1 dholland
140 1.1 dholland close(fd);
141 1.1 dholland
142 1.6 martin return true;
143 1.1 dholland }
144 1.1 dholland
145 1.1 dholland /*
146 1.1 dholland * md back-end code for menu-driven BSD disklabel editor.
147 1.1 dholland */
148 1.6 martin bool
149 1.6 martin md_make_bsd_partitions(struct install_partition_desc *install)
150 1.1 dholland {
151 1.6 martin return make_bsd_partitions(install);
152 1.1 dholland }
153 1.1 dholland
154 1.1 dholland /*
155 1.1 dholland * any additional partition validation
156 1.1 dholland */
157 1.6 martin bool
158 1.6 martin md_check_partitions(struct install_partition_desc *install)
159 1.1 dholland {
160 1.1 dholland /* X68k partitions must be in order of the range. */
161 1.6 martin daddr_t last_end = 0;
162 1.6 martin size_t i;
163 1.6 martin char desc[STRSIZE];
164 1.6 martin
165 1.6 martin for (i = 0; i < install->num; i++) {
166 1.6 martin if (i > 0) {
167 1.6 martin if (install->infos[i].cur_start < last_end) {
168 1.6 martin snprintf(desc, sizeof desc,
169 1.6 martin "%zu (%s)", i,
170 1.6 martin install->infos[i].mount);
171 1.7 christos msg_fmt_display(MSG_ordering, "%s", desc);
172 1.4 martin if (ask_yesno(NULL))
173 1.6 martin return false;
174 1.1 dholland }
175 1.1 dholland }
176 1.6 martin last_end = install->infos[i].cur_start + install->infos[i].size;
177 1.1 dholland }
178 1.1 dholland
179 1.6 martin return true;
180 1.1 dholland }
181 1.1 dholland
182 1.1 dholland #ifdef notyet
183 1.1 dholland static int
184 1.1 dholland md_check_partitions(void)
185 1.1 dholland {
186 1.1 dholland int i, j;
187 1.1 dholland int preserve;
188 1.1 dholland
189 1.1 dholland /* check existing BSD partitions. */
190 1.1 dholland for (i = 0; i < NDOSPART; i++) {
191 1.1 dholland if (md_disklabel.dosparts[i].dp_size == 0)
192 1.1 dholland break;
193 1.1 dholland if (memcmp(md_disklabel.dosparts[i].dp_typename, "Human68k", 8)) {
194 1.1 dholland msg_display(MSG_existing);
195 1.4 martin preserve = ask_noyes(NULL);
196 1.1 dholland break;
197 1.1 dholland }
198 1.1 dholland }
199 1.2 martin emptylabel(pm->bsdlabel);
200 1.2 martin pm->bsdlabel[C].pi_fstype = FS_UNUSED;
201 1.2 martin pm->bsdlabel[C].pi_offset = 0;
202 1.2 martin pm->bsdlabel[C].pi_size = pm->dlsize;
203 1.1 dholland for (i = 0, j = A; i < NDOSPART;) {
204 1.1 dholland if (j == C) {
205 1.1 dholland j++;
206 1.1 dholland continue;
207 1.1 dholland }
208 1.1 dholland if (!preserve &&
209 1.1 dholland memcmp(md_disklabel.dosparts[i].dp_typename,
210 1.1 dholland "Human68k", 8)) {
211 1.1 dholland /* discard it. */
212 1.1 dholland i++;
213 1.1 dholland continue;
214 1.1 dholland }
215 1.2 martin pm->bsdlabel[j].pi_fstype = (i == 1) ? FS_SWAP : FS_BSDFFS;
216 1.2 martin pm->bsdlabel[j].pi_offset = md_disklabel.dosparts[i].dp_start*2;
217 1.2 martin pm->bsdlabel[j].pi_size = md_disklabel.dosparts[i].dp_size*2;
218 1.1 dholland i++;
219 1.1 dholland j++;
220 1.1 dholland }
221 1.1 dholland if (j > 6) {
222 1.7 christos msg_fmt_display(MSG_nofreepart, "%s" pm->diskdev);
223 1.1 dholland return 0;
224 1.1 dholland }
225 1.1 dholland md_nfreepart = 8 - j;
226 1.1 dholland
227 1.1 dholland /* check for free space */
228 1.2 martin fspm->ptsize = pm->bsdlabel[A].pi_offset - 64;
229 1.2 martin if (fpm->ptsize <= 0) { /* XXX: should not be 0; minfsdb? */
230 1.7 christos msg_fmt_display(MSG_notfirst, "%s", pm->diskdev);
231 1.1 dholland process_menu(MENU_ok);
232 1.1 dholland exit(1);
233 1.1 dholland }
234 1.1 dholland
235 1.1 dholland /* Partitions should be preserved in md_make_bsdpartitions() */
236 1.1 dholland }
237 1.1 dholland #endif /* notyet */
238 1.1 dholland
239 1.1 dholland /*
240 1.1 dholland * hook called before writing new disklabel.
241 1.1 dholland */
242 1.6 martin bool
243 1.6 martin md_pre_disklabel(struct install_partition_desc *install,
244 1.6 martin struct disk_partitions *parts)
245 1.1 dholland {
246 1.1 dholland if (md_need_newdisk)
247 1.1 dholland md_newdisk ();
248 1.6 martin return true;
249 1.1 dholland }
250 1.1 dholland
251 1.1 dholland /*
252 1.1 dholland * hook called after writing disklabel to new target disk.
253 1.1 dholland */
254 1.6 martin bool
255 1.6 martin md_post_disklabel(struct install_partition_desc *install,
256 1.6 martin struct disk_partitions *parts)
257 1.1 dholland {
258 1.6 martin return true;
259 1.1 dholland }
260 1.1 dholland
261 1.1 dholland /*
262 1.1 dholland * hook called after upgrade() or install() has finished setting
263 1.1 dholland * up the target disk but immediately before the user is given the
264 1.1 dholland * ``disks are now set up'' message.
265 1.1 dholland *
266 1.1 dholland * On the x68k, we use this opportunity to install the boot blocks.
267 1.1 dholland */
268 1.1 dholland int
269 1.6 martin md_post_newfs(struct install_partition_desc *install)
270 1.1 dholland {
271 1.1 dholland /* boot blocks ... */
272 1.7 christos msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
273 1.1 dholland cp_to_target("/usr/mdec/boot", "/boot");
274 1.1 dholland if (run_program(RUN_DISPLAY | RUN_NO_CLEAR,
275 1.1 dholland "/usr/mdec/installboot.new /usr/mdec/sdboot_ufs /dev/r%sa",
276 1.2 martin pm->diskdev))
277 1.1 dholland process_menu(MENU_ok,
278 1.5 joerg __UNCONST("Warning: disk is probably not bootable"));
279 1.1 dholland return 0;
280 1.1 dholland }
281 1.1 dholland
282 1.1 dholland int
283 1.6 martin md_post_extract(struct install_partition_desc *install)
284 1.1 dholland {
285 1.1 dholland return 0;
286 1.1 dholland }
287 1.1 dholland
288 1.1 dholland void
289 1.6 martin md_cleanup_install(struct install_partition_desc *install)
290 1.1 dholland {
291 1.1 dholland #ifndef DEBUG
292 1.1 dholland enable_rc_conf();
293 1.1 dholland #endif
294 1.1 dholland }
295 1.1 dholland
296 1.1 dholland int
297 1.6 martin md_pre_update(struct install_partition_desc *install)
298 1.1 dholland {
299 1.1 dholland return 1;
300 1.1 dholland }
301 1.1 dholland
302 1.1 dholland /* Upgrade support */
303 1.1 dholland int
304 1.6 martin md_update(struct install_partition_desc *install)
305 1.1 dholland {
306 1.6 martin md_post_newfs(install);
307 1.1 dholland return 1;
308 1.1 dholland }
309 1.1 dholland
310 1.1 dholland static int
311 1.1 dholland md_newdisk(void)
312 1.1 dholland {
313 1.7 christos msg_fmt_display(MSG_newdisk, "%s%s", pm->diskdev, pm->diskdev);
314 1.1 dholland
315 1.1 dholland return run_program(RUN_FATAL|RUN_DISPLAY,
316 1.2 martin "/usr/mdec/newdisk -v %s", pm->diskdev);
317 1.1 dholland }
318 1.1 dholland
319 1.1 dholland
320 1.1 dholland int
321 1.6 martin md_pre_mount(struct install_partition_desc *install)
322 1.1 dholland {
323 1.1 dholland return 0;
324 1.1 dholland }
325 1.6 martin
326 1.6 martin bool
327 1.6 martin md_parts_use_wholedisk(struct disk_partitions *parts)
328 1.6 martin {
329 1.6 martin return parts_use_wholedisk(parts, 0, NULL);
330 1.6 martin }
331 1.6 martin
332 1.6 martin #ifdef HAVE_GPT
333 1.6 martin bool
334 1.6 martin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
335 1.6 martin bool root_is_new, part_id efi_id, bool efi_is_new)
336 1.6 martin {
337 1.6 martin /* no GPT boot support, nothing needs to be done here */
338 1.6 martin return true;
339 1.6 martin }
340 1.6 martin #endif
341 1.6 martin
342