Skip to content

Commit

Permalink
ratp: new reset command
Browse files Browse the repository at this point in the history
E.g.:
    $ ./bbremote -v --port /dev/ttyUSB2 reset

Signed-off-by: Aleksander Morgado <[email protected]>
Signed-off-by: Sascha Hauer <[email protected]>
  • Loading branch information
aleksander0m authored and saschahauer committed Mar 1, 2018
1 parent bfcdef3 commit 266db81
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions common/ratp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ obj-y += ping.o
obj-y += getenv.o
obj-y += md.o
obj-y += mw.o
obj-y += reset.o
55 changes: 55 additions & 0 deletions common/ratp/reset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* reset.c - reset the cpu
*
* Copyright (c) 2007 Sascha Hauer <[email protected]>, 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 <common.h>
#include <command.h>
#include <ratp_bb.h>
#include <complete.h>
#include <getopt.h>
#include <restart.h>

struct ratp_bb_reset {
struct ratp_bb header;
uint8_t force;
} __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
1 change: 1 addition & 0 deletions include/ratp_bb.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define BB_RATP_TYPE_MD_RETURN 11
#define BB_RATP_TYPE_MW 12
#define BB_RATP_TYPE_MW_RETURN 13
#define BB_RATP_TYPE_RESET 14

struct ratp_bb {
uint16_t type;
Expand Down
9 changes: 6 additions & 3 deletions scripts/remote/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -136,6 +136,9 @@ def mw(self, path, addr, data):
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()

Expand Down
13 changes: 13 additions & 0 deletions scripts/remote/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -181,6 +188,12 @@ def auto_int(x):
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(required=False)
parser_reset_force.add_argument('--force', dest='force', action='store_true')
parser_reset_force.add_argument('--no-force', dest='force', action='store_false')
parser_reset.set_defaults(func=handle_reset,force=False)

parser_listen = subparsers.add_parser('listen', help="listen for an incoming connection")
parser_listen.set_defaults(func=handle_listen)

Expand Down
16 changes: 16 additions & 0 deletions scripts/remote/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BBType(object):
md_return = 11
mw = 12
mw_return = 13
reset = 14


class BBPacket(object):
Expand Down Expand Up @@ -241,3 +242,18 @@ def _unpack_payload(self, payload):
def _pack_payload(self):
# header size is always 4 bytes (HH) and we have 8 bytes of fixed data (HLH), so buffer offset is 14
return struct.pack("!HLH", 12, self.exit_code, self.written)


class BBPacketReset(BBPacket):
def __init__(self, raw=None, force=None):
self.force = force
super(BBPacketReset, self).__init__(BBType.reset, raw=raw)

def __repr__(self):
return "BBPacketReset(force=%c)" % (self.force)

def _unpack_payload(self, payload):
self.force = struct.unpack("?", payload[:1])

def _pack_payload(self):
return struct.pack("?", self.force)

0 comments on commit 266db81

Please sign in to comment.