Skip to content

Commit

Permalink
Add validation phase
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Nov 11, 2024
1 parent 7291749 commit acafa41
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
43 changes: 43 additions & 0 deletions app/dd.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,49 @@ func FlashFileToBlockDevice(iff string, of string) {
quit <- true
}

// ValidateBlockDeviceContent checks if the block device contents match the given file.
func ValidateBlockDeviceContent(iff string, of string) {
quit := handleStopInput(func() { os.Exit(0) })
src := openFile(iff, os.O_RDONLY, 0, "file")
dest := openFile(of, os.O_RDONLY|os.O_EXCL, os.ModePerm, "destination")
bs := 4 * 1024 * 1024 // TODO: Allow configurability?
timer := time.NewTimer(time.Second)
startTime := time.Now().UnixMilli()
var total int
buf1 := make([]byte, bs)
buf2 := make([]byte, bs)
for {
n1, err1 := src.Read(buf1)
n2, err2 := dest.Read(buf2)
if err1 == io.EOF && err2 == io.EOF {
break
} else if err1 != nil && err1 != io.EOF {
log.Fatalln("Encountered error while validating device!", err1)
} else if err2 != nil && err2 != io.EOF {
log.Fatalln("Encountered error while validating device!", err2)
} else if n2 != n1 || err1 != nil || err2 != nil || !bytes.Equal(buf1[:n1], buf2[:n2]) {

Check failure on line 124 in app/dd.go

View workflow job for this annotation

GitHub Actions / Build (macos-latest)

undefined: bytes
log.Fatalln("Read/write mismatch! Validation of image failed. It is unsafe to boot this device.")
}
total += n1
if len(timer.C) > 0 {
// There's some minor differences in output with dd, mainly decimal places and kB vs KB.
timeDifference := time.Now().UnixMilli() - startTime
print(strconv.Itoa(total) + " bytes " +
"(" + BytesToString(total, false) + ", " + BytesToString(total, true) + ") validated, " +
strconv.Itoa(int(timeDifference/1000)) + " s, " +
BytesToString(total/(int(timeDifference)/1000), false) + "/s\r")
<-timer.C
timer.Reset(time.Second)
}
}
timeDifference := float64(time.Now().UnixMilli()-startTime) / 1000
println(strconv.Itoa(total) + " bytes " +
"(" + BytesToString(total, false) + ", " + BytesToString(total, true) + ") validated, " +
strconv.FormatFloat(timeDifference, 'f', 3, 64) + " s, " +
BytesToString(int(float64(total)/timeDifference), false) + "/s")
quit <- true
}

func openFile(filePath string, flag int, mode fs.FileMode, name string) *os.File {
path, err := filepath.Abs(filePath)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@ func main() {
println("Invalid usage: imprint flash <file> <destination> (--use-system-dd)")
os.Exit(1)
}
log.Println("Phase 1/2: Unmounting disk.")
log.Println("Phase 1/3: Unmounting disk.")
if err := app.UnmountDevice(os.Args[3]); err != nil {
log.Println(err)
if !strings.HasSuffix(os.Args[3], "debug.iso") {
os.Exit(1)
}
}
log.Println("Phase 2/2: Writing ISO to disk.")
log.Println("Phase 2/3: Writing ISO to disk.")
if len(os.Args) > 4 && os.Args[4] == "--use-system-dd" {
app.RunDd(os.Args[2], os.Args[3])
} else {
app.FlashFileToBlockDevice(os.Args[2], os.Args[3])
}
log.Println("Phase 3/3: Validating written image on disk.")
app.ValidateBlockDeviceContent(os.Args[2], os.Args[3])
return
}
debug := false
Expand Down

0 comments on commit acafa41

Please sign in to comment.