[PATCH v2 03/16] ratp: send missing RST in behavior C2

2017-06-21 Thread Aleksander Morgado
The reference says:

If SYN was set we assume that the other end crashed and has
attempted to open a new connection.  We respond by sending a
legal reset:

[PATCH v2 04/16] ratp: add missing RST flag in behavior G

2017-06-21 Thread Aleksander Morgado
The reference says:

   If the ACK flag was set then send:
   

[PATCH v2 00/16] RATP logic fixes and improvements

2017-06-21 Thread Aleksander Morgado
Hey Sascha,

This v2 series of patches includes just changes in the commit messages, trying 
to explain which of the patches apply to barebox and/or the barebox<->bbremote 
interactions. I did test all these patches also with RATP FS, and I didn't see 
any regression.

I did see issues in RATP FS happening with and without my patches, though. E.g. 
"cat"-ing a long file (>1024 bytes) that is being exported via RATFS results in 
each byte read being sent through the ratp output console messages one by one 
(the actual read operation over RATFS goes in chunks of 255, but the cat stdout 
messages via RATP go byte by byte...). After that, the additional read 
operations above offset 1024 end up timing out. I believe I'm going to have to 
work with RATFS in the near future, so I'll likely spend some time looking at 
those issues soon anyway.

Aleksander Morgado (16):
  ratp: add missing transition to SYN-RECEIVED in behavior B
  ratp: avoid unnecessary variable initializations
  ratp: send missing RST in behavior C2
  ratp: add missing RST flag in behavior G
  ratp: completely ignore RST flagged packets in behavior G
  ratp: fix data presence check
  ratp: fix single byte sending flagged with SO
  ratp: remove bogus data checks in behavior C2
  ratp: remove FIXME comment: FIN always requires ACK
  ratp: fix sending ACKs without data
  ratp: consolidate ratp_sn_expected() and ratp_an_expected()
  ratp: prefer using ratp_send_ack() in behaviour I1
  ratp: send initial data in behaviour B if any pending
  ratp: don't ignore data that may arrive in behaviour H1
  ratp: consolidate setting the next AN or SN flags
  ratp: user close may happen in SYN-RECEIVED state

 lib/ratp.c | 116 +++--
 scripts/remote/ratp.py |  38 +++-
 2 files changed, 78 insertions(+), 76 deletions(-)

--
2.13.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 06/16] ratp: fix data presence check

2017-06-21 Thread Aleksander Morgado
Looking at the "data length" and SO flag isn't enough to declare a
packet with or without data, because SYN flagged packets will also use
the "data length" field to define MDL.

So, improve the check to match against SYN|RST|FIN flagged packets,
which can never have data.

This commit fixed a segfault in barebox when an unexpected SYN packet
was sent in the middle of a connection; barebox thought the packet had
data because the "data length" in the SYN packet was different than 0.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 4 ++--
 scripts/remote/ratp.py | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index d3c252047..c946bea1a 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -165,7 +165,7 @@ static bool ratp_has_data(struct ratp_header *hdr)
 {
if (hdr->control & RATP_CONTROL_SO)
return 1;
-   if (hdr->data_length)
+   if (!(hdr->control & (RATP_CONTROL_SYN | RATP_CONTROL_RST | 
RATP_CONTROL_FIN)) && hdr->data_length)
return 1;
return 0;
 }
@@ -1338,7 +1338,7 @@ static int ratp_behaviour_i1(struct ratp_internal *ri, 
void *pkt)
struct ratp_header *hdr = pkt;
uint8_t control = 0;
 
-   if (!hdr->data_length && !(hdr->control & RATP_CONTROL_SO))
+   if (!ratp_has_data (hdr))
return 1;
 
pr_vdebug("%s **received** %d\n", __func__, hdr->data_length);
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index 079fb871a..a41d2e8a3 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -525,7 +525,7 @@ class RatpConnection(object):
 # Our fin was lost, rely on retransmission
 return False
 
-if r.length or r.c_so:
+if (r.length and not r.c_syn and not r.c_rst and not r.c_fin) or 
r.c_so:
 self._retrans = None
 s = RatpPacket(flags='RA')
 s.c_sn = r.c_an
@@ -596,7 +596,7 @@ class RatpConnection(object):
 if r.c_so:
 self._r_sn = r.c_sn
 self._rx_buf.append(chr(r.length))
-elif r.length:
+elif r.length and not r.c_syn and not r.c_rst and not r.c_fin:
 self._r_sn = r.c_sn
 self._rx_buf.append(r.payload)
 else:
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 05/16] ratp: completely ignore RST flagged packets in behavior G

2017-06-21 Thread Aleksander Morgado
The reference says:

This procedure represents the behavior of the CLOSED state of a
connection. All incoming packets are discarded. If the packet
had the RST flag set take no action. Otherwise it is necessary
to build a RST packet.

So, skip building the RST packet if the incoming one had RST set.

This commit fixes an infinite loop of messages sent and received
between both ends during the connection close procedure, found when
testing barebox against a third party ratp implementation.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/ratp.c b/lib/ratp.c
index 43b8b04dc..d3c252047 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1033,6 +1033,9 @@ static int ratp_behaviour_g(struct ratp_internal *ri, 
void *pkt)
 
pr_debug("%s\n", __func__);
 
+   if (hdr->control & RATP_CONTROL_RST)
+   return 0;
+
control = RATP_CONTROL_RST;
 
if (hdr->control & RATP_CONTROL_ACK)
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 07/16] ratp: fix single byte sending flagged with SO

2017-06-21 Thread Aleksander Morgado
When a single data byte is sent, it is flagged as SO and the actual
data goes in the length byte within the header.

Worth noting that this issue wasn't found in any barebox<->bbremote
interaction because all data packets sent between them always have
more than one byte (due to the barebox command specific header).

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index c946bea1a..846f8130c 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -384,6 +384,7 @@ static int ratp_send_next_data(struct ratp_internal *ri)
uint16_t crc;
uint8_t control;
struct ratp_header *hdr;
+   uint8_t *data;
int pktlen;
struct ratp_message *msg;
int len;
@@ -409,19 +410,19 @@ static int ratp_send_next_data(struct ratp_internal *ri)
RATP_CONTROL_ACK;
 
hdr = msg->buf;
+   data = (uint8_t *)(hdr + 1);
 
if (msg->eor)
control |= RATP_CONTROL_EOR;
 
+   pktlen = sizeof(struct ratp_header);
if (len > 1) {
-   void *data = hdr + 1;
-   pktlen = sizeof(*hdr) + len + 2;
+   pktlen += len + 2;
crc = cyg_crc16(data, len);
put_unaligned_be16(crc, data + len);
-   } else {
-   pktlen = sizeof(struct ratp_header);
+   } else if (len == 1) {
control |= RATP_CONTROL_SO;
-   len = 0;
+   len = *data;
}
 
ratp_create_packet(ri, hdr, control, len);
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 01/16] ratp: add missing transition to SYN-RECEIVED in behavior B

2017-06-21 Thread Aleksander Morgado
The reference says:

If the SYN flag was set but the ACK was not set then the other
end of the connection has executed an active open also.
Acknowledge the SYN, choose your MDL, and send:

[PATCH v2 10/16] ratp: fix sending ACKs without data

2017-06-21 Thread Aleksander Morgado
All ACKs without data must be built in the same way from the input
message:
 

[PATCH v2 11/16] ratp: consolidate ratp_sn_expected() and ratp_an_expected()

2017-06-21 Thread Aleksander Morgado
This is no code change in ratp_sn_expected(), it's just rewritten in
the same way as ratp_an_expected(), which follows the RFC916 approach.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 5c52d3b5f..46a2b645c 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -359,7 +359,7 @@ static bool ratp_an_expected(struct ratp_internal *ri, 
struct ratp_header *hdr)
 
 static bool ratp_sn_expected(struct ratp_internal *ri, struct ratp_header *hdr)
 {
-   return ratp_sn(hdr) != ri->sn_received;
+   return ratp_sn(hdr) == (ri->sn_received + 1) % 2;
 }
 
 static int ratp_send_ack(struct ratp_internal *ri, struct ratp_header *hdr)
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 08/16] ratp: remove bogus data checks in behavior C2

2017-06-21 Thread Aleksander Morgado
The SN validation was being completely ignored if the packet had no
data (e.g. for RST, FIN or SYN or plain ACKs). This condition is now
removed so that the SN check is done.

The second check removed was actually never being used, as it was
already being tested for not having data in the first one.

These two fixes are a cleanup to follow the protocol correctly.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c |  8 +---
 scripts/remote/ratp.py | 16 +---
 2 files changed, 6 insertions(+), 18 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 846f8130c..321721ab7 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -721,9 +721,6 @@ static int ratp_behaviour_c2(struct ratp_internal *ri, void 
*pkt)
 
pr_debug("%s\n", __func__);
 
-   if (!ratp_has_data(hdr))
-   return 0;
-
if (ratp_sn_expected(ri, hdr))
return 0;
 
@@ -745,9 +742,6 @@ static int ratp_behaviour_c2(struct ratp_internal *ri, void 
*pkt)
return 1;
}
 
-   if (!ratp_has_data(hdr))
-   return 1;
-
pr_debug("Sending ack for duplicate message\n");
ret = ratp_send_ack(ri, hdr);
if (ret)
@@ -1846,4 +1840,4 @@ eor:
}
 
return 0;
-}
\ No newline at end of file
+}
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index a41d2e8a3..0dfc8420c 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -339,9 +339,6 @@ class RatpConnection(object):
 def _c2(self, r):
 logging.info("C2")
 
-if r.length == 0 and r.c_so == 0:
-return True
-
 if r.c_sn != self._r_sn:
 return True
 
@@ -358,14 +355,11 @@ class RatpConnection(object):
 self._state = RatpState.closed
 return False
 
-# FIXME: only ack duplicate data packages?
-# This is not documented in RFC 916
-if r.length or r.c_so:
-logging.info("C2: duplicate data packet, dropping")
-s = RatpPacket(flags='A')
-s.c_sn = r.c_an
-s.c_an = (r.c_sn + 1) % 2
-self._write(s)
+logging.info("C2: duplicate packet")
+s = RatpPacket(flags='A')
+s.c_sn = r.c_an
+s.c_an = (r.c_sn + 1) % 2
+self._write(s)
 
 return False
 
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 02/16] ratp: avoid unnecessary variable initializations

2017-06-21 Thread Aleksander Morgado
This is just a cleanup; the variables are completely initialized later
on so the initial values are totally discarded anyway.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 0d384aa4e..e9536499e 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -382,7 +382,7 @@ static int ratp_send_ack(struct ratp_internal *ri, struct 
ratp_header *hdr)
 static int ratp_send_next_data(struct ratp_internal *ri)
 {
uint16_t crc;
-   uint8_t control = RATP_CONTROL_ACK;
+   uint8_t control;
struct ratp_header *hdr;
int pktlen;
struct ratp_message *msg;
@@ -594,7 +594,7 @@ static void ratp_behaviour_b(struct ratp_internal *ri, void 
*pkt)
 
if ((hdr->control & RATP_CONTROL_ACK) && !ratp_an_expected(ri, hdr)) {
if (!(hdr->control & RATP_CONTROL_RST)) {
-   uint8_t control = RATP_CONTROL_RST;
+   uint8_t control;
 
control = RATP_CONTROL_RST |
ratp_set_sn(ratp_an(hdr));
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 15/16] ratp: consolidate setting the next AN or SN flags

2017-06-21 Thread Aleksander Morgado
Setting the next AN or SN flag was being done in two different ways
throughout the code; e.g. here for AN:

ratp_set_an(ratp_sn(hdr) + 1);
or:
ratp_set_an(!ratp_sn(hdr));

We define a pair of new ratp_set_next_sn() and ratp_set_next_an()
macros that make it clear that the next value is desired, and also
hide the computation of the actual flag value within the macro, so the
previous example would now look like:

ratp_set_next_an(ratp_sn(hdr));

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 30 --
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 2dee41009..46a82c69a 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -139,8 +139,10 @@ static bool ratp_an(struct ratp_header *hdr)
return hdr->control & RATP_CONTROL_AN ? 1 : 0;
 }
 
-#define ratp_set_sn(sn) (((sn) % 2) ? RATP_CONTROL_SN : 0)
-#define ratp_set_an(an) (((an) % 2) ? RATP_CONTROL_AN : 0)
+#define ratp_set_sn(sn) (sn ? RATP_CONTROL_SN : 0)
+#define ratp_set_an(an) (an ? RATP_CONTROL_AN : 0)
+#define ratp_set_next_sn(sn) (((sn + 1) % 2) ? RATP_CONTROL_SN : 0)
+#define ratp_set_next_an(an) (((an + 1) % 2) ? RATP_CONTROL_AN : 0)
 
 static inline int ratp_header_ok(struct ratp_internal *ri, struct ratp_header 
*h)
 {
@@ -368,7 +370,7 @@ static int ratp_send_ack(struct ratp_internal *ri, struct 
ratp_header *hdr)
int ret;
 
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_ACK;
 
ret = ratp_send_hdr(ri, control);
@@ -404,8 +406,8 @@ static int ratp_send_next_data(struct ratp_internal *ri)
 
len = msg->len;
 
-   control = ratp_set_sn(ri->sn_sent + 1) |
-   ratp_set_an(ri->sn_received + 1) |
+   control = ratp_set_next_sn(ri->sn_sent) |
+   ratp_set_next_an(ri->sn_received) |
RATP_CONTROL_ACK;
 
hdr = msg->buf;
@@ -630,7 +632,7 @@ static void ratp_behaviour_b(struct ratp_internal *ri, void 
*pkt)
} else {
struct ratp_header synack = {};
 
-   control = ratp_set_an(!ratp_sn(hdr)) |
+   control = ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_SYN |
RATP_CONTROL_ACK;
 
@@ -734,7 +736,7 @@ static int ratp_behaviour_c2(struct ratp_internal *ri, void 
*pkt)
pr_debug("Error: Connection reset\n");
 
control = RATP_CONTROL_RST | RATP_CONTROL_ACK |
-   ratp_set_sn(ratp_an(hdr)) | ratp_set_an(!ratp_sn(hdr));
+   ratp_set_sn(ratp_an(hdr)) | 
ratp_set_next_an(ratp_sn(hdr));
ratp_send_hdr(ri, control);
 
ratp_state_change(ri, RATP_STATE_CLOSED);
@@ -1035,7 +1037,7 @@ static int ratp_behaviour_g(struct ratp_internal *ri, 
void *pkt)
if (hdr->control & RATP_CONTROL_ACK)
control |= ratp_set_sn(ratp_an(hdr));
else
-   control |= ratp_set_an(ratp_sn(hdr) + 1) | RATP_CONTROL_ACK;
+   control |= ratp_set_next_an(ratp_sn(hdr)) | RATP_CONTROL_ACK;
 
ratp_send_hdr(ri, control);
 
@@ -1099,7 +1101,7 @@ static int ratp_behaviour_h2(struct ratp_internal *ri, 
void *pkt)
ri->status = -ENETDOWN;
 
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_FIN |
RATP_CONTROL_ACK;
 
@@ -1165,7 +1167,7 @@ static int ratp_behaviour_h3(struct ratp_internal *ri, 
void *pkt)
 
if (ratp_has_data(hdr)) {
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_RST |
RATP_CONTROL_ACK;
ratp_send_hdr(ri, control);
@@ -1176,7 +1178,7 @@ static int ratp_behaviour_h3(struct ratp_internal *ri, 
void *pkt)
}
 
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_ACK;
 
expected = ratp_an_expected(ri, hdr);
@@ -1278,7 +1280,7 @@ static int ratp_behaviour_h6(struct ratp_internal *ri, 
void *pkt)
if (!(hdr->control & RATP_CONTROL_FIN))
return 1;
 
-   control = ratp_set_sn(ratp_an(hdr) + 1) | RATP_CONTROL_ACK;
+   control = ratp_set_next_sn(ratp_an(hdr)) | RATP_CONTROL_ACK;
 
ratp_send_hdr(ri, control);
 
@@ -1695,8 +1697,8 @@ void ratp_close(struct ratp *ratp)
 
ratp_state_change(ri, RATP_STATE_FIN_WAIT);
 
-   control = ratp_set_sn(!ri->sn_sent) |
-   ratp_set_an(ri->

[PATCH v2 14/16] ratp: don't ignore data that may arrive in behaviour H1

2017-06-21 Thread Aleksander Morgado
If an input packet arrives H1 that has data in it, we need to:
  * track sn_received
  * if we have data pending, send it
  * if we don't have data pending, send a plain ACK

This process, as noted in RFC916, is the same as the I1 procedure, so
go and run it:

 Go to the ESTABLISHED state and execute procedure I1 to process
 any data which might be in this packet.

This fix allows the peer to queue data in the last packet doing the
connection establishment. It doesn't apply to the barebox<->bbremote
interaction because bbremote won't queue data until the connection is
completely established, but it allows third party ratp implementations
to do that.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c |  8 +++-
 scripts/remote/ratp.py | 14 ++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index e810a9e54..2dee41009 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1042,6 +1042,8 @@ static int ratp_behaviour_g(struct ratp_internal *ri, 
void *pkt)
return 0;
 }
 
+static int ratp_behaviour_i1(struct ratp_internal *ri, void *pkt);
+
 /*
  * Our SYN has been acknowledged.  At this point we are
  * technically in the ESTABLISHED state.  Send any initial data
@@ -1062,7 +1064,11 @@ static int ratp_behaviour_h1(struct ratp_internal *ri, 
void *pkt)
 
ratp_state_change(ri, RATP_STATE_ESTABLISHED);
 
-   return 0;
+   /* If the input message has data (i.e. it is not just an ACK
+* without data) then we need to send back an ACK ourselves,
+* or even data if we have it pending. This is the same
+* procedure done in i1, so just run it. */
+   return ratp_behaviour_i1 (ri, pkt);
 }
 
 /*
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index e6b3e19b6..7972d31f2 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -489,12 +489,8 @@ class RatpConnection(object):
 
 def _h1(self, r):
 logging.info("H1")
-
-# FIXME: initial data?
 self._state = RatpState.established
-self._r_sn = r.c_sn
-
-return False
+return self._common_i1(r)
 
 def _h2(self, r):
 logging.info("H2")
@@ -584,9 +580,7 @@ class RatpConnection(object):
 self._time_wait_deadline = monotonic() + self._get_rto()
 return False
 
-def _i1(self, r):
-logging.info("I1")
-
+def _common_i1(self, r):
 if r.c_so:
 self._r_sn = r.c_sn
 self._rx_buf.append(chr(r.length))
@@ -608,6 +602,10 @@ class RatpConnection(object):
 self._write(s)
 return False
 
+def _i1(self, r):
+logging.info("I1")
+return self._common_i1(r)
+
 def _machine(self, pkt):
 logging.info("State: %r", self._state)
 if self._state == RatpState.listen:
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 16/16] ratp: user close may happen in SYN-RECEIVED state

2017-06-21 Thread Aleksander Morgado
The reference says:

 5.2.3. SYN-RECEIVED
 ...
 Departures
   - A CLOSE request is made by the user.  Create a packet with
 FIN set.  Send it and go to the FIN-WAIT state.

Add this missing step.

Probably not a real usecase for barebox anyway as there is no user
triggered close.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 2 +-
 scripts/remote/ratp.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 46a82c69a..e7fbf640a 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1689,7 +1689,7 @@ void ratp_close(struct ratp *ratp)
if (!ri)
return;
 
-   if (ri->state == RATP_STATE_ESTABLISHED) {
+   if (ri->state == RATP_STATE_ESTABLISHED || ri->state == 
RATP_STATE_SYN_RECEIVED) {
uint64_t start;
u8 control;
 
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index 7972d31f2..44f3e2f40 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -721,7 +721,7 @@ class RatpConnection(object):
 def close(self, timeout=1.0):
 deadline = monotonic() + timeout
 logging.info("CLOSE")
-if self._state == RatpState.established:
+if self._state == RatpState.established or self._state == 
RatpState.syn_received:
 fin = RatpPacket(flags='FA')
 fin.c_sn = (self._s_sn + 1) % 2
 fin.c_an = (self._r_sn + 1) % 2
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 09/16] ratp: remove FIXME comment: FIN always requires ACK

2017-06-21 Thread Aleksander Morgado
Section 3.4 in the RFC916 shows a packet flow for the connection close
where the initial packet sent by the endpoint starting the close has
just the FIN flag set, without an ACK:

  --> 

[PATCH v2 12/16] ratp: prefer using ratp_send_ack() in behaviour I1

2017-06-21 Thread Aleksander Morgado
Instead of manually constructing an ACK, use the ratp_send_ack()
method, which already does that properly.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 12 +++-
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 46a2b645c..c7f3f4171 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1330,7 +1330,6 @@ static int msg_recv(struct ratp_internal *ri, void *pkt)
 static int ratp_behaviour_i1(struct ratp_internal *ri, void *pkt)
 {
struct ratp_header *hdr = pkt;
-   uint8_t control = 0;
 
if (!ratp_has_data (hdr))
return 1;
@@ -1341,15 +1340,10 @@ static int ratp_behaviour_i1(struct ratp_internal *ri, 
void *pkt)
 
msg_recv(ri, pkt);
 
-   if (list_empty(>sendmsg) || ri->sendmsg_current) {
-   control = ratp_set_sn(!ri->sn_sent) |
-   ratp_set_an(ri->sn_received + 1) |
-   RATP_CONTROL_ACK;
-
-   ratp_send_hdr(ri, control);
-   } else {
+   if (list_empty(>sendmsg) || ri->sendmsg_current)
+   ratp_send_ack(ri, hdr);
+   else
ratp_send_next_data(ri);
-   }
 
return 0;
 }
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 13/16] ratp: send initial data in behaviour B if any pending

2017-06-21 Thread Aleksander Morgado
And also, use ratp_send_ack() instead of manually constructing an ACK
if no data is pending to be sent.

The current barebox implementation doesn't allow any queueing of data
until the connection is established, so this is probably not a case
that would get run anyway.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index c7f3f4171..e810a9e54 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -622,11 +622,11 @@ static void ratp_behaviour_b(struct ratp_internal *ri, 
void *pkt)
ri->sn_received = ratp_sn(hdr);
 
if (hdr->control & RATP_CONTROL_ACK) {
-   control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(!ratp_sn(hdr)) |
-   RATP_CONTROL_ACK;
-   ratp_send_hdr(ri, control);
ratp_state_change(ri, RATP_STATE_ESTABLISHED);
+   if (list_empty(>sendmsg) || ri->sendmsg_current)
+   ratp_send_ack(ri, hdr);
+   else
+   ratp_send_next_data(ri);
} else {
struct ratp_header synack = {};
 
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] Documentation: remote-control: fix typo

2017-05-30 Thread Aleksander Morgado
Correctly reference `--port` instead of `--baudrate` when explaining
how to specify the port.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 Documentation/user/remote-control.rst | 7 +++
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/Documentation/user/remote-control.rst 
b/Documentation/user/remote-control.rst
index 99c2181df..217251429 100644
--- a/Documentation/user/remote-control.rst
+++ b/Documentation/user/remote-control.rst
@@ -53,9 +53,9 @@ configuring bbremote
 
 
 bbremote needs the port and possibly the baudrate to access the remote
-barebox. The port can be configured with the ``--baudrate`` option or
-with the ``BBREMOTE_PORT`` environment variable. The port can either be
-the device special file if it's a local port or if it's a remote port a
+barebox. The port can be configured with the ``--port`` option or with
+the ``BBREMOTE_PORT`` environment variable. The port can either be the
+device special file if it's a local port or if it's a remote port a
 string of the form: ``rfc2217://host:port``. The baudrate can be given
 with the ``--baudrate`` option or the ``BBREMOTE_BAUDRATE`` environment
 variable. For the rest of this document it is assumed that ``bbremote``
@@ -131,4 +131,3 @@ This can be mounted on barebox using the regular mount 
command using
   bbremote --export=somedir console
   mkdir -p /ratpfs; mount -t ratpfs none /ratpfs
   ls /ratpfs
-
-- 
2.12.2


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 0/2] Enabling support for the FIFO based console in sandbox

2017-05-31 Thread Aleksander Morgado
Hey,

I've been trying to make the FIFO based extra sandbox console work for some 
time now, and ended up preparing a couple of patches that seem to serve the 
purpose. I'm not totally sure whether this hasn't been working for a long time 
or if I was doing somethin wrong myself (possibly!).

The first one is a fix to allow registering multiple sandbox consoles; looks 
like this was not possible with the current codebase as the additional consoles 
were all being registered as devices with the same name and id.

The second patch is open for discussion. I wasn't able to make the setup work 
with separate console devices registered for input and output, the only way I 
could make it work was registering a console that did both input and output, so 
I ended up modifying it so that the logic of the application allows only that, 
an extra bidirectional console using two separate FIFO files. If either --stdin 
or --stdout is not given, or if either of them gets given multiple times, an 
error is issued. With this setup it works for me, I can run barebox and use 
either the default stdin/stdout console or the FIFO based one, but maybe we're 
losing other useful usecases.

What do you think?

Aleksander Morgado (2):
  sandbox: fix registering multiple consoles
  sandbox: --stdin and --stdout allow max one bidirectional console

 arch/sandbox/board/console.c   |  5 ++-
 arch/sandbox/mach-sandbox/include/mach/linux.h |  2 +-
 arch/sandbox/os/common.c   | 50 +++---
 drivers/serial/linux_console.c |  3 ++
 4 files changed, 44 insertions(+), 16 deletions(-)

--
2.13.0

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 2/2] sandbox: --stdin and --stdout allow max one bidirectional console

2017-05-31 Thread Aleksander Morgado
Allow up to one bidirectional FIFO/file based console in addition to
the default stdin/stdout console that is always registered.

We avoid opening the FIFO files while parsing the options because the
whole logic may block if e.g. trying to open the --stdout FIFO and
there is no reader in the other end. So instead, we store the --stdout
and --stdin file paths given, and we open both sequentially once all
the options have been parsed. This also allows us to validate that at
most a single pair of --stdin and --stdout paths has been given.
e.g.:
term1 $ mkfifo /tmp/bbstdin
term1 $ mkfifo /tmp/bbstdout
term1 $ ./barebox -I /tmp/bbstdin -O /tmp/bbstdout
   (blocks until a reader is available in the stdout FIFO)

term2 $ cat /tmp/bbstdout & cat > /tmp/bbstdin
   (starts reader and writer, which triggers barebox to continue)

If only one console is activated (CONFIG_CONSOLE_ACTIVATE_ALL=n), the
default stdin/stdout console will be preferred instead of the new
FIFO/file based, which would need to be activated explicitly later on.
e.g.:
barebox@barebox sandbox:/ cs1.active=ioe

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 arch/sandbox/os/common.c | 48 +---
 1 file changed, 37 insertions(+), 11 deletions(-)

diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 192917ac2..0461abaac 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -39,6 +39,7 @@
 #include 
 #include 
 #include 
+#include 
 /*
  * ...except the ones needed to connect with barebox
  */
@@ -329,9 +330,11 @@ static const char optstring[] = "hm:i:e:d:O:I:x:y:";
 int main(int argc, char *argv[])
 {
void *ram;
-   int opt, ret, fd;
+   int opt, ret;
int malloc_size = CONFIG_MALLOC_SIZE;
int fdno = 0, envno = 0, option_index = 0;
+   char path_stdout[PATH_MAX + 1] = { 0 };
+   char path_stdin[PATH_MAX + 1] = { 0 };
 
while (1) {
option_index = 0;
@@ -360,22 +363,18 @@ int main(int argc, char *argv[])
}
break;
case 'O':
-   fd = open(optarg, O_WRONLY);
-   if (fd < 0) {
-   perror("open");
+   if (path_stdout[0]) {
+   printf("error: cannot specify -O,--stdout 
multiple times\n");
exit(1);
}
-
-   barebox_register_console(-1, fd);
+   strncpy(path_stdout, optarg, PATH_MAX);
break;
case 'I':
-   fd = open(optarg, O_RDWR);
-   if (fd < 0) {
-   perror("open");
+   if (path_stdin[0]) {
+   printf("error: cannot specify -I,--stdin 
multiple times\n");
exit(1);
}
-
-   barebox_register_console(fd, -1);
+   strncpy(path_stdin, optarg, PATH_MAX);
break;
case 'x':
sdl_xres = strtoul(optarg, NULL, 0);
@@ -426,6 +425,33 @@ int main(int argc, char *argv[])
}
}
 
+   /* Register additional FIFO console */
+   if (path_stdout[0] || path_stdin[0]) {
+   int fdout, fdin;
+
+   /* Both must be given to build a FIFO console */
+   if (!path_stdin[0] || !path_stdout[0]) {
+   printf("error: both -I,--stdin and -O,--stdout must be"
+  "specified to enable the FIFO console\n");
+   exit(1);
+   }
+
+   fdout = open(path_stdout, O_WRONLY);
+   if (fdout < 0) {
+   perror("open stdout");
+   exit(1);
+   }
+
+   fdin = open(path_stdin, O_RDWR);
+   if (fdin < 0) {
+   perror("open stdin");
+   exit(1);
+   }
+
+   barebox_register_console(fdin, fdout);
+   }
+
+   /* Register default stdin/stdout console */
barebox_register_console(fileno(stdin), fileno(stdout));
 
rawmode();
-- 
2.13.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 1/2] sandbox: fix registering multiple consoles

2017-05-31 Thread Aleksander Morgado
Consoles need to be registered with the "console" device name so that
they are probed by the correct driver. The barebox_register_console()
was already forcing this as it was overwriting the name that was being
passed as argument, but it was failing to provide a unique id for
each new console, so the underlying register_device() would just
return an error when wanting to re-register a device with device name
"console" and id 0.

We remove the unused name parameter from barebox_register_console() as
it is really nowhere used, and also specify DEVICE_ID_DYNAMIC as id,
so that a new unique device id is given to each newly registered
console device.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 arch/sandbox/board/console.c   | 5 ++---
 arch/sandbox/mach-sandbox/include/mach/linux.h | 2 +-
 arch/sandbox/os/common.c   | 6 +++---
 drivers/serial/linux_console.c | 3 +++
 4 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/arch/sandbox/board/console.c b/arch/sandbox/board/console.c
index cd5ad5723..cf1781d15 100644
--- a/arch/sandbox/board/console.c
+++ b/arch/sandbox/board/console.c
@@ -22,7 +22,7 @@
 #include 
 #include 
 
-int barebox_register_console(char *name, int stdinfd, int stdoutfd)
+int barebox_register_console(int stdinfd, int stdoutfd)
 {
struct device_d *dev;
struct linux_console_data *data;
@@ -32,9 +32,8 @@ int barebox_register_console(char *name, int stdinfd, int 
stdoutfd)
data = (struct linux_console_data *)(dev + 1);
 
dev->platform_data = data;
-   strcpy(dev->name, name);
-
strcpy(dev->name, "console");
+   dev->id = DEVICE_ID_DYNAMIC;
 
data->stdoutfd = stdoutfd;
data->stdinfd  = stdinfd;
diff --git a/arch/sandbox/mach-sandbox/include/mach/linux.h 
b/arch/sandbox/mach-sandbox/include/mach/linux.h
index 1f11ed449..1327a56ca 100644
--- a/arch/sandbox/mach-sandbox/include/mach/linux.h
+++ b/arch/sandbox/mach-sandbox/include/mach/linux.h
@@ -19,7 +19,7 @@ void __attribute__((noreturn)) linux_exit(void);
 
 int linux_execve(const char * filename, char *const argv[], char *const 
envp[]);
 
-int barebox_register_console(char *name_template, int stdinfd, int stdoutfd);
+int barebox_register_console(int stdinfd, int stdoutfd);
 
 int barebox_register_dtb(const void *dtb);
 
diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 67667d40d..192917ac2 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -366,7 +366,7 @@ int main(int argc, char *argv[])
exit(1);
}
 
-   barebox_register_console("cout", -1, fd);
+   barebox_register_console(-1, fd);
break;
case 'I':
fd = open(optarg, O_RDWR);
@@ -375,7 +375,7 @@ int main(int argc, char *argv[])
exit(1);
}
 
-   barebox_register_console("cin", fd, -1);
+   barebox_register_console(fd, -1);
break;
case 'x':
sdl_xres = strtoul(optarg, NULL, 0);
@@ -426,7 +426,7 @@ int main(int argc, char *argv[])
}
}
 
