Skip to content

Commit

Permalink
better uf2 validation
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Sep 22, 2024
1 parent c7032cc commit 8fc4c62
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/picobin/uf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func uf2info(r io.ReaderAt, flags Flags) error {
if err != nil {
return err
}
return blockInfo(blocks, uint64(block0start), flags)
return blockInfo(blocks, romstart+uint64(block0start), flags)
}

func uf2conv(r io.ReaderAt, flags Flags) error {
Expand Down
22 changes: 15 additions & 7 deletions uf2/uf2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
)

var (
errSmallBlock = errors.New("buffer too small to contain block")
errMagic0 = errors.New("first word is not magic0 word \"UF2\\n\"")
errMagic1 = errors.New("second word is not magic1 word")
errMagicEnd = errors.New("last word is not magic end word")
errSmallBlock = errors.New("buffer too small to contain block")
errPayload = errors.New("payload size exceeds max UF2 block size")
errBlockNumbering = errors.New("block number larger than number of blocks")
errMagic0 = errors.New("first word is not magic0 word \"UF2\\n\"")
errMagic1 = errors.New("second word is not magic1 word")
errMagicEnd = errors.New("last word is not magic end word")
)

const (
Expand Down Expand Up @@ -89,9 +91,11 @@ func (b *Block) Data() ([]byte, error) {
func (b *Block) Validate() error {
sz := b.PayloadSize
if sz > BlockMaxData {
return errors.New("payload size exeeds permissible maximum")
return errPayload
} else if sz == 0 {
return errors.New("zero payload size")
} else if b.BlockNum >= b.NumBlocks {
return errBlockNumbering
}
return nil
}
Expand Down Expand Up @@ -128,11 +132,15 @@ func DecodeAppendBlocks(dst []Block, r io.Reader, scratchBuf []byte) ([]Block, i

// DecodeBlock decodes a 512 byte block from the argument buffer. Buffer must be at least 512 bytes long.
func DecodeBlock(text []byte) (Block, error) {
err := ValidateBlock(text)
err := validateMagic(text)
if err != nil {
return Block{}, err
}
block := MustDecodeBlock(text)
err = block.Validate()
if err != nil {
return Block{}, err
}
return block, nil
}

Expand All @@ -149,7 +157,7 @@ func MustDecodeBlock(text []byte) (block Block) {
return block
}

func ValidateBlock(text []byte) error {
func validateMagic(text []byte) error {
if len(text) < 512 {
return errSmallBlock
}
Expand Down

0 comments on commit 8fc4c62

Please sign in to comment.