md.c revision 1.3.6.2 1 1.3.6.2 tls /* $NetBSD: md.c,v 1.3.6.2 2014/08/20 00:05:16 tls Exp $ */
2 1.3.6.2 tls
3 1.3.6.2 tls /*
4 1.3.6.2 tls * Copyright 1997 Piermont Information Systems Inc.
5 1.3.6.2 tls * All rights reserved.
6 1.3.6.2 tls *
7 1.3.6.2 tls * Based on code written by Philip A. Nelson for Piermont Information
8 1.3.6.2 tls * Systems Inc.
9 1.3.6.2 tls *
10 1.3.6.2 tls * Redistribution and use in source and binary forms, with or without
11 1.3.6.2 tls * modification, are permitted provided that the following conditions
12 1.3.6.2 tls * are met:
13 1.3.6.2 tls * 1. Redistributions of source code must retain the above copyright
14 1.3.6.2 tls * notice, this list of conditions and the following disclaimer.
15 1.3.6.2 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.6.2 tls * notice, this list of conditions and the following disclaimer in the
17 1.3.6.2 tls * documentation and/or other materials provided with the distribution.
18 1.3.6.2 tls * 3. The name of Piermont Information Systems Inc. may not be used to endorse
19 1.3.6.2 tls * or promote products derived from this software without specific prior
20 1.3.6.2 tls * written permission.
21 1.3.6.2 tls *
22 1.3.6.2 tls * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
23 1.3.6.2 tls * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.3.6.2 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.3.6.2 tls * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
26 1.3.6.2 tls * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.3.6.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.3.6.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.3.6.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.3.6.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.3.6.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.3.6.2 tls * THE POSSIBILITY OF SUCH DAMAGE.
33 1.3.6.2 tls */
34 1.3.6.2 tls
35 1.3.6.2 tls /* md.c -- luna68k machine specific routines */
36 1.3.6.2 tls
37 1.3.6.2 tls #include <sys/types.h>
38 1.3.6.2 tls #include <sys/disklabel.h>
39 1.3.6.2 tls #include <sys/ioctl.h>
40 1.3.6.2 tls #include <sys/param.h>
41 1.3.6.2 tls #include <sys/stat.h>
42 1.3.6.2 tls #include <stdio.h>
43 1.3.6.2 tls #include <stdlib.h>
44 1.3.6.2 tls #include <curses.h>
45 1.3.6.2 tls #include <unistd.h>
46 1.3.6.2 tls #include <fcntl.h>
47 1.3.6.2 tls #include <util.h>
48 1.3.6.2 tls
49 1.3.6.2 tls #include "defs.h"
50 1.3.6.2 tls #include "md.h"
51 1.3.6.2 tls #include "msg_defs.h"
52 1.3.6.2 tls #include "menu_defs.h"
53 1.3.6.2 tls
54 1.3.6.2 tls #define PART_BOOT_BSIZE 4096
55 1.3.6.2 tls #define PART_BOOT_FSIZE 512
56 1.3.6.2 tls
57 1.3.6.2 tls void
58 1.3.6.2 tls md_init(void)
59 1.3.6.2 tls {
60 1.3.6.2 tls }
61 1.3.6.2 tls
62 1.3.6.2 tls void
63 1.3.6.2 tls md_init_set_status(int flags)
64 1.3.6.2 tls {
65 1.3.6.2 tls
66 1.3.6.2 tls (void)flags;
67 1.3.6.2 tls }
68 1.3.6.2 tls
69 1.3.6.2 tls int
70 1.3.6.2 tls md_get_info(void)
71 1.3.6.2 tls {
72 1.3.6.2 tls struct disklabel disklabel;
73 1.3.6.2 tls int fd;
74 1.3.6.2 tls char dev_name[100];
75 1.3.6.2 tls
76 1.3.6.2 tls snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
77 1.3.6.2 tls
78 1.3.6.2 tls fd = open(dev_name, O_RDONLY, 0);
79 1.3.6.2 tls if (fd < 0) {
80 1.3.6.2 tls endwin();
81 1.3.6.2 tls fprintf (stderr, "Can't open %s\n", dev_name);
82 1.3.6.2 tls exit(1);
83 1.3.6.2 tls }
84 1.3.6.2 tls if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
85 1.3.6.2 tls endwin();
86 1.3.6.2 tls fprintf (stderr, "Can't read disklabel on %s.\n", dev_name);
87 1.3.6.2 tls close(fd);
88 1.3.6.2 tls exit(1);
89 1.3.6.2 tls }
90 1.3.6.2 tls close(fd);
91 1.3.6.2 tls
92 1.3.6.2 tls pm->dlcyl = disklabel.d_ncylinders;
93 1.3.6.2 tls pm->dlhead = disklabel.d_ntracks;
94 1.3.6.2 tls pm->dlsec = disklabel.d_nsectors;
95 1.3.6.2 tls pm->sectorsize = disklabel.d_secsize;
96 1.3.6.2 tls pm->dlcylsize = disklabel.d_secpercyl;
97 1.3.6.2 tls
98 1.3.6.2 tls /*
99 1.3.6.2 tls * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
100 1.3.6.2 tls * and secperunit, just in case the disk is already labelled.
101 1.3.6.2 tls * (If our new label's RAW_PART size ends up smaller than the
102 1.3.6.2 tls * in-core RAW_PART size value, updating the label will fail.)
103 1.3.6.2 tls */
104 1.3.6.2 tls pm->dlsize = pm->dlcyl * pm->dlhead * pm->dlsec;
105 1.3.6.2 tls if (disklabel.d_secperunit > pm->dlsize)
106 1.3.6.2 tls pm->dlsize = disklabel.d_secperunit;
107 1.3.6.2 tls
108 1.3.6.2 tls return 1;
109 1.3.6.2 tls }
110 1.3.6.2 tls
111 1.3.6.2 tls /*
112 1.3.6.2 tls * md back-end code for menu-driven BSD disklabel editor.
113 1.3.6.2 tls */
114 1.3.6.2 tls int
115 1.3.6.2 tls md_make_bsd_partitions(void)
116 1.3.6.2 tls {
117 1.3.6.2 tls
118 1.3.6.2 tls return make_bsd_partitions();
119 1.3.6.2 tls }
120 1.3.6.2 tls
121 1.3.6.2 tls /*
122 1.3.6.2 tls * any additional partition validation
123 1.3.6.2 tls */
124 1.3.6.2 tls int
125 1.3.6.2 tls md_check_partitions(void)
126 1.3.6.2 tls {
127 1.3.6.2 tls
128 1.3.6.2 tls /*
129 1.3.6.2 tls * Make sure that a boot partition (old 4.3BSD UFS) is prepared
130 1.3.6.2 tls * properly for our native bootloader.
131 1.3.6.2 tls */
132 1.3.6.2 tls if (pm->bsdlabel[PART_BOOT].pi_fstype != FS_BSDFFS ||
133 1.3.6.2 tls (pm->bsdlabel[PART_BOOT].pi_flags & PIF_NEWFS) == 0) {
134 1.3.6.2 tls msg_display(MSG_nobootpartdisklabel);
135 1.3.6.2 tls process_menu(MENU_ok, NULL);
136 1.3.6.2 tls return 0;
137 1.3.6.2 tls }
138 1.3.6.2 tls return 1;
139 1.3.6.2 tls }
140 1.3.6.2 tls
141 1.3.6.2 tls /*
142 1.3.6.2 tls * hook called before writing new disklabel.
143 1.3.6.2 tls */
144 1.3.6.2 tls int
145 1.3.6.2 tls md_pre_disklabel(void)
146 1.3.6.2 tls {
147 1.3.6.2 tls
148 1.3.6.2 tls return 0;
149 1.3.6.2 tls }
150 1.3.6.2 tls
151 1.3.6.2 tls /*
152 1.3.6.2 tls * hook called after writing disklabel to new target disk.
153 1.3.6.2 tls */
154 1.3.6.2 tls int
155 1.3.6.2 tls md_post_disklabel(void)
156 1.3.6.2 tls {
157 1.3.6.2 tls
158 1.3.6.2 tls if (get_ramsize() <= 32)
159 1.3.6.2 tls set_swap(pm->diskdev, pm->bsdlabel);
160 1.3.6.2 tls
161 1.3.6.2 tls return 0;
162 1.3.6.2 tls }
163 1.3.6.2 tls
164 1.3.6.2 tls static int
165 1.3.6.2 tls copy_bootloader(void)
166 1.3.6.2 tls {
167 1.3.6.2 tls const char *mntdir = "/mnt2";
168 1.3.6.2 tls
169 1.3.6.2 tls msg_display(MSG_copybootloader, pm->diskdev);
170 1.3.6.2 tls if (!run_program(RUN_SILENT | RUN_ERROR_OK,
171 1.3.6.2 tls "mount /dev/%s%c %s", pm->diskdev, 'a' + PART_BOOT, mntdir)) {
172 1.3.6.2 tls mnt2_mounted = 1;
173 1.3.6.2 tls run_program(0, "/bin/cp /usr/mdec/boot %s", mntdir);
174 1.3.6.2 tls run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir);
175 1.3.6.2 tls mnt2_mounted = 0;
176 1.3.6.2 tls } else {
177 1.3.6.2 tls /* XXX print proper error message */
178 1.3.6.2 tls return 1;
179 1.3.6.2 tls }
180 1.3.6.2 tls return 0;
181 1.3.6.2 tls }
182 1.3.6.2 tls
183 1.3.6.2 tls /*
184 1.3.6.2 tls * hook called after install() has finished setting up the target disk
185 1.3.6.2 tls * but immediately before the user is given the ``disks are now set up''
186 1.3.6.2 tls * message.
187 1.3.6.2 tls */
188 1.3.6.2 tls int
189 1.3.6.2 tls md_post_newfs(void)
190 1.3.6.2 tls {
191 1.3.6.2 tls
192 1.3.6.2 tls if (run_program(RUN_DISPLAY | RUN_PROGRESS,
193 1.3.6.2 tls "/sbin/newfs -V2 -O 0 -b %d -f %d /dev/r%s%c",
194 1.3.6.2 tls PART_BOOT_BSIZE, PART_BOOT_FSIZE, pm->diskdev, 'a' + PART_BOOT))
195 1.3.6.2 tls return 1;
196 1.3.6.2 tls return copy_bootloader();
197 1.3.6.2 tls }
198 1.3.6.2 tls
199 1.3.6.2 tls int
200 1.3.6.2 tls md_post_extract(void)
201 1.3.6.2 tls {
202 1.3.6.2 tls
203 1.3.6.2 tls return 0;
204 1.3.6.2 tls }
205 1.3.6.2 tls
206 1.3.6.2 tls void
207 1.3.6.2 tls md_cleanup_install(void)
208 1.3.6.2 tls {
209 1.3.6.2 tls
210 1.3.6.2 tls #ifndef DEBUG
211 1.3.6.2 tls enable_rc_conf();
212 1.3.6.2 tls #endif
213 1.3.6.2 tls msg_display(MSG_howtoboot);
214 1.3.6.2 tls process_menu(MENU_ok, NULL);
215 1.3.6.2 tls }
216 1.3.6.2 tls
217 1.3.6.2 tls int
218 1.3.6.2 tls md_pre_update(void)
219 1.3.6.2 tls {
220 1.3.6.2 tls
221 1.3.6.2 tls if (get_ramsize() <= 32)
222 1.3.6.2 tls set_swap(pm->diskdev, pm->bsdlabel);
223 1.3.6.2 tls
224 1.3.6.2 tls return 1;
225 1.3.6.2 tls }
226 1.3.6.2 tls
227 1.3.6.2 tls /* Upgrade support */
228 1.3.6.2 tls int
229 1.3.6.2 tls md_update(void)
230 1.3.6.2 tls {
231 1.3.6.2 tls const char *mntdir = "/mnt2";
232 1.3.6.2 tls char bootpath[MAXPATHLEN];
233 1.3.6.2 tls struct stat sb;
234 1.3.6.2 tls bool hasboot = false;
235 1.3.6.2 tls
236 1.3.6.2 tls /*
237 1.3.6.2 tls * Check if there is a boot UFS parttion and it has the old bootloader.
238 1.3.6.2 tls * We'll update bootloader only if the old one was installed.
239 1.3.6.2 tls */
240 1.3.6.2 tls if (!run_program(RUN_SILENT | RUN_ERROR_OK,
241 1.3.6.2 tls "mount -r /dev/%s%c %s", pm->diskdev, 'a' + PART_BOOT, mntdir)) {
242 1.3.6.2 tls mnt2_mounted = 1;
243 1.3.6.2 tls snprintf(bootpath, sizeof(bootpath), "%s/%s", mntdir, "boot");
244 1.3.6.2 tls if (stat(bootpath, &sb) == 0 && S_ISREG(sb.st_mode))
245 1.3.6.2 tls hasboot = true;
246 1.3.6.2 tls run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir);
247 1.3.6.2 tls mnt2_mounted = 0;
248 1.3.6.2 tls if (hasboot)
249 1.3.6.2 tls (void)copy_bootloader();
250 1.3.6.2 tls }
251 1.3.6.2 tls return 1;
252 1.3.6.2 tls }
253 1.3.6.2 tls
254 1.3.6.2 tls int
255 1.3.6.2 tls md_pre_mount()
256 1.3.6.2 tls {
257 1.3.6.2 tls return 0;
258 1.3.6.2 tls }
259