-   barebox_register_console("console", fileno(stdin), fileno(stdout));
+   barebox_register_console(fileno(stdin), fileno(stdout));
 
rawmode();
start_barebox();
diff --git a/drivers/serial/linux_console.c b/drivers/serial/linux_console.c
index 760b3b81f..0d5da9d1b 100644
--- a/drivers/serial/linux_console.c
+++ b/drivers/serial/linux_console.c
@@ -73,6 +73,9 @@ static int linux_console_probe(struct device_d *dev)
 
console_register(cdev);
 
+   pr_info("%s: registered as %s%d\n", dev->name, cdev->class_dev.name,
+   cdev->class_dev.id);
+
return 0;
 }
 
-- 
2.13.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] sandbox: new --stdinout option to enable a bidirectional console

2017-06-02 Thread Aleksander Morgado
In addition to allowing read-only and write-only consoles with --stdin
and --stdout, we now allow bidirectional read/write consoles with FIFO
files. This is e.g. to allow doing RATP over the FIFO based consoles.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 arch/sandbox/os/common.c | 53 ++--
 1 file changed, 42 insertions(+), 11 deletions(-)

diff --git a/arch/sandbox/os/common.c b/arch/sandbox/os/common.c
index 8cf087313..665e8194e 100644
--- a/arch/sandbox/os/common.c
+++ b/arch/sandbox/os/common.c
@@ -312,26 +312,28 @@ static int add_dtb(const char *file)
 static void print_usage(const char*);
 
 static struct option long_options[] = {
-   {"help",   0, 0, 'h'},
-   {"malloc", 1, 0, 'm'},
-   {"image",  1, 0, 'i'},
-   {"env",1, 0, 'e'},
-   {"dtb",1, 0, 'd'},
-   {"stdout", 1, 0, 'O'},
-   {"stdin",  1, 0, 'I'},
-   {"xres",  1, 0, 'x'},
-   {"yres",  1, 0, 'y'},
+   {"help", 0, 0, 'h'},
+   {"malloc",   1, 0, 'm'},
+   {"image",1, 0, 'i'},
+   {"env",  1, 0, 'e'},
+   {"dtb",  1, 0, 'd'},
+   {"stdout",   1, 0, 'O'},
+   {"stdin",1, 0, 'I'},
+   {"stdinout", 1, 0, 'B'},
+   {"xres", 1, 0, 'x'},
+   {"yres", 1, 0, 'y'},
{0, 0, 0, 0},
 };
 
-static const char optstring[] = "hm:i:e:d:O:I:x:y:";
+static const char optstring[] = "hm:i:e:d:O:I:B:x:y:";
 
 int main(int argc, char *argv[])
 {
void *ram;
-   int opt, ret, fd;
+   int opt, ret, fd, fd2;
int malloc_size = CONFIG_MALLOC_SIZE;
int fdno = 0, envno = 0, option_index = 0;
+   char *aux;
 
while (1) {
option_index = 0;
@@ -421,6 +423,31 @@ int main(int argc, char *argv[])
 
barebox_register_console(fd, -1);
break;
+   case 'B':
+   aux = strchr(optarg, ',');
+   if (!aux) {
+   printf("-B, --stdinout requires two file paths 
given\n");
+   exit(1);
+   }
+
+   /* open stdout file */
+   fd = open(aux + 1, O_WRONLY);
+   if (fd < 0) {
+   perror("open stdout");
+   exit(1);
+   }
+
+   /* open stdin file */
+   aux = strndup(optarg, aux - optarg);
+   fd2 = open(aux, O_RDWR);
+   if (fd2 < 0) {
+   perror("open stdin");
+   exit(1);
+   }
+   free(aux);
+
+   barebox_register_console(fd2, fd);
+   break;
default:
break;
}
@@ -463,6 +490,10 @@ static void print_usage(const char *prgname)
 "can be a regular file or a FIFO.\n"
 "  -I, --stdin=   Register a file as a console capable of doing stdin.\n"
 "can be a regular file or a FIFO.\n"
+"  -B, --stdinout=,\n"
+"   Register a bidirectional console capable of doing 
both\n"
+"   stdin and stdout.  and  can be 
regular\n"
+"   files or FIFOs.\n"
 "  -x, --xres= SDL width.\n"
 "  -y, --yres= SDL height.\n",
prgname
-- 
2.13.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: avoid using already freed memory

2017-06-02 Thread Aleksander Morgado
If ratp_establish() fails we would be accessing the ratp_internal
struct after having disposed it.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index d596a0e8b..22e83636f 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1658,6 +1658,8 @@ int ratp_establish(struct ratp *ratp, bool active, int 
timeout_ms)
}
 
 out:
+   ri->in_ratp--;
+
if (ret) {
free(ri->recvbuf);
free(ri->sendbuf);
@@ -1665,8 +1667,6 @@ out:
ratp->internal = NULL;
}
 
-   ri->in_ratp--;
-
return ret;
 }
 
-- 
2.13.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


RFC: barebox RATP C library?

2017-06-05 Thread Aleksander Morgado
Hey,

I've been playing a bit with the possibility of having a C library to
talk RATP with barebox, totally equivalent to what bbremote does in
python, but as a general C lib with a stable API that may be
integrated in other C/C++ applications.

>From my POV I see two options to try:
   a) build a library based on lib/ratp.c and common/ratp.c but
without directly sharing the source code; i.e. just take bits and
pieces from those implementations where necessary, and write the
library as any other userspace library.
   b) build a small library that allows including lib/ratp.c (and
maybe common/ratp.c) directly in the build, but which would require
those files to be updated in a way that allow being shared by a
separate library that isn't running in the whole barebox runtime
context.

I'm not sure if anyone has thoughts on this; I initially thought b)
would be definitely the way to go, but the current implementation
seems too tied to the actual barebox runtime, so maybe it's just
easier to setup a) and just share e.g. the barebox RATP message format
structs, enums and so on.

Comments, suggestions?

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH] Documentation: remote-control: fix typo

2017-06-01 Thread Aleksander Morgado
On Thu, Jun 1, 2017 at 2:31 PM, Sascha Hauer <s.ha...@pengutronix.de> wrote:
>> >> Correctly reference `--port` instead of   `--baudrate` when explaining
>> >> how to specify the port.
>> >>
>> >> Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
>> >> ---
>> >>  Documentation/user/remote-control.rst | 7 +++
>> >>  1 file changed, 3 insertions(+), 4 deletions(-)
>> >
>> > Applied, thanks
>>
>> Sorry I'm a bit out of the loop, in which git repository are the
>> patches applied? Didn't see them in
>> https://git.pengutronix.de/cgit/barebox so I assume you have a 'next'
>> repo somewhere?
>
> I haven't pushed yet, try again. The repository is the correct one.

Got it now, thanks.

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH] Documentation: remote-control: fix typo

2017-06-01 Thread Aleksander Morgado
On Thu, Jun 1, 2017 at 9:30 AM, Sascha Hauer <s.ha...@pengutronix.de> wrote:
> On Tue, May 30, 2017 at 11:46:34PM +0200, Aleksander Morgado wrote:
>> Correctly reference `--port` instead of   `--baudrate` when explaining
>> how to specify the port.
>>
>> Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
>> ---
>>  Documentation/user/remote-control.rst | 7 +++
>>  1 file changed, 3 insertions(+), 4 deletions(-)
>
> Applied, thanks

Sorry I'm a bit out of the loop, in which git repository are the
patches applied? Didn't see them in
https://git.pengutronix.de/cgit/barebox so I assume you have a 'next'
repo somewhere?

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH] sandbox: new --stdinout option to enable a bidirectional console

2017-06-06 Thread Aleksander Morgado
On Tue, Jun 6, 2017 at 8:53 AM, Sascha Hauer <s.ha...@pengutronix.de> wrote:
>> In addition to allowing read-only and write-only consoles with --stdin
>> and --stdout, we now allow bidirectional read/write consoles with FIFO
>> files. This is e.g. to allow doing RATP over the FIFO based consoles.
>>
>> Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
>> ---
>>  arch/sandbox/os/common.c | 53 
>> ++--
>>  1 file changed, 42 insertions(+), 11 deletions(-)
>
> Applied, thanks.
>
> Have you tried RATP over the FIFO consoles? Does it work as expected?

Not completely, because the pyserial backend in bbremote didn't want
FIFOs, but I did try with my own minimal RATP implementation and it
did work ok up to where I tested.

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: only allow bidirectional consoles

2017-06-06 Thread Aleksander Morgado
Ignore requests to switch to RATP mode in input-only or output-only
consoles.

This actually also avoids segfaulting later on:

#0  0x in ?? ()
#1  0x0040b2a4 in console_send (r=, 
pkt=0x7fffd8ec, len=4) at common/ratp.c:102
#2  0x0042ab99 in ratp_behaviour_a (pkt=0x77298780, 
ri=0x772766d0) at lib/ratp.c:530
#3  ratp_state_machine (pkt=0x77298780, ri=0x772766d0) at 
lib/ratp.c:1384
#4  ratp_poll (ratp=0x77277ed8) at lib/ratp.c:1561
#5  0x0042b2ab in ratp_establish (ratp=ratp@entry=0x77277ed8, 
active=active@entry=false, timeout_ms=timeout_ms@entry=100) at lib/ratp.c:1645
#6  0x0040b888 in barebox_ratp (cdev=cdev@entry=0x77212bd0) at 
common/ratp.c:470
#7  0x004046c3 in getc_raw () at common/console.c:416
...

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/common/ratp.c b/common/ratp.c
index e879e2b3c..7be86d49a 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -443,6 +443,9 @@ int barebox_ratp(struct console_device *cdev)
struct ratp_ctx *ctx;
struct ratp *ratp;

+   if (!cdev->getc || !cdev->putc)
+   return -EINVAL;
+
if (ratp_command_ctx) {
ctx = ratp_command_ctx;
} else {
--
2.12.2

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 06/16] ratp: fix data presence check

2017-06-15 Thread Aleksander Morgado
Looking at the "data length" and SO flag isn't enough to declare a
packet with or without data, because SYN flagged packets will also use
the "data length" field to define MDL.

So, improve the check to match against SYN|RST|FIN flagged packets,
which can never have data.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 4 ++--
 scripts/remote/ratp.py | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index d3c252047..c946bea1a 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -165,7 +165,7 @@ static bool ratp_has_data(struct ratp_header *hdr)
 {
if (hdr->control & RATP_CONTROL_SO)
return 1;
-   if (hdr->data_length)
+   if (!(hdr->control & (RATP_CONTROL_SYN | RATP_CONTROL_RST | 
RATP_CONTROL_FIN)) && hdr->data_length)
return 1;
return 0;
 }
@@ -1338,7 +1338,7 @@ static int ratp_behaviour_i1(struct ratp_internal *ri, 
void *pkt)
struct ratp_header *hdr = pkt;
uint8_t control = 0;
 
-   if (!hdr->data_length && !(hdr->control & RATP_CONTROL_SO))
+   if (!ratp_has_data (hdr))
return 1;
 
pr_vdebug("%s **received** %d\n", __func__, hdr->data_length);
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index 079fb871a..a41d2e8a3 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -525,7 +525,7 @@ class RatpConnection(object):
 # Our fin was lost, rely on retransmission
 return False
 
-if r.length or r.c_so:
+if (r.length and not r.c_syn and not r.c_rst and not r.c_fin) or 
r.c_so:
 self._retrans = None
 s = RatpPacket(flags='RA')
 s.c_sn = r.c_an
@@ -596,7 +596,7 @@ class RatpConnection(object):
 if r.c_so:
 self._r_sn = r.c_sn
 self._rx_buf.append(chr(r.length))
-elif r.length:
+elif r.length and not r.c_syn and not r.c_rst and not r.c_fin:
 self._r_sn = r.c_sn
 self._rx_buf.append(r.payload)
 else:
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 03/16] ratp: send missing RST in behavior C2

2017-06-15 Thread Aleksander Morgado
The reference says:

If SYN was set we assume that the other end crashed and has
attempted to open a new connection.  We respond by sending a
legal reset:

[PATCH 05/16] ratp: completely ignore RST flagged packets in behavior G

2017-06-15 Thread Aleksander Morgado
The reference says:

This procedure represents the behavior of the CLOSED state of a
connection. All incoming packets are discarded. If the packet
had the RST flag set take no action. Otherwise it is necessary
to build a RST packet.

So, skip building the RST packet if the incoming one had RST set.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/ratp.c b/lib/ratp.c
index 43b8b04dc..d3c252047 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1033,6 +1033,9 @@ static int ratp_behaviour_g(struct ratp_internal *ri, 
void *pkt)
 
pr_debug("%s\n", __func__);
 
+   if (hdr->control & RATP_CONTROL_RST)
+   return 0;
+
control = RATP_CONTROL_RST;
 
if (hdr->control & RATP_CONTROL_ACK)
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 04/16] ratp: add missing RST flag in behavior G

2017-06-15 Thread Aleksander Morgado
The reference says:

   If the ACK flag was set then send:
   

[PATCH 00/16] RATP logic fixes and improvements

2017-06-15 Thread Aleksander Morgado
Hey Sascha,

I went through the RFC916 and ended up preparing a set of fixes and 
improvements for the RATP logic in barebox.
Let me know what you think.

Cheers!

Aleksander Morgado (16):
  ratp: add missing transition to SYN-RECEIVED in behavior B
  ratp: avoid unnecessary variable initializations
  ratp: send missing RST in behavior C2
  ratp: add missing RST flag in behavior G
  ratp: completely ignore RST flagged packets in behavior G
  ratp: fix data presence check
  ratp: fix single byte sending flagged with SO
  ratp: remove bogus data checks in behavior C2
  ratp: remove FIXME comment: FIN always requires ACK
  ratp: fix sending ACKs without data
  ratp: consolidate ratp_sn_expected() and ratp_an_expected()
  ratp: prefer using ratp_send_ack() in behaviour I1
  ratp: send initial data in behaviour B if any pending
  ratp: don't ignore data that may arrive in behaviour H1
  ratp: consolidate setting the next AN or SN flags
  ratp: user close may happen in SYN-RECEIVED state

 lib/ratp.c | 116 +++--
 scripts/remote/ratp.py |  38 +++-
 2 files changed, 78 insertions(+), 76 deletions(-)

--
2.13.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


libratp, libratp-barebox and ratp-barebox-cli

2017-06-15 Thread Aleksander Morgado
Hey,

For anyone interested, I ended up preparing a pair of C libraries and
a CLI tool that allow controlling barebox remotely using RATP, in the
same way as bbremote does it. The libraries would allow easy
integration on C applications that may want to remotely control
barebox.

Repositories:
  https://github.com/aleksander0m/libratp
  https://github.com/aleksander0m/libratp-barebox

Some initial 0.0.2 tagged versions here:
  https://aleksander.es/software/libratp-0.0.2.tar.xz
  https://aleksander.es/software/libratp-barebox-0.0.2.tar.xz

APIs:
  https://aleksander0m.github.io/libratp
  https://aleksander0m.github.io/libratp-barebox

The libratp library provides a generic RATP implementation, much like
barebox' lib/ratp.c:
  * The library currently supports creating ratp_t objects for TTYs or
FIFO pairs (e.g. for barebox sandbox testing).
  * The library launches a background thread running a libevent main
loop, which takes care of all the async reading from the TTY (or
FIFO). all the timers in the RATP logic and all the link management
state machine.
  * Callbacks may be registered by the user to get reported when new
data arrives or when RATP link state changes. Worth noting that these
callbacks are called from within the support thread.
  * The ratp_close() and ratp_establish() operations in barebox'
lib/ratp.c are blocking, they will not return until the desired final
state has been reached or a timeout happened. In the libratp library
the equivalent operations would be ratp_link_active_open_sync() and
ratp_link_close_sync(); but the library also includes support for just
sending the active open or close requests, without waiting for the
final state to be reached, via ratp_link_active_open() and
ratp_link_close(). This allows to e.g. request an active open and
queue data to be sent, so that the data is directly sent on the ACK
finishing the establishment.
  * The library doesn't make any assumption on the maximum data length
chosen by each peer, so a peer may choose a MDL<255 to receive data in
shorter chunks or even choose MDL=0 to indicate no data may be
received.

The libratp-barebox library "extends" the libratp library with barebox
specific operations. Right now, these are implemented:
 * ratp_barebox_link_ping()
 * ratp_barebox_link_command()
 * ratp_barebox_link_getenv()

The ratp-barebox-cli implements the barebox specific operations in a
CLI, just a simple way to exercise the library APIs:

$ ratp-barebox-cli -t /dev/ttyUSB3 --ping
Sending PING...
PONG received...

$ ratp-barebox-cli -t /dev/ttyUSB3 --getenv global.boot.default
Sending getenv request: global.boot.default
global.boot.default: net

$ ratp-barebox-cli -t /dev/ttyUSB3 --command "ls /dev"
Sending command: ls /dev
Received response (errno Success):
cs0  eeprom0  eeprom1  full
imx-ocotpmem  netconsole-1 null
prng ram0 ratpconsole-1serial0-1
serial1-1serial2-1zero

Cheers!

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 16/16] ratp: user close may happen in SYN-RECEIVED state

2017-06-15 Thread Aleksander Morgado
The reference says:

 5.2.3. SYN-RECEIVED
 ...
 Departures
   - A CLOSE request is made by the user.  Create a packet with
 FIN set.  Send it and go to the FIN-WAIT state.

Add this missing step.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 2 +-
 scripts/remote/ratp.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 46a82c69a..e7fbf640a 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1689,7 +1689,7 @@ void ratp_close(struct ratp *ratp)
if (!ri)
return;
 
-   if (ri->state == RATP_STATE_ESTABLISHED) {
+   if (ri->state == RATP_STATE_ESTABLISHED || ri->state == 
RATP_STATE_SYN_RECEIVED) {
uint64_t start;
u8 control;
 
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index 7972d31f2..44f3e2f40 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -721,7 +721,7 @@ class RatpConnection(object):
 def close(self, timeout=1.0):
 deadline = monotonic() + timeout
 logging.info("CLOSE")
-if self._state == RatpState.established:
+if self._state == RatpState.established or self._state == 
RatpState.syn_received:
 fin = RatpPacket(flags='FA')
 fin.c_sn = (self._s_sn + 1) % 2
 fin.c_an = (self._r_sn + 1) % 2
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 14/16] ratp: don't ignore data that may arrive in behaviour H1

2017-06-15 Thread Aleksander Morgado
If an input packet arrives H1 that has data in it, we need to:
  * track sn_received
  * if we have data pending, send it
  * if we don't have data pending, send a plain ACK

This process, as noted in RFC916, is the same as the I1 procedure, so
go and run it:

 Go to the ESTABLISHED state and execute procedure I1 to process
 any data which might be in this packet.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c |  8 +++-
 scripts/remote/ratp.py | 14 ++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index e810a9e54..2dee41009 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1042,6 +1042,8 @@ static int ratp_behaviour_g(struct ratp_internal *ri, 
void *pkt)
return 0;
 }
 
