1995-05-30 06:12:45 +00:00
|
|
|
/*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Mach Operating System
|
|
|
|
* Copyright (c) 1992 Carnegie Mellon University
|
|
|
|
* All Rights Reserved.
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00: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.
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00: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.
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Carnegie Mellon requests users of this software to return to
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
|
|
|
|
* School of Computer Science
|
|
|
|
* Carnegie Mellon University
|
|
|
|
* Pittsburgh PA 15213-3890
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1993-06-12 14:58:17 +00:00
|
|
|
* any improvements or extensions that they make and grant Carnegie Mellon
|
|
|
|
* the rights to redistribute these changes.
|
|
|
|
*/
|
|
|
|
|
2003-05-03 18:41:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1998-07-06 06:44:36 +00:00
|
|
|
|
2002-09-15 16:08:52 +00:00
|
|
|
#include <sys/disk.h>
|
1993-06-12 14:58:17 +00:00
|
|
|
#include <sys/disklabel.h>
|
2002-10-01 07:24:55 +00:00
|
|
|
#include <sys/diskmbr.h>
|
2003-04-13 21:57:08 +00:00
|
|
|
#include <sys/endian.h>
|
2001-07-13 16:48:56 +00:00
|
|
|
#include <sys/param.h>
|
1993-06-12 14:58:17 +00:00
|
|
|
#include <sys/stat.h>
|
2001-07-13 16:48:56 +00:00
|
|
|
#include <sys/mount.h>
|
1998-07-06 06:44:36 +00:00
|
|
|
#include <ctype.h>
|
1993-06-12 14:58:17 +00:00
|
|
|
#include <fcntl.h>
|
1998-07-06 06:44:36 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2005-07-15 08:02:51 +00:00
|
|
|
#include <libgeom.h>
|
2001-07-13 16:48:56 +00:00
|
|
|
#include <paths.h>
|
|
|
|
#include <regex.h>
|
2002-07-16 23:18:29 +00:00
|
|
|
#include <stdint.h>
|
1998-07-06 06:44:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1996-06-21 02:39:19 +00:00
|
|
|
#include <unistd.h>
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2011-11-04 13:36:02 +00:00
|
|
|
static int iotest;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2011-01-25 04:35:07 +00:00
|
|
|
#define NO_DISK_SECTORS ((u_int32_t)-1)
|
|
|
|
#define NO_TRACK_CYLINDERS 1023
|
|
|
|
#define NO_TRACK_HEADS 255
|
|
|
|
#define NO_TRACK_SECTORS 63
|
1993-06-12 14:58:17 +00:00
|
|
|
#define LBUF 100
|
|
|
|
static char lbuf[LBUF];
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Ported to 386bsd by Julian Elischer Thu Oct 15 20:26:46 PDT 1992
|
|
|
|
*
|
|
|
|
* 14-Dec-89 Robert Baron (rvb) at Carnegie-Mellon University
|
|
|
|
* Copyright (c) 1989 Robert. V. Baron
|
|
|
|
* Created.
|
|
|
|
*/
|
|
|
|
|
2011-01-25 04:35:07 +00:00
|
|
|
#define Decimal(str, ans, tmp, maxval) if (decimal(str, &tmp, ans, maxval)) ans = tmp
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
#define RoundCyl(x) ((((x) + cylsecs - 1) / cylsecs) * cylsecs)
|
|
|
|
|
1996-12-01 11:25:38 +00:00
|
|
|
#define MAX_SEC_SIZE 2048 /* maximum section size that is supported */
|
|
|
|
#define MIN_SEC_SIZE 512 /* the sector size to start sensing at */
|
2002-03-29 19:33:14 +00:00
|
|
|
static int secsize = 0; /* the sensed sector size */
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
static char *disk;
|
1995-09-01 18:00:14 +00:00
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
static int cyls, sectors, heads, cylsecs, disksecs;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
struct mboot {
|
2005-05-01 09:50:02 +00:00
|
|
|
unsigned char *bootinst; /* boot code */
|
|
|
|
off_t bootinst_size;
|
2003-04-13 21:57:08 +00:00
|
|
|
struct dos_partition parts[NDOSPART];
|
1993-06-12 14:58:17 +00:00
|
|
|
};
|
2002-03-29 19:33:14 +00:00
|
|
|
|
|
|
|
static struct mboot mboot;
|
2005-07-15 08:02:51 +00:00
|
|
|
static int fd;
|
2002-12-29 15:17:11 +00:00
|
|
|
|
1993-06-12 14:58:17 +00:00
|
|
|
#define ACTIVE 0x80
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
static uint dos_cyls;
|
|
|
|
static uint dos_heads;
|
|
|
|
static uint dos_sectors;
|
|
|
|
static uint dos_cylsecs;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
#define DOSSECT(s,c) ((s & 0x3f) | ((c >> 2) & 0xc0))
|
|
|
|
#define DOSCYL(c) (c & 0xff)
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
#define MAX_ARGS 10
|
|
|
|
|
|
|
|
static int current_line_number;
|
|
|
|
|
|
|
|
static int geom_processed = 0;
|
|
|
|
static int part_processed = 0;
|
|
|
|
static int active_processed = 0;
|
|
|
|
|
|
|
|
typedef struct cmd {
|
|
|
|
char cmd;
|
|
|
|
int n_args;
|
|
|
|
struct arg {
|
2011-07-17 21:08:16 +00:00
|
|
|
char argtype;
|
|
|
|
unsigned long arg_val;
|
|
|
|
char * arg_str;
|
1996-11-06 14:08:39 +00:00
|
|
|
} args[MAX_ARGS];
|
|
|
|
} CMD;
|
|
|
|
|
1999-06-27 19:29:15 +00:00
|
|
|
static int B_flag = 0; /* replace boot code */
|
2000-03-10 22:03:00 +00:00
|
|
|
static int I_flag = 0; /* use entire disk for FreeBSD */
|
1993-06-12 14:58:17 +00:00
|
|
|
static int a_flag = 0; /* set active partition */
|
1999-06-27 19:29:15 +00:00
|
|
|
static char *b_flag = NULL; /* path to boot code */
|
1993-06-12 14:58:17 +00:00
|
|
|
static int i_flag = 0; /* replace partition data */
|
2008-07-31 00:55:29 +00:00
|
|
|
static int q_flag = 0; /* Be quiet */
|
1993-06-12 14:58:17 +00:00
|
|
|
static int u_flag = 0; /* update partition data */
|
2000-03-10 22:03:00 +00:00
|
|
|
static int s_flag = 0; /* Print a summary and exit */
|
2002-04-18 16:25:18 +00:00
|
|
|
static int t_flag = 0; /* test only */
|
1996-11-06 14:08:39 +00:00
|
|
|
static char *f_flag = NULL; /* Read config info from file */
|
|
|
|
static int v_flag = 0; /* Be verbose */
|
2007-04-30 18:29:36 +00:00
|
|
|
static int print_config_flag = 0;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2009-01-14 21:31:09 +00:00
|
|
|
/*
|
|
|
|
* A list of partition types, probably outdated.
|
|
|
|
*/
|
|
|
|
static const char *const part_types[256] = {
|
|
|
|
[0x00] = "unused",
|
|
|
|
[0x01] = "Primary DOS with 12 bit FAT",
|
|
|
|
[0x02] = "XENIX / file system",
|
|
|
|
[0x03] = "XENIX /usr file system",
|
|
|
|
[0x04] = "Primary DOS with 16 bit FAT (< 32MB)",
|
|
|
|
[0x05] = "Extended DOS",
|
2009-01-14 22:05:51 +00:00
|
|
|
[0x06] = "Primary DOS, 16 bit FAT (>= 32MB)",
|
|
|
|
[0x07] = "NTFS, OS/2 HPFS, QNX-2 (16 bit) or Advanced UNIX",
|
2009-01-14 21:31:09 +00:00
|
|
|
[0x08] = "AIX file system or SplitDrive",
|
|
|
|
[0x09] = "AIX boot partition or Coherent",
|
|
|
|
[0x0A] = "OS/2 Boot Manager, OPUS or Coherent swap",
|
|
|
|
[0x0B] = "DOS or Windows 95 with 32 bit FAT",
|
|
|
|
[0x0C] = "DOS or Windows 95 with 32 bit FAT (LBA)",
|
|
|
|
[0x0E] = "Primary 'big' DOS (>= 32MB, LBA)",
|
|
|
|
[0x0F] = "Extended DOS (LBA)",
|
|
|
|
[0x10] = "OPUS",
|
|
|
|
[0x11] = "OS/2 BM: hidden DOS with 12-bit FAT",
|
|
|
|
[0x12] = "Compaq diagnostics",
|
|
|
|
[0x14] = "OS/2 BM: hidden DOS with 16-bit FAT (< 32MB)",
|
|
|
|
[0x16] = "OS/2 BM: hidden DOS with 16-bit FAT (>= 32MB)",
|
|
|
|
[0x17] = "OS/2 BM: hidden IFS (e.g. HPFS)",
|
|
|
|
[0x18] = "AST Windows swapfile",
|
2009-01-14 22:05:51 +00:00
|
|
|
[0x1b] = "ASUS Recovery partition (NTFS)",
|
2009-01-14 21:31:09 +00:00
|
|
|
[0x24] = "NEC DOS",
|
|
|
|
[0x3C] = "PartitionMagic recovery",
|
|
|
|
[0x39] = "plan9",
|
|
|
|
[0x40] = "VENIX 286",
|
|
|
|
[0x41] = "Linux/MINIX (sharing disk with DRDOS)",
|
|
|
|
[0x42] = "SFS or Linux swap (sharing disk with DRDOS)",
|
|
|
|
[0x43] = "Linux native (sharing disk with DRDOS)",
|
|
|
|
[0x4D] = "QNX 4.2 Primary",
|
|
|
|
[0x4E] = "QNX 4.2 Secondary",
|
|
|
|
[0x4F] = "QNX 4.2 Tertiary",
|
|
|
|
[0x50] = "DM (disk manager)",
|
|
|
|
[0x51] = "DM6 Aux1 (or Novell)",
|
|
|
|
[0x52] = "CP/M or Microport SysV/AT",
|
|
|
|
[0x53] = "DM6 Aux3",
|
|
|
|
[0x54] = "DM6",
|
|
|
|
[0x55] = "EZ-Drive (disk manager)",
|
|
|
|
[0x56] = "Golden Bow (disk manager)",
|
|
|
|
[0x5c] = "Priam Edisk (disk manager)", /* according to S. Widlake */
|
|
|
|
[0x61] = "SpeedStor",
|
|
|
|
[0x63] = "System V/386 (such as ISC UNIX), GNU HURD or Mach",
|
|
|
|
[0x64] = "Novell Netware/286 2.xx",
|
|
|
|
[0x65] = "Novell Netware/386 3.xx",
|
|
|
|
[0x70] = "DiskSecure Multi-Boot",
|
|
|
|
[0x75] = "PCIX",
|
|
|
|
[0x77] = "QNX4.x",
|
|
|
|
[0x78] = "QNX4.x 2nd part",
|
|
|
|
[0x79] = "QNX4.x 3rd part",
|
|
|
|
[0x80] = "Minix until 1.4a",
|
|
|
|
[0x81] = "Minix since 1.4b, early Linux partition or Mitac disk manager",
|
|
|
|
[0x82] = "Linux swap or Solaris x86",
|
|
|
|
[0x83] = "Linux native",
|
|
|
|
[0x84] = "OS/2 hidden C: drive",
|
|
|
|
[0x85] = "Linux extended",
|
|
|
|
[0x86] = "NTFS volume set??",
|
|
|
|
[0x87] = "NTFS volume set??",
|
|
|
|
[0x93] = "Amoeba file system",
|
|
|
|
[0x94] = "Amoeba bad block table",
|
|
|
|
[0x9F] = "BSD/OS",
|
|
|
|
[0xA0] = "Suspend to Disk",
|
|
|
|
[0xA5] = "FreeBSD/NetBSD/386BSD",
|
|
|
|
[0xA6] = "OpenBSD",
|
|
|
|
[0xA7] = "NeXTSTEP",
|
|
|
|
[0xA9] = "NetBSD",
|
|
|
|
[0xAC] = "IBM JFS",
|
|
|
|
[0xAF] = "HFS+",
|
|
|
|
[0xB7] = "BSDI BSD/386 file system",
|
|
|
|
[0xB8] = "BSDI BSD/386 swap",
|
|
|
|
[0xBE] = "Solaris x86 boot",
|
|
|
|
[0xBF] = "Solaris x86 (new)",
|
|
|
|
[0xC1] = "DRDOS/sec with 12-bit FAT",
|
|
|
|
[0xC4] = "DRDOS/sec with 16-bit FAT (< 32MB)",
|
|
|
|
[0xC6] = "DRDOS/sec with 16-bit FAT (>= 32MB)",
|
|
|
|
[0xC7] = "Syrinx",
|
|
|
|
[0xDB] = "CP/M, Concurrent CP/M, Concurrent DOS or CTOS",
|
2009-01-14 22:05:51 +00:00
|
|
|
[0xDE] = "DELL Utilities - FAT filesystem",
|
2009-01-14 21:31:09 +00:00
|
|
|
[0xE1] = "DOS access or SpeedStor with 12-bit FAT extended partition",
|
|
|
|
[0xE3] = "DOS R/O or SpeedStor",
|
|
|
|
[0xE4] = "SpeedStor with 16-bit FAT extended partition < 1024 cyl.",
|
|
|
|
[0xEB] = "BeOS file system",
|
|
|
|
[0xEE] = "EFI GPT",
|
|
|
|
[0xEF] = "EFI System Partition",
|
|
|
|
[0xF1] = "SpeedStor",
|
|
|
|
[0xF2] = "DOS 3.3+ Secondary",
|
|
|
|
[0xF4] = "SpeedStor large partition",
|
2012-04-16 17:30:19 +00:00
|
|
|
[0xFB] = "VMware VMFS",
|
2009-01-14 21:31:09 +00:00
|
|
|
[0xFE] = "SpeedStor >1024 cyl. or LANstep",
|
|
|
|
[0xFF] = "Xenix bad blocks table",
|
1993-06-12 14:58:17 +00:00
|
|
|
};
|
|
|
|
|
2009-01-14 21:31:09 +00:00
|
|
|
static const char *
|
|
|
|
get_type(int t)
|
|
|
|
{
|
|
|
|
const char *ret;
|
|
|
|
|
|
|
|
ret = (t >= 0 && t <= 255) ? part_types[t] : NULL;
|
|
|
|
return ret ? ret : "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-07 06:24:35 +00:00
|
|
|
static int geom_class_available(const char *);
|
2009-01-14 20:53:46 +00:00
|
|
|
static void print_s0(void);
|
2009-01-14 21:08:19 +00:00
|
|
|
static void print_part(const struct dos_partition *);
|
1996-06-21 02:39:19 +00:00
|
|
|
static void init_sector0(unsigned long start);
|
1996-11-06 14:08:39 +00:00
|
|
|
static void init_boot(void);
|
1996-06-21 02:39:19 +00:00
|
|
|
static void change_part(int i);
|
2002-03-29 19:33:14 +00:00
|
|
|
static void print_params(void);
|
1996-06-21 02:39:19 +00:00
|
|
|
static void change_active(int which);
|
2002-03-29 19:33:14 +00:00
|
|
|
static void change_code(void);
|
|
|
|
static void get_params_to_use(void);
|
2001-07-13 16:48:56 +00:00
|
|
|
static char *get_rootdisk(void);
|
2001-08-05 16:24:13 +00:00
|
|
|
static void dos(struct dos_partition *partp);
|
2002-03-29 19:33:14 +00:00
|
|
|
static int open_disk(int flag);
|
1996-06-21 02:39:19 +00:00
|
|
|
static ssize_t read_disk(off_t sector, void *buf);
|
2005-07-15 08:02:51 +00:00
|
|
|
static int write_disk(off_t sector, void *buf);
|
2002-03-29 19:33:14 +00:00
|
|
|
static int get_params(void);
|
|
|
|
static int read_s0(void);
|
|
|
|
static int write_s0(void);
|
|
|
|
static int ok(const char *str);
|
2011-01-25 04:35:07 +00:00
|
|
|
static int decimal(const char *str, int *num, int deflt, uint32_t maxval);
|
1996-11-06 14:08:39 +00:00
|
|
|
static int read_config(char *config_file);
|
|
|
|
static void reset_boot(void);
|
2000-08-24 17:54:45 +00:00
|
|
|
static int sanitize_partition(struct dos_partition *);
|
1998-07-06 06:44:36 +00:00
|
|
|
static void usage(void);
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
1999-06-27 19:29:15 +00:00
|
|
|
int c, i;
|
2002-03-29 19:33:14 +00:00
|
|
|
int partition = -1;
|
|
|
|
struct dos_partition *partp;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2008-07-31 00:55:29 +00:00
|
|
|
while ((c = getopt(argc, argv, "BIab:f:ipqstuv1234")) != -1)
|
1999-06-27 19:29:15 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'B':
|
|
|
|
B_flag = 1;
|
|
|
|
break;
|
2000-03-10 22:03:00 +00:00
|
|
|
case 'I':
|
|
|
|
I_flag = 1;
|
|
|
|
break;
|
1999-06-27 19:29:15 +00:00
|
|
|
case 'a':
|
|
|
|
a_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
b_flag = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
f_flag = optarg;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
i_flag = 1;
|
1993-06-12 14:58:17 +00:00
|
|
|
break;
|
2007-04-30 18:29:36 +00:00
|
|
|
case 'p':
|
|
|
|
print_config_flag = 1;
|
|
|
|
break;
|
2008-07-31 00:55:29 +00:00
|
|
|
case 'q':
|
|
|
|
q_flag = 1;
|
|
|
|
break;
|
2000-03-10 22:03:00 +00:00
|
|
|
case 's':
|
|
|
|
s_flag = 1;
|
|
|
|
break;
|
1999-06-27 19:29:15 +00:00
|
|
|
case 't':
|
|
|
|
t_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
u_flag = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
v_flag = 1;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
partition = c - '0';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
1999-06-27 19:29:15 +00:00
|
|
|
if (f_flag || i_flag)
|
|
|
|
u_flag = 1;
|
|
|
|
if (t_flag)
|
|
|
|
v_flag = 1;
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2001-07-13 16:48:56 +00:00
|
|
|
if (argc == 0) {
|
|
|
|
disk = get_rootdisk();
|
|
|
|
} else {
|
2008-09-07 13:58:35 +00:00
|
|
|
disk = g_device_path(argv[0]);
|
|
|
|
if (disk == NULL)
|
2009-01-13 23:36:38 +00:00
|
|
|
err(1, "unable to get correct path for %s", argv[0]);
|
1995-09-01 18:00:14 +00:00
|
|
|
}
|
2001-07-13 16:48:56 +00:00
|
|
|
if (open_disk(u_flag) < 0)
|
|
|
|
err(1, "cannot open disk %s", disk);
|
2000-07-12 16:45:11 +00:00
|
|
|
|
|
|
|
/* (abu)use mboot.bootinst to probe for the sector size */
|
|
|
|
if ((mboot.bootinst = malloc(MAX_SEC_SIZE)) == NULL)
|
|
|
|
err(1, "cannot allocate buffer to determine disk sector size");
|
2006-06-18 22:02:22 +00:00
|
|
|
if (read_disk(0, mboot.bootinst) == -1)
|
|
|
|
errx(1, "could not detect sector size");
|
2000-07-17 19:51:42 +00:00
|
|
|
free(mboot.bootinst);
|
|
|
|
mboot.bootinst = NULL;
|
2000-07-12 16:45:11 +00:00
|
|
|
|
2007-04-30 18:29:36 +00:00
|
|
|
if (print_config_flag) {
|
|
|
|
if (read_s0())
|
|
|
|
err(1, "read_s0");
|
|
|
|
|
|
|
|
printf("# %s\n", disk);
|
|
|
|
printf("g c%d h%d s%d\n", dos_cyls, dos_heads, dos_sectors);
|
|
|
|
|
|
|
|
for (i = 0; i < NDOSPART; i++) {
|
2009-01-13 23:36:38 +00:00
|
|
|
partp = &mboot.parts[i];
|
2007-04-30 18:29:36 +00:00
|
|
|
|
|
|
|
if (partp->dp_start == 0 && partp->dp_size == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
printf("p %d 0x%02x %lu %lu\n", i + 1, partp->dp_typ,
|
|
|
|
(u_long)partp->dp_start, (u_long)partp->dp_size);
|
|
|
|
|
|
|
|
/* Fill flags for the partition. */
|
|
|
|
if (partp->dp_flag & 0x80)
|
|
|
|
printf("a %d\n", i + 1);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (s_flag) {
|
2000-03-10 22:03:00 +00:00
|
|
|
if (read_s0())
|
|
|
|
err(1, "read_s0");
|
|
|
|
printf("%s: %d cyl %d hd %d sec\n", disk, dos_cyls, dos_heads,
|
|
|
|
dos_sectors);
|
|
|
|
printf("Part %11s %11s Type Flags\n", "Start", "Size");
|
|
|
|
for (i = 0; i < NDOSPART; i++) {
|
2009-01-13 23:36:38 +00:00
|
|
|
partp = &mboot.parts[i];
|
2000-03-10 22:03:00 +00:00
|
|
|
if (partp->dp_start == 0 && partp->dp_size == 0)
|
|
|
|
continue;
|
|
|
|
printf("%4d: %11lu %11lu 0x%02x 0x%02x\n", i + 1,
|
|
|
|
(u_long) partp->dp_start,
|
|
|
|
(u_long) partp->dp_size, partp->dp_typ,
|
|
|
|
partp->dp_flag);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
printf("******* Working on device %s *******\n",disk);
|
1996-11-06 14:08:39 +00:00
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
if (I_flag) {
|
1999-08-23 11:06:19 +00:00
|
|
|
read_s0();
|
|
|
|
reset_boot();
|
2009-01-13 23:36:38 +00:00
|
|
|
partp = &mboot.parts[0];
|
1999-08-23 11:06:19 +00:00
|
|
|
partp->dp_typ = DOSPTYP_386BSD;
|
|
|
|
partp->dp_flag = ACTIVE;
|
|
|
|
partp->dp_start = dos_sectors;
|
2000-08-24 17:54:45 +00:00
|
|
|
partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs -
|
|
|
|
dos_sectors;
|
2001-08-05 16:24:13 +00:00
|
|
|
dos(partp);
|
1999-08-23 11:06:19 +00:00
|
|
|
if (v_flag)
|
2009-01-14 20:53:46 +00:00
|
|
|
print_s0();
|
2002-04-18 16:25:18 +00:00
|
|
|
if (!t_flag)
|
|
|
|
write_s0();
|
1999-08-23 11:06:19 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (f_flag) {
|
1996-11-06 14:08:39 +00:00
|
|
|
if (read_s0() || i_flag)
|
|
|
|
reset_boot();
|
|
|
|
if (!read_config(f_flag))
|
|
|
|
exit(1);
|
|
|
|
if (v_flag)
|
2009-01-14 20:53:46 +00:00
|
|
|
print_s0();
|
1996-11-06 14:08:39 +00:00
|
|
|
if (!t_flag)
|
|
|
|
write_s0();
|
2002-03-29 19:33:14 +00:00
|
|
|
} else {
|
1996-11-06 14:08:39 +00:00
|
|
|
if(u_flag)
|
|
|
|
get_params_to_use();
|
|
|
|
else
|
1993-06-12 14:58:17 +00:00
|
|
|
print_params();
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
if (read_s0())
|
2000-08-24 17:54:45 +00:00
|
|
|
init_sector0(dos_sectors);
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1996-12-01 11:25:38 +00:00
|
|
|
printf("Media sector size is %d\n", secsize);
|
1996-11-06 14:08:39 +00:00
|
|
|
printf("Warning: BIOS sector numbering starts with sector 1\n");
|
|
|
|
printf("Information from DOS bootblock is:\n");
|
|
|
|
if (partition == -1)
|
1997-06-03 21:24:39 +00:00
|
|
|
for (i = 1; i <= NDOSPART; i++)
|
1996-11-06 14:08:39 +00:00
|
|
|
change_part(i);
|
|
|
|
else
|
1993-06-12 14:58:17 +00:00
|
|
|
change_part(partition);
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
if (u_flag || a_flag)
|
1993-06-12 14:58:17 +00:00
|
|
|
change_active(partition);
|
|
|
|
|
1999-06-27 19:29:15 +00:00
|
|
|
if (B_flag)
|
1999-01-22 11:54:17 +00:00
|
|
|
change_code();
|
|
|
|
|
1999-06-27 19:29:15 +00:00
|
|
|
if (u_flag || a_flag || B_flag) {
|
2002-04-18 16:25:18 +00:00
|
|
|
if (!t_flag) {
|
1996-11-06 14:08:39 +00:00
|
|
|
printf("\nWe haven't changed the partition table yet. ");
|
|
|
|
printf("This is your last chance.\n");
|
|
|
|
}
|
2009-01-14 20:53:46 +00:00
|
|
|
print_s0();
|
2002-04-18 16:25:18 +00:00
|
|
|
if (!t_flag) {
|
1996-11-06 14:08:39 +00:00
|
|
|
if (ok("Should we write new partition table?"))
|
1993-06-12 14:58:17 +00:00
|
|
|
write_s0();
|
2005-05-01 09:50:02 +00:00
|
|
|
} else {
|
1996-11-06 14:08:39 +00:00
|
|
|
printf("\n-t flag specified -- partition table not written.\n");
|
|
|
|
}
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
1998-07-06 06:44:36 +00:00
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1998-07-06 06:44:36 +00:00
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
1999-06-27 19:29:15 +00:00
|
|
|
fprintf(stderr, "%s%s",
|
2008-07-31 00:55:29 +00:00
|
|
|
"usage: fdisk [-BIaipqstu] [-b bootcode] [-1234] [disk]\n",
|
1999-06-27 19:29:15 +00:00
|
|
|
" fdisk -f configfile [-itv] [disk]\n");
|
1998-07-06 06:44:36 +00:00
|
|
|
exit(1);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
2009-01-14 20:53:46 +00:00
|
|
|
print_s0(void)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2002-03-29 19:33:14 +00:00
|
|
|
int i;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
print_params();
|
|
|
|
printf("Information from DOS bootblock is:\n");
|
2009-01-14 21:08:19 +00:00
|
|
|
for (i = 1; i <= NDOSPART; i++) {
|
|
|
|
printf("%d: ", i);
|
|
|
|
print_part(&mboot.parts[i - 1]);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
static struct dos_partition mtpart;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
2009-01-14 21:08:19 +00:00
|
|
|
print_part(const struct dos_partition *partp)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
1997-06-02 19:25:48 +00:00
|
|
|
u_int64_t part_mb;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
if (!bcmp(partp, &mtpart, sizeof (struct dos_partition))) {
|
|
|
|
printf("<UNUSED>\n");
|
|
|
|
return;
|
|
|
|
}
|
1997-06-02 19:25:48 +00:00
|
|
|
/*
|
|
|
|
* Be careful not to overflow.
|
|
|
|
*/
|
|
|
|
part_mb = partp->dp_size;
|
|
|
|
part_mb *= secsize;
|
|
|
|
part_mb /= (1024 * 1024);
|
2002-02-18 19:32:38 +00:00
|
|
|
printf("sysid %d (%#04x),(%s)\n", partp->dp_typ, partp->dp_typ,
|
|
|
|
get_type(partp->dp_typ));
|
2002-07-16 23:18:29 +00:00
|
|
|
printf(" start %lu, size %lu (%ju Meg), flag %x%s\n",
|
1998-06-28 20:48:03 +00:00
|
|
|
(u_long)partp->dp_start,
|
2011-10-29 17:30:57 +00:00
|
|
|
(u_long)partp->dp_size,
|
2002-07-16 23:18:29 +00:00
|
|
|
(uintmax_t)part_mb,
|
1998-03-29 23:41:51 +00:00
|
|
|
partp->dp_flag,
|
|
|
|
partp->dp_flag == ACTIVE ? " (active)" : "");
|
2000-11-29 20:22:47 +00:00
|
|
|
printf("\tbeg: cyl %d/ head %d/ sector %d;\n\tend: cyl %d/ head %d/ sector %d\n"
|
1993-06-12 14:58:17 +00:00
|
|
|
,DPCYL(partp->dp_scyl, partp->dp_ssect)
|
|
|
|
,partp->dp_shd
|
2000-11-29 20:22:47 +00:00
|
|
|
,DPSECT(partp->dp_ssect)
|
1993-06-12 14:58:17 +00:00
|
|
|
,DPCYL(partp->dp_ecyl, partp->dp_esect)
|
2000-11-29 20:22:47 +00:00
|
|
|
,partp->dp_ehd
|
|
|
|
,DPSECT(partp->dp_esect));
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_boot(void)
|
|
|
|
{
|
2002-05-01 06:48:29 +00:00
|
|
|
#ifndef __ia64__
|
1999-06-27 19:29:15 +00:00
|
|
|
const char *fname;
|
2002-03-29 19:33:14 +00:00
|
|
|
int fdesc, n;
|
2000-07-12 16:45:11 +00:00
|
|
|
struct stat sb;
|
1999-06-27 19:29:15 +00:00
|
|
|
|
|
|
|
fname = b_flag ? b_flag : "/boot/mbr";
|
2002-03-29 19:33:14 +00:00
|
|
|
if ((fdesc = open(fname, O_RDONLY)) == -1 ||
|
|
|
|
fstat(fdesc, &sb) == -1)
|
2000-07-12 16:45:11 +00:00
|
|
|
err(1, "%s", fname);
|
2011-10-29 17:28:59 +00:00
|
|
|
if (sb.st_size == 0)
|
|
|
|
errx(1, "%s is empty, must not be.", fname);
|
2000-07-12 16:45:11 +00:00
|
|
|
if ((mboot.bootinst_size = sb.st_size) % secsize != 0)
|
|
|
|
errx(1, "%s: length must be a multiple of sector size", fname);
|
2000-07-15 13:37:47 +00:00
|
|
|
if (mboot.bootinst != NULL)
|
|
|
|
free(mboot.bootinst);
|
2000-07-12 16:45:11 +00:00
|
|
|
if ((mboot.bootinst = malloc(mboot.bootinst_size = sb.st_size)) == NULL)
|
|
|
|
errx(1, "%s: unable to allocate read buffer", fname);
|
2002-03-29 19:33:14 +00:00
|
|
|
if ((n = read(fdesc, mboot.bootinst, mboot.bootinst_size)) == -1 ||
|
|
|
|
close(fdesc))
|
1999-06-27 19:29:15 +00:00
|
|
|
err(1, "%s", fname);
|
2000-07-12 16:45:11 +00:00
|
|
|
if (n != mboot.bootinst_size)
|
|
|
|
errx(1, "%s: short read", fname);
|
2002-05-01 06:48:29 +00:00
|
|
|
#else
|
|
|
|
if (mboot.bootinst != NULL)
|
|
|
|
free(mboot.bootinst);
|
|
|
|
mboot.bootinst_size = secsize;
|
|
|
|
if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL)
|
|
|
|
errx(1, "unable to allocate boot block buffer");
|
|
|
|
memset(mboot.bootinst, 0, mboot.bootinst_size);
|
2003-04-13 21:57:08 +00:00
|
|
|
le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
|
2002-05-01 06:48:29 +00:00
|
|
|
#endif
|
1996-11-06 14:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
|
|
|
init_sector0(unsigned long start)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2009-01-13 23:36:38 +00:00
|
|
|
struct dos_partition *partp = &mboot.parts[0];
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
init_boot();
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
partp->dp_typ = DOSPTYP_386BSD;
|
|
|
|
partp->dp_flag = ACTIVE;
|
2000-08-24 17:54:45 +00:00
|
|
|
start = ((start + dos_sectors - 1) / dos_sectors) * dos_sectors;
|
2000-07-12 16:45:11 +00:00
|
|
|
if(start == 0)
|
|
|
|
start = dos_sectors;
|
1993-06-12 14:58:17 +00:00
|
|
|
partp->dp_start = start;
|
2000-08-24 17:54:45 +00:00
|
|
|
partp->dp_size = (disksecs / dos_cylsecs) * dos_cylsecs - start;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2001-08-05 16:24:13 +00:00
|
|
|
dos(partp);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
|
|
|
change_part(int i)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2009-01-13 23:36:38 +00:00
|
|
|
struct dos_partition *partp = &mboot.parts[i - 1];
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
printf("The data for partition %d is:\n", i);
|
2009-01-14 21:08:19 +00:00
|
|
|
print_part(partp);
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
if (u_flag && ok("Do you want to change it?")) {
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if (i_flag) {
|
2009-01-14 21:08:19 +00:00
|
|
|
bzero(partp, sizeof (*partp));
|
|
|
|
if (i == 1) {
|
|
|
|
init_sector0(1);
|
|
|
|
printf("\nThe static data for the slice 1 has been reinitialized to:\n");
|
|
|
|
print_part(partp);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2011-01-25 04:35:07 +00:00
|
|
|
Decimal("sysid (165=FreeBSD)", partp->dp_typ, tmp, 255);
|
|
|
|
Decimal("start", partp->dp_start, tmp, NO_DISK_SECTORS);
|
|
|
|
Decimal("size", partp->dp_size, tmp, NO_DISK_SECTORS);
|
2001-08-05 16:24:13 +00:00
|
|
|
if (!sanitize_partition(partp)) {
|
|
|
|
warnx("ERROR: failed to adjust; setting sysid to 0");
|
|
|
|
partp->dp_typ = 0;
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1998-05-20 22:23:16 +00:00
|
|
|
if (ok("Explicitly specify beg/end address ?"))
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
int tsec,tcyl,thd;
|
|
|
|
tcyl = DPCYL(partp->dp_scyl,partp->dp_ssect);
|
|
|
|
thd = partp->dp_shd;
|
|
|
|
tsec = DPSECT(partp->dp_ssect);
|
2011-01-25 04:35:07 +00:00
|
|
|
Decimal("beginning cylinder", tcyl, tmp, NO_TRACK_CYLINDERS);
|
|
|
|
Decimal("beginning head", thd, tmp, NO_TRACK_HEADS);
|
|
|
|
Decimal("beginning sector", tsec, tmp, NO_TRACK_SECTORS);
|
1993-06-12 14:58:17 +00:00
|
|
|
partp->dp_scyl = DOSCYL(tcyl);
|
|
|
|
partp->dp_ssect = DOSSECT(tsec,tcyl);
|
|
|
|
partp->dp_shd = thd;
|
|
|
|
|
|
|
|
tcyl = DPCYL(partp->dp_ecyl,partp->dp_esect);
|
|
|
|
thd = partp->dp_ehd;
|
|
|
|
tsec = DPSECT(partp->dp_esect);
|
2011-01-25 04:35:07 +00:00
|
|
|
Decimal("ending cylinder", tcyl, tmp, NO_TRACK_CYLINDERS);
|
|
|
|
Decimal("ending head", thd, tmp, NO_TRACK_HEADS);
|
|
|
|
Decimal("ending sector", tsec, tmp, NO_TRACK_SECTORS);
|
1993-06-12 14:58:17 +00:00
|
|
|
partp->dp_ecyl = DOSCYL(tcyl);
|
|
|
|
partp->dp_esect = DOSSECT(tsec,tcyl);
|
|
|
|
partp->dp_ehd = thd;
|
2001-08-05 16:24:13 +00:00
|
|
|
} else
|
|
|
|
dos(partp);
|
1995-05-30 06:12:45 +00:00
|
|
|
|
2009-01-14 21:08:19 +00:00
|
|
|
print_part(partp);
|
1993-06-12 14:58:17 +00:00
|
|
|
} while (!ok("Are we happy with this entry?"));
|
|
|
|
}
|
2005-05-01 09:50:02 +00:00
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
1993-06-12 14:58:17 +00:00
|
|
|
print_params()
|
|
|
|
{
|
|
|
|
printf("parameters extracted from in-core disklabel are:\n");
|
|
|
|
printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
|
|
|
|
,cyls,heads,sectors,cylsecs);
|
2005-05-01 09:50:02 +00:00
|
|
|
if (dos_cyls > 1023 || dos_heads > 255 || dos_sectors > 63)
|
1996-12-01 11:25:38 +00:00
|
|
|
printf("Figures below won't work with BIOS for partitions not in cyl 1\n");
|
1993-06-12 14:58:17 +00:00
|
|
|
printf("parameters to be used for BIOS calculations are:\n");
|
|
|
|
printf("cylinders=%d heads=%d sectors/track=%d (%d blks/cyl)\n\n"
|
|
|
|
,dos_cyls,dos_heads,dos_sectors,dos_cylsecs);
|
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
|
|
|
change_active(int which)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2001-08-05 16:24:13 +00:00
|
|
|
struct dos_partition *partp = &mboot.parts[0];
|
|
|
|
int active, i, new, tmp;
|
|
|
|
|
|
|
|
active = -1;
|
|
|
|
for (i = 0; i < NDOSPART; i++) {
|
|
|
|
if ((partp[i].dp_flag & ACTIVE) == 0)
|
|
|
|
continue;
|
|
|
|
printf("Partition %d is marked active\n", i + 1);
|
|
|
|
if (active == -1)
|
|
|
|
active = i + 1;
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
if (a_flag && which != -1)
|
|
|
|
active = which;
|
2001-08-05 16:24:13 +00:00
|
|
|
else if (active == -1)
|
|
|
|
active = 1;
|
|
|
|
|
1994-10-19 21:25:28 +00:00
|
|
|
if (!ok("Do you want to change the active partition?"))
|
|
|
|
return;
|
1998-03-29 23:41:51 +00:00
|
|
|
setactive:
|
|
|
|
do {
|
2001-08-05 16:24:13 +00:00
|
|
|
new = active;
|
2011-01-22 05:21:20 +00:00
|
|
|
Decimal("active partition", new, tmp, 0);
|
2001-08-05 16:24:13 +00:00
|
|
|
if (new < 1 || new > 4) {
|
1998-03-29 23:41:51 +00:00
|
|
|
printf("Active partition number must be in range 1-4."
|
|
|
|
" Try again.\n");
|
|
|
|
goto setactive;
|
|
|
|
}
|
2001-08-05 16:24:13 +00:00
|
|
|
active = new;
|
1998-03-29 23:41:51 +00:00
|
|
|
} while (!ok("Are you happy with this choice"));
|
1993-06-12 14:58:17 +00:00
|
|
|
for (i = 0; i < NDOSPART; i++)
|
|
|
|
partp[i].dp_flag = 0;
|
1997-06-03 21:24:39 +00:00
|
|
|
if (active > 0 && active <= NDOSPART)
|
|
|
|
partp[active-1].dp_flag = ACTIVE;
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1999-01-22 11:54:17 +00:00
|
|
|
static void
|
|
|
|
change_code()
|
|
|
|
{
|
|
|
|
if (ok("Do you want to change the boot code?"))
|
|
|
|
init_boot();
|
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
void
|
1993-06-12 14:58:17 +00:00
|
|
|
get_params_to_use()
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
print_params();
|
|
|
|
if (ok("Do you want to change our idea of what BIOS thinks ?"))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
2011-01-22 05:21:20 +00:00
|
|
|
Decimal("BIOS's idea of #cylinders", dos_cyls, tmp, 0);
|
|
|
|
Decimal("BIOS's idea of #heads", dos_heads, tmp, 0);
|
|
|
|
Decimal("BIOS's idea of #sectors", dos_sectors, tmp, 0);
|
1993-06-12 14:58:17 +00:00
|
|
|
dos_cylsecs = dos_heads * dos_sectors;
|
|
|
|
print_params();
|
|
|
|
}
|
|
|
|
while(!ok("Are you happy with this choice"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
|
1993-06-12 14:58:17 +00:00
|
|
|
/***********************************************\
|
|
|
|
* Change real numbers into strange dos numbers *
|
|
|
|
\***********************************************/
|
1996-06-21 02:39:19 +00:00
|
|
|
static void
|
2002-03-29 19:33:14 +00:00
|
|
|
dos(struct dos_partition *partp)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2001-08-05 16:24:13 +00:00
|
|
|
int cy, sec;
|
|
|
|
u_int32_t end;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2001-12-30 18:51:51 +00:00
|
|
|
if (partp->dp_typ == 0 && partp->dp_start == 0 && partp->dp_size == 0) {
|
|
|
|
memcpy(partp, &mtpart, sizeof(*partp));
|
1994-10-19 21:25:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-08-05 16:24:13 +00:00
|
|
|
/* Start c/h/s. */
|
|
|
|
partp->dp_shd = partp->dp_start % dos_cylsecs / dos_sectors;
|
|
|
|
cy = partp->dp_start / dos_cylsecs;
|
|
|
|
sec = partp->dp_start % dos_sectors + 1;
|
|
|
|
partp->dp_scyl = DOSCYL(cy);
|
|
|
|
partp->dp_ssect = DOSSECT(sec, cy);
|
|
|
|
|
|
|
|
/* End c/h/s. */
|
|
|
|
end = partp->dp_start + partp->dp_size - 1;
|
|
|
|
partp->dp_ehd = end % dos_cylsecs / dos_sectors;
|
|
|
|
cy = end / dos_cylsecs;
|
|
|
|
sec = end % dos_sectors + 1;
|
|
|
|
partp->dp_ecyl = DOSCYL(cy);
|
|
|
|
partp->dp_esect = DOSSECT(sec, cy);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
open_disk(int flag)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2005-07-15 08:02:51 +00:00
|
|
|
int rwmode;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2008-09-07 13:58:35 +00:00
|
|
|
/* Write mode if one of these flags are set. */
|
|
|
|
rwmode = (a_flag || I_flag || B_flag || flag);
|
|
|
|
fd = g_open(disk, rwmode);
|
|
|
|
/* If the mode fails, try read-only if we didn't. */
|
|
|
|
if (fd == -1 && errno == EPERM && rwmode)
|
|
|
|
fd = g_open(disk, 0);
|
2002-12-29 15:17:11 +00:00
|
|
|
if (fd == -1 && errno == ENXIO)
|
|
|
|
return -2;
|
|
|
|
if (fd == -1) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("can't open device %s", disk);
|
1993-06-12 14:58:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (get_params() == -1) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("can't get disk parameters on %s", disk);
|
1993-06-12 14:58:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static ssize_t
|
|
|
|
read_disk(off_t sector, void *buf)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2005-05-01 09:50:02 +00:00
|
|
|
|
|
|
|
lseek(fd, (sector * 512), 0);
|
|
|
|
if (secsize == 0)
|
|
|
|
for (secsize = MIN_SEC_SIZE; secsize <= MAX_SEC_SIZE;
|
|
|
|
secsize *= 2) {
|
1996-12-01 11:25:38 +00:00
|
|
|
/* try the read */
|
|
|
|
int size = read(fd, buf, secsize);
|
2005-05-01 09:50:02 +00:00
|
|
|
if (size == secsize)
|
1996-12-01 11:25:38 +00:00
|
|
|
/* it worked so return */
|
|
|
|
return secsize;
|
2005-05-01 09:50:02 +00:00
|
|
|
}
|
1996-12-01 11:25:38 +00:00
|
|
|
else
|
2005-05-01 09:50:02 +00:00
|
|
|
return read(fd, buf, secsize);
|
1996-12-01 11:25:38 +00:00
|
|
|
|
|
|
|
/* we failed to read at any of the sizes */
|
|
|
|
return -1;
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2005-07-15 08:02:51 +00:00
|
|
|
static int
|
2011-11-07 06:24:35 +00:00
|
|
|
geom_class_available(const char *name)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2011-11-07 06:24:35 +00:00
|
|
|
struct gclass *class;
|
|
|
|
struct gmesh mesh;
|
2005-07-15 08:02:51 +00:00
|
|
|
int error;
|
2011-11-07 06:24:35 +00:00
|
|
|
|
|
|
|
error = geom_gettree(&mesh);
|
|
|
|
if (error != 0)
|
|
|
|
errc(1, error, "Cannot get GEOM tree");
|
|
|
|
|
|
|
|
LIST_FOREACH(class, &mesh.lg_class, lg_class) {
|
|
|
|
if (strcmp(class->lg_name, name) == 0) {
|
|
|
|
geom_deletetree(&mesh);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
geom_deletetree(&mesh);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
write_disk(off_t sector, void *buf)
|
|
|
|
{
|
2005-07-15 08:02:51 +00:00
|
|
|
struct gctl_req *grq;
|
2008-09-07 13:58:35 +00:00
|
|
|
const char *errmsg;
|
2011-11-07 06:24:35 +00:00
|
|
|
char *pname;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Check that GEOM_MBR is available */
|
|
|
|
if (geom_class_available("MBR") != 0) {
|
|
|
|
grq = gctl_get_handle();
|
|
|
|
gctl_ro_param(grq, "verb", -1, "write MBR");
|
|
|
|
gctl_ro_param(grq, "class", -1, "MBR");
|
|
|
|
pname = g_providername(fd);
|
|
|
|
if (pname == NULL) {
|
|
|
|
warn("Error getting providername for %s", disk);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
gctl_ro_param(grq, "geom", -1, pname);
|
|
|
|
gctl_ro_param(grq, "data", secsize, buf);
|
|
|
|
errmsg = gctl_issue(grq);
|
|
|
|
free(pname);
|
|
|
|
if (errmsg == NULL) {
|
|
|
|
gctl_free(grq);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
if (!q_flag)
|
|
|
|
warnx("GEOM_MBR: %s", errmsg);
|
2005-09-17 14:56:10 +00:00
|
|
|
gctl_free(grq);
|
2011-11-07 06:24:35 +00:00
|
|
|
} else {
|
2011-11-07 07:50:35 +00:00
|
|
|
/*
|
|
|
|
* Try to write MBR directly. This may help when disk
|
2011-11-07 06:24:35 +00:00
|
|
|
* is not in use.
|
|
|
|
* XXX: hardcoded sectorsize
|
|
|
|
*/
|
|
|
|
error = pwrite(fd, buf, secsize, (sector * 512));
|
|
|
|
if (error == secsize)
|
2005-07-15 08:02:51 +00:00
|
|
|
return (0);
|
2002-12-29 15:17:11 +00:00
|
|
|
}
|
2011-11-07 06:24:35 +00:00
|
|
|
|
2011-11-07 07:50:35 +00:00
|
|
|
/*
|
|
|
|
* GEOM_MBR is not available or failed to write MBR.
|
2011-11-07 06:24:35 +00:00
|
|
|
* Now check that we have GEOM_PART and recommend to use gpart (8).
|
|
|
|
*/
|
|
|
|
if (geom_class_available("PART") != 0)
|
|
|
|
warnx("Failed to write MBR. Try to use gpart(8).");
|
|
|
|
else
|
|
|
|
warnx("Failed to write sector zero");
|
2005-07-15 08:02:51 +00:00
|
|
|
return(EINVAL);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static int
|
|
|
|
get_params()
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2002-09-20 08:12:51 +00:00
|
|
|
int error;
|
|
|
|
u_int u;
|
|
|
|
off_t o;
|
|
|
|
|
|
|
|
error = ioctl(fd, DIOCGFWSECTORS, &u);
|
|
|
|
if (error == 0)
|
|
|
|
sectors = dos_sectors = u;
|
2003-03-29 22:06:52 +00:00
|
|
|
else
|
|
|
|
sectors = dos_sectors = 63;
|
|
|
|
|
2002-09-20 08:12:51 +00:00
|
|
|
error = ioctl(fd, DIOCGFWHEADS, &u);
|
|
|
|
if (error == 0)
|
|
|
|
heads = dos_heads = u;
|
2003-03-29 22:06:52 +00:00
|
|
|
else
|
|
|
|
heads = dos_heads = 255;
|
2002-09-20 08:12:51 +00:00
|
|
|
|
1994-09-15 20:19:51 +00:00
|
|
|
dos_cylsecs = cylsecs = heads * sectors;
|
|
|
|
disksecs = cyls * heads * sectors;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2008-09-07 13:58:35 +00:00
|
|
|
u = g_sectorsize(fd);
|
|
|
|
if (u <= 0)
|
|
|
|
return (-1);
|
2002-09-20 08:12:51 +00:00
|
|
|
|
2008-09-07 13:58:35 +00:00
|
|
|
o = g_mediasize(fd);
|
|
|
|
if (o < 0)
|
|
|
|
return (-1);
|
|
|
|
disksecs = o / u;
|
|
|
|
cyls = dos_cyls = o / (u * dos_heads * dos_sectors);
|
2002-09-20 08:12:51 +00:00
|
|
|
|
|
|
|
return (disksecs);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static int
|
1993-06-12 14:58:17 +00:00
|
|
|
read_s0()
|
|
|
|
{
|
2003-04-13 21:57:08 +00:00
|
|
|
int i;
|
|
|
|
|
2000-07-15 13:37:47 +00:00
|
|
|
mboot.bootinst_size = secsize;
|
|
|
|
if (mboot.bootinst != NULL)
|
2000-07-12 16:45:11 +00:00
|
|
|
free(mboot.bootinst);
|
2000-07-15 13:37:47 +00:00
|
|
|
if ((mboot.bootinst = malloc(mboot.bootinst_size)) == NULL) {
|
|
|
|
warnx("unable to allocate buffer to read fdisk "
|
|
|
|
"partition table");
|
|
|
|
return -1;
|
2000-07-12 16:45:11 +00:00
|
|
|
}
|
|
|
|
if (read_disk(0, mboot.bootinst) == -1) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("can't read fdisk partition table");
|
1993-06-12 14:58:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2003-04-13 21:57:08 +00:00
|
|
|
if (le16dec(&mboot.bootinst[DOSMAGICOFFSET]) != DOSMAGIC) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("invalid fdisk partition table found");
|
1993-06-12 14:58:17 +00:00
|
|
|
/* So should we initialize things */
|
|
|
|
return -1;
|
|
|
|
}
|
2003-04-13 21:57:08 +00:00
|
|
|
for (i = 0; i < NDOSPART; i++)
|
|
|
|
dos_partition_dec(
|
|
|
|
&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
|
|
|
|
&mboot.parts[i]);
|
1993-06-12 14:58:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static int
|
1993-06-12 14:58:17 +00:00
|
|
|
write_s0()
|
|
|
|
{
|
2003-04-13 21:57:08 +00:00
|
|
|
int sector, i;
|
2000-07-12 16:45:11 +00:00
|
|
|
|
1993-06-12 14:58:17 +00:00
|
|
|
if (iotest) {
|
2009-01-14 20:53:46 +00:00
|
|
|
print_s0();
|
1993-06-12 14:58:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-04-13 21:57:08 +00:00
|
|
|
for(i = 0; i < NDOSPART; i++)
|
|
|
|
dos_partition_enc(&mboot.bootinst[DOSPARTOFF + i * DOSPARTSIZE],
|
|
|
|
&mboot.parts[i]);
|
|
|
|
le16enc(&mboot.bootinst[DOSMAGICOFFSET], DOSMAGIC);
|
2011-10-29 17:30:57 +00:00
|
|
|
for(sector = 0; sector < mboot.bootinst_size / secsize; sector++)
|
2000-07-12 16:45:11 +00:00
|
|
|
if (write_disk(sector,
|
|
|
|
&mboot.bootinst[sector * secsize]) == -1) {
|
|
|
|
warn("can't write fdisk partition table");
|
|
|
|
return -1;
|
|
|
|
}
|
1996-06-21 02:39:19 +00:00
|
|
|
return(0);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
ok(const char *str)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
printf("%s [n] ", str);
|
2001-08-05 16:24:13 +00:00
|
|
|
fflush(stdout);
|
|
|
|
if (fgets(lbuf, LBUF, stdin) == NULL)
|
|
|
|
exit(1);
|
1993-06-12 14:58:17 +00:00
|
|
|
lbuf[strlen(lbuf)-1] = 0;
|
|
|
|
|
|
|
|
if (*lbuf &&
|
|
|
|
(!strcmp(lbuf, "yes") || !strcmp(lbuf, "YES") ||
|
|
|
|
!strcmp(lbuf, "y") || !strcmp(lbuf, "Y")))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-06-21 02:39:19 +00:00
|
|
|
static int
|
2011-01-25 04:35:07 +00:00
|
|
|
decimal(const char *str, int *num, int deflt, uint32_t maxval)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2011-11-06 21:12:52 +00:00
|
|
|
long long acc;
|
2011-01-22 05:21:20 +00:00
|
|
|
int c;
|
2002-03-29 19:33:14 +00:00
|
|
|
char *cp;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
while (1) {
|
2011-11-06 21:12:52 +00:00
|
|
|
acc = 0;
|
1993-06-12 14:58:17 +00:00
|
|
|
printf("Supply a decimal value for \"%s\" [%d] ", str, deflt);
|
2001-08-05 16:24:13 +00:00
|
|
|
fflush(stdout);
|
|
|
|
if (fgets(lbuf, LBUF, stdin) == NULL)
|
|
|
|
exit(1);
|
1993-06-12 14:58:17 +00:00
|
|
|
lbuf[strlen(lbuf)-1] = 0;
|
|
|
|
|
|
|
|
if (!*lbuf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
cp = lbuf;
|
|
|
|
while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
|
|
|
|
if (!c)
|
|
|
|
return 0;
|
1996-06-21 02:39:19 +00:00
|
|
|
while ((c = *cp++)) {
|
2011-01-22 05:21:20 +00:00
|
|
|
if (c <= '9' && c >= '0') {
|
2011-08-19 12:48:06 +00:00
|
|
|
if (acc <= maxval || maxval == 0)
|
2011-01-22 05:21:20 +00:00
|
|
|
acc = acc * 10 + c - '0';
|
|
|
|
} else
|
1993-06-12 14:58:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c == ' ' || c == '\t')
|
|
|
|
while ((c = *cp) && (c == ' ' || c == '\t')) cp++;
|
|
|
|
if (!c) {
|
2011-01-25 04:35:07 +00:00
|
|
|
if (maxval > 0 && acc > maxval) {
|
|
|
|
acc = maxval;
|
|
|
|
printf("%s exceeds maximum value allowed for "
|
|
|
|
"this field. The value has been reduced "
|
|
|
|
"to %lld\n", lbuf, acc);
|
2011-01-22 05:21:20 +00:00
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
*num = acc;
|
|
|
|
return 1;
|
|
|
|
} else
|
1998-03-29 23:41:51 +00:00
|
|
|
printf("%s is an invalid decimal number. Try again.\n",
|
1993-06-12 14:58:17 +00:00
|
|
|
lbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
|
|
|
|
static void
|
2002-03-29 19:33:14 +00:00
|
|
|
parse_config_line(char *line, CMD *command)
|
1996-11-06 14:08:39 +00:00
|
|
|
{
|
|
|
|
char *cp, *end;
|
|
|
|
|
|
|
|
cp = line;
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
1996-11-06 14:08:39 +00:00
|
|
|
memset(command, 0, sizeof(*command));
|
|
|
|
|
|
|
|
while (isspace(*cp)) ++cp;
|
|
|
|
if (*cp == '\0' || *cp == '#')
|
|
|
|
break;
|
|
|
|
command->cmd = *cp++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse args
|
|
|
|
*/
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
1996-11-06 14:08:39 +00:00
|
|
|
while (isspace(*cp)) ++cp;
|
2009-05-25 09:23:26 +00:00
|
|
|
if (*cp == '\0')
|
|
|
|
break; /* eol */
|
1996-11-06 14:08:39 +00:00
|
|
|
if (*cp == '#')
|
|
|
|
break; /* found comment */
|
|
|
|
if (isalpha(*cp))
|
|
|
|
command->args[command->n_args].argtype = *cp++;
|
|
|
|
end = NULL;
|
2011-07-17 21:08:16 +00:00
|
|
|
command->args[command->n_args].arg_val = strtoul(cp, &end, 0);
|
2009-05-25 09:23:26 +00:00
|
|
|
if (cp == end || (!isspace(*end) && *end != '\0')) {
|
|
|
|
char ch;
|
|
|
|
end = cp;
|
|
|
|
while (!isspace(*end) && *end != '\0') ++end;
|
|
|
|
ch = *end; *end = '\0';
|
|
|
|
command->args[command->n_args].arg_str = strdup(cp);
|
|
|
|
*end = ch;
|
|
|
|
} else
|
|
|
|
command->args[command->n_args].arg_str = NULL;
|
1996-11-06 14:08:39 +00:00
|
|
|
cp = end;
|
|
|
|
command->n_args++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
process_geometry(CMD *command)
|
1996-11-06 14:08:39 +00:00
|
|
|
{
|
|
|
|
int status = 1, i;
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
1996-11-06 14:08:39 +00:00
|
|
|
geom_processed = 1;
|
2002-03-29 19:33:14 +00:00
|
|
|
if (part_processed) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx(
|
|
|
|
"ERROR line %d: the geometry specification line must occur before\n\
|
|
|
|
all partition specifications",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (command->n_args != 3) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: incorrect number of geometry args",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
dos_cyls = 0;
|
|
|
|
dos_heads = 0;
|
|
|
|
dos_sectors = 0;
|
|
|
|
for (i = 0; i < 3; ++i) {
|
|
|
|
switch (command->args[i].argtype) {
|
1996-11-06 14:08:39 +00:00
|
|
|
case 'c':
|
|
|
|
dos_cyls = command->args[i].arg_val;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
dos_heads = command->args[i].arg_val;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
dos_sectors = command->args[i].arg_val;
|
|
|
|
break;
|
|
|
|
default:
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx(
|
|
|
|
"ERROR line %d: unknown geometry arg type: '%c' (0x%02x)",
|
|
|
|
current_line_number, command->args[i].argtype,
|
1996-11-06 14:08:39 +00:00
|
|
|
command->args[i].argtype);
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
dos_cylsecs = dos_heads * dos_sectors;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do sanity checks on parameter values
|
|
|
|
*/
|
2002-03-29 19:33:14 +00:00
|
|
|
if (dos_cyls == 0) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: number of cylinders not specified",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (dos_cyls > 1024) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING line %d: number of cylinders (%d) may be out-of-range\n\
|
1996-11-06 14:08:39 +00:00
|
|
|
(must be within 1-1024 for normal BIOS operation, unless the entire disk\n\
|
1998-07-06 06:44:36 +00:00
|
|
|
is dedicated to FreeBSD)",
|
|
|
|
current_line_number, dos_cyls);
|
1996-11-06 14:08:39 +00:00
|
|
|
}
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
if (dos_heads == 0) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: number of heads not specified",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
2009-06-17 06:41:10 +00:00
|
|
|
} else if (dos_heads > 256) {
|
|
|
|
warnx("ERROR line %d: number of heads must be within (1-256)",
|
1998-07-06 06:44:36 +00:00
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
if (dos_sectors == 0) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: number of sectors not specified",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
2002-03-29 19:33:14 +00:00
|
|
|
} else if (dos_sectors > 63) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: number of sectors must be within (1-63)",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
|
2009-05-25 09:23:26 +00:00
|
|
|
static u_int32_t
|
|
|
|
str2sectors(const char *str)
|
|
|
|
{
|
|
|
|
char *end;
|
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
val = strtoul(str, &end, 0);
|
|
|
|
if (str == end || *end == '\0') {
|
|
|
|
warnx("ERROR line %d: unexpected size: \'%s\'",
|
|
|
|
current_line_number, str);
|
2011-01-25 04:35:07 +00:00
|
|
|
return NO_DISK_SECTORS;
|
2009-05-25 09:23:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-29 17:30:57 +00:00
|
|
|
if (*end == 'K')
|
2009-05-25 09:23:26 +00:00
|
|
|
val *= 1024UL / secsize;
|
|
|
|
else if (*end == 'M')
|
|
|
|
val *= 1024UL * 1024UL / secsize;
|
|
|
|
else if (*end == 'G')
|
|
|
|
val *= 1024UL * 1024UL * 1024UL / secsize;
|
|
|
|
else {
|
|
|
|
warnx("ERROR line %d: unexpected modifier: %c "
|
|
|
|
"(not K/M/G)", current_line_number, *end);
|
2011-01-25 04:35:07 +00:00
|
|
|
return NO_DISK_SECTORS;
|
2009-05-25 09:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
1996-11-06 14:08:39 +00:00
|
|
|
|
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
process_partition(CMD *command)
|
1996-11-06 14:08:39 +00:00
|
|
|
{
|
|
|
|
int status = 0, partition;
|
2000-08-24 17:54:45 +00:00
|
|
|
u_int32_t prev_head_boundary, prev_cyl_boundary;
|
|
|
|
u_int32_t adj_size, max_end;
|
1996-11-06 14:08:39 +00:00
|
|
|
struct dos_partition *partp;
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
1996-11-06 14:08:39 +00:00
|
|
|
part_processed = 1;
|
2002-03-29 19:33:14 +00:00
|
|
|
if (command->n_args != 4) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: incorrect number of partition args",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
partition = command->args[0].arg_val;
|
2002-03-29 19:33:14 +00:00
|
|
|
if (partition < 1 || partition > 4) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: invalid partition number %d",
|
|
|
|
current_line_number, partition);
|
1996-11-06 14:08:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-01-13 23:36:38 +00:00
|
|
|
partp = &mboot.parts[partition - 1];
|
|
|
|
bzero(partp, sizeof (*partp));
|
1996-11-06 14:08:39 +00:00
|
|
|
partp->dp_typ = command->args[1].arg_val;
|
2009-05-25 09:23:26 +00:00
|
|
|
if (command->args[2].arg_str != NULL) {
|
|
|
|
if (strcmp(command->args[2].arg_str, "*") == 0) {
|
|
|
|
int i;
|
|
|
|
partp->dp_start = dos_sectors;
|
|
|
|
for (i = 1; i < partition; i++) {
|
|
|
|
struct dos_partition *prev_partp;
|
|
|
|
prev_partp = ((struct dos_partition *)
|
|
|
|
&mboot.parts) + i - 1;
|
|
|
|
if (prev_partp->dp_typ != 0)
|
|
|
|
partp->dp_start = prev_partp->dp_start +
|
|
|
|
prev_partp->dp_size;
|
|
|
|
}
|
|
|
|
if (partp->dp_start % dos_sectors != 0) {
|
|
|
|
prev_head_boundary = partp->dp_start /
|
|
|
|
dos_sectors * dos_sectors;
|
|
|
|
partp->dp_start = prev_head_boundary +
|
|
|
|
dos_sectors;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
partp->dp_start = str2sectors(command->args[2].arg_str);
|
2011-01-25 04:35:07 +00:00
|
|
|
if (partp->dp_start == NO_DISK_SECTORS)
|
2009-05-25 09:23:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
partp->dp_start = command->args[2].arg_val;
|
|
|
|
|
|
|
|
if (command->args[3].arg_str != NULL) {
|
|
|
|
if (strcmp(command->args[3].arg_str, "*") == 0)
|
|
|
|
partp->dp_size = ((disksecs / dos_cylsecs) *
|
|
|
|
dos_cylsecs) - partp->dp_start;
|
|
|
|
else {
|
|
|
|
partp->dp_size = str2sectors(command->args[3].arg_str);
|
2011-01-25 04:35:07 +00:00
|
|
|
if (partp->dp_size == NO_DISK_SECTORS)
|
2009-05-25 09:23:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev_cyl_boundary = ((partp->dp_start + partp->dp_size) /
|
|
|
|
dos_cylsecs) * dos_cylsecs;
|
|
|
|
if (prev_cyl_boundary > partp->dp_start)
|
|
|
|
partp->dp_size = prev_cyl_boundary - partp->dp_start;
|
|
|
|
} else
|
|
|
|
partp->dp_size = command->args[3].arg_val;
|
|
|
|
|
1996-11-06 14:08:39 +00:00
|
|
|
max_end = partp->dp_start + partp->dp_size;
|
|
|
|
|
2009-01-13 23:36:38 +00:00
|
|
|
if (partp->dp_typ == 0) {
|
1996-11-06 14:08:39 +00:00
|
|
|
/*
|
|
|
|
* Get out, the partition is marked as unused.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Insure that it's unused.
|
|
|
|
*/
|
2009-01-13 23:36:38 +00:00
|
|
|
bzero(partp, sizeof(*partp));
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-12-30 21:18:15 +00:00
|
|
|
* Adjust start upwards, if necessary, to fall on a head boundary.
|
1996-11-06 14:08:39 +00:00
|
|
|
*/
|
2002-03-29 19:33:14 +00:00
|
|
|
if (partp->dp_start % dos_sectors != 0) {
|
2000-08-24 17:54:45 +00:00
|
|
|
prev_head_boundary = partp->dp_start / dos_sectors * dos_sectors;
|
|
|
|
if (max_end < dos_sectors ||
|
2002-03-29 19:33:14 +00:00
|
|
|
prev_head_boundary > max_end - dos_sectors) {
|
1996-11-06 14:08:39 +00:00
|
|
|
/*
|
|
|
|
* Can't go past end of partition
|
|
|
|
*/
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx(
|
|
|
|
"ERROR line %d: unable to adjust start of partition %d to fall on\n\
|
2000-08-24 17:54:45 +00:00
|
|
|
a head boundary",
|
1998-07-06 06:44:36 +00:00
|
|
|
current_line_number, partition);
|
1996-11-06 14:08:39 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx(
|
2000-08-24 17:54:45 +00:00
|
|
|
"WARNING: adjusting start offset of partition %d\n\
|
|
|
|
from %u to %u, to fall on a head boundary",
|
|
|
|
partition, (u_int)partp->dp_start,
|
|
|
|
(u_int)(prev_head_boundary + dos_sectors));
|
|
|
|
partp->dp_start = prev_head_boundary + dos_sectors;
|
1996-11-06 14:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adjust size downwards, if necessary, to fall on a cylinder
|
|
|
|
* boundary.
|
|
|
|
*/
|
2000-08-24 17:54:45 +00:00
|
|
|
prev_cyl_boundary =
|
1996-11-06 14:08:39 +00:00
|
|
|
((partp->dp_start + partp->dp_size) / dos_cylsecs) * dos_cylsecs;
|
2000-08-24 17:54:45 +00:00
|
|
|
if (prev_cyl_boundary > partp->dp_start)
|
|
|
|
adj_size = prev_cyl_boundary - partp->dp_start;
|
2002-03-29 19:33:14 +00:00
|
|
|
else {
|
2000-08-24 17:54:45 +00:00
|
|
|
warnx(
|
|
|
|
"ERROR: could not adjust partition to start on a head boundary\n\
|
|
|
|
and end on a cylinder boundary.");
|
|
|
|
return (0);
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (adj_size != partp->dp_size) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx(
|
2000-08-24 17:54:45 +00:00
|
|
|
"WARNING: adjusting size of partition %d from %u to %u\n\
|
|
|
|
to end on a cylinder boundary",
|
|
|
|
partition, (u_int)partp->dp_size, (u_int)adj_size);
|
|
|
|
partp->dp_size = adj_size;
|
1996-11-06 14:08:39 +00:00
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (partp->dp_size == 0) {
|
2001-08-05 16:24:13 +00:00
|
|
|
warnx("ERROR line %d: size of partition %d is zero",
|
1998-07-06 06:44:36 +00:00
|
|
|
current_line_number, partition);
|
1996-11-06 14:08:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2001-08-05 16:24:13 +00:00
|
|
|
dos(partp);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
process_active(CMD *command)
|
1996-11-06 14:08:39 +00:00
|
|
|
{
|
|
|
|
int status = 0, partition, i;
|
|
|
|
struct dos_partition *partp;
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
1996-11-06 14:08:39 +00:00
|
|
|
active_processed = 1;
|
2002-03-29 19:33:14 +00:00
|
|
|
if (command->n_args != 1) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: incorrect number of active args",
|
|
|
|
current_line_number);
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
partition = command->args[0].arg_val;
|
2002-03-29 19:33:14 +00:00
|
|
|
if (partition < 1 || partition > 4) {
|
1998-07-06 06:44:36 +00:00
|
|
|
warnx("ERROR line %d: invalid partition number %d",
|
|
|
|
current_line_number, partition);
|
1996-11-06 14:08:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Reset active partition
|
|
|
|
*/
|
2009-01-13 23:36:38 +00:00
|
|
|
partp = mboot.parts;
|
1996-11-06 14:08:39 +00:00
|
|
|
for (i = 0; i < NDOSPART; i++)
|
|
|
|
partp[i].dp_flag = 0;
|
1997-06-03 21:24:39 +00:00
|
|
|
partp[partition-1].dp_flag = ACTIVE;
|
1996-11-06 14:08:39 +00:00
|
|
|
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
process_line(char *line)
|
1996-11-06 14:08:39 +00:00
|
|
|
{
|
|
|
|
CMD command;
|
|
|
|
int status = 1;
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
1996-11-06 14:08:39 +00:00
|
|
|
parse_config_line(line, &command);
|
2002-03-29 19:33:14 +00:00
|
|
|
switch (command.cmd) {
|
1996-11-06 14:08:39 +00:00
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* Comment or blank line
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
/*
|
|
|
|
* Set geometry
|
|
|
|
*/
|
|
|
|
status = process_geometry(&command);
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
status = process_partition(&command);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
status = process_active(&command);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
read_config(char *config_file)
|
1996-11-06 14:08:39 +00:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
int status = 1;
|
|
|
|
char buf[1010];
|
|
|
|
|
2002-03-29 19:33:14 +00:00
|
|
|
while (1) {
|
|
|
|
if (strcmp(config_file, "-") != 0) {
|
1996-11-06 14:08:39 +00:00
|
|
|
/*
|
|
|
|
* We're not reading from stdin
|
|
|
|
*/
|
2002-03-29 19:33:14 +00:00
|
|
|
if ((fp = fopen(config_file, "r")) == NULL) {
|
1996-11-06 14:08:39 +00:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
} else {
|
1996-11-06 14:08:39 +00:00
|
|
|
fp = stdin;
|
|
|
|
}
|
|
|
|
current_line_number = 0;
|
2002-03-29 19:33:14 +00:00
|
|
|
while (!feof(fp)) {
|
1996-11-06 14:08:39 +00:00
|
|
|
if (fgets(buf, sizeof(buf), fp) == NULL)
|
|
|
|
break;
|
|
|
|
++current_line_number;
|
|
|
|
status = process_line(buf);
|
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-03-29 19:33:14 +00:00
|
|
|
if (fp) {
|
1996-11-06 14:08:39 +00:00
|
|
|
/*
|
|
|
|
* It doesn't matter if we're reading from stdin, as we've reached EOF
|
|
|
|
*/
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return (status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
reset_boot(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dos_partition *partp;
|
|
|
|
|
|
|
|
init_boot();
|
2009-01-13 23:36:38 +00:00
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
partp = &mboot.parts[i];
|
|
|
|
bzero(partp, sizeof(*partp));
|
1996-11-06 14:08:39 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-24 17:54:45 +00:00
|
|
|
|
|
|
|
static int
|
2002-03-29 19:33:14 +00:00
|
|
|
sanitize_partition(struct dos_partition *partp)
|
2000-08-24 17:54:45 +00:00
|
|
|
{
|
|
|
|
u_int32_t prev_head_boundary, prev_cyl_boundary;
|
2001-08-05 16:24:13 +00:00
|
|
|
u_int32_t max_end, size, start;
|
|
|
|
|
|
|
|
start = partp->dp_start;
|
|
|
|
size = partp->dp_size;
|
|
|
|
max_end = start + size;
|
|
|
|
/* Only allow a zero size if the partition is being marked unused. */
|
|
|
|
if (size == 0) {
|
|
|
|
if (start == 0 && partp->dp_typ == 0)
|
|
|
|
return (1);
|
|
|
|
warnx("ERROR: size of partition is zero");
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* Return if no adjustment is necessary. */
|
|
|
|
if (start % dos_sectors == 0 && (start + size) % dos_sectors == 0)
|
|
|
|
return (1);
|
|
|
|
|
2003-11-13 21:13:43 +00:00
|
|
|
if (start == 0) {
|
|
|
|
warnx("WARNING: partition overlaps with partition table");
|
|
|
|
if (ok("Correct this automatically?"))
|
|
|
|
start = dos_sectors;
|
|
|
|
}
|
2001-08-05 16:24:13 +00:00
|
|
|
if (start % dos_sectors != 0)
|
|
|
|
warnx("WARNING: partition does not start on a head boundary");
|
|
|
|
if ((start +size) % dos_sectors != 0)
|
|
|
|
warnx("WARNING: partition does not end on a cylinder boundary");
|
|
|
|
warnx("WARNING: this may confuse the BIOS or some operating systems");
|
|
|
|
if (!ok("Correct this automatically?"))
|
|
|
|
return (1);
|
2000-08-24 17:54:45 +00:00
|
|
|
|
|
|
|
/*
|
2002-12-30 21:18:15 +00:00
|
|
|
* Adjust start upwards, if necessary, to fall on a head boundary.
|
2000-08-24 17:54:45 +00:00
|
|
|
*/
|
2001-08-05 16:24:13 +00:00
|
|
|
if (start % dos_sectors != 0) {
|
|
|
|
prev_head_boundary = start / dos_sectors * dos_sectors;
|
2000-08-24 17:54:45 +00:00
|
|
|
if (max_end < dos_sectors ||
|
2001-08-05 16:24:13 +00:00
|
|
|
prev_head_boundary >= max_end - dos_sectors) {
|
2000-08-24 17:54:45 +00:00
|
|
|
/*
|
|
|
|
* Can't go past end of partition
|
|
|
|
*/
|
|
|
|
warnx(
|
|
|
|
"ERROR: unable to adjust start of partition to fall on a head boundary");
|
|
|
|
return (0);
|
|
|
|
}
|
2001-08-05 16:24:13 +00:00
|
|
|
start = prev_head_boundary + dos_sectors;
|
2000-08-24 17:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adjust size downwards, if necessary, to fall on a cylinder
|
|
|
|
* boundary.
|
|
|
|
*/
|
2001-08-05 16:24:13 +00:00
|
|
|
prev_cyl_boundary = ((start + size) / dos_cylsecs) * dos_cylsecs;
|
|
|
|
if (prev_cyl_boundary > start)
|
|
|
|
size = prev_cyl_boundary - start;
|
|
|
|
else {
|
2000-08-24 17:54:45 +00:00
|
|
|
warnx("ERROR: could not adjust partition to start on a head boundary\n\
|
|
|
|
and end on a cylinder boundary.");
|
|
|
|
return (0);
|
|
|
|
}
|
2001-08-05 16:24:13 +00:00
|
|
|
|
|
|
|
/* Finally, commit any changes to partp and return. */
|
|
|
|
if (start != partp->dp_start) {
|
|
|
|
warnx("WARNING: adjusting start offset of partition to %u",
|
|
|
|
(u_int)start);
|
|
|
|
partp->dp_start = start;
|
2000-08-24 17:54:45 +00:00
|
|
|
}
|
2001-08-05 16:24:13 +00:00
|
|
|
if (size != partp->dp_size) {
|
|
|
|
warnx("WARNING: adjusting size of partition to %u", (u_int)size);
|
|
|
|
partp->dp_size = size;
|
2000-08-24 17:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
2001-07-13 16:48:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try figuring out the root device's canonical disk name.
|
|
|
|
* The following choices are considered:
|
|
|
|
* /dev/ad0s1a => /dev/ad0
|
|
|
|
* /dev/da0a => /dev/da0
|
|
|
|
* /dev/vinum/root => /dev/vinum/root
|
2010-09-06 04:15:49 +00:00
|
|
|
* A ".eli" part is removed if it exists (see geli(8)).
|
2010-09-16 08:16:53 +00:00
|
|
|
* A ".journal" ending is removed if it exists (see gjournal(8)).
|
2001-07-13 16:48:56 +00:00
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_rootdisk(void)
|
|
|
|
{
|
|
|
|
struct statfs rootfs;
|
|
|
|
regex_t re;
|
|
|
|
#define NMATCHES 2
|
|
|
|
regmatch_t rm[NMATCHES];
|
2010-09-06 04:15:49 +00:00
|
|
|
char dev[PATH_MAX], *s;
|
2001-07-13 16:48:56 +00:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (statfs("/", &rootfs) == -1)
|
|
|
|
err(1, "statfs(\"/\")");
|
|
|
|
|
2010-09-16 08:16:53 +00:00
|
|
|
if ((rv = regcomp(&re, "^(/dev/[a-z/]+[0-9]*)([sp][0-9]+)?[a-h]?(\\.journal)?$",
|
2001-07-13 16:48:56 +00:00
|
|
|
REG_EXTENDED)) != 0)
|
|
|
|
errx(1, "regcomp() failed (%d)", rv);
|
2010-09-06 04:15:49 +00:00
|
|
|
strlcpy(dev, rootfs.f_mntfromname, sizeof (dev));
|
|
|
|
if ((s = strstr(dev, ".eli")) != NULL)
|
|
|
|
memmove(s, s+4, strlen(s + 4) + 1);
|
|
|
|
|
|
|
|
if ((rv = regexec(&re, dev, NMATCHES, rm, 0)) != 0)
|
2001-07-13 16:48:56 +00:00
|
|
|
errx(1,
|
|
|
|
"mounted root fs resource doesn't match expectations (regexec returned %d)",
|
|
|
|
rv);
|
|
|
|
if ((s = malloc(rm[1].rm_eo - rm[1].rm_so + 1)) == NULL)
|
|
|
|
errx(1, "out of memory");
|
|
|
|
memcpy(s, rootfs.f_mntfromname + rm[1].rm_so,
|
|
|
|
rm[1].rm_eo - rm[1].rm_so);
|
|
|
|
s[rm[1].rm_eo - rm[1].rm_so] = 0;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|