Skip to content

Commit

Permalink
feat: mark MBR bootable
Browse files Browse the repository at this point in the history
Similar to bec914f but make it optional
instead.

Signed-off-by: Artem Chernyshev <artem.0xD2@gmail.com>
  • Loading branch information
Unix4ever authored and talos-bot committed May 26, 2021
1 parent 1292574 commit 30c2bc3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
28 changes: 19 additions & 9 deletions blockdevice/partition/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ func (outOfSpaceError) OutOfSpaceError() {}

// GPT represents the GUID Partition Table.
type GPT struct {
f *os.File
l *lba.LBA
h *Header
e *Partitions
f *os.File
l *lba.LBA
h *Header
e *Partitions
markMBRBootable bool
}

// Open attempts to open a partition table on f.
Expand Down Expand Up @@ -117,10 +118,11 @@ func New(f *os.File, setters ...Option) (g *GPT, err error) {
h.GUUID = guuid

g = &GPT{
f: f,
l: l,
h: h,
e: &Partitions{h: h, devname: f.Name()},
f: f,
l: l,
h: h,
e: &Partitions{h: h, devname: f.Name()},
markMBRBootable: opts.MarkMBRBootable,
}

return g, nil
Expand Down Expand Up @@ -344,6 +346,7 @@ func (g *GPT) Repair() error {
// - https://en.wikipedia.org/wiki/GUID_Partition_Table#Protective_MBR_(LBA_0)
// - https://www.syslinux.org/wiki/index.php?title=Doc/gpt
// - https://en.wikipedia.org/wiki/Master_boot_record
// - http://www.rodsbooks.com/gdisk/bios.html
func (g *GPT) newPMBR(h *Header) ([]byte, error) {
p, err := g.l.ReadAt(0, 0, 512)
if err != nil {
Expand All @@ -355,7 +358,14 @@ func (g *GPT) newPMBR(h *Header) ([]byte, error) {

// PMBR protective entry.
b := p[446 : 446+16]
b[0] = 0x00

if g.markMBRBootable {
// Some BIOSes in legacy mode won't boot from a disk unless there is at least one
// partition in the MBR marked bootable. Mark this partition as bootable.
b[0] = 0x80
} else {
b[0] = 0x00
}

// Partition type: EFI data partition.
b[4] = 0xee
Expand Down
10 changes: 10 additions & 0 deletions blockdevice/partition/gpt/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "fmt"
// Options is the functional options struct.
type Options struct {
PartitionEntriesStartLBA uint64
MarkMBRBootable bool
}

// Option is the functional option func.
Expand All @@ -27,6 +28,15 @@ func WithPartitionEntriesStartLBA(o uint64) Option {
}
}

// WithMarkMBRBootable marks MBR partition as bootable.
func WithMarkMBRBootable(value bool) Option {
return func(args *Options) error {
args.MarkMBRBootable = value

return nil
}
}

// NewDefaultOptions initializes an `Options` struct with default values.
func NewDefaultOptions(setters ...Option) (*Options, error) {
opts := &Options{
Expand Down

0 comments on commit 30c2bc3

Please sign in to comment.