+static int ratp_behaviour_i1(struct ratp_internal *ri, void *pkt);
+
 /*
  * Our SYN has been acknowledged.  At this point we are
  * technically in the ESTABLISHED state.  Send any initial data
@@ -1062,7 +1064,11 @@ static int ratp_behaviour_h1(struct ratp_internal *ri, 
void *pkt)
 
ratp_state_change(ri, RATP_STATE_ESTABLISHED);
 
-   return 0;
+   /* If the input message has data (i.e. it is not just an ACK
+* without data) then we need to send back an ACK ourselves,
+* or even data if we have it pending. This is the same
+* procedure done in i1, so just run it. */
+   return ratp_behaviour_i1 (ri, pkt);
 }
 
 /*
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index e6b3e19b6..7972d31f2 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -489,12 +489,8 @@ class RatpConnection(object):
 
 def _h1(self, r):
 logging.info("H1")
-
-# FIXME: initial data?
 self._state = RatpState.established
-self._r_sn = r.c_sn
-
-return False
+return self._common_i1(r)
 
 def _h2(self, r):
 logging.info("H2")
@@ -584,9 +580,7 @@ class RatpConnection(object):
 self._time_wait_deadline = monotonic() + self._get_rto()
 return False
 
-def _i1(self, r):
-logging.info("I1")
-
+def _common_i1(self, r):
 if r.c_so:
 self._r_sn = r.c_sn
 self._rx_buf.append(chr(r.length))
@@ -608,6 +602,10 @@ class RatpConnection(object):
 self._write(s)
 return False
 
+def _i1(self, r):
+logging.info("I1")
+return self._common_i1(r)
+
 def _machine(self, pkt):
 logging.info("State: %r", self._state)
 if self._state == RatpState.listen:
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 15/16] ratp: consolidate setting the next AN or SN flags

2017-06-15 Thread Aleksander Morgado
Setting the next AN or SN flag was being done in two different ways
throughout the code; e.g. here for AN:

ratp_set_an(ratp_sn(hdr) + 1);
or:
ratp_set_an(!ratp_sn(hdr));

We define a pair of new ratp_set_next_sn() and ratp_set_next_an()
macros that make it clear that the next value is desired, and also
hide the computation of the actual flag value within the macro, so the
previous example would now look like:

ratp_set_next_an(ratp_sn(hdr));

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 30 --
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 2dee41009..46a82c69a 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -139,8 +139,10 @@ static bool ratp_an(struct ratp_header *hdr)
return hdr->control & RATP_CONTROL_AN ? 1 : 0;
 }
 
-#define ratp_set_sn(sn) (((sn) % 2) ? RATP_CONTROL_SN : 0)
-#define ratp_set_an(an) (((an) % 2) ? RATP_CONTROL_AN : 0)
+#define ratp_set_sn(sn) (sn ? RATP_CONTROL_SN : 0)
+#define ratp_set_an(an) (an ? RATP_CONTROL_AN : 0)
+#define ratp_set_next_sn(sn) (((sn + 1) % 2) ? RATP_CONTROL_SN : 0)
+#define ratp_set_next_an(an) (((an + 1) % 2) ? RATP_CONTROL_AN : 0)
 
 static inline int ratp_header_ok(struct ratp_internal *ri, struct ratp_header 
*h)
 {
@@ -368,7 +370,7 @@ static int ratp_send_ack(struct ratp_internal *ri, struct 
ratp_header *hdr)
int ret;
 
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_ACK;
 
ret = ratp_send_hdr(ri, control);
@@ -404,8 +406,8 @@ static int ratp_send_next_data(struct ratp_internal *ri)
 
len = msg->len;
 
-   control = ratp_set_sn(ri->sn_sent + 1) |
-   ratp_set_an(ri->sn_received + 1) |
+   control = ratp_set_next_sn(ri->sn_sent) |
+   ratp_set_next_an(ri->sn_received) |
RATP_CONTROL_ACK;
 
hdr = msg->buf;
@@ -630,7 +632,7 @@ static void ratp_behaviour_b(struct ratp_internal *ri, void 
*pkt)
} else {
struct ratp_header synack = {};
 
-   control = ratp_set_an(!ratp_sn(hdr)) |
+   control = ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_SYN |
RATP_CONTROL_ACK;
 
@@ -734,7 +736,7 @@ static int ratp_behaviour_c2(struct ratp_internal *ri, void 
*pkt)
pr_debug("Error: Connection reset\n");
 
control = RATP_CONTROL_RST | RATP_CONTROL_ACK |
-   ratp_set_sn(ratp_an(hdr)) | ratp_set_an(!ratp_sn(hdr));
+   ratp_set_sn(ratp_an(hdr)) | 
ratp_set_next_an(ratp_sn(hdr));
ratp_send_hdr(ri, control);
 
ratp_state_change(ri, RATP_STATE_CLOSED);
@@ -1035,7 +1037,7 @@ static int ratp_behaviour_g(struct ratp_internal *ri, 
void *pkt)
if (hdr->control & RATP_CONTROL_ACK)
control |= ratp_set_sn(ratp_an(hdr));
else
-   control |= ratp_set_an(ratp_sn(hdr) + 1) | RATP_CONTROL_ACK;
+   control |= ratp_set_next_an(ratp_sn(hdr)) | RATP_CONTROL_ACK;
 
ratp_send_hdr(ri, control);
 
@@ -1099,7 +1101,7 @@ static int ratp_behaviour_h2(struct ratp_internal *ri, 
void *pkt)
ri->status = -ENETDOWN;
 
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_FIN |
RATP_CONTROL_ACK;
 
@@ -1165,7 +1167,7 @@ static int ratp_behaviour_h3(struct ratp_internal *ri, 
void *pkt)
 
if (ratp_has_data(hdr)) {
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_RST |
RATP_CONTROL_ACK;
ratp_send_hdr(ri, control);
@@ -1176,7 +1178,7 @@ static int ratp_behaviour_h3(struct ratp_internal *ri, 
void *pkt)
}
 
control = ratp_set_sn(ratp_an(hdr)) |
-   ratp_set_an(ratp_sn(hdr) + 1) |
+   ratp_set_next_an(ratp_sn(hdr)) |
RATP_CONTROL_ACK;
 
expected = ratp_an_expected(ri, hdr);
@@ -1278,7 +1280,7 @@ static int ratp_behaviour_h6(struct ratp_internal *ri, 
void *pkt)
if (!(hdr->control & RATP_CONTROL_FIN))
return 1;
 
-   control = ratp_set_sn(ratp_an(hdr) + 1) | RATP_CONTROL_ACK;
+   control = ratp_set_next_sn(ratp_an(hdr)) | RATP_CONTROL_ACK;
 
ratp_send_hdr(ri, control);
 
@@ -1695,8 +1697,8 @@ void ratp_close(struct ratp *ratp)
 
ratp_state_change(ri, RATP_STATE_FIN_WAIT);
 
-   control = ratp_set_sn(!ri->sn_sent) |
-   ratp_set_an(ri->

[PATCH 10/16] ratp: fix sending ACKs without data

2017-06-15 Thread Aleksander Morgado
All ACKs without data must be built in the same way from the input
message:
 

[PATCH 08/16] ratp: remove bogus data checks in behavior C2

2017-06-15 Thread Aleksander Morgado
The SN validation was being completely ignored if the packet had no
data (e.g. for RST, FIN or SYN or plain ACKs). This condition is now
removed so that the SN check is done.

The second check removed was actually never being used, as it was
already being tested for not having data in the first one.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c |  8 +---
 scripts/remote/ratp.py | 16 +---
 2 files changed, 6 insertions(+), 18 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 846f8130c..321721ab7 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -721,9 +721,6 @@ static int ratp_behaviour_c2(struct ratp_internal *ri, void 
*pkt)
 
pr_debug("%s\n", __func__);
 
-   if (!ratp_has_data(hdr))
-   return 0;
-
if (ratp_sn_expected(ri, hdr))
return 0;
 
@@ -745,9 +742,6 @@ static int ratp_behaviour_c2(struct ratp_internal *ri, void 
*pkt)
return 1;
}
 
-   if (!ratp_has_data(hdr))
-   return 1;
-
pr_debug("Sending ack for duplicate message\n");
ret = ratp_send_ack(ri, hdr);
if (ret)
@@ -1846,4 +1840,4 @@ eor:
}
 
return 0;
-}
\ No newline at end of file
+}
diff --git a/scripts/remote/ratp.py b/scripts/remote/ratp.py
index a41d2e8a3..0dfc8420c 100644
--- a/scripts/remote/ratp.py
+++ b/scripts/remote/ratp.py
@@ -339,9 +339,6 @@ class RatpConnection(object):
 def _c2(self, r):
 logging.info("C2")
 
-if r.length == 0 and r.c_so == 0:
-return True
-
 if r.c_sn != self._r_sn:
 return True
 
@@ -358,14 +355,11 @@ class RatpConnection(object):
 self._state = RatpState.closed
 return False
 
-# FIXME: only ack duplicate data packages?
-# This is not documented in RFC 916
-if r.length or r.c_so:
-logging.info("C2: duplicate data packet, dropping")
-s = RatpPacket(flags='A')
-s.c_sn = r.c_an
-s.c_an = (r.c_sn + 1) % 2
-self._write(s)
+logging.info("C2: duplicate packet")
+s = RatpPacket(flags='A')
+s.c_sn = r.c_an
+s.c_an = (r.c_sn + 1) % 2
+self._write(s)
 
 return False
 
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 11/16] ratp: consolidate ratp_sn_expected() and ratp_an_expected()

2017-06-15 Thread Aleksander Morgado
This is no code change in ratp_sn_expected(), it's just rewritten in
the same way as ratp_an_expected(), which follows the RFC916 approach.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 5c52d3b5f..46a2b645c 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -359,7 +359,7 @@ static bool ratp_an_expected(struct ratp_internal *ri, 
struct ratp_header *hdr)
 
 static bool ratp_sn_expected(struct ratp_internal *ri, struct ratp_header *hdr)
 {
-   return ratp_sn(hdr) != ri->sn_received;
+   return ratp_sn(hdr) == (ri->sn_received + 1) % 2;
 }
 
 static int ratp_send_ack(struct ratp_internal *ri, struct ratp_header *hdr)
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 01/16] ratp: add missing transition to SYN-RECEIVED in behavior B

2017-06-15 Thread Aleksander Morgado
The reference says:

If the SYN flag was set but the ACK was not set then the other
end of the connection has executed an active open also.
Acknowledge the SYN, choose your MDL, and send:

[PATCH 02/16] ratp: avoid unnecessary variable initializations

2017-06-15 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 0d384aa4e..e9536499e 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -382,7 +382,7 @@ static int ratp_send_ack(struct ratp_internal *ri, struct 
ratp_header *hdr)
 static int ratp_send_next_data(struct ratp_internal *ri)
 {
uint16_t crc;
-   uint8_t control = RATP_CONTROL_ACK;
+   uint8_t control;
struct ratp_header *hdr;
int pktlen;
struct ratp_message *msg;
@@ -594,7 +594,7 @@ static void ratp_behaviour_b(struct ratp_internal *ri, void 
*pkt)
 
if ((hdr->control & RATP_CONTROL_ACK) && !ratp_an_expected(ri, hdr)) {
if (!(hdr->control & RATP_CONTROL_RST)) {
-   uint8_t control = RATP_CONTROL_RST;
+   uint8_t control;
 
control = RATP_CONTROL_RST |
ratp_set_sn(ratp_an(hdr));
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 07/16] ratp: fix single byte sending flagged with SO

2017-06-15 Thread Aleksander Morgado
When a single data byte is sent, it is flagged as SO and the actual
data goes in the length byte within the header.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index c946bea1a..846f8130c 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -384,6 +384,7 @@ static int ratp_send_next_data(struct ratp_internal *ri)
uint16_t crc;
uint8_t control;
struct ratp_header *hdr;
+   uint8_t *data;
int pktlen;
struct ratp_message *msg;
int len;
@@ -409,19 +410,19 @@ static int ratp_send_next_data(struct ratp_internal *ri)
RATP_CONTROL_ACK;
 
hdr = msg->buf;
+   data = (uint8_t *)(hdr + 1);
 
if (msg->eor)
control |= RATP_CONTROL_EOR;
 
+   pktlen = sizeof(struct ratp_header);
if (len > 1) {
-   void *data = hdr + 1;
-   pktlen = sizeof(*hdr) + len + 2;
+   pktlen += len + 2;
crc = cyg_crc16(data, len);
put_unaligned_be16(crc, data + len);
-   } else {
-   pktlen = sizeof(struct ratp_header);
+   } else if (len == 1) {
control |= RATP_CONTROL_SO;
-   len = 0;
+   len = *data;
}
 
ratp_create_packet(ri, hdr, control, len);
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 09/16] ratp: remove FIXME comment: FIN always requires ACK

2017-06-15 Thread Aleksander Morgado
Section 3.4 in the RFC916 shows a packet flow for the connection close
where the initial packet sent by the endpoint starting the close has
just the FIN flag set, without an ACK:

  --> 

[PATCH 12/16] ratp: prefer using ratp_send_ack() in behaviour I1

2017-06-15 Thread Aleksander Morgado
Instead of manually constructing an ACK, use the ratp_send_ack()
method, which already does that properly.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 lib/ratp.c | 12 +++-
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/lib/ratp.c b/lib/ratp.c
index 46a2b645c..c7f3f4171 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1330,7 +1330,6 @@ static int msg_recv(struct ratp_internal *ri, void *pkt)
 static int ratp_behaviour_i1(struct ratp_internal *ri, void *pkt)
 {
struct ratp_header *hdr = pkt;
-   uint8_t control = 0;
 
if (!ratp_has_data (hdr))
return 1;
@@ -1341,15 +1340,10 @@ static int ratp_behaviour_i1(struct ratp_internal *ri, 
void *pkt)
 
msg_recv(ri, pkt);
 
-   if (list_empty(>sendmsg) || ri->sendmsg_current) {
-   control = ratp_set_sn(!ri->sn_sent) |
-   ratp_set_an(ri->sn_received + 1) |
-   RATP_CONTROL_ACK;
-
-   ratp_send_hdr(ri, control);
-   } else {
+   if (list_empty(>sendmsg) || ri->sendmsg_current)
+   ratp_send_ack(ri, hdr);
+   else
ratp_send_next_data(ri);
-   }
 
return 0;
 }
-- 
2.13.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 00/16] RATP logic fixes and improvements

2017-06-19 Thread Aleksander Morgado
Hey,

On 19/06/17 08:46, Sascha Hauer wrote:
>> I went through the RFC916 and ended up preparing a set of fixes and 
>> improvements for the RATP logic in barebox.
>> Let me know what you think.
> As far as I can say the patches look good. It's quite a while since I
> last looked at the RATP code, so I can't really judge. To which extent
> are the patches tested? Have you explicitly tested for the corner cases
> you fix in each patch? You probably have tested against your new
> library. Have you also tested against the python implementation?

I did test against bbremote, and also did several fixes there as well. I 
haven't tested against the "ratp filesystem support" feature though, maybe I 
should do that as well. 

Regarding which corner cases are tested, well, some of them apply to code paths 
that I believe wouldn't really apply to barebox right now (e.g. barebox doing 
active open at the same time as bbremote doing active open), so that's hard to 
test. I could go one by one over each patch and try to provide logs 
before/after applying the patch, how about that?

BTW; how would you debug barebox (e.g. get the debug messages generated) while 
testing the RATP link over the TTY? Right now I validated the barebox behavior 
just by looking at which RATP messages were returned to me.

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] console_countdown: ignore errors in getchar()

2017-09-20 Thread Aleksander Morgado
The getchar() call may return an error reported as a -1, e.g. when the
console detects a RATP message and switches to RATP mode.

In general it probably is a good idea to ignore these errors in the
console countdown operation; and in particular for the RATP usecase,
this also prevents from interfering with the countdown and menu just
when switching one of the consoles to RATP mode.

As a hint, this is what the standard console was printing due to this
issue when the RATP console was activated:

   Hit m for menu or any other key to stop autoboot: [: missing `]'

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/console_countdown.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/common/console_countdown.c b/common/console_countdown.c
index 03b9b3353..36da1ce57 100644
--- a/common/console_countdown.c
+++ b/common/console_countdown.c
@@ -47,12 +47,14 @@ int console_countdown(int timeout_s, unsigned flags, char 
*out_key)
do {
if (tstc()) {
key = getchar();
-   if (flags & CONSOLE_COUNTDOWN_ANYKEY)
-   goto out;
-   if (flags & CONSOLE_COUNTDOWN_RETURN && key == '\n')
-   goto out;
-   if (flags & CONSOLE_COUNTDOWN_CTRLC && key == 3)
-   goto out;
+   if (key >= 0) {
+   if (flags & CONSOLE_COUNTDOWN_ANYKEY)
+   goto out;
+   if (flags & CONSOLE_COUNTDOWN_RETURN && key == 
'\n')
+   goto out;
+   if (flags & CONSOLE_COUNTDOWN_CTRLC && key == 3)
+   goto out;
+   }
key = 0;
}
if ((flags & CONSOLE_COUNTDOWN_EXTERN) &&
-- 
2.14.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: move barebox-specific command to ratp_bb.h

2018-01-28 Thread Aleksander Morgado
The ratp_run_command() is really an implementation detail of the
barebox ratp 'command' operation support. Move it to the barebox
specific header and rename it with the correct prefix.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c | 2 +-
 include/ratp.h| 2 --
 include/ratp_bb.h | 7 ---
 lib/readline.c| 4 ++--
 4 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index c5eae2e2c..6db4df870 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -323,7 +323,7 @@ static int ratp_console_register(struct ratp_ctx *ctx)
return 0;
 }
 
-void ratp_run_command(void)
+void barebox_ratp_command_run(void)
 {
int ret;
 
diff --git a/include/ratp.h b/include/ratp.h
index 94fd004f4..6f4cf8a6f 100644
--- a/include/ratp.h
+++ b/include/ratp.h
@@ -17,6 +17,4 @@ int ratp_poll(struct ratp *ratp);
 bool ratp_closed(struct ratp *ratp);
 bool ratp_busy(struct ratp *ratp);
 
-void ratp_run_command(void);
-
 #endif /* __RATP_H */
\ No newline at end of file
diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index 52ecaff37..70fd58a67 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -8,8 +8,9 @@ struct ratp_bb_pkt {
uint8_t data[];
 };
 
-int barebox_ratp(struct console_device *cdev);
-int barebox_ratp_fs_call(struct ratp_bb_pkt *tx, struct ratp_bb_pkt **rx);
-int barebox_ratp_fs_mount(const char *path);
+int  barebox_ratp(struct console_device *cdev);
+void barebox_ratp_command_run(void);
+int  barebox_ratp_fs_call(struct ratp_bb_pkt *tx, struct ratp_bb_pkt **rx);
+int  barebox_ratp_fs_mount(const char *path);
 
 #endif /* __RATP_BB_H */
diff --git a/lib/readline.c b/lib/readline.c
index b5d99ca18..1e380abec 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -3,7 +3,7 @@
 #include 
 #include 
 #include 
-#include 
+#include 
 #include 
 #include 
 #include 
@@ -203,7 +203,7 @@ int readline(const char *prompt, char *buf, int len)
while (!tstc()) {
poller_call();
if (IS_ENABLED(CONFIG_RATP))
-   ratp_run_command();
+   barebox_ratp_command_run();
}
 
ichar = read_key();
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: remove unused list pointer from ratp_bb_pkt

2018-01-28 Thread Aleksander Morgado
This struct type is used in the RATP FS implementation, but was never
used as a list element anywhere.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 include/ratp_bb.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index 70fd58a67..f485f7d8a 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -2,8 +2,6 @@
 #define __RATP_BB_H
 
 struct ratp_bb_pkt {
-   struct list_head list;
-
unsigned int len;
uint8_t data[];
 };
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 06/10] ratp: implement generic command support

2018-02-02 Thread Aleksander Morgado
The RATP implementation now allows executing generic commands with a
binary interface: binary requests are received and binary responses
are returned.

Each command can define its own RATP request contents (e.g. to specify
command-specific options) as well as its own RATP response contents
(if any data is to be returned).

Each command is associated with a numeric unique command ID, and for
easy reference these IDs are maintained in the common ratp_bb header.
Modules may override generic implemented commands or include their own
new ones (as long as the numeric IDs introduced are unique).

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 arch/arm/lib32/barebox.lds.S  |   4 +
 arch/arm/lib64/barebox.lds.S  |   4 +
 arch/blackfin/boards/ipe337/barebox.lds.S |   5 +-
 arch/mips/lib/barebox.lds.S   |   4 +
 arch/nios2/cpu/barebox.lds.S  |   5 +-
 arch/openrisc/cpu/barebox.lds.S   |   4 +
 arch/ppc/boards/pcm030/barebox.lds.S  |   4 +
 arch/ppc/mach-mpc85xx/barebox.lds.S   |   4 +
 arch/sandbox/board/barebox.lds.S  |   5 +
 arch/x86/lib/barebox.lds.S|   7 +
 arch/x86/mach-efi/elf_ia32_efi.lds.S  |   5 +
 arch/x86/mach-efi/elf_x86_64_efi.lds.S|   5 +
 common/module.lds.S   |   2 +
 common/ratp.c | 255 ++
 include/asm-generic/barebox.lds.h |   2 +
 include/ratp_bb.h |  47 ++
 16 files changed, 258 insertions(+), 104 deletions(-)

diff --git a/arch/arm/lib32/barebox.lds.S b/arch/arm/lib32/barebox.lds.S
index e7b87b7cd..6fadc2a35 100644
--- a/arch/arm/lib32/barebox.lds.S
+++ b/arch/arm/lib32/barebox.lds.S
@@ -85,6 +85,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/arm/lib64/barebox.lds.S b/arch/arm/lib64/barebox.lds.S
index 240699f1a..a53b933bb 100644
--- a/arch/arm/lib64/barebox.lds.S
+++ b/arch/arm/lib64/barebox.lds.S
@@ -82,6 +82,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/blackfin/boards/ipe337/barebox.lds.S 
b/arch/blackfin/boards/ipe337/barebox.lds.S
index 51a586af2..7e82a1bd7 100644
--- a/arch/blackfin/boards/ipe337/barebox.lds.S
+++ b/arch/blackfin/boards/ipe337/barebox.lds.S
@@ -68,6 +68,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
___barebox_cmd_end = .;
 
+   ___barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   ___barebox_ratp_cmd_end = .;
+
___barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
___barebox_magicvar_end = .;
@@ -91,4 +95,3 @@ SECTIONS
___bss_stop = .;
_end = .;
 }
-
diff --git a/arch/mips/lib/barebox.lds.S b/arch/mips/lib/barebox.lds.S
index 899f62b96..660d4be85 100644
--- a/arch/mips/lib/barebox.lds.S
+++ b/arch/mips/lib/barebox.lds.S
@@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/nios2/cpu/barebox.lds.S b/arch/nios2/cpu/barebox.lds.S
index a2d7fa8cd..fbcd1cd3f 100644
--- a/arch/nios2/cpu/barebox.lds.S
+++ b/arch/nios2/cpu/barebox.lds.S
@@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
@@ -129,4 +133,3 @@ SECTIONS
_end = .;
PROVIDE (end = .);
 }
-
diff --git a/arch/openrisc/cpu/barebox.lds.S b/arch/openrisc/cpu/barebox.lds.S
index b819ca099..c6807aec3 100644
--- a/arch/openrisc/cpu/barebox.lds.S
+++ b/arch/openrisc/cpu/barebox.lds.S
@@ -57,6 +57,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS } > ram
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS } > ram
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS } > ram
__barebox_magicvar_end = .;
diff -

[RFC PATCH 00/10] ratp: new generic RATP command support

2018-02-02 Thread Aleksander Morgado
Until now, the barebox-specific RATP commands were all defined and
implemented in common/ratp.c. This series of patches allow ratp
commands to be defined in a similar way to console commands.

The first patches (1-5) break the current RATP API, by introducing
the concept of requests, responses and indications:
 * Requests sent to one endpoint are expected to be replied with
   a response by the peer endpoint.
 * Indications are messages sent from one endpoint to another which
   are not expected to be replied.

The current RATP operations are reformatted using this approach, by
specifying the message type in the until now unused 'flags' field of
the RATP barebox message, and making all messages of the same
operation share the same command id.

The next patches (6-8) add support to specifying RATP commands in
separate implementation files, defined with some new helper
BAREBOX_RATP_CMD_START/BAREBOX_RATP_CMD_END macros. The getenv and
ping commands are updated to use this new approach.

The last patches (9-10) implement three new commands via RATP: reset,
md and mw. Both md and mw operations are defined by a binary API, and
allow reading/writing memory without needing to do any kind of
parsing (as it was the case when e.g. running the md or mw console
commands).

The new commands were tested with the libratp-barebox library
(wip/md-mw branch) in https://github.com/aleksander0m/libratp-barebox

What do you think of these changes? The initial RATP API break is bad
but not sure how many other RATP API users are around except for
bbremote (ported along with the changes) and the libratp-barebox I'm
writing.

Aleksander Morgado (10):
  ratp: define message type flags
  ratp: port command operation to req/rsp/ind format
  ratp: port ping operation to req/rsp format
  ratp: port getenv operation to req/rsp format
  ratp: port filesystem operation to req/rsp format
  ratp: implement generic command support
  ratp: implement ping as a standard ratp command
  ratp: implement getenv as a standard ratp command
  ratp: new reset command
  ratp: new md and mw commands

 arch/arm/lib32/barebox.lds.S  |   4 +
 arch/arm/lib64/barebox.lds.S  |   4 +
 arch/blackfin/boards/ipe337/barebox.lds.S |   5 +-
 arch/mips/lib/barebox.lds.S   |   4 +
 arch/nios2/cpu/barebox.lds.S  |   5 +-
 arch/openrisc/cpu/barebox.lds.S   |   4 +
 arch/ppc/boards/pcm030/barebox.lds.S  |   4 +
 arch/ppc/mach-mpc85xx/barebox.lds.S   |   4 +
 arch/sandbox/board/barebox.lds.S  |   5 +
 arch/x86/lib/barebox.lds.S|   7 +
 arch/x86/mach-efi/elf_ia32_efi.lds.S  |   5 +
 arch/x86/mach-efi/elf_x86_64_efi.lds.S|   5 +
 commands/Makefile |   2 +
 commands/md.c | 209 ++
 commands/mw.c | 150 +++-
 commands/ratp-getenv.c|  50 ++
 commands/ratp-ping.c  |  38 
 commands/reset.c  |  48 -
 common/module.lds.S   |   2 +
 common/ratp.c | 283 +++---
 include/asm-generic/barebox.lds.h |   2 +
 include/ratp_bb.h |  49 ++
 scripts/remote/controller.py  |  71 
 scripts/remote/messages.py|  90 ++
 scripts/remote/ratpfs.py  |   6 +-
 25 files changed, 800 insertions(+), 256 deletions(-)
 create mode 100644 commands/ratp-getenv.c
 create mode 100644 commands/ratp-ping.c

-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 10/10] ratp: new md and mw commands

2018-02-02 Thread Aleksander Morgado
This commit introduces support for running the md and mw commands
using the binary interface provided by RAPT. This allows clients to
read and write memory files without needing to do custom string
parsing on the data returned by the console 'md' and 'mw' operations.

The request and response messages used for these new operations are
structured in the same way:

 * An initial fixed-sized section includes the fixed-sized
   variables (e.g. integers), as well as the size and offset of the
   variable-length variables.

 * After the initial fixed-sized section, the buffer is given, which
   contains the variable-length variables in the offsets previously
   defined and with the size previously defined.

