History log of /openbsd-current/sbin/fdisk/mbr.h
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.44 26-Jul-2022 krw

Allow editing of an MBR of all zeros.

Fallout from regress failure spotted by anton@.


# 1.43 25-Jul-2022 krw

Only MBR and GPT structures can be edited by fdisk.

If neither are found, restrict user actions to printing basic
information on the disk, reading the man page, initializing an
MBR or GPT, or terminating fdisk without changing anything.

Feedback on earlier attempt by miod@ and brynet@.


Revision tags: OPENBSD_7_1_BASE
# 1.42 25-Oct-2021 krw

Nuke a bunch of pointless #ifndef _<.h file>/#endif guards.


Revision tags: OPENBSD_7_0_BASE
# 1.41 06-Aug-2021 krw

Cleanup, clarify and generally polish the MBR/GPT initialization
code.

'-g' is promoted to be independant of '-i'. This makes it clearer
that there are four mutually exclusive initialization options.

'-i' puts the default MBR on disk, '-g' puts the default GPT on
disk, '-u' updates the MBR boot code on disk and '-A' puts a new
set of GPT partitions on disk without overwriting 'protected'
partitions.

The last initialization option specified is the one executed, so
existing '-i -g' finger memory, etc. continue to work as before.

man page/usage feedback/tweaks from jmc@


# 1.40 21-Jul-2021 krw

Use a dedicated struct mbr to hold the GPT protective MBR,
either the one read from disk or the default one created
when initializing GPT.

Simplifies logic, eliminates MBR_init_GPT() and demotes
MBR_protective_mbr() to a helper function called from
GPT_read().

Nuke an unused variable in passing.

No intentional functional change.


# 1.39 19-Jul-2021 krw

MBR_protective_mbr() can take a const struct mbr *.

No functional change.


# 1.38 19-Jul-2021 krw

An MBR knows (mbr_lba_self) where it is supposed to go, no
need to pass the value to MBR_write().

Let MBR_write() do the translation from struct mbr to the
struct dos_mbr that will be written to the disk. Thus
eliminating unnecessary struct dos_mbr variables and the
parsing thereof.

No intentional functional change.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.43 25-Jul-2022 krw

Only MBR and GPT structures can be edited by fdisk.

If neither are found, restrict user actions to printing basic
information on the disk, reading the man page, initializing an
MBR or GPT, or terminating fdisk without changing anything.

Feedback on earlier attempt by miod@ and brynet@.


Revision tags: OPENBSD_7_1_BASE
# 1.42 25-Oct-2021 krw

Nuke a bunch of pointless #ifndef _<.h file>/#endif guards.


Revision tags: OPENBSD_7_0_BASE
# 1.41 06-Aug-2021 krw

Cleanup, clarify and generally polish the MBR/GPT initialization
code.

'-g' is promoted to be independant of '-i'. This makes it clearer
that there are four mutually exclusive initialization options.

'-i' puts the default MBR on disk, '-g' puts the default GPT on
disk, '-u' updates the MBR boot code on disk and '-A' puts a new
set of GPT partitions on disk without overwriting 'protected'
partitions.

The last initialization option specified is the one executed, so
existing '-i -g' finger memory, etc. continue to work as before.

man page/usage feedback/tweaks from jmc@


# 1.40 21-Jul-2021 krw

Use a dedicated struct mbr to hold the GPT protective MBR,
either the one read from disk or the default one created
when initializing GPT.

Simplifies logic, eliminates MBR_init_GPT() and demotes
MBR_protective_mbr() to a helper function called from
GPT_read().

Nuke an unused variable in passing.

No intentional functional change.


# 1.39 19-Jul-2021 krw

MBR_protective_mbr() can take a const struct mbr *.

No functional change.


# 1.38 19-Jul-2021 krw

An MBR knows (mbr_lba_self) where it is supposed to go, no
need to pass the value to MBR_write().

Let MBR_write() do the translation from struct mbr to the
struct dos_mbr that will be written to the disk. Thus
eliminating unnecessary struct dos_mbr variables and the
parsing thereof.

No intentional functional change.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.42 25-Oct-2021 krw

Nuke a bunch of pointless #ifndef _<.h file>/#endif guards.


Revision tags: OPENBSD_7_0_BASE
# 1.41 06-Aug-2021 krw

Cleanup, clarify and generally polish the MBR/GPT initialization
code.

'-g' is promoted to be independant of '-i'. This makes it clearer
that there are four mutually exclusive initialization options.

'-i' puts the default MBR on disk, '-g' puts the default GPT on
disk, '-u' updates the MBR boot code on disk and '-A' puts a new
set of GPT partitions on disk without overwriting 'protected'
partitions.

