Skip to content

Commit fe2075d

Browse files
henrikbrixandersennashif
authored andcommitted
shell: modules: promote edac mem shell subcommand to root shell command
Promote the "edac mem" shell subcommand to a generic "devmem" root shell command. This command is useful for poking around registers and memory outside of the EDAC drivers. Signed-off-by: Henrik Brix Andersen <[email protected]>
1 parent c7397f7 commit fe2075d

File tree

4 files changed

+118
-97
lines changed

4 files changed

+118
-97
lines changed

drivers/edac/shell.c

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -457,107 +457,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(sub_info_cmds,
457457
SHELL_SUBCMD_SET_END /* Array terminated */
458458
);
459459

460-
/* Memory operation commands */
461-
462-
static int memory_read(const struct shell *sh, mem_addr_t addr, uint8_t width)
463-
{
464-
uint64_t value;
465-
int err = 0;
466-
467-
switch (width) {
468-
case 8:
469-
value = sys_read8(addr);
470-
break;
471-
case 16:
472-
value = sys_read16(addr);
473-
break;
474-
case 32:
475-
value = sys_read32(addr);
476-
break;
477-
default:
478-
shell_fprintf(sh, SHELL_NORMAL, "Incorrect data width\n");
479-
err = -EINVAL;
480-
break;
481-
}
482-
483-
if (err == 0) {
484-
shell_fprintf(sh, SHELL_NORMAL, "Read value 0x%lx\n", value);
485-
}
486-
487-
return err;
488-
}
489-
490-
static int memory_write(const struct shell *sh, mem_addr_t addr,
491-
uint8_t width, uint64_t value)
492-
{
493-
int err = 0;
494-
495-
switch (width) {
496-
case 8:
497-
sys_write8(value, addr);
498-
break;
499-
case 16:
500-
sys_write16(value, addr);
501-
break;
502-
case 32:
503-
sys_write32(value, addr);
504-
break;
505-
default:
506-
shell_fprintf(sh, SHELL_NORMAL, "Incorrect data width\n");
507-
err = -EINVAL;
508-
break;
509-
}
510-
511-
return err;
512-
}
513-
514-
/* The syntax of the command is similar to busybox's devmem */
515-
static int cmd_mem(const struct shell *shell, size_t argc, char **argv)
516-
{
517-
uint64_t value = 0;
518-
mem_addr_t phys_addr, addr;
519-
uint8_t width;
520-
521-
if (argc < 2 || argc > 4) {
522-
return -EINVAL;
523-
}
524-
525-
phys_addr = strtol(argv[1], NULL, 16);
526-
device_map((mm_reg_t *)&addr, phys_addr, 0x100, K_MEM_CACHE_NONE);
527-
528-
shell_fprintf(shell, SHELL_NORMAL, "Mapped 0x%lx to 0x%lx\n",
529-
phys_addr, addr);
530-
531-
if (argc < 3) {
532-
width = 32;
533-
} else {
534-
width = strtol(argv[2], NULL, 10);
535-
}
536-
537-
shell_fprintf(shell, SHELL_NORMAL, "Using data width %d\n", width);
538-
539-
if (argc <= 3) {
540-
return memory_read(shell, addr, width);
541-
}
542-
543-
/* If there are more then 3 arguments, that means we are going to write
544-
* this value at the address provided
545-
*/
546-
547-
value = strtol(argv[3], NULL, 16);
548-
549-
shell_fprintf(shell, SHELL_NORMAL, "Writing value 0x%lx\n", value);
550-
551-
return memory_write(shell, addr, width, value);
552-
}
553-
554460
SHELL_STATIC_SUBCMD_SET_CREATE(sub_edac_cmds,
555461
SHELL_CMD(info, &sub_info_cmds,
556462
"Show EDAC information\n"
557463
"edac info <subcommands>", cmd_edac_info),
558-
SHELL_CMD(mem, NULL,
559-
"Read / Write physical memory\n"
560-
"edac mem address [width [value]]", cmd_mem),
561464
#if defined(CONFIG_EDAC_ERROR_INJECT)
562465
/* This does not work with SHELL_COND_CMD */
563466
SHELL_CMD(inject, &sub_inject_cmds,

subsys/shell/modules/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ zephyr_sources_ifdef(
1212
CONFIG_DATE_SHELL
1313
date_service.c
1414
)
15+
zephyr_sources_ifdef(
16+
CONFIG_DEVMEM_SHELL
17+
devmem_service.c
18+
)

subsys/shell/modules/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ config DATE_SHELL
3838
default y if !SHELL_MINIMAL
3939
help
4040
This shell provides access to date and time based on Unix time.
41+
42+
config DEVMEM_SHELL
43+
bool "Enable devmem shell"
44+
default y if !SHELL_MINIMAL
45+
help
46+
This shell command provides read/write access to physical memory.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2020 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <device.h>
8+
#include <shell/shell.h>
9+
#include <stdlib.h>
10+
11+
static int memory_read(const struct shell *sh, mem_addr_t addr, uint8_t width)
12+
{
13+
uint64_t value;
14+
int err = 0;
15+
16+
switch (width) {
17+
case 8:
18+
value = sys_read8(addr);
19+
break;
20+
case 16:
21+
value = sys_read16(addr);
22+
break;
23+
case 32:
24+
value = sys_read32(addr);
25+
break;
26+
default:
27+
shell_fprintf(sh, SHELL_NORMAL, "Incorrect data width\n");
28+
err = -EINVAL;
29+
break;
30+
}
31+
32+
if (err == 0) {
33+
shell_fprintf(sh, SHELL_NORMAL, "Read value 0x%lx\n", value);
34+
}
35+
36+
return err;
37+
}
38+
39+
static int memory_write(const struct shell *sh, mem_addr_t addr,
40+
uint8_t width, uint64_t value)
41+
{
42+
int err = 0;
43+
44+
switch (width) {
45+
case 8:
46+
sys_write8(value, addr);
47+
break;
48+
case 16:
49+
sys_write16(value, addr);
50+
break;
51+
case 32:
52+
sys_write32(value, addr);
53+
break;
54+
default:
55+
shell_fprintf(sh, SHELL_NORMAL, "Incorrect data width\n");
56+
err = -EINVAL;
57+
break;
58+
}
59+
60+
return err;
61+
}
62+
63+
/* The syntax of the command is similar to busybox's devmem */
64+
static int cmd_devmem(const struct shell *sh, size_t argc, char **argv)
65+
{
66+
mem_addr_t phys_addr, addr;
67+
uint32_t value = 0;
68+
uint8_t width;
69+
70+
if (argc < 2 || argc > 4) {
71+
return -EINVAL;
72+
}
73+
74+
phys_addr = strtol(argv[1], NULL, 16);
75+
76+
#if defined(CONFIG_MMU) || defined(CONFIG_PCIE)
77+
device_map((mm_reg_t *)&addr, phys_addr, 0x100, K_MEM_CACHE_NONE);
78+
79+
shell_print(sh, "Mapped 0x%lx to 0x%lx\n", phys_addr, addr);
80+
#else
81+
addr = phys_addr;
82+
#endif /* defined(CONFIG_MMU) || defined(CONFIG_PCIE) */
83+
84+
if (argc < 3) {
85+
width = 32;
86+
} else {
87+
width = strtol(argv[2], NULL, 10);
88+
}
89+
90+
shell_fprintf(sh, SHELL_NORMAL, "Using data width %d\n", width);
91+
92+
if (argc <= 3) {
93+
return memory_read(sh, addr, width);
94+
}
95+
96+
/* If there are more then 3 arguments, that means we are going to write
97+
* this value at the address provided
98+
*/
99+
100+
value = strtol(argv[3], NULL, 16);
101+
102+
shell_fprintf(sh, SHELL_NORMAL, "Writing value 0x%lx\n", value);
103+
104+
return memory_write(sh, addr, width, value);
105+
}
106+
107+
SHELL_CMD_REGISTER(devmem, NULL, "Read/write physical memory\""
108+
"devmem address [width [value]]", cmd_devmem);

0 commit comments

Comments
 (0)