[Pdbg] [PATCH 13/29] main: Use new command handling for cfams
Cyril Bur
cyrilbur at gmail.com
Fri Feb 9 15:38:41 AEDT 2018
Signed-off-by: Cyril Bur <cyrilbur at gmail.com>
---
src/cfam.c | 47 +++++++++++++++++++++++++++++++++++++++++++++--
src/cfam.h | 3 +--
src/main.c | 24 ++++--------------------
src/main.h | 2 ++
4 files changed, 52 insertions(+), 24 deletions(-)
diff --git a/src/cfam.c b/src/cfam.c
index 8120e20..3ba8224 100644
--- a/src/cfam.c
+++ b/src/cfam.c
@@ -13,12 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <inttypes.h>
#include <target.h>
-int getcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t *unused)
+#include "main.h"
+
+static int getcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t *unused)
{
uint32_t value;
@@ -30,7 +35,7 @@ int getcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t
return 1;
}
-int putcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t *data)
+static int putcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t *data)
{
if (fsi_write(target, *addr, *data))
return 0;
@@ -38,6 +43,44 @@ int putcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t
return 1;
}
+int handle_cfams(int optind, int argc, char *argv[])
+{
+ uint64_t addr;
+ char *endptr;
+
+ if (optind + 1 >= argc) {
+ printf("%s: command '%s' requires an address\n", argv[0], argv[optind]);
+ return -1;
+ }
+
+ errno = 0;
+ addr = strtoull(argv[optind + 1], &endptr, 0);
+ if (errno || *endptr != '\0') {
+ printf("%s: command '%s' couldn't parse address '%s'\n",
+ argv[0], argv[optind], argv[optind + 1]);
+ return -1;
+ }
+
+ if (strcmp(argv[optind], "putcfam") == 0) {
+ uint64_t data;
+ if (optind + 2 >= argc) {
+ printf("%s: command '%s' requires data\n", argv[0], argv[optind]);
+ return -1;
+ }
+
+ errno = 0;
+ data = strtoull(argv[optind + 2], &endptr, 0);
+ if (errno || *endptr != '\0') {
+ printf("%s: command '%s' couldn't parse data '%s'\n",
+ argv[0], argv[optind], argv[optind + 1]);
+ return -1;
+ }
+
+ return for_each_target("fsi", putcfam, &addr, &data);
+ }
+
+ return for_each_target("fsi", getcfam, &addr, NULL);
+}
diff --git a/src/cfam.h b/src/cfam.h
index 80354e4..997ed3d 100644
--- a/src/cfam.h
+++ b/src/cfam.h
@@ -15,5 +15,4 @@
*/
#include <inttypes.h>
-int getcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t *unused);
-int putcfam(struct pdbg_target *target, uint32_t index, uint64_t *addr, uint64_t *data);
+int handle_cfams(int optind, int argc, char *argv[]);
diff --git a/src/main.c b/src/main.c
index ca060d6..708f314 100644
--- a/src/main.c
+++ b/src/main.c
@@ -84,7 +84,8 @@ static struct {
const char *desc;
int (*fn)(int, int, char **);
} actions[] = {
- { "none yet", "nothing", "placeholder", NULL },
+ { "getcfam", "<address>", "Read system cfam", &handle_cfams },
+ { "putcfam", "<address> <value> [<mask>]", "Write system cfam", &handle_cfams },
};
static void print_usage(char *pname)
@@ -145,19 +146,9 @@ enum command parse_cmd(char *optarg)
{
cmd_max_arg_count = 0;
- if (strcmp(optarg, "getcfam") == 0) {
- cmd = GETCFAM;
- cmd_min_arg_count = 1;
- } else if (strcmp(optarg, "getscom") == 0) {
+ if (strcmp(optarg, "getscom") == 0) {
cmd = GETSCOM;
cmd_min_arg_count = 1;
- } else if (strcmp(optarg, "putcfam") == 0) {
- cmd = PUTCFAM;
- cmd_min_arg_count = 2;
- cmd_max_arg_count = 3;
-
- /* No mask by default */
- cmd_args[2] = -1ULL;
} else if (strcmp(optarg, "putscom") == 0) {
cmd = PUTSCOM;
cmd_min_arg_count = 2;
@@ -430,8 +421,7 @@ int for_each_child_target(char *class, struct pdbg_target *parent,
return rc;
}
-/* Call the given call back on each enabled target in the given class */
-static int for_each_target(char *class, int (*cb)(struct pdbg_target *, uint32_t, uint64_t *, uint64_t *), uint64_t *arg1, uint64_t *arg2)
+int for_each_target(char *class, int (*cb)(struct pdbg_target *, uint32_t, uint64_t *, uint64_t *), uint64_t *arg1, uint64_t *arg2)
{
struct pdbg_target *target;
uint32_t index;
@@ -625,12 +615,6 @@ int main(int argc, char *argv[])
return -1;
switch(cmd) {
- case GETCFAM:
- rc = for_each_target("fsi", getcfam, &cmd_args[0], NULL);
- break;
- case PUTCFAM:
- rc = for_each_target("fsi", putcfam, &cmd_args[0], &cmd_args[1]);
- break;
case GETSCOM:
rc = for_each_target("pib", getscom, &cmd_args[0], NULL);
break;
diff --git a/src/main.h b/src/main.h
index ef22ba8..e47d3dc 100644
--- a/src/main.h
+++ b/src/main.h
@@ -21,3 +21,5 @@
int for_each_child_target(char *class, struct pdbg_target *parent,
int (*cb)(struct pdbg_target *, uint32_t, uint64_t *, uint64_t *),
uint64_t *arg1, uint64_t *arg2);
+
+int for_each_target(char *class, int (*cb)(struct pdbg_target *, uint32_t, uint64_t *, uint64_t *), uint64_t *arg1, uint64_t *arg2);
--
2.16.1
More information about the Pdbg
mailing list