The last initialization option specified is the one executed, so
existing '-i -g' finger memory, etc. continue to work as before.

man page/usage feedback/tweaks from jmc@


# 1.40 21-Jul-2021 krw

Use a dedicated struct mbr to hold the GPT protective MBR,
either the one read from disk or the default one created
when initializing GPT.

Simplifies logic, eliminates MBR_init_GPT() and demotes
MBR_protective_mbr() to a helper function called from
GPT_read().

Nuke an unused variable in passing.

No intentional functional change.


# 1.39 19-Jul-2021 krw

MBR_protective_mbr() can take a const struct mbr *.

No functional change.


# 1.38 19-Jul-2021 krw

An MBR knows (mbr_lba_self) where it is supposed to go, no
need to pass the value to MBR_write().

Let MBR_write() do the translation from struct mbr to the
struct dos_mbr that will be written to the disk. Thus
eliminating unnecessary struct dos_mbr variables and the
parsing thereof.

No intentional functional change.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.41 06-Aug-2021 krw

Cleanup, clarify and generally polish the MBR/GPT initialization
code.

'-g' is promoted to be independant of '-i'. This makes it clearer
that there are four mutually exclusive initialization options.

'-i' puts the default MBR on disk, '-g' puts the default GPT on
disk, '-u' updates the MBR boot code on disk and '-A' puts a new
set of GPT partitions on disk without overwriting 'protected'
partitions.

The last initialization option specified is the one executed, so
existing '-i -g' finger memory, etc. continue to work as before.

man page/usage feedback/tweaks from jmc@


# 1.40 21-Jul-2021 krw

Use a dedicated struct mbr to hold the GPT protective MBR,
either the one read from disk or the default one created
when initializing GPT.

Simplifies logic, eliminates MBR_init_GPT() and demotes
MBR_protective_mbr() to a helper function called from
GPT_read().

Nuke an unused variable in passing.

No intentional functional change.


# 1.39 19-Jul-2021 krw

MBR_protective_mbr() can take a const struct mbr *.

No functional change.


# 1.38 19-Jul-2021 krw

An MBR knows (mbr_lba_self) where it is supposed to go, no
need to pass the value to MBR_write().

Let MBR_write() do the translation from struct mbr to the
struct dos_mbr that will be written to the disk. Thus
eliminating unnecessary struct dos_mbr variables and the
parsing thereof.

No intentional functional change.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.40 21-Jul-2021 krw

Use a dedicated struct mbr to hold the GPT protective MBR,
either the one read from disk or the default one created
when initializing GPT.

Simplifies logic, eliminates MBR_init_GPT() and demotes
MBR_protective_mbr() to a helper function called from
GPT_read().

Nuke an unused variable in passing.

No intentional functional change.


# 1.39 19-Jul-2021 krw

MBR_protective_mbr() can take a const struct mbr *.

No functional change.


# 1.38 19-Jul-2021 krw

An MBR knows (mbr_lba_self) where it is supposed to go, no
need to pass the value to MBR_write().

Let MBR_write() do the translation from struct mbr to the
struct dos_mbr that will be written to the disk. Thus
eliminating unnecessary struct dos_mbr variables and the
parsing thereof.

No intentional functional change.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.39 19-Jul-2021 krw

MBR_protective_mbr() can take a const struct mbr *.

No functional change.


# 1.38 19-Jul-2021 krw

An MBR knows (mbr_lba_self) where it is supposed to go, no
need to pass the value to MBR_write().

Let MBR_write() do the translation from struct mbr to the
struct dos_mbr that will be written to the disk. Thus
eliminating unnecessary struct dos_mbr variables and the
parsing thereof.

No intentional functional change.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.37 18-Jul-2021 krw

Don't save the prt_scyl/prt_ecyl values, change them, use them
and then restore them. Just change/use the saved values and skip
the restoring.

Allows PRT_make() to add 'const' to its struct mbr parameter, and
thus allows MBR_make() to add 'const' to its struct mbr
parameter.

No intentional functional change.


# 1.36 18-Jul-2021 krw

Put MBR_parse() invocation inside MBR_read() instead of always
following an invocation of MBR_read() with a call to MBR_parse().

No functional change.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.35 13-Jul-2021 krw

Disk sector addresses are normally stored/provided in uint64_t
variables/parameters, not off_t.

Adjust various parameters and variables accordingly. A few missed const's
and verbiage tweaks in passing.

No functional change.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.34 12-Jul-2021 krw

Toss 'const' in anywhere the compiler doesn't complain about, thus identifying
places that *should* be const but currently aren't and preventing the rot from
spreading further while those issues are dealt with.

No functional change.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.33 11-Jul-2021 krw

Add 'mbr_' prefix to struct mbr field names.