The message also defines separately the offset of the buffer
w.r.t. the start of the message. The endpoint reading the message will
use this information to decide where the buffer starts. This allows to
extend the message format in the future without needing to break the
message API, as new fields can be appended to the fixed-sized section
as long as the buffer offset is also updated to report the new
position of the buffer.

E.g. testing with ratp-barebox-cli:

  $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
--timeout 1000
  Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  00:00:00:00:00

  $ ratp-barebox-cli -t /dev/ttyUSB2 --mw 
"/dev/pic_eeprom_rdu,0x107,01:02:03:04:05" --timeout 1000
  Sending mw request: write '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  5/5 bytes written

  $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
--timeout 1000
  Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  01:02:03:04:05

  $ ratp-barebox-cli -t /dev/ttyUSB2 --mw 
"/dev/pic_eeprom_rdu,0x107,00:00:00:00:00" --timeout 1000
  Sending mw request: write '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  5/5 bytes written

  $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
--timeout 1000
  Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  00:00:00:00:00

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 commands/md.c | 209 ++
 commands/mw.c | 150 ++-
 include/ratp_bb.h |   2 +
 3 files changed, 330 insertions(+), 31 deletions(-)

diff --git a/commands/md.c b/commands/md.c
index 3e83c723a..6cd3b9bcf 100644
--- a/commands/md.c
+++ b/commands/md.c
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 2011 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ * Copyright (c) 2011-2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ * Copyright (c) 2018 Zodiac Inflight Innovations
  *
  * See file CREDITS for list of people who contributed to this
  * project.
@@ -23,6 +24,7 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -36,66 +38,84 @@
 
 extern char *mem_rw_buf;
 
-static int do_mem_md(int argc, char *argv[])
+static int common_mem_md(const char *filename,
+loff_t start,
+loff_t size,
+int swab,
+int mode,
+uint8_t *output)
 {
-   loff_t  start = 0, size = 0x100;
-   int r, now;
-   int ret = 0;
+   int r, now, t;
+   int ret = 0;
int fd;
-   char *filename = "/dev/mem";
-   int mode = O_RWSIZE_4;
-   int swab = 0;
void *map;
 
-   if (argc < 2)
-   return COMMAND_ERROR_USAGE;
-
-   if (mem_parse_options(argc, argv, "bwlqs:x", , , NULL,
-   ) < 0)
-   return 1;
-
-   if (optind < argc) {
-   if (parse_area_spec(argv[optind], , )) {
-   printf("could not parse: %s\n", argv[optind]);
-   return 1;
-   }
-   if (size == ~0)
-   size = 0x100;
-   }
-
fd = open_and_lseek(filename, mode | O_RDONLY, start);
if (fd < 0)
return 1;
 
map = memmap(fd, PROT_READ);
if (map != (void *)-1) {
-   ret = memory_display(map + start, start, size,
-   mode >> O_RWSIZE_SHIFT, swab);
+   if (output)
+   memcpy(output, (uint8_t *)(map + start), size);
+else
+   ret = memory_display(map + start, start, size,
+mode >> O_RWSIZE_SHIFT, swab);
goto out;
}
 
+   t = 0;
do {
now = min(size, (loff_t)RW_BUF_SIZE);
r = read(fd, mem_rw_buf, now);
if (r < 0) {
+   ret = -errno;
perror("read");
goto out;
 

[PATCH 09/10] ratp: new reset command

2018-02-02 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 commands/reset.c | 48 +---
 1 file changed, 41 insertions(+), 7 deletions(-)

diff --git a/commands/reset.c b/commands/reset.c
index 6eac53262..7971b9f2f 100644
--- a/commands/reset.c
+++ b/commands/reset.c
@@ -19,10 +19,26 @@
 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 
+static int common_reset (int shutdown_flag)
+{
+   debug("running reset %s\n", shutdown_flag ? "" : "(forced)");
+
+   if (shutdown_flag)
+   shutdown_barebox();
+
+   restart_machine();
+
+   /* Not reached */
+   return 1;
+}
+
+/* Console command */
+
 static int cmd_reset(int argc, char *argv[])
 {
int opt, shutdown_flag;
@@ -39,13 +55,7 @@ static int cmd_reset(int argc, char *argv[])
}
}
 
-   if (shutdown_flag)
-   shutdown_barebox();
-
-   restart_machine();
-
-   /* Not reached */
-   return 1;
+   return common_reset (shutdown_flag);
 }
 
 BAREBOX_CMD_HELP_START(reset)
@@ -61,3 +71,27 @@ BAREBOX_CMD_START(reset)
BAREBOX_CMD_HELP(cmd_reset_help)
BAREBOX_CMD_COMPLETE(empty_complete)
 BAREBOX_CMD_END
+
+/* RATP command */
+
+struct ratp_bb_reset {
+   struct ratp_bb header;
+   uint8_tforce;
+} __attribute__((packed));
+
+static int ratp_cmd_reset(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_reset *reset_req = (struct ratp_bb_reset *)req;
+
+   if (req_len < sizeof (*reset_req)) {
+   printf ("ratp reset ignored: size mismatch (%d < %zu)\n", 
req_len, sizeof (*reset_req));
+   return 2;
+   }
+
+   return common_reset (reset_req->force);
+}
+
+BAREBOX_RATP_CMD_START(RESET)
+   .cmd = ratp_cmd_reset
+BAREBOX_RATP_CMD_END
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 02/10] ratp: port command operation to req/rsp/ind format

2018-02-02 Thread Aleksander Morgado
The commands executed by the client via RATP are processed in the
following way:

 * The client sends a 'command' packet to the barebox console.
 * The result (errno) of the command is returned to the client via a
   'command_return' message.
 * The standard output of the command executed is sent to the client
   via 'consolemsg' packets.

If the client doesn't need any explicit response to the command sent
(e.g. when simulating a console in the client side), it may also just
do the following:

 * The client sends 'consolemsg' packets to the barebox console.

We now consolidate this process using the request, response and
indication packet flags, and making them part of the same 'console'
packet type. In this specific message type, indications may be sent in
both directions.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c| 28 +++-
 scripts/remote/controller.py | 32 
 scripts/remote/messages.py   | 28 +++-
 3 files changed, 46 insertions(+), 42 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index a1fa6fd5f..7d7fb5fcd 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -31,9 +31,7 @@
 #include 
 #include 
 
-#define BB_RATP_TYPE_COMMAND   1
-#define BB_RATP_TYPE_COMMAND_RETURN2
-#define BB_RATP_TYPE_CONSOLEMSG3
+#define BB_RATP_TYPE_CONSOLE   1
 #define BB_RATP_TYPE_PING  4
 #define BB_RATP_TYPE_PONG  5
 #define BB_RATP_TYPE_GETENV6
@@ -120,7 +118,8 @@ static void ratp_queue_console_tx(struct ratp_ctx *ctx)
unsigned int now, maxlen = 255 - sizeof(*rbb);
int ret;
 
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_CONSOLEMSG);
+   rbb->type = cpu_to_be16(BB_RATP_TYPE_CONSOLE);
+   rbb->flags = cpu_to_be16(BB_RATP_FLAG_INDICATION);
 
while (1) {
now = min(maxlen, kfifo_len(ctx->console_transmit_fifo));
@@ -149,7 +148,8 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
rbb = buf;
rbb_ret = buf + sizeof(*rbb);
 
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_COMMAND_RETURN);
+   rbb->type = cpu_to_be16(BB_RATP_TYPE_CONSOLE);
+   rbb->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
rbb_ret->errno = cpu_to_be32(errno);
 
ret = ratp_send(>ratp, buf, len);
@@ -211,27 +211,29 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
int dlen = len - sizeof(struct ratp_bb);
char *varname;
int ret = 0;
+   uint16_t flags = be16_to_cpu(rbb->flags);
 
switch (be16_to_cpu(rbb->type)) {
-   case BB_RATP_TYPE_COMMAND:
+   case BB_RATP_TYPE_CONSOLE:
+   if (flags & BB_RATP_FLAG_RESPONSE)
+   break;
+
+   if (flags & BB_RATP_FLAG_INDICATION) {
+   kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
+   break;
+   }
+
if (ratp_command)
return 0;
 
ratp_command = xmemdup_add_zero(>data, dlen);
ratp_ctx = ctx;
pr_debug("got command: %s\n", ratp_command);
-
break;
 
-   case BB_RATP_TYPE_COMMAND_RETURN:
case BB_RATP_TYPE_PONG:
break;
 
-   case BB_RATP_TYPE_CONSOLEMSG:
-
-   kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
-   break;
-
case BB_RATP_TYPE_PING:
ret = ratp_bb_send_pong(ctx);
break;
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index a7257ecc9..c3f29334a 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -20,17 +20,17 @@ except:
 
 
 def unpack(data):
-p_type, = struct.unpack("!H", data[:2])
-logging.debug("unpack: %r data=%r", p_type, repr(data))
-if p_type == BBType.command:
-logging.debug("received: command")
-return BBPacketCommand(raw=data)
-elif p_type == BBType.command_return:
-logging.debug("received: command_return")
-return BBPacketCommandReturn(raw=data)
-elif p_type == BBType.consolemsg:
-logging.debug("received: consolemsg")
-return BBPacketConsoleMsg(raw=data)
+p_type, p_flag = struct.unpack("!HH", data[:4])
+logging.debug("unpack: type %r flag %r data=%r", p_type, p_flag, 
repr(data))
+if p_type == BBType.console:
+if p_flag & BBFlag.response:
+logging.debug("received: console response")
+return BBPacketConsoleResponse(raw=data)
+if p_flag & BBFlag.indication:
+logging.debug("received: console indication")
+return BBPacketConsoleIndication(raw=data)
+logging.deb

[PATCH 03/10] ratp: port ping operation to req/rsp format

2018-02-02 Thread Aleksander Morgado
The ping operation executed via RATP is processed in the following way:

 * The client sends a 'ping' packet to barebox.
 * Barebox replies with a 'pong' packet.

We now consolidate this process using the request and response
packet flags, and making them part of the same 'ping' packet type.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c| 12 ++--
 scripts/remote/controller.py | 14 +++---
 scripts/remote/messages.py   | 18 ++
 3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index 7d7fb5fcd..222bf624a 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -32,8 +32,7 @@
 #include 
 
 #define BB_RATP_TYPE_CONSOLE   1
-#define BB_RATP_TYPE_PING  4
-#define BB_RATP_TYPE_PONG  5
+#define BB_RATP_TYPE_PING  2
 #define BB_RATP_TYPE_GETENV6
 #define BB_RATP_TYPE_GETENV_RETURN 7
 #define BB_RATP_TYPE_FS8
@@ -169,7 +168,8 @@ static int ratp_bb_send_pong(struct ratp_ctx *ctx)
buf = xzalloc(len);
rbb = buf;
 
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_PONG);
+   rbb->type = cpu_to_be16(BB_RATP_TYPE_PING);
+   rbb->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
 
ret = ratp_send(>ratp, buf, len);
 
@@ -231,10 +231,10 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
pr_debug("got command: %s\n", ratp_command);
break;
 
-   case BB_RATP_TYPE_PONG:
-   break;
-
case BB_RATP_TYPE_PING:
+   if (flags & BB_RATP_FLAG_RESPONSE)
+   break;
+
ret = ratp_bb_send_pong(ctx);
break;
 
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index c3f29334a..518973038 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -32,11 +32,11 @@ def unpack(data):
 logging.debug("received: console request")
 return BBPacketConsoleRequest(raw=data)
 elif p_type == BBType.ping:
+if p_flag & BBFlag.response:
+logging.debug("received: pong")
+return BBPacketPingResponse(raw=data)
 logging.debug("received: ping")
-return BBPacketPing(raw=data)
-elif p_type == BBType.pong:
-logging.debug("received: pong")
-return BBPacketPong(raw=data)
+return BBPacketPingRequest(raw=data)
 elif p_type == BBType.getenv_return:
 logging.debug("received: getenv_return")
 return BBPacketGetenvReturn(raw=data)
@@ -92,8 +92,8 @@ class Controller(Thread):
 self.fsserver = RatpFSServer(path)
 
 def ping(self):
-self._send(BBPacketPing())
-r = self._expect(BBPacketPong)
+self._send(BBPacketPingRequest())
+r = self._expect(BBPacketPingResponse)
 logging.info("Ping: %r", r)
 if not r:
 return 1
@@ -157,7 +157,7 @@ class Controller(Thread):
 self._txq.put(BBPacketConsoleIndication(text=text))
 
 def send_async_ping(self):
-self._txq.put(BBPacketPing())
+self._txq.put(BBPacketPingRequest())
 
 
 def main():
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index 2f63f1831..6c5601d78 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -13,8 +13,7 @@ class BBFlag(object):
 
 class BBType(object):
 console = 1
-ping = 4
-pong = 5
+ping = 2
 getenv = 6
 getenv_return = 7
 fs = 8
@@ -98,20 +97,23 @@ class BBPacketConsoleIndication(BBPacket):
 return self.text
 
 
-class BBPacketPing(BBPacket):
+class BBPacketPingRequest(BBPacket):
 def __init__(self, raw=None):
-super(BBPacketPing, self).__init__(BBType.ping, raw=raw)
+super(BBPacketPingRequest, self).__init__(BBType.ping,
+  raw=raw)
 
 def __repr__(self):
-return "BBPacketPing()"
+return "BBPacketPingRequest()"
 
 
-class BBPacketPong(BBPacket):
+class BBPacketPingResponse(BBPacket):
 def __init__(self, raw=None):
-super(BBPacketPong, self).__init__(BBType.pong, raw=raw)
+super(BBPacketPingResponse, self).__init__(BBType.ping,
+   BBFlag.response,
+   raw=raw)
 
 def __repr__(self):
-return "BBPacketPong()"
+return "BBPacketPingResponse()"
 
 
 class BBPacketGetenv(BBPacket):
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 01/10] ratp: define message type flags

2018-02-02 Thread Aleksander Morgado
Split message types in 3 different groups:

  * Requests: messages generated by one RATP endpoint and sent to the
the other endpoint to be processed.

  * Responses: messages generated by the RATP endpoint as a result of
having received and processed a specific request message.

  * Indications: messages generated by one RATP endpoint for which
there is no need to generate an explicit response message.

These message types are identified by new command flags.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c  | 4 
 scripts/remote/messages.py | 5 +
 2 files changed, 9 insertions(+)

diff --git a/common/ratp.c b/common/ratp.c
index 80863f81f..a1fa6fd5f 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -41,6 +41,10 @@
 #define BB_RATP_TYPE_FS8
 #define BB_RATP_TYPE_FS_RETURN 9
 
+#define BB_RATP_FLAG_NONE  0
+#define BB_RATP_FLAG_RESPONSE  (1 << 0) /* Packet is a response */
+#define BB_RATP_FLAG_INDICATION(1 << 1) /* Packet is an 
indication */
+
 struct ratp_bb {
uint16_t type;
uint16_t flags;
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index 8e8495b12..7a597bc9d 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -5,6 +5,11 @@ from __future__ import absolute_import, division, 
print_function
 
 import struct
 
+class BBFlag(object):
+none = 0
+response = 1 << 0
+indication = 1 << 1
+
 
 class BBType(object):
 command = 1
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 04/10] ratp: port getenv operation to req/rsp format

2018-02-02 Thread Aleksander Morgado
The getenv operation executed via RATP is processed in the following
way:

 * The client sends a 'getenv' packet to barebox specifying the name
   of the variable to read.
 * Barebox replies with a 'getenv_result' packet including the value
   of the variable read.

We now consolidate this process using the request and response
packet flags, and making them part of the same 'getenv' packet type.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c| 10 ++
 scripts/remote/controller.py | 13 -
 scripts/remote/messages.py   | 19 ++-
 3 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index 222bf624a..2423c0651 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -33,8 +33,7 @@
 
 #define BB_RATP_TYPE_CONSOLE   1
 #define BB_RATP_TYPE_PING  2
-#define BB_RATP_TYPE_GETENV6
-#define BB_RATP_TYPE_GETENV_RETURN 7
+#define BB_RATP_TYPE_GETENV3
 #define BB_RATP_TYPE_FS8
 #define BB_RATP_TYPE_FS_RETURN 9
 
@@ -192,7 +191,8 @@ static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, 
const char *val)
rbb = buf;
strcpy(rbb->data, val);
 
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
+   rbb->type = cpu_to_be16(BB_RATP_TYPE_GETENV);
+   rbb->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
 
ret = ratp_send(>ratp, buf, len);
 
@@ -239,8 +239,10 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
break;
 
case BB_RATP_TYPE_GETENV:
-   varname = xmemdup_add_zero(>data, dlen);
+   if (flags & BB_RATP_FLAG_RESPONSE)
+   break;
 
+   varname = xmemdup_add_zero(>data, dlen);
ret = ratp_bb_send_getenv_return(ctx, getenv(varname));
break;
 
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index 518973038..29aa42ad9 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -37,9 +37,12 @@ def unpack(data):
 return BBPacketPingResponse(raw=data)
 logging.debug("received: ping")
 return BBPacketPingRequest(raw=data)
-elif p_type == BBType.getenv_return:
-logging.debug("received: getenv_return")
-return BBPacketGetenvReturn(raw=data)
+elif p_type == BBType.getenv:
+if p_flag & BBFlag.response:
+logging.debug("received: getenv response")
+return BBPacketGetenvResponse(raw=data)
+logging.debug("received: getenv")
+return BBPacketGetenvRequest(raw=data)
 elif p_type == BBType.fs:
 logging.debug("received: fs")
 return BBPacketFS(raw=data)
@@ -108,8 +111,8 @@ class Controller(Thread):
 return r.exit_code
 
 def getenv(self, varname):
-self._send(BBPacketGetenv(varname=varname))
-r = self._expect(BBPacketGetenvReturn)
+self._send(BBPacketGetenvRequest(varname=varname))
+r = self._expect(BBPacketGetenvResponse)
 return r.text
 
 def close(self):
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index 6c5601d78..88841f4f6 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -14,8 +14,7 @@ class BBFlag(object):
 class BBType(object):
 console = 1
 ping = 2
-getenv = 6
-getenv_return = 7
+getenv = 3
 fs = 8
 fs_return = 9
 
@@ -116,13 +115,14 @@ class BBPacketPingResponse(BBPacket):
 return "BBPacketPingResponse()"
 
 
-class BBPacketGetenv(BBPacket):
+class BBPacketGetenvRequest(BBPacket):
 def __init__(self, raw=None, varname=None):
 self.varname = varname
-super(BBPacketGetenv, self).__init__(BBType.getenv, raw=raw)
+super(BBPacketGetenvRequest, self).__init__(BBType.getenv,
+raw=raw)
 
 def __repr__(self):
-return "BBPacketGetenv(varname=%r)" % self.varname
+return "BBPacketGetenvRequest(varname=%r)" % self.varname
 
 def _unpack_payload(self, payload):
 self.varname = payload
@@ -131,14 +131,15 @@ class BBPacketGetenv(BBPacket):
 return self.varname
 
 
-class BBPacketGetenvReturn(BBPacket):
+class BBPacketGetenvResponse(BBPacket):
 def __init__(self, raw=None, text=None):
 self.text = text
-super(BBPacketGetenvReturn, self).__init__(BBType.getenv_return,
-   raw=raw)
+super(BBPacketGetenvResponse, self).__init__(BBType.getenv,
+ BBFlag.response,
+ raw=raw)
 
 def __repr__(self):
-return "BBPacketGetenvReturn(varvalue=%s)

[PATCH 05/10] ratp: port filesystem operation to req/rsp format

2018-02-02 Thread Aleksander Morgado
The filesystem operation executed via RATP is processed in the
following way:

 * The barebox RATP endpoint sends 'fs' packets to the client, with
   the specific filesystem operations to execute.
 * The client replies with 'fs_result' packets to barebox, containing
   the result of the filesystem operation.

We now consolidate this process using the request and response
packet flags, and making them part of the same 'fs' packet type.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c|  9 ++---
 scripts/remote/controller.py | 12 ++--
 scripts/remote/messages.py   | 20 
 scripts/remote/ratpfs.py |  6 +++---
 4 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index 2423c0651..79b0a9906 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -34,8 +34,7 @@
 #define BB_RATP_TYPE_CONSOLE   1
 #define BB_RATP_TYPE_PING  2
 #define BB_RATP_TYPE_GETENV3
-#define BB_RATP_TYPE_FS8
-#define BB_RATP_TYPE_FS_RETURN 9
+#define BB_RATP_TYPE_FS4
 
 #define BB_RATP_FLAG_NONE  0
 #define BB_RATP_FLAG_RESPONSE  (1 << 0) /* Packet is a response */
@@ -246,7 +245,11 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
ret = ratp_bb_send_getenv_return(ctx, getenv(varname));
break;
 
-   case BB_RATP_TYPE_FS_RETURN:
+   case BB_RATP_TYPE_FS:
+   /* Only responses expected */
+   if (!(flags & BB_RATP_FLAG_RESPONSE))
+   break;
+
pkt = xzalloc(sizeof(*pkt) + dlen);
pkt->len = dlen;
memcpy(pkt->data, >data, dlen);
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index 29aa42ad9..db4af3120 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -44,11 +44,11 @@ def unpack(data):
 logging.debug("received: getenv")
 return BBPacketGetenvRequest(raw=data)
 elif p_type == BBType.fs:
-logging.debug("received: fs")
-return BBPacketFS(raw=data)
-elif p_type == BBType.fs_return:
-logging.debug("received: fs_return")
-return BBPacketFSReturn(raw=data)
+if p_flag & BBFlag.response:
+logging.debug("received: fs response")
+return BBPacketFsResponse(raw=data)
+logging.debug("received: fs request")
+return BBPacketFsRequest(raw=data)
 else:
 logging.debug("received: UNKNOWN")
 return BBPacket(raw=data)
@@ -74,7 +74,7 @@ class Controller(Thread):
 os.write(sys.stdout.fileno(), bbpkt.text)
 elif isinstance(bbpkt, BBPacketPong):
 print("pong",)
-elif isinstance(bbpkt, BBPacketFS):
+elif isinstance(bbpkt, BBPacketFsRequest):
 if self.fsserver != None:
 self._send(self.fsserver.handle(bbpkt))
 
diff --git a/scripts/remote/messages.py b/scripts/remote/messages.py
index 88841f4f6..98dda8b79 100644
--- a/scripts/remote/messages.py
+++ b/scripts/remote/messages.py
@@ -15,8 +15,7 @@ class BBType(object):
 console = 1
 ping = 2
 getenv = 3
-fs = 8
-fs_return = 9
+fs = 4
 
 
 class BBPacket(object):
@@ -148,17 +147,22 @@ class BBPacketGetenvResponse(BBPacket):
 return self.text
 
 
-class BBPacketFS(BBPacket):
+class BBPacketFsRequest(BBPacket):
 def __init__(self, raw=None, payload=None):
-super(BBPacketFS, self).__init__(BBType.fs, payload=payload, raw=raw)
+super(BBPacketFsRequest, self).__init__(BBType.fs,
+payload=payload,
+raw=raw)
 
 def __repr__(self):
-return "BBPacketFS(payload=%r)" % self.payload
+return "BBPacketFsRequest(payload=%r)" % self.payload
 
 
-class BBPacketFSReturn(BBPacket):
+class BBPacketFsResponse(BBPacket):
 def __init__(self, raw=None, payload=None):
-super(BBPacketFSReturn, self).__init__(BBType.fs_return, 
payload=payload, raw=raw)
+super(BBPacketFsResponse, self).__init__(BBType.fs,
+ BBFlag.response,
+ payload=payload,
+ raw=raw)
 
 def __repr__(self):
-return "BBPacketFSReturn(payload=%r)" % self.payload
+return "BBPacketFsResponse(payload=%r)" % self.payload
diff --git a/scripts/remote/ratpfs.py b/scripts/remote/ratpfs.py
index 91ca04454..9e88b03a6 100644
--- a/scripts/remote/ratpfs.py
+++ b/scripts/remote/ratpfs.py
@@ -9,7 +9,7 @@ import stat
 import struct
 from enum import IntEnum
 
-from .messages import BBPacke

[PATCH 08/10] ratp: implement getenv as a standard ratp command

2018-02-02 Thread Aleksander Morgado
Also, use xstrndup() instead of the custom xmemdup_add_zero() as we're
working with strings anyway.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 commands/Makefile  |  1 +
 commands/ratp-getenv.c | 50 ++
 common/ratp.c  | 34 --
 3 files changed, 51 insertions(+), 34 deletions(-)
 create mode 100644 commands/ratp-getenv.c

diff --git a/commands/Makefile b/commands/Makefile
index 012a3ad68..5e5464ec3 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -127,3 +127,4 @@ obj-$(CONFIG_CMD_ZODIAC_PIC)+= pic.o zii-pic-esb.o \
zii-pic-mezz.o zii-pic-niu.o \
zii-pic-rdu.o zii-pic-rdu2.o
 obj-$(CONFIG_RATP) += ratp-ping.o
