Skip to content

Commit

Permalink
cmd: devmem: rework for argp
Browse files Browse the repository at this point in the history
Signed-off-by: Tan Siewert <[email protected]>
  • Loading branch information
sinuscosinustan committed Dec 10, 2024
1 parent 1147130 commit c78889f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
59 changes: 56 additions & 3 deletions src/cmd/devmem.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2018,2021 IBM Corp.

/* For program_invocation_short_name */
#define _GNU_SOURCE

#include <argp.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ahb.h"
#include "arg_helper.h"
#include "ast.h"
#include "bridge/devmem.h"
#include "priv.h"

int cmd_devmem(const char *name, int argc, char *argv[])
struct cmd_devmem_args {
char *args[3];
};

static struct argp_option options[] = {
{0}
};

static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
switch (key) {
case ARGP_KEY_ARG:
if (state->arg_num == 0) {
if (strcmp("read", arg) && strcmp("write", arg))
argp_usage(state);
}
break;
case ARGP_KEY_END:
if (!strcmp("write", state->argv[1])) {
if (state->argc != 4)
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}

return 0;
}

static struct argp argp_devmem = {
options,
parse_opt,
"read|write ADDRESS [VALUE]",
"/dev/mem stuff",
NULL,
NULL,
NULL
};

int cmd_devmem(struct argp_state* state)
{
struct devmem _devmem, *devmem = &_devmem;
struct ahb *ahb;
int cleanup;
int rc;

struct subcommand devmem_cmd;
struct cmd_devmem_args arguments = {0};
parse_subcommand(&argp_devmem, "devmem", &arguments, state, &devmem_cmd);

rc = devmem_init(devmem);
if (rc < 0) {
bool denied = (rc == -EACCES || rc == -EPERM);
if (denied && !priv_am_root()) {
priv_print_unprivileged(name);
priv_print_unprivileged(program_invocation_short_name);
} else {
errno = -rc;
perror("devmem_init");
Expand All @@ -29,7 +80,9 @@ int cmd_devmem(const char *name, int argc, char *argv[])
}

ahb = devmem_as_ahb(devmem);
rc = ast_ahb_access(name, argc, argv, ahb);
/* FIXME: argc + argv once all commands are migrated */
rc = ast_ahb_access(program_invocation_short_name,
devmem_cmd.argc - 1, devmem_cmd.argv + 1, ahb);
if (rc) {
errno = -rc;
perror("ast_ahb_access");
Expand Down
5 changes: 1 addition & 4 deletions src/culvert.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
// Copyright (C) 2018,2021 IBM Corp.
// Copyright (C) 2021, Oracle and/or its affiliates.

/* For program_invocation_short_name */
#define _GNU_SOURCE

#include <errno.h>
#include <argp.h>
#include <stdbool.h>
Expand All @@ -22,12 +19,12 @@
/* Migrated to argp */
int cmd_console(struct argp_state *state);
int cmd_coprocessor(struct argp_state *state);
int cmd_devmem(struct argp_state *state);

/* Still using the old argument parser -> to be migrated */
int cmd_ilpc(const char *name, int argc, char *argv[]);
int cmd_p2a(const char *name, int argc, char *argv[]);
int cmd_debug(const char *name, int argc, char *argv[]);
int cmd_devmem(const char *name, int argc, char *argv[]);
int cmd_read(const char *name, int argc, char *argv[]);
int cmd_write(const char *name, int argc, char *argv[]);
int cmd_replace(const char *name, int argc, char *argv[]);
Expand Down

0 comments on commit c78889f

Please sign in to comment.