blob: aeb32b13afbcc1a1c865bab5d51587cf835b3452 [file] [log] [blame]
Denis Vlasenkoc94fa562008-10-26 11:08:14 +00001/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02002 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoc94fa562008-10-26 11:08:14 +00003 * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
4 * Copyright (C) 2008, BusyBox Team. -solar 4/26/08
5 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01006//config:config DEVMEM
7//config: bool "devmem"
8//config: default y
9//config: help
10//config: devmem is a small program that reads and writes from physical
11//config: memory using /dev/mem.
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000012
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010013//applet:IF_DEVMEM(APPLET(devmem, BB_DIR_SBIN, BB_SUID_DROP))
14
15//kbuild:lib-$(CONFIG_DEVMEM) += devmem.o
16
Pere Orga5bc8c002011-04-11 03:29:49 +020017//usage:#define devmem_trivial_usage
18//usage: "ADDRESS [WIDTH [VALUE]]"
19//usage:#define devmem_full_usage "\n\n"
20//usage: "Read/write from physical address\n"
21//usage: "\n ADDRESS Address to act upon"
22//usage: "\n WIDTH Width (8/16/...)"
23//usage: "\n VALUE Data to be written"
24
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000025#include "libbb.h"
26
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000027int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
28int devmem_main(int argc UNUSED_PARAM, char **argv)
29{
30 void *map_base, *virt_addr;
31 uint64_t read_result;
32 uint64_t writeval = writeval; /* for compiler */
33 off_t target;
Denys Vlasenko351ef712010-04-14 13:37:25 -070034 unsigned page_size, mapped_size, offset_in_page;
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000035 int fd;
Denys Vlasenko351ef712010-04-14 13:37:25 -070036 unsigned width = 8 * sizeof(int);
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000037
38 /* devmem ADDRESS [WIDTH [VALUE]] */
Denis Vlasenko04324652008-10-26 17:26:55 +000039// TODO: options?
40// -r: read and output only the value in hex, with 0x prefix
41// -w: write only, no reads before or after, and no output
42// or make this behavior default?
43// Let's try this and see how users react.
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000044
45 /* ADDRESS */
46 if (!argv[1])
47 bb_show_usage();
48 errno = 0;
49 target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */
50
51 /* WIDTH */
52 if (argv[2]) {
53 if (isdigit(argv[2][0]) || argv[2][1])
54 width = xatou(argv[2]);
55 else {
56 static const char bhwl[] ALIGN1 = "bhwl";
57 static const uint8_t sizes[] ALIGN1 = {
58 8 * sizeof(char),
59 8 * sizeof(short),
60 8 * sizeof(int),
61 8 * sizeof(long),
62 0 /* bad */
63 };
64 width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
65 width = sizes[width];
66 }
67 /* VALUE */
68 if (argv[3])
69 writeval = bb_strtoull(argv[3], NULL, 0);
70 } else { /* argv[2] == NULL */
Denys Vlasenko351ef712010-04-14 13:37:25 -070071 /* make argv[3] to be a valid thing to fetch */
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000072 argv--;
73 }
74 if (errno)
Denys Vlasenko351ef712010-04-14 13:37:25 -070075 bb_show_usage(); /* one of bb_strtouXX failed */
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000076
77 fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
Denys Vlasenko351ef712010-04-14 13:37:25 -070078 mapped_size = page_size = getpagesize();
79 offset_in_page = (unsigned)target & (page_size - 1);
80 if (offset_in_page + width > page_size) {
81 /* This access spans pages.
82 * Must map two pages to make it possible: */
83 mapped_size *= 2;
84 }
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000085 map_base = mmap(NULL,
Denys Vlasenko351ef712010-04-14 13:37:25 -070086 mapped_size,
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000087 argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
88 MAP_SHARED,
89 fd,
Denis Vlasenko04324652008-10-26 17:26:55 +000090 target & ~(off_t)(page_size - 1));
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000091 if (map_base == MAP_FAILED)
92 bb_perror_msg_and_die("mmap");
93
94// printf("Memory mapped at address %p.\n", map_base);
95
Denys Vlasenko351ef712010-04-14 13:37:25 -070096 virt_addr = (char*)map_base + offset_in_page;
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000097
Denis Vlasenko04324652008-10-26 17:26:55 +000098 if (!argv[3]) {
Denis Vlasenkoc94fa562008-10-26 11:08:14 +000099 switch (width) {
100 case 8:
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000101 read_result = *(volatile uint8_t*)virt_addr;
102 break;
103 case 16:
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000104 read_result = *(volatile uint16_t*)virt_addr;
105 break;
106 case 32:
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000107 read_result = *(volatile uint32_t*)virt_addr;
108 break;
109 case 64:
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000110 read_result = *(volatile uint64_t*)virt_addr;
111 break;
Denis Vlasenko04324652008-10-26 17:26:55 +0000112 default:
113 bb_error_msg_and_die("bad width");
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000114 }
Denis Vlasenko04324652008-10-26 17:26:55 +0000115// printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n",
116// target, virt_addr,
117// (unsigned long long)read_result);
118 /* Zero-padded output shows the width of access just done */
119 printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
120 } else {
121 switch (width) {
122 case 8:
123 *(volatile uint8_t*)virt_addr = writeval;
124// read_result = *(volatile uint8_t*)virt_addr;
125 break;
126 case 16:
127 *(volatile uint16_t*)virt_addr = writeval;
128// read_result = *(volatile uint16_t*)virt_addr;
129 break;
130 case 32:
131 *(volatile uint32_t*)virt_addr = writeval;
132// read_result = *(volatile uint32_t*)virt_addr;
133 break;
134 case 64:
135 *(volatile uint64_t*)virt_addr = writeval;
136// read_result = *(volatile uint64_t*)virt_addr;
137 break;
138 default:
139 bb_error_msg_and_die("bad width");
140 }
141// printf("Written 0x%llX; readback 0x%llX\n",
142// (unsigned long long)writeval,
143// (unsigned long long)read_result);
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000144 }
145
146 if (ENABLE_FEATURE_CLEAN_UP) {
Denys Vlasenko351ef712010-04-14 13:37:25 -0700147 if (munmap(map_base, mapped_size) == -1)
Denis Vlasenkoc94fa562008-10-26 11:08:14 +0000148 bb_perror_msg_and_die("munmap");
149 close(fd);
150 }
151
152 return EXIT_SUCCESS;
153}