- Add 2 functions in label.c, getpartoff()/getpartsize() used to let the user
enter partitions offet and size. The user can chose unit independantly of
the unit used for display ('M'/'c'/'s'). These functions do the proper
bound checks and alignement/roudups. Used in the edfspart menu and
i386 md_make_bsd_partitions() (other ports should do the change as
well).
- now that getpartsize() does the rigth thing, kill the swapadj hack (which was
buggy anyway).
- in i386 md_make_bsd_partitions(), don't propose defaults that don't fit on
the disk. If the disk is too small, fallback to custom instead.
- fix a bug in mbr.c, where the partition flags would not have always been
reset. sysinst created me a MBR with 2 active partition. The boot code
doesn't like it :)
- added a message for eventual mount failures.
- killed donewfs and extracting messages, as we run the commands in a
subwindow these messages just flashed on the screen.
- Changes a few exit(1) to return(1), to give the user a second chance.
- added msg_clear() or wclear(stdscr) in a place or two, to make
display a bit nicer.
- in run_cmd(), if the command succeeded, don't wait for the user to
press enter.
1999-04-12 02:40:19 +04:00
|
|
|
/* $NetBSD: mbr.c,v 1.13 1999/04/11 22:40:20 bouyer Exp $ */
|
1997-09-27 03:02:53 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 1997 Piermont Information Systems Inc.
|
|
|
|
* All rights reserved.
|
1998-02-24 08:36:03 +03:00
|
|
|
* * Copyright 1997 Piermont Information Systems Inc.
|
|
|
|
* All rights reserved.
|
1997-09-27 03:02:53 +04:00
|
|
|
*
|
|
|
|
* Written by Philip A. Nelson for Piermont Information Systems Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software develooped for the NetBSD Project by
|
|
|
|
* Piermont Information Systems Inc.
|
|
|
|
* 4. The name of Piermont Information Systems Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
/*
|
|
|
|
* Following applies to the geometry guessing code
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1992 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify and distribute this software and its
|
|
|
|
* documentation is hereby granted, provided that both the copyright
|
|
|
|
* notice and this permission notice appear in all copies of the
|
|
|
|
* software, derivative works or modified versions, and any portions
|
|
|
|
* thereof, and that both notices appear in supporting documentation.
|
|
|
|
*
|
|
|
|
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
|
|
|
|
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
|
|
|
|
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
* Carnegie Mellon requests users of this software to return to
|
|
|
|
*
|
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
|
|
|
*
|
|
|
|
* any improvements or extensions that they make and grant Carnegie Mellon
|
|
|
|
* the rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
1998-02-24 08:36:03 +03:00
|
|
|
/* mbr.c -- DOS Master Boot Record editing code */
|
1997-09-27 03:02:53 +04:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
1997-09-27 03:02:53 +04:00
|
|
|
#include <stdio.h>
|
1999-03-31 04:44:48 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
1998-02-24 08:36:03 +03:00
|
|
|
#include <util.h>
|
1997-09-27 03:02:53 +04:00
|
|
|
#include "defs.h"
|
1999-03-31 04:44:48 +04:00
|
|
|
#include "mbr.h"
|
1997-09-27 03:02:53 +04:00
|
|
|
#include "md.h"
|
|
|
|
#include "msg_defs.h"
|
|
|
|
#include "menu_defs.h"
|
1999-03-31 04:44:48 +04:00
|
|
|
#include "endian.h"
|
1997-09-27 03:02:53 +04:00
|
|
|
|
|
|
|
struct part_id {
|
|
|
|
int id;
|
|
|
|
char *name;
|
|
|
|
} part_ids[] = {
|
|
|
|
{0, "unused"},
|
1999-03-31 04:44:48 +04:00
|
|
|
{MBR_PTYPE_FAT12, "Primary DOS, 12 bit FAT"},
|
|
|
|
{MBR_PTYPE_FAT16S, "Primary DOS, 16 bit FAT <32M"},
|
|
|
|
{MBR_PTYPE_EXT, "Extended DOS"},
|
|
|
|
{MBR_PTYPE_FAT16B, "Primary DOS, 16-bit FAT >32MB"},
|
|
|
|
{MBR_PTYPE_NTFS, "NTFS"},
|
|
|
|
{MBR_PTYPE_LNXSWAP, "Linux swap"},
|
|
|
|
{MBR_PTYPE_LNXEXT2, "Linux native"},
|
|
|
|
{MBR_PTYPE_386BSD, "old NetBSD/FreeBSD/386BSD"},
|
|
|
|
{MBR_PTYPE_NETBSD, "NetBSD"},
|
1997-09-27 03:02:53 +04:00
|
|
|
{-1, "Unknown"},
|
|
|
|
};
|
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
int dosptyp_nbsd = MBR_PTYPE_NETBSD;
|
1998-02-24 08:36:03 +03:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
static int get_mapping __P((struct mbr_partition *, int, int *, int *, int *,
|
|
|
|
long *absolute));
|
|
|
|
static void convert_mbr_chs __P((int, int, int, u_int8_t *, u_int8_t *,
|
|
|
|
u_int8_t *, u_int32_t));
|
1998-02-24 08:36:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, geometry stuff...
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
|
|
|
check_geom()
|
1998-02-24 08:36:03 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1998-02-24 08:36:03 +03:00
|
|
|
return bcyl <= 1024 && bsec < 64 && bcyl > 0 && bhead > 0 && bsec > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get C/H/S geometry from user via menu interface and
|
|
|
|
* store in globals.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
void
|
1999-03-31 04:44:48 +04:00
|
|
|
set_bios_geom(cyl, head, sec)
|
|
|
|
int cyl, head, sec;
|
1998-02-24 08:36:03 +03:00
|
|
|
{
|
|
|
|
char res[80];
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1998-02-24 08:36:03 +03:00
|
|
|
msg_display_add(MSG_setbiosgeom);
|
1999-03-31 04:44:48 +04:00
|
|
|
snprintf(res, 80, "%d", cyl);
|
1998-07-21 18:53:36 +04:00
|
|
|
msg_prompt_add(MSG_cylinders, res, res, 80);
|
1998-02-24 08:36:03 +03:00
|
|
|
bcyl = atoi(res);
|
1998-07-21 18:53:36 +04:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
snprintf(res, 80, "%d", head);
|
1998-07-21 18:53:36 +04:00
|
|
|
msg_prompt_add(MSG_heads, res, res, 80);
|
1998-02-24 08:36:03 +03:00
|
|
|
bhead = atoi(res);
|
1998-07-21 18:53:36 +04:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
snprintf(res, 80, "%d", sec);
|
1998-07-21 18:53:36 +04:00
|
|
|
msg_prompt_add(MSG_sectors, res, res, 80);
|
1998-02-24 08:36:03 +03:00
|
|
|
bsec = atoi(res);
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
void
|
|
|
|
disp_cur_geom()
|
1998-02-24 08:36:03 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1998-02-24 08:36:03 +03:00
|
|
|
msg_display_add(MSG_realgeom, dlcyl, dlhead, dlsec);
|
|
|
|
msg_display_add(MSG_biosgeom, bcyl, bhead, bsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Then, the partition stuff...
|
|
|
|
*/
|
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
otherpart(id)
|
|
|
|
int id;
|
1998-02-24 08:36:03 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
return (id != 0 && id != MBR_PTYPE_386BSD && id != MBR_PTYPE_NETBSD);
|
1998-02-24 08:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-06-20 17:05:48 +04:00
|
|
|
ourpart(id)
|
|
|
|
int id;
|
1998-02-24 08:36:03 +03:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
return (id == MBR_PTYPE_386BSD || id == MBR_PTYPE_NETBSD);
|
1998-02-24 08:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let user change incore Master Boot Record partitions via menu.
|
|
|
|
*/
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
1999-03-31 04:44:48 +04:00
|
|
|
edit_mbr(partition)
|
|
|
|
struct mbr_partition *partition;
|
1998-02-24 08:36:03 +03:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
/* Ask full/part */
|
1999-03-31 04:44:48 +04:00
|
|
|
|
|
|
|
/* XXX this sucks ("part" is used in menus, no param passing there) */
|
|
|
|
part = partition;
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_display(MSG_fullpart, diskdev);
|
|
|
|
process_menu(MENU_fullpart);
|
1998-02-24 08:36:03 +03:00
|
|
|
|
|
|
|
/* DOS fdisk label checking and value setting. */
|
|
|
|
if (usefull) {
|
|
|
|
int otherparts = 0;
|
|
|
|
int ourparts = 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
/* Count nonempty, non-BSD partitions. */
|
1999-03-31 04:44:48 +04:00
|
|
|
for (i = 0; i < NMBRPART; i++) {
|
|
|
|
otherparts += otherpart(part[0].mbrp_typ);
|
1998-02-24 08:36:03 +03:00
|
|
|
/* check for dualboot *bsd too */
|
1999-03-31 04:44:48 +04:00
|
|
|
ourparts += ourpart(part[0].mbrp_typ);
|
1998-02-24 08:36:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ask if we really want to blow away non-NetBSD stuff */
|
|
|
|
if (otherparts != 0 || ourparts > 1) {
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_display(MSG_ovrwrite);
|
|
|
|
process_menu(MENU_noyes);
|
1998-02-24 08:36:03 +03:00
|
|
|
if (!yesno) {
|
1999-01-21 11:02:17 +03:00
|
|
|
if (logging)
|
|
|
|
(void)fprintf(log, "User answered no to destroy other data, aborting.\n");
|
1998-02-24 08:36:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the partition information for full disk usage. */
|
1999-03-31 04:44:48 +04:00
|
|
|
part[0].mbrp_typ = part[0].mbrp_size = 0;
|
|
|
|
part[1].mbrp_typ = part[1].mbrp_size = 0;
|
|
|
|
part[2].mbrp_typ = part[2].mbrp_size = 0;
|
|
|
|
part[3].mbrp_typ = dosptyp_nbsd;
|
|
|
|
part[3].mbrp_size = bsize - bsec;
|
|
|
|
part[3].mbrp_start = bsec;
|
|
|
|
part[3].mbrp_flag = 0x80;
|
1998-02-24 08:36:03 +03:00
|
|
|
|
|
|
|
ptstart = bsec;
|
|
|
|
ptsize = bsize - bsec;
|
|
|
|
fsdsize = dlsize;
|
|
|
|
fsptsize = dlsize - bsec;
|
|
|
|
fsdmb = fsdsize / MEG;
|
|
|
|
activepart = 3;
|
|
|
|
} else {
|
|
|
|
int numbsd, overlap;
|
|
|
|
int numfreebsd, freebsdpart; /* dual-boot */
|
|
|
|
|
|
|
|
/* Ask for sizes, which partitions, ... */
|
|
|
|
ask_sizemult();
|
|
|
|
bsdpart = freebsdpart = -1;
|
|
|
|
activepart = -1;
|
1998-06-20 17:05:48 +04:00
|
|
|
for (i = 0; i<4; i++)
|
- Add 2 functions in label.c, getpartoff()/getpartsize() used to let the user
enter partitions offet and size. The user can chose unit independantly of
the unit used for display ('M'/'c'/'s'). These functions do the proper
bound checks and alignement/roudups. Used in the edfspart menu and
i386 md_make_bsd_partitions() (other ports should do the change as
well).
- now that getpartsize() does the rigth thing, kill the swapadj hack (which was
buggy anyway).
- in i386 md_make_bsd_partitions(), don't propose defaults that don't fit on
the disk. If the disk is too small, fallback to custom instead.
- fix a bug in mbr.c, where the partition flags would not have always been
reset. sysinst created me a MBR with 2 active partition. The boot code
doesn't like it :)
- added a message for eventual mount failures.
- killed donewfs and extracting messages, as we run the commands in a
subwindow these messages just flashed on the screen.
- Changes a few exit(1) to return(1), to give the user a second chance.
- added msg_clear() or wclear(stdscr) in a place or two, to make
display a bit nicer.
- in run_cmd(), if the command succeeded, don't wait for the user to
press enter.
1999-04-12 02:40:19 +04:00
|
|
|
if (part[i].mbrp_flag != 0) {
|
1998-02-24 08:36:03 +03:00
|
|
|
activepart = i;
|
- Add 2 functions in label.c, getpartoff()/getpartsize() used to let the user
enter partitions offet and size. The user can chose unit independantly of
the unit used for display ('M'/'c'/'s'). These functions do the proper
bound checks and alignement/roudups. Used in the edfspart menu and
i386 md_make_bsd_partitions() (other ports should do the change as
well).
- now that getpartsize() does the rigth thing, kill the swapadj hack (which was
buggy anyway).
- in i386 md_make_bsd_partitions(), don't propose defaults that don't fit on
the disk. If the disk is too small, fallback to custom instead.
- fix a bug in mbr.c, where the partition flags would not have always been
reset. sysinst created me a MBR with 2 active partition. The boot code
doesn't like it :)
- added a message for eventual mount failures.
- killed donewfs and extracting messages, as we run the commands in a
subwindow these messages just flashed on the screen.
- Changes a few exit(1) to return(1), to give the user a second chance.
- added msg_clear() or wclear(stdscr) in a place or two, to make
display a bit nicer.
- in run_cmd(), if the command succeeded, don't wait for the user to
press enter.
1999-04-12 02:40:19 +04:00
|
|
|
part[i].mbrp_flag = 0;
|
|
|
|
}
|
1998-02-24 08:36:03 +03:00
|
|
|
do {
|
|
|
|
process_menu (MENU_editparttable);
|
|
|
|
numbsd = 0;
|
|
|
|
bsdpart = -1;
|
|
|
|
freebsdpart = -1;
|
|
|
|
numfreebsd = 0;
|
|
|
|
overlap = 0;
|
|
|
|
yesno = 0;
|
|
|
|
for (i=0; i<4; i++) {
|
1998-06-20 17:05:48 +04:00
|
|
|
/* Count 386bsd/FreeBSD/NetBSD(old) partitions */
|
1999-03-31 04:44:48 +04:00
|
|
|
if (part[i].mbrp_typ == MBR_PTYPE_386BSD) {
|
1998-02-24 08:36:03 +03:00
|
|
|
freebsdpart = i;
|
|
|
|
numfreebsd++;
|
|
|
|
}
|
|
|
|
/* Count NetBSD-only partitions */
|
1999-03-31 04:44:48 +04:00
|
|
|
if (part[i].mbrp_typ == MBR_PTYPE_NETBSD) {
|
1998-02-24 08:36:03 +03:00
|
|
|
bsdpart = i;
|
|
|
|
numbsd++;
|
|
|
|
}
|
|
|
|
for (j = i+1; j<4; j++)
|
1999-03-31 04:44:48 +04:00
|
|
|
if (partsoverlap(part, i,j))
|
1998-02-24 08:36:03 +03:00
|
|
|
overlap = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no new-NetBSD partition, use 386bsd instead */
|
|
|
|
if (numbsd == 0 && numfreebsd > 0) {
|
|
|
|
numbsd = numfreebsd;
|
|
|
|
bsdpart = freebsdpart;
|
|
|
|
/* XXX check partition type? */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for overlap or multiple native partitions */
|
|
|
|
if (overlap || numbsd != 1) {
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_display(MSG_reeditpart);
|
|
|
|
process_menu(MENU_yesno);
|
1998-02-24 08:36:03 +03:00
|
|
|
}
|
|
|
|
} while (yesno && (numbsd != 1 || overlap));
|
|
|
|
|
1999-04-01 15:33:02 +04:00
|
|
|
if (activepart != -1)
|
|
|
|
part[activepart].mbrp_flag = 0x80;
|
|
|
|
|
1998-02-24 08:36:03 +03:00
|
|
|
if (numbsd == 0) {
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_display(MSG_nobsdpart);
|
|
|
|
process_menu(MENU_ok);
|
1998-02-24 08:36:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numbsd > 1) {
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_display(MSG_multbsdpart, bsdpart);
|
|
|
|
process_menu(MENU_ok);
|
1998-02-24 08:36:03 +03:00
|
|
|
}
|
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
ptstart = part[bsdpart].mbrp_start;
|
|
|
|
ptsize = part[bsdpart].mbrp_size;
|
1998-02-24 08:36:03 +03:00
|
|
|
fsdsize = dlsize;
|
|
|
|
if (ptstart + ptsize < bsize)
|
|
|
|
fsptsize = ptsize;
|
|
|
|
else
|
|
|
|
fsptsize = dlsize - ptstart;
|
|
|
|
fsdmb = fsdsize / MEG;
|
|
|
|
|
|
|
|
/* Ask if a boot selector is wanted. XXXX */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute minimum NetBSD partition sizes (in sectors). */
|
|
|
|
minfsdmb = (80 + 4*rammb) * (MEG / sectorsize);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
int
|
1999-03-31 04:44:48 +04:00
|
|
|
partsoverlap(part, i, j)
|
|
|
|
struct mbr_partition *part;
|
1998-06-20 17:05:48 +04:00
|
|
|
int i;
|
|
|
|
int j;
|
1997-09-27 03:02:53 +04:00
|
|
|
{
|
1998-06-20 17:05:48 +04:00
|
|
|
|
1999-03-31 04:44:48 +04:00
|
|
|
if (part[i].mbrp_size == 0 || part[j].mbrp_size == 0)
|
1997-09-27 03:02:53 +04:00
|
|
|
return 0;
|
|
|
|
|
1997-10-18 01:10:39 +04:00
|
|
|
return
|
1999-03-31 04:44:48 +04:00
|
|
|
(part[i].mbrp_start < part[j].mbrp_start &&
|
|
|
|
part[i].mbrp_start + part[i].mbrp_size > part[j].mbrp_start)
|
1997-09-27 03:02:53 +04:00
|
|
|
||
|
1999-03-31 04:44:48 +04:00
|
|
|
(part[i].mbrp_start > part[j].mbrp_start &&
|
|
|
|
part[i].mbrp_start < part[j].mbrp_start + part[j].mbrp_size)
|
1997-09-27 03:02:53 +04:00
|
|
|
||
|
1999-03-31 04:44:48 +04:00
|
|
|
(part[i].mbrp_start == part[j].mbrp_start);
|
1997-09-27 03:02:53 +04:00
|
|
|
}
|
|
|
|
|
1998-06-20 17:05:48 +04:00
|
|
|
void
|
1999-03-31 04:44:48 +04:00
|
|
|
disp_cur_part(part, sel, disp)
|
|
|
|
struct mbr_partition *part;
|
1998-06-20 17:05:48 +04:00
|
|
|
int sel;
|
|
|
|
int disp;
|
1997-09-27 03:02:53 +04:00
|
|
|
{
|
1997-11-30 00:49:13 +03:00
|
|
|
int i, j, start, stop, rsize, rend;
|
1997-09-27 03:02:53 +04:00
|
|
|
|
|
|
|
if (disp < 0)
|
|
|
|
start = 0, stop = 4;
|
|
|
|
else
|
|
|
|
start = disp, stop = disp+1;
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_display_add(MSG_part_head, multname, multname, multname);
|
|
|
|
for (i = start; i < stop; i++) {
|
|
|
|
if (sel == i)
|
|
|
|
msg_standout();
|
1999-03-31 04:44:48 +04:00
|
|
|
if (part[i].mbrp_size == 0 && part[i].mbrp_start == 0)
|
1998-06-20 17:05:48 +04:00
|
|
|
msg_printf_add("%d %36s ", i, "");
|
1997-11-30 00:49:13 +03:00
|
|
|
else {
|
1999-03-31 04:44:48 +04:00
|
|
|
rsize = part[i].mbrp_size / sizemult;
|
|
|
|
if (part[i].mbrp_size % sizemult)
|
1997-11-30 00:49:13 +03:00
|
|
|
rsize++;
|
1999-03-31 04:44:48 +04:00
|
|
|
rend = (part[i].mbrp_start + part[i].mbrp_size) / sizemult;
|
|
|
|
if ((part[i].mbrp_size + part[i].mbrp_size) % sizemult)
|
1997-11-30 00:49:13 +03:00
|
|
|
rend++;
|
|
|
|
msg_printf_add("%d %12d%12d%12d ", i,
|
1999-03-31 04:44:48 +04:00
|
|
|
part[i].mbrp_start / sizemult, rsize, rend);
|
1997-11-30 00:49:13 +03:00
|
|
|
}
|
1997-09-27 03:02:53 +04:00
|
|
|
for (j = 0; part_ids[j].id != -1 &&
|
1999-03-31 04:44:48 +04:00
|
|
|
part_ids[j].id != part[i].mbrp_typ; j++);
|
|
|
|
msg_printf_add("%s\n", part_ids[j].name);
|
|
|
|
if (sel == i)
|
|
|
|
msg_standend();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
read_mbr(disk, buf, len)
|
|
|
|
char *disk, *buf;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
char diskpath[MAXPATHLEN];
|
|
|
|
int fd, i;
|
|
|
|
struct mbr_partition *mbrp;
|
|
|
|
|
|
|
|
/* Open the disk. */
|
|
|
|
fd = opendisk(disk, O_RDONLY, diskpath, sizeof(diskpath), 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (lseek(fd, MBR_BBSECTOR * MBR_SECSIZE, SEEK_SET) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (read(fd, buf, len) < len) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid_mbr(buf)) {
|
|
|
|
mbrp = (struct mbr_partition *)&buf[MBR_PARTOFF];
|
|
|
|
for (i = 0; i < NMBRPART; i++) {
|
|
|
|
if (mbrp[i].mbrp_typ != 0) {
|
|
|
|
mbrp[i].mbrp_start =
|
|
|
|
le_to_native32(mbrp[i].mbrp_start);
|
|
|
|
mbrp[i].mbrp_size =
|
|
|
|
le_to_native32(mbrp[i].mbrp_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
write_mbr(disk, buf, len)
|
|
|
|
char *disk, *buf;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
char diskpath[MAXPATHLEN];
|
|
|
|
int fd, i, ret = 0;
|
|
|
|
struct mbr_partition *mbrp;
|
|
|
|
u_int32_t pstart, psize;
|
|
|
|
|
|
|
|
/* Open the disk. */
|
|
|
|
fd = opendisk(disk, O_WRONLY, diskpath, sizeof(diskpath), 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (lseek(fd, MBR_BBSECTOR * MBR_SECSIZE, SEEK_SET) < 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbrp = (struct mbr_partition *)&buf[MBR_PARTOFF];
|
|
|
|
for (i = 0; i < NMBRPART; i++) {
|
|
|
|
if (mbrp[i].mbrp_typ != 0) {
|
|
|
|
pstart = mbrp[i].mbrp_start;
|
|
|
|
psize = mbrp[i].mbrp_size;
|
|
|
|
mbrp[i].mbrp_start = native_to_le32(pstart);
|
|
|
|
mbrp[i].mbrp_size = native_to_le32(psize);
|
|
|
|
convert_mbr_chs(bcyl, bhead, bsec,
|
|
|
|
&mbrp[i].mbrp_scyl, &mbrp[i].mbrp_shd,
|
|
|
|
&mbrp[i].mbrp_ssect, pstart);
|
|
|
|
convert_mbr_chs(bcyl, bhead, bsec,
|
|
|
|
&mbrp[i].mbrp_ecyl, &mbrp[i].mbrp_ehd,
|
|
|
|
&mbrp[i].mbrp_esect, pstart + psize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write(fd, buf, len) < 0)
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
(void)close(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
valid_mbr(buf)
|
|
|
|
char *buf;
|
|
|
|
{
|
1999-04-04 02:56:22 +04:00
|
|
|
u_int16_t magic;
|
1999-03-31 04:44:48 +04:00
|
|
|
|
1999-04-04 02:56:22 +04:00
|
|
|
magic = *((u_int16_t *)&buf[MBR_MAGICOFF]);
|
1999-03-31 04:44:48 +04:00
|
|
|
|
|
|
|
return (le_to_native16(magic) == MBR_MAGIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
convert_mbr_chs(cyl, head, sec, cylp, headp, secp, relsecs)
|
|
|
|
int cyl, head, sec;
|
|
|
|
u_int8_t *cylp, *headp, *secp;
|
|
|
|
u_int32_t relsecs;
|
|
|
|
{
|
|
|
|
unsigned int tcyl, temp, thead, tsec;
|
|
|
|
|
|
|
|
temp = head * sec;
|
|
|
|
tcyl = relsecs / temp;
|
|
|
|
|
|
|
|
if (tcyl >= 1024) {
|
|
|
|
*cylp = *headp = *secp = 0xff;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
relsecs %= temp;
|
|
|
|
thead = relsecs / sec;
|
|
|
|
|
|
|
|
tsec = (relsecs % sec) + 1;
|
|
|
|
|
|
|
|
*cylp = MBR_PUT_LSCYL(tcyl);
|
|
|
|
*headp = thead;
|
|
|
|
*secp = MBR_PUT_MSCYLANDSEC(tcyl, tsec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is ONLY to be used as a last resort to provide a
|
|
|
|
* hint for the user. Ports should provide a more reliable way
|
|
|
|
* of getting the BIOS geometry. The i386 code, for example,
|
|
|
|
* uses the BIOS geometry as passed on from the bootblocks,
|
|
|
|
* and only uses this as a hint to the user when that information
|
|
|
|
* is not present, or a match could not be made with a NetBSD
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
guess_biosgeom_from_mbr(buf, cyl, head, sec)
|
|
|
|
char *buf;
|
|
|
|
int *cyl, *head, *sec;
|
|
|
|
{
|
|
|
|
struct mbr_partition *parts = (struct mbr_partition *)&buf[MBR_PARTOFF];
|
|
|
|
int cylinders = -1, heads = -1, sectors = -1, i, j;
|
|
|
|
int c1, h1, s1, c2, h2, s2;
|
|
|
|
long a1, a2;
|
|
|
|
quad_t num, denom;
|
|
|
|
|
|
|
|
*cyl = *head = *sec = -1;
|
|
|
|
|
|
|
|
/* Try to deduce the number of heads from two different mappings. */
|
|
|
|
for (i = 0; i < NMBRPART * 2; i++) {
|
|
|
|
if (get_mapping(parts, i, &c1, &h1, &s1, &a1) < 0)
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (get_mapping(parts, j, &c2, &h2, &s2, &a2) < 0)
|
|
|
|
continue;
|
|
|
|
num = (quad_t)h1*(a2-s2) - (quad_t)h2*(a1-s1);
|
|
|
|
denom = (quad_t)c2*(a1-s1) - (quad_t)c1*(a2-s2);
|
|
|
|
if (denom != 0 && num % denom == 0) {
|
|
|
|
heads = num / denom;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (heads != -1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (heads == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Now figure out the number of sectors from a single mapping. */
|
|
|
|
for (i = 0; i < NMBRPART * 2; i++) {
|
|
|
|
if (get_mapping(parts, i, &c1, &h1, &s1, &a1) < 0)
|
|
|
|
continue;
|
|
|
|
num = a1 - s1;
|
|
|
|
denom = c1 * heads + h1;
|
|
|
|
if (denom != 0 && num % denom == 0) {
|
|
|
|
sectors = num / denom;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectors == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate the number of cylinders.
|
|
|
|
* XXX relies on get_disks having been called.
|
|
|
|
*/
|
|
|
|
cylinders = disk->dd_totsec / heads / sectors;
|
|
|
|
|
|
|
|
/* Now verify consistency with each of the partition table entries.
|
|
|
|
* Be willing to shove cylinders up a little bit to make things work,
|
|
|
|
* but translation mismatches are fatal. */
|
|
|
|
for (i = 0; i < NMBRPART * 2; i++) {
|
|
|
|
if (get_mapping(parts, i, &c1, &h1, &s1, &a1) < 0)
|
|
|
|
continue;
|
|
|
|
if (sectors * (c1 * heads + h1) + s1 != a1)
|
|
|
|
return -1;
|
|
|
|
if (c1 >= cylinders)
|
|
|
|
cylinders = c1 + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Everything checks out. Reset the geometry to use for further
|
|
|
|
* calculations. */
|
|
|
|
*cyl = cylinders;
|
|
|
|
*head = heads;
|
|
|
|
*sec = sectors;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_mapping(parts, i, cylinder, head, sector, absolute)
|
|
|
|
struct mbr_partition *parts;
|
|
|
|
int i, *cylinder, *head, *sector;
|
|
|
|
long *absolute;
|
|
|
|
{
|
|
|
|
struct mbr_partition *part = &parts[i / 2];
|
|
|
|
|
|
|
|
if (part->mbrp_typ == 0)
|
|
|
|
return -1;
|
|
|
|
if (i % 2 == 0) {
|
|
|
|
*cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
|
|
|
|
*head = part->mbrp_shd;
|
|
|
|
*sector = MBR_PSECT(part->mbrp_ssect) - 1;
|
|
|
|
*absolute = part->mbrp_start;
|
|
|
|
} else {
|
|
|
|
*cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
|
|
|
|
*head = part->mbrp_ehd;
|
|
|
|
*sector = MBR_PSECT(part->mbrp_esect) - 1;
|
|
|
|
*absolute = part->mbrp_start + part->mbrp_size - 1;
|
1997-09-27 03:02:53 +04:00
|
|
|
}
|
1999-03-31 04:44:48 +04:00
|
|
|
return 0;
|
1997-09-27 03:02:53 +04:00
|
|
|
}
|