1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
// Copyright 2022 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package linux
import (
"fmt"
"os"
efi "github.com/canonical/go-efilib"
)
// ReadPartitionTable reads a complete GUID partition table from the supplied
// device path.
//
// This function expects the device to have a valid protective MBR.
//
// If role is efi.PrimaryPartitionTable, this will read the primary partition
// table that is located immediately after the protective MBR. If role is
// efi.BackupPartitionTable, this will read the backup partition table that is
// located at the end of the device.
//
// If checkCrc is true and either CRC check fails for the requested table, an
// error will be returned. Setting checkCrc to false disables the CRC checks.
//
// Note that whilst this function checks the integrity of the header and
// partition table entries, it does not check the contents of the partition
// table entries.
//
// If role is efi.BackupPartitionTable and the backup table is not located at
// the end of the device, this will return efi.ErrInvalidBackupPartitionTableLocation
// along with the valid table.
func ReadPartitionTable(path string, role efi.PartitionTableRole, checkCrc bool) (*efi.PartitionTable, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
sz, err := getDeviceSize(f)
if err != nil {
return nil, fmt.Errorf("cannot determine device size: %w", err)
}
ssz, err := getSectorSize(f)
if err != nil {
return nil, fmt.Errorf("cannot determine logical sector size: %w", err)
}
return efi.ReadPartitionTable(f, sz, ssz, role, checkCrc)
}
|