Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: usedbytes/serial-flash
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: nayarsystems/serial-flash
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: nayar
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 5 commits
  • 5 files changed
  • 1 contributor

Commits on Oct 8, 2024

  1. improve compatibility

    dcentelles committed Oct 8, 2024
    Copy the full SHA
    c0093d2 View commit details

Commits on Jan 7, 2025

  1. update module declaration

    dcentelles committed Jan 7, 2025
    Copy the full SHA
    d07e133 View commit details

Commits on Jan 9, 2025

  1. add program with context

    dcentelles committed Jan 9, 2025
    Copy the full SHA
    92eaab9 View commit details
  2. Copy the full SHA
    7b5f1d4 View commit details

Commits on Jan 10, 2025

  1. add reboot command

    dcentelles committed Jan 10, 2025
    Copy the full SHA
    ddc486d View commit details
Showing with 83 additions and 21 deletions.
  1. +4 −5 go.mod
  2. +2 −1 go.sum
  3. +30 −13 main.go
  4. +20 −2 program/program.go
  5. +27 −0 protocol/protocol.go
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module github.com/usedbytes/serial-flash
module github.com/nayarsystems/serial-flash

go 1.17

require (
github.com/cheggaaa/pb v1.0.29
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4
github.com/usedbytes/serial-flash v0.0.0-20220717212919-51e30aae59cd
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac
)

require (
github.com/fatih/color v1.10.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ github.com/cheggaaa/pb v1.0.29/go.mod h1:W40334L7FMC5JKWldsTWbdGjLo0RxUKK73K+TuP
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4 h1:G2ztCwXov8mRvP0ZfjE6nAlaCX2XbykaeHdbT6KwDz0=
github.com/jacobsa/go-serial v0.0.0-20180131005756-15cf729a72d4/go.mod h1:2RvX5ZjVtsznNZPEt4xwJXNJrM3VTZoQf7V6gk0ysvs=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
@@ -18,6 +17,8 @@ github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/usedbytes/serial-flash v0.0.0-20220717212919-51e30aae59cd h1:QJyv7PW4Z8z8PlD39vNhAqvVMX4hh6PIOHarvPkxxRw=
github.com/usedbytes/serial-flash v0.0.0-20220717212919-51e30aae59cd/go.mod h1:92lvzre7DfycmOm1qh7YfFYl1wG2fa9V6dcpoq+cX+I=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
43 changes: 30 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
@@ -10,10 +10,11 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/cheggaaa/pb"
tty "github.com/jacobsa/go-serial/serial"
"golang.org/x/sys/unix"

"github.com/usedbytes/serial-flash/program"
"github.com/usedbytes/serial-flash/protocol"
@@ -27,6 +28,27 @@ func usage() error {
return fmt.Errorf("Usage: %s PORT FILE [BASE]", os.Args[0])
}

func setSerialAttributes(fd int) error {
// Obtener los atributos actuales del puerto serial
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
return fmt.Errorf("ioctl TCGETS failed: %v", err)
}

// Configurar baud rate, data bits, stop bits, etc.
termios.Cflag = unix.B921600 | unix.CS8 | unix.CLOCAL | unix.CREAD
termios.Iflag = unix.IGNPAR
termios.Oflag = 0
termios.Lflag = 0

// Aplicar los nuevos atributos
if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
return fmt.Errorf("ioctl TCSETS failed: %v", err)
}

return nil
}

