Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions cmd/gosedctl/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"crypto/sha1"
"fmt"
"github.com/davecgh/go-spew/spew"
"os"

"github.com/open-source-firmware/go-tcg-storage/pkg/core"
Expand Down Expand Up @@ -60,6 +61,17 @@
SIDPassword string `flag:"" required:"" short:"p" help:"Password to SID authority"`
}

type readDataStoreCmd struct {
Device string `flag:"" required:"" short:"d" help:"Path to SED device (e.g. /dev/nvme0)"`
Password string `flag:"" required:"" short:"p"`
}

type writeDataStoreCmd struct {
Path string `flag:"" required:"" short:"i" help:"Path to DataStore content"`
Device string `flag:"" required:"" short:"d" help:"Path to SED device (e.g. /dev/nvme0)"`
Password string `flag:"" required:"" short:"p"`
}

// cli is the main command line interface struct required by kong command line parser
var cli struct {
InitialSetup initialSetupCmd `cmd:"" help:"Take ownership of a given OPAL SSC device"`
Expand All @@ -70,6 +82,8 @@
RevertEnterprise resetDeviceEnterprise `cmd:"" help:"delete after use"`
UnlockEnterprise unlockEnterprise `cmd:"" help:"Unlocks global range with BandMaster0"`
ResetSID resetSIDcmd `cmd:"" help:"Resets the SID PIN to MSID"`
ReadDataStore readDataStoreCmd `cmd:"" help:"Reads the DataStore table"`
WriteDataStore writeDataStoreCmd `cmd:"" help:"Writes to DataStore table"`
}

// Run executes when the initial-setup command is invoked
Expand Down Expand Up @@ -600,3 +614,114 @@

return nil
}

func (l *readDataStoreCmd) Run(ctx *context) error {
if l.Password == "" {
return fmt.Errorf("empty password not allowed")
}

Check warning on line 621 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L618-L621

Added lines #L618 - L621 were not covered by tests

coreObj, err := core.NewCore(l.Device)
if err != nil {
return fmt.Errorf("NewCore() failed: %v", err)
}

Check warning on line 626 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L623-L626

Added lines #L623 - L626 were not covered by tests

comID, _, err := core.FindComID(coreObj.DriveIntf, coreObj.DiskInfo.Level0Discovery)
if err != nil {
return fmt.Errorf("FindComID() failed: %v", err)
}
cs, err := core.NewControlSession(coreObj.DriveIntf, coreObj.Level0Discovery, core.WithComID(comID))
if err != nil {
return fmt.Errorf("NewControllSession() failed: %v", err)
}

Check warning on line 635 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L628-L635

Added lines #L628 - L635 were not covered by tests

serial, err := coreObj.SerialNumber()
if err != nil {
return fmt.Errorf("coreObj.SerialNumber() failed: %v", err)
}
salt := fmt.Sprintf("%-20s", serial)
pwhash := pbkdf2.Key([]byte(l.Password), []byte(salt[:20]), 75000, 32, sha1.New)

lockingSession, err := cs.NewSession(uid.LockingSP)
if err != nil {
return fmt.Errorf("NewSession() to LockingSP failed: %v", err)
}
defer lockingSession.Close()
// Elevate the session to Admin1 with required credentials
if err := table.ThisSP_Authenticate(lockingSession, uid.LockingAuthorityAdmin1, pwhash); err != nil {
return fmt.Errorf("authenticating as Admin1 failed: %v", err)
}

Check warning on line 652 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L637-L652

Added lines #L637 - L652 were not covered by tests

info, err := table.DataStoreTableInfo(lockingSession)
if err != nil {
return fmt.Errorf("DataStoreTableInfo() failed: %v", err)
} else {
spew.Dump(info)
}
var buf = make([]byte, 4096)
read, err := table.DataStoreRead(lockingSession, buf, 0, uint(info.RecommendedAccessGranularity))
if err != nil {
return fmt.Errorf("DataStoreRead() failed: %v", err)
}
fmt.Printf("DataStoreRead() red %d bytes\n", read)
spew.Dump(buf)
return nil

Check warning on line 667 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L654-L667

Added lines #L654 - L667 were not covered by tests
}

