github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/mount/scuzz/control.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package scuzz
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"time"
    11  )
    12  
    13  // DefaultTimeout is the default timeout for disk operations.
    14  const DefaultTimeout time.Duration = 15 * time.Second
    15  
    16  const (
    17  	securitySupported    DiskSecurityStatus = 0x1
    18  	securityEnabled      DiskSecurityStatus = 0x2
    19  	securityLocked       DiskSecurityStatus = 0x4
    20  	securityFrozen       DiskSecurityStatus = 0x8
    21  	securityCountExpired DiskSecurityStatus = 0x10
    22  	securityLevelMax     DiskSecurityStatus = 0x100
    23  )
    24  
    25  var securityStatusStrings = map[DiskSecurityStatus]string{
    26  	securitySupported:    "SUPPORTED",
    27  	securityEnabled:      "ENABLED",
    28  	securityLocked:       "LOCKED",
    29  	securityFrozen:       "FROZEN",
    30  	securityCountExpired: "COUNT EXPIRED",
    31  	securityLevelMax:     "LEVEL MAX",
    32  }
    33  
    34  // Info is information about a SCSI disk device.
    35  type Info struct {
    36  	NumberSectors           uint64
    37  	ECCBytes                uint
    38  	MasterRevision          uint16 `json:"MasterPasswordRevision"`
    39  	SecurityStatus          DiskSecurityStatus
    40  	TrustedComputingSupport uint16
    41  
    42  	Serial           string
    43  	Model            string
    44  	FirmwareRevision string
    45  
    46  	// These are the pair-byte-swapped space-padded versions of serial,
    47  	// model, and firmware revision as originally returned by the SCSI
    48  	// device.
    49  	OrigSerial           string
    50  	OrigModel            string
    51  	OrigFirmwareRevision string
    52  }
    53  
    54  // Disk is the interface to a disk, with operations to create packets and
    55  // operate on them.
    56  type Disk interface {
    57  	// Unlock unlocks the drive, given a password and an indication of
    58  	// whether it is the admin (true) or user (false) password.
    59  	Unlock(password string, admin bool) error
    60  
    61  	// Identify returns drive identity information
    62  	Identify() (*Info, error)
    63  }
    64  
    65  // DiskSecurityStatus is information about how the disk is secured.
    66  type DiskSecurityStatus uint16
    67  
    68  // SecuritySupported returns true if the disk has security.
    69  func (d DiskSecurityStatus) SecuritySupported() bool {
    70  	return (d & securitySupported) != 0
    71  }
    72  
    73  // SecurityEnabled returns true if security is enabled on the disk.
    74  func (d DiskSecurityStatus) SecurityEnabled() bool {
    75  	return (d & securityEnabled) != 0
    76  }
    77  
    78  // SecurityLocked returns true if the disk is locked.
    79  func (d DiskSecurityStatus) SecurityLocked() bool {
    80  	return (d & securityLocked) != 0
    81  }
    82  
    83  // SecurityFrozen returns true if the disk is frozen and its security state
    84  // cannot be changed.
    85  func (d DiskSecurityStatus) SecurityFrozen() bool {
    86  	return (d & securityFrozen) != 0
    87  }
    88  
    89  // SecurityCountExpired returns true if all attempts to unlock the disk have
    90  // been used up.
    91  func (d DiskSecurityStatus) SecurityCountExpired() bool {
    92  	return (d & securityCountExpired) != 0
    93  }
    94  
    95  func (d DiskSecurityStatus) String() string {
    96  	s := "Security Status: "
    97  	for v, name := range securityStatusStrings {
    98  		if d&v != 0 {
    99  			s += name + ", "
   100  		}
   101  	}
   102  	return s
   103  }
   104  
   105  // String prints a nice JSON-formatted info.
   106  func (i *Info) String() string {
   107  	s, err := json.MarshalIndent(i, "", "\t")
   108  	if err != nil {
   109  		return fmt.Sprintf("%v", err)
   110  	}
   111  	return string(s)
   112  }