Change 'offset' to mbr_lba_self and 'reloffset'
to mbr_lba_firstembr to make their use more evident.
Adjust a few parameter names to match.

Change 'part[]' to mbr_prt[] to reflect that it is an
array of struct prt.

No functional change.


# 1.32 11-Jul-2021 krw

Add tabs to make struct definitions and function prototypes easier
to read.

No functional change.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.31 16-Jun-2021 krw

Ensure that the '-i' command line option and the 'reinit [mbr]'
editing commnd completely remove any existing on-disk GPT header
when writing the MBR.

'-ig'/'reinit gpt' must be used to create GPT format.

Previous interactions between '-i', '-b' and overly clever
'reinit' produced surprising, inconsistent and incorrect
behaviour.

ok deraadt@ kettenis@


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.30 20-May-2021 krw

Nuke duplicate declaration of MBR_init_GPT().


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


# 1.29 07-May-2021 krw

Replace random mix of u_int* and uint* types with consistent uint* types.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE OPENBSD_6_3_BASE OPENBSD_6_4_BASE OPENBSD_6_5_BASE OPENBSD_6_6_BASE OPENBSD_6_7_BASE OPENBSD_6_8_BASE OPENBSD_6_9_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.


Revision tags: OPENBSD_5_9_BASE OPENBSD_6_0_BASE OPENBSD_6_1_BASE OPENBSD_6_2_BASE
# 1.28 18-Nov-2015 krw

Parse the original on-disk MBR into a local variable. Use the
partition table in this variable when doing an 'u'pdate of the MBR.
Remove now superfluous MBR_pcopy() and its re-read of the on-disk MBR.


# 1.27 13-Nov-2015 krw

Move from opening/closing disk for every i/o to opening the disk once
and saving the fd in the global 'disk' structure. Stop passing around
fd's and just use the global.

Makes pledge() feasible.

Prompted by and ok deraadt@


# 1.26 26-Oct-2015 krw

Add GPT editing. Based on GSOC 2014 work by Markus Muller.


# 1.25 05-Oct-2015 krw

Enhance '-g' to create a default GPT label in addition to the protective
MBR. If '-b' is specified an EFI System partition of the requested size is
created. All remaining space is put into an OpenBSD partition.

Minimal enhancement necessary for upcoming UEFI install support.
Committed first to flush out any unexpected impacts on 'normal' MBR
operation and install media.

ok deraadt@


# 1.24 19-Aug-2015 krw

Missing prototypes. Noted by deraadt@


Revision tags: OPENBSD_5_8_BASE
# 1.23 30-Mar-2015 krw

Nuke declaration of unused and non-existant function MBR_print_disk().


# 1.22 29-Mar-2015 krw

Rename MBR_readsector() and MBR_writesector() to just readsector()
and writesector(). Move them to misc.[ch]. Soon to be used for more
than MBR reading/writing.

No intentional functional change.


# 1.21 18-Mar-2015 krw

Stop passing around little used 'struct mbr tt' parameter. Just
make the initial mbr that tt pointed at a global that can be directly
accessed in the couple of places it is needed.

Fewer parameters, less confusion, no functional change.


# 1.20 16-Mar-2015 krw

Stop passing around a pointer to the stack variable 'disk' in main().
There is only one disk being worked on, so just make it a global.

Fewer parameters, less confusion, no functional change.


# 1.19 14-Mar-2015 krw

Switch all the license blocks to the standard OpenBSD/ISC license.

With the permission of Toby.

ok deraadt@


Revision tags: OPENBSD_5_7_BASE
# 1.18 10-Feb-2015 krw

If 'write' is issued after a 'reinit' command, and the MBR to be
written does not contain an EFI partition, zap any GPT signature
found on the 2nd (LBA 1) and last sectors on the disk. The install
script uses this sequence.

This is a temporary workaround until more GPT support is imported
post-5.7.

Should fix at least some machines who insist on using the GPT even
after we have overritten the 'protective' MBR.

ok deraadt@ tedu@ beck@ millert@


Revision tags: OPENBSD_5_6_BASE
# 1.17 23-Mar-2014 krw

Change struct mbr signature field type from 'unsigned short' to
'u_int16_t' to be consistant with disklabel.h's struct dos_mbr.


# 1.16 17-Mar-2014 krw

Nuke pointless blank lines, defines, comments and casts. Eliminate
#include in *.h files in favour of listing them as required in the *.c
files. Fix error message to correctly state that 64 is the minimum
value for -l. Use errx() where errno is not relevant. Use 'continue'
rather than a label to go back to start of a loop.

No intentional functional change.


# 1.15 09-Mar-2014 krw