func (l *writeDataStoreCmd) Run(ctx *context) error {
data, err := os.ReadFile(l.Path)
if err != nil {
return fmt.Errorf("ReadFile(l.Path) failed: %v", err)
}

Check warning on line 674 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L670-L674

Added lines #L670 - L674 were not covered by tests

if l.Password == "" {
return fmt.Errorf("empty password not allowed")
}

Check warning on line 678 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L676-L678

Added lines #L676 - L678 were not covered by tests

coreObj, err := core.NewCore(l.Device)
if err != nil {
return fmt.Errorf("NewCore() failed: %v", err)
}

Check warning on line 683 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L680-L683

Added lines #L680 - L683 were not covered by tests

comID, _, err := core.FindComID(coreObj.DriveIntf, coreObj.DiskInfo.Level0Discovery)
if err != nil {
return fmt.Errorf("FindComID() failed: %v", err)
}
cs, err := core.NewControlSession(coreObj.DriveIntf, coreObj.Level0Discovery, core.WithComID(comID))
if err != nil {
return fmt.Errorf("NewControllSession() failed: %v", err)
}

Check warning on line 692 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L685-L692

Added lines #L685 - L692 were not covered by tests

serial, err := coreObj.SerialNumber()
if err != nil {
return fmt.Errorf("coreObj.SerialNumber() failed: %v", err)
}
salt := fmt.Sprintf("%-20s", serial)
pwhash := pbkdf2.Key([]byte(l.Password), []byte(salt[:20]), 75000, 32, sha1.New)

lockingSession, err := cs.NewSession(uid.LockingSP)
if err != nil {
return fmt.Errorf("NewSession() to LockingSP failed: %v", err)
}
defer lockingSession.Close()
// Elevate the session to Admin1 with required credentials
if err := table.ThisSP_Authenticate(lockingSession, uid.LockingAuthorityAdmin1, pwhash); err != nil {
return fmt.Errorf("authenticating as Admin1 failed: %v", err)
}

Check warning on line 709 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L694-L709

Added lines #L694 - L709 were not covered by tests

info, err := table.DataStoreTableInfo(lockingSession)
if err != nil {
return fmt.Errorf("DataStoreTableInfo() failed: %v", err)
} else {
spew.Dump(info)
}

Check warning on line 716 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L711-L716

Added lines #L711 - L716 were not covered by tests

if uint(len(data)) > uint(info.Size) {
return fmt.Errorf("data is too large %d > %d", len(data), info.Size)
}

Check warning on line 720 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L718-L720

Added lines #L718 - L720 were not covered by tests

if err := table.DataStoreWrite(lockingSession, data, 0, uint(info.RecommendedAccessGranularity)); err != nil {
return fmt.Errorf("DataStoreWrite() failed: %v", err)
}

Check warning on line 724 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L722-L724

Added lines #L722 - L724 were not covered by tests

return nil

Check warning on line 726 in cmd/gosedctl/cmd.go

View check run for this annotation

Codecov / codecov/patch

cmd/gosedctl/cmd.go#L726

Added line #L726 was not covered by tests
}
21 changes: 18 additions & 3 deletions pkg/core/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
All bool
}
type DataStore struct {
// TODO
MaxTables uint16
MaxTablesSize uint32
TableSizeAlignment uint32
}

type OpalV2 struct {
Expand Down Expand Up @@ -252,8 +254,21 @@
}

func ReadDataStoreFeature(rdr io.Reader) (*DataStore, error) {
f := &DataStore{}
return f, nil
d := struct {
_ [2]byte
MaxTables uint16
MaxTablesSize uint32
TableSizeAlignment uint32
}{}
if err := binary.Read(rdr, binary.BigEndian, &d); err != nil {
return nil, err
}

Check warning on line 265 in pkg/core/feature/feature.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/feature/feature.go#L257-L265

Added lines #L257 - L265 were not covered by tests

return &DataStore{
MaxTables: d.MaxTables,
MaxTablesSize: d.MaxTablesSize,
TableSizeAlignment: d.TableSizeAlignment,
}, nil

Check warning on line 271 in pkg/core/feature/feature.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/feature/feature.go#L267-L271

Added lines #L267 - L271 were not covered by tests
}

func ReadOpalV2Feature(rdr io.Reader) (*OpalV2, error) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/core/method/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
m.buf.Write(stream.Token(stream.EndName))
}

// adds a Half-UID token (encoded as bytes)
func (m *MethodCall) HalfUID(b uid.HalfUID) {
m.buf.Write(stream.Bytes(b[:]))

Check warning on line 155 in pkg/core/method/method.go

View check run for this annotation

Codecov / codecov/patch

pkg/core/method/method.go#L154-L155

Added lines #L154 - L155 were not covered by tests
}

// Bytes adds a bytes atom
func (m *MethodCall) Bytes(b []byte) {
m.buf.Write(stream.Bytes(b))
Expand Down
3 changes: 3 additions & 0 deletions pkg/core/stream/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var (
OpalWhere TokenType = 0x00
ReadLockEnabled TokenType = 0x05
WriteLockEnabled TokenType = 0x06
OpalBooleanAnd TokenType = 0x00
OpalBooleanOr TokenType = 0x01
OpalBooleanNot TokenType = 0x02

ErrUnbalancedList = errors.New("message contained unbalanced list structures")
)
Expand Down
Loading