File: mem.c

package info (click to toggle)
pdbg 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,212 kB
  • sloc: ansic: 21,934; cpp: 3,363; sh: 3,343; makefile: 314; asm: 11
file content (232 lines) | stat: -rw-r--r-- 5,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>

#include <libpdbg.h>

#include "main.h"
#include "progress.h"
#include "optcmd.h"
#include "parsers.h"
#include "util.h"
#include "path.h"

#define PR_ERROR(x, args...) \
	pdbg_log(PDBG_ERROR, x, ##args)

#define PUTMEM_BUF_SIZE 1024

struct mem_flags {
	bool ci;
	bool raw;
};

struct mem_io_flags {
	bool raw;
};

#define MEM_CI_FLAG ("--ci", ci, parse_flag_noarg, false)
#define MEM_RAW_FLAG ("--raw", raw, parse_flag_noarg, false)

#define BLOCK_SIZE (parse_number8_pow2, NULL)

static uint8_t *read_stdin(size_t *size)
{
	uint8_t *buf = NULL;
	size_t allocated = 0;
	size_t buflen = 0;
	ssize_t n;

	while (1) {
		if (allocated == buflen) {
			uint8_t *ptr;

			ptr = realloc(buf, allocated + PUTMEM_BUF_SIZE);
			if (!ptr) {
				free(buf);
				return NULL;
			}
			buf = ptr;
			allocated += PUTMEM_BUF_SIZE;
		}

		n = read(STDIN_FILENO, buf + buflen, allocated - buflen);
		if (n <= 0)
			break;

		buflen += n;
	}

	*size = buflen;
	return buf;
}

static int _getmem(const char *mem_prefix, uint64_t addr, uint64_t size, uint8_t block_size, bool ci, bool raw)
{
	struct pdbg_target *target;
	uint8_t *buf;
	int rc, count = 0;

	if (size == 0) {
		PR_ERROR("Size must be > 0\n");
		return 1;
	}

	buf = malloc(size);
	assert(buf);

	for_each_path_target_class("pib", target) {
		char mem_path[128];
		struct pdbg_target *mem;

		sprintf(mem_path, "/%s%u", mem_prefix, pdbg_target_index(target));

		mem = pdbg_target_from_path(NULL, mem_path);
		if (!mem)
			continue;

		if (pdbg_target_probe(mem) != PDBG_TARGET_ENABLED)
			continue;

		pdbg_set_progress_tick(progress_tick);
		progress_init();
		rc = mem_read(mem, addr, buf, size, block_size, ci);
		progress_end();
		if (rc) {
			PR_ERROR("Unable to read memory from %s\n",
				 pdbg_target_path(mem));
			continue;
		}

		count++;
		break;
	}

	if (count > 0) {
		if (raw) {
			if (write(STDOUT_FILENO, buf, size) < 0)
				PR_ERROR("Unable to write stdout.\n");
		} else {
			hexdump(addr, buf, size, 1);
		}
	}

	free(buf);
	return count;
}

static int getmem(uint64_t addr, uint64_t size, struct mem_flags flags)
{
	if (flags.ci)
		return _getmem("mem", addr, size, 8, true, flags.raw);
	else
		return _getmem("mem", addr, size, 0, false, flags.raw);
}
OPTCMD_DEFINE_CMD_WITH_FLAGS(getmem, getmem, (ADDRESS, DATA),
			     mem_flags, (MEM_CI_FLAG, MEM_RAW_FLAG));

static int getmempba(uint64_t addr, uint64_t size, struct mem_flags flags)
{
	if (flags.ci)
		return _getmem("mempba", addr, size, 0, true, flags.raw);
	else
		return _getmem("mempba", addr, size, 0, false, flags.raw);
}
OPTCMD_DEFINE_CMD_WITH_FLAGS(getmempba, getmempba, (ADDRESS, DATA),
			     mem_flags, (MEM_CI_FLAG, MEM_RAW_FLAG));

static int getmemio(uint64_t addr, uint64_t size, uint8_t block_size, struct mem_io_flags flags)
{
	return _getmem("mem", addr, size, block_size, true, flags.raw);
}
OPTCMD_DEFINE_CMD_WITH_FLAGS(getmemio, getmemio, (ADDRESS, DATA, BLOCK_SIZE),
			     mem_io_flags, (MEM_RAW_FLAG));

static int _putmem(const char *mem_prefix, uint64_t addr, uint8_t block_size, bool ci)
{
	uint8_t *buf;
	size_t buflen = 0;
	int rc, count = 0;
	struct pdbg_target *target;

	buf = read_stdin(&buflen);
	assert(buf);

	for_each_path_target_class("pib", target) {
		char mem_path[128];
		struct pdbg_target *mem;

		sprintf(mem_path, "/%s%u", mem_prefix, pdbg_target_index(target));

		mem = pdbg_target_from_path(NULL, mem_path);
		if (!mem)
			continue;

		if (pdbg_target_probe(mem) != PDBG_TARGET_ENABLED)
			continue;

		pdbg_set_progress_tick(progress_tick);
		progress_init();
		rc = mem_write(mem, addr, buf, buflen, block_size, ci);
		progress_end();
		if (rc) {
			printf("Unable to write memory using %s\n",
			       pdbg_target_path(mem));
			continue;
		}

		count++;
		break;
	}

	if (count > 0)
		printf("Wrote %zu bytes starting at 0x%016" PRIx64 "\n", buflen, addr);

	free(buf);
	return count;
}

static int putmem(uint64_t addr, struct mem_flags flags)
{
	if (flags.ci)
		return _putmem("mem", addr, 8, true);
	else
		return _putmem("mem", addr, 0, false);
}
OPTCMD_DEFINE_CMD_WITH_FLAGS(putmem, putmem, (ADDRESS), mem_flags, (MEM_CI_FLAG));

static int putmempba(uint64_t addr, struct mem_flags flags)
{
	if (flags.ci)
		return _putmem("mempba", addr, 0, true);
	else
		return _putmem("mempba", addr, 0, false);
}
OPTCMD_DEFINE_CMD_WITH_FLAGS(putmempba, putmempba, (ADDRESS), mem_flags, (MEM_CI_FLAG));

static int putmemio(uint64_t addr, uint8_t block_size)
{
	return _putmem("mem", addr, block_size, true);
}
OPTCMD_DEFINE_CMD_WITH_ARGS(putmemio, putmemio, (ADDRESS, BLOCK_SIZE));