2005-12-28 09:03:15 +03:00
|
|
|
/* $NetBSD: mbrlabel.c,v 1.26 2005/12/28 06:03:15 christos Exp $ */
|
2000-03-15 14:56:02 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 1998 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1998 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2005-12-28 09:03:15 +03:00
|
|
|
__RCSID("$NetBSD: mbrlabel.c,v 1.26 2005/12/28 06:03:15 christos Exp $");
|
2000-03-15 14:56:02 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-12-27 18:37:56 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2000-03-15 14:56:02 +03:00
|
|
|
#include <fcntl.h>
|
2005-12-27 18:37:56 +03:00
|
|
|
#include <limits.h>
|
2000-07-03 07:37:59 +04:00
|
|
|
#include <stdlib.h>
|
2000-03-15 14:56:02 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
2000-12-24 16:57:37 +03:00
|
|
|
#define FSTYPENAMES
|
2000-03-15 14:56:02 +03:00
|
|
|
#include <sys/disklabel.h>
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
#include <sys/bootblock.h>
|
2000-03-15 14:56:02 +03:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include "dkcksum.h"
|
2000-12-24 16:57:37 +03:00
|
|
|
#include "extern.h"
|
2000-03-15 14:56:02 +03:00
|
|
|
|
2000-12-24 16:57:37 +03:00
|
|
|
int main(int, char **);
|
|
|
|
void usage(void);
|
|
|
|
void getlabel(int);
|
2000-12-27 07:22:11 +03:00
|
|
|
void setlabel(int, int);
|
2001-02-18 06:36:07 +03:00
|
|
|
int getparts(int, u_int32_t, u_int32_t, int);
|
|
|
|
u_int16_t getshort(void *);
|
|
|
|
u_int32_t getlong(void *);
|
2000-03-15 14:56:02 +03:00
|
|
|
|
|
|
|
struct disklabel label;
|
|
|
|
|
|
|
|
void
|
2000-12-24 04:50:29 +03:00
|
|
|
getlabel(int sd)
|
2000-03-15 14:56:02 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
if (ioctl(sd, DIOCGDINFO, &label) < 0) {
|
|
|
|
perror("get label");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Some ports seem to not set the number of partitions
|
2001-07-27 02:47:34 +04:00
|
|
|
* correctly, albeit they seem to set the raw partition ok!
|
2000-03-15 14:56:02 +03:00
|
|
|
*/
|
2001-08-20 12:21:09 +04:00
|
|
|
if (label.d_npartitions <= getrawpartition())
|
|
|
|
label.d_npartitions = getrawpartition() + 1;
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-12-27 07:22:11 +03:00
|
|
|
setlabel(int sd, int doraw)
|
2000-03-15 14:56:02 +03:00
|
|
|
{
|
2003-06-07 14:03:39 +04:00
|
|
|
int one = 1;
|
2000-12-24 16:57:37 +03:00
|
|
|
|
2000-03-15 14:56:02 +03:00
|
|
|
label.d_checksum = 0;
|
|
|
|
label.d_checksum = dkcksum(&label);
|
2000-12-27 07:22:11 +03:00
|
|
|
if (ioctl(sd, doraw ? DIOCWDINFO : DIOCSDINFO, &label) < 0) {
|
2000-03-15 14:56:02 +03:00
|
|
|
perror("set label");
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-07 14:03:39 +04:00
|
|
|
if (!doraw)
|
|
|
|
/* If we haven't written to the disk, don't discard on close */
|
|
|
|
ioctl(sd, DIOCKLABEL, &one);
|
|
|
|
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
2001-02-18 06:36:07 +03:00
|
|
|
u_int16_t
|
|
|
|
getshort(void *p)
|
|
|
|
{
|
|
|
|
unsigned char *cp = p;
|
|
|
|
|
|
|
|
return (cp[0] | (cp[1] << 8));
|
|
|
|
}
|
|
|
|
|
2000-03-15 14:56:02 +03:00
|
|
|
u_int32_t
|
2000-12-24 04:50:29 +03:00
|
|
|
getlong(void *p)
|
2000-03-15 14:56:02 +03:00
|
|
|
{
|
|
|
|
unsigned char *cp = p;
|
|
|
|
|
2000-12-24 16:57:37 +03:00
|
|
|
return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-02-18 06:36:07 +03:00
|
|
|
getparts(int sd, u_int32_t off, u_int32_t extoff, int verbose)
|
2000-03-15 14:56:02 +03:00
|
|
|
{
|
2000-12-24 16:57:37 +03:00
|
|
|
unsigned char buf[DEV_BSIZE];
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
struct mbr_partition parts[MBR_PART_COUNT];
|
2000-12-24 16:57:37 +03:00
|
|
|
struct partition npe;
|
|
|
|
off_t loff;
|
|
|
|
int i, j, unused, changed;
|
2000-03-15 14:56:02 +03:00
|
|
|
|
2000-12-24 16:57:37 +03:00
|
|
|
changed = 0;
|
2000-03-15 14:56:02 +03:00
|
|
|
loff = (off_t)off * DEV_BSIZE;
|
|
|
|
|
|
|
|
if (lseek(sd, loff, SEEK_SET) != loff) {
|
|
|
|
perror("seek label");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-02-07 05:14:02 +03:00
|
|
|
if (read(sd, buf, sizeof buf) != DEV_BSIZE) {
|
2005-12-27 18:37:56 +03:00
|
|
|
if (off != MBR_BBSECTOR)
|
|
|
|
perror("read label (sector is possibly out of "
|
|
|
|
"range)");
|
|
|
|
else
|
|
|
|
perror("read label");
|
2000-03-15 14:56:02 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
if (getshort(buf + MBR_MAGIC_OFFSET) != MBR_MAGIC)
|
2000-12-24 16:57:37 +03:00
|
|
|
return (changed);
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
memcpy(parts, buf + MBR_PART_OFFSET, sizeof parts);
|
2000-12-24 16:57:37 +03:00
|
|
|
|
|
|
|
/* scan partition table */
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
for (i = 0; i < MBR_PART_COUNT; i++) {
|
|
|
|
if (parts[i].mbrp_type == 0 ||
|
2000-12-24 16:57:37 +03:00
|
|
|
/* extended partitions are handled below */
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
MBR_IS_EXTENDED(parts[i].mbrp_type))
|
2000-12-24 16:57:37 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
memset((void *)&npe, 0, sizeof(npe));
|
|
|
|
npe.p_size = getlong(&parts[i].mbrp_size);
|
|
|
|
npe.p_offset = getlong(&parts[i].mbrp_start) + off;
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
npe.p_fstype = xlat_mbr_fstype(parts[i].mbrp_type);
|
2000-12-24 16:57:37 +03:00
|
|
|
|
|
|
|
/* find existing entry, or first free slot */
|
|
|
|
unused = -1; /* flag as no free slot */
|
|
|
|
if (verbose)
|
2001-01-04 03:57:14 +03:00
|
|
|
printf(
|
|
|
|
"Found %s partition; size %u (%u MB), offset %u\n",
|
|
|
|
fstypenames[npe.p_fstype],
|
|
|
|
npe.p_size, npe.p_size / 2048, npe.p_offset);
|
2000-12-24 16:57:37 +03:00
|
|
|
for (j = 0; j < label.d_npartitions; j++) {
|
|
|
|
struct partition *lpe;
|
|
|
|
|
|
|
|
if (j == RAW_PART)
|
|
|
|
continue;
|
|
|
|
lpe = &label.d_partitions[j];
|
|
|
|
if (lpe->p_size == npe.p_size &&
|
|
|
|
lpe->p_offset == npe.p_offset
|
|
|
|
#ifdef notyet
|
|
|
|
&& (lpe->p_fstype == npe.p_fstype ||
|
|
|
|
lpe->p_fstype == FS_UNUSED) */
|
2000-03-15 14:56:02 +03:00
|
|
|
#endif
|
2000-12-24 16:57:37 +03:00
|
|
|
) {
|
|
|
|
if (verbose)
|
|
|
|
printf(
|
|
|
|
" skipping existing %s partition at slot %c.\n",
|
|
|
|
fstypenames[lpe->p_fstype],
|
|
|
|
j + 'a');
|
|
|
|
unused = -2; /* flag as existing */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (unused == -1 && lpe->p_size == 0 &&
|
|
|
|
lpe->p_fstype == FS_UNUSED)
|
|
|
|
unused = j;
|
|
|
|
}
|
|
|
|
if (unused == -2)
|
|
|
|
continue; /* entry exists, skip... */
|
|
|
|
if (unused == -1) {
|
|
|
|
if (label.d_npartitions < MAXPARTITIONS) {
|
|
|
|
unused = label.d_npartitions;
|
|
|
|
label.d_npartitions++;
|
|
|
|
} else {
|
|
|
|
printf(
|
|
|
|
" WARNING: no slots free for %s partition.\n",
|
|
|
|
fstypenames[npe.p_fstype]);
|
|
|
|
continue;
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
2000-12-24 16:57:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf(" adding %s partition to slot %c.\n",
|
|
|
|
fstypenames[npe.p_fstype], unused + 'a');
|
|
|
|
switch (npe.p_fstype) {
|
|
|
|
case FS_BSDFFS:
|
2002-09-28 04:56:25 +04:00
|
|
|
case FS_APPLEUFS:
|
2000-12-24 16:57:37 +03:00
|
|
|
npe.p_size = 16384; /* XXX */
|
|
|
|
npe.p_fsize = 1024;
|
|
|
|
npe.p_frag = 8;
|
|
|
|
npe.p_cpg = 16;
|
|
|
|
break;
|
|
|
|
#ifdef __does_not_happen__
|
|
|
|
case FS_BSDLFS:
|
|
|
|
npe.p_size = 16384; /* XXX */
|
|
|
|
npe.p_fsize = 1024;
|
|
|
|
npe.p_frag = 8;
|
|
|
|
npe.p_sgs = XXX;
|
2000-03-15 14:56:02 +03:00
|
|
|
break;
|
2000-12-24 16:57:37 +03:00
|
|
|
#endif
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
2000-12-24 16:57:37 +03:00
|
|
|
changed++;
|
|
|
|
label.d_partitions[unused] = npe;
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
2000-12-24 16:57:37 +03:00
|
|
|
|
|
|
|
/* recursively scan extended partitions */
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
for (i = 0; i < MBR_PART_COUNT; i++) {
|
2000-03-15 14:56:02 +03:00
|
|
|
u_int32_t poff;
|
|
|
|
|
Overhaul MBR handling (part 1):
<sys/bootblock.h>:
* Added definitions for the Master Boot Record (MBR) used by
a variety of systems (primarily i386), including the format
of the BIOS Parameter Block (BPB).
This information was cribbed from a variety of sources
including <sys/disklabel_mbr.h> which this is a superset of.
As part of this, some data structure elements and #defines
were renamed to be more "namespace friendly" and consistent
with other bootblocks and MBR documentation.
Update all uses of the old names to the new names.
<sys/disklabel_mbr.h>:
* Deprecated in favor of <sys/bootblock.h> (the latter is more
"host tool" friendly).
amd64 & i386:
* Renamed /usr/mdec/bootxx_dosfs to /usr/mdec/bootxx_msdos, to
be consistent with the naming convention of the msdosfs tools.
* Removed /usr/mdec/bootxx_ufs, as it's equivalent to bootxx_ffsv1
and it's confusing to have two functionally equivalent bootblocks,
especially given that "ufs" has multiple meanings (it could be
a synonym for "ffs", or the group of ffs/lfs/ext2fs file systems).
* Rework pbr.S (the first sector of bootxx_*):
+ Ensure that BPB (bytes 11..89) and the partition table
(bytes 446..509) do not contain code.
+ Add support for booting from FAT partitions if BOOT_FROM_FAT
is defined. (Only set for bootxx_msdos).
+ Remove "dummy" partition 3; if people want to installboot(8)
these to the start of the disk they can use fdisk(8) to
create a real MBR partition table...
+ Compile with TERSE_ERROR so it fits because of the above.
Whilst this is less user friendly, I feel it's important
to have a valid partition table and BPB in the MBR/PBR.
* Renamed /usr/mdec/biosboot to /usr/mdec/boot, to be consistent
with other platforms.
* Enable SUPPORT_DOSFS in /usr/mdec/boot (stage2), so that
we can boot off FAT partitions.
* Crank version of /usr/mdec/boot to 3.1, and fix some of the other
entries in the version file.
installboot(8) (i386):
* Read the existing MBR of the filesystem and retain the BIOS
Parameter Block (BPB) in bytes 11..89 and the MBR partition
table in bytes 446..509. (Previously installboot(8) would
trash those two sections of the MBR.)
mbrlabel(8):
* Use sys/lib/libkern/xlat_mbr_fstype.c instead of homegrown code
to map the MBR partition type to the NetBSD disklabel type.
Test built "make release" for i386, and new bootblocks verified to work
(even off FAT!).
2003-10-08 08:25:43 +04:00
|
|
|
if (MBR_IS_EXTENDED(parts[i].mbrp_type)) {
|
2000-12-24 16:57:37 +03:00
|
|
|
poff = getlong(&parts[i].mbrp_start) + extoff;
|
2001-02-18 06:36:07 +03:00
|
|
|
changed += getparts(sd, poff,
|
2000-12-24 16:57:37 +03:00
|
|
|
extoff ? extoff : poff, verbose);
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
|
|
|
}
|
2000-12-24 16:57:37 +03:00
|
|
|
return (changed);
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-12-24 04:50:29 +03:00
|
|
|
usage(void)
|
2000-03-15 14:56:02 +03:00
|
|
|
{
|
2005-12-27 18:37:56 +03:00
|
|
|
fprintf(stderr, "usage: %s [-fqrw] [-s sector] rawdisk\n",
|
|
|
|
getprogname());
|
2000-03-15 14:56:02 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2000-12-24 16:57:37 +03:00
|
|
|
|
2000-03-15 14:56:02 +03:00
|
|
|
int
|
2000-12-24 04:50:29 +03:00
|
|
|
main(int argc, char **argv)
|
2000-03-15 14:56:02 +03:00
|
|
|
{
|
2000-12-24 16:57:37 +03:00
|
|
|
int sd, ch, changed;
|
2005-12-27 18:37:56 +03:00
|
|
|
char *ep, name[MAXPATHLEN];
|
2000-12-24 16:57:37 +03:00
|
|
|
int force; /* force label update */
|
2000-12-27 07:22:11 +03:00
|
|
|
int raw; /* update on-disk label as well */
|
2000-12-24 16:57:37 +03:00
|
|
|
int verbose; /* verbose output */
|
2001-02-18 06:36:07 +03:00
|
|
|
int write_it; /* update in-core label if changed */
|
2005-12-27 18:37:56 +03:00
|
|
|
uint32_t sector; /* sector that contains the MBR */
|
2005-12-28 09:03:15 +03:00
|
|
|
ulong ulsector;
|
2000-03-15 14:56:02 +03:00
|
|
|
|
2000-12-24 16:57:37 +03:00
|
|
|
force = 0;
|
2000-12-27 07:22:11 +03:00
|
|
|
raw = 0;
|
2000-12-24 16:57:37 +03:00
|
|
|
verbose = 1;
|
2001-02-18 06:36:07 +03:00
|
|
|
write_it = 0;
|
2005-12-27 18:37:56 +03:00
|
|
|
sector = MBR_BBSECTOR;
|
|
|
|
while ((ch = getopt(argc, argv, "fqrs:w")) != -1) {
|
2000-12-24 16:57:37 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'f':
|
|
|
|
force = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
verbose = 0;
|
|
|
|
break;
|
2000-12-27 07:22:11 +03:00
|
|
|
case 'r':
|
|
|
|
raw = 1;
|
2005-12-27 18:37:56 +03:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
errno = 0;
|
2005-12-28 09:03:15 +03:00
|
|
|
ulsector = strtoul(optarg, &ep, 10);
|
2005-12-27 18:37:56 +03:00
|
|
|
if (optarg[0] == '\0' || *ep != '\0')
|
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"sector number (%s) incorrectly specified",
|
|
|
|
optarg);
|
2005-12-28 09:03:15 +03:00
|
|
|
if ((errno == ERANGE && ulsector == ULONG_MAX) ||
|
|
|
|
ulsector > UINT32_MAX)
|
2005-12-27 18:37:56 +03:00
|
|
|
errx(EXIT_FAILURE,
|
|
|
|
"sector number (%s) out of range",
|
|
|
|
optarg);
|
2005-12-28 09:03:15 +03:00
|
|
|
sector = (uint32_t)ulsector;
|
2000-12-27 07:22:11 +03:00
|
|
|
break;
|
|
|
|
case 'w':
|
2001-02-18 06:36:07 +03:00
|
|
|
write_it = 1;
|
2000-12-27 07:22:11 +03:00
|
|
|
break;
|
2000-12-24 16:57:37 +03:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 1)
|
2000-03-15 14:56:02 +03:00
|
|
|
usage();
|
|
|
|
|
2002-05-21 05:39:56 +04:00
|
|
|
if ((sd = opendisk(argv[0], write_it ? O_RDWR : O_RDONLY, name,
|
|
|
|
(size_t)MAXPATHLEN, 0)) < 0) {
|
2000-12-24 16:57:37 +03:00
|
|
|
perror(argv[0]);
|
2000-03-15 14:56:02 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
getlabel(sd);
|
2005-12-27 18:37:56 +03:00
|
|
|
changed = getparts(sd, sector, 0, verbose);
|
2000-12-24 16:57:37 +03:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
putchar('\n');
|
|
|
|
showpartitions(stdout, &label, 0);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2001-02-18 06:36:07 +03:00
|
|
|
if (write_it) {
|
2000-12-27 07:22:11 +03:00
|
|
|
if (! changed && ! force)
|
2000-12-24 16:57:37 +03:00
|
|
|
printf("No change; not updating disk label.\n");
|
|
|
|
else {
|
|
|
|
if (verbose)
|
2000-12-27 07:22:11 +03:00
|
|
|
printf("Updating in-core %sdisk label.\n",
|
|
|
|
raw ? "and on-disk " : "");
|
|
|
|
setlabel(sd, raw);
|
2000-12-24 16:57:37 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("Not updating disk label.\n");
|
|
|
|
}
|
2000-03-15 14:56:02 +03:00
|
|
|
close(sd);
|
2000-12-24 16:57:37 +03:00
|
|
|
return (0);
|
2000-03-15 14:56:02 +03:00
|
|
|
}
|