Instead of passing around 'char buf[DEV_BSIZE]' buffers, pass around
'struct dos_mbr' variables, since that is what the buffers were used
for. Removes need to know about DEV_BSIZE and thus include param.h
from all files but one so move the param.h #include to that file
(mbr.c).

Nuke a bunch of local MBR #defines in favour of the disklabel.h ones.

Remove a bunch of unneeded #includes, replace the odd malloc/bzero
with calloc, replace equally odd bcopy's with memcpy, remove a stray
duplicate MBR parsing in MBR_pcopy().

No intentional functional change.


# 1.14 08-Mar-2014 krw

It's silly to check at compile time for DEV_BSIZE == 512.

ok deraadt@ with gritted teeth on keeping <sys/param.h> for now.


# 1.13 07-Mar-2014 krw

Relieve the code of an overburden of unnecessary typedef
abstraction. Call a 'struct' a 'struct' and not a pony.

No functional change.

idea ok deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.12 21-Mar-2013 deraadt

I don't feel great about adding an include for <sys/param.h>, but I feel
great for removing an include of <machine/param.h>
This needs further cleanup if someone has time.


Revision tags: OPENBSD_3_4_BASE OPENBSD_3_5_BASE OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE OPENBSD_3_9_BASE OPENBSD_4_0_BASE OPENBSD_4_1_BASE OPENBSD_4_2_BASE OPENBSD_4_3_BASE OPENBSD_4_4_BASE OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE OPENBSD_4_8_BASE OPENBSD_4_9_BASE OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE OPENBSD_5_3_BASE
# 1.11 03-Jun-2003 weingart

Nuke terms 3 & 4.


Revision tags: OPENBSD_3_1_BASE OPENBSD_3_2_BASE OPENBSD_3_3_BASE
# 1.10 16-Feb-2002 millert

Part one of userland __P removal. Done with a simple regexp with some minor hand editing to make comments line up correctly. Another pass is forthcoming that handles the cases that could not be done automatically.


# 1.9 18-Jan-2002 kjell

Allow size values to be displayed in the user's choice of units.
Idea borrowed from disklabel. i.e. Users can now type "print M"
and be rewarded with partition sizes in Megs


# 1.8 04-Jan-2002 kjell

Remove references to the NT serial number. Now the first 0x1be
bytes of the MBR are treated as code for the purposes of the
(u)pdate command. This is consistent with the new -u flag.
This means boot managers like BootEasy can now be written
out correctly. Note that rewriting the MBR now whacks the
NT serial number - a behavior that is consistent with
the DOS FDISK /MBR.


# 1.7 15-Dec-2001 kjell

Get this damn diff out of my tree. ok from weingart@, deraadt@ and others.
Add a -u flag to fdisk which updates MBR code, but leaves the partition
table intact. There are still issues with 'update' having different behavior,
but I'll resolve those shortly


Revision tags: OPENBSD_3_0_BASE
# 1.6 12-Aug-2001 heko

#(endif|else) foo is incorrect, make it #endif /* foo */
deraadt@ ok


Revision tags: OPENBSD_2_9_BASE
# 1.5 28-Jan-2001 weingart

Quick -Wall fixes, ok niklas@, aaron@


Revision tags: OPENBSD_2_4_BASE OPENBSD_2_5_BASE OPENBSD_2_6_BASE OPENBSD_2_7_BASE OPENBSD_2_8_BASE
# 1.4 14-Sep-1998 rahnds

Since on the powerpc, the system does not supply the user with
bios valid cyl,head,sector information (the information comes from the
sd driver) I have made some changes to fdisk to do the following.

Allow the powerpc to specify values larger than the bios limits.

All platforms now have code that will translate the LBA values
in the mbr into CHS values according to the disk geometry.
This occurs if the start and ending CHS values have been stored as
0xffffff.

If writing to the disk and one of the values of a partition violates
the bios limits, it writes the requested values in the LBA fields ,
and stores 0xffffff for the starting and ending CHS values.

This should not change the default formatting of any existing system
other than the CHS and LBA values should always match given the detected
geometry of the disk.


Revision tags: OPENBSD_2_2_BASE OPENBSD_2_3_BASE
# 1.3 21-Oct-1997 provos

make fdisk grok extended partitions again. we have to keep track of the
absolute offset of the current mbr and its relative position. because:
the starting sector of the first mbr entry in an extended partition is relative
to the starting offset of the whole mbr itself. the starting offset of a new
extended partition is relative to the offset of the very first extended
partition.


# 1.2 29-Sep-1997 mickey

ID in the first line w/ tabs


# 1.1 29-Sep-1997 weingart

New fdisk code with interactive (command line type)
editing code. Rewrite from the ground up, save about
20 lines of code. Seems to create valid partition
tables on i386 and alphas.