2003-04-30 23:47:10 +04:00
|
|
|
/* $NetBSD: fdisk.c,v 1.62 2003/04/30 19:47:10 dsl Exp $ */
|
1995-03-18 17:54:19 +03:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
/*
|
1993-04-09 23:23:56 +04:00
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1992 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
1994-09-23 08:30:13 +04:00
|
|
|
*
|
1993-04-09 23:23:56 +04:00
|
|
|
* 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.
|
1994-09-23 08:30:13 +04:00
|
|
|
*
|
1993-04-09 23:23:56 +04:00
|
|
|
* 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.
|
1994-09-23 08:30:13 +04:00
|
|
|
*
|
1993-04-09 23:23:56 +04:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
1994-09-23 08:30:13 +04:00
|
|
|
*
|
1993-04-09 23:23:56 +04:00
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
1994-09-23 08:30:13 +04:00
|
|
|
*
|
1993-04-09 23:23:56 +04:00
|
|
|
* any improvements or extensions that they make and grant Carnegie Mellon
|
|
|
|
* the rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/*
|
|
|
|
* 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
|
|
|
|
* Copyright (c) 1989 Robert. V. Baron
|
|
|
|
* Created.
|
|
|
|
*/
|
|
|
|
|
1997-09-06 01:26:57 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
1993-08-02 21:48:44 +04:00
|
|
|
#ifndef lint
|
2003-04-30 23:47:10 +04:00
|
|
|
__RCSID("$NetBSD: fdisk.c,v 1.62 2003/04/30 19:47:10 dsl Exp $");
|
1993-08-02 21:48:44 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/disklabel.h>
|
1999-01-28 00:41:31 +03:00
|
|
|
#include <sys/disklabel_mbr.h>
|
1993-04-09 23:23:56 +04:00
|
|
|
#include <sys/ioctl.h>
|
1997-09-14 17:52:26 +04:00
|
|
|
#include <sys/param.h>
|
1994-09-23 08:30:13 +04:00
|
|
|
#include <sys/stat.h>
|
2001-11-07 17:50:32 +03:00
|
|
|
#include <sys/sysctl.h>
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-12-05 23:15:31 +03:00
|
|
|
#include <ctype.h>
|
1994-09-23 08:30:13 +04:00
|
|
|
#include <err.h>
|
1997-09-14 17:52:26 +04:00
|
|
|
#include <errno.h>
|
1994-09-23 08:30:13 +04:00
|
|
|
#include <fcntl.h>
|
1997-09-14 17:52:26 +04:00
|
|
|
#include <paths.h>
|
1994-09-23 08:30:13 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-04-30 23:47:10 +04:00
|
|
|
#include <stdarg.h>
|
1994-12-05 23:15:31 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1997-09-25 09:08:28 +04:00
|
|
|
#include <util.h>
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2002-11-30 16:47:19 +03:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
1999-04-17 05:38:00 +04:00
|
|
|
#include <machine/cpu.h>
|
2003-04-30 23:47:10 +04:00
|
|
|
#define BOOTSEL
|
1999-04-17 05:38:00 +04:00
|
|
|
#endif
|
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
#define LBUF 100
|
|
|
|
static char lbuf[LBUF];
|
|
|
|
|
2000-01-31 18:54:48 +03:00
|
|
|
#ifndef _PATH_DEFDISK
|
2003-04-30 23:47:10 +04:00
|
|
|
#define _PATH_DEFDISK "/dev/rwd0d"
|
2000-01-31 18:54:48 +03:00
|
|
|
#endif
|
|
|
|
|
2000-12-19 19:01:28 +03:00
|
|
|
const char *disk = _PATH_DEFDISK;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
|
|
|
struct disklabel disklabel; /* disk parameters */
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
uint cylinders, sectors, heads;
|
|
|
|
daddr_t disksectors;
|
|
|
|
#define cylindersectors (heads * sectors)
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
mbr_sector_t mboot;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
|
|
|
|
struct {
|
|
|
|
mbr_sector_t *ptn; /* array of pbrs */
|
|
|
|
daddr_t base; /* first sector of ext. ptn */
|
|
|
|
daddr_t limit; /* last sector of ext. ptn */
|
|
|
|
int num_ptn; /* number of contained partitions */
|
|
|
|
int ptn_id; /* entry in mbr */
|
|
|
|
int is_corrupt; /* 1 if extended chain illegal */
|
|
|
|
} ext;
|
|
|
|
|
|
|
|
char *boot_dir = DEFAULT_BOOTDIR;
|
|
|
|
char *boot_path = 0; /* name of file we actually opened */
|
|
|
|
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
|
|
|
|
#define DEFAULT_ACTIVE (~(daddr_t)0)
|
|
|
|
|
|
|
|
#define OPTIONS "0123BSafiluvs:b:c:E:r:w:"
|
1999-04-17 05:38:00 +04:00
|
|
|
#else
|
2003-04-30 23:47:10 +04:00
|
|
|
#define change_part(e, p, id, st, sz, bm) change__part(e, p, id, st, sz)
|
|
|
|
#define OPTIONS "0123Safiluvs:b:c:E:r:w:"
|
1998-10-15 19:23:23 +04:00
|
|
|
#endif
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
uint dos_cylinders;
|
|
|
|
uint dos_heads;
|
|
|
|
uint dos_sectors;
|
|
|
|
daddr_t dos_disksectors;
|
|
|
|
#define dos_cylindersectors (dos_heads * dos_sectors)
|
|
|
|
#define dos_totalsectors (dos_heads * dos_sectors * dos_cylinders)
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
#define DOSSECT(s,c) (((s) & 0x3f) | (((c) >> 2) & 0xc0))
|
|
|
|
#define DOSCYL(c) ((c) & 0xff)
|
2003-04-30 23:47:10 +04:00
|
|
|
#define SEC_IN_1M (1024 * 1024 / 512)
|
|
|
|
#define SEC_TO_MB(sec) ((uint)(((sec) + SEC_IN_1M / 2) / SEC_IN_1M))
|
|
|
|
#define SEC_TO_CYL(sec) (((sec) + dos_cylindersectors/2) / dos_cylindersectors)
|
1998-09-28 19:44:18 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
#define MAXCYL 1024 /* Usual limit is 1023 */
|
|
|
|
#define MAXHEAD 256 /* Usual limit is 255 */
|
|
|
|
#define MAXSECTOR 63
|
1994-09-23 08:30:13 +04:00
|
|
|
int partition = -1;
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
int fd = -1, wfd = -1, *rfd = &fd;
|
|
|
|
char *disk_file;
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int a_flag; /* set active partition */
|
1998-09-28 19:44:18 +04:00
|
|
|
int i_flag; /* init bootcode */
|
1994-09-23 08:30:13 +04:00
|
|
|
int u_flag; /* update partition data */
|
2003-04-30 23:47:10 +04:00
|
|
|
int v_flag; /* more verbose */
|
1997-08-12 03:31:41 +04:00
|
|
|
int sh_flag; /* Output data as shell defines */
|
|
|
|
int f_flag; /* force --not interactive */
|
|
|
|
int s_flag; /* set id,offset,size */
|
1997-09-06 01:26:57 +04:00
|
|
|
int b_flag; /* Set cyl, heads, secs (as c/h/s) */
|
1999-04-17 05:38:00 +04:00
|
|
|
int B_flag; /* Edit/install bootselect code */
|
2003-04-30 23:47:10 +04:00
|
|
|
int E_flag; /* extended partition number */
|
1997-09-06 01:26:57 +04:00
|
|
|
int b_cyl, b_head, b_sec; /* b_flag values. */
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
mbr_sector_t bootcode[8192 / sizeof (mbr_sector_t)];
|
1998-10-15 19:23:23 +04:00
|
|
|
int bootsize; /* actual size of bootcode */
|
2003-04-30 23:47:10 +04:00
|
|
|
int boot_installed; /* 1 if we've copied code into the mbr */
|
|
|
|
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
struct disklist *dl;
|
|
|
|
#endif
|
1998-10-15 19:23:23 +04:00
|
|
|
|
2000-12-19 18:44:27 +03:00
|
|
|
|
1998-02-19 17:47:43 +03:00
|
|
|
static char reserved[] = "reserved";
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
struct part_type {
|
2000-12-19 19:01:28 +03:00
|
|
|
int type;
|
|
|
|
const char *name;
|
1994-09-23 08:30:13 +04:00
|
|
|
} part_types[] = {
|
2003-04-30 23:47:10 +04:00
|
|
|
{0x00, "<UNUSED>"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0x01, "Primary DOS with 12 bit FAT"},
|
|
|
|
{0x02, "XENIX / filesystem"},
|
|
|
|
{0x03, "XENIX /usr filesystem"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x04, "Primary DOS with 16 bit FAT <32M"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x05, "Extended partition"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x06, "Primary 'big' DOS, 16-bit FAT (> 32MB)"},
|
|
|
|
{0x07, "OS/2 HPFS or NTFS or QNX2 or Advanced UNIX"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x08, "AIX filesystem or OS/2 (thru v1.3) or DELL multiple drives"
|
|
|
|
"or Commodore DOS or SplitDrive"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0x09, "AIX boot partition or Coherent"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x0A, "OS/2 Boot Manager or Coherent swap or OPUS"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x0b, "Primary DOS with 32 bit FAT"},
|
|
|
|
{0x0c, "Primary DOS with 32 bit FAT - LBA"},
|
|
|
|
{0x0d, "Type 7??? - LBA"},
|
|
|
|
{0x0E, "DOS (16-bit FAT) - LBA"},
|
|
|
|
{0x0F, "Ext. partition - LBA"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0x10, "OPUS"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x11, "OS/2 BM: hidden DOS 12-bit FAT"},
|
|
|
|
{0x12, "Compaq diagnostics"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x14, "OS/2 BM: hidden DOS 16-bit FAT <32M or Novell DOS 7.0 bug"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x16, "OS/2 BM: hidden DOS 16-bit FAT >=32M"},
|
|
|
|
{0x17, "OS/2 BM: hidden IFS"},
|
|
|
|
{0x18, "AST Windows swapfile"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x19, "Willowtech Photon coS"},
|
|
|
|
{0x1e, "hidden FAT95"},
|
|
|
|
{0x20, "Willowsoft OFS1"},
|
|
|
|
{0x21, reserved},
|
|
|
|
{0x23, reserved},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x24, "NEC DOS"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x26, reserved},
|
|
|
|
{0x31, reserved},
|
|
|
|
{0x33, reserved},
|
|
|
|
{0x34, reserved},
|
|
|
|
{0x36, reserved},
|
|
|
|
{0x38, "Theos"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x3C, "PartitionMagic recovery"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x40, "VENIX 286 or LynxOS"},
|
|
|
|
{0x41, "Linux/MINIX (sharing disk with DRDOS) or Personal RISC boot"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x42, "SFS or Linux swap (sharing disk with DRDOS)"},
|
|
|
|
{0x43, "Linux native (sharing disk with DRDOS)"},
|
2000-06-14 23:35:50 +04:00
|
|
|
{0x4D, "QNX4.x"},
|
|
|
|
{0x4E, "QNX4.x 2nd part"},
|
|
|
|
{0x4F, "QNX4.x 3rd part"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x50, "DM (disk manager)"},
|
|
|
|
{0x51, "DM6 Aux1 (or Novell)"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0x52, "CP/M or Microport SysV/AT"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x53, "DM6 Aux3"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x54, "DM6 DDO"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x55, "EZ-Drive (disk manager)"},
|
|
|
|
{0x56, "Golden Bow (disk manager)"},
|
|
|
|
{0x5C, "Priam Edisk (disk manager)"},
|
|
|
|
{0x61, "SpeedStor"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x63, "GNU HURD or Mach or Sys V/386 (such as ISC UNIX) or MtXinu"},
|
|
|
|
{0x64, "Novell Netware 2.xx or Speedstore"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0x65, "Novell Netware 3.xx"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x66, "Novell 386 Netware"},
|
|
|
|
{0x67, "Novell"},
|
|
|
|
{0x68, "Novell"},
|
|
|
|
{0x69, "Novell"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x70, "DiskSecure Multi-Boot"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x71, reserved},
|
|
|
|
{0x73, reserved},
|
|
|
|
{0x74, reserved},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x75, "PC/IX"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x76, reserved},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x80, "MINIX until 1.4a"},
|
|
|
|
{0x81, "MINIX since 1.4b, early Linux, Mitac dmgr"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x82, "Linux swap or Prime or Solaris"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0x83, "Linux native"},
|
|
|
|
{0x84, "OS/2 hidden C: drive"},
|
|
|
|
{0x85, "Linux extended"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x86, "NT FAT volume set"},
|
|
|
|
{0x87, "NTFS volume set or HPFS mirrored"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0x93, "Amoeba filesystem"},
|
|
|
|
{0x94, "Amoeba bad block table"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0x99, "Mylex EISA SCSI"},
|
|
|
|
{0x9f, "BSDI?"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0xA0, "IBM Thinkpad hibernation"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xa1, reserved},
|
|
|
|
{0xa3, reserved},
|
|
|
|
{0xa4, reserved},
|
|
|
|
{0xA5, "FreeBSD or 386BSD or old NetBSD"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0xA6, "OpenBSD"},
|
|
|
|
{0xA7, "NeXTSTEP 486"},
|
2002-03-04 07:22:22 +03:00
|
|
|
{0xa8, "Apple UFS"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xa9, "NetBSD"},
|
2002-03-04 07:22:22 +03:00
|
|
|
{0xab, "Apple Boot"},
|
2002-09-28 03:19:56 +04:00
|
|
|
{0xaf, "Apple HFS"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xb1, reserved},
|
|
|
|
{0xb3, reserved},
|
|
|
|
{0xb4, reserved},
|
|
|
|
{0xb6, reserved},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0xB7, "BSDI BSD/386 filesystem"},
|
|
|
|
{0xB8, "BSDI BSD/386 swap"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xc0, "CTOS"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0xC1, "DRDOS/sec (FAT-12)"},
|
|
|
|
{0xC4, "DRDOS/sec (FAT-16, < 32M)"},
|
|
|
|
{0xC6, "DRDOS/sec (FAT-16, >= 32M)"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xC7, "Syrinx (Cyrnix?) or HPFS disabled"},
|
|
|
|
{0xd8, "CP/M 86"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0xDB, "CP/M or Concurrent CP/M or Concurrent DOS or CTOS"},
|
|
|
|
{0xE1, "DOS access or SpeedStor 12-bit FAT extended partition"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xE3, "DOS R/O or SpeedStor or Storage Dimensions"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0xE4, "SpeedStor 16-bit FAT extended partition < 1024 cyl."},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xe5, reserved},
|
|
|
|
{0xe6, reserved},
|
1999-02-09 22:11:46 +03:00
|
|
|
{0xeb, "BeOS"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xF1, "SpeedStor or Storage Dimensions"},
|
1994-09-23 08:30:13 +04:00
|
|
|
{0xF2, "DOS 3.3+ Secondary"},
|
1998-02-19 17:47:43 +03:00
|
|
|
{0xf3, reserved},
|
|
|
|
{0xF4, "SpeedStor large partition or Storage Dimensions"},
|
|
|
|
{0xf6, reserved},
|
|
|
|
{0xFE, "SpeedStor >1024 cyl. or LANstep or IBM PS/2 IML"},
|
1997-06-24 10:38:50 +04:00
|
|
|
{0xFF, "Xenix Bad Block Table"},
|
1993-04-09 23:23:56 +04:00
|
|
|
};
|
|
|
|
|
2002-03-27 02:56:05 +03:00
|
|
|
#define KNOWN_SYSIDS (sizeof(part_types)/sizeof(part_types[0]))
|
|
|
|
|
2000-12-19 18:44:27 +03:00
|
|
|
void usage(void);
|
|
|
|
void print_s0(int);
|
2003-04-30 23:47:10 +04:00
|
|
|
void print_part(mbr_sector_t *, int, daddr_t);
|
|
|
|
void print_mbr_partition(mbr_sector_t *, int, daddr_t, daddr_t, int);
|
|
|
|
int read_boot(const char *, void *, size_t, int);
|
|
|
|
void init_sector0(int);
|
2000-12-19 18:44:27 +03:00
|
|
|
void intuit_translated_geometry(void);
|
|
|
|
void get_geometry(void);
|
2003-04-30 23:47:10 +04:00
|
|
|
void get_extended_ptn(void);
|
2000-12-19 19:01:28 +03:00
|
|
|
void get_diskname(const char *, char *, size_t);
|
2003-04-30 23:47:10 +04:00
|
|
|
int change_part(int, int, int, daddr_t, daddr_t, char *);
|
2000-12-19 18:44:27 +03:00
|
|
|
void print_params(void);
|
|
|
|
void change_active(int);
|
|
|
|
void get_params_to_use(void);
|
|
|
|
void dos(int, unsigned char *, unsigned char *, unsigned char *);
|
|
|
|
int open_disk(int);
|
2003-04-30 23:47:10 +04:00
|
|
|
int read_disk(daddr_t, void *);
|
|
|
|
int write_disk(daddr_t, void *);
|
2000-12-19 18:44:27 +03:00
|
|
|
int get_params(void);
|
2003-04-30 23:47:10 +04:00
|
|
|
int read_s0(daddr_t, mbr_sector_t *);
|
|
|
|
int write_mbr(void);
|
|
|
|
int yesno(const char *, ...);
|
|
|
|
int decimal(const char *, int, int, int, int);
|
|
|
|
#define DEC_SEC 1 /* asking for a sector number */
|
|
|
|
#define DEC_RND 2 /* round to end of first track */
|
|
|
|
#define DEC_RND_0 4 /* round 0 to size of a track */
|
|
|
|
#define DEC_RND_DOWN 8 /* subtract 1 track */
|
|
|
|
#define DEC_RND_DOWN_2 16 /* subtract 2 tracks */
|
|
|
|
void string(const char *, int, char *);
|
|
|
|
int ptn_id(const char *, int *);
|
2000-12-19 18:44:27 +03:00
|
|
|
int type_match(const void *, const void *);
|
|
|
|
const char *get_type(int);
|
2003-04-30 23:47:10 +04:00
|
|
|
int get_mapping(int, uint *, uint *, uint *, unsigned long *);
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
daddr_t configure_bootsel(daddr_t);
|
|
|
|
void install_bootsel(int);
|
|
|
|
daddr_t get_default_boot(void);
|
|
|
|
void set_default_boot(daddr_t);
|
1999-04-17 05:38:00 +04:00
|
|
|
#endif
|
1997-09-06 01:26:57 +04:00
|
|
|
|
|
|
|
|
2000-12-19 18:44:27 +03:00
|
|
|
int main(int, char *[]);
|
1994-09-23 08:30:13 +04:00
|
|
|
|
|
|
|
int
|
2000-12-19 18:44:27 +03:00
|
|
|
main(int argc, char *argv[])
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
struct stat sb;
|
|
|
|
int ch, mib[2];
|
2002-04-03 07:17:36 +04:00
|
|
|
size_t len;
|
2001-11-07 17:50:32 +03:00
|
|
|
char *root_device;
|
2003-04-30 23:47:10 +04:00
|
|
|
char *cp;
|
|
|
|
int n;
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
daddr_t default_ptn; /* start sector of default ptn */
|
|
|
|
char *cbootmenu = 0;
|
|
|
|
#endif
|
1994-09-23 08:30:13 +04:00
|
|
|
|
1997-08-12 03:31:41 +04:00
|
|
|
int csysid, cstart, csize; /* For the b_flag. */
|
|
|
|
|
2001-11-07 17:50:32 +03:00
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_ROOT_DEVICE;
|
|
|
|
if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1 &&
|
|
|
|
(root_device = malloc(len)) != NULL &&
|
|
|
|
sysctl(mib, 2, root_device, &len, NULL, 0) != -1)
|
|
|
|
disk = root_device;
|
|
|
|
|
1997-09-06 01:26:57 +04:00
|
|
|
a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
|
2003-04-30 23:47:10 +04:00
|
|
|
v_flag = 0;
|
|
|
|
E_flag = 0;
|
1997-08-12 03:31:41 +04:00
|
|
|
csysid = cstart = csize = 0;
|
1999-04-17 05:38:00 +04:00
|
|
|
while ((ch = getopt(argc, argv, OPTIONS)) != -1)
|
1994-09-23 08:30:13 +04:00
|
|
|
switch (ch) {
|
|
|
|
case '0':
|
|
|
|
partition = 0;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
partition = 1;
|
1993-04-09 23:23:56 +04:00
|
|
|
break;
|
1994-09-23 08:30:13 +04:00
|
|
|
case '2':
|
|
|
|
partition = 2;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
partition = 3;
|
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'E': /* Extended partition number */
|
|
|
|
E_flag = 1;
|
|
|
|
partition = strtoul(optarg, &cp, 0);
|
|
|
|
if (*cp || partition < 0)
|
|
|
|
errx(1, "Bad partition number -E %s.", optarg);
|
|
|
|
break;
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
case 'B': /* Bootselect parameters */
|
1999-04-17 05:38:00 +04:00
|
|
|
B_flag = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'S': /* Output as shell variables */
|
1997-08-12 03:31:41 +04:00
|
|
|
sh_flag = 1;
|
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'a': /* Set active partition */
|
1994-09-23 08:30:13 +04:00
|
|
|
a_flag = 1;
|
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'f': /* Non interactive */
|
1997-08-12 03:31:41 +04:00
|
|
|
f_flag = 1;
|
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'i': /* Always update bootcode */
|
1994-09-23 08:30:13 +04:00
|
|
|
i_flag = 1;
|
1997-08-12 03:31:41 +04:00
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'l': /* List known partition types */
|
2002-03-27 02:56:05 +03:00
|
|
|
for (len = 0; len < KNOWN_SYSIDS; len++)
|
2002-10-08 11:38:47 +04:00
|
|
|
printf("%03d %s\n", part_types[len].type,
|
2003-04-30 23:47:10 +04:00
|
|
|
part_types[len].name);
|
2002-03-27 02:56:05 +03:00
|
|
|
return 0;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'u': /* Update partition details */
|
1994-09-23 08:30:13 +04:00
|
|
|
u_flag = 1;
|
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'v': /* Be verbose */
|
|
|
|
v_flag++;
|
|
|
|
break;
|
|
|
|
case 's': /* Partition details */
|
1997-07-29 12:31:31 +04:00
|
|
|
s_flag = 1;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (sscanf(optarg, "%d/%d/%d%n", &csysid, &cstart,
|
|
|
|
&csize, &n) == 3) {
|
|
|
|
if (optarg[n] == 0)
|
|
|
|
break;
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (optarg[n] == '/') {
|
|
|
|
cbootmenu = optarg + n + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
errx(1, "Bad argument to the -s flag.");
|
1997-09-06 01:26:57 +04:00
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'b': /* BIOS geometry */
|
1997-09-06 01:26:57 +04:00
|
|
|
b_flag = 1;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (sscanf(optarg, "%d/%d/%d%n", &b_cyl, &b_head,
|
|
|
|
&b_sec, &n) != 3 || optarg[n] != 0)
|
2002-08-08 17:19:18 +04:00
|
|
|
errx(1, "Bad argument to the -b flag.");
|
1998-09-28 19:44:18 +04:00
|
|
|
if (b_cyl > MAXCYL)
|
|
|
|
b_cyl = MAXCYL;
|
1997-07-29 12:31:31 +04:00
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
case 'c': /* file/directory containing boot code */
|
|
|
|
if (strchr(optarg, '/') != NULL &&
|
|
|
|
stat(optarg, &sb) == 0 &&
|
|
|
|
(sb.st_mode & S_IFMT) == S_IFDIR) {
|
|
|
|
boot_dir = optarg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bootsize = read_boot(optarg, bootcode,
|
|
|
|
sizeof bootcode, 1);
|
|
|
|
i_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'r': /* read data from disk_file (not raw disk) */
|
|
|
|
rfd = &wfd;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'w': /* write data to disk_file */
|
|
|
|
disk_file = optarg;
|
1998-10-15 19:23:23 +04:00
|
|
|
break;
|
2003-04-30 23:47:10 +04:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
default:
|
|
|
|
usage();
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
1994-09-23 08:30:13 +04:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1997-08-12 03:31:41 +04:00
|
|
|
if (sh_flag && (a_flag || i_flag || u_flag || f_flag || s_flag))
|
|
|
|
usage();
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (B_flag && f_flag) {
|
|
|
|
warnx("Bootselector may only be configured interactively");
|
1999-04-17 05:38:00 +04:00
|
|
|
usage();
|
2003-04-30 23:47:10 +04:00
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (f_flag && u_flag && !s_flag) {
|
|
|
|
warnx("Partition data not specified");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_flag && partition == -1) {
|
2002-03-27 02:56:05 +03:00
|
|
|
warnx("-s flag requires a partition selected.");
|
1997-07-29 12:31:31 +04:00
|
|
|
usage();
|
1997-08-12 03:31:41 +04:00
|
|
|
}
|
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
if (argc > 0)
|
|
|
|
disk = argv[0];
|
1994-09-23 08:30:13 +04:00
|
|
|
|
1999-04-17 05:38:00 +04:00
|
|
|
if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
|
1993-04-09 23:23:56 +04:00
|
|
|
exit(1);
|
|
|
|
|
2000-12-19 19:01:28 +03:00
|
|
|
if (read_s0(0, &mboot))
|
2003-04-30 23:47:10 +04:00
|
|
|
/* must have been a blank disk */
|
|
|
|
init_sector0(1);
|
1995-10-05 02:11:19 +03:00
|
|
|
|
2002-11-30 16:47:19 +03:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
1999-04-17 05:38:00 +04:00
|
|
|
get_geometry();
|
|
|
|
#else
|
1995-10-05 02:11:19 +03:00
|
|
|
intuit_translated_geometry();
|
1999-04-17 05:38:00 +04:00
|
|
|
#endif
|
2003-04-30 23:47:10 +04:00
|
|
|
get_extended_ptn();
|
1997-08-12 03:31:41 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
#ifdef BOOTSEL
|
|
|
|
default_ptn = get_default_boot();
|
|
|
|
#endif
|
1997-08-12 03:31:41 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (E_flag && !u_flag && partition >= ext.num_ptn)
|
|
|
|
errx(1, "Extended partition %d is not defined.", partition);
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (u_flag && (!f_flag || b_flag))
|
|
|
|
get_params_to_use();
|
1997-08-12 03:31:41 +04:00
|
|
|
|
|
|
|
/* Do the update stuff! */
|
|
|
|
if (u_flag) {
|
2003-04-30 23:47:10 +04:00
|
|
|
if (s_flag)
|
|
|
|
change_part(E_flag, partition, csysid, cstart, csize,
|
|
|
|
cbootmenu);
|
|
|
|
else {
|
|
|
|
int part = partition, chg_ext = E_flag, prompt = 1;
|
|
|
|
do {
|
|
|
|
if (prompt) {
|
|
|
|
printf("\n");
|
|
|
|
print_s0(partition);
|
|
|
|
}
|
|
|
|
if (partition == -1)
|
|
|
|
part = ptn_id(
|
|
|
|
"Which partition do you want to change?",
|
|
|
|
&chg_ext);
|
|
|
|
if (part < 0)
|
|
|
|
break;
|
|
|
|
prompt = change_part(chg_ext, part, 0, 0, 0, 0);
|
|
|
|
} while (partition == -1);
|
|
|
|
}
|
1994-09-23 08:30:13 +04:00
|
|
|
} else
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!i_flag && !B_flag) {
|
|
|
|
print_params();
|
1997-08-12 03:31:41 +04:00
|
|
|
print_s0(partition);
|
2003-04-30 23:47:10 +04:00
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (a_flag && !E_flag)
|
1993-04-09 23:23:56 +04:00
|
|
|
change_active(partition);
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (B_flag || u_flag || i_flag)
|
|
|
|
/* Ensure the mbr code supports this configuration */
|
|
|
|
install_bootsel(0);
|
|
|
|
if (B_flag)
|
|
|
|
default_ptn = configure_bootsel(default_ptn);
|
|
|
|
set_default_boot(default_ptn);
|
|
|
|
#else
|
|
|
|
if (i_flag)
|
|
|
|
init_sector0(0);
|
1999-04-17 05:38:00 +04:00
|
|
|
#endif
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (u_flag || a_flag || i_flag || B_flag) {
|
1997-08-12 03:31:41 +04:00
|
|
|
if (!f_flag) {
|
1999-04-17 05:38:00 +04:00
|
|
|
printf("\nWe haven't written the MBR back to disk "
|
1997-08-12 03:31:41 +04:00
|
|
|
"yet. This is your last chance.\n");
|
2003-04-30 23:47:10 +04:00
|
|
|
if (u_flag)
|
|
|
|
print_s0(-1);
|
1997-08-12 03:31:41 +04:00
|
|
|
if (yesno("Should we write new partition table?"))
|
2003-04-30 23:47:10 +04:00
|
|
|
write_mbr();
|
1997-08-12 03:31:41 +04:00
|
|
|
} else
|
2003-04-30 23:47:10 +04:00
|
|
|
write_mbr();
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
1994-09-23 08:30:13 +04:00
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
usage(void)
|
1994-09-23 08:30:13 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
int indent = 7 + (int)strlen(getprogname()) + 1;
|
|
|
|
|
|
|
|
(void)fprintf(stderr, "Usage: %s [-afiluvBS] "
|
|
|
|
"[-b cylinders/heads/sectors] \\\n"
|
|
|
|
"%*s[-0123 | -E num "
|
|
|
|
"[-s id/start/size[/bootmenu]]] \\\n"
|
|
|
|
"%*s[-c bootcode] [-r|-w file] [device]\n"
|
|
|
|
"\t-a change active partition\n"
|
|
|
|
"\t-f force - not interactive\n"
|
|
|
|
"\t-i initialise MBR code\n"
|
|
|
|
"\t-l list partition types\n"
|
|
|
|
"\t-u update partition data\n"
|
|
|
|
"\t-v verbose output, -v -v more verbose still\n"
|
|
|
|
"\t-B update bootselect options\n"
|
|
|
|
"\t-S output as shell defines\n",
|
|
|
|
getprogname(), indent, "", indent, "");
|
1994-09-23 08:30:13 +04:00
|
|
|
exit(1);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
static daddr_t
|
|
|
|
ext_offset(int part)
|
|
|
|
{
|
|
|
|
daddr_t offset = ext.base;
|
|
|
|
|
|
|
|
if (part != 0)
|
|
|
|
offset += le32toh(ext.ptn[part - 1].mbr_parts[1].mbrp_start);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
print_s0(int which)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
int part;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
if (which == -1) {
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!sh_flag)
|
|
|
|
printf("Partition table:\n");
|
1999-01-28 00:41:31 +03:00
|
|
|
for (part = 0; part < NMBRPART; part++) {
|
1997-08-12 03:31:41 +04:00
|
|
|
if (!sh_flag)
|
|
|
|
printf("%d: ", part);
|
2003-04-30 23:47:10 +04:00
|
|
|
print_part(&mboot, part, 0);
|
1997-08-12 03:31:41 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!sh_flag) {
|
|
|
|
if (ext.is_corrupt)
|
|
|
|
printf("Extended partition table is currupt\n");
|
|
|
|
else
|
|
|
|
if (ext.num_ptn != 0)
|
|
|
|
printf("Extended partition table:\n");
|
|
|
|
}
|
|
|
|
for (part = 0; part < ext.num_ptn; part++) {
|
|
|
|
if (!sh_flag)
|
|
|
|
printf("E%d: ", part);
|
|
|
|
print_part(&ext.ptn[part], 0, ext_offset(part));
|
|
|
|
if (!sh_flag && v_flag >= 2) {
|
|
|
|
printf("link: ");
|
|
|
|
print_mbr_partition(&ext.ptn[part], 1,
|
|
|
|
ext_offset(part), ext.base, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (!sh_flag &&
|
|
|
|
le16toh(mboot.mbr_bootsel.mbrb_magic) == MBR_MAGIC){
|
|
|
|
int tmo;
|
|
|
|
printf("Bootselector ");
|
|
|
|
if (mboot.mbr_bootsel.mbrb_flags & BFL_SELACTIVE) {
|
|
|
|
printf("enabled");
|
|
|
|
tmo = le16toh(mboot.mbr_bootsel.mbrb_timeo);
|
|
|
|
if (tmo == 0xffff)
|
|
|
|
printf(", infinite timeout");
|
|
|
|
else
|
|
|
|
printf(", timeout %d seconds",
|
|
|
|
(10 * tmo + 9) / 182);
|
|
|
|
} else
|
|
|
|
printf("disabled");
|
|
|
|
printf(".\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
1997-03-29 23:46:17 +03:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (E_flag) {
|
|
|
|
if (!sh_flag)
|
|
|
|
printf("Extended partition E%d:\n", which);
|
|
|
|
if (which > ext.num_ptn)
|
|
|
|
printf("Undefined\n");
|
|
|
|
else
|
|
|
|
print_part(&ext.ptn[which], 0, ext_offset(which));
|
|
|
|
} else {
|
|
|
|
if (!sh_flag)
|
|
|
|
printf("Partition %d:\n", which);
|
|
|
|
print_part(&mboot, which, 0);
|
|
|
|
}
|
1997-03-29 23:46:17 +03:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
void
|
|
|
|
print_part(mbr_sector_t *boot, int part, daddr_t offset)
|
1997-03-29 23:46:17 +03:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
mbr_partition_t *partp;
|
|
|
|
char *e;
|
1997-03-29 23:46:17 +03:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!sh_flag) {
|
|
|
|
print_mbr_partition(boot, part, offset, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
1997-03-29 23:46:17 +03:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
partp = &boot->mbr_parts[part];
|
|
|
|
if (boot != &mboot) {
|
|
|
|
part = boot - ext.ptn;
|
|
|
|
e = "E";
|
|
|
|
} else
|
|
|
|
e = "";
|
1997-03-29 23:46:17 +03:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (partp->mbrp_typ == 0) {
|
|
|
|
printf("PART%s%dSIZE=0\n", e, part);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("PART%s%dID=%d\n", e, part, partp->mbrp_typ);
|
|
|
|
printf("PART%s%dSIZE=%u\n", e, part, le32toh(partp->mbrp_size));
|
|
|
|
printf("PART%s%dSTART=%llu\n", e, part,
|
|
|
|
(uintmax_t)(offset + le32toh(partp->mbrp_start)));
|
|
|
|
printf("PART%s%dFLAG=0x%x\n", e, part, partp->mbrp_flag);
|
|
|
|
printf("PART%s%dBCYL=%d\n", e, part,
|
|
|
|
MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect));
|
|
|
|
printf("PART%s%dBHEAD=%d\n", e, part, partp->mbrp_shd);
|
|
|
|
printf("PART%s%dBSEC=%d\n", e, part, MBR_PSECT(partp->mbrp_ssect));
|
|
|
|
printf("PART%s%dECYL=%d\n", e, part,
|
|
|
|
MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect));
|
|
|
|
printf("PART%s%dEHEAD=%d\n", e, part, partp->mbrp_ehd);
|
|
|
|
printf("PART%s%dESEC=%d\n", e, part, MBR_PSECT(partp->mbrp_esect));
|
1997-03-29 23:46:17 +03:00
|
|
|
}
|
|
|
|
|
2000-12-19 18:44:27 +03:00
|
|
|
static void
|
2003-04-30 23:47:10 +04:00
|
|
|
pr_cyls(daddr_t sector)
|
1997-03-29 23:46:17 +03:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
ulong cyl, head, sect;
|
|
|
|
cyl = sector / dos_cylindersectors;
|
|
|
|
sect = sector - cyl * dos_cylindersectors;
|
|
|
|
head = sect / dos_sectors;
|
|
|
|
sect -= head * dos_sectors;
|
|
|
|
|
|
|
|
printf("%lu", cyl);
|
|
|
|
if (head == 0 && sect == 0)
|
|
|
|
return;
|
|
|
|
printf("/%lu/%lu", head, sect + 1);
|
1997-03-29 23:46:17 +03:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2003-04-30 23:47:10 +04:00
|
|
|
print_mbr_partition(mbr_sector_t *boot, int part,
|
|
|
|
daddr_t offset, daddr_t exoffset, int indent)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
daddr_t start;
|
|
|
|
daddr_t size;
|
|
|
|
mbr_partition_t *partp = &boot->mbr_parts[part];
|
|
|
|
mbr_sector_t eboot;
|
|
|
|
int p;
|
|
|
|
static int dumped = 0;
|
|
|
|
|
|
|
|
if (partp->mbrp_typ == 0 && v_flag < 2) {
|
|
|
|
printf("<UNUSED>\n");
|
|
|
|
return;
|
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
start = le32toh(partp->mbrp_start);
|
|
|
|
size = le32toh(partp->mbrp_size);
|
|
|
|
if (MBR_IS_EXTENDED(partp->mbrp_typ))
|
|
|
|
start += exoffset;
|
|
|
|
else
|
|
|
|
start += offset;
|
|
|
|
|
|
|
|
printf("%s (sysid %d)\n", get_type(partp->mbrp_typ), partp->mbrp_typ);
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC &&
|
|
|
|
boot->mbr_bootsel.mbrb_nametab[part][0])
|
|
|
|
printf("%*s bootmenu: %s\n", indent, "",
|
|
|
|
boot->mbr_bootsel.mbrb_nametab[part]);
|
|
|
|
#endif
|
1997-07-29 12:31:31 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
printf("%*s start %llu, size %llu",
|
|
|
|
indent, "", (uintmax_t)start, (uintmax_t)size);
|
|
|
|
if (size != 0) {
|
|
|
|
printf(" (%u MB, Cyls ", SEC_TO_MB(size));
|
|
|
|
if (v_flag == 0 && le32toh(partp->mbrp_start) == dos_sectors)
|
|
|
|
pr_cyls(start - dos_sectors);
|
|
|
|
else
|
|
|
|
pr_cyls(start);
|
|
|
|
printf("-");
|
|
|
|
pr_cyls(start + size);
|
|
|
|
printf(")");
|
|
|
|
}
|
1997-07-29 12:31:31 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
switch (partp->mbrp_flag) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case MBR_FLAGS_ACTIVE:
|
|
|
|
printf(", Active");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf(", flag 0x%x", partp->mbrp_flag);
|
|
|
|
break;
|
1997-07-29 12:31:31 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
printf("\n");
|
2000-12-19 19:01:28 +03:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (v_flag) {
|
|
|
|
printf("%*s beg: cylinder %4d, head %3d, sector %2d\n",
|
|
|
|
indent, "",
|
|
|
|
MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect),
|
|
|
|
partp->mbrp_shd, MBR_PSECT(partp->mbrp_ssect));
|
|
|
|
printf("%*s end: cylinder %4d, head %3d, sector %2d\n",
|
|
|
|
indent, "",
|
|
|
|
MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect),
|
|
|
|
partp->mbrp_ehd, MBR_PSECT(partp->mbrp_esect));
|
|
|
|
}
|
1997-07-29 12:31:31 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!MBR_IS_EXTENDED(partp->mbrp_typ) ||
|
|
|
|
(v_flag <= 2 && !ext.is_corrupt))
|
1993-04-09 23:23:56 +04:00
|
|
|
return;
|
2003-04-30 23:47:10 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Recursive dump extended table,
|
|
|
|
* This is read from the disk - so is wrong during editing.
|
|
|
|
* Just ensure we only show it once.
|
|
|
|
*/
|
|
|
|
if (dumped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%*s Extended partition table:\n", indent, "");
|
|
|
|
indent += 4;
|
|
|
|
if (read_s0(start, &eboot) == -1)
|
|
|
|
return;
|
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
printf("%*s%d: ", indent, "", p);
|
|
|
|
print_mbr_partition(&eboot, p, start,
|
|
|
|
exoffset ? exoffset : start, indent);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
|
|
|
|
if (exoffset == 0)
|
|
|
|
dumped = 1;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1999-04-17 05:38:00 +04:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
read_boot(const char *name, void *buf, size_t len, int err_exit)
|
1998-10-15 19:23:23 +04:00
|
|
|
{
|
1999-04-17 05:38:00 +04:00
|
|
|
int bfd, ret;
|
1998-10-15 19:23:23 +04:00
|
|
|
struct stat st;
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (boot_path != NULL)
|
|
|
|
free(boot_path);
|
|
|
|
boot_path = malloc(strlen(boot_dir) + 1 + strlen(name) + 1);
|
|
|
|
if (boot_path == NULL)
|
|
|
|
err(1, "Malloc failed");
|
|
|
|
if (strchr(name, '/') == 0) {
|
|
|
|
strcpy(boot_path, boot_dir);
|
|
|
|
strcat(boot_path, "/");
|
|
|
|
} else
|
|
|
|
boot_path[0] = 0;
|
|
|
|
strcat(boot_path, name);
|
|
|
|
|
|
|
|
if ((bfd = open(boot_path, O_RDONLY)) < 0 || fstat(bfd, &st) == -1) {
|
|
|
|
warn("%s", boot_path);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.st_size > (off_t)len) {
|
|
|
|
warnx("%s: bootcode too large", boot_path);
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
ret = st.st_size;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (ret < 0x200) {
|
|
|
|
warnx("%s: bootcode too small", boot_path);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (read(bfd, buf, len) != ret) {
|
|
|
|
warn("%s", boot_path);
|
|
|
|
goto fail;
|
|
|
|
}
|
1998-10-15 19:23:23 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do some sanity checking here
|
|
|
|
*/
|
2003-04-30 23:47:10 +04:00
|
|
|
if (le16toh(bootcode[0].mbr_signature) != MBR_MAGIC) {
|
|
|
|
warnx("%s: invalid magic", boot_path);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(bfd);
|
|
|
|
ret = (ret + 0x1ff) & ~0x1ff;
|
1999-04-17 05:38:00 +04:00
|
|
|
return ret;
|
2003-04-30 23:47:10 +04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
close(bfd);
|
|
|
|
if (err_exit)
|
|
|
|
exit(1);
|
|
|
|
return 0;
|
1998-10-15 19:23:23 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2003-04-30 23:47:10 +04:00
|
|
|
init_sector0(int dopart)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1997-08-12 03:31:41 +04:00
|
|
|
int i;
|
2003-04-30 23:47:10 +04:00
|
|
|
int copy_size = MBR_PARTOFF;
|
1997-08-12 03:31:41 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
#ifdef DEFAULT_BOOTCODE
|
|
|
|
if (bootsize == 0)
|
|
|
|
bootsize = read_boot(DEFAULT_BOOTCODE, bootcode,
|
|
|
|
sizeof bootcode, 1);
|
|
|
|
#endif
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (le16toh(mboot.mbr_bootsel.mbrb_magic) == MBR_MAGIC
|
|
|
|
&& le16toh(bootcode[0].mbr_bootsel.mbrb_magic) == MBR_MAGIC)
|
|
|
|
copy_size = MBR_BOOTSELOFF;
|
1998-10-15 19:23:23 +04:00
|
|
|
#endif
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (bootsize != 0) {
|
|
|
|
boot_installed = 1;
|
|
|
|
memcpy(&mboot.mbr_bootinst, bootcode, copy_size);
|
|
|
|
}
|
|
|
|
mboot.mbr_signature = htole16(MBR_MAGIC);
|
1997-08-12 03:31:41 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!dopart)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
memset(&mboot.mbr_parts[i], 0, sizeof(mboot.mbr_parts[i]));
|
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
void
|
|
|
|
get_extended_ptn(void)
|
|
|
|
{
|
|
|
|
mbr_partition_t *mp;
|
|
|
|
mbr_sector_t *boot;
|
|
|
|
daddr_t offset;
|
|
|
|
|
|
|
|
/* find first (there should only be one) extended partition */
|
|
|
|
for (mp = mboot.mbr_parts; !MBR_IS_EXTENDED(mp->mbrp_typ); mp++)
|
|
|
|
if (mp >= &mboot.mbr_parts[NMBRPART])
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The extended partition should be structured as a linked list
|
|
|
|
* (even though it appears, at first glance, to be a tree).
|
|
|
|
*/
|
|
|
|
ext.base = le32toh(mp->mbrp_start);
|
|
|
|
ext.limit = ext.base + le32toh(mp->mbrp_size);
|
|
|
|
ext.ptn_id = mp - mboot.mbr_parts;
|
|
|
|
for (offset = 0;; offset = le32toh(boot->mbr_parts[1].mbrp_start)) {
|
|
|
|
ext.ptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
|
|
|
|
if (ext.ptn == NULL)
|
|
|
|
err(1, "Malloc failed");
|
|
|
|
boot = ext.ptn + ext.num_ptn;
|
|
|
|
if (read_s0(offset + ext.base, boot) == -1)
|
|
|
|
break;
|
|
|
|
/* expect p0 to be valid and p1 to be another extended ptn */
|
|
|
|
if (MBR_IS_EXTENDED(boot->mbr_parts[0].mbrp_typ))
|
|
|
|
break;
|
|
|
|
if (boot->mbr_parts[1].mbrp_typ != 0 &&
|
|
|
|
!MBR_IS_EXTENDED(boot->mbr_parts[1].mbrp_typ))
|
|
|
|
break;
|
|
|
|
/* p2 and p3 should be unallocated */
|
|
|
|
if (boot->mbr_parts[2].mbrp_typ != 0 ||
|
|
|
|
boot->mbr_parts[3].mbrp_typ != 0)
|
|
|
|
break;
|
|
|
|
/* data ptn inside extended one */
|
|
|
|
if (boot->mbr_parts[0].mbrp_typ != 0 &&
|
|
|
|
offset + le32toh(boot->mbr_parts[0].mbrp_start)
|
|
|
|
+ le32toh(boot->mbr_parts[0].mbrp_size) > ext.limit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ext.num_ptn++;
|
|
|
|
|
|
|
|
if (boot->mbr_parts[1].mbrp_typ == 0)
|
|
|
|
/* end of extended partition chain */
|
|
|
|
return;
|
|
|
|
/* must be in sector order */
|
|
|
|
if (offset >= le32toh(boot->mbr_parts[1].mbrp_start))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
warnx("Extended partition table is corrupt\n");
|
|
|
|
ext.is_corrupt = 1;
|
|
|
|
ext.num_ptn = 0;
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2002-11-30 16:47:19 +03:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
1999-04-17 05:38:00 +04:00
|
|
|
|
|
|
|
void
|
2000-12-19 19:01:28 +03:00
|
|
|
get_diskname(const char *fullname, char *diskname, size_t size)
|
1999-04-17 05:38:00 +04:00
|
|
|
{
|
2000-12-19 19:01:28 +03:00
|
|
|
const char *p, *p2;
|
1999-04-17 05:38:00 +04:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
p = strrchr(fullname, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
p = fullname;
|
|
|
|
else
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == 0) {
|
2003-04-30 23:47:10 +04:00
|
|
|
strlcpy(diskname, fullname, size);
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == 'r')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
for (p2 = p; *p2 != 0; p2++)
|
|
|
|
if (isdigit(*p2))
|
|
|
|
break;
|
|
|
|
if (*p2 == 0) {
|
|
|
|
/* XXX invalid diskname? */
|
2003-04-30 23:47:10 +04:00
|
|
|
strlcpy(diskname, fullname, size);
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (isdigit(*p2))
|
|
|
|
p2++;
|
|
|
|
|
|
|
|
len = p2 - p;
|
|
|
|
if (len > size) {
|
|
|
|
/* XXX */
|
2003-04-30 23:47:10 +04:00
|
|
|
strlcpy(diskname, fullname, size);
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
memcpy(diskname, p, len);
|
1999-04-17 05:38:00 +04:00
|
|
|
diskname[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
get_geometry(void)
|
1999-04-17 05:38:00 +04:00
|
|
|
{
|
|
|
|
int mib[2], i;
|
|
|
|
size_t len;
|
|
|
|
struct biosdisk_info *bip;
|
|
|
|
struct nativedisk_info *nip;
|
|
|
|
char diskname[8];
|
|
|
|
|
|
|
|
mib[0] = CTL_MACHDEP;
|
|
|
|
mib[1] = CPU_DISKINFO;
|
|
|
|
if (sysctl(mib, 2, NULL, &len, NULL, 0) < 0) {
|
|
|
|
intuit_translated_geometry();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dl = (struct disklist *) malloc(len);
|
|
|
|
sysctl(mib, 2, dl, &len, NULL, 0);
|
|
|
|
|
|
|
|
get_diskname(disk, diskname, sizeof diskname);
|
|
|
|
|
|
|
|
for (i = 0; i < dl->dl_nnativedisks; i++) {
|
|
|
|
nip = &dl->dl_nativedisks[i];
|
|
|
|
if (strcmp(diskname, nip->ni_devname))
|
|
|
|
continue;
|
|
|
|
/*
|
2003-04-30 23:47:10 +04:00
|
|
|
* XXX listing possible matches is better. This is ok for
|
|
|
|
* now because the user has a chance to change it later.
|
|
|
|
* Also, if all the disks have the same parameters then we can
|
|
|
|
* just use them, we don't need to know which disk is which.
|
1999-04-17 05:38:00 +04:00
|
|
|
*/
|
|
|
|
if (nip->ni_nmatches != 0) {
|
|
|
|
bip = &dl->dl_biosdisks[nip->ni_biosmatches[0]];
|
|
|
|
dos_cylinders = bip->bi_cyl;
|
|
|
|
dos_heads = bip->bi_head;
|
|
|
|
dos_sectors = bip->bi_sec;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (bip->bi_lbasecs)
|
|
|
|
dos_disksectors = bip->bi_lbasecs;
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Allright, allright, make a stupid guess.. */
|
|
|
|
intuit_translated_geometry();
|
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
#endif
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
#ifdef BOOTSEL
|
|
|
|
daddr_t
|
|
|
|
get_default_boot(void)
|
1999-04-17 05:38:00 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
uint id;
|
|
|
|
int p;
|
|
|
|
|
|
|
|
if (le16toh(mboot.mbr_bootsel.mbrb_magic) != MBR_MAGIC)
|
|
|
|
/* default to first active partition */
|
|
|
|
return DEFAULT_ACTIVE;
|
|
|
|
|
|
|
|
if (mboot.mbr_bootsel.mbrb_defkey == SCAN_ENTER)
|
|
|
|
return DEFAULT_ACTIVE;
|
|
|
|
|
|
|
|
id = mboot.mbr_bootsel.mbrb_defkey;
|
|
|
|
if (!(mboot.mbr_bootsel.mbrb_flags & BFL_NEWMBR)) {
|
|
|
|
/* F1..F4 => ptn 0..3, F5+ => disk 0+ */
|
|
|
|
id -= SCAN_F1;
|
|
|
|
if (id >= NMBRPART)
|
|
|
|
/* Return number of disk */
|
|
|
|
return id - NMBRPART;
|
|
|
|
if (mboot.mbr_parts[id].mbrp_typ != 0)
|
|
|
|
return le32toh(mboot.mbr_parts[id].mbrp_start);
|
|
|
|
return DEFAULT_ACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1+ => allocated partition id, F1+ => disk 0+ */
|
|
|
|
if (id >= SCAN_F1)
|
|
|
|
return id - SCAN_F1;
|
|
|
|
id -= SCAN_1;
|
|
|
|
|
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
if (mboot.mbr_parts[p].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (mboot.mbr_bootsel.mbrb_nametab[p][0] == 0)
|
|
|
|
continue;
|
|
|
|
if (id-- == 0)
|
|
|
|
return le32toh(mboot.mbr_parts[p].mbrp_start);
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
for (p = 0; p < ext.num_ptn; p++) {
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (ext.ptn[p].mbr_bootsel.mbrb_nametab[0][0] == 0)
|
|
|
|
continue;
|
|
|
|
if (id-- == 0)
|
|
|
|
return ext_offset(p)
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start);
|
|
|
|
}
|
|
|
|
|
|
|
|
return DEFAULT_ACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
set_default_boot(daddr_t default_ptn)
|
|
|
|
{
|
|
|
|
int p;
|
|
|
|
int key = SCAN_1;
|
|
|
|
|
|
|
|
if (le16toh(mboot.mbr_bootsel.mbrb_magic) != MBR_MAGIC)
|
|
|
|
/* sanity */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (default_ptn == DEFAULT_ACTIVE) {
|
|
|
|
mboot.mbr_bootsel.mbrb_defkey = SCAN_ENTER;
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
if (mboot.mbr_parts[p].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (mboot.mbr_bootsel.mbrb_nametab[p][0] == 0)
|
|
|
|
continue;
|
|
|
|
if (le32toh(mboot.mbr_parts[p].mbrp_start) == default_ptn) {
|
|
|
|
if (mboot.mbr_bootsel.mbrb_flags & BFL_NEWMBR)
|
|
|
|
mboot.mbr_bootsel.mbrb_defkey = key;
|
|
|
|
else
|
|
|
|
mboot.mbr_bootsel.mbrb_defkey = SCAN_F1 + p;
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
key++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mboot.mbr_bootsel.mbrb_flags & BFL_EXTLBA) {
|
|
|
|
for (p = 0; p < ext.num_ptn; p++) {
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (ext.ptn[p].mbr_bootsel.mbrb_nametab[0][0] == 0)
|
|
|
|
continue;
|
|
|
|
if (le32toh(ext.ptn[p].mbr_parts[0].mbrp_start) +
|
|
|
|
ext_offset(p) == default_ptn) {
|
|
|
|
mboot.mbr_bootsel.mbrb_defkey = key;
|
1999-04-17 05:38:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
key++;
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (default_ptn < 8) {
|
|
|
|
if (mboot.mbr_bootsel.mbrb_flags & BFL_NEWMBR)
|
|
|
|
key = SCAN_F1;
|
1999-04-17 05:38:00 +04:00
|
|
|
else
|
2003-04-30 23:47:10 +04:00
|
|
|
key = SCAN_F1 + 4;
|
|
|
|
mboot.mbr_bootsel.mbrb_defkey = key + default_ptn;
|
|
|
|
return;
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/* Default to first active partition */
|
|
|
|
mboot.mbr_bootsel.mbrb_defkey = SCAN_ENTER;
|
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
void
|
|
|
|
install_bootsel(int needed)
|
|
|
|
{
|
|
|
|
struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
|
|
|
|
int p;
|
|
|
|
int ext13 = 0;
|
|
|
|
char *code;
|
|
|
|
|
|
|
|
/* Work out which boot code we need for this configuration */
|
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
if (mboot.mbr_parts[p].mbrp_typ == 0)
|
1999-04-17 05:38:00 +04:00
|
|
|
continue;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (le16toh(mbs->mbrb_magic) != MBR_MAGIC)
|
|
|
|
break;
|
|
|
|
if (mbs->mbrb_nametab[p][0] == 0)
|
1999-04-17 05:38:00 +04:00
|
|
|
continue;
|
2003-04-30 23:47:10 +04:00
|
|
|
needed |= BFL_SELACTIVE;
|
|
|
|
if (le32toh(mboot.mbr_parts[p].mbrp_start) >= dos_totalsectors)
|
|
|
|
ext13 = BFL_EXTINT13;
|
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
for (p = 0; p < ext.num_ptn; p++) {
|
|
|
|
if (le16toh(ext.ptn[p].mbr_bootsel.mbrb_magic) != MBR_MAGIC)
|
|
|
|
continue;
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (ext.ptn[p].mbr_bootsel.mbrb_nametab[p][0] == 0)
|
|
|
|
continue;
|
|
|
|
needed |= BFL_EXTLBA | BFL_SELACTIVE;
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (B_flag)
|
|
|
|
needed |= BFL_SELACTIVE;
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/* Is the installed code good enough ? */
|
|
|
|
if (!i_flag && (needed == 0 || (le16toh(mbs->mbrb_magic) == MBR_MAGIC
|
|
|
|
&& (mbs->mbrb_flags & needed) == needed))) {
|
|
|
|
/* yes - just set flags */
|
|
|
|
mbs->mbrb_flags |= ext13;
|
|
|
|
return;
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/* ok - we need to replace the bootcode */
|
|
|
|
|
|
|
|
if (f_flag && !(i_flag || B_flag)) {
|
|
|
|
warnx("Installed bootfile doesn't support required options.");
|
|
|
|
return;
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!f_flag && bootsize == 0)
|
|
|
|
/* Output an explanation for the 'update bootcode' prompt. */
|
|
|
|
printf("Installed bootfile doesn't support required options.\n");
|
1999-06-04 22:59:15 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/* Were we told a specific file ? (which we have already read) */
|
|
|
|
/* If so check that it supports what we need. */
|
|
|
|
if (bootsize != 0 && needed != 0
|
|
|
|
&& (le16toh(bootcode[0].mbr_bootsel.mbrb_magic) != MBR_MAGIC
|
|
|
|
|| ((bootcode[0].mbr_bootsel.mbrb_flags & needed) != needed))) {
|
|
|
|
/* No it doesn't... */
|
|
|
|
if (f_flag)
|
|
|
|
warnx("Bootfile %s doesn't support "
|
|
|
|
"required bootsel options", boot_path );
|
|
|
|
/* But install it anyway */
|
|
|
|
else
|
|
|
|
if (yesno("Bootfile %s doesn't support the required "
|
|
|
|
"options,\ninstall default bootfile instead?",
|
|
|
|
boot_path))
|
|
|
|
bootsize = 0;
|
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (bootsize == 0) {
|
|
|
|
/* Get name of bootfile that supports the required facilities */
|
|
|
|
code = DEFAULT_BOOTCODE;
|
|
|
|
if (needed & BFL_SELACTIVE)
|
|
|
|
code = DEFAULT_BOOTSELCODE;
|
|
|
|
#ifdef DEFAULT_BOOTEXTCODE
|
|
|
|
if (needed & BFL_EXTLBA)
|
|
|
|
code = DEFAULT_BOOTEXTCODE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bootsize = read_boot(code, bootcode, sizeof bootcode, 0);
|
|
|
|
if (bootsize == 0)
|
|
|
|
/* The old bootcode is better than no bootcode at all */
|
|
|
|
return;
|
|
|
|
if ((bootcode[0].mbr_bootsel.mbrb_flags & needed) != needed)
|
|
|
|
warnx("Default bootfile %s doesn't support required "
|
|
|
|
"options. Got flags 0x%x, wanted 0x%x\n",
|
|
|
|
boot_path, bootcode[0].mbr_bootsel.mbrb_flags,
|
|
|
|
needed);
|
1999-04-17 05:38:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!f_flag && !yesno("Update the bootcode from %s?", boot_path))
|
|
|
|
return;
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
init_sector0(0);
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (le16toh(mbs->mbrb_magic) == MBR_MAGIC)
|
|
|
|
mbs->mbrb_flags = bootcode[0].mbr_bootsel.mbrb_flags | ext13;
|
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
daddr_t
|
|
|
|
configure_bootsel(daddr_t default_ptn)
|
|
|
|
{
|
|
|
|
struct mbr_bootsel *mbs = &mboot.mbr_bootsel;
|
|
|
|
int i, item, opt;
|
|
|
|
int tmo;
|
|
|
|
daddr_t *off;
|
|
|
|
int num_bios_disks;
|
|
|
|
|
|
|
|
if (dl != NULL) {
|
|
|
|
num_bios_disks = dl->dl_nbiosdisks;
|
|
|
|
if (num_bios_disks > 8)
|
|
|
|
num_bios_disks = 8;
|
|
|
|
} else
|
|
|
|
num_bios_disks = 8;
|
|
|
|
|
|
|
|
printf("\nBoot selector configuration:\n");
|
|
|
|
|
|
|
|
/* The timeout value is in ticks, ~18.2 Hz. Avoid using floats.
|
|
|
|
* Ticks are nearly 64k/3600 - so our long timers are sligtly out!
|
|
|
|
* Newer bootcode always waits for 1 tick, so treats 0xffff
|
|
|
|
* as wait forever.
|
|
|
|
*/
|
|
|
|
tmo = le16toh(mbs->mbrb_timeo);
|
|
|
|
tmo = tmo == 0xffff ? -1 : (10 * tmo + 9) / 182;
|
|
|
|
tmo = decimal("Timeout value (0 to 3600 seconds, -1 => never)",
|
|
|
|
tmo, 0, -1, 3600);
|
|
|
|
mbs->mbrb_timeo = htole16(tmo == -1 ? 0xffff : (tmo * 182) / 10);
|
1999-04-17 05:38:00 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
off = calloc(1 + NMBRPART + ext.num_ptn + num_bios_disks, sizeof *off);
|
|
|
|
if (off == NULL)
|
|
|
|
err(1, "Malloc failed");
|
|
|
|
|
|
|
|
printf("Select the default boot option. Options are:\n\n");
|
|
|
|
item = 0;
|
|
|
|
opt = 0;
|
|
|
|
off[opt] = DEFAULT_ACTIVE;
|
|
|
|
printf("%d: The first active partition\n", opt);
|
1999-04-29 03:27:01 +04:00
|
|
|
for (i = 0; i < NMBRPART; i++) {
|
2003-04-30 23:47:10 +04:00
|
|
|
if (mboot.mbr_parts[i].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (mbs->mbrb_nametab[i][0] == 0)
|
|
|
|
continue;
|
|
|
|
printf("%d: %s\n", ++opt, &mbs->mbrb_nametab[i][0]);
|
|
|
|
off[opt] = le32toh(mboot.mbr_parts[i].mbrp_start);
|
|
|
|
if (off[opt] == default_ptn)
|
|
|
|
item = opt;
|
|
|
|
}
|
|
|
|
if (mbs->mbrb_flags & BFL_EXTLBA) {
|
|
|
|
for (i = 0; i < ext.num_ptn; i++) {
|
|
|
|
if (ext.ptn[i].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
if (ext.ptn[i].mbr_bootsel.mbrb_nametab[0][0] == 0)
|
|
|
|
continue;
|
|
|
|
printf("%d: %s\n",
|
|
|
|
++opt, ext.ptn[i].mbr_bootsel.mbrb_nametab[0]);
|
|
|
|
off[opt] = ext_offset(i) +
|
|
|
|
le32toh(ext.ptn[i].mbr_parts[0].mbrp_start);
|
|
|
|
if (off[opt] == default_ptn)
|
|
|
|
item = opt;
|
1999-04-29 03:27:01 +04:00
|
|
|
}
|
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
for (i = 0; i < num_bios_disks; i++) {
|
|
|
|
printf("%d: Harddisk %d\n", ++opt, i);
|
|
|
|
off[opt] = i;
|
|
|
|
if (i == default_ptn)
|
|
|
|
item = opt;
|
|
|
|
}
|
1999-04-29 03:27:01 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
item = decimal("Default boot option", item, 0, 0, opt);
|
|
|
|
|
|
|
|
default_ptn = off[item];
|
|
|
|
free(off);
|
|
|
|
return default_ptn;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
1999-04-17 05:38:00 +04:00
|
|
|
#endif
|
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1995-10-05 02:11:19 +03:00
|
|
|
/* Prerequisite: the disklabel parameters and master boot record must
|
|
|
|
* have been read (i.e. dos_* and mboot are meaningful).
|
|
|
|
* Specification: modifies dos_cylinders, dos_heads, dos_sectors, and
|
|
|
|
* dos_cylindersectors to be consistent with what the
|
|
|
|
* partition table is using, if we can find a geometry
|
|
|
|
* which is consistent with all partition table entries.
|
|
|
|
* We may get the number of cylinders slightly wrong (in
|
|
|
|
* the conservative direction). The idea is to be able
|
|
|
|
* to create a NetBSD partition on a disk we don't know
|
|
|
|
* the translated geometry of.
|
2003-04-30 23:47:10 +04:00
|
|
|
* This routine is only used for non-x86 systems or when we fail to
|
|
|
|
* get the BIOS geometry from the kernel.
|
|
|
|
*/
|
1995-10-05 02:11:19 +03:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
intuit_translated_geometry(void)
|
1995-10-05 02:11:19 +03:00
|
|
|
{
|
2000-12-19 19:01:28 +03:00
|
|
|
int xcylinders = -1, xheads = -1, xsectors = -1, i, j;
|
2003-04-30 23:47:10 +04:00
|
|
|
uint c1, h1, s1, c2, h2, s2;
|
|
|
|
ulong a1, a2;
|
|
|
|
uint64_t num, denom;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The physical parameters may be invalid as bios geometry.
|
|
|
|
* If we cannot determine the actual bios geometry, we are
|
|
|
|
* better off picking a likely 'faked' geometry than leaving
|
|
|
|
* the invalid physical one.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (dos_cylinders > MAXCYL || dos_heads > MAXHEAD ||
|
|
|
|
dos_sectors > MAXSECTOR) {
|
|
|
|
h1 = MAXHEAD - 1;
|
|
|
|
c1 = MAXCYL - 1;
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
if (dl != NULL) {
|
|
|
|
/* BIOS may use 256 heads or 1024 cylinders */
|
|
|
|
for (i = 0; i < dl->dl_nbiosdisks; i++) {
|
|
|
|
if (h1 < dl->dl_biosdisks[i].bi_head)
|
|
|
|
h1 = dl->dl_biosdisks[i].bi_head;
|
|
|
|
if (c1 < dl->dl_biosdisks[i].bi_cyl)
|
|
|
|
c1 = dl->dl_biosdisks[i].bi_cyl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
dos_sectors = MAXSECTOR;
|
|
|
|
dos_heads = h1;
|
|
|
|
dos_cylinders = disklabel.d_secperunit / (MAXSECTOR * h1);
|
|
|
|
if (dos_cylinders > c1)
|
|
|
|
dos_cylinders = c1;
|
|
|
|
}
|
1995-10-05 02:11:19 +03:00
|
|
|
|
|
|
|
/* Try to deduce the number of heads from two different mappings. */
|
1999-01-28 00:41:31 +03:00
|
|
|
for (i = 0; i < NMBRPART * 2; i++) {
|
1995-10-05 02:11:19 +03:00
|
|
|
if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
if (get_mapping(j, &c2, &h2, &s2, &a2) < 0)
|
|
|
|
continue;
|
2003-04-30 23:47:10 +04:00
|
|
|
a1 -= s1;
|
|
|
|
a2 -= s2;
|
|
|
|
num = (uint64_t)h1 * a2 - (uint64_t)h2 * a1;
|
|
|
|
denom = (uint64_t)c2 * a1 - (uint64_t)c1 * a2;
|
1995-10-05 02:11:19 +03:00
|
|
|
if (denom != 0 && num % denom == 0) {
|
2000-12-19 19:01:28 +03:00
|
|
|
xheads = num / denom;
|
2003-04-30 23:47:10 +04:00
|
|
|
xsectors = a1 / (c1 * xheads + h1);
|
1995-10-05 02:11:19 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-12-19 19:01:28 +03:00
|
|
|
if (xheads != -1)
|
1995-10-05 02:11:19 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2000-12-19 19:01:28 +03:00
|
|
|
if (xheads == -1)
|
1995-10-05 02:11:19 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Estimate the number of cylinders. */
|
2000-12-19 19:01:28 +03:00
|
|
|
xcylinders = disklabel.d_secperunit / xheads / xsectors;
|
1995-10-05 02:11:19 +03:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/*
|
|
|
|
* Now verify consistency with each of the partition table entries.
|
1995-10-05 02:11:19 +03:00
|
|
|
* Be willing to shove cylinders up a little bit to make things work,
|
2003-04-30 23:47:10 +04:00
|
|
|
* but translation mismatches are fatal.
|
|
|
|
*/
|
1999-01-28 00:41:31 +03:00
|
|
|
for (i = 0; i < NMBRPART * 2; i++) {
|
1995-10-05 02:11:19 +03:00
|
|
|
if (get_mapping(i, &c1, &h1, &s1, &a1) < 0)
|
|
|
|
continue;
|
2000-12-19 19:01:28 +03:00
|
|
|
if (xsectors * (c1 * xheads + h1) + s1 != a1)
|
1995-10-05 02:11:19 +03:00
|
|
|
return;
|
2000-12-19 19:01:28 +03:00
|
|
|
if (c1 >= xcylinders)
|
|
|
|
xcylinders = c1 + 1;
|
1995-10-05 02:11:19 +03:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
|
|
|
|
/* Everything checks out.
|
|
|
|
* Reset the geometry to use for further calculations.
|
|
|
|
* But cylinders cannot be > 1024, we set the max at the top.
|
|
|
|
*/
|
|
|
|
if (dos_cylinders > xcylinders)
|
|
|
|
dos_cylinders = xcylinders;
|
2000-12-19 19:01:28 +03:00
|
|
|
dos_heads = xheads;
|
|
|
|
dos_sectors = xsectors;
|
1995-10-05 02:11:19 +03:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/*
|
|
|
|
* For the purposes of intuit_translated_geometry(), treat the partition
|
1995-10-05 02:11:19 +03:00
|
|
|
* table as a list of eight mapping between (cylinder, head, sector)
|
|
|
|
* triplets and absolute sectors. Get the relevant geometry triplet and
|
|
|
|
* absolute sectors for a given entry, or return -1 if it isn't present.
|
2003-04-30 23:47:10 +04:00
|
|
|
* Note: for simplicity, the returned sector is 0-based.
|
|
|
|
*/
|
1995-10-05 02:11:19 +03:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
get_mapping(int i, uint *cylinder, uint *head, uint *sector,
|
2000-12-19 18:44:27 +03:00
|
|
|
unsigned long *absolute)
|
1995-10-05 02:11:19 +03:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
mbr_partition_t *part = &mboot.mbr_parts[i / 2];
|
1995-10-05 02:11:19 +03:00
|
|
|
|
1999-01-28 00:41:31 +03:00
|
|
|
if (part->mbrp_typ == 0)
|
1995-10-05 02:11:19 +03:00
|
|
|
return -1;
|
|
|
|
if (i % 2 == 0) {
|
1999-01-28 00:41:31 +03:00
|
|
|
*cylinder = MBR_PCYL(part->mbrp_scyl, part->mbrp_ssect);
|
|
|
|
*head = part->mbrp_shd;
|
|
|
|
*sector = MBR_PSECT(part->mbrp_ssect) - 1;
|
2003-04-30 23:47:10 +04:00
|
|
|
*absolute = le32toh(part->mbrp_start);
|
1995-10-05 02:11:19 +03:00
|
|
|
} else {
|
1999-01-28 00:41:31 +03:00
|
|
|
*cylinder = MBR_PCYL(part->mbrp_ecyl, part->mbrp_esect);
|
|
|
|
*head = part->mbrp_ehd;
|
|
|
|
*sector = MBR_PSECT(part->mbrp_esect) - 1;
|
2003-04-30 23:47:10 +04:00
|
|
|
*absolute = le32toh(part->mbrp_start)
|
|
|
|
+ le32toh(part->mbrp_size) - 1;
|
1995-10-05 02:11:19 +03:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
/* Sanity check the data against max values */
|
|
|
|
if ((((*cylinder * MAXHEAD) + *head) * MAXSECTOR + *sector) < *absolute)
|
|
|
|
/* cannot be a CHS mapping */
|
|
|
|
return -1;
|
1995-10-05 02:11:19 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
static void
|
|
|
|
delete_ptn(int part)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
if (part == ext.ptn_id) {
|
|
|
|
/* forget all about the extended partition */
|
|
|
|
free(ext.ptn);
|
|
|
|
memset(&ext, 0, sizeof ext);
|
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
mboot.mbr_parts[part].mbrp_typ = 0;
|
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
static void
|
|
|
|
delete_ext_ptn(int part)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (part == 0) {
|
|
|
|
ext.ptn[0].mbr_parts[0].mbrp_typ = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ext.ptn[part - 1].mbr_parts[1] = ext.ptn[part].mbr_parts[1];
|
|
|
|
memmove(&ext.ptn[part], &ext.ptn[part + 1],
|
|
|
|
(ext.num_ptn - part - 1) * sizeof ext.ptn[0]);
|
|
|
|
ext.num_ptn--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_ext_ptn(daddr_t start, daddr_t size)
|
|
|
|
{
|
|
|
|
int part;
|
|
|
|
mbr_partition_t *partp;
|
|
|
|
|
|
|
|
ext.ptn = realloc(ext.ptn, (ext.num_ptn + 1) * sizeof *ext.ptn);
|
|
|
|
for (part = 0; part < ext.num_ptn; part++)
|
|
|
|
if (ext_offset(part) > start)
|
|
|
|
break;
|
|
|
|
/* insert before 'part' - make space... */
|
|
|
|
memmove(&ext.ptn[part + 1], &ext.ptn[part],
|
|
|
|
(ext.num_ptn - part) * sizeof ext.ptn[0]);
|
|
|
|
memset(&ext.ptn[part], 0, sizeof ext.ptn[0]);
|
|
|
|
ext.ptn[part].mbr_signature = htole16(MBR_MAGIC);
|
|
|
|
/* we will be 'part' */
|
|
|
|
if (part == 0) {
|
|
|
|
/* link us to 'next' */
|
|
|
|
partp = &ext.ptn[0].mbr_parts[1];
|
|
|
|
/* offset will be fixed by caller */
|
|
|
|
partp->mbrp_size = htole32(
|
|
|
|
le32toh(ext.ptn[1].mbr_parts[0].mbrp_start) +
|
|
|
|
le32toh(ext.ptn[1].mbr_parts[0].mbrp_size));
|
|
|
|
} else {
|
|
|
|
/* link us to prev's next */
|
|
|
|
partp = &ext.ptn[part - 1].mbr_parts[1];
|
|
|
|
ext.ptn[part].mbr_parts[1] = *partp;
|
|
|
|
/* and prev onto us */
|
|
|
|
partp->mbrp_start = htole32(start - dos_sectors - ext.base);
|
|
|
|
partp->mbrp_size = htole32(size + dos_sectors);
|
|
|
|
}
|
|
|
|
partp->mbrp_typ = 5; /* as used by win98 */
|
|
|
|
partp->mbrp_flag = 0;
|
|
|
|
/* wallop in some CHS values - win98 doesn't saturate them */
|
|
|
|
dos(le32toh(partp->mbrp_start),
|
|
|
|
&partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
|
|
|
|
dos(le32toh(partp->mbrp_start) + le32toh(partp->mbrp_size) - 1,
|
|
|
|
&partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
|
|
|
|
ext.num_ptn++;
|
|
|
|
|
|
|
|
return part;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
check_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
|
|
|
|
{
|
|
|
|
int p;
|
|
|
|
uint p_s, p_e;
|
|
|
|
|
|
|
|
if (sysid != 0) {
|
|
|
|
if (start < dos_sectors)
|
|
|
|
return "Track zero is reserved for the BIOS";
|
|
|
|
if (start + size > dos_disksectors)
|
|
|
|
return "Partition exceeds size of disk";
|
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
if (p == part || mboot.mbr_parts[p].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
p_s = le32toh(mboot.mbr_parts[p].mbrp_start);
|
|
|
|
p_e = p_s + le32toh(mboot.mbr_parts[p].mbrp_size);
|
|
|
|
if (start + size <= p_s || start >= p_e)
|
|
|
|
continue;
|
|
|
|
if (f_flag) {
|
|
|
|
if (fix)
|
|
|
|
delete_ptn(p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return "Overlaps another partition";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we trying to create an extended partition */
|
|
|
|
if (!MBR_IS_EXTENDED(mboot.mbr_parts[part].mbrp_typ)) {
|
|
|
|
/* this wasn't the extended partition */
|
|
|
|
if (!MBR_IS_EXTENDED(sysid))
|
|
|
|
return 0;
|
|
|
|
/* making an extended partition */
|
|
|
|
if (ext.base != 0) {
|
|
|
|
if (!f_flag)
|
|
|
|
return "There cannot be 2 extended partitions";
|
|
|
|
if (fix)
|
|
|
|
delete_ptn(ext.ptn_id);
|
|
|
|
}
|
|
|
|
if (fix) {
|
|
|
|
/* allocate a new extended partition */
|
|
|
|
ext.ptn = calloc(1, sizeof ext.ptn[0]);
|
|
|
|
if (ext.ptn == NULL)
|
|
|
|
err(1, "Malloc failed");
|
|
|
|
ext.ptn[0].mbr_signature = htole16(MBR_MAGIC);
|
|
|
|
ext.ptn_id = part;
|
|
|
|
ext.base = start;
|
|
|
|
ext.limit = start + size;
|
|
|
|
ext.num_ptn = 1;
|
1998-10-02 21:23:22 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check we haven't cut space allocated to an extended ptn */
|
|
|
|
|
|
|
|
if (!MBR_IS_EXTENDED(sysid)) {
|
|
|
|
/* no longer an extended partition */
|
|
|
|
if (fix) {
|
|
|
|
/* Kill all memory of the extended partitions */
|
|
|
|
delete_ptn(part);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ext.num_ptn == 0 ||
|
|
|
|
(ext.num_ptn == 1 && ext.ptn[0].mbr_parts[0].mbrp_typ == 0))
|
|
|
|
/* nothing in extended partition */
|
|
|
|
return 0;
|
1997-08-12 03:31:41 +04:00
|
|
|
if (f_flag)
|
2003-04-30 23:47:10 +04:00
|
|
|
return 0;
|
|
|
|
if (yesno("Do you really want to delete all the extended partitions?"))
|
|
|
|
return 0;
|
|
|
|
return "Extended partition busy";
|
1997-08-12 03:31:41 +04:00
|
|
|
}
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (le32toh(mboot.mbr_parts[part].mbrp_start) != ext.base)
|
|
|
|
/* maybe impossible, but an extra sanity check */
|
|
|
|
return 0;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
for (p = ext.num_ptn; --p >= 0;) {
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
p_s = ext_offset(p);
|
|
|
|
p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
|
|
|
|
if (p_s >= start && p_e <= start + size)
|
|
|
|
continue;
|
|
|
|
if (!f_flag)
|
|
|
|
return "Extended partition outside main partition";
|
|
|
|
if (fix)
|
|
|
|
delete_ext_ptn(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fix && start != ext.base) {
|
|
|
|
/* The internal offsets need to be fixed up */
|
|
|
|
for (p = 0; p < ext.num_ptn - 1; p++)
|
|
|
|
ext.ptn[p].mbr_parts[1].mbrp_start = htole32(
|
|
|
|
le32toh(ext.ptn[p].mbr_parts[1].mbrp_start)
|
|
|
|
+ ext.base - start);
|
|
|
|
/* and maybe an empty partition at the start */
|
|
|
|
if (ext.ptn[0].mbr_parts[0].mbrp_typ == 0) {
|
|
|
|
if (le32toh(ext.ptn[0].mbr_parts[1].mbrp_start) == 0) {
|
|
|
|
/* don't need the empty slot */
|
|
|
|
memmove(&ext.ptn[0], &ext.ptn[1],
|
|
|
|
(ext.num_ptn - 1) * sizeof ext.ptn[0]);
|
|
|
|
ext.num_ptn--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* must create an empty slot */
|
|
|
|
add_ext_ptn(start, dos_sectors);
|
|
|
|
ext.ptn[0].mbr_parts[1].mbrp_start = htole32(ext.base
|
|
|
|
- start);
|
1994-09-23 09:30:09 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
}
|
|
|
|
if (fix) {
|
|
|
|
ext.base = start;
|
|
|
|
ext.limit = start + size;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
static const char *
|
|
|
|
check_ext_overlap(int part, int sysid, daddr_t start, daddr_t size, int fix)
|
|
|
|
{
|
|
|
|
int p;
|
|
|
|
uint p_s, p_e;
|
|
|
|
|
|
|
|
if (sysid == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (MBR_IS_EXTENDED(sysid))
|
|
|
|
return "Nested extended partitions are not allowed";
|
|
|
|
|
|
|
|
/* allow one track at start for extended partition header */
|
|
|
|
start -= dos_sectors;
|
|
|
|
size += dos_sectors;
|
|
|
|
if (start < ext.base || start + size > ext.limit)
|
|
|
|
return "Outside bounds of extended partition";
|
|
|
|
|
|
|
|
if (f_flag && !fix)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (p = ext.num_ptn; --p >= 0;) {
|
|
|
|
if (p == part || ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
p_s = ext_offset(p);
|
|
|
|
p_e = p_s + le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
|
|
|
|
if (p == 0)
|
|
|
|
p_s += le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
|
|
|
|
- dos_sectors;
|
|
|
|
if (start < p_e && start + size > p_s) {
|
|
|
|
if (!f_flag)
|
|
|
|
return "Overlaps another extended partition";
|
|
|
|
if (fix) {
|
|
|
|
if (part == -1)
|
|
|
|
delete_ext_ptn(p);
|
|
|
|
else
|
|
|
|
/* must not change numbering yet */
|
|
|
|
ext.ptn[p].mbr_parts[0].mbrp_typ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1994-09-23 09:30:09 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
int
|
|
|
|
change_part(int extended, int part, int sysid, daddr_t start, daddr_t size,
|
|
|
|
char *bootmenu)
|
|
|
|
{
|
|
|
|
mbr_partition_t *partp;
|
|
|
|
mbr_sector_t *boot;
|
|
|
|
daddr_t offset;
|
|
|
|
char *e;
|
|
|
|
int upart = part;
|
|
|
|
int p;
|
|
|
|
int fl;
|
|
|
|
daddr_t n_s, n_e;
|
|
|
|
const char *errtext;
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
char tmp_bootmenu[NMBRPART * (PARTNAMESIZE + 1)];
|
|
|
|
int bootmenu_len = (extended ? NMBRPART : 1) * (PARTNAMESIZE + 1);
|
1998-09-28 19:44:18 +04:00
|
|
|
#endif
|
2003-04-30 23:47:10 +04:00
|
|
|
|
|
|
|
if (extended) {
|
|
|
|
if (part != -1 && part < ext.num_ptn) {
|
|
|
|
boot = &ext.ptn[part];
|
|
|
|
partp = &boot->mbr_parts[0];
|
|
|
|
offset = ext_offset(part);
|
1993-04-09 23:23:56 +04:00
|
|
|
} else {
|
2003-04-30 23:47:10 +04:00
|
|
|
part = -1;
|
|
|
|
boot = 0;
|
|
|
|
partp = 0;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
upart = 0;
|
|
|
|
e = "E";
|
|
|
|
} else {
|
|
|
|
boot = &mboot;
|
|
|
|
partp = &boot->mbr_parts[part];
|
|
|
|
offset = 0;
|
|
|
|
e = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!f_flag && part != -1) {
|
|
|
|
printf("The data for partition %s%d is:\n", e, part);
|
|
|
|
print_part(boot, upart, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (bootmenu != NULL)
|
|
|
|
strlcpy(tmp_bootmenu, bootmenu, bootmenu_len);
|
|
|
|
else
|
|
|
|
if (boot != NULL &&
|
|
|
|
le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC)
|
|
|
|
strlcpy(tmp_bootmenu,
|
|
|
|
boot->mbr_bootsel.mbrb_nametab[upart],
|
|
|
|
bootmenu_len);
|
|
|
|
else
|
|
|
|
tmp_bootmenu[0] = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!s_flag && partp != NULL) {
|
|
|
|
/* values not specified, default to current ones */
|
|
|
|
sysid = partp->mbrp_typ;
|
|
|
|
start = offset + le32toh(partp->mbrp_start);
|
|
|
|
size = le32toh(partp->mbrp_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* creating a new partition, default to free space */
|
|
|
|
if (!s_flag && sysid == 0 && extended) {
|
|
|
|
/* non-extended partition */
|
|
|
|
start = ext.base;
|
|
|
|
for (p = 0; p < ext.num_ptn; p++) {
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
n_s = ext_offset(p);
|
|
|
|
if (n_s > start + dos_sectors)
|
|
|
|
break;
|
|
|
|
start = ext_offset(p)
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size);
|
|
|
|
}
|
|
|
|
if (ext.limit - start <= dos_sectors) {
|
|
|
|
printf("No space in extended partition\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
start += dos_sectors;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s_flag && sysid == 0 && !extended) {
|
|
|
|
/* same for non-extended partition */
|
|
|
|
/* first see if old start is free */
|
|
|
|
if (start < dos_sectors)
|
|
|
|
start = 0;
|
|
|
|
for (p = 0; start != 0 && p < NMBRPART; p++) {
|
|
|
|
if (mboot.mbr_parts[p].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
|
|
|
|
if (start >= n_s &&
|
|
|
|
start < n_s + le32toh(mboot.mbr_parts[p].mbrp_size))
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (start == 0) {
|
|
|
|
/* Look for first gap */
|
|
|
|
start = dos_sectors;
|
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
if (mboot.mbr_parts[p].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
|
|
|
|
n_e = n_s + le32toh(mboot.mbr_parts[p].mbrp_size);
|
|
|
|
if (start >= n_s && start < n_e) {
|
|
|
|
start = n_e;
|
|
|
|
p = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start >= dos_disksectors) {
|
|
|
|
printf("No free space\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-09-28 19:44:18 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!f_flag) {
|
|
|
|
/* request new values from user */
|
|
|
|
if (sysid == 0)
|
|
|
|
sysid = 169;
|
|
|
|
sysid = decimal("sysid", sysid, 0, 0, 255);
|
|
|
|
if (sysid == 0 && !v_flag) {
|
|
|
|
start = 0;
|
|
|
|
size = 0;
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
tmp_bootmenu[0] = 0;
|
1998-09-28 19:44:18 +04:00
|
|
|
#endif
|
2003-04-30 23:47:10 +04:00
|
|
|
} else {
|
|
|
|
daddr_t old = start;
|
|
|
|
daddr_t lim = extended ? ext.limit : dos_disksectors;
|
|
|
|
start = decimal("start", start,
|
|
|
|
DEC_SEC | DEC_RND_0 | (extended ? DEC_RND : 0),
|
|
|
|
extended ? ext.base : 0, lim);
|
|
|
|
/* Adjust 'size' so that end doesn't move when 'start'
|
|
|
|
* is only changed slightly.
|
|
|
|
*/
|
|
|
|
if (size > start - old)
|
|
|
|
size -= start - old;
|
|
|
|
else
|
|
|
|
size = 0;
|
|
|
|
/* Find end of available space from this start point */
|
|
|
|
if (extended) {
|
|
|
|
for (p = 0; p < ext.num_ptn; p++) {
|
|
|
|
if (p == part)
|
|
|
|
continue;
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
n_s = ext_offset(p);
|
|
|
|
if (n_s > start && n_s < lim)
|
|
|
|
lim = n_s;
|
|
|
|
if (start >= n_s && start < n_s
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_start)
|
|
|
|
+ le32toh(ext.ptn[p].mbr_parts[0].mbrp_size)) {
|
|
|
|
lim = start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (p = 0; p < NMBRPART; p++) {
|
|
|
|
if (p == part)
|
|
|
|
continue;
|
|
|
|
if (mboot.mbr_parts[p].mbrp_typ == 0)
|
|
|
|
continue;
|
|
|
|
n_s = le32toh(mboot.mbr_parts[p].mbrp_start);
|
|
|
|
if (n_s > start && n_s < lim)
|
|
|
|
lim = n_s;
|
|
|
|
if (start >= n_s && start < n_s
|
|
|
|
+ le32toh(mboot.mbr_parts[p].mbrp_size)) {
|
|
|
|
lim = start;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lim -= start;
|
|
|
|
if (lim == 0) {
|
|
|
|
printf("Start sector already allocated\n");
|
|
|
|
return 0;
|
1998-10-02 21:23:22 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
if (size == 0 || size > lim)
|
|
|
|
size = lim;
|
|
|
|
fl = DEC_SEC;
|
|
|
|
if (start % dos_cylindersectors == dos_sectors)
|
|
|
|
fl |= DEC_RND_DOWN;
|
|
|
|
printf("start %lld, cps %d\n",
|
|
|
|
start, dos_cylindersectors);
|
|
|
|
if (start == 2 * dos_sectors)
|
|
|
|
fl |= DEC_RND_DOWN | DEC_RND_DOWN_2;
|
|
|
|
size = decimal("size", size, fl, 0, lim);
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
#ifndef DEFAULT_BOOTEXTCODE
|
|
|
|
if (!extended)
|
|
|
|
#endif
|
|
|
|
string("bootmenu", bootmenu_len, tmp_bootmenu);
|
|
|
|
#endif
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before we write these away, we must verify that nothing
|
|
|
|
* untoward has been requested.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (extended)
|
|
|
|
errtext = check_ext_overlap(part, sysid, start, size, 0);
|
|
|
|
else
|
|
|
|
errtext = check_overlap(part, sysid, start, size, 0);
|
|
|
|
if (errtext != NULL) {
|
|
|
|
if (f_flag)
|
|
|
|
errx(2, "%s\n", errtext);
|
|
|
|
printf("%s\n", errtext);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Before proceeding, delete any overlapped partitions.
|
|
|
|
* This can only happen if '-f' was supplied on the command line.
|
|
|
|
* Just hope the caller knows what they are doing.
|
|
|
|
* This also fixes the base of each extended partition if the
|
|
|
|
* partition itself has moved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (extended)
|
|
|
|
errtext = check_ext_overlap(part, sysid, start, size, 1);
|
|
|
|
else
|
|
|
|
errtext = check_overlap(part, sysid, start, size, 1);
|
|
|
|
|
|
|
|
if (errtext)
|
|
|
|
errx(1, "%s\n", errtext);
|
|
|
|
|
|
|
|
if (sysid == 0) {
|
|
|
|
/* delete this partition - save info though */
|
|
|
|
if (partp == NULL)
|
|
|
|
/* must have been trying to create an extended ptn */
|
|
|
|
return 0;
|
|
|
|
if (start == 0 && size == 0)
|
|
|
|
memset(partp, 0, sizeof *partp);
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC)
|
|
|
|
memset(boot->mbr_bootsel.mbrb_nametab[upart], 0,
|
|
|
|
sizeof boot->mbr_bootsel.mbrb_nametab[0]);
|
|
|
|
#endif
|
|
|
|
if (extended)
|
|
|
|
delete_ext_ptn(part);
|
|
|
|
else
|
|
|
|
delete_ptn(part);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (extended) {
|
|
|
|
if (part != -1)
|
|
|
|
delete_ext_ptn(part);
|
|
|
|
if (start == ext.base + dos_sectors)
|
|
|
|
/* First one must have been free */
|
|
|
|
part = 0;
|
|
|
|
else
|
|
|
|
part = add_ext_ptn(start, size);
|
|
|
|
|
|
|
|
/* These must be re-calculated because of the realloc */
|
|
|
|
boot = &ext.ptn[part];
|
|
|
|
partp = &boot->mbr_parts[0];
|
|
|
|
offset = ext_offset(part);
|
|
|
|
}
|
|
|
|
|
|
|
|
partp->mbrp_typ = sysid;
|
|
|
|
partp->mbrp_start = htole32( start - offset);
|
|
|
|
partp->mbrp_size = htole32( size);
|
|
|
|
dos(start, &partp->mbrp_scyl, &partp->mbrp_shd, &partp->mbrp_ssect);
|
|
|
|
dos(start + size - 1,
|
|
|
|
&partp->mbrp_ecyl, &partp->mbrp_ehd, &partp->mbrp_esect);
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
if (extended) {
|
|
|
|
boot->mbr_bootsel.mbrb_magic = htole16(MBR_MAGIC);
|
|
|
|
strncpy(boot->mbr_bootsel.mbrb_nametab[upart], tmp_bootmenu,
|
|
|
|
bootmenu_len);
|
|
|
|
} else {
|
|
|
|
/* We need to bootselect code installed in order to have
|
|
|
|
* somewhere to safely write the menu tag.
|
|
|
|
*/
|
|
|
|
if (le16toh(boot->mbr_bootsel.mbrb_magic) != MBR_MAGIC) {
|
|
|
|
if (yesno("The bootselect code is not installed, "
|
|
|
|
"do you want to install it now?"))
|
|
|
|
install_bootsel(BFL_SELACTIVE);
|
|
|
|
}
|
|
|
|
if (le16toh(boot->mbr_bootsel.mbrb_magic) == MBR_MAGIC) {
|
|
|
|
strncpy(boot->mbr_bootsel.mbrb_nametab[upart],
|
|
|
|
tmp_bootmenu, bootmenu_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (v_flag && !f_flag && yesno("Explicitly specify beg/end address?")) {
|
|
|
|
/* this really isn't a good idea.... */
|
|
|
|
int tsector, tcylinder, thead;
|
|
|
|
|
|
|
|
tcylinder = MBR_PCYL(partp->mbrp_scyl, partp->mbrp_ssect);
|
|
|
|
thead = partp->mbrp_shd;
|
|
|
|
tsector = MBR_PSECT(partp->mbrp_ssect);
|
|
|
|
tcylinder = decimal("beginning cylinder",
|
|
|
|
tcylinder, 0, 0, dos_cylinders - 1);
|
|
|
|
thead = decimal("beginning head",
|
|
|
|
thead, 0, 0, dos_heads - 1);
|
|
|
|
tsector = decimal("beginning sector",
|
|
|
|
tsector, 0, 1, dos_sectors);
|
|
|
|
partp->mbrp_scyl = DOSCYL(tcylinder);
|
|
|
|
partp->mbrp_shd = thead;
|
|
|
|
partp->mbrp_ssect = DOSSECT(tsector, tcylinder);
|
|
|
|
|
|
|
|
tcylinder = MBR_PCYL(partp->mbrp_ecyl, partp->mbrp_esect);
|
|
|
|
thead = partp->mbrp_ehd;
|
|
|
|
tsector = MBR_PSECT(partp->mbrp_esect);
|
|
|
|
tcylinder = decimal("ending cylinder",
|
|
|
|
tcylinder, 0, 0, dos_cylinders - 1);
|
|
|
|
thead = decimal("ending head",
|
|
|
|
thead, 0, 0, dos_heads - 1);
|
|
|
|
tsector = decimal("ending sector",
|
|
|
|
tsector, 0, 1, dos_sectors);
|
|
|
|
partp->mbrp_ecyl = DOSCYL(tcylinder);
|
|
|
|
partp->mbrp_ehd = thead;
|
|
|
|
partp->mbrp_esect = DOSSECT(tsector, tcylinder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we had to mark an extended partition as deleted because
|
|
|
|
* another request would have overlapped it, now is the time
|
|
|
|
* to do the actual delete.
|
|
|
|
*/
|
|
|
|
if (extended && f_flag) {
|
|
|
|
for (p = ext.num_ptn; --p >= 0;)
|
|
|
|
if (ext.ptn[p].mbr_parts[0].mbrp_typ == 0)
|
|
|
|
delete_ext_ptn(p);
|
|
|
|
}
|
|
|
|
return 1;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
print_params(void)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
|
1997-08-12 03:31:41 +04:00
|
|
|
if (sh_flag) {
|
2003-04-30 23:47:10 +04:00
|
|
|
printf("DLCYL=%d\nDLHEAD=%d\nDLSEC=%d\nDLSIZE=%llu\n",
|
|
|
|
cylinders, heads, sectors, (uintmax_t)disksectors);
|
|
|
|
printf("BCYL=%d\nBHEAD=%d\nBSEC=%d\nBDLSIZE=%llu\n",
|
|
|
|
dos_cylinders, dos_heads, dos_sectors,
|
|
|
|
(uintmax_t)dos_disksectors);
|
|
|
|
printf("NUMEXTPTN=%d\n", ext.num_ptn);
|
1997-07-29 12:31:31 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-08-12 03:31:41 +04:00
|
|
|
/* Not sh_flag */
|
2001-11-07 17:50:32 +03:00
|
|
|
printf("Disk: %s\n", disk);
|
1999-04-17 05:38:00 +04:00
|
|
|
printf("NetBSD disklabel disk geometry:\n");
|
2003-04-30 23:47:10 +04:00
|
|
|
printf("cylinders: %d, heads: %d, sectors/track: %d "
|
|
|
|
"(%d sectors/cylinder)\ntotal sectors: %llu\n\n",
|
|
|
|
cylinders, heads, sectors, cylindersectors,
|
|
|
|
(uintmax_t)disksectors);
|
1999-04-17 05:38:00 +04:00
|
|
|
printf("BIOS disk geometry:\n");
|
2003-04-30 23:47:10 +04:00
|
|
|
printf("cylinders: %d, heads: %d, sectors/track: %d "
|
|
|
|
"(%d sectors/cylinder)\ntotal sectors: %llu\n\n",
|
|
|
|
dos_cylinders, dos_heads, dos_sectors, dos_cylindersectors,
|
|
|
|
(uintmax_t)dos_disksectors);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
change_active(int which)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
mbr_partition_t *partp;
|
1994-09-23 08:30:13 +04:00
|
|
|
int part;
|
1997-08-12 03:31:41 +04:00
|
|
|
int active = 4;
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 14:29:51 +04:00
|
|
|
partp = &mboot.mbr_parts[0];
|
1993-04-09 23:23:56 +04:00
|
|
|
|
|
|
|
if (a_flag && which != -1)
|
|
|
|
active = which;
|
1994-09-23 08:30:13 +04:00
|
|
|
else {
|
1999-01-28 00:41:31 +03:00
|
|
|
for (part = 0; part < NMBRPART; part++)
|
2003-04-30 23:47:10 +04:00
|
|
|
if (partp[part].mbrp_flag & MBR_FLAGS_ACTIVE)
|
1994-09-23 08:30:13 +04:00
|
|
|
active = part;
|
|
|
|
}
|
1997-08-12 03:31:41 +04:00
|
|
|
if (!f_flag) {
|
|
|
|
if (yesno("Do you want to change the active partition?")) {
|
|
|
|
printf ("Choosing 4 will make no partition active.\n");
|
|
|
|
do {
|
2003-04-30 23:47:10 +04:00
|
|
|
active = decimal("active partition",
|
|
|
|
active, 0, 0, 4);
|
1997-08-12 03:31:41 +04:00
|
|
|
} while (!yesno("Are you happy with this choice?"));
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
if (active != 4)
|
|
|
|
printf ("Making partition %d active.\n", active);
|
|
|
|
|
1999-01-28 00:41:31 +03:00
|
|
|
for (part = 0; part < NMBRPART; part++)
|
2003-04-30 23:47:10 +04:00
|
|
|
partp[part].mbrp_flag &= ~MBR_FLAGS_ACTIVE;
|
1997-08-12 03:31:41 +04:00
|
|
|
if (active < 4)
|
2003-04-30 23:47:10 +04:00
|
|
|
partp[active].mbrp_flag |= MBR_FLAGS_ACTIVE;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
get_params_to_use(void)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
2003-04-30 23:47:10 +04:00
|
|
|
struct biosdisk_info *bip;
|
|
|
|
int i;
|
2000-12-19 18:44:27 +03:00
|
|
|
|
1997-09-06 01:26:57 +04:00
|
|
|
if (b_flag) {
|
|
|
|
dos_cylinders = b_cyl;
|
|
|
|
dos_heads = b_head;
|
|
|
|
dos_sectors = b_sec;
|
|
|
|
return;
|
|
|
|
}
|
1994-09-23 08:30:13 +04:00
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
print_params();
|
2003-04-30 23:47:10 +04:00
|
|
|
if (!yesno("Do you want to change our idea of what BIOS thinks?"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
if (dl != NULL) {
|
|
|
|
for (i = 0; i < dl->dl_nbiosdisks; i++) {
|
|
|
|
if (i == 0)
|
|
|
|
printf("\nGeometries of known disks:\n");
|
|
|
|
bip = &dl->dl_biosdisks[i];
|
|
|
|
printf("Disk %d: cylinders %u, heads %u, sectors %u"
|
|
|
|
" (%lld sectors, %dMB)\n",
|
|
|
|
i, bip->bi_cyl, bip->bi_head, bip->bi_sec,
|
|
|
|
(uintmax_t)bip->bi_lbasecs,
|
|
|
|
SEC_TO_MB(bip->bi_lbasecs));
|
|
|
|
|
|
|
|
}
|
|
|
|
printf("\n");
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
#endif
|
|
|
|
do {
|
|
|
|
dos_cylinders = decimal("BIOS's idea of #cylinders",
|
|
|
|
dos_cylinders, 0, 0, MAXCYL);
|
|
|
|
dos_heads = decimal("BIOS's idea of #heads",
|
|
|
|
dos_heads, 0, 0, MAXHEAD);
|
|
|
|
dos_sectors = decimal("BIOS's idea of #sectors",
|
|
|
|
dos_sectors, 0, 1, MAXSECTOR);
|
|
|
|
print_params();
|
|
|
|
} while (!yesno("Are you happy with this choice?"));
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
/***********************************************\
|
|
|
|
* Change real numbers into strange dos numbers *
|
|
|
|
\***********************************************/
|
1994-09-23 08:30:13 +04:00
|
|
|
void
|
2000-12-19 18:44:27 +03:00
|
|
|
dos(int sector, unsigned char *cylinderp, unsigned char *headp,
|
|
|
|
unsigned char *sectorp)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 09:30:09 +04:00
|
|
|
int cylinder, head;
|
2000-05-27 23:02:54 +04:00
|
|
|
int biosmaxsec;
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
biosmaxsec = dos_totalsectors - 1;
|
2000-05-27 23:02:54 +04:00
|
|
|
if (sector > biosmaxsec)
|
|
|
|
sector = biosmaxsec;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 09:30:09 +04:00
|
|
|
cylinder = sector / dos_cylindersectors;
|
1998-09-28 19:44:18 +04:00
|
|
|
|
1994-09-23 09:30:09 +04:00
|
|
|
sector -= cylinder * dos_cylindersectors;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 09:30:09 +04:00
|
|
|
head = sector / dos_sectors;
|
|
|
|
sector -= head * dos_sectors;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 09:30:09 +04:00
|
|
|
*cylinderp = DOSCYL(cylinder);
|
|
|
|
*headp = head;
|
|
|
|
*sectorp = DOSSECT(sector + 1, cylinder);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2000-12-19 19:01:28 +03:00
|
|
|
open_disk(int update)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1997-09-14 17:52:26 +04:00
|
|
|
static char namebuf[MAXPATHLEN + 1];
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
fd = opendisk(disk, update && disk_file == NULL ? O_RDWR : O_RDONLY,
|
|
|
|
namebuf, sizeof(namebuf), 0);
|
1997-09-14 17:52:26 +04:00
|
|
|
if (fd < 0) {
|
2001-11-01 10:04:18 +03:00
|
|
|
if (errno == ENODEV)
|
|
|
|
warnx("%s is not a character device", namebuf);
|
|
|
|
else
|
|
|
|
warn("%s", namebuf);
|
1994-09-23 08:30:13 +04:00
|
|
|
return (-1);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
1997-09-14 17:52:26 +04:00
|
|
|
disk = namebuf;
|
1994-09-23 08:30:13 +04:00
|
|
|
if (get_params() == -1) {
|
|
|
|
close(fd);
|
2003-04-30 23:47:10 +04:00
|
|
|
fd = -1;
|
1994-09-23 08:30:13 +04:00
|
|
|
return (-1);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
if (disk_file != NULL) {
|
|
|
|
/* for testing: read/write data from a disk file */
|
|
|
|
wfd = open(disk_file, update ? O_RDWR|O_CREAT : O_RDONLY, 0777);
|
|
|
|
if (wfd == -1) {
|
|
|
|
warn("%s", disk_file);
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
wfd = fd;
|
1994-09-23 08:30:13 +04:00
|
|
|
return (0);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
read_disk(daddr_t sector, void *buf)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (*rfd == -1)
|
2000-12-19 19:01:28 +03:00
|
|
|
errx(1, "read_disk(); fd == -1");
|
2003-04-30 23:47:10 +04:00
|
|
|
if (lseek(*rfd, sector * (off_t)512, 0) == -1)
|
1994-09-23 08:30:13 +04:00
|
|
|
return (-1);
|
2003-04-30 23:47:10 +04:00
|
|
|
return (read(*rfd, buf, 512));
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
write_disk(daddr_t sector, void *buf)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (wfd == -1)
|
|
|
|
errx(1, "write_disk(); wfd == -1");
|
|
|
|
if (lseek(wfd, sector * (off_t)512, 0) == -1)
|
1994-09-23 08:30:13 +04:00
|
|
|
return (-1);
|
2003-04-30 23:47:10 +04:00
|
|
|
return (write(wfd, buf, 512));
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2000-12-19 18:44:27 +03:00
|
|
|
get_params(void)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
|
|
|
|
2000-07-06 21:22:43 +04:00
|
|
|
if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
|
|
|
|
warn("DIOCGDEFLABEL");
|
|
|
|
if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
|
|
|
|
warn("DIOCGDINFO");
|
|
|
|
return (-1);
|
|
|
|
}
|
1994-09-23 08:30:13 +04:00
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
cylinders = disklabel.d_ncylinders;
|
|
|
|
heads = disklabel.d_ntracks;
|
|
|
|
sectors = disklabel.d_nsectors;
|
1998-11-08 03:29:08 +03:00
|
|
|
disksectors = disklabel.d_secperunit;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
/* pick up some defaults for the BIOS sizes */
|
|
|
|
if (sectors <= MAXSECTOR) {
|
|
|
|
dos_cylinders = cylinders;
|
|
|
|
dos_heads = heads;
|
|
|
|
dos_sectors = sectors;
|
|
|
|
} else {
|
|
|
|
/* guess - has to better than the above */
|
|
|
|
dos_sectors = MAXSECTOR;
|
|
|
|
dos_heads = MAXHEAD - 1; /* some BIOS might use 256 */
|
|
|
|
dos_cylinders = disksectors / (MAXSECTOR * (MAXHEAD - 1));
|
|
|
|
if (dos_cylinders > MAXCYL - 1)
|
|
|
|
dos_cylinders = MAXCYL - 1;
|
|
|
|
}
|
|
|
|
dos_disksectors = disksectors;
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
return (0);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
read_s0(daddr_t offset, mbr_sector_t *boot)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (read_disk(offset, &boot->mbr_bootinst) == -1) {
|
|
|
|
warn("Can't read %spartition table",
|
|
|
|
offset ? "extended " : "");
|
|
|
|
return -1;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
if (le16toh(boot->mbr_signature) != MBR_MAGIC) {
|
|
|
|
warnx("%spartition table invalid, no magic in sector %lld",
|
|
|
|
offset ? "extended " : "", (long long)offset);
|
|
|
|
return -1;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
1994-09-23 08:30:13 +04:00
|
|
|
return (0);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
write_mbr(void)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1998-10-15 19:23:23 +04:00
|
|
|
int flag, i;
|
2003-04-30 23:47:10 +04:00
|
|
|
daddr_t offset;
|
|
|
|
int rval = -1;
|
1994-09-23 08:30:13 +04:00
|
|
|
|
1993-04-09 23:23:56 +04:00
|
|
|
/*
|
|
|
|
* write enable label sector before write (if necessary),
|
|
|
|
* disable after writing.
|
|
|
|
* needed if the disklabel protected area also protects
|
|
|
|
* sector 0. (e.g. empty disk)
|
|
|
|
*/
|
|
|
|
flag = 1;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (wfd == fd && ioctl(wfd, DIOCWLABEL, &flag) < 0)
|
1994-09-23 08:30:13 +04:00
|
|
|
warn("DIOCWLABEL");
|
2003-04-30 23:47:10 +04:00
|
|
|
if (write_disk(0, &mboot.mbr_bootinst) == -1) {
|
|
|
|
warn("Can't write fdisk partition table");
|
|
|
|
goto protect_label;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
if (boot_installed)
|
|
|
|
for (i = bootsize; (i -= 0x200) > 0;)
|
|
|
|
if (write_disk(i / 0x200, &bootcode[i / 0x200]) == -1) {
|
|
|
|
warn("Can't write bootcode");
|
|
|
|
goto protect_label;
|
|
|
|
}
|
|
|
|
for (offset = 0, i = 0; i < ext.num_ptn; i++) {
|
|
|
|
if (write_disk(ext.base + offset, ext.ptn + i) == -1) {
|
|
|
|
warn("Can't write %dth extended partition", i);
|
|
|
|
goto protect_label;
|
1998-10-15 19:23:23 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
offset = le32toh(ext.ptn[i].mbr_parts[1].mbrp_start);
|
|
|
|
}
|
|
|
|
rval = 0;
|
|
|
|
protect_label:
|
1994-09-23 08:30:13 +04:00
|
|
|
flag = 0;
|
2003-04-30 23:47:10 +04:00
|
|
|
if (wfd == fd && ioctl(wfd, DIOCWLABEL, &flag) < 0)
|
1994-09-23 08:30:13 +04:00
|
|
|
warn("DIOCWLABEL");
|
2003-04-30 23:47:10 +04:00
|
|
|
return rval;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2003-04-30 23:47:10 +04:00
|
|
|
yesno(const char *str, ...)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
int ch, first;
|
2003-04-30 23:47:10 +04:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, str);
|
1994-09-23 08:30:13 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
vprintf(str, ap);
|
|
|
|
printf(" [n] ");
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
first = ch = getchar();
|
|
|
|
while (ch != '\n' && ch != EOF)
|
|
|
|
ch = getchar();
|
2002-06-05 16:19:23 +04:00
|
|
|
if (ch == EOF)
|
|
|
|
errx(1, "EOF");
|
1994-09-23 08:30:13 +04:00
|
|
|
return (first == 'y' || first == 'Y');
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
int
|
|
|
|
decimal(const char *prompt, int dflt, int flags, int minval, int maxval)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-12-05 23:15:31 +03:00
|
|
|
int acc = 0;
|
1994-09-23 08:30:13 +04:00
|
|
|
char *cp;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
for (;;) {
|
|
|
|
if (flags & DEC_SEC) {
|
|
|
|
printf("%s: [%d..%dcyl default: %d, %dcyl, %uMB] ",
|
|
|
|
prompt, SEC_TO_CYL(minval), SEC_TO_CYL(maxval),
|
|
|
|
dflt, SEC_TO_CYL(dflt), SEC_TO_MB(dflt));
|
|
|
|
} else
|
|
|
|
printf("%s: [%d..%d default: %d] ",
|
|
|
|
prompt, minval, maxval, dflt);
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2002-06-05 16:19:23 +04:00
|
|
|
if (!fgets(lbuf, LBUF, stdin))
|
|
|
|
errx(1, "EOF");
|
1994-10-20 03:06:17 +03:00
|
|
|
lbuf[strlen(lbuf)-1] = '\0';
|
|
|
|
cp = lbuf;
|
|
|
|
|
|
|
|
cp += strspn(cp, " \t");
|
|
|
|
if (*cp == '\0')
|
2003-04-30 23:47:10 +04:00
|
|
|
return dflt;
|
|
|
|
|
|
|
|
if (cp[0] == '$' && cp[1] == 0)
|
|
|
|
return maxval;
|
|
|
|
|
|
|
|
if (isdigit(*cp) || *cp == '-') {
|
|
|
|
acc = strtol(lbuf, &cp, 10);
|
|
|
|
if (flags & DEC_SEC) {
|
|
|
|
if (*cp == 'm' || *cp == 'M') {
|
|
|
|
acc *= SEC_IN_1M;
|
|
|
|
/* round to whole number of cylinders */
|
|
|
|
acc += dos_cylindersectors / 2;
|
|
|
|
acc /= dos_cylindersectors;
|
|
|
|
cp = "c";
|
|
|
|
}
|
|
|
|
if (*cp == 'c' || *cp == 'C') {
|
|
|
|
cp = "";
|
|
|
|
acc *= dos_cylindersectors;
|
|
|
|
/* adjustments for cylinder boundary */
|
|
|
|
if (acc == 0 && flags & DEC_RND_0)
|
|
|
|
acc += dos_sectors;
|
|
|
|
if (flags & DEC_RND)
|
|
|
|
acc += dos_sectors;
|
|
|
|
if (flags & DEC_RND_DOWN)
|
|
|
|
acc -= dos_sectors;
|
|
|
|
if (flags & DEC_RND_DOWN_2)
|
|
|
|
acc -= dos_sectors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
cp += strspn(cp, " \t");
|
|
|
|
if (*cp != '\0') {
|
|
|
|
printf("%s is not a valid %s number.\n", lbuf,
|
|
|
|
flags & DEC_SEC ? "sector" : "decimal");
|
1994-09-23 09:30:09 +04:00
|
|
|
continue;
|
2003-04-30 23:47:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (acc >= minval && acc <= maxval)
|
|
|
|
return acc;
|
|
|
|
printf("%d is not between %d and %d.\n", acc, minval, maxval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ptn_id(const char *prompt, int *extended)
|
|
|
|
{
|
|
|
|
uint acc = 0;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
for (;; printf("%s is not a valid partition number.\n", lbuf)) {
|
|
|
|
printf("%s: [none] ", prompt);
|
|
|
|
|
|
|
|
if (!fgets(lbuf, LBUF, stdin))
|
|
|
|
errx(1, "EOF");
|
|
|
|
lbuf[strlen(lbuf)-1] = '\0';
|
|
|
|
cp = lbuf;
|
|
|
|
|
|
|
|
cp += strspn(cp, " \t");
|
|
|
|
*extended = 0;
|
|
|
|
if (*cp == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (*cp == 'E' || *cp == 'e') {
|
|
|
|
cp++;
|
|
|
|
*extended = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc = strtoul(cp, &cp, 10);
|
1994-10-20 03:06:17 +03:00
|
|
|
|
1994-09-23 09:30:09 +04:00
|
|
|
cp += strspn(cp, " \t");
|
|
|
|
if (*cp != '\0')
|
|
|
|
continue;
|
1993-04-09 23:23:56 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
if (*extended || acc < NMBRPART)
|
|
|
|
return acc;
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BOOTSEL
|
|
|
|
void
|
|
|
|
string(const char *prompt, int length, char *buf)
|
|
|
|
{
|
|
|
|
int len;
|
1994-09-23 09:30:09 +04:00
|
|
|
|
2003-04-30 23:47:10 +04:00
|
|
|
for (;;) {
|
|
|
|
printf("%s: [%.*s] ", prompt, length, buf);
|
|
|
|
|
|
|
|
if (!fgets(lbuf, LBUF, stdin))
|
|
|
|
errx(1, "EOF");
|
|
|
|
len = strlen(lbuf);
|
|
|
|
if (len <= 1)
|
|
|
|
/* unchanged if just <enter> */
|
|
|
|
return;
|
|
|
|
/* now strip trailing spaces, <space><enter> deletes string */
|
|
|
|
do
|
|
|
|
lbuf[--len] = 0;
|
|
|
|
while (len != 0 && lbuf[len - 1] == ' ');
|
|
|
|
if (len < length)
|
|
|
|
break;
|
|
|
|
printf("'%s' is longer than %d character.\n", lbuf, length - 1);
|
|
|
|
}
|
|
|
|
strncpy(buf, lbuf, length);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|
2003-04-30 23:47:10 +04:00
|
|
|
#endif
|
1993-04-09 23:23:56 +04:00
|
|
|
|
1994-09-23 08:30:13 +04:00
|
|
|
int
|
2000-12-19 18:44:27 +03:00
|
|
|
type_match(const void *key, const void *item)
|
1993-04-09 23:23:56 +04:00
|
|
|
{
|
1994-09-23 08:30:13 +04:00
|
|
|
const int *typep = key;
|
|
|
|
const struct part_type *ptr = item;
|
|
|
|
|
|
|
|
if (*typep < ptr->type)
|
|
|
|
return (-1);
|
|
|
|
if (*typep > ptr->type)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-12-19 18:44:27 +03:00
|
|
|
const char *
|
|
|
|
get_type(int type)
|
1994-09-23 08:30:13 +04:00
|
|
|
{
|
|
|
|
struct part_type *ptr;
|
|
|
|
|
|
|
|
ptr = bsearch(&type, part_types,
|
|
|
|
sizeof(part_types) / sizeof(struct part_type),
|
|
|
|
sizeof(struct part_type), type_match);
|
|
|
|
if (ptr == 0)
|
|
|
|
return ("unknown");
|
1997-09-06 01:26:57 +04:00
|
|
|
return (ptr->name);
|
1993-04-09 23:23:56 +04:00
|
|
|
}
|