+obj-$(CONFIG_RATP) += ratp-getenv.o
diff --git a/commands/ratp-getenv.c b/commands/ratp-getenv.c
new file mode 100644
index 0..2daaa63d8
--- /dev/null
+++ b/commands/ratp-getenv.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/*
+ * RATP getenv
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+static int ratp_cmd_getenv(const struct ratp_bb *req, int req_len,
+  struct ratp_bb **rsp, int *rsp_len)
+{
+   int dlen = req_len - sizeof (struct ratp_bb);
+   char *varname;
+   const char *value;
+
+   varname = xstrndup ((const char *)req->data, dlen);
+   value = getenv (varname);
+   free (varname);
+
+   dlen = strlen (value);
+
+   *rsp_len = sizeof(struct ratp_bb) + dlen;
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GETENV);
+   (*rsp)->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
+   memcpy ((*rsp)->data, value, dlen);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(GETENV)
+   .cmd = ratp_cmd_getenv
+BAREBOX_RATP_CMD_END
diff --git a/common/ratp.c b/common/ratp.c
index a880e8e03..e1ecb314c 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -24,7 +24,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -146,30 +145,6 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
return ret;
 }
 
-static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, const char *val)
-{
-   void *buf;
-   struct ratp_bb *rbb;
-   int len, ret;
-
-   if (!val)
-   val = "";
-
-   len = sizeof(*rbb) + strlen(val);
-   buf = xzalloc(len);
-   rbb = buf;
-   strcpy(rbb->data, val);
-
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_GETENV);
-   rbb->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
-
-   ret = ratp_send(>ratp, buf, len);
-
-   free(buf);
-
-   return ret;
-}
-
 static char *ratp_command;
 static struct ratp_ctx *ratp_ctx;
 
@@ -355,7 +330,6 @@ static int dispatch_ratp_message(struct ratp_ctx *ctx, 
const void *buf, int len)
const struct ratp_bb *rbb = buf;
struct ratp_bb_pkt *pkt;
int dlen = len - sizeof(struct ratp_bb);
-   char *varname;
int ret = 0;
uint16_t flags;
uint16_t type = be16_to_cpu(rbb->type);
@@ -394,14 +368,6 @@ static int dispatch_ratp_message(struct ratp_ctx *ctx, 
const void *buf, int len)
pr_debug("got command: %s\n", ratp_command);
break;
 
-   case BB_RATP_TYPE_GETENV:
-   if (flags & BB_RATP_FLAG_RESPONSE)
-   break;
-
-   varname = xmemdup_add_zero(>data, dlen);
-   ret = ratp_bb_send_getenv_return(ctx, getenv(varname));
-   break;
-
case BB_RATP_TYPE_FS:
/* Only responses expected */
if (!(flags & BB_RATP_FLAG_RESPONSE))
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 07/10] ratp: implement ping as a standard ratp command

2018-02-02 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 commands/Makefile|  1 +
 commands/ratp-ping.c | 38 ++
 common/ratp.c| 27 ---
 3 files changed, 39 insertions(+), 27 deletions(-)
 create mode 100644 commands/ratp-ping.c

diff --git a/commands/Makefile b/commands/Makefile
index 00a863919..012a3ad68 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -126,3 +126,4 @@ obj-$(CONFIG_CMD_SEED)  += seed.o
 obj-$(CONFIG_CMD_ZODIAC_PIC)   += pic.o zii-pic-esb.o \
zii-pic-mezz.o zii-pic-niu.o \
zii-pic-rdu.o zii-pic-rdu2.o
+obj-$(CONFIG_RATP) += ratp-ping.o
diff --git a/commands/ratp-ping.c b/commands/ratp-ping.c
new file mode 100644
index 0..837c56762
--- /dev/null
+++ b/commands/ratp-ping.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/*
+ * RATP ping
+ */
+
+#include 
+#include 
+#include 
+
+static int ratp_cmd_ping(const struct ratp_bb *req, int req_len,
+struct ratp_bb **rsp, int *rsp_len)
+{
+   *rsp_len = sizeof(struct ratp_bb);
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_PING);
+   (*rsp)->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(PING)
+   .cmd = ratp_cmd_ping
+BAREBOX_RATP_CMD_END
diff --git a/common/ratp.c b/common/ratp.c
index 2cdb1cd89..a880e8e03 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -146,26 +146,6 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
return ret;
 }
 
-static int ratp_bb_send_pong(struct ratp_ctx *ctx)
-{
-   void *buf;
-   struct ratp_bb *rbb;
-   int len = sizeof(*rbb);
-   int ret;
-
-   buf = xzalloc(len);
-   rbb = buf;
-
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_PING);
-   rbb->flags = cpu_to_be16(BB_RATP_FLAG_RESPONSE);
-
-   ret = ratp_send(>ratp, buf, len);
-
-   free(buf);
-
-   return ret;
-}
-
 static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, const char *val)
 {
void *buf;
@@ -414,13 +394,6 @@ static int dispatch_ratp_message(struct ratp_ctx *ctx, 
const void *buf, int len)
pr_debug("got command: %s\n", ratp_command);
break;
 
-   case BB_RATP_TYPE_PING:
-   if (flags & BB_RATP_FLAG_RESPONSE)
-   break;
-
-   ret = ratp_bb_send_pong(ctx);
-   break;
-
case BB_RATP_TYPE_GETENV:
if (flags & BB_RATP_FLAG_RESPONSE)
break;
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: rename global context variable

2018-01-28 Thread Aleksander Morgado
The 'ratp_command_ctx' seems to specify that this variable is only
used to process command operations, but it really is used in every
async operation (e.g. also in FS), so just rename it to a more generic
name for clarity.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c | 18 +-
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index 7be86d49a..c5eae2e2c 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -204,7 +204,7 @@ static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, 
const char *val)
 }
 
 static char *ratp_command;
-static struct ratp_ctx *ratp_command_ctx;
+static struct ratp_ctx *ratp_ctx;
 
 static int ratp_bb_dispatch(struct ratp_ctx *ctx, const void *buf, int len)
 {
@@ -220,7 +220,7 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
return 0;
 
ratp_command = xmemdup_add_zero(>data, dlen);
-   ratp_command_ctx = ctx;
+   ratp_ctx = ctx;
pr_debug("got command: %s\n", ratp_command);
 
break;
@@ -337,7 +337,7 @@ void ratp_run_command(void)
free(ratp_command);
ratp_command = NULL;
 
-   ratp_bb_send_command_return(ratp_command_ctx, ret);
+   ratp_bb_send_command_return(ratp_ctx, ret);
 }
 
 static const char *ratpfs_mount_path;
@@ -400,7 +400,7 @@ out:
 
 int barebox_ratp_fs_call(struct ratp_bb_pkt *tx, struct ratp_bb_pkt **rx)
 {
-   struct ratp_ctx *ctx = ratp_command_ctx;
+   struct ratp_ctx *ctx = ratp_ctx;
struct ratp_bb *rbb;
int len;
u64 start;
@@ -446,11 +446,11 @@ int barebox_ratp(struct console_device *cdev)
if (!cdev->getc || !cdev->putc)
return -EINVAL;
 
-   if (ratp_command_ctx) {
-   ctx = ratp_command_ctx;
+   if (ratp_ctx) {
+   ctx = ratp_ctx;
} else {
ctx = xzalloc(sizeof(*ctx));
-   ratp_command_ctx = ctx;
+   ratp_ctx = ctx;
ctx->ratp.send = console_send;
ctx->ratp.recv = console_recv;
ctx->console_recv_fifo = kfifo_alloc(512);
@@ -494,7 +494,7 @@ out:
 
 static void barebox_ratp_close(void)
 {
-   if (ratp_command_ctx && ratp_command_ctx->cdev)
-   ratp_console_unregister(ratp_command_ctx);
+   if (ratp_ctx && ratp_ctx->cdev)
+   ratp_console_unregister(ratp_ctx);
 }
 predevshutdown_exitcall(barebox_ratp_close);
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] bbremote: rename command subparser variables

2018-01-28 Thread Aleksander Morgado
Don't reuse unrelated subparser variables for new command subparsers,
make each subparser have its own variable.

Just for consistency really, not a bugfix.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 scripts/remote/main.py | 14 +++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index bd304723b..79203df05 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -139,15 +139,15 @@ parser_run.set_defaults(func=handle_run)
 parser_ping = subparsers.add_parser('ping', help="test connection")
 parser_ping.set_defaults(func=handle_ping)
 
-parser_ping = subparsers.add_parser('getenv', help="get a barebox environment 
variable")
-parser_ping.add_argument('arg', nargs='+', help="variable name")
-parser_ping.set_defaults(func=handle_getenv)
+parser_getenv = subparsers.add_parser('getenv', help="get a barebox 
environment variable")
+parser_getenv.add_argument('arg', nargs='+', help="variable name")
+parser_getenv.set_defaults(func=handle_getenv)
 
-parser_run = subparsers.add_parser('listen', help="listen for an incoming 
connection")
-parser_run.set_defaults(func=handle_listen)
+parser_listen = subparsers.add_parser('listen', help="listen for an incoming 
connection")
+parser_listen.set_defaults(func=handle_listen)
 
-parser_run = subparsers.add_parser('console', help="connect to the console")
-parser_run.set_defaults(func=handle_console)
+parser_console = subparsers.add_parser('console', help="connect to the 
console")
+parser_console.set_defaults(func=handle_console)
 
 args = parser.parse_args()
 logging.basicConfig(level=VERBOSITY[args.verbose],
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: remove unused context fields

2018-01-29 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp.c | 6 --
 1 file changed, 6 deletions(-)

diff --git a/common/ratp.c b/common/ratp.c
index 6db4df870..80863f81f 100644
--- a/common/ratp.c
+++ b/common/ratp.c
@@ -54,14 +54,8 @@ struct ratp_bb_command_return {
 struct ratp_ctx {
struct console_device *cdev;
struct ratp ratp;
-   int ratp_status;
struct console_device ratp_console;
int have_synch;
-   int in_ratp_console;
-
-   u8 sendbuf[256];
-   u8 sendbuf_len;
-
int old_active;
 
struct kfifo *console_recv_fifo;
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] of: base: fix segfault in of_get_machine_compatible()

2018-01-28 Thread Aleksander Morgado
This is running the barebox sandbox:

  Thread 1 "barebox" received signal SIGSEGV, Segmentation fault.
  0x55579e2b in _strchr (s=s@entry=0x0, c=c@entry=44) at 
lib/string.c:251
  251   for(; *s != (char) c; ++s)
  (gdb) bt
  #0  0x55579e2b in _strchr (s=s@entry=0x0, c=c@entry=44) at 
lib/string.c:251
  #1  0x5556fd91 in of_get_machine_compatible () at 
drivers/of/base.c:2380
  #2  0x5556fda8 in of_init_hostname () at drivers/of/base.c:2389
  #3  0xf9e6 in start_barebox () at common/startup.c:106
  #4  0x555a291a in main ()
  (gdb) fr 1
  #1  0x5556fd91 in of_get_machine_compatible () at 
drivers/of/base.c:2380
  2380  p = strchr(name, ',');

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 drivers/of/base.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/of/base.c b/drivers/of/base.c
index 6a582177b..10b62890b 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2376,6 +2376,8 @@ const char *of_get_machine_compatible(void)
 
prop = of_find_property(root_node, "compatible", NULL);
name = of_prop_next_string(prop, NULL);
+   if (!name)
+   return NULL;
 
p = strchr(name, ',');
return p ? p + 1 : name;
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 07/10] ratp: implement ping as a standard ratp command

2018-02-06 Thread Aleksander Morgado
On Tue, Feb 6, 2018 at 10:33 AM, Sascha Hauer <s.ha...@pengutronix.de> wrote:
> On Fri, Feb 02, 2018 at 12:14:39PM +0100, Aleksander Morgado wrote:
>> Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
>> ---
>>  commands/Makefile|  1 +
>>  commands/ratp-ping.c | 38 ++
>>  common/ratp.c| 27 ---
>
> Can we put the ratp commands to a separate directory like
> common/ratp/commands/ or similar?
> There doesn't seem to be very much code sharing between the regular
> commands and their ratp correspondents. Most good commands are written
> in the way that the functionality is in common code outside the command
> file itself anyway.
>

I actually did that originally, but then implemented "md" for RATP and
decided I could reuse part of the code doing the memory dump. But then
no other command I implemented shared anything with the console
counterpart... so yes, totally agree changing that would be much
better. I did think of moving them to commands/ratp/* instead of
common/ratp/commands/* though. Why do you suggest to have them in
common/? Shouldn't they be closer to the console commands in the
hierarchy?

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [RFC PATCH 00/10] ratp: new generic RATP command support

2018-02-06 Thread Aleksander Morgado
>>
>> The first patches (1-5) break the current RATP API, by introducing
>> the concept of requests, responses and indications:
>>  * Requests sent to one endpoint are expected to be replied with
>>a response by the peer endpoint.
>>  * Indications are messages sent from one endpoint to another which
>>are not expected to be replied.
>
> I do not see why we have to break the RATP API. I mean currently we
> have BB_RATP_TYPE_COMMAND and BB_RATP_TYPE_COMMAND_RETURN which you
> convert to .type = BB_RATP_TYPE_COMMAND, .flags = 0 | RESPONSE.
>
> I see that using flags looks somewhat nicer, but besides of that,
> what is your selling point to break the API?
>

Well, it was just easier to say "if I send a request of type X, I
expect back a response of the same type X". It avoids the need of
having to define which command id is expected as response for which
command id request. I don't think I have any other selling point :)
Being so early in the amount of commands we have in RATP, thought this
could be a good improvement. Maybe I'm biased because I'm used to
talking to mobile broadband modems, and that is how all protocols I
know are defined. That said, changing this to keep the API would still
be very easy, it's no big deal.

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: [PATCH 06/10] ratp: implement generic command support

2018-02-06 Thread Aleksander Morgado
>> +
>> +#define BAREBOX_RATP_CMD_START(_name)   
>>  \
>> +extern const struct ratp_command __barebox_cmd_##_name; 
>>  \
>
> You shouldn't use the same name as the existing barebox commands,
> otherwise there might be name clashes. Add some _ratp_ to it.
>

Ah, sure.

>> +const struct ratp_command __barebox_cmd_##_name 
>>  \
>> + __attribute__ ((unused,section (".barebox_ratp_cmd_" 
>> __stringify(_name = {  \
>> + .id = BB_RATP_TYPE_##_name,
>
> I am not sure if I like the magic construction of the id field. Being
> able to grep for BB_RATP_TYPE_PING and finding the user has advantages.
> How about manually setting the field in the body of the command instead
> of constructing it?
>

You mean doing this instead?

BAREBOX_RATP_CMD_START(PING)
.cmd = ratp_cmd_ping,
.id = BB_RATP_TYPE_PING
BAREBOX_RATP_CMD_END


-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 7/8] ratp: new md and mw commands

2018-02-08 Thread Aleksander Morgado
This commit introduces support for running the md and mw commands
using the binary interface provided by RAPT. This allows clients to
read and write memory files without needing to do custom string
parsing on the data returned by the console 'md' and 'mw' operations.

The request and response messages used for these new operations are
structured in the same way:

 * An initial fixed-sized section includes the fixed-sized
   variables (e.g. integers), as well as the size and offset of the
   variable-length variables.

 * After the initial fixed-sized section, the buffer is given, which
   contains the variable-length variables in the offsets previously
   defined and with the size previously defined.

The message also defines separately the offset of the buffer
w.r.t. the start of the message. The endpoint reading the message will
use this information to decide where the buffer starts. This allows to
extend the message format in the future without needing to break the
message API, as new fields can be appended to the fixed-sized section
as long as the buffer offset is also updated to report the new
position of the buffer.

E.g. testing with ratp-barebox-cli:

  $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
--timeout 1000
  Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  00:00:00:00:00

  $ ratp-barebox-cli -t /dev/ttyUSB2 --mw 
"/dev/pic_eeprom_rdu,0x107,01:02:03:04:05" --timeout 1000
  Sending mw request: write '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  5/5 bytes written

  $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
--timeout 1000
  Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  01:02:03:04:05

  $ ratp-barebox-cli -t /dev/ttyUSB2 --mw 
"/dev/pic_eeprom_rdu,0x107,00:00:00:00:00" --timeout 1000
  Sending mw request: write '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  5/5 bytes written

  $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
--timeout 1000
  Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
  00:00:00:00:00

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |   2 +
 common/ratp/md.c | 202 +++
 common/ratp/mw.c | 173 +++
 include/ratp_bb.h|   4 +
 4 files changed, 381 insertions(+)
 create mode 100644 common/ratp/md.c
 create mode 100644 common/ratp/mw.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index 2fa9d63c0..d4cfdf95f 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -1,3 +1,5 @@
 obj-y += ratp.o
 obj-y += ping.o
 obj-y += getenv.o
+obj-y += md.o
+obj-y += mw.o
diff --git a/common/ratp/md.c b/common/ratp/md.c
new file mode 100644
index 0..bbb7a50f0
--- /dev/null
+++ b/common/ratp/md.c
@@ -0,0 +1,202 @@
+/*
+ * Copyright (c) 2011-2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* NOTE:
+ *  - Fixed-size fields (e.g. integers) are given just after the header.
+ *  - Variable-length fields are stored inside the buffer[] and their position
+ *within the buffer[] and their size are given as fixed-sized fields after
+ *the header.
+ *  The message may be extended at any time keeping backwards compatibility,
+ *  as the position of the buffer[] is given by the buffer_offset field. i.e.
+ *  increasing the buffer_offset field we can extend the fixed-sized section
+ *  to add more fields.
+ */
+
+struct ratp_bb_md_request {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint16_t addr;
+   uint16_t size;
+   uint16_t path_size;
+   uint16_t path_offset;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+struct ratp_bb_md_response {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint32_t errno;
+   uint16_t data_size;
+   uint16_t data_offset;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+extern char *mem_rw_buf;
+
+static int do_ratp_mem_md(const char *filename,
+ loff_t start,
+ loff_t size,
+ uint8_t *output)
+{
+   int r, now, t;
+   int ret = 0;
+   int fd;
+   void *map;
+
+   fd = open_and_lse

[RFC PATCH v2 5/8] ratp: implement getenv as a standard ratp command

2018-02-08 Thread Aleksander Morgado
Also, use xstrndup() instead of the custom xmemdup_add_zero() as we're
working with strings anyway.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |  1 +
 common/ratp/getenv.c | 51 +++
 common/ratp/ratp.c   | 30 --
 3 files changed, 52 insertions(+), 30 deletions(-)
 create mode 100644 common/ratp/getenv.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index acad61ee5..2fa9d63c0 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -1,2 +1,3 @@
 obj-y += ratp.o
 obj-y += ping.o
+obj-y += getenv.o
diff --git a/common/ratp/getenv.c b/common/ratp/getenv.c
new file mode 100644
index 0..b40963488
--- /dev/null
+++ b/common/ratp/getenv.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/*
+ * RATP getenv
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+static int ratp_cmd_getenv(const struct ratp_bb *req, int req_len,
+  struct ratp_bb **rsp, int *rsp_len)
+{
+   int dlen = req_len - sizeof (struct ratp_bb);
+   char *varname;
+   const char *value;
+
+   varname = xstrndup ((const char *)req->data, dlen);
+   value = getenv (varname);
+   free (varname);
+
+   dlen = strlen (value);
+
+   *rsp_len = sizeof(struct ratp_bb) + dlen;
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
+   memcpy ((*rsp)->data, value, dlen);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(GETENV)
+   .request_id = BB_RATP_TYPE_GETENV,
+   .response_id = BB_RATP_TYPE_GETENV_RETURN,
+   .cmd = ratp_cmd_getenv
+BAREBOX_RATP_CMD_END
diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 965c67a0a..1e9c65179 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -189,29 +189,6 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
return ret;
 }

-static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, const char *val)
-{
-   void *buf;
-   struct ratp_bb *rbb;
-   int len, ret;
-
-   if (!val)
-   val = "";
-
-   len = sizeof(*rbb) + strlen(val);
-   buf = xzalloc(len);
-   rbb = buf;
-   strcpy(rbb->data, val);
-
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
-
-   ret = ratp_send(>ratp, buf, len);
-
-   free(buf);
-
-   return ret;
-}
-
 static char *ratp_command;
 static struct ratp_ctx *ratp_ctx;

@@ -220,7 +197,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
const struct ratp_bb *rbb = buf;
struct ratp_bb_pkt *pkt;
int dlen = len - sizeof(struct ratp_bb);
-   char *varname;
int ret = 0;
uint16_t type = be16_to_cpu(rbb->type);
struct ratp_command *cmd;
@@ -260,12 +236,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
break;

-   case BB_RATP_TYPE_GETENV:
-   varname = xmemdup_add_zero(>data, dlen);
-
-   ret = ratp_bb_send_getenv_return(ctx, getenv(varname));
-   break;
-
case BB_RATP_TYPE_FS_RETURN:
pkt = xzalloc(sizeof(*pkt) + dlen);
pkt->len = dlen;
--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 4/8] ratp: implement ping as a standard ratp command

2018-02-08 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |  1 +
 common/ratp/ping.c   | 40 
 common/ratp/ratp.c   | 24 
 3 files changed, 41 insertions(+), 24 deletions(-)
 create mode 100644 common/ratp/ping.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index cab14c6fb..acad61ee5 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -1 +1,2 @@
 obj-y += ratp.o
+obj-y += ping.o
diff --git a/common/ratp/ping.c b/common/ratp/ping.c
new file mode 100644
index 0..cc29ea36c
--- /dev/null
+++ b/common/ratp/ping.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/*
+ * RATP ping
+ */
+
+#include 
+#include 
+#include 
+
+static int ratp_cmd_ping(const struct ratp_bb *req, int req_len,
+struct ratp_bb **rsp, int *rsp_len)
+{
+   /* Just build response */
+   *rsp_len = sizeof(struct ratp_bb);
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_PONG);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(PING)
+   .request_id = BB_RATP_TYPE_PING,
+   .response_id = BB_RATP_TYPE_PONG,
+   .cmd = ratp_cmd_ping
+BAREBOX_RATP_CMD_END
diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 7c8a2f6f5..965c67a0a 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -189,25 +189,6 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
return ret;
 }

-static int ratp_bb_send_pong(struct ratp_ctx *ctx)
-{
-   void *buf;
-   struct ratp_bb *rbb;
-   int len = sizeof(*rbb);
-   int ret;
-
-   buf = xzalloc(len);
-   rbb = buf;
-
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_PONG);
-
-   ret = ratp_send(>ratp, buf, len);
-
-   free(buf);
-
-   return ret;
-}
-
 static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, const char *val)
 {
void *buf;
@@ -270,7 +251,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
break;

case BB_RATP_TYPE_COMMAND_RETURN:
-   case BB_RATP_TYPE_PONG:
break;

case BB_RATP_TYPE_CONSOLEMSG:
@@ -280,10 +260,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
break;

-   case BB_RATP_TYPE_PING:
-   ret = ratp_bb_send_pong(ctx);
-   break;
-
case BB_RATP_TYPE_GETENV:
varname = xmemdup_add_zero(>data, dlen);

--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 2/8] ratp: moved logic to its own subdirectory

2018-02-08 Thread Aleksander Morgado
We are going to add new RATP command implementations in separate files
within this subdirectory.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/Kconfig   | 13 +
 common/Makefile  |  4 ++--
 common/ratp/Kconfig  | 14 ++
 common/ratp/Makefile |  1 +
 common/{ => ratp}/ratp.c |  0
 5 files changed, 18 insertions(+), 14 deletions(-)
 create mode 100644 common/ratp/Kconfig
 create mode 100644 common/ratp/Makefile
 rename common/{ => ratp}/ratp.c (100%)

diff --git a/common/Kconfig b/common/Kconfig
index 57418cadc..a96c6ef20 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -755,18 +755,7 @@ config PBL_CONSOLE
  must be running at the address it's linked at and bss must
  be cleared. On ARM that would be after setup_c().

-config CONSOLE_RATP
-   bool
-   select RATP
-   select CRC16
-   select POLLER
-   depends on CONSOLE_FULL
-   prompt "RATP console support"
-   help
- This option adds support for remote controlling barebox via serial
- port. The regular console is designed for human interaction whereas
- this option adds a machine readable interface for controlling barebox.
- Say yes here if you want to control barebox from a remote host.
+source common/ratp/Kconfig

 config PARTITION
bool
diff --git a/common/Makefile b/common/Makefile
index 8cd0ab300..90d5f19ec 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -46,7 +46,8 @@ obj-$(CONFIG_RESET_SOURCE)+= reset_source.o
 obj-$(CONFIG_SHELL_HUSH)   += hush.o
 obj-$(CONFIG_SHELL_SIMPLE) += parser.o
 obj-$(CONFIG_STATE)+= state/
-obj-$(CONFIG_RATP) += ratp.o
+obj-$(CONFIG_RATP) += ratp/
+obj-$(CONFIG_CONSOLE_RATP) += ratp/
 obj-$(CONFIG_BOOTCHOOSER)  += bootchooser.o
 obj-$(CONFIG_UIMAGE)   += image.o uimage.o
 obj-$(CONFIG_FITIMAGE) += image-fit.o
@@ -60,7 +61,6 @@ obj-$(CONFIG_FILE_LIST)   += file-list.o
 obj-$(CONFIG_FIRMWARE) += firmware.o
 obj-$(CONFIG_UBIFORMAT)+= ubiformat.o
 obj-$(CONFIG_BAREBOX_UPDATE_IMX_NAND_FCB) += imx-bbu-nand-fcb.o
-obj-$(CONFIG_CONSOLE_RATP) += ratp.o
 obj-$(CONFIG_BOOT) += boot.o

 quiet_cmd_pwd_h = PWDH$@
diff --git a/common/ratp/Kconfig b/common/ratp/Kconfig
new file mode 100644
index 0..93ff75d64
--- /dev/null
+++ b/common/ratp/Kconfig
@@ -0,0 +1,14 @@
+
+config CONSOLE_RATP
+   bool
+   select RATP
+   select CRC16
+   select POLLER
+   depends on CONSOLE_FULL
+   prompt "RATP console support"
+   help
+ This option adds support for remote controlling barebox via serial
+ port. The regular console is designed for human interaction whereas
+ this option adds a machine readable interface for controlling barebox.
+ Say yes here if you want to control barebox from a remote host.
+
diff --git a/common/ratp/Makefile b/common/ratp/Makefile
new file mode 100644
index 0..cab14c6fb
--- /dev/null
+++ b/common/ratp/Makefile
@@ -0,0 +1 @@
+obj-y += ratp.o
diff --git a/common/ratp.c b/common/ratp/ratp.c
similarity index 100%
rename from common/ratp.c
rename to common/ratp/ratp.c
--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 1/8] ratp: implement generic command support

2018-02-08 Thread Aleksander Morgado
The RATP implementation now allows executing generic commands with a
binary interface: binary requests are received and binary responses
are returned.

Each command can define its own RATP request contents (e.g. to specify
command-specific options) as well as its own RATP response contents
(if any data is to be returned).

Each command is associated with a pair of numeric unique request and
response IDs, and for easy reference these IDs are maintained in the
common ratp_bb header. Modules may override generic implemented
commands or include their own new ones (as long as the numeric IDs
introduced are unique).

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 arch/arm/lib32/barebox.lds.S  |  4 ++
 arch/arm/lib64/barebox.lds.S  |  4 ++
 arch/blackfin/boards/ipe337/barebox.lds.S |  5 +-
 arch/mips/lib/barebox.lds.S   |  4 ++
 arch/nios2/cpu/barebox.lds.S  |  5 +-
 arch/openrisc/cpu/barebox.lds.S   |  4 ++
 arch/ppc/boards/pcm030/barebox.lds.S  |  4 ++
 arch/ppc/mach-mpc85xx/barebox.lds.S   |  4 ++
 arch/sandbox/board/barebox.lds.S  |  5 ++
 arch/x86/lib/barebox.lds.S|  7 +++
 arch/x86/mach-efi/elf_ia32_efi.lds.S  |  5 ++
 arch/x86/mach-efi/elf_x86_64_efi.lds.S|  5 ++
 common/module.lds.S   |  2 +
 common/ratp.c | 82 +--
 include/asm-generic/barebox.lds.h |  2 +
 include/ratp_bb.h | 47 ++
 16 files changed, 171 insertions(+), 18 deletions(-)

diff --git a/arch/arm/lib32/barebox.lds.S b/arch/arm/lib32/barebox.lds.S
index e7b87b7cd..6fadc2a35 100644
--- a/arch/arm/lib32/barebox.lds.S
+++ b/arch/arm/lib32/barebox.lds.S
@@ -85,6 +85,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/arm/lib64/barebox.lds.S b/arch/arm/lib64/barebox.lds.S
index 240699f1a..a53b933bb 100644
--- a/arch/arm/lib64/barebox.lds.S
+++ b/arch/arm/lib64/barebox.lds.S
@@ -82,6 +82,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/blackfin/boards/ipe337/barebox.lds.S 
b/arch/blackfin/boards/ipe337/barebox.lds.S
index 51a586af2..7e82a1bd7 100644
--- a/arch/blackfin/boards/ipe337/barebox.lds.S
+++ b/arch/blackfin/boards/ipe337/barebox.lds.S
@@ -68,6 +68,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
___barebox_cmd_end = .;

+   ___barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   ___barebox_ratp_cmd_end = .;
+
___barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
___barebox_magicvar_end = .;
@@ -91,4 +95,3 @@ SECTIONS
___bss_stop = .;
_end = .;
 }
-
diff --git a/arch/mips/lib/barebox.lds.S b/arch/mips/lib/barebox.lds.S
index 899f62b96..660d4be85 100644
--- a/arch/mips/lib/barebox.lds.S
+++ b/arch/mips/lib/barebox.lds.S
@@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/nios2/cpu/barebox.lds.S b/arch/nios2/cpu/barebox.lds.S
index a2d7fa8cd..fbcd1cd3f 100644
--- a/arch/nios2/cpu/barebox.lds.S
+++ b/arch/nios2/cpu/barebox.lds.S
@@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;

+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
@@ -129,4 +133,3 @@ SECTIONS
_end = .;
PROVIDE (end = .);
 }
-
diff --git a/arch/openrisc/cpu/barebox.lds.S b/arch/openrisc/cpu/barebox.lds.S
index b819ca099..c6807aec3 100644
--- a/arch/openrisc/cpu/barebox.lds.S
+++ b/arch/openrisc/cpu/barebox.lds.S
@@ -57,6 +57,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS } > ram
__barebox_cmd_end = .;

+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS } > ram
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS } > ram
__bareb

[RFC PATCH v2 6/8] ratp: use xstrndup() instead of a custom xmemdup_add_zero()

2018-02-08 Thread Aleksander Morgado
The console operations done via RATP expect strings, so just use
xstrndup() instead of defining a custom method.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/ratp.c | 13 +
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 1e9c65179..fae9cec5b 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -132,17 +132,6 @@ static int console_send(struct ratp *r, void *pkt, int len)
return 0;
 }

-static void *xmemdup_add_zero(const void *buf, int len)
-{
-   void *ret;
-
-   ret = xzalloc(len + 1);
-   *(uint8_t *)(ret + len) = 0;
-   memcpy(ret, buf, len);
-
-   return ret;
-}
-
 static void ratp_queue_console_tx(struct ratp_ctx *ctx)
 {
u8 buf[255];
@@ -220,7 +209,7 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
if (!IS_ENABLED(CONFIG_CONSOLE_RATP) || ratp_command)
return 0;

-   ratp_command = xmemdup_add_zero(>data, dlen);
+   ratp_command = xstrndup((const char *)rbb->data, dlen);
ratp_ctx = ctx;
pr_debug("got command: %s\n", ratp_command);

--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 8/8] ratp: new reset command

2018-02-08 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |  1 +
 common/ratp/reset.c  | 55 
 include/ratp_bb.h|  1 +
 3 files changed, 57 insertions(+)
 create mode 100644 common/ratp/reset.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index d4cfdf95f..2c6d674f6 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -3,3 +3,4 @@ obj-y += ping.o
 obj-y += getenv.o
 obj-y += md.o
 obj-y += mw.o
+obj-y += reset.o
diff --git a/common/ratp/reset.c b/common/ratp/reset.c
new file mode 100644
index 0..ca8be4e62
--- /dev/null
+++ b/common/ratp/reset.c
@@ -0,0 +1,55 @@
+/*
+ * reset.c - reset the cpu
+ *
+ * Copyright (c) 2007 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct ratp_bb_reset {
+   struct ratp_bb header;
+   uint8_tforce;
+} __attribute__((packed));
+
+static int ratp_cmd_reset(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_reset *reset_req = (struct ratp_bb_reset *)req;
+
+   if (req_len < sizeof (*reset_req)) {
+   printf ("ratp reset ignored: size mismatch (%d < %zu)\n", 
req_len, sizeof (*reset_req));
+   return 2;
+   }
+
+   debug("running reset %s\n", reset_req->force ? "(forced)" : "");
+
+   if (!reset_req->force)
+   shutdown_barebox();
+
+   restart_machine();
+   /* Not reached */
+   return 1;
+}
+
+BAREBOX_RATP_CMD_START(RESET)
+   .request_id = BB_RATP_TYPE_RESET,
+   .cmd = ratp_cmd_reset
+BAREBOX_RATP_CMD_END
diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index 00b165f77..3a80cf6ae 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -16,6 +16,7 @@
 #define BB_RATP_TYPE_MD_RETURN 11
 #define BB_RATP_TYPE_MW12
 #define BB_RATP_TYPE_MW_RETURN 13
