diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-05-11 11:56:15 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-05-11 11:56:15 -0600 |
commit | 7e9213c75b9361c174f4662ff3feb4104d2c8f07 (patch) | |
tree | 46e37147b439add714cb6b49032ca38612596616 /cmd/btrfs-dbg | |
parent | 2744e0700ca6fe956f569d47010fd4e693fedcfa (diff) |
wip
Diffstat (limited to 'cmd/btrfs-dbg')
-rw-r--r-- | cmd/btrfs-dbg/main.go | 5 | ||||
-rw-r--r-- | cmd/btrfs-dbg/types.go | 38 |
2 files changed, 42 insertions, 1 deletions
diff --git a/cmd/btrfs-dbg/main.go b/cmd/btrfs-dbg/main.go index d6c9c68..0990378 100644 --- a/cmd/btrfs-dbg/main.go +++ b/cmd/btrfs-dbg/main.go @@ -43,6 +43,11 @@ func Main(imgfilename string) (err error) { spew.DisablePointerAddresses = true spew.Dump(superblocks[0].data) + sum, err := superblocks[0].data.CalculateChecksum() + if err != nil { + return err + } + fmt.Printf("calculated sum: %x\n", sum) return nil } diff --git a/cmd/btrfs-dbg/types.go b/cmd/btrfs-dbg/types.go index 699f1a2..3cd348d 100644 --- a/cmd/btrfs-dbg/types.go +++ b/cmd/btrfs-dbg/types.go @@ -1,8 +1,11 @@ package main import ( + "encoding/binary" "encoding/hex" + "hash/crc32" "strings" + "time" "lukeshu.com/btrfs-tools/pkg/binstruct" ) @@ -11,6 +14,7 @@ type ( PhysicalAddr int64 LogicalAddr int64 ObjID int64 + CSum [0x20]byte UUID [16]byte ) @@ -25,8 +29,32 @@ func (uuid UUID) String() string { }, "-") } +func crc32c(data []byte) CSum { + sum := crc32.Update(0xFFFFFFFF, crc32.MakeTable(0x1EDC6F41), data) + var buf CSum + binary.LittleEndian.PutUint32(buf[:], sum) + return buf +} + +type Key struct { + ObjectID ObjID `bin:"off=0, siz=8, desc=Object ID. Each tree has its own set of Object IDs."` + ItemType uint8 `bin:"off=8, siz=1, desc=Item type."` + Offset uint64 `bin:"off=9, siz=8, desc=Offset. The meaning depends on the item type."` + binstruct.End `bin:"off=11"` +} + +type Time struct { + Sec int64 `bin:"off=0, siz=8, desc=Number of seconds since 1970-01-01T00:00:00Z."` + NSec uint64 `bin:"off=8, siz=4, desc=Number of nanoseconds since the beginning of the second."` + binstruct.End `bin:"off=c"` +} + +func (t Time) ToStd() time.Time { + return time.Unix(t.Sec, int64(t.NSec)) +} + type Superblock struct { - Checksum [0x20]byte `bin:"off=0, siz=20, desc=Checksum of everything past this field (from 20 to 1000)"` + Checksum CSum `bin:"off=0, siz=20, desc=Checksum of everything past this field (from 20 to 1000)"` FSUUID UUID `bin:"off=20, siz=10, desc=FS UUID"` Self PhysicalAddr `bin:"off=30, siz=8, desc=physical address of this block (different for mirrors)"` Flags uint64 `bin:"off=38, siz=8, desc=flags"` @@ -74,6 +102,14 @@ type Superblock struct { binstruct.End `bin:"off=1000"` } +func (sb Superblock) CalculateChecksum() (CSum, error) { + data, err := binstruct.Marshal(sb) + if err != nil { + return CSum{}, err + } + return crc32c(data[0x20:]), nil +} + type DevItem struct { DeviceID uint64 `bin:"off=0, siz=8, desc=device id"` |