From 709e47a5856d785f5f982e29fff65362698d48c4 Mon Sep 17 00:00:00 2001 From: Torsten Harenberg Date: Thu, 30 Jun 2022 11:10:13 +0200 Subject: [PATCH] v0.2: improved serial i/o significantly --- .idea/remote-targets.xml | 21 +++++ .../go_build_go_scsupdate.xml | 14 +++ CHANGELOG.md | 6 ++ firmware/firmware.go | 2 +- go.mod | 14 ++- go.sum | 57 +++++------- main.go | 18 ++-- update/update.go | 88 ++++++++++++------- usb/usb.go | 2 +- 9 files changed, 131 insertions(+), 91 deletions(-) create mode 100644 .idea/remote-targets.xml create mode 100644 .idea/runConfigurations/go_build_go_scsupdate.xml diff --git a/.idea/remote-targets.xml b/.idea/remote-targets.xml new file mode 100644 index 0000000..b2e49f4 --- /dev/null +++ b/.idea/remote-targets.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/go_build_go_scsupdate.xml b/.idea/runConfigurations/go_build_go_scsupdate.xml new file mode 100644 index 0000000..715f067 --- /dev/null +++ b/.idea/runConfigurations/go_build_go_scsupdate.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index c5ba54f..3effc64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # go-scsupdate Changelog +## v0.2 + +- serial write process now handles error (especially interrupted system call errors) properly +- remove serial timeout parameter - no needed anymore with the change above +- moved to go-playground/log/v8 + ## v0.1 Initial release \ No newline at end of file diff --git a/firmware/firmware.go b/firmware/firmware.go index d1fd49d..e272054 100644 --- a/firmware/firmware.go +++ b/firmware/firmware.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/binary" "errors" - "github.com/go-playground/log/v7" + "github.com/go-playground/log/v8" "hash/crc32" "io" "io/ioutil" diff --git a/go.mod b/go.mod index d719bd1..c145526 100644 --- a/go.mod +++ b/go.mod @@ -4,23 +4,21 @@ go 1.17 require ( github.com/akamensky/argparse v1.3.1 - github.com/albenik/go-serial/v2 v2.4.0 + github.com/albenik/go-serial/v2 v2.5.1 + github.com/go-playground/log/v8 v8.0.1 + github.com/schollz/progressbar/v3 v3.8.3 ) require ( github.com/creack/goselect v0.1.2 // indirect - github.com/go-playground/ansi/v3 v3.0.0 // indirect - github.com/go-playground/errors/v5 v5.1.1 // indirect - github.com/go-playground/log/v7 v7.0.2 // indirect - github.com/go-playground/pkg/v4 v4.0.0 // indirect - github.com/go-playground/pkg/v5 v5.3.0 // indirect + github.com/go-playground/errors/v5 v5.2.3 // indirect + github.com/go-playground/pkg/v5 v5.6.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/rivo/uniseg v0.2.0 // indirect - github.com/schollz/progressbar/v3 v3.8.3 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.7.0 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect - golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect + golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect ) diff --git a/go.sum b/go.sum index 6106747..af744cd 100644 --- a/go.sum +++ b/go.sum @@ -1,35 +1,27 @@ -github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91/go.mod h1:cDLGBht23g0XQdLjzn6xOGXDkLK182YfINAaZEQLCHQ= github.com/akamensky/argparse v1.3.1 h1:kP6+OyvR0fuBH6UhbE6yh/nskrDEIQgEA1SUXDPjx4g= github.com/akamensky/argparse v1.3.1/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= -github.com/albenik/go-serial/v2 v2.4.0 h1:2yIM9C0l0YznmMbHF8Yw+j0XY1ZDy5YciqhGdOik6r8= -github.com/albenik/go-serial/v2 v2.4.0/go.mod h1:JUrQKdczCMB0FlXt2rlJJ8zbfFzmjTIAkLPyyVfr5ho= -github.com/aws/aws-sdk-go v1.25.31/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= -github.com/aws/aws-sdk-go v1.32.11/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= +github.com/albenik/go-serial/v2 v2.5.1 h1:nnnyjuwIJLdih8yJyrncLFLQtGCnIppLcTNNtPg+ME0= +github.com/albenik/go-serial/v2 v2.5.1/go.mod h1:ySdCqoERscw1xluK1n62R8Faoyu+jXKwVHPa1lSSAew= +github.com/aws/aws-sdk-go v1.44.24/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo= github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0= github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-playground/ansi/v3 v3.0.0 h1:TXexAx1sXuL/N/cqV472ffRdkoiqPA3LeKMXCoz8t4Y= -github.com/go-playground/ansi/v3 v3.0.0/go.mod h1:btm11Vjtjy20mZiJwufDvdVAVxjLuYFDu164b83ekBs= github.com/go-playground/assert v1.2.1/go.mod h1:Lgy+k19nOB/wQG/fVSQ7rra5qYugmytMQqvQ2dgjWn8= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= -github.com/go-playground/errors/v5 v5.0.1/go.mod h1:p7HVwyH3I0esuPnvChd2Uf4ceZwG2GcaDT/KpzCN18M= -github.com/go-playground/errors/v5 v5.1.1 h1:H/igfatbYnCqa4Xednb1nnxljpPWnogA4TgsA4U891I= -github.com/go-playground/errors/v5 v5.1.1/go.mod h1:xyrVVz3LrfHZQmZQ1ri/8rdiis2F96q15wGX89xWsL0= -github.com/go-playground/form/v4 v4.0.0/go.mod h1:bodWfd97U9PVMZFcDsbVzSbQQTtaWrebnTwQtWjSW1M= +github.com/go-playground/errors/v5 v5.2.3 h1:RPxaFHgJZjgk/OFkUcfytJgRQKRINLtueVxgOdnfPpg= +github.com/go-playground/errors/v5 v5.2.3/go.mod h1:DincxRGwraWmq39TZDqtnOtHGOJ+AbNbO0OmBzX6MLw= github.com/go-playground/form/v4 v4.1.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U= -github.com/go-playground/log/v7 v7.0.2 h1:jHdecwWiVlQsOr9CKnAy1yjlwMTXITwUB2Tb3jVUrfU= -github.com/go-playground/log/v7 v7.0.2/go.mod h1:AWMsaRSXCVhwbCSvvchHqQfyREAODmWrX435KTjpuYU= -github.com/go-playground/pkg/v4 v4.0.0 h1:0lTr9H8RyCwom4TcfNhMCNvlTlFuin7uUlXcZQuxrA4= -github.com/go-playground/pkg/v4 v4.0.0/go.mod h1:TLowM3d3a/m04JlHK/zM6Ia8zf8+0C/9pTwhDEUqKO0= +github.com/go-playground/form/v4 v4.2.0/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U= +github.com/go-playground/log/v8 v8.0.1 h1:yclpYIMZ49kTwtRZsmFlPRr/mmgBrRAXzC7FcZn9jmM= +github.com/go-playground/log/v8 v8.0.1/go.mod h1:G6XdEdQBItRKCi6XtQ2CMSkyP81AScSQEnNqKzknaT4= github.com/go-playground/pkg/v5 v5.0.0/go.mod h1:0380E0lsFB1POWFypOLL8tX2f0O1OBCBpSdVmEy4mg0= -github.com/go-playground/pkg/v5 v5.3.0 h1:G+DOJRqMoQTpnSGEjc1rWat6pE4EKp7ejM3udkjCi78= -github.com/go-playground/pkg/v5 v5.3.0/go.mod h1:HXedFsSf7dogjVi5wmbOK7P+doyvzvLYMNlyX+PfGZI= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= -github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= +github.com/go-playground/pkg/v5 v5.6.0 h1:97YpRFzIcS5NFP2Uzxj8cPYz4zTuwweyXADYcA1KfUQ= +github.com/go-playground/pkg/v5 v5.6.0/go.mod h1:TvZ2nNtNh6VfoNteY9ApA2BXt1ZwJliFZ4hzPAwLS9Y= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -40,7 +32,6 @@ github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4 github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -48,31 +39,23 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/schollz/progressbar/v3 v3.8.3 h1:FnLGl3ewlDUP+YdSwveXBaXs053Mem/du+wr7XSYKl8= github.com/schollz/progressbar/v3 v3.8.3/go.mod h1:pWnVCjSBZsT2X3nx9HfRdnCDrpbevliMeoEVhStwHko= -github.com/segmentio/errors-go v1.0.0/go.mod h1:RDVEREUrpa4/jM8rt5KsQpu+JoXPi6i07vG7m4tX0MY= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec= go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210223212115-eede4237b368/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -80,16 +63,16 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+R golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM= +golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 51fbeba..661031f 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,14 @@ // Copyright 2021 Torsten Harenberg DL1THM. All rights reserved. -// A SCS PACTOR modem flashing utility written in Go. Should run anywhere Go runs. +// A SCS PACTOR modem flashing utility written in Go. Should run everywhere Go runs. package main import ( + "bytes" "fmt" "github.com/akamensky/argparse" - "github.com/go-playground/log/v7" - "github.com/go-playground/log/v7/handlers/console" + "github.com/go-playground/log/v8" "go-scsupdate/firmware" "go-scsupdate/update" "go-scsupdate/usb" @@ -24,9 +24,9 @@ func main() { debug := parser.Flag("d", "debug", &argparse.Options{Required: false, Help: "(optional) enable debug logs"}) dryrun := parser.Flag("n", "dryrun", &argparse.Options{Required: false, Help: "(optional) drying: if set, modem will detected but no firmware being written"}) force := parser.Flag("", "force", &argparse.Options{Required: false, Help: "(optional) skip testing if firmware fits the modem. Useful if the modem is bricked and you need to put it into BIOS mode. Read the manual!"}) - sertimeout := parser.Int("t", "timeout", &argparse.Options{Required: false, Help: "(optional) Sets the read and write timeout. Default: 70. If you have handshake problems, enlarge this values, especially for older modems."}) err := parser.Parse(os.Args) - cLog := console.New(true) + buff := new(bytes.Buffer) + cLog := log.NewConsoleBuilder().WithWriter(buff).Build() if *debug { log.AddHandler(cLog, log.AllLevels...) log.Debug("DEBUG logging enabled") @@ -34,10 +34,6 @@ func main() { log.AddHandler(cLog, log.InfoLevel, log.ErrorLevel, log.AlertLevel, log.WarnLevel, log.NoticeLevel, log.PanicLevel) } - if *sertimeout == 0 { // if not set, set the write / read timeout to 70 - *sertimeout = 70 - } - if *serialDevice != "" && *brate == 0 { log.Fatal("ERROR: if you specify a device, you need to specify a baud rate as well. Cannot continue") } @@ -90,9 +86,9 @@ func main() { if *dryrun != true { var err error if *serialDevice != "" { - err = update.Update(*serialDevice, baudrate, *sertimeout, *f, *force) + err = update.Update(*serialDevice, baudrate, *f, *force) } else { - err = update.Update(ports[0].Port, baudrate, *sertimeout, *f, *force) + err = update.Update(ports[0].Port, baudrate, *f, *force) } if err != nil { log.Panic(err) diff --git a/update/update.go b/update/update.go index 3da8a14..53b52fe 100644 --- a/update/update.go +++ b/update/update.go @@ -6,7 +6,7 @@ import ( "errors" "fmt" "github.com/albenik/go-serial/v2" - "github.com/go-playground/log/v7" + "github.com/go-playground/log/v8" "github.com/schollz/progressbar/v3" "io/ioutil" "os" @@ -25,14 +25,14 @@ type modem struct { } // initmodem: opens the serial line, returns instance of serial.Port -func initmodem(serport string, baudrate int, sertimeout int) (*modem, error) { +func initmodem(serport string, baudrate int) (*modem, error) { p, err := serial.Open(serport, serial.WithBaudrate(baudrate), serial.WithDataBits(8), serial.WithParity(serial.NoParity), serial.WithStopBits(serial.OneStopBit), - serial.WithReadTimeout(sertimeout), - serial.WithWriteTimeout(sertimeout)) + serial.WithReadTimeout(-1), //blocking I/O, see https://pkg.go.dev/github.com/albenik/go-serial#Port + serial.WithWriteTimeout(-1)) //blocking I/O if err != nil { fmt.Printf("ERROR: File %s could not be opened\n", serport) return nil, err @@ -41,9 +41,10 @@ func initmodem(serport string, baudrate int, sertimeout int) (*modem, error) { return m, nil } -// write: writes to the serial ports, returns answer and logs output -func (m *modem) write(out string, nolog ...bool) ([]byte, error) { - if len(nolog) == 0 { +// write: writes to the serial ports, returns answer and logs output. Optional toread forces a +// certain number of bytes to read +func (m *modem) write(out string, toread ...uint32) ([]byte, error) { + if len(toread) == 0 { log.Debug("Sending " + strings.TrimFunc(out, func(r rune) bool { return !unicode.IsGraphic(r) })) @@ -52,18 +53,37 @@ func (m *modem) write(out string, nolog ...bool) ([]byte, error) { log.Debugf("Sending %d byte(s)", len(out)) } } - _, err := m.port.Write([]byte(out)) - if err != nil { - log.Errorf("ERROR while sending serial command: %s\n", out) - log.Error(err) - return []byte(""), err + for { + status, err := m.port.GetModemStatusBits() + if err != nil { + log.Fatal("GetModemStatusBits failed") + } + if status.CTS { + for { + sent, err := m.port.Write([]byte(out)) + if err == nil { + break + } else { + // log.Errorf("ERROR while sending serial command: %s\n", out) + log.Debug(err) + out = out[sent:] + } + } + break + } } - buff := make([]byte, 1024) - if len(nolog) == 0 { - time.Sleep(50 * time.Millisecond) // give the PTC some time to answer, no needed with binary + var btr uint32 + if len(toread) == 0 { + time.Sleep(80 * time.Millisecond) // give the PTC some time to answer, no needed with binary + btr, _ = m.port.ReadyToRead() + if btr == 0 { + return []byte(""), nil + } + } else { + btr = toread[0] } + buff := make([]byte, btr) n, err := m.port.Read(buff) - // fmt.Printf("Read %d bytes\n", n) if err != nil { log.Errorf("ERROR: cannot read answer from command: %s\n", out) return []byte(""), err @@ -103,27 +123,29 @@ func (m *modem) setDateTime() error { // writeUpdate: main flash utility. Writes the firmware taken from fwfile to the modem. Write progress bar. func (m *modem) writeUpdate(fwfile string) error { + m.port.SetWriteTimeout(-50) + m.port.SetReadTimeout(-1) _, err := m.write("UPDATE\r") if err != nil { - _, _ = m.write("\033", true) // send ESC to cancel the update + _, _ = m.write("\033") // send ESC to cancel the update return err } - answer, err := m.write("\006", true) + answer, err := m.write("\006") if err != nil { - _, _ = m.write("\033", true) // send ESC to cancel the update + fmt.Println(err) + _, _ = m.write("\033") // send ESC to cancel the update return err } - log.Debugf("flash stamp: % X", binary.LittleEndian.Uint32(answer[2:6])) flashId := binary.LittleEndian.Uint16(answer[0:2]) log.Debugf("flash id: % X", flashId) if flashId != 0xa41f && flashId != 0x5b1f && flashId != 0xda1f { - _, _ = m.write("\033", true) // send ESC to cancel the update + _, _ = m.write("\033") // send ESC to cancel the update return errors.New("wrong flash id") } r, err := os.Open(fwfile) if err != nil { - _, _ = m.write("\033", true) // send ESC to cancel the update + _, _ = m.write("\033") // send ESC to cancel the update return err } fstat, _ := r.Stat() @@ -132,7 +154,7 @@ func (m *modem) writeUpdate(fwfile string) error { fwblob, err := ioutil.ReadFile(fwfile) if err != nil { - _, _ = m.write("\033", true) // send ESC to cancel the update + _, _ = m.write("\033") // send ESC to cancel the update return err } // fmt.Println(fsize) @@ -140,20 +162,20 @@ func (m *modem) writeUpdate(fwfile string) error { if fsize%256 != 0 { chunks++ } - // send ACK - _, err = m.write("\006", true) + _, err = m.write("\006") if err != nil { - _, _ = m.write("\033", true) // send ESC to cancel the update + _, _ = m.write("\033") // send ESC to cancel the update return err } // send number of chunks a := make([]byte, 2) binary.BigEndian.PutUint16(a, chunks) - res, err := m.write(string(a), true) + res, err := m.write(string(a)) if err != nil || string(res) != "\006" { //no ACK - _, _ = m.write("\033", true) // send ESC to cancel the update + fmt.Println("kein ACK") + _, _ = m.write("\033") // send ESC to cancel the update return err } @@ -168,15 +190,15 @@ func (m *modem) writeUpdate(fwfile string) error { } else { sendbuff = string(fwblob[i*CHUNKSIZE : i*CHUNKSIZE+CHUNKSIZE]) } - res, err = m.write(sendbuff, true) + res, err = m.write(sendbuff, 1) if err != nil || string(res) != "\006" { //no ACK - _, _ = m.write("\033", true) // send ESC to cancel the update log.Errorf("Error in Handshake! Rx: %s", hex.EncodeToString(res)) + _, _ = m.write("\033") // send ESC to cancel the update return err } } log.Infof("Firmware file %s successfully written", fwfile) - _, err = m.write("\r", true) + _, err = m.write("\r") if err != nil { log.Error("Error in Handshake: cannot send final cr") } @@ -226,8 +248,8 @@ func (m *modem) checkFirmwareFits(fwfile string) error { } // Update: main firmware update utility. Runs pre-checks and calls main flashing function writeUpdate -func Update(serport string, baudrate int, sertimeout int, fwfile string, force bool) error { - m, err := initmodem(serport, baudrate, sertimeout) +func Update(serport string, baudrate int, fwfile string, force bool) error { + m, err := initmodem(serport, baudrate) defer m.port.Close() if err != nil { return errors.New("FEHLER in Update") diff --git a/usb/usb.go b/usb/usb.go index 98f19a5..23082ab 100644 --- a/usb/usb.go +++ b/usb/usb.go @@ -3,7 +3,7 @@ package usb import ( "errors" enumerator2 "github.com/albenik/go-serial/v2/enumerator" - "github.com/go-playground/log/v7" + "github.com/go-playground/log/v8" ) /*