func run() error {
if len(os.Args) < 3 {
return usage()
@@ -81,24 +103,19 @@ func run() error {

rw = conn
} else {
options := tty.OpenOptions{
PortName: port,
BaudRate: 921600,
DataBits: 8,
StopBits: 1,
MinimumReadSize: 1,
InterCharacterTimeout: 100,
fd, err := syscall.Open(port, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return fmt.Errorf("syscall.Open %s: %v", port, err)
}
defer syscall.Close(fd)

ser, err := tty.Open(options)
if err != nil {
return fmt.Errorf("tty.Open %s: %v", port, err)
if err := setSerialAttributes(fd); err != nil {
return fmt.Errorf("setSerialAttributes %s: %v", port, err)
}
defer ser.Close()

fmt.Println("Opened", port)

rw = ser
rw = os.NewFile(uintptr(fd), port)
}

prog := make(chan program.ProgressReport)
22 changes: 20 additions & 2 deletions program/program.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
package program

import (
"context"
"errors"
"fmt"
"io"
@@ -37,7 +38,7 @@ func reportProgress(reportChan chan<- ProgressReport, stage string, progress int
}
}

func sync(rw io.ReadWriter, progress chan<- ProgressReport) error {
func sync(ctx context.Context, rw io.ReadWriter, progress chan<- ProgressReport) error {
var err error

for i := 0; i < maxSyncAttempts; i++ {
@@ -52,6 +53,9 @@ func sync(rw io.ReadWriter, progress chan<- ProgressReport) error {
} else if !errors.Is(err, protocol.ErrNotSynced) {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
}

return err
@@ -61,12 +65,20 @@ func align(val, to uint32) uint32 {
return (val + (to - 1)) & ^(to - 1)
}

func Sync(ctx context.Context, rw io.ReadWriter) error {
return sync(ctx, rw, nil)
}

func Program(rw io.ReadWriter, img *Image, progress chan<- ProgressReport) error {
return ProgramWithCtx(context.Background(), rw, img, progress)
}

func ProgramWithCtx(ctx context.Context, rw io.ReadWriter, img *Image, progress chan<- ProgressReport) error {
if progress != nil {
defer close(progress)
}

err := sync(rw, progress)
err := sync(ctx, rw, progress)
if err != nil {
return fmt.Errorf("sync: %v", err)
}
@@ -106,6 +118,9 @@ func Program(rw io.ReadWriter, img *Image, progress chan<- ProgressReport) error
if err != nil {
return fmt.Errorf("erase: %v", err)
}
if ctx.Err() != nil {
return ctx.Err()
}
}

reportProgress(progress, "Writing", 0, len(data))
@@ -125,6 +140,9 @@ func Program(rw io.ReadWriter, img *Image, progress chan<- ProgressReport) error
if err != nil {
return fmt.Errorf("write: %v", err)
}
if ctx.Err() != nil {
return ctx.Err()
}
}

reportProgress(progress, "Finalising", 0, 1)
27 changes: 27 additions & 0 deletions protocol/protocol.go
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ var (
OpcodeSeal [4]byte = [4]byte{'S', 'E', 'A', 'L'}
OpcodeGo [4]byte = [4]byte{'G', 'O', 'G', 'O'}
OpcodeInfo [4]byte = [4]byte{'I', 'N', 'F', 'O'}
OpcodeReboot [4]byte = [4]byte{'B', 'O', 'O', 'T'}
ResponseSync [4]byte = [4]byte{'P', 'I', 'C', 'O'}
ResponseSyncWota [4]byte = [4]byte{'W', 'O', 'T', 'A'}
ResponseOK [4]byte = [4]byte{'O', 'K', 'O', 'K'}
@@ -317,6 +318,32 @@ func (c *GoCommand) Execute(rw io.ReadWriter) error {
return nil
}

type RebootCommand struct {
Bootloader bool
}

func (c *RebootCommand) Execute(rw io.ReadWriter) error {
buf := make([]byte, len(OpcodeReboot)+4)

copy(buf[0:], OpcodeReboot[:])
var arg0 uint32
if c.Bootloader {
arg0 = 1
}
binary.LittleEndian.PutUint32(buf[4:], arg0)

n, err := rw.Write(buf)
if err != nil {
return err
} else if n != len(buf) {
return fmt.Errorf("unexpected write length: %v", n)
}

// Fire and forget

return nil
}

type InfoCommand struct {
FlashAddr uint32
FlashSize uint32