md.c revision 1.2 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 -- luna68k machine specific routines */
36
37 #include <sys/types.h>
38 #include <sys/disklabel.h>
39 #include <sys/ioctl.h>
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <curses.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <util.h>
48
49 #include "defs.h"
50 #include "md.h"
51 #include "msg_defs.h"
52 #include "menu_defs.h"
53
54 #define PART_BOOT_BSIZE 4096
55 #define PART_BOOT_FSIZE 512
56
57 void
58 md_init(void)
59 {
60 }
61
62 void
63 md_init_set_status(int flags)
64 {
65
66 (void)flags;
67 }
68
69 int
70 md_get_info(void)
71 {
72 struct disklabel disklabel;
73 int fd;
74 char dev_name[100];
75
76 snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
77
78 fd = open(dev_name, O_RDONLY, 0);
79 if (fd < 0) {
80 endwin();
81 fprintf (stderr, "Can't open %s\n", dev_name);
82 exit(1);
83 }
84 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
85 endwin();
86 fprintf (stderr, "Can't read disklabel on %s.\n", dev_name);
87 close(fd);
88 exit(1);
89 }
90 close(fd);
91
92 pm->dlcyl = disklabel.d_ncylinders;
93 pm->dlhead = disklabel.d_ntracks;
94 pm->dlsec = disklabel.d_nsectors;
95 pm->sectorsize = disklabel.d_secsize;
96 pm->dlcylsize = disklabel.d_secpercyl;
97
98 /*
99 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
100 * and secperunit, just in case the disk is already labelled.
101 * (If our new label's RAW_PART size ends up smaller than the
102 * in-core RAW_PART size value, updating the label will fail.)
103 */
104 pm->dlsize = pm->dlcyl * pm->dlhead * pm->dlsec;
105 if (disklabel.d_secperunit > pm->dlsize)
106 pm->dlsize = disklabel.d_secperunit;
107
108 return 1;
109 }
110
111 /*
112 * md back-end code for menu-driven BSD disklabel editor.
113 */
114 int
115 md_make_bsd_partitions(void)
116 {
117
118 return make_bsd_partitions();
119 }
120
121 /*
122 * any additional partition validation
123 */
124 int
125 md_check_partitions(void)
126 {
127
128 /*
129 * Make sure that a boot partition (old 4.3BSD UFS) is prepared
130 * properly for our native bootloader.
131 */
132 if (bsdlabel[PART_BOOT].pi_fstype != FS_BSDFFS ||
133 (bsdlabel[PART_BOOT].pi_flags & PIF_NEWFS) == 0) {
134 msg_display(MSG_nobootpartdisklabel);
135 process_menu(MENU_ok, NULL);
136 return 0;
137 }
138 return 1;
139 }
140
141 /*
142 * hook called before writing new disklabel.
143 */
144 int
145 md_pre_disklabel(void)
146 {
147
148 return 0;
149 }
150
151 /*
152 * hook called after writing disklabel to new target disk.
153 */
154 int
155 md_post_disklabel(void)
156 {
157
158 if (get_ramsize() <= 32)
159 set_swap(diskdev, bsdlabel);
160
161 return 0;
162 }
163
164 static int
165 copy_bootloader(void)
166 {
167 const char *mntdir = "/mnt2";
168
169 msg_display(MSG_copybootloader, diskdev);
170 if (!run_program(RUN_SILENT | RUN_ERROR_OK,
171 "mount /dev/%s%c %s", diskdev, 'a' + PART_BOOT, mntdir)) {
172 mnt2_mounted = 1;
173 run_program(0, "/bin/cp /usr/mdec/boot %s", mntdir);
174 run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir);
175 mnt2_mounted = 0;
176 } else {
177 /* XXX print proper error message */
178 return 1;
179 }
180 return 0;
181 }
182
183 /*
184 * hook called after install() has finished setting up the target disk
185 * but immediately before the user is given the ``disks are now set up''
186 * message.
187 */
188 int
189 md_post_newfs(void)
190 {
191
192 if (run_program(RUN_DISPLAY | RUN_PROGRESS,
193 "/sbin/newfs -V2 -O 0 -b %d -f %d /dev/r%s%c",
194 PART_BOOT_BSIZE, PART_BOOT_FSIZE, pm->diskdev, 'a' + PART_BOOT))
195 return 1;
196 return copy_bootloader();
197 }
198
199 int
200 md_post_extract(void)
201 {
202
203 return 0;
204 }
205
206 void
207 md_cleanup_install(void)
208 {
209
210 #ifndef DEBUG
211 enable_rc_conf();
212 #endif
213 msg_display(MSG_howtoboot);
214 process_menu(MENU_ok, NULL);
215 }
216
217 int
218 md_pre_update(void)
219 {
220
221 if (get_ramsize() <= 32)
222 set_swap(diskdev, bsdlabel);
223
224 return 1;
225 }
226
227 /* Upgrade support */
228 int
229 md_update(void)
230 {
231 const char *mntdir = "/mnt2";
232 char bootpath[MAXPATHLEN];
233 struct stat sb;
234 bool hasboot = false;
235
236 /*
237 * Check if there is a boot UFS parttion and it has the old bootloader.
238 * We'll update bootloader only if the old one was installed.
239 */
240 if (!run_program(RUN_SILENT | RUN_ERROR_OK,
241 "mount -r /dev/%s%c %s", diskdev, 'a' + PART_BOOT, mntdir)) {
242 mnt2_mounted = 1;
243 snprintf(bootpath, sizeof(bootpath), "%s/%s", mntdir, "boot");
244 if (stat(bootpath, &sb) == 0 && S_ISREG(sb.st_mode))
245 hasboot = true;
246 run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir);
247 mnt2_mounted = 0;
248 if (hasboot)
249 (void)copy_bootloader();
250 }
251 return 1;
252 }
253
254 int
255 md_pre_mount()
256 {
257 return 0;
258 }
259