md.c revision 1.19 1 1.19 martin /* $NetBSD: md.c,v 1.19 2019/07/13 17:13:38 martin 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.
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 -- i386 machine specific routines - also used by amd64 */
36 1.1 dholland
37 1.1 dholland #include <sys/param.h>
38 1.1 dholland #include <sys/sysctl.h>
39 1.1 dholland #include <sys/exec.h>
40 1.1 dholland #include <sys/utsname.h>
41 1.1 dholland #include <sys/types.h>
42 1.1 dholland #include <sys/stat.h>
43 1.1 dholland #include <machine/cpu.h>
44 1.15 martin #include <assert.h>
45 1.1 dholland #include <stdio.h>
46 1.1 dholland #include <stddef.h>
47 1.1 dholland #include <util.h>
48 1.1 dholland #include <dirent.h>
49 1.1 dholland #include <termios.h>
50 1.1 dholland
51 1.1 dholland #include "defs.h"
52 1.1 dholland #include "md.h"
53 1.1 dholland #include "endian.h"
54 1.1 dholland #include "msg_defs.h"
55 1.1 dholland #include "menu_defs.h"
56 1.1 dholland
57 1.1 dholland #ifdef NO_LBA_READS /* for testing */
58 1.1 dholland #undef BIFLAG_EXTINT13
59 1.1 dholland #define BIFLAG_EXTINT13 0
60 1.1 dholland #endif
61 1.1 dholland
62 1.1 dholland static struct biosdisk_info *biosdisk = NULL;
63 1.1 dholland
64 1.1 dholland /* prototypes */
65 1.1 dholland
66 1.15 martin static bool get_bios_info(const char*, struct disk_partitions*, int*, int*, int*);
67 1.1 dholland static int mbr_root_above_chs(void);
68 1.1 dholland static int md_read_bootcode(const char *, struct mbr_sector *);
69 1.1 dholland static unsigned int get_bootmodel(void);
70 1.1 dholland
71 1.13 martin static int conmib[] = {CTL_MACHDEP, CPU_CONSDEV};
72 1.13 martin
73 1.1 dholland void
74 1.1 dholland md_init(void)
75 1.1 dholland {
76 1.1 dholland }
77 1.1 dholland
78 1.1 dholland void
79 1.1 dholland md_init_set_status(int flags)
80 1.1 dholland {
81 1.1 dholland (void)flags;
82 1.1 dholland
83 1.1 dholland /* Default to install same type of kernel as we are running */
84 1.1 dholland set_kernel_set(get_bootmodel());
85 1.1 dholland }
86 1.1 dholland
87 1.15 martin bool
88 1.15 martin md_get_info(struct install_partition_desc *install)
89 1.1 dholland {
90 1.15 martin int bcyl = 0, bhead = 0, bsec = 0;
91 1.1 dholland
92 1.15 martin if (pm->no_mbr || pm->no_part)
93 1.15 martin return true;
94 1.1 dholland
95 1.15 martin if (pm->parts == NULL) {
96 1.1 dholland
97 1.15 martin const struct disk_partitioning_scheme *ps =
98 1.15 martin select_part_scheme(pm, NULL, true, NULL);
99 1.1 dholland
100 1.15 martin if (!ps)
101 1.15 martin return true;
102 1.1 dholland
103 1.15 martin struct disk_partitions *parts =
104 1.15 martin (*ps->create_new_for_disk)(pm->diskdev,
105 1.15 martin 0, pm->dlsize, pm->dlsize, true);
106 1.15 martin if (!parts)
107 1.15 martin return false;
108 1.1 dholland
109 1.15 martin pm->parts = parts;
110 1.15 martin if (ps->size_limit > 0 && pm->dlsize > ps->size_limit)
111 1.15 martin pm->dlsize = ps->size_limit;
112 1.1 dholland }
113 1.1 dholland
114 1.15 martin if (get_bios_info(pm->diskdev, pm->parts, &bcyl, &bhead, &bsec)
115 1.15 martin && pm->parts->pscheme->change_disk_geom != NULL)
116 1.15 martin pm->parts->pscheme->change_disk_geom(pm->parts,
117 1.15 martin bcyl, bhead, bsec);
118 1.15 martin else
119 1.15 martin set_default_sizemult(MEG/512);
120 1.1 dholland
121 1.15 martin /*
122 1.15 martin * If the selected scheme does not need two-stage partitioning
123 1.15 martin * (like GPT), do not bother to edit the outer partitions.
124 1.15 martin */
125 1.15 martin if (pm->parts->pscheme->secondary_partitions == NULL ||
126 1.15 martin pm->parts->pscheme->secondary_scheme == NULL)
127 1.15 martin return true;
128 1.1 dholland
129 1.15 martin if (pm->no_mbr || pm->no_part)
130 1.15 martin return true;
131 1.1 dholland
132 1.15 martin return edit_outer_parts(pm->parts);
133 1.1 dholland }
134 1.1 dholland
135 1.1 dholland /*
136 1.1 dholland * md back-end code for menu-driven BSD disklabel editor.
137 1.1 dholland */
138 1.15 martin bool
139 1.15 martin md_make_bsd_partitions(struct install_partition_desc *install)
140 1.1 dholland {
141 1.15 martin return make_bsd_partitions(install);
142 1.1 dholland }
143 1.1 dholland
144 1.1 dholland /*
145 1.1 dholland * any additional partition validation
146 1.1 dholland */
147 1.15 martin bool
148 1.15 martin md_check_partitions(struct install_partition_desc *install)
149 1.1 dholland {
150 1.1 dholland int rval;
151 1.1 dholland char *bootxx;
152 1.1 dholland
153 1.1 dholland /* check we have boot code for the root partition type */
154 1.15 martin bootxx = bootxx_name(install);
155 1.1 dholland rval = access(bootxx, R_OK);
156 1.1 dholland free(bootxx);
157 1.1 dholland if (rval == 0)
158 1.15 martin return true;
159 1.8 joerg process_menu(MENU_ok, __UNCONST(MSG_No_Bootcode));
160 1.15 martin return false;
161 1.1 dholland }
162 1.1 dholland
163 1.1 dholland /*
164 1.1 dholland * hook called before writing new disklabel.
165 1.1 dholland */
166 1.15 martin bool
167 1.15 martin md_pre_disklabel(struct install_partition_desc *install,
168 1.15 martin struct disk_partitions *parts)
169 1.1 dholland {
170 1.1 dholland
171 1.15 martin if (parts->parent == NULL)
172 1.15 martin return true; /* no outer partitions */
173 1.15 martin
174 1.15 martin parts = parts->parent;
175 1.15 martin
176 1.15 martin msg_display_subst(MSG_dofdisk, 3, parts->disk,
177 1.15 martin msg_string(parts->pscheme->name),
178 1.15 martin msg_string(parts->pscheme->short_name));
179 1.1 dholland
180 1.15 martin /* write edited "MBR" onto disk. */
181 1.15 martin if (!parts->pscheme->write_to_disk(parts)) {
182 1.1 dholland msg_display(MSG_wmbrfail);
183 1.1 dholland process_menu(MENU_ok, NULL);
184 1.15 martin return false;
185 1.1 dholland }
186 1.15 martin return true;
187 1.1 dholland }
188 1.1 dholland
189 1.1 dholland /*
190 1.1 dholland * hook called after writing disklabel to new target disk.
191 1.1 dholland */
192 1.15 martin bool
193 1.15 martin md_post_disklabel(struct install_partition_desc *install,
194 1.15 martin struct disk_partitions *parts)
195 1.1 dholland {
196 1.15 martin return true;
197 1.1 dholland }
198 1.1 dholland
199 1.1 dholland /*
200 1.1 dholland * hook called after upgrade() or install() has finished setting
201 1.1 dholland * up the target disk but immediately before the user is given the
202 1.1 dholland * ``disks are now set up'' message.
203 1.1 dholland */
204 1.1 dholland int
205 1.15 martin md_post_newfs(struct install_partition_desc *install)
206 1.1 dholland {
207 1.1 dholland int ret;
208 1.1 dholland size_t len;
209 1.3 riz char boot_options[1024];
210 1.1 dholland char *bootxx_filename;
211 1.1 dholland /*
212 1.3 riz * XXX - this code retains a lot of cruft from when we went
213 1.3 riz * to great pains to exclude installboot from the ramdisk
214 1.3 riz * for size reasons and should be rewritten.
215 1.1 dholland */
216 1.3 riz static const char *consoles[]={
217 1.3 riz "pc", /* CONSDEV_PC */
218 1.3 riz "com0", /* CONSDEV_COM0 */
219 1.3 riz "com1", /* CONSDEV_COM1 */
220 1.3 riz "com2", /* CONSDEV_COM2 */
221 1.3 riz "com3", /* CONSDEV_COM3 */
222 1.3 riz "com0kbd", /* CONSDEV_COM0KBD */
223 1.3 riz "com1kbd", /* CONSDEV_COM1KBD */
224 1.3 riz "com2kbd", /* CONSDEV_COM2KBD */
225 1.3 riz "com3kbd" /* CONSDEV_COM3KBD */ };
226 1.1 dholland static struct x86_boot_params boottype =
227 1.1 dholland {sizeof boottype, 0, 5, 0, 9600, { '\0' }, "", 0};
228 1.1 dholland struct termios t;
229 1.1 dholland dev_t condev;
230 1.1 dholland
231 1.9 martin if (pm == NULL || !pm->no_part) {
232 1.9 martin /*
233 1.9 martin * Get console device, should either be ttyE0 or tty0n.
234 1.9 martin * Too hard to double check, so just 'know' the device numbers.
235 1.9 martin */
236 1.9 martin len = sizeof condev;
237 1.15 martin if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
238 1.9 martin && (condev & ~3) == 0x800) {
239 1.9 martin /* Motherboard serial port */
240 1.9 martin boottype.bp_consdev = (condev & 3) + 1;
241 1.9 martin /* Defaulting the baud rate to that of stdin should suffice */
242 1.9 martin if (tcgetattr(0, &t) != -1)
243 1.9 martin boottype.bp_conspeed = t.c_ispeed;
244 1.9 martin }
245 1.9 martin
246 1.9 martin process_menu(MENU_getboottype, &boottype);
247 1.18 christos msg_fmt_display(MSG_dobootblks, "%s", pm->diskdev);
248 1.9 martin if (boottype.bp_consdev == ~0u)
249 1.9 martin /* Use existing bootblocks */
250 1.9 martin return 0;
251 1.1 dholland }
252 1.1 dholland
253 1.1 dholland ret = cp_to_target("/usr/mdec/boot", "/boot");
254 1.1 dholland if (ret)
255 1.1 dholland return ret;
256 1.9 martin if (pm && pm->no_part)
257 1.9 martin return 0;
258 1.1 dholland
259 1.15 martin bootxx_filename = bootxx_name(install);
260 1.9 martin if (bootxx_filename != NULL) {
261 1.15 martin char rdev[PATH_MAX];
262 1.15 martin
263 1.15 martin install->infos[0].parts->pscheme->get_part_device(
264 1.15 martin install->infos[0].parts, install->infos[0].cur_part_id,
265 1.15 martin rdev, sizeof rdev, NULL, raw_dev_name, true);
266 1.15 martin
267 1.3 riz snprintf(boot_options, sizeof boot_options,
268 1.3 riz "console=%s,speed=%u", consoles[boottype.bp_consdev],
269 1.3 riz boottype.bp_conspeed);
270 1.15 martin ret = run_program(RUN_DISPLAY,
271 1.15 martin "/usr/sbin/installboot -o %s %s %s",
272 1.16 martin boot_options, rdev, bootxx_filename);
273 1.15 martin free(bootxx_filename);
274 1.16 martin } else {
275 1.16 martin ret = -1;
276 1.16 martin }
277 1.16 martin
278 1.16 martin if (ret != 0)
279 1.16 martin process_menu(MENU_ok,
280 1.16 martin __UNCONST("Warning: disk is probably not bootable"));
281 1.1 dholland
282 1.1 dholland return ret;
283 1.1 dholland }
284 1.1 dholland
285 1.1 dholland int
286 1.15 martin md_post_extract(struct install_partition_desc *install)
287 1.1 dholland {
288 1.1 dholland return 0;
289 1.1 dholland }
290 1.1 dholland
291 1.1 dholland void
292 1.15 martin md_cleanup_install(struct install_partition_desc *install)
293 1.1 dholland {
294 1.13 martin size_t len;
295 1.13 martin dev_t condev;
296 1.13 martin
297 1.1 dholland #ifndef DEBUG
298 1.1 dholland enable_rc_conf();
299 1.1 dholland add_rc_conf("wscons=YES\n");
300 1.1 dholland
301 1.1 dholland # if defined(__i386__) && defined(SET_KERNEL_TINY)
302 1.1 dholland /*
303 1.1 dholland * For GENERIC_TINY, do not enable any extra screens or wsmux.
304 1.1 dholland * Otherwise, run getty on 4 VTs.
305 1.1 dholland */
306 1.1 dholland if (get_kernel_set() == SET_KERNEL_TINY)
307 1.1 dholland run_program(RUN_CHROOT,
308 1.1 dholland "sed -an -e '/^screen/s/^/#/;/^mux/s/^/#/;"
309 1.1 dholland "H;$!d;g;w /etc/wscons.conf' /etc/wscons.conf");
310 1.1 dholland else
311 1.1 dholland # endif
312 1.1 dholland run_program(RUN_CHROOT,
313 1.1 dholland "sed -an -e '/^ttyE[1-9]/s/off/on/;"
314 1.1 dholland "H;$!d;g;w /etc/ttys' /etc/ttys");
315 1.1 dholland
316 1.1 dholland #endif
317 1.13 martin
318 1.13 martin /*
319 1.13 martin * Get console device, should either be ttyE0 or tty0n.
320 1.13 martin * Too hard to double check, so just 'know' the device numbers.
321 1.13 martin */
322 1.13 martin len = sizeof condev;
323 1.15 martin if (sysctl(conmib, __arraycount(conmib), &condev, &len, NULL, 0) != -1
324 1.13 martin && (condev & ~3) != 0x800) {
325 1.13 martin
326 1.13 martin /*
327 1.13 martin * Current console is not com*, assume ttyE*.
328 1.13 martin * Modify /etc/ttys to use wsvt25 for all ports.
329 1.13 martin */
330 1.13 martin
331 1.13 martin run_program(RUN_CHROOT,
332 1.13 martin "sed -an -e 's/vt100/wsvt25/g;"
333 1.13 martin "H;$!d;g;w /etc/ttys' /etc/ttys");
334 1.13 martin }
335 1.1 dholland }
336 1.1 dholland
337 1.1 dholland int
338 1.15 martin md_pre_update(struct install_partition_desc *install)
339 1.1 dholland {
340 1.1 dholland return 1;
341 1.1 dholland }
342 1.1 dholland
343 1.1 dholland /* Upgrade support */
344 1.1 dholland int
345 1.15 martin md_update(struct install_partition_desc *install)
346 1.1 dholland {
347 1.15 martin md_post_newfs(install);
348 1.1 dholland return 1;
349 1.1 dholland }
350 1.1 dholland
351 1.1 dholland int
352 1.15 martin md_check_mbr(struct disk_partitions *parts, mbr_info_t *mbri, bool quiet)
353 1.1 dholland {
354 1.15 martin mbr_info_t *ext;
355 1.15 martin struct mbr_partition *p;
356 1.15 martin const char *bootcode;
357 1.15 martin int i, names, fl, ofl;
358 1.15 martin #define ACTIVE_FOUND 0x0100
359 1.15 martin #define NETBSD_ACTIVE 0x0200
360 1.15 martin #define NETBSD_NAMED 0x0400
361 1.15 martin #define ACTIVE_NAMED 0x0800
362 1.15 martin
363 1.15 martin root_limit = 0;
364 1.15 martin if (biosdisk != NULL && (biosdisk->bi_flags & BIFLAG_EXTINT13) == 0) {
365 1.15 martin if (mbr_root_above_chs()) {
366 1.15 martin if (quiet)
367 1.15 martin return 0;
368 1.15 martin msg_display(MSG_partabovechs);
369 1.15 martin if (!ask_noyes(NULL))
370 1.15 martin return 1;
371 1.15 martin /* The user is shooting themselves in the foot here...*/
372 1.15 martin } else {
373 1.15 martin if (parts->pscheme->size_limit)
374 1.15 martin root_limit = min(parts->pscheme->size_limit,
375 1.15 martin parts->disk_size);
376 1.15 martin else
377 1.15 martin root_limit = parts->disk_size;
378 1.15 martin }
379 1.15 martin }
380 1.15 martin
381 1.15 martin /*
382 1.15 martin * Ensure the install partition (at sector pm->ptstart) and the active
383 1.15 martin * partition are bootable.
384 1.15 martin * Determine whether the bootselect code is needed.
385 1.15 martin * Note that MBR_BS_NEWMBR is always set, so we ignore it!
386 1.15 martin */
387 1.15 martin fl = 0;
388 1.15 martin names = 0;
389 1.15 martin for (ext = mbri; ext != NULL; ext = ext->extended) {
390 1.15 martin p = ext->mbr.mbr_parts;
391 1.15 martin for (i = 0; i < MBR_PART_COUNT; p++, i++) {
392 1.15 martin if (p->mbrp_flag == MBR_PFLAG_ACTIVE) {
393 1.15 martin fl |= ACTIVE_FOUND;
394 1.15 martin if (ext->sector + p->mbrp_start == pm->ptstart)
395 1.15 martin fl |= NETBSD_ACTIVE;
396 1.15 martin }
397 1.15 martin if (ext->mbrb.mbrbs_nametab[i][0] == 0) {
398 1.15 martin /* No bootmenu label... */
399 1.15 martin if (ext->sector == 0)
400 1.15 martin continue;
401 1.15 martin if (ext->sector + p->mbrp_start == pm->ptstart)
402 1.15 martin /*
403 1.15 martin * Have installed into an extended ptn
404 1.15 martin * force name & bootsel...
405 1.15 martin */
406 1.15 martin names++;
407 1.15 martin continue;
408 1.15 martin }
409 1.15 martin /* Partition has a bootmenu label... */
410 1.15 martin if (ext->sector != 0)
411 1.15 martin fl |= MBR_BS_EXTLBA;
412 1.15 martin if (ext->sector + p->mbrp_start == pm->ptstart)
413 1.15 martin fl |= NETBSD_NAMED;
414 1.15 martin else if (p->mbrp_flag == MBR_PFLAG_ACTIVE)
415 1.15 martin fl |= ACTIVE_NAMED;
416 1.15 martin else
417 1.15 martin names++;
418 1.15 martin }
419 1.15 martin }
420 1.15 martin if (!(fl & ACTIVE_FOUND))
421 1.15 martin fl |= NETBSD_ACTIVE;
422 1.15 martin if (fl & NETBSD_NAMED && fl & NETBSD_ACTIVE)
423 1.15 martin fl |= ACTIVE_NAMED;
424 1.15 martin
425 1.15 martin if ((names > 0 || !(fl & NETBSD_ACTIVE)) &&
426 1.15 martin (!(fl & NETBSD_NAMED) || !(fl & ACTIVE_NAMED))) {
427 1.15 martin /*
428 1.15 martin * There appear to be multiple bootable partitions, but they
429 1.15 martin * don't all have bootmenu texts.
430 1.15 martin */
431 1.15 martin if (quiet)
432 1.15 martin return 0;
433 1.15 martin
434 1.15 martin msg_display(MSG_missing_bootmenu_text);
435 1.15 martin if (ask_yesno(NULL))
436 1.15 martin return 1;
437 1.15 martin }
438 1.15 martin
439 1.15 martin if ((fl & MBR_BS_EXTLBA) &&
440 1.15 martin (biosdisk == NULL || !(biosdisk->bi_flags & BIFLAG_EXTINT13))) {
441 1.15 martin /* Need unsupported LBA reads to read boot sectors */
442 1.15 martin if (quiet)
443 1.15 martin return 0;
444 1.15 martin
445 1.15 martin msg_display(MSG_no_extended_bootmenu);
446 1.15 martin if (!ask_noyes(NULL))
447 1.15 martin return 1;
448 1.15 martin }
449 1.15 martin
450 1.15 martin /* Sort out the name of the mbr code we need */
451 1.15 martin if (names > 0 || fl & (NETBSD_NAMED | ACTIVE_NAMED)) {
452 1.15 martin /* Need bootselect code */
453 1.15 martin fl |= MBR_BS_ACTIVE;
454 1.15 martin bootcode = fl & MBR_BS_EXTLBA ? _PATH_BOOTEXT : _PATH_BOOTSEL;
455 1.15 martin } else
456 1.15 martin bootcode = _PATH_MBR;
457 1.15 martin
458 1.15 martin fl &= MBR_BS_ACTIVE | MBR_BS_EXTLBA;
459 1.15 martin
460 1.15 martin /* Look at what is installed */
461 1.15 martin ofl = mbri->mbrb.mbrbs_flags;
462 1.15 martin if (ofl == 0) {
463 1.15 martin /* Check there is some bootcode at all... */
464 1.15 martin if (mbri->mbr.mbr_magic != htole16(MBR_MAGIC) ||
465 1.15 martin mbri->mbr.mbr_jmpboot[0] == 0 ||
466 1.15 martin mbr_root_above_chs())
467 1.15 martin /* Existing won't do, force update */
468 1.15 martin fl |= MBR_BS_NEWMBR;
469 1.15 martin }
470 1.15 martin ofl = mbri->oflags & (MBR_BS_ACTIVE | MBR_BS_EXTLBA);
471 1.15 martin
472 1.15 martin if (!quiet) {
473 1.15 martin if (fl & ~ofl || (fl == 0 && ofl & MBR_BS_ACTIVE)) {
474 1.15 martin /* Existing boot code isn't the right one... */
475 1.15 martin if (fl & MBR_BS_ACTIVE)
476 1.15 martin msg_display(MSG_installbootsel);
477 1.15 martin else
478 1.15 martin msg_display(MSG_installmbr);
479 1.15 martin } else
480 1.15 martin /* Existing code would (probably) be ok */
481 1.15 martin msg_display(MSG_updatembr);
482 1.15 martin
483 1.15 martin if (!ask_yesno(NULL))
484 1.15 martin /* User doesn't want to update mbr code */
485 1.15 martin return 2;
486 1.15 martin }
487 1.15 martin
488 1.15 martin if (md_read_bootcode(bootcode, &mbri->mbr) == 0)
489 1.15 martin /* update suceeded - to memory copy */
490 1.15 martin return 2;
491 1.15 martin
492 1.15 martin /* This shouldn't happen since the files are in the floppy fs... */
493 1.18 christos msg_fmt_display("Can't find %s", "%s", bootcode);
494 1.15 martin return ask_reedit(parts);
495 1.1 dholland }
496 1.1 dholland
497 1.15 martin bool
498 1.15 martin md_parts_use_wholedisk(struct disk_partitions *parts)
499 1.1 dholland {
500 1.15 martin return parts_use_wholedisk(parts, 0, NULL);
501 1.1 dholland }
502 1.1 dholland
503 1.15 martin static bool
504 1.15 martin get_bios_info(const char *dev, struct disk_partitions *parts, int *bcyl,
505 1.15 martin int *bhead, int *bsec)
506 1.1 dholland {
507 1.1 dholland static struct disklist *disklist = NULL;
508 1.1 dholland static int mib[2] = {CTL_MACHDEP, CPU_DISKINFO};
509 1.1 dholland int i;
510 1.1 dholland size_t len;
511 1.1 dholland struct biosdisk_info *bip;
512 1.1 dholland struct nativedisk_info *nip = NULL, *nat;
513 1.15 martin int cyl, head, sec;
514 1.1 dholland
515 1.1 dholland if (disklist == NULL) {
516 1.1 dholland if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0)
517 1.1 dholland goto nogeom;
518 1.1 dholland disklist = malloc(len);
519 1.1 dholland if (disklist == NULL) {
520 1.1 dholland fprintf(stderr, "Out of memory\n");
521 1.15 martin return false;
522 1.1 dholland }
523 1.1 dholland sysctl(mib, 2, disklist, &len, NULL, 0);
524 1.1 dholland }
525 1.1 dholland
526 1.1 dholland for (i = 0; i < disklist->dl_nnativedisks; i++) {
527 1.1 dholland nat = &disklist->dl_nativedisks[i];
528 1.1 dholland if (!strcmp(dev, nat->ni_devname)) {
529 1.1 dholland nip = nat;
530 1.1 dholland break;
531 1.1 dholland }
532 1.1 dholland }
533 1.1 dholland if (nip == NULL || nip->ni_nmatches == 0) {
534 1.1 dholland nogeom:
535 1.1 dholland if (nip != NULL)
536 1.18 christos msg_fmt_display(MSG_nobiosgeom, "%d%d%d",
537 1.18 christos pm->dlcyl, pm->dlhead, pm->dlsec);
538 1.15 martin if (guess_biosgeom_from_parts(parts, &cyl, &head, &sec) >= 0
539 1.1 dholland && nip != NULL)
540 1.18 christos {
541 1.18 christos msg_fmt_display_add(MSG_biosguess, "%d%d%d",
542 1.18 christos cyl, head, sec);
543 1.18 christos }
544 1.1 dholland biosdisk = NULL;
545 1.1 dholland } else {
546 1.15 martin guess_biosgeom_from_parts(parts, &cyl, &head, &sec);
547 1.1 dholland if (nip->ni_nmatches == 1) {
548 1.1 dholland bip = &disklist->dl_biosdisks[nip->ni_biosmatches[0]];
549 1.1 dholland msg_display(MSG_onebiosmatch);
550 1.1 dholland msg_table_add(MSG_onebiosmatch_header);
551 1.18 christos msg_fmt_table_add(MSG_onebiosmatch_row, "%d%d%d%d%u%u",
552 1.18 christos bip->bi_dev, bip->bi_cyl, bip->bi_head, bip->bi_sec,
553 1.1 dholland (unsigned)bip->bi_lbasecs,
554 1.1 dholland (unsigned)(bip->bi_lbasecs / (1000000000 / 512)));
555 1.1 dholland msg_display_add(MSG_biosgeom_advise);
556 1.1 dholland biosdisk = bip;
557 1.1 dholland process_menu(MENU_biosonematch, &biosdisk);
558 1.1 dholland } else {
559 1.1 dholland msg_display(MSG_biosmultmatch);
560 1.1 dholland msg_table_add(MSG_biosmultmatch_header);
561 1.1 dholland for (i = 0; i < nip->ni_nmatches; i++) {
562 1.1 dholland bip = &disklist->dl_biosdisks[
563 1.1 dholland nip->ni_biosmatches[i]];
564 1.18 christos msg_fmt_table_add(MSG_biosmultmatch_row,
565 1.18 christos "%d%d%d%d%d%u%u", i,
566 1.1 dholland bip->bi_dev, bip->bi_cyl, bip->bi_head,
567 1.1 dholland bip->bi_sec, (unsigned)bip->bi_lbasecs,
568 1.1 dholland (unsigned)bip->bi_lbasecs/(1000000000/512));
569 1.1 dholland }
570 1.1 dholland process_menu(MENU_biosmultmatch, &i);
571 1.1 dholland if (i == -1)
572 1.1 dholland biosdisk = NULL;
573 1.1 dholland else
574 1.1 dholland biosdisk = &disklist->dl_biosdisks[
575 1.1 dholland nip->ni_biosmatches[i]];
576 1.1 dholland }
577 1.1 dholland }
578 1.1 dholland if (biosdisk == NULL) {
579 1.17 martin *bcyl = cyl;
580 1.17 martin *bhead = head;
581 1.17 martin *bsec = sec;
582 1.17 martin if (nip != NULL)
583 1.17 martin set_bios_geom(parts, bcyl, bhead, bsec);
584 1.1 dholland } else {
585 1.15 martin *bcyl = biosdisk->bi_cyl;
586 1.15 martin *bhead = biosdisk->bi_head;
587 1.15 martin *bsec = biosdisk->bi_sec;
588 1.1 dholland }
589 1.15 martin return true;
590 1.1 dholland }
591 1.1 dholland
592 1.1 dholland static int
593 1.1 dholland mbr_root_above_chs(void)
594 1.1 dholland {
595 1.15 martin return pm->ptstart + (daddr_t)DEFROOTSIZE * (daddr_t)(MEG / 512)
596 1.15 martin >= pm->max_chs;
597 1.1 dholland }
598 1.1 dholland
599 1.15 martin /* returns false if no write-back of parts is required */
600 1.15 martin bool
601 1.15 martin md_mbr_update_check(struct disk_partitions *parts, mbr_info_t *mbri)
602 1.1 dholland {
603 1.1 dholland struct mbr_partition *mbrp;
604 1.1 dholland int i, netbsdpart = -1, oldbsdpart = -1, oldbsdcount = 0;
605 1.1 dholland
606 1.15 martin if (pm->no_mbr || pm->no_part)
607 1.15 martin return false;
608 1.1 dholland
609 1.15 martin mbrp = &mbri->mbr.mbr_parts[0];
610 1.1 dholland
611 1.1 dholland for (i = 0; i < MBR_PART_COUNT; i++) {
612 1.1 dholland if (mbrp[i].mbrp_type == MBR_PTYPE_386BSD) {
613 1.1 dholland oldbsdpart = i;
614 1.1 dholland oldbsdcount++;
615 1.1 dholland } else if (mbrp[i].mbrp_type == MBR_PTYPE_NETBSD)
616 1.1 dholland netbsdpart = i;
617 1.1 dholland }
618 1.1 dholland
619 1.1 dholland if (netbsdpart == -1 && oldbsdcount == 1) {
620 1.1 dholland mbrp[oldbsdpart].mbrp_type = MBR_PTYPE_NETBSD;
621 1.15 martin return true;
622 1.1 dholland }
623 1.15 martin
624 1.15 martin return false;
625 1.1 dholland }
626 1.1 dholland
627 1.1 dholland /*
628 1.1 dholland * Read MBR code from a file.
629 1.1 dholland * The existing partition table and bootselect configuration is kept.
630 1.1 dholland */
631 1.1 dholland static int
632 1.1 dholland md_read_bootcode(const char *path, struct mbr_sector *mbrs)
633 1.1 dholland {
634 1.1 dholland int fd;
635 1.1 dholland struct stat st;
636 1.1 dholland size_t len;
637 1.1 dholland struct mbr_sector new_mbr;
638 1.1 dholland uint32_t dsn;
639 1.1 dholland
640 1.1 dholland fd = open(path, O_RDONLY);
641 1.1 dholland if (fd < 0)
642 1.1 dholland return -1;
643 1.1 dholland
644 1.1 dholland if (fstat(fd, &st) < 0 || st.st_size != sizeof *mbrs) {
645 1.1 dholland close(fd);
646 1.1 dholland return -1;
647 1.1 dholland }
648 1.1 dholland
649 1.1 dholland if (read(fd, &new_mbr, sizeof new_mbr) != sizeof new_mbr) {
650 1.1 dholland close(fd);
651 1.1 dholland return -1;
652 1.1 dholland }
653 1.1 dholland close(fd);
654 1.1 dholland
655 1.1 dholland if (new_mbr.mbr_bootsel_magic != htole16(MBR_BS_MAGIC))
656 1.1 dholland return -1;
657 1.1 dholland
658 1.1 dholland if (mbrs->mbr_bootsel_magic == htole16(MBR_BS_MAGIC)) {
659 1.1 dholland len = offsetof(struct mbr_sector, mbr_bootsel);
660 1.1 dholland } else
661 1.1 dholland len = offsetof(struct mbr_sector, mbr_parts);
662 1.1 dholland
663 1.1 dholland /* Preserve the 'drive serial number' - especially for Vista */
664 1.1 dholland dsn = mbrs->mbr_dsn;
665 1.1 dholland memcpy(mbrs, &new_mbr, len);
666 1.1 dholland mbrs->mbr_dsn = dsn;
667 1.1 dholland
668 1.1 dholland /* Keep flags from object file - indicate the properties */
669 1.1 dholland mbrs->mbr_bootsel.mbrbs_flags = new_mbr.mbr_bootsel.mbrbs_flags;
670 1.1 dholland mbrs->mbr_magic = htole16(MBR_MAGIC);
671 1.1 dholland
672 1.1 dholland return 0;
673 1.1 dholland }
674 1.1 dholland
675 1.1 dholland static unsigned int
676 1.1 dholland get_bootmodel(void)
677 1.1 dholland {
678 1.1 dholland #if defined(__i386__)
679 1.1 dholland struct utsname ut;
680 1.1 dholland #ifdef DEBUG
681 1.1 dholland char *envstr;
682 1.1 dholland
683 1.1 dholland envstr = getenv("BOOTMODEL");
684 1.1 dholland if (envstr != NULL)
685 1.1 dholland return atoi(envstr);
686 1.1 dholland #endif
687 1.1 dholland
688 1.1 dholland if (uname(&ut) < 0)
689 1.1 dholland ut.version[0] = 0;
690 1.1 dholland
691 1.1 dholland #if defined(SET_KERNEL_TINY)
692 1.1 dholland if (strstr(ut.version, "TINY") != NULL)
693 1.1 dholland return SET_KERNEL_TINY;
694 1.1 dholland #endif
695 1.1 dholland #if defined(SET_KERNEL_PS2)
696 1.1 dholland if (strstr(ut.version, "PS2") != NULL)
697 1.1 dholland return SET_KERNEL_PS2;
698 1.1 dholland #endif
699 1.1 dholland #endif
700 1.1 dholland return SET_KERNEL_GENERIC;
701 1.1 dholland }
702 1.1 dholland
703 1.1 dholland
704 1.1 dholland int
705 1.19 martin md_pre_mount(struct install_partition_desc *install, size_t ndx)
706 1.1 dholland {
707 1.1 dholland return 0;
708 1.1 dholland }
709 1.15 martin
710 1.15 martin #ifdef HAVE_GPT
711 1.15 martin /*
712 1.15 martin * New GPT partitions have been written, update bootloader or remember
713 1.15 martin * data untill needed in md_post_newfs
714 1.15 martin */
715 1.15 martin bool
716 1.15 martin md_gpt_post_write(struct disk_partitions *parts, part_id root_id,
717 1.15 martin bool root_is_new, part_id efi_id, bool efi_is_new)
718 1.15 martin {
719 1.15 martin struct disk_part_info info;
720 1.15 martin
721 1.15 martin assert(efi_id == NO_PART); /* XXX EFI support still missing */
722 1.15 martin
723 1.15 martin if (root_id != NO_PART) {
724 1.15 martin /* we always update the gpt boot record for now */
725 1.15 martin if (!parts->pscheme->get_part_info(parts, root_id, &info))
726 1.15 martin return false;
727 1.15 martin if (run_program(RUN_SILENT, "gpt biosboot -b %" PRIu64 " %s",
728 1.15 martin info.start, parts->disk) != 0)
729 1.15 martin return false;
730 1.15 martin }
731 1.15 martin
732 1.15 martin return true;
733 1.15 martin }
734 1.15 martin #endif
735