| 1 | // Copyright 2020 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3 with static-linking exception. |
| 3 | // See LICENCE file for details. |
| 4 | |
| 5 | package mbrscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/ |
| 6 | |
| 7 | import ( |
| 8 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/" |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 10 | "ioscip-go gomod github.com/golang/go/src go1.18 io/" |
| 11 | ) |
| 12 | |
| 13 | const mbrSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/mbrSignature. = 0xaa55 |
| 14 | |
| 15 | var ErrInvalidSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/ErrInvalidSignature. = errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("invalid master boot record signature") |
| 16 | |
| 17 | // Address is a CHS address. |
| 18 | type Addressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address# [3]uint8 |
| 19 | |
| 20 | func (a Addressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address#) Headscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address#Head().() uint8 { |
| 21 | return a[0] |
| 22 | } |
| 23 | |
| 24 | func (a Addressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address#) Sectorscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address#Sector().() uint8 { |
| 25 | return a[1] & 0x3f |
| 26 | } |
| 27 | |
| 28 | func (a Addressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address#) Cylinderscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address#Cylinder().() uint16 { |
| 29 | c := uint16(a[2]) |
| 30 | c |= uint16(a[1]&0xc0) << 2 |
| 31 | return c |
| 32 | } |
| 33 | |
| 34 | // PartitionEntry corresponds to a partition entry from a MBR. |
| 35 | type PartitionEntryscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry# struct { |
| 36 | BootIndicatorscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#BootIndicator. uint8 |
| 37 | StartAddressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#StartAddress. Addressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address# |
| 38 | Typescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#Type. uint8 |
| 39 | EndAddressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#EndAddress. Addressscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Address# |
| 40 | StartingLBAscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#StartingLBA. uint32 |
| 41 | NumberOfSectorsscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#NumberOfSectors. uint32 |
| 42 | } |
| 43 | |
| 44 | // Record corresponds to a MBR. |
| 45 | type Recordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record# struct { |
| 46 | BootstrapCodescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#BootstrapCode. [440]byte |
| 47 | UniqueSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#UniqueSignature. uint32 |
| 48 | Partitionsscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#Partitions. [4]PartitionEntryscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry# |
| 49 | } |
| 50 | |
| 51 | // IsProtectiveMBR indicates whether this is a PMBR, which is present |
| 52 | // in the first sector of a GPT volume. |
| 53 | func (r *Recordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#) IsProtectiveMBRscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#IsProtectiveMBR().() bool { |
| 54 | var maybePMBR bool |
| 55 | for _, p := range r.Partitionsscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#Partitions. { |
| 56 | switch p.Typescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry#Type. { |
| 57 | case 0x00: |
| 58 | // This is ok for a PMBR |
| 59 | case 0xee: |
| 60 | // Maybe a PMBR. |
| 61 | // XXX: We probably should check the geometry. |
| 62 | switch { |
| 63 | case maybePMBR: |
| 64 | // There should only be one of these. |
| 65 | return false |
| 66 | default: |
| 67 | maybePMBR = true |
| 68 | } |
| 69 | default: |
| 70 | // This is not a valid PMBR |
| 71 | return false |
| 72 | } |
| 73 | } |
| 74 | return maybePMBR |
| 75 | } |
| 76 | |
| 77 | type recordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record# struct { |
| 78 | BootstrapCodescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#BootstrapCode. [440]byte |
| 79 | UniqueSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#UniqueSignature. uint32 |
| 80 | Unknownscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#Unknown. [2]uint8 |
| 81 | Partitionsscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#Partitions. [4]PartitionEntryscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/PartitionEntry# |
| 82 | Signaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#Signature. uint16 |
| 83 | } |
| 84 | |
| 85 | // ReadRecord reads a MBR from r. It returns ErrInvalidSignature if the |
| 86 | // MBR has an invalid signature. |
| 87 | func ReadRecordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/ReadRecord().(r ioscip-go gomod github.com/golang/go/src go1.18 io/.Readerscip-go gomod github.com/golang/go/src go1.18 io/Reader#) (*Recordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#, error) { |
| 88 | var rec recordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record# |
| 89 | if err := binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.Readscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/Read().(r, binaryscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.18 `encoding/binary`/LittleEndian., &rec); err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | if rec.Signaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#Signature. != mbrSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/mbrSignature. { |
| 93 | return nil, ErrInvalidSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/ErrInvalidSignature. |
| 94 | } |
| 95 | return &Recordscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#{BootstrapCodescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#BootstrapCode.: rec.BootstrapCodescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#BootstrapCode., |
| 96 | UniqueSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#UniqueSignature.: rec.UniqueSignaturescip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#UniqueSignature., |
| 97 | Partitionsscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/Record#Partitions.: rec.Partitionsscip-go gomod github.com/canonical/go-efilib . `github.com/canonical/go-efilib/mbr`/record#Partitions.}, nil |
| 98 | } |