-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from solipsis/flash-validate
Flash Hashing and Flash Dumping
- Loading branch information
Showing
8 changed files
with
765 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
flashDumpCmd.Flags().StringVarP(&memAddress, "address", "a", "", "memory address") | ||
flashDumpCmd.Flags().StringVarP(&file, "file", "f", "", "store result to file") | ||
flashDumpCmd.Flags().Uint32VarP(&length, "length", "l", 0, "length of memory to dump") | ||
rootCmd.AddCommand(flashDumpCmd) | ||
} | ||
|
||
var file string | ||
|
||
var flashDumpCmd = &cobra.Command{ | ||
Use: "flashDump", | ||
Short: "dump certain section of flash", | ||
Long: "dumps certain section of flash", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
addrStr := mustParseHex(memAddress) | ||
addr := binary.BigEndian.Uint32(addrStr) | ||
buf := bytes.Buffer{} | ||
|
||
for length > 0 { | ||
l := min(1024, length) | ||
data, err := kk.FlashDump(addr, l) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
buf.Write(data) | ||
|
||
length -= l | ||
addr += l | ||
} | ||
|
||
if file != "" { | ||
if err := ioutil.WriteFile(file, buf.Bytes(), 0644); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} else { | ||
fmt.Println(hex.EncodeToString(buf.Bytes())) | ||
} | ||
}, | ||
} | ||
|
||
func min(x, y uint32) uint32 { | ||
if x < y { | ||
return x | ||
} | ||
return y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
flashHashCmd.Flags().Uint32VarP(&length, "length", "l", 0, "length of memory to hash") | ||
flashHashCmd.Flags().StringVarP(&challenge, "challenge", "c", "", "challenge response nonce") | ||
flashHashCmd.Flags().StringVarP(&memAddress, "address", "a", "", "memory address") | ||
rootCmd.AddCommand(flashHashCmd) | ||
} | ||
|
||
var ( | ||
length uint32 | ||
memAddress string | ||
challenge string | ||
) | ||
|
||
var flashHashCmd = &cobra.Command{ | ||
Use: "flashHash", | ||
Short: "Request hash of certain segment of flash memory", | ||
Long: "Requests hash of certain segment of flash memory", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
nonce := mustParseHex(challenge) | ||
addr := mustParseHex(memAddress) | ||
data, err := kk.FlashHash(addr, nonce, length) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println(hex.EncodeToString(data)) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
writeHashCmd.Flags().StringVarP(&memAddress, "address", "a", "", "memory address to begin writing") | ||
writeHashCmd.Flags().StringVarP(&writeData, "sector", "s", "", "data to write in hex") | ||
rootCmd.AddCommand(writeHashCmd) | ||
} | ||
|
||
// Data to write to device | ||
var writeData string | ||
|
||
var writeHashCmd = &cobra.Command{ | ||
Use: "writeFlash", | ||
Short: "Write data over flash sectors", | ||
Long: "Writes data over flash sectors", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
// data to write | ||
data := mustParseHex(writeData) | ||
|
||
// Convert hex address to uint32 | ||
addr := binary.BigEndian.Uint32(mustParseHex(memAddress)) | ||
|
||
resp, err := kk.FlashWrite(addr, data) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println(hex.EncodeToString(resp)) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.