1 /* $NetBSD: md.c,v 1.12 2024/02/10 18:43:54 andvar 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/ioctl.h>
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <curses.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <util.h>
47
48 #include "defs.h"
49 #include "md.h"
50 #include "msg_defs.h"
51 #include "menu_defs.h"
52
53 #define PART_BOOT_BSIZE 4096
54 #define PART_BOOT_FSIZE 512
55
56 void
md_init(void)57 md_init(void)
58 {
59 }
60
61 void
md_init_set_status(int flags)62 md_init_set_status(int flags)
63 {
64
65 (void)flags;
66 }
67
68 bool
md_get_info(struct install_partition_desc * install)69 md_get_info(struct install_partition_desc *install)
70 {
71 struct disklabel disklabel;
72 int fd;
73 char dev_name[100];
74
75 snprintf(dev_name, sizeof(dev_name), "/dev/r%sc", pm->diskdev);
76
77 fd = open(dev_name, O_RDONLY, 0);
78 if (fd < 0) {
79 endwin();
80 fprintf (stderr, "Can't open %s\n", dev_name);
81 exit(1);
82 }
83 if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
84 endwin();
85 fprintf (stderr, "Can't read disklabel on %s.\n", dev_name);
86 close(fd);
87 exit(1);
88 }
89 close(fd);
90
91 pm->dlcyl = disklabel.d_ncylinders;
92 pm->dlhead = disklabel.d_ntracks;
93 pm->dlsec = disklabel.d_nsectors;
94 pm->sectorsize = disklabel.d_secsize;
95 pm->dlcylsize = disklabel.d_secpercyl;
96
97 /*
98 * Compute whole disk size. Take max of (pm->dlcyl*pm->dlhead*pm->dlsec)
99 * and secperunit, just in case the disk is already labelled.
100 * (If our new label's RAW_PART size ends up smaller than the
101 * in-core RAW_PART size value, updating the label will fail.)
102 */
103 pm->dlsize = pm->dlcyl * pm->dlhead * pm->dlsec;
104 if (disklabel.d_secperunit > pm->dlsize)
105 pm->dlsize = disklabel.d_secperunit;
106
107 return true;
108 }
109
110 /*
111 * md back-end code for menu-driven BSD disklabel editor.
112 */
113 int
md_make_bsd_partitions(struct install_partition_desc * install)114 md_make_bsd_partitions(struct install_partition_desc *install)
115 {
116
117 return make_bsd_partitions(install);
118 }
119
120 static part_id
find_boot_part(struct install_partition_desc * install)121 find_boot_part(struct install_partition_desc *install)
122 {
123 for (size_t i = 0; i < install->num; i++) {
124 if (install->infos[i].fs_type != PART_BOOT_TYPE)
125 continue;
126 if (install->infos[i].fs_version != PART_BOOT_SUBT)
127 continue;
128 if (install->infos[i].size < (PART_BOOT/512))
129 continue;
130 if (install->infos[i].cur_start > 0)
131 continue;
132 return i;
133 }
134 return NO_PART;
135 }
136
137 /*
138 * any additional partition validation
139 */
140 bool
md_check_partitions(struct install_partition_desc * install)141 md_check_partitions(struct install_partition_desc *install)
142 {
143
144 /*
145 * Make sure that a boot partition (old 4.3BSD UFS) is prepared
146 * properly for our native bootloader.
147 */
148 if (find_boot_part(install) != NO_PART)
149 return true;
150
151 msg_display(MSG_nobootpartdisklabel);
152 process_menu(MENU_ok, NULL);
153 return false;
154 }
155
156 /*
157 * hook called before writing new disklabel.
158 */
159 bool
md_pre_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)160 md_pre_disklabel(struct install_partition_desc *install,
161 struct disk_partitions *parts)
162 {
163
164 return true;
165 }
166
167 /*
168 * hook called after writing disklabel to new target disk.
169 */
170 bool
md_post_disklabel(struct install_partition_desc * install,struct disk_partitions * parts)171 md_post_disklabel(struct install_partition_desc *install,
172 struct disk_partitions *parts)
173 {
174 return true;
175 }
176
177 static int
copy_bootloader(const char * diskdev)178 copy_bootloader(const char *diskdev)
179 {
180 const char *mntdir = "/mnt2";
181
182 msg_fmt_display(MSG_copybootloader, "%s", diskdev);
183 if (!run_program(RUN_SILENT | RUN_ERROR_OK,
184 "mount %s %s", diskdev, mntdir)) {
185 mnt2_mounted = 1;
186 run_program(0, "/bin/cp /usr/mdec/boot %s", mntdir);
187 run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir);
188 mnt2_mounted = 0;
189 } else {
190 /* XXX print proper error message */
191 return 1;
192 }
193 return 0;
194 }
195
196 /*
197 * hook called after install() has finished setting up the target disk
198 * but immediately before the user is given the ``disks are now set up''
199 * message.
200 */
201 int
md_post_newfs(struct install_partition_desc * install)202 md_post_newfs(struct install_partition_desc *install)
203 {
204 char rdisk[STRSIZE], disk[STRSIZE];
205 part_id boot_part = find_boot_part(install);
206
207 if (boot_part == NO_PART)
208 return 1;
209
210 if (!install->infos[boot_part].parts->pscheme->get_part_device(
211 install->infos[boot_part].parts,
212 install->infos[boot_part].cur_part_id,
213 rdisk, sizeof rdisk, NULL, raw_dev_name, true, true))
214 return 1;
215 if (!install->infos[boot_part].parts->pscheme->get_part_device(
216 install->infos[boot_part].parts,
217 install->infos[boot_part].cur_part_id,
218 disk, sizeof disk, NULL, plain_name, true, true))
219 return 1;
220
221 if (run_program(RUN_DISPLAY | RUN_PROGRESS,
222 "/sbin/newfs -V2 -O 0 -b %d -f %d %s",
223 PART_BOOT_BSIZE, PART_BOOT_FSIZE, rdisk))
224 return 1;
225 return copy_bootloader(disk);
226 }
227
228 int
md_post_extract(struct install_partition_desc * install,bool upgrade)229 md_post_extract(struct install_partition_desc *install, bool upgrade)
230 {
231
232 return 0;
233 }
234
235 void
md_cleanup_install(struct install_partition_desc * install)236 md_cleanup_install(struct install_partition_desc *install)
237 {
238
239 #ifndef DEBUG
240 enable_rc_conf();
241 #endif
242 msg_display(MSG_howtoboot);
243 process_menu(MENU_ok, NULL);
244 }
245
246 int
md_pre_update(struct install_partition_desc * install)247 md_pre_update(struct install_partition_desc *install)
248 {
249 return 1;
250 }
251
252 /* Upgrade support */
253 int
md_update(struct install_partition_desc * install)254 md_update(struct install_partition_desc *install)
255 {
256 const char *mntdir = "/mnt2";
257 char bootpath[MAXPATHLEN];
258 struct stat sb;
259 bool hasboot = false;
260 char disk[STRSIZE];
261 part_id boot_part = find_boot_part(install);
262
263 if (boot_part == NO_PART)
264 return 0;
265
266 if (!install->infos[boot_part].parts->pscheme->get_part_device(
267 install->infos[boot_part].parts,
268 install->infos[boot_part].cur_part_id,
269 disk, sizeof disk, NULL, plain_name, true, true))
270 return 0;
271
272 /*
273 * Check if there is a boot UFS partition and it has the old bootloader.
274 * We'll update bootloader only if the old one was installed.
275 */
276 if (!run_program(RUN_SILENT | RUN_ERROR_OK,
277 "mount -r %s %s", disk, mntdir)) {
278 mnt2_mounted = 1;
279 snprintf(bootpath, sizeof(bootpath), "%s/%s", mntdir, "boot");
280 if (stat(bootpath, &sb) == 0 && S_ISREG(sb.st_mode))
281 hasboot = true;
282 run_program(RUN_SILENT | RUN_ERROR_OK, "umount %s", mntdir);
283 mnt2_mounted = 0;
284 if (hasboot)
285 (void)copy_bootloader(disk);
286 }
287 return 1;
288 }
289
290 int
md_pre_mount(struct install_partition_desc * install,size_t ndx)291 md_pre_mount(struct install_partition_desc *install, size_t ndx)
292 {
293 return 0;
294 }
295
296 bool
md_parts_use_wholedisk(struct disk_partitions * parts)297 md_parts_use_wholedisk(struct disk_partitions *parts)
298 {
299 return parts_use_wholedisk(parts, 0, NULL);
300 }
301
302 #ifdef HAVE_GPT
303 bool
md_gpt_post_write(struct disk_partitions * parts,part_id root_id,bool root_is_new,part_id efi_id,bool efi_is_new)304 md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
305 bool root_is_new, part_id efi_id, bool efi_is_new)
306 {
307 /* no GPT boot support, nothing needs to be done here */
308 return true;
309 }
310 #endif
311