+#define BB_RATP_TYPE_RESET 14

 struct ratp_bb {
uint16_t type;
--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 0/8] ratp: new generic RATP command support

2018-02-08 Thread Aleksander Morgado
This updated patchset doesn't break the RATP API. It keeps the separate IDs for 
requests and responses, and doesn't try to flag responses or indications in a 
different way.

The command definition logic is also updated so that the request and response 
IDs associated to each command are defined in the body of the command, instead 
of magically constructing the id fields with macro glues.

All the RATP specific commands are now implemented in files completely 
separated from the console commands, and they are all kept in common/ratp, 
along with the core ratp implementation itself.

As additional changes w.r.t. v1, this patchset also includes a change to make 
it possible building the RATP logic without full console support (e.g. making 
it possible to enable CONFIG_RATP with CONFIG_CONSOLE_RATP disabled).

The new commands were tested with the libratp-barebox library
(wip2/md-mw branch) in https://github.com/aleksander0m/libratp-barebox

Aleksander Morgado (8):
  ratp: implement generic command support
  ratp: moved logic to its own subdirectory
  ratp: allow building without full console support
  ratp: implement ping as a standard ratp command
  ratp: implement getenv as a standard ratp command
  ratp: use xstrndup() instead of a custom xmemdup_add_zero()
  ratp: new md and mw commands
  ratp: new reset command

 arch/arm/lib32/barebox.lds.S  |   4 +
 arch/arm/lib64/barebox.lds.S  |   4 +
 arch/blackfin/boards/ipe337/barebox.lds.S |   5 +-
 arch/mips/lib/barebox.lds.S   |   4 +
 arch/nios2/cpu/barebox.lds.S  |   5 +-
 arch/openrisc/cpu/barebox.lds.S   |   4 +
 arch/ppc/boards/pcm030/barebox.lds.S  |   4 +
 arch/ppc/mach-mpc85xx/barebox.lds.S   |   4 +
 arch/sandbox/board/barebox.lds.S  |   5 +
 arch/x86/lib/barebox.lds.S|   7 ++
 arch/x86/mach-efi/elf_ia32_efi.lds.S  |   5 +
 arch/x86/mach-efi/elf_x86_64_efi.lds.S|   5 +
 common/Kconfig|  13 +-
 common/Makefile   |   3 +-
 common/module.lds.S   |   2 +
 common/ratp/Kconfig   |  14 +++
 common/ratp/Makefile  |   6 +
 common/ratp/getenv.c  |  51 
 common/ratp/md.c  | 202 ++
 common/ratp/mw.c  | 173 +
 common/ratp/ping.c|  40 ++
 common/{ => ratp}/ratp.c  | 156 +++
 common/ratp/reset.c   |  55 
 fs/Kconfig|   2 +-
 include/asm-generic/barebox.lds.h |   2 +
 include/ratp_bb.h |  52 
 lib/Kconfig   |   2 +-
 lib/readline.c|   2 +-
 28 files changed, 728 insertions(+), 103 deletions(-)
 create mode 100644 common/ratp/Kconfig
 create mode 100644 common/ratp/Makefile
 create mode 100644 common/ratp/getenv.c
 create mode 100644 common/ratp/md.c
 create mode 100644 common/ratp/mw.c
 create mode 100644 common/ratp/ping.c
 rename common/{ => ratp}/ratp.c (81%)
 create mode 100644 common/ratp/reset.c

--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[RFC PATCH v2 3/8] ratp: allow building without full console support

2018-02-08 Thread Aleksander Morgado
Make CONFIG_RATP a selectable config option, so that the user can
enable RATP support without explicitly needing to enable the full
console support over RATP (e.g. only for RATP FS or built-in command
support).

The full console can still be explicitly enabled with
CONFIG_CONSOLE_RATP.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/Makefile| 1 -
 common/ratp/ratp.c | 7 +--
 fs/Kconfig | 2 +-
 lib/Kconfig| 2 +-
 lib/readline.c | 2 +-
 5 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/common/Makefile b/common/Makefile
index 90d5f19ec..8a0ce84e1 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -47,7 +47,6 @@ obj-$(CONFIG_SHELL_HUSH)  += hush.o
 obj-$(CONFIG_SHELL_SIMPLE) += parser.o
 obj-$(CONFIG_STATE)+= state/
 obj-$(CONFIG_RATP) += ratp/
-obj-$(CONFIG_CONSOLE_RATP) += ratp/
 obj-$(CONFIG_BOOTCHOOSER)  += bootchooser.o
 obj-$(CONFIG_UIMAGE)   += image.o uimage.o
 obj-$(CONFIG_FITIMAGE) += image-fit.o
diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index b051fdee4..7c8a2f6f5 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -260,7 +260,7 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)

switch (type) {
case BB_RATP_TYPE_COMMAND:
-   if (ratp_command)
+   if (!IS_ENABLED(CONFIG_CONSOLE_RATP) || ratp_command)
return 0;

ratp_command = xmemdup_add_zero(>data, dlen);
@@ -274,6 +274,8 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
break;

case BB_RATP_TYPE_CONSOLEMSG:
+   if (!IS_ENABLED(CONFIG_CONSOLE_RATP))
+   return 0;

kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
break;
@@ -420,7 +422,8 @@ static void ratp_poller(struct poller_struct *poller)
size_t len;
void *buf;

-   ratp_queue_console_tx(ctx);
+   if (IS_ENABLED(CONFIG_CONSOLE_RATP))
+   ratp_queue_console_tx(ctx);

ret = ratp_poll(>ratp);
if (ret == -EINTR)
diff --git a/fs/Kconfig b/fs/Kconfig
index 57f2676f4..351200055 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -94,7 +94,7 @@ source fs/squashfs/Kconfig

 config FS_RATP
bool
-   depends on CONSOLE_RATP
+   depends on RATP
prompt "RATP filesystem support"
help
  This enables support for transferring files over RATP. A host can
diff --git a/lib/Kconfig b/lib/Kconfig
index 9562b1b8c..572985860 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -84,7 +84,7 @@ config STMP_DEVICE

 config RATP
select CRC16
-   bool
+   bool "RATP protocol support"
help
  Reliable Asynchronous Transfer Protocol (RATP) is a protocol for 
reliably
  transferring packets over serial links described in RFC916. This 
implementation
diff --git a/lib/readline.c b/lib/readline.c
index 1e380abec..904a77639 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -202,7 +202,7 @@ int readline(const char *prompt, char *buf, int len)
while (1) {
while (!tstc()) {
poller_call();
-   if (IS_ENABLED(CONFIG_RATP))
+   if (IS_ENABLED(CONFIG_CONSOLE_RATP))
barebox_ratp_command_run();
}

--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 7/9] ratp: new md and mw commands

2018-02-24 Thread Aleksander Morgado
This commit introduces support for running the md and mw commands
using the binary interface provided by RATP. This allows clients to
read and write memory files without needing to do custom string
parsing on the data returned by the console 'md' and 'mw' operations.

The request and response messages used for these new operations are
structured in the same way:

 * An initial fixed-sized section includes the fixed-sized
   variables (e.g. integers), as well as the size and offset of the
   variable-length variables.

 * After the initial fixed-sized section, the buffer is given, which
   contains the variable-length variables in the offsets previously
   defined and with the size previously defined.

The message also defines separately the offset of the buffer
w.r.t. the start of the message. The endpoint reading the message will
use this information to decide where the buffer starts. This allows to
extend the message format in the future without needing to break the
message API, as new fields can be appended to the fixed-sized section
as long as the buffer offset is also updated to report the new
position of the buffer.

E.g.:
$ ./bbremote --port /dev/ttyUSB2 md /dev/pic_eeprom_rdu 0x107 5
00

$ ./bbremote --port /dev/ttyUSB2 mw /dev/pic_eeprom_rdu 0x107 0102030405
5 bytes written

$ ./bbremote --port /dev/ttyUSB2 md /dev/pic_eeprom_rdu 0x107 5
0102030405

$ ./bbremote --port /dev/ttyUSB2 mw /dev/pic_eeprom_rdu 0x107 00
5 bytes written

$ ./bbremote --port /dev/ttyUSB2 md /dev/pic_eeprom_rdu 0x107 5
00

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |   2 +
 common/ratp/md.c | 202 +++
 common/ratp/mw.c | 173 
 include/ratp_bb.h|   4 +
 scripts/remote/controller.py |  24 +
 scripts/remote/main.py   |  38 
 scripts/remote/messages.py   |  89 +++
 7 files changed, 532 insertions(+)
 create mode 100644 common/ratp/md.c
 create mode 100644 common/ratp/mw.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index 2fa9d63c0..d4cfdf95f 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -1,3 +1,5 @@
 obj-y += ratp.o
 obj-y += ping.o
 obj-y += getenv.o
+obj-y += md.o
+obj-y += mw.o
diff --git a/common/ratp/md.c b/common/ratp/md.c
new file mode 100644
index 0..9b8fc2bc5
--- /dev/null
+++ b/common/ratp/md.c
@@ -0,0 +1,202 @@
+/*
+ * Copyright (c) 2011-2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* NOTE:
+ *  - Fixed-size fields (e.g. integers) are given just after the header.
+ *  - Variable-length fields are stored inside the buffer[] and their position
+ *within the buffer[] and their size are given as fixed-sized fields after
+ *the header.
+ *  The message may be extended at any time keeping backwards compatibility,
+ *  as the position of the buffer[] is given by the buffer_offset field. i.e.
+ *  increasing the buffer_offset field we can extend the fixed-sized section
+ *  to add more fields.
+ */
+
+struct ratp_bb_md_request {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint16_t addr;
+   uint16_t size;
+   uint16_t path_size;
+   uint16_t path_offset;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+struct ratp_bb_md_response {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint32_t errno;
+   uint16_t data_size;
+   uint16_t data_offset;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+extern char *mem_rw_buf;
+
+static int do_ratp_mem_md(const char *filename,
+ loff_t start,
+ loff_t size,
+ uint8_t *output)
+{
+   int r, now, t;
+   int ret = 0;
+   int fd;
+   void *map;
+
+   fd = open_and_lseek(filename, O_RWSIZE_1 | O_RDONLY, start);
+   if (fd < 0)
+   return -errno;
+
+   map = memmap(fd, PROT_READ);
+   if (map != (void *)-1) {
+   memcpy(output, (uint8_t *)(map + start), size);
+   goto out;
+   }
+
+   t = 0;
+   do {
+   now = min(size, (loff_t)RW_BUF_SIZE);
+   r = read

[PATCH v3 8/9] ratp: new reset command

2018-02-24 Thread Aleksander Morgado
E.g.:
$ ./bbremote -v --port /dev/ttyUSB2 reset

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |  1 +
 common/ratp/reset.c  | 55 
 include/ratp_bb.h|  1 +
 scripts/remote/controller.py |  9 +---
 scripts/remote/main.py   | 13 +++
 scripts/remote/messages.py   | 16 +
 6 files changed, 92 insertions(+), 3 deletions(-)
 create mode 100644 common/ratp/reset.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index d4cfdf95f..2c6d674f6 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -3,3 +3,4 @@ obj-y += ping.o
 obj-y += getenv.o
 obj-y += md.o
 obj-y += mw.o
+obj-y += reset.o
diff --git a/common/ratp/reset.c b/common/ratp/reset.c
new file mode 100644
index 0..ca8be4e62
--- /dev/null
+++ b/common/ratp/reset.c
@@ -0,0 +1,55 @@
+/*
+ * reset.c - reset the cpu
+ *
+ * Copyright (c) 2007 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct ratp_bb_reset {
+   struct ratp_bb header;
+   uint8_tforce;
+} __attribute__((packed));
+
+static int ratp_cmd_reset(const struct ratp_bb *req, int req_len,
+ struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_reset *reset_req = (struct ratp_bb_reset *)req;
+
+   if (req_len < sizeof (*reset_req)) {
+   printf ("ratp reset ignored: size mismatch (%d < %zu)\n", 
req_len, sizeof (*reset_req));
+   return 2;
+   }
+
+   debug("running reset %s\n", reset_req->force ? "(forced)" : "");
+
+   if (!reset_req->force)
+   shutdown_barebox();
+
+   restart_machine();
+   /* Not reached */
+   return 1;
+}
+
+BAREBOX_RATP_CMD_START(RESET)
+   .request_id = BB_RATP_TYPE_RESET,
+   .cmd = ratp_cmd_reset
+BAREBOX_RATP_CMD_END
diff --git a/include/ratp_bb.h b/include/ratp_bb.h
index 00b165f77..3a80cf6ae 100644
--- a/include/ratp_bb.h
+++ b/include/ratp_bb.h
@@ -16,6 +16,7 @@
 #define BB_RATP_TYPE_MD_RETURN 11
 #define BB_RATP_TYPE_MW12
 #define BB_RATP_TYPE_MW_RETURN 13
+#define BB_RATP_TYPE_RESET 14
 
 struct ratp_bb {
uint16_t type;
diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index eaab9f8f6..2ed834613 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -46,9 +46,6 @@ def unpack(data):
 elif p_type == BBType.fs_return:
 logging.debug("received: fs_return")
 return BBPacketFSReturn(raw=data)
-elif p_type == BBType.md:
-logging.debug("received: md")
-return BBPacketMd(raw=data)
 elif p_type == BBType.md_return:
 logging.debug("received: md_return")
 return BBPacketMdReturn(raw=data)
@@ -58,6 +55,9 @@ def unpack(data):
 elif p_type == BBType.mw_return:
 logging.debug("received: mw_return")
 return BBPacketMwReturn(raw=data)
+elif p_type == BBType.reset:
+logging.debug("received: reset")
+return BBPacketReset(raw=data)
 else:
 logging.debug("received: UNKNOWN")
 return BBPacket(raw=data)
@@ -136,6 +136,9 @@ class Controller(Thread):
 logging.info("Mw return: %r", r)
 return (r.exit_code,r.written)
 
+def reset(self, force):
+self._send(BBPacketReset(force=force))
+
 def close(self):
 self.conn.close()
 
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index 29f601e9f..38d280bfe 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -98,6 +98,13 @@ def handle_mw(args):
 return res
 
 
+def handle_reset(args):
+ctrl = get_controller(args)
+ctrl.reset(args.force)
+ctrl.close()
+return 0
+
+
 def handle_listen(args):
 port = serial.serial_for_url(args.port, args.baudrate)
 conn = SerialRatpConnection(port)
@@ -181,6 +188,12 @@ parser_mw.add_argument('address', type=auto_int, 
help="address")
 parser_mw.add_argument('data', help="data")
 parser_mw.set_defaults(func=handle_mw)
 
+parser_reset = subparsers.add_parser('reset', help="run reset command")
+parser_reset_force = parser_reset.add_mutually_exclusive_group

[PATCH v3 5/9] ratp: implement getenv as a standard ratp command

2018-02-24 Thread Aleksander Morgado
Also, use xstrndup() instead of the custom xmemdup_add_zero() as we're
working with strings anyway.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |  1 +
 common/ratp/getenv.c | 51 +++
 common/ratp/ratp.c   | 30 --
 3 files changed, 52 insertions(+), 30 deletions(-)
 create mode 100644 common/ratp/getenv.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index acad61ee5..2fa9d63c0 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -1,2 +1,3 @@
 obj-y += ratp.o
 obj-y += ping.o
+obj-y += getenv.o
diff --git a/common/ratp/getenv.c b/common/ratp/getenv.c
new file mode 100644
index 0..b40963488
--- /dev/null
+++ b/common/ratp/getenv.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/*
+ * RATP getenv
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+static int ratp_cmd_getenv(const struct ratp_bb *req, int req_len,
+  struct ratp_bb **rsp, int *rsp_len)
+{
+   int dlen = req_len - sizeof (struct ratp_bb);
+   char *varname;
+   const char *value;
+
+   varname = xstrndup ((const char *)req->data, dlen);
+   value = getenv (varname);
+   free (varname);
+
+   dlen = strlen (value);
+
+   *rsp_len = sizeof(struct ratp_bb) + dlen;
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
+   memcpy ((*rsp)->data, value, dlen);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(GETENV)
+   .request_id = BB_RATP_TYPE_GETENV,
+   .response_id = BB_RATP_TYPE_GETENV_RETURN,
+   .cmd = ratp_cmd_getenv
+BAREBOX_RATP_CMD_END
diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 965c67a0a..1e9c65179 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -189,29 +189,6 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
return ret;
 }
 
-static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, const char *val)
-{
-   void *buf;
-   struct ratp_bb *rbb;
-   int len, ret;
-
-   if (!val)
-   val = "";
-
-   len = sizeof(*rbb) + strlen(val);
-   buf = xzalloc(len);
-   rbb = buf;
-   strcpy(rbb->data, val);
-
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
-
-   ret = ratp_send(>ratp, buf, len);
-
-   free(buf);
-
-   return ret;
-}
-
 static char *ratp_command;
 static struct ratp_ctx *ratp_ctx;
 
@@ -220,7 +197,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
const struct ratp_bb *rbb = buf;
struct ratp_bb_pkt *pkt;
int dlen = len - sizeof(struct ratp_bb);
-   char *varname;
int ret = 0;
uint16_t type = be16_to_cpu(rbb->type);
struct ratp_command *cmd;
@@ -260,12 +236,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
break;
 
-   case BB_RATP_TYPE_GETENV:
-   varname = xmemdup_add_zero(>data, dlen);
-
-   ret = ratp_bb_send_getenv_return(ctx, getenv(varname));
-   break;
-
case BB_RATP_TYPE_FS_RETURN:
pkt = xzalloc(sizeof(*pkt) + dlen);
pkt->len = dlen;
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 6/9] ratp: use xstrndup() instead of a custom xmemdup_add_zero()

2018-02-24 Thread Aleksander Morgado
The console operations done via RATP expect strings, so just use
xstrndup() instead of defining a custom method.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/ratp.c | 13 +
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 1e9c65179..fae9cec5b 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -132,17 +132,6 @@ static int console_send(struct ratp *r, void *pkt, int len)
return 0;
 }
 
-static void *xmemdup_add_zero(const void *buf, int len)
-{
-   void *ret;
-
-   ret = xzalloc(len + 1);
-   *(uint8_t *)(ret + len) = 0;
-   memcpy(ret, buf, len);
-
-   return ret;
-}
-
 static void ratp_queue_console_tx(struct ratp_ctx *ctx)
 {
u8 buf[255];
@@ -220,7 +209,7 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
if (!IS_ENABLED(CONFIG_CONSOLE_RATP) || ratp_command)
return 0;
 
-   ratp_command = xmemdup_add_zero(>data, dlen);
+   ratp_command = xstrndup((const char *)rbb->data, dlen);
ratp_ctx = ctx;
pr_debug("got command: %s\n", ratp_command);
 
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 3/9] ratp: allow building without full console support

2018-02-24 Thread Aleksander Morgado
Make CONFIG_RATP a selectable config option, so that the user can
enable RATP support without explicitly needing to enable the full
console support over RATP (e.g. only for RATP FS or built-in command
support).

The full console can still be explicitly enabled with
CONFIG_CONSOLE_RATP.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/Makefile| 1 -
 common/ratp/ratp.c | 7 +--
 fs/Kconfig | 2 +-
 lib/Kconfig| 2 +-
 lib/readline.c | 2 +-
 5 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/common/Makefile b/common/Makefile
index bc9474a3a..a9abcd1bc 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -47,7 +47,6 @@ obj-$(CONFIG_SHELL_HUSH)  += hush.o
 obj-$(CONFIG_SHELL_SIMPLE) += parser.o
 obj-$(CONFIG_STATE)+= state/
 obj-$(CONFIG_RATP) += ratp/
-obj-$(CONFIG_CONSOLE_RATP) += ratp/
 obj-$(CONFIG_BOOTCHOOSER)  += bootchooser.o
 obj-$(CONFIG_UIMAGE)   += image.o uimage.o
 obj-$(CONFIG_FITIMAGE) += image-fit.o
diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index b051fdee4..7c8a2f6f5 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -260,7 +260,7 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
 
switch (type) {
case BB_RATP_TYPE_COMMAND:
-   if (ratp_command)
+   if (!IS_ENABLED(CONFIG_CONSOLE_RATP) || ratp_command)
return 0;
 
ratp_command = xmemdup_add_zero(>data, dlen);
@@ -274,6 +274,8 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
break;
 
case BB_RATP_TYPE_CONSOLEMSG:
+   if (!IS_ENABLED(CONFIG_CONSOLE_RATP))
+   return 0;
 
kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
break;
@@ -420,7 +422,8 @@ static void ratp_poller(struct poller_struct *poller)
size_t len;
void *buf;
 
-   ratp_queue_console_tx(ctx);
+   if (IS_ENABLED(CONFIG_CONSOLE_RATP))
+   ratp_queue_console_tx(ctx);
 
ret = ratp_poll(>ratp);
if (ret == -EINTR)
diff --git a/fs/Kconfig b/fs/Kconfig
index 57f2676f4..351200055 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -94,7 +94,7 @@ source fs/squashfs/Kconfig
 
 config FS_RATP
bool
-   depends on CONSOLE_RATP
+   depends on RATP
prompt "RATP filesystem support"
help
  This enables support for transferring files over RATP. A host can
diff --git a/lib/Kconfig b/lib/Kconfig
index dca93b80d..4e9213f3e 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -95,7 +95,7 @@ config STMP_DEVICE
 
 config RATP
select CRC16
-   bool
+   bool "RATP protocol support"
help
  Reliable Asynchronous Transfer Protocol (RATP) is a protocol for 
reliably
  transferring packets over serial links described in RFC916. This 
implementation
diff --git a/lib/readline.c b/lib/readline.c
index 1e380abec..904a77639 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -202,7 +202,7 @@ int readline(const char *prompt, char *buf, int len)
while (1) {
while (!tstc()) {
poller_call();
-   if (IS_ENABLED(CONFIG_RATP))
+   if (IS_ENABLED(CONFIG_CONSOLE_RATP))
barebox_ratp_command_run();
}
 
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 9/9] docs: add reference to libratp-barebox in the remote-control docs

2018-02-24 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 Documentation/user/remote-control.rst | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/Documentation/user/remote-control.rst 
b/Documentation/user/remote-control.rst
index 217251429..c8b7442f1 100644
--- a/Documentation/user/remote-control.rst
+++ b/Documentation/user/remote-control.rst
@@ -18,6 +18,13 @@ Additionally files can be transferred from/to barebox and a 
regular
 console offers interactive access to barebox on flawy serial
 connections.
 
+In addition to the bbremote tool provided with barebox, other third
+party tools exist that use the same RATP based protocol to communicate
+with the barebox instance, e.g. the libratp-barebox C library and the
+ratp-barebox-cli tool:
+
+  https://github.com/aleksander0m/libratp-barebox
+
 Enabling remote control support
 ---
 
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 2/9] ratp: moved logic to its own subdirectory

2018-02-24 Thread Aleksander Morgado
We are going to add new RATP command implementations in separate files
within this subdirectory.

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/Kconfig   | 13 +
 common/Makefile  |  4 ++--
 common/ratp/Kconfig  | 14 ++
 common/ratp/Makefile |  1 +
 common/{ => ratp}/ratp.c |  0
 5 files changed, 18 insertions(+), 14 deletions(-)
 create mode 100644 common/ratp/Kconfig
 create mode 100644 common/ratp/Makefile
 rename common/{ => ratp}/ratp.c (100%)

diff --git a/common/Kconfig b/common/Kconfig
index 93b1d8927..81cf72a95 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -755,18 +755,7 @@ config PBL_CONSOLE
  must be running at the address it's linked at and bss must
  be cleared. On ARM that would be after setup_c().
 
-config CONSOLE_RATP
-   bool
-   select RATP
-   select CRC16
-   select POLLER
-   depends on CONSOLE_FULL
-   prompt "RATP console support"
-   help
- This option adds support for remote controlling barebox via serial
- port. The regular console is designed for human interaction whereas
- this option adds a machine readable interface for controlling barebox.
- Say yes here if you want to control barebox from a remote host.
+source common/ratp/Kconfig
 
 config PARTITION
bool
diff --git a/common/Makefile b/common/Makefile
index 5351ef0f7..bc9474a3a 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -46,7 +46,8 @@ obj-$(CONFIG_RESET_SOURCE)+= reset_source.o
 obj-$(CONFIG_SHELL_HUSH)   += hush.o
 obj-$(CONFIG_SHELL_SIMPLE) += parser.o
 obj-$(CONFIG_STATE)+= state/
-obj-$(CONFIG_RATP) += ratp.o
+obj-$(CONFIG_RATP) += ratp/
+obj-$(CONFIG_CONSOLE_RATP) += ratp/
 obj-$(CONFIG_BOOTCHOOSER)  += bootchooser.o
 obj-$(CONFIG_UIMAGE)   += image.o uimage.o
 obj-$(CONFIG_FITIMAGE) += image-fit.o
@@ -60,7 +61,6 @@ obj-$(CONFIG_FILE_LIST)   += file-list.o
 obj-$(CONFIG_FIRMWARE) += firmware.o
 obj-$(CONFIG_UBIFORMAT)+= ubiformat.o
 obj-$(CONFIG_BAREBOX_UPDATE_IMX_NAND_FCB) += imx-bbu-nand-fcb.o
-obj-$(CONFIG_CONSOLE_RATP) += ratp.o
 obj-$(CONFIG_BOOT) += boot.o
 
 ifdef CONFIG_PASSWORD
diff --git a/common/ratp/Kconfig b/common/ratp/Kconfig
new file mode 100644
index 0..93ff75d64
--- /dev/null
+++ b/common/ratp/Kconfig
@@ -0,0 +1,14 @@
+
+config CONSOLE_RATP
+   bool
+   select RATP
+   select CRC16
+   select POLLER
+   depends on CONSOLE_FULL
+   prompt "RATP console support"
+   help
+ This option adds support for remote controlling barebox via serial
+ port. The regular console is designed for human interaction whereas
+ this option adds a machine readable interface for controlling barebox.
+ Say yes here if you want to control barebox from a remote host.
+
diff --git a/common/ratp/Makefile b/common/ratp/Makefile
new file mode 100644
index 0..cab14c6fb
--- /dev/null
+++ b/common/ratp/Makefile
@@ -0,0 +1 @@
+obj-y += ratp.o
diff --git a/common/ratp.c b/common/ratp/ratp.c
similarity index 100%
rename from common/ratp.c
rename to common/ratp/ratp.c
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 4/9] ratp: implement ping as a standard ratp command

2018-02-24 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 common/ratp/Makefile |  1 +
 common/ratp/ping.c   | 40 
 common/ratp/ratp.c   | 24 
 3 files changed, 41 insertions(+), 24 deletions(-)
 create mode 100644 common/ratp/ping.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index cab14c6fb..acad61ee5 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -1 +1,2 @@
 obj-y += ratp.o
+obj-y += ping.o
diff --git a/common/ratp/ping.c b/common/ratp/ping.c
new file mode 100644
index 0..cc29ea36c
--- /dev/null
+++ b/common/ratp/ping.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer <s.ha...@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+/*
+ * RATP ping
+ */
+
+#include 
+#include 
+#include 
+
+static int ratp_cmd_ping(const struct ratp_bb *req, int req_len,
+struct ratp_bb **rsp, int *rsp_len)
+{
+   /* Just build response */
+   *rsp_len = sizeof(struct ratp_bb);
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_PONG);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(PING)
+   .request_id = BB_RATP_TYPE_PING,
+   .response_id = BB_RATP_TYPE_PONG,
+   .cmd = ratp_cmd_ping
+BAREBOX_RATP_CMD_END
diff --git a/common/ratp/ratp.c b/common/ratp/ratp.c
index 7c8a2f6f5..965c67a0a 100644
--- a/common/ratp/ratp.c
+++ b/common/ratp/ratp.c
@@ -189,25 +189,6 @@ static int ratp_bb_send_command_return(struct ratp_ctx 
*ctx, uint32_t errno)
return ret;
 }
 
-static int ratp_bb_send_pong(struct ratp_ctx *ctx)
-{
-   void *buf;
-   struct ratp_bb *rbb;
-   int len = sizeof(*rbb);
-   int ret;
-
-   buf = xzalloc(len);
-   rbb = buf;
-
-   rbb->type = cpu_to_be16(BB_RATP_TYPE_PONG);
-
-   ret = ratp_send(>ratp, buf, len);
-
-   free(buf);
-
-   return ret;
-}
-
 static int ratp_bb_send_getenv_return(struct ratp_ctx *ctx, const char *val)
 {
void *buf;
@@ -270,7 +251,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
break;
 
case BB_RATP_TYPE_COMMAND_RETURN:
-   case BB_RATP_TYPE_PONG:
break;
 
case BB_RATP_TYPE_CONSOLEMSG:
@@ -280,10 +260,6 @@ static int ratp_bb_dispatch(struct ratp_ctx *ctx, const 
void *buf, int len)
kfifo_put(ctx->console_recv_fifo, rbb->data, dlen);
break;
 
-   case BB_RATP_TYPE_PING:
-   ret = ratp_bb_send_pong(ctx);
-   break;
-
case BB_RATP_TYPE_GETENV:
varname = xmemdup_add_zero(>data, dlen);
 
-- 
2.15.1


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 0/9] ratp: new generic RATP command support

2018-02-24 Thread Aleksander Morgado
This v3 series includes new md, mw and reset operations in bbremote, using the 
new binary API interface. The actual commits including the new RATP commands 
include the bbremote changes as well.

An additional patch is also included to add a reference to the third-party 
libratp-barebox and ratp-barebox-cli tools.

Aleksander Morgado (9):
  ratp: implement generic command support
  ratp: moved logic to its own subdirectory
  ratp: allow building without full console support
  ratp: implement ping as a standard ratp command
  ratp: implement getenv as a standard ratp command
  ratp: use xstrndup() instead of a custom xmemdup_add_zero()
  ratp: new md and mw commands
  ratp: new reset command
  docs: add reference to libratp-barebox in the remote-control docs

 Documentation/user/remote-control.rst |   7 ++
 arch/arm/lib32/barebox.lds.S  |   4 +
 arch/arm/lib64/barebox.lds.S  |   4 +
 arch/blackfin/boards/ipe337/barebox.lds.S |   5 +-
 arch/mips/lib/barebox.lds.S   |   4 +
 arch/nios2/cpu/barebox.lds.S  |   5 +-
 arch/openrisc/cpu/barebox.lds.S   |   4 +
 arch/ppc/boards/pcm030/barebox.lds.S  |   4 +
 arch/ppc/mach-mpc85xx/barebox.lds.S   |   4 +
 arch/sandbox/board/barebox.lds.S  |   5 +
 arch/x86/lib/barebox.lds.S|   7 ++
 arch/x86/mach-efi/elf_ia32_efi.lds.S  |   5 +
 arch/x86/mach-efi/elf_x86_64_efi.lds.S|   5 +
 common/Kconfig|  13 +-
 common/Makefile   |   3 +-
 common/module.lds.S   |   2 +
 common/ratp/Kconfig   |  14 +++
 common/ratp/Makefile  |   6 +
 common/ratp/getenv.c  |  51 
 common/ratp/md.c  | 202 ++
 common/ratp/mw.c  | 173 +
 common/ratp/ping.c|  40 ++
 common/{ => ratp}/ratp.c  | 156 +++
 common/ratp/reset.c   |  55 
 fs/Kconfig|   2 +-
 include/asm-generic/barebox.lds.h |   2 +
 include/ratp_bb.h |  52 
 lib/Kconfig   |   2 +-
 lib/readline.c|   2 +-
 scripts/remote/controller.py  |  27 
 scripts/remote/main.py|  51 
 scripts/remote/messages.py| 105 
 32 files changed, 918 insertions(+), 103 deletions(-)
 create mode 100644 common/ratp/Kconfig
 create mode 100644 common/ratp/Makefile
 create mode 100644 common/ratp/getenv.c
 create mode 100644 common/ratp/md.c
 create mode 100644 common/ratp/mw.c
 create mode 100644 common/ratp/ping.c
 rename common/{ => ratp}/ratp.c (81%)
 create mode 100644 common/ratp/reset.c

--
2.15.1

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v3 1/9] ratp: implement generic command support

2018-02-24 Thread Aleksander Morgado
The RATP implementation now allows executing generic commands with a
binary interface: binary requests are received and binary responses
are returned.

Each command can define its own RATP request contents (e.g. to specify
command-specific options) as well as its own RATP response contents
(if any data is to be returned).

Each command is associated with a pair of numeric unique request and
response IDs, and for easy reference these IDs are maintained in the
common ratp_bb header. Modules may override generic implemented
commands or include their own new ones (as long as the numeric IDs
introduced are unique).

Signed-off-by: Aleksander Morgado <aleksan...@aleksander.es>
---
 arch/arm/lib32/barebox.lds.S  |  4 ++
 arch/arm/lib64/barebox.lds.S  |  4 ++
 arch/blackfin/boards/ipe337/barebox.lds.S |  5 +-
 arch/mips/lib/barebox.lds.S   |  4 ++
 arch/nios2/cpu/barebox.lds.S  |  5 +-
 arch/openrisc/cpu/barebox.lds.S   |  4 ++
 arch/ppc/boards/pcm030/barebox.lds.S  |  4 ++
 arch/ppc/mach-mpc85xx/barebox.lds.S   |  4 ++
 arch/sandbox/board/barebox.lds.S  |  5 ++
 arch/x86/lib/barebox.lds.S|  7 +++
 arch/x86/mach-efi/elf_ia32_efi.lds.S  |  5 ++
 arch/x86/mach-efi/elf_x86_64_efi.lds.S|  5 ++
 common/module.lds.S   |  2 +
 common/ratp.c | 82 +--
 include/asm-generic/barebox.lds.h |  2 +
 include/ratp_bb.h | 47 ++
 16 files changed, 171 insertions(+), 18 deletions(-)

diff --git a/arch/arm/lib32/barebox.lds.S b/arch/arm/lib32/barebox.lds.S
index e7b87b7cd..6fadc2a35 100644
--- a/arch/arm/lib32/barebox.lds.S
+++ b/arch/arm/lib32/barebox.lds.S
@@ -85,6 +85,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/arm/lib64/barebox.lds.S b/arch/arm/lib64/barebox.lds.S
index 240699f1a..a53b933bb 100644
--- a/arch/arm/lib64/barebox.lds.S
+++ b/arch/arm/lib64/barebox.lds.S
@@ -82,6 +82,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/blackfin/boards/ipe337/barebox.lds.S 
b/arch/blackfin/boards/ipe337/barebox.lds.S
index 51a586af2..7e82a1bd7 100644
--- a/arch/blackfin/boards/ipe337/barebox.lds.S
+++ b/arch/blackfin/boards/ipe337/barebox.lds.S
@@ -68,6 +68,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
___barebox_cmd_end = .;
 
+   ___barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   ___barebox_ratp_cmd_end = .;
+
___barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
___barebox_magicvar_end = .;
@@ -91,4 +95,3 @@ SECTIONS
___bss_stop = .;
_end = .;
 }
-
diff --git a/arch/mips/lib/barebox.lds.S b/arch/mips/lib/barebox.lds.S
index 899f62b96..660d4be85 100644
--- a/arch/mips/lib/barebox.lds.S
+++ b/arch/mips/lib/barebox.lds.S
@@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
diff --git a/arch/nios2/cpu/barebox.lds.S b/arch/nios2/cpu/barebox.lds.S
index a2d7fa8cd..fbcd1cd3f 100644
--- a/arch/nios2/cpu/barebox.lds.S
+++ b/arch/nios2/cpu/barebox.lds.S
@@ -55,6 +55,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS }
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS }
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGICVARS }
__barebox_magicvar_end = .;
@@ -129,4 +133,3 @@ SECTIONS
_end = .;
PROVIDE (end = .);
 }
-
diff --git a/arch/openrisc/cpu/barebox.lds.S b/arch/openrisc/cpu/barebox.lds.S
index b819ca099..c6807aec3 100644
--- a/arch/openrisc/cpu/barebox.lds.S
+++ b/arch/openrisc/cpu/barebox.lds.S
@@ -57,6 +57,10 @@ SECTIONS
.barebox_cmd : { BAREBOX_CMDS } > ram
__barebox_cmd_end = .;
 
+   __barebox_ratp_cmd_start = .;
+   .barebox_ratp_cmd : { BAREBOX_RATP_CMDS } > ram
+   __barebox_ratp_cmd_end = .;
+
__barebox_magicvar_start = .;
.barebox_magicvar : { BAREBOX_MAGIC

Re: [RFC PATCH v2 7/8] ratp: new md and mw commands

2018-02-21 Thread Aleksander Morgado
>> read and write memory files without needing to do custom string
>> parsing on the data returned by the console 'md' and 'mw' operations.
>>
>> The request and response messages used for these new operations are
>> structured in the same way:
>>
>>  * An initial fixed-sized section includes the fixed-sized
>>variables (e.g. integers), as well as the size and offset of the
>>variable-length variables.
>>
>>  * After the initial fixed-sized section, the buffer is given, which
>>contains the variable-length variables in the offsets previously
>>defined and with the size previously defined.
>>
>> The message also defines separately the offset of the buffer
>> w.r.t. the start of the message. The endpoint reading the message will
>> use this information to decide where the buffer starts. This allows to
>> extend the message format in the future without needing to break the
>> message API, as new fields can be appended to the fixed-sized section
>> as long as the buffer offset is also updated to report the new
>> position of the buffer.
>>
>> E.g. testing with ratp-barebox-cli:
>>
>>   $ ratp-barebox-cli -t /dev/ttyUSB2 --md "/dev/pic_eeprom_rdu,0x107,5" 
>> --timeout 1000
>>   Sending md request: read '/dev/pic_eeprom_rdu': 0x0107 (+5 bytes)
>>   00:00:00:00:00
>
> It would be good to have to pointer to libratp and ratp-barebox-cli in
> Documentation/user/remote-control.rst.
>

I'll add the info, ok.

> What's your plan for the bbremote tool? It's a bit unfortunate to have
> the new features only available in an external tool.
>

Yeah, I knew you were going to say that :) So, don't know. Didn't want
to spend much time on it because the new commands (md, mw, reset)
could directly be run with bbremote as "bbremote run ..." and you
would get the same output just with a different format. The benefit of
the binary API is clear in libratp-barebox, i.e. to integrate it into
applications that would make use of those operations without requiring
formatting output for the human eye. The ratp-barebox-cli and bbremote
support of the commands with the binary API would just be a
convenience. I actually only developed the support for the new
commands in the cli to make sure the library worked, as a way of
testing it. That said, if you want I can try to implement them in
bbremote as well and provide the same kind of output that you'd see in
ratp-barebox-cli; it would at least be a way of testing the API
directly within barebox without requiring any external tool.

>> +static int do_ratp_mem_md(const char *filename,
>> +   loff_t start,
>> +   loff_t size,
>> +   uint8_t *output)
>> +{
>> + int r, now, t;
>> + int ret = 0;
>> + int fd;
>> + void *map;
>> +
>> + fd = open_and_lseek(filename, O_RWSIZE_1 | O_RDONLY, start);
>
> It would be nice to support different read/write widths. Not every
> memory is readable in byte size chunks. But this could be added later
> aswell, I just saw that the way you defined the messages allows it to
> add additional fields later.

Yes, the binary message API should allow extending the format with
additional fields. But now that you said that, I may actually include
the width in the format, will check that.

>> + if (fd < 0)
>> + return 1;
>
> Is '1' the right return value here? It goes into a variable named
> 'errno' which looks wrong.
>

Oh, probably not. I'll review that.

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


Re: Cannot set GPIOs in barebox

2018-07-30 Thread Aleksander Morgado
Hey,

>
> This topic could be ignored. GPIO set/get value functions from
> barebox are working fine BUT gpioinfo function has some bug it reports
> unchanged status for gpios value.
>

Not that I got into much detail, but when I saw that same issue (I
have some WIP patches to manage GPIOs via RATP) I just assumed that we
can reliably gpio_get_value() on input GPIOs, and gpio_set_value() on
output GPIOs. Trying to "read" the output GPIO value with
gpio_get_value() may not be reliable or even supported at all. Is that
assumption correct?

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] bbremote: add missing 'md' packet handler

2018-08-21 Thread Aleksander Morgado
Signed-off-by: Aleksander Morgado 
---
 scripts/remote/controller.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index 2ed834613..1a8390904 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -46,6 +46,9 @@ def unpack(data):
 elif p_type == BBType.fs_return:
 logging.debug("received: fs_return")
 return BBPacketFSReturn(raw=data)
+elif p_type == BBType.md:
+logging.debug("received: md")
+return BBPacketMd(raw=data)
 elif p_type == BBType.md_return:
 logging.debug("received: md_return")
 return BBPacketMdReturn(raw=data)
-- 
2.18.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 2/2] i2c_read: implement support for master receive mode

2018-08-21 Thread Aleksander Morgado
If no start register is explicitly given, receive data in master
receive mode.

Signed-off-by: Aleksander Morgado 
---
 commands/i2c.c | 21 +++--
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/commands/i2c.c b/commands/i2c.c
index 57dc092c2..2f7f820d4 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -208,7 +208,7 @@ static int do_i2c_read(int argc, char *argv[])
}
}
 
-   if ((addr < 0) || (reg < 0) || (count < 1) || (addr > 0x7F))
+   if ((addr < 0) || (count < 1) || (addr > 0x7F))
return COMMAND_ERROR_USAGE;
 
adapter = i2c_get_adapter(bus);
@@ -221,12 +221,21 @@ static int do_i2c_read(int argc, char *argv[])
client.addr = addr;
 
buf = xmalloc(count);
-   ret = i2c_read_reg(, reg | wide, buf, count);
+   if (reg >= 0)
+   ret = i2c_read_reg(, reg | wide, buf, count);
+   else
+   ret = i2c_master_recv(, buf, count);
if (ret == count) {
int i;
-   if (verbose)
-   printf("read %i bytes starting at reg 0x%04x from 
i2cdev 0x%02x on bus %i\n",
-   count, reg, addr, adapter->nr);
+   if (verbose) {
+   if (reg >= 0)
+   printf("read %i bytes starting at reg 0x%04x 
from i2cdev 0x%02x on bus %i\n",
+  count, reg, addr, adapter->nr);
+   else
+   printf("received %i bytes in master receive 
mode from i2cdev 0x%02x on bus %i\n",
+  count, addr, adapter->nr);
+   }
+
for (i = 0; i < count; i++)
printf("0x%02x ", *(buf + i));
printf("\n");
@@ -241,7 +250,7 @@ BAREBOX_CMD_HELP_START(i2c_read)
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT("-b BUS\t", "i2c bus number (default 0)")
 BAREBOX_CMD_HELP_OPT("-a ADDR\t", "i2c device address")
-BAREBOX_CMD_HELP_OPT("-r START", "start register")
+BAREBOX_CMD_HELP_OPT("-r START", "start register (optional, master receive 
mode if none given)")
 BAREBOX_CMD_HELP_OPT("-w\t",   "use word (16 bit) wide access")
 BAREBOX_CMD_HELP_OPT("-c COUNT", "byte count")
 BAREBOX_CMD_HELP_OPT("-v\t",   "verbose")
-- 
2.18.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 2/4] bbremote: implement i2c read/write support

2018-08-21 Thread Aleksander Morgado
Extend the bbremote script with operations to perform binary i2c
read/write operations.

E.g.:

barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x8f 0x30 0x00 0x00

barebox:/ i2c_write -b 0 -a 0x68 -r 0x06A0 -w 0x87 0x30 0x00 0x00

barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x87 0x30 0x00 0x00

barebox:/ i2c_write -b 0 -a 0x68 -r 0x06A0 -w 0x8f 0x30 0x00 0x00

barebox:/ i2c_read -b 0 -a 0x68 -r 0x06A0 -c 4 -w
0x8f 0x30 0x00 0x00

===

$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8f30

$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-write 0x00 0x68 0x06A0 0x01 
"8730"
4 bytes written

$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8730

$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-write 0x00 0x68 0x06A0 0x01 
"8f30"
4 bytes written

$ ./scripts/bbremote --port /dev/ttyUSB2 i2c-read 0x00 0x68 0x06A0 0x01 4
8f300000

Signed-off-by: Aleksander Morgado 
---
 scripts/remote/controller.py | 24 ++
 scripts/remote/main.py   | 37 
 scripts/remote/messages.py   | 86 
 3 files changed, 147 insertions(+)

diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index 2ed834613..e94ad88c0 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -58,6 +58,18 @@ def unpack(data):
 elif p_type == BBType.reset:
 logging.debug("received: reset")
 return BBPacketReset(raw=data)
+elif p_type == BBType.i2c_read:
+logging.debug("received: i2c_read")
+return BBPacketI2cRead(raw=data)
+elif p_type == BBType.i2c_read_return:
+logging.debug("received: i2c_read_return")
+return BBPacketI2cReadReturn(raw=data)
+elif p_type == BBType.i2c_write:
+logging.debug("received: i2c_write")
+return BBPacketI2cWrite(raw=data)
+elif p_type == BBType.i2c_write_return:
+logging.debug("received: i2c_write_return")
+return BBPacketI2cWriteReturn(raw=data)
 else:
 logging.debug("received: UNKNOWN")
 return BBPacket(raw=data)
@@ -136,6 +148,18 @@ class Controller(Thread):
 logging.info("Mw return: %r", r)
 return (r.exit_code,r.written)
 
+def i2c_read(self, bus, addr, reg, flags, size):
+self._send(BBPacketI2cRead(bus=bus, addr=addr, reg=reg, flags=flags, 
size=size))
+r = self._expect(BBPacketI2cReadReturn)
+logging.info("i2c read return: %r", r)
+return (r.exit_code,r.data)
+
+def i2c_write(self, bus, addr, reg, flags, data):
+self._send(BBPacketI2cWrite(bus=bus, addr=addr, reg=reg, flags=flags, 
data=data))
+r = self._expect(BBPacketI2cWriteReturn)
+logging.info("i2c write return: %r", r)
+return (r.exit_code,r.written)
+
 def reset(self, force):
 self._send(BBPacketReset(force=force))
 
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index 38d280bfe..0f7783927 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -98,6 +98,27 @@ def handle_mw(args):
 return res
 
 
+def handle_i2c_read(args):
+ctrl = get_controller(args)
+(res,data) = ctrl.i2c_read(args.bus, args.address, args.reg, args.flags, 
args.size)
+if res == 0:
+print(binascii.hexlify(data))
+ctrl.close()
+return res
+
+
+def handle_i2c_write(args):
+ctrl = get_controller(args)
+data=args.data
+if ((len(data) % 2) != 0):
+data="0"+data
+(res,written) = ctrl.i2c_write(args.bus, args.address, args.reg, 
args.flags, binascii.unhexlify(data))
+if res == 0:
+print("%i bytes written" % written)
+ctrl.close()
+return res
+
+
 def handle_reset(args):
 ctrl = get_controller(args)
 ctrl.reset(args.force)
@@ -188,6 +209,22 @@ parser_mw.add_argument('address', type=auto_int, 
help="address")
 parser_mw.add_argument('data', help="data")
 parser_mw.set_defaults(func=handle_mw)
 
+parser_i2c_read = subparsers.add_parser('i2c-read', help="run i2c read 
command")
+parser_i2c_read.add_argument('bus', type=auto_int, help="bus")
+parser_i2c_read.add_argument('address', type=auto_int, help="address")
+parser_i2c_read.add_argument('reg', type=auto_int, help="reg")
+parser_i2c_read.add_argument('flags', type=auto_int, help="flags")
+parser_i2c_read.add_argument('size', type=auto_int, help="size")
+parser_i2c_read.set_defaults(func=handle_i2c_read)
+
+parser_i2c_write = subparsers.add_parser('i2c-write', help="run i2c write 
command")
+parser_i2c_write.add_argument('bus', type=auto_int, help="bus")
+parser_i2c_write.add_argu

[PATCH 3/4] ratp: implement support for GPIO commands

2018-08-21 Thread Aleksander Morgado
Introduce three new RATP commands that allow getting and setting GPIO
values as well as configuring the direction of the GPIO pins.

Signed-off-by: Aleksander Morgado 
---
 common/ratp/Makefile |   1 +
 common/ratp/gpio.c   | 148 +++
 include/ratp_bb.h|   6 ++
 3 files changed, 155 insertions(+)
 create mode 100644 common/ratp/gpio.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index 0234b55c1..3b5e495ab 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -5,3 +5,4 @@ obj-y += md.o
 obj-y += mw.o
 obj-y += reset.o
 obj-y += i2c.o
+obj-y += gpio.o
diff --git a/common/ratp/gpio.c b/common/ratp/gpio.c
new file mode 100644
index 0..d247cd614
--- /dev/null
+++ b/common/ratp/gpio.c
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2018 Sascha Hauer , Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct ratp_bb_gpio_get_value_request {
+   struct ratp_bb header;
+   uint32_t   gpio;
+} __attribute__((packed));
+
+struct ratp_bb_gpio_get_value_response {
+   struct ratp_bb header;
+   uint8_tvalue;
+} __attribute__((packed));
+
+static int ratp_cmd_gpio_get_value(const struct ratp_bb *req, int req_len,
+  struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_gpio_get_value_request *gpio_req = (struct 
ratp_bb_gpio_get_value_request *)req;
+   struct ratp_bb_gpio_get_value_response *gpio_rsp;
+   int gpio_rsp_len;
+   uint32_t gpio;
+   uint8_t value;
+
+   if (req_len < sizeof (*gpio_req)) {
+   printf ("ratp gpio get value request ignored: size mismatch (%d 
< %zu)\n", req_len, sizeof (*gpio_req));
+   return 2;
+   }
+
+   gpio = be32_to_cpu (gpio_req->gpio);
+   value = !!gpio_get_value(gpio);
+
+   gpio_rsp_len = sizeof(struct ratp_bb_gpio_get_value_response);
+   gpio_rsp = xzalloc(gpio_rsp_len);
+   gpio_rsp->header.type = cpu_to_be16(BB_RATP_TYPE_GPIO_GET_VALUE_RETURN);
+   gpio_rsp->value = value;
+
+   *rsp_len = gpio_rsp_len;
+   *rsp = (struct ratp_bb *)gpio_rsp;
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(GPIO_GET_VALUE)
+   .request_id = BB_RATP_TYPE_GPIO_GET_VALUE,
+   .response_id = BB_RATP_TYPE_GPIO_GET_VALUE_RETURN,
+   .cmd = ratp_cmd_gpio_get_value
+BAREBOX_RATP_CMD_END
+
+
+struct ratp_bb_gpio_set_value_request {
+   struct ratp_bb header;
+   uint32_t   gpio;
+   uint8_tvalue;
+} __attribute__((packed));
+
+static int ratp_cmd_gpio_set_value(const struct ratp_bb *req, int req_len,
+  struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_gpio_set_value_request *gpio_req = (struct 
ratp_bb_gpio_set_value_request *)req;
+   uint32_t gpio;
+
+   if (req_len < sizeof (*gpio_req)) {
+   printf ("ratp gpio set value request ignored: size mismatch (%d 
< %zu)\n", req_len, sizeof (*gpio_req));
+   return 2;
+   }
+
+   gpio = be32_to_cpu (gpio_req->gpio);
+   gpio_set_value(gpio, gpio_req->value);
+
+   *rsp_len = sizeof(struct ratp_bb);
+   *rsp = xzalloc(*rsp_len);
+   (*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GPIO_SET_VALUE_RETURN);
+   return 0;
+}
+
+BAREBOX_RATP_CMD_START(GPIO_SET_VALUE)
+   .request_id = BB_RATP_TYPE_GPIO_SET_VALUE,
+   .response_id = BB_RATP_TYPE_GPIO_SET_VALUE_RETURN,
+   .cmd = ratp_cmd_gpio_set_value
+BAREBOX_RATP_CMD_END
+
+
+struct ratp_bb_gpio_set_direction_request {
+   struct ratp_bb header;
+   uint32_t   gpio;
+   uint8_tdirection; /* 0: input, 1: output */
+   uint8_tvalue; /* applicable only if direction output */
+} __attribute__((packed));
+
+struct ratp_bb_gpio_set_direction_response {
+   struct ratp_bb header;
+   uint32_t   errno;
+} __attribute__((packed));
+
+static int ratp_cmd_gpio_set_direction(const struct ratp_bb *req, int req_len,
+  struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_gpio_set_direction_request *gpio_req = (struct 
ratp_bb_gpio_set_direction_request *)req;
+   struct ratp_bb_gpio_set_direction_response *gpio_rsp;
+   int gpio_rsp_len;
+   uint32_t gpio;
+   int ret;
+
+   if (req_len < sizeof (*gpio_req)) {
+   printf ("ratp gp

[PATCH 1/2] i2c_write: document master send mode

2018-08-21 Thread Aleksander Morgado
When no explicit start register is given, the i2c message is emitted
in master send mode.

Signed-off-by: Aleksander Morgado 
---
 commands/i2c.c | 10 +++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/commands/i2c.c b/commands/i2c.c
index f0d16af0c..57dc092c2 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -145,8 +145,12 @@ static int do_i2c_write(int argc, char *argv[])
ret = 0;
 
if (verbose) {
-   printf("wrote %i bytes starting at reg 0x%04x to i2cdev 0x%02x 
on bus %i\n",
-   count, reg, addr, adapter->nr);
+   if (reg >= 0)
+   printf("wrote %i bytes starting at reg 0x%04x to i2cdev 
0x%02x on bus %i\n",
+  count, reg, addr, adapter->nr);
+   else
+   printf("sent %i bytes in master send mode to i2cdev 
0x%02x on bus %i\n",
+  count, addr, adapter->nr);
for (i = 0; i < count; i++)
printf("0x%02x ", *(buf + i));
printf("\n");
@@ -161,7 +165,7 @@ BAREBOX_CMD_HELP_START(i2c_write)
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT ("-b BUS\t", "i2c bus number (default 0)")
 BAREBOX_CMD_HELP_OPT ("-a ADDR\t", "i2c device address")
-BAREBOX_CMD_HELP_OPT ("-r START", "start register")
+BAREBOX_CMD_HELP_OPT ("-r START", "start register (optional, master send mode 
if none given)")
 BAREBOX_CMD_HELP_OPT ("-w\t",   "use word (16 bit) wide access")
 BAREBOX_CMD_HELP_OPT ("-v\t",   "verbose")
 BAREBOX_CMD_HELP_END
-- 
2.18.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


i2c master send/receive mode

2018-08-21 Thread Aleksander Morgado
The i2c master send support was already implemented in the i2c_write command, 
but it was not properly documented. The first patch in the series addresses 
that.

In the second patch, the i2c master receive mode is implemented in the i2c_read 
command.

 [PATCH 1/2] i2c_write: document master send mode
 [PATCH 2/2] i2c_read: implement support for master receive mode


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 1/4] ratp: implement i2c read/write support

2018-08-21 Thread Aleksander Morgado
Introduce two new RATP commands that allow running i2c read/write
operations, very similar in format to the already existing md/mw
RATP commands.

The messages are defined with a fixed 16-bit long register field, but
it will only be treated as a 16-bit address if I2C_FLAG_WIDE_ADDRESS
is set in the message flags field. If this flag is unset, the start
register address is assumed 8-bit long.

If the message includes the I2C_FLAG_MASTER_MODE flag, the start
register field is ignored and a i2c master send/receive operation is
performed.

Signed-off-by: Aleksander Morgado 
---
 common/ratp/Makefile |   1 +
 common/ratp/i2c.c| 281 +++
 include/ratp_bb.h|   4 +
 3 files changed, 286 insertions(+)
 create mode 100644 common/ratp/i2c.c

diff --git a/common/ratp/Makefile b/common/ratp/Makefile
index 2c6d674f6..0234b55c1 100644
--- a/common/ratp/Makefile
+++ b/common/ratp/Makefile
@@ -4,3 +4,4 @@ obj-y += getenv.o
 obj-y += md.o
 obj-y += mw.o
 obj-y += reset.o
+obj-y += i2c.o
diff --git a/common/ratp/i2c.c b/common/ratp/i2c.c
new file mode 100644
index 0..b8d055b67
--- /dev/null
+++ b/common/ratp/i2c.c
@@ -0,0 +1,281 @@
+/*
+ * Copyright (c) 2011-2018 Sascha Hauer , Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* NOTE:
+ *  - Fixed-size fields (e.g. integers) are given just after the header.
+ *  - Variable-length fields are stored inside the buffer[] and their position
+ *within the buffer[] and their size are given as fixed-sized fields after
+ *the header.
+ *  The message may be extended at any time keeping backwards compatibility,
+ *  as the position of the buffer[] is given by the buffer_offset field. i.e.
+ *  increasing the buffer_offset field we can extend the fixed-sized section
+ *  to add more fields.
+ */
+
+#define I2C_FLAG_WIDE_ADDRESS (1 << 0)
+#define I2C_FLAG_MASTER_MODE  (1 << 1)
+
+struct ratp_bb_i2c_read_request {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint8_t  bus;
+   uint8_t  addr;
+   uint16_t reg;
+   uint8_t  flags;
+   uint16_t size;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+struct ratp_bb_i2c_read_response {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint32_t errno;
+   uint16_t data_size;
+   uint16_t data_offset;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+struct ratp_bb_i2c_write_request {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint8_t  bus;
+   uint8_t  addr;
+   uint16_t reg;
+   uint8_t  flags;
+   uint16_t data_size;
+   uint16_t data_offset;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+struct ratp_bb_i2c_write_response {
+   struct ratp_bb header;
+   uint16_t buffer_offset;
+   uint32_t errno;
+   uint16_t written;
+   uint8_t  buffer[];
+} __attribute__((packed));
+
+static int ratp_cmd_i2c_read(const struct ratp_bb *req, int req_len,
+struct ratp_bb **rsp, int *rsp_len)
+{
+   struct ratp_bb_i2c_read_request *i2c_read_req = (struct 
ratp_bb_i2c_read_request *)req;
+   struct ratp_bb_i2c_read_response *i2c_read_rsp;
+   struct i2c_adapter *adapter;
+   struct i2c_client client;
+   uint16_t buffer_offset;
+   int i2c_read_rsp_len;
+   uint16_t reg;
+   uint16_t size;
+   uint32_t wide = 0;
+   int ret = 0;
+
+   /* At least message header should be valid */
+   if (req_len < sizeof(*i2c_read_req)) {
+   printf("ratp i2c read ignored: size mismatch (%d < %zu)\n",
+  req_len, sizeof (*i2c_read_req));
+   ret = -EINVAL;
+   goto out_rsp;
+   }
+
+   /* We don't require any buffer here, but just validate buffer position 
and size */
+   buffer_offset = be16_to_cpu(i2c_read_req->buffer_offset);
+   if (buffer_offset != req_len) {
+   printf("ratp i2c read ignored: invalid buffer offset (%hu != 
%d)\n",
+  buffer_offset, req_len);
+   ret = -EINVAL;
+   goto out_rsp;
+   }
+
+   reg  = be16_to_cpu (i2c_read_req->reg);
+   size = be16_to_cpu (i2c_read_req->size);
+   if (i2c_read_req->flags & I2C_FLAG_WIDE_ADDRESS)
+   wide = I2C_ADDR_16_BIT;
+
+out_rsp:
+   /* A

RATP i2c and GPIO support

2018-08-21 Thread Aleksander Morgado
This patch series implements support for i2c and GPIO operations via RATP.

  [PATCH 1/4] ratp: implement i2c read/write support
  [PATCH 2/4] bbremote: implement i2c read/write support
  [PATCH 3/4] ratp: implement support for GPIO commands
  [PATCH 4/4] bbremote: implement support for GPIO operations


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH 4/4] bbremote: implement support for GPIO operations

2018-08-21 Thread Aleksander Morgado
Extend the bbremote script with operations to manage GPIOs.

E.g.:

barebox@ZII RDU2+ Board:/ gpio_direction_input 205
barebox@ZII RDU2+ Board:/ gpio_get_value 205
barebox@ZII RDU2+ Board:/ echo $?
1

barebox@ZII RDU2+ Board:/ gpio_direction_input 206
barebox@ZII RDU2+ Board:/ gpio_get_value 206
barebox@ZII RDU2+ Board:/ echo $?
0

barebox@ZII RDU2+ Board:/ gpio_direction_output 204 0
barebox@ZII RDU2+ Board:/ gpio_set_value 204 1

===

$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-direction 205 0 0
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-get-value 205
1

$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-direction 206 0 0
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-get-value 206
0

$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-direction 200 1 0
$ ./scripts/bbremote --port /dev/ttyUSB2 gpio-set-value 200 1

Signed-off-by: Aleksander Morgado 
---
 scripts/remote/controller.py | 36 ++
 scripts/remote/main.py   | 37 ++
 scripts/remote/messages.py   | 93 
 3 files changed, 166 insertions(+)

diff --git a/scripts/remote/controller.py b/scripts/remote/controller.py
index e94ad88c0..f64eede98 100644
--- a/scripts/remote/controller.py
+++ b/scripts/remote/controller.py
@@ -70,6 +70,24 @@ def unpack(data):
 elif p_type == BBType.i2c_write_return:
 logging.debug("received: i2c_write_return")
 return BBPacketI2cWriteReturn(raw=data)
+elif p_type == BBType.gpio_get_value:
+logging.debug("received: gpio_get_value")
+return BBPacketGpioGetValue(raw=data)
+elif p_type == BBType.gpio_get_value_return:
+logging.debug("received: gpio_get_value_return")
+return BBPacketGpioGetValueReturn(raw=data)
+elif p_type == BBType.gpio_set_value:
+logging.debug("received: gpio_set_value")
+return BBPacketGpioSetValue(raw=data)
+elif p_type == BBType.gpio_set_value_return:
+logging.debug("received: gpio_set_value_return")
+return BBPacketGpioSetValueReturn(raw=data)
+elif p_type == BBType.gpio_set_direction:
+logging.debug("received: gpio_set_direction")
+return BBPacketGpioSetDirection(raw=data)
+elif p_type == BBType.gpio_set_direction_return:
+logging.debug("received: gpio_set_direction_return")
+return BBPacketGpioSetDirectionReturn(raw=data)
 else:
 logging.debug("received: UNKNOWN")
 return BBPacket(raw=data)
@@ -160,6 +178,24 @@ class Controller(Thread):
 logging.info("i2c write return: %r", r)
 return (r.exit_code,r.written)
 
+def gpio_get_value(self, gpio):
+self._send(BBPacketGpioGetValue(gpio=gpio))
+r = self._expect(BBPacketGpioGetValueReturn)
+logging.info("gpio get value return: %r", r)
+return r.value
+
+def gpio_set_value(self, gpio, value):
+self._send(BBPacketGpioSetValue(gpio=gpio, value=value))
+r = self._expect(BBPacketGpioSetValueReturn)
+logging.info("gpio set value return: %r", r)
+return 0
+
+def gpio_set_direction(self, gpio, direction, value):
+self._send(BBPacketGpioSetDirection(gpio=gpio, direction=direction, 
value=value))
+r = self._expect(BBPacketGpioSetDirectionReturn)
+logging.info("gpio set direction return: %r", r)
+return r.exit_code
+
 def reset(self, force):
 self._send(BBPacketReset(force=force))
 
diff --git a/scripts/remote/main.py b/scripts/remote/main.py
index 0f7783927..cef5d92ee 100644
--- a/scripts/remote/main.py
+++ b/scripts/remote/main.py
@@ -119,6 +119,28 @@ def handle_i2c_write(args):
 return res
 
 
+def handle_gpio_get_value(args):
+ctrl = get_controller(args)
+value = ctrl.gpio_get_value(args.gpio)
+print ("%u" % value);
+ctrl.close()
+return 0
+
+
+def handle_gpio_set_value(args):
+ctrl = get_controller(args)
+ctrl.gpio_set_value(args.gpio, args.value)
+ctrl.close()
+return 0
+
+
+def handle_gpio_set_direction(args):
+ctrl = get_controller(args)
+res = ctrl.gpio_set_direction(args.gpio, args.direction, args.value)
+ctrl.close()
+return res
+
+
 def handle_reset(args):
 ctrl = get_controller(args)
 ctrl.reset(args.force)
@@ -225,6 +247,21 @@ parser_i2c_write.add_argument('flags', type=auto_int, 
help="flags")
 parser_i2c_write.add_argument('data', help="data")
 parser_i2c_write.set_defaults(func=handle_i2c_write)
 
+parser_gpio_get_value = subparsers.add_parser('gpio-get-value', help="run gpio 
get value command")
+parser_gpio_get_value.add_argument('gpio', type=auto_int, help="gpio")
+parser_gpio_get_value.set_defaults(func=handle_gpio_get_value)
+

Re: [PATCH 1/4] ratp: implement i2c read/write support

2018-08-23 Thread Aleksander Morgado
>> +struct ratp_bb_i2c_read_request {
>> + struct ratp_bb header;
>> + uint16_t buffer_offset;
>> + uint8_t  bus;
>> + uint8_t  addr;
>
> I wonder how we see the RATP support. If it's for adhoc debugging then
> bus/addr is fine. The caller should have no expectations that the bus
> number is constant though. Likewise for the address which might change
> across different board revisions.
>
> Should we have support for resolving names, which could be provided by
> aliases in dt?
>
> We could still add name resolving support later as a separate call, I
> just thought that now is the time to think how we proceed.
>

I truly have no opinion here, but if name resolving is added at some
point I can either update this operation or even add a new one.

-- 
Aleksander
https://aleksander.es

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH] ratp: fix sending data that won't fit in a single RATP packet

2018-08-30 Thread Aleksander Morgado
We need to advance the input buffer used to create messages when the
data doesn't fit in a single RATP packet.

Signed-off-by: Aleksander Morgado 
---
 include/ratp.h | 2 +-
 lib/ratp.c | 6 --
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/include/ratp.h b/include/ratp.h
index 6f4cf8a6f..d2a819235 100644
--- a/include/ratp.h
+++ b/include/ratp.h
@@ -11,7 +11,7 @@ int ratp_establish(struct ratp *ratp, bool active, int 
timeout_ms);
 void ratp_close(struct ratp *ratp);
 int ratp_recv(struct ratp *ratp, void **data, size_t *len);
 int ratp_send(struct ratp *ratp, const void *data, size_t len);
-int ratp_send_complete(struct ratp *ratp, const void *data, size_t len,
+int ratp_send_complete(struct ratp *ratp, const uint8_t *data, size_t len,
   void (*complete)(void *ctx, int status), void *complete_ctx);
 int ratp_poll(struct ratp *ratp);
 bool ratp_closed(struct ratp *ratp);
diff --git a/lib/ratp.c b/lib/ratp.c
index 4c5c748b4..7801cae51 100644
--- a/lib/ratp.c
+++ b/lib/ratp.c
@@ -1734,11 +1734,12 @@ void ratp_close(struct ratp *ratp)
  *
  * Return: 0 if successful, a negative error code otherwise.
  */
-int ratp_send_complete(struct ratp *ratp, const void *data, size_t len,
+int ratp_send_complete(struct ratp *ratp, const uint8_t *data, size_t len,
   void (*complete)(void *ctx, int status), void *complete_ctx)
 {
struct ratp_internal *ri = ratp->internal;
struct ratp_message *msg;
+   int sent = 0;

if (!ri || ri->state != RATP_STATE_ESTABLISHED)
return -ENETDOWN;
@@ -1754,11 +1755,12 @@ int ratp_send_complete(struct ratp *ratp, const void 
*data, size_t len,
msg = xzalloc(sizeof(*msg));
msg->buf = xzalloc(sizeof(struct ratp_header) + now + 2);
msg->len = now;
-   memcpy(msg->buf + sizeof(struct ratp_header), data, now);
+   memcpy(msg->buf + sizeof(struct ratp_header), data + sent, now);

list_add_tail(>list, >sendmsg);

len -= now;
+   sent += now;
}

msg->eor = 1;
--
2.18.0

___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


[PATCH v2 7/7] ratp: fix incorrect whitespaces in method calls

2018-09-12 Thread Aleksander Morgado
This is a simple coding style fix to avoid the whitespace before the
open-parenthesis in method calls.

Signed-off-by: Aleksander Morgado 
---
 common/ratp/getenv.c | 12 ++--
 common/ratp/md.c |  8 
 common/ratp/mw.c |  6 +++---
 common/ratp/reset.c  |  4 ++--
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/common/ratp/getenv.c b/common/ratp/getenv.c
index b40963488..fdb4b0b28 100644
--- a/common/ratp/getenv.c
+++ b/common/ratp/getenv.c
@@ -27,20 +27,20 @@
 static int ratp_cmd_getenv(const struct ratp_bb *req, int req_len,
   struct ratp_bb **rsp, int *rsp_len)
 {
-   int dlen = req_len - sizeof (struct ratp_bb);
+   int dlen = req_len - sizeof(struct ratp_bb);
char *varname;
const char *value;
 
-   varname = xstrndup ((const char *)req->data, dlen);
-   value = getenv (varname);
-   free (varname);
+   varname = xstrndup((const char *)req->data, dlen);
+   value = getenv(varname);
+   free(varname);
 
-   dlen = strlen (value);
+   dlen = strlen(value);
 
*rsp_len = sizeof(struct ratp_bb) + dlen;
*rsp = xzalloc(*rsp_len);
(*rsp)->type = cpu_to_be16(BB_RATP_TYPE_GETENV_RETURN);
-   memcpy ((*rsp)->data, value, dlen);
+   memcpy((*rsp)->data, value, dlen);
return 0;
 }
 
diff --git a/common/ratp/md.c b/common/ratp/md.c
index 2e5a956cb..9ce7e99df 100644
--- a/common/ratp/md.c
+++ b/common/ratp/md.c
@@ -124,7 +124,7 @@ static int ratp_cmd_md(const struct ratp_bb *req, int 
req_len,
/* At least message header should be valid */
if (req_len < sizeof(*md_req)) {
pr_err("ignored: size mismatch (%d < %zu)\n",
-  req_len, sizeof (*md_req));
+  req_len, sizeof(*md_req));
ret = -EINVAL;
goto out;
}
@@ -162,8 +162,8 @@ static int ratp_cmd_md(const struct ratp_bb *req, int 
req_len,
goto out;
}
 
-   addr = be16_to_cpu (md_req->addr);
-   size = be16_to_cpu (md_req->size);
+   addr = be16_to_cpu(md_req->addr);
+   size = be16_to_cpu(md_req->size);
path = xstrndup((const char *)[path_offset], path_size);
 
 out:
@@ -193,7 +193,7 @@ out:
*rsp = (struct ratp_bb *)md_rsp;
*rsp_len = md_rsp_len;
 
-   free (path);
+   free(path);
return ret;
 }
 
diff --git a/common/ratp/mw.c b/common/ratp/mw.c
index 0579da3c1..55e79bbaf 100644
--- a/common/ratp/mw.c
+++ b/common/ratp/mw.c
@@ -80,7 +80,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int req_len,
/* At least message header should be valid */
if (req_len < sizeof(*mw_req)) {
pr_err("ignored: size mismatch (%d < %zu)\n",
-  req_len, sizeof (*mw_req));
+  req_len, sizeof(*mw_req));
ret = -EINVAL;
goto out;
}
@@ -131,7 +131,7 @@ static int ratp_cmd_mw(const struct ratp_bb *req, int 
req_len,
goto out;
}
 
-   addr = be16_to_cpu (mw_req->addr);
+   addr = be16_to_cpu(mw_req->addr);
path = xstrndup((const char *)[path_offset], path_size);
 
fd = open_and_lseek(path, O_RWSIZE_1 | O_WRONLY, addr);
@@ -164,7 +164,7 @@ out:
*rsp = (struct ratp_bb *)mw_rsp;
*rsp_len = sizeof(*mw_rsp);
 
-   free (path);
+   free(path);
return ret;
 }
 
diff --git a/common/ratp/reset.c b/common/ratp/reset.c
index 5439f344f..d0229d5d6 100644
--- a/common/ratp/reset.c
+++ b/common/ratp/reset.c
@@ -36,8 +36,8 @@ static int ratp_cmd_reset(const struct ratp_bb *req, int 
req_len,
 {
struct ratp_bb_reset *reset_req = (struct ratp_bb_reset *)req;
 
-   if (req_len < sizeof (*reset_req)) {
-   pr_err("ignored: size mismatch (%d < %zu)\n", req_len, sizeof 
(*reset_req));
+   if (req_len < sizeof(*reset_req)) {
+   pr_err("ignored: size mismatch (%d < %zu)\n", req_len, 
sizeof(*reset_req));
return 2;
}
 
-- 
2.19.0


___
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  1   2   >