summaryrefslogtreecommitdiff
path: root/common/host_command_memory_dump.c
blob: f8d06c5762c20501bdce04a3bf6af0483483e9ad (plain)
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
/* Copyright 2023 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "console.h"
#include "ec_commands.h"
#include "host_command.h"
#include "host_command_memory_dump.h"
#include "string.h"
#include "task.h"

#define MAX_DUMP_ENTRIES 64

#ifndef CONFIG_ZEPHYR
/* TODO(b/271883902): Add memory dump command support for CrOS EC */
#error "Memory Dump commands are only supported on Zephyr EC."
#endif

struct memory_dump_entry {
	uint32_t address;
	uint32_t size;
};

static struct memory_dump_entry entries[MAX_DUMP_ENTRIES];
static uint16_t memory_dump_entry_count;
K_MUTEX_DEFINE(memory_dump_mutex);

int register_memory_dump(uint32_t address, uint32_t size)
{
	mutex_lock(&memory_dump_mutex);

	if (memory_dump_entry_count >= MAX_DUMP_ENTRIES) {
		mutex_unlock(&memory_dump_mutex);
		ccprintf("ERROR: Memory dump count exceeds max\n");
		return EC_ERROR_OVERFLOW;
	}

	entries[memory_dump_entry_count].address = address;
	entries[memory_dump_entry_count].size = size;

	memory_dump_entry_count += 1;

	mutex_unlock(&memory_dump_mutex);

	return EC_SUCCESS;
}

int clear_memory_dump(void)
{
	mutex_lock(&memory_dump_mutex);
	memset(entries, 0, sizeof(entries));
	memory_dump_entry_count = 0;
	mutex_unlock(&memory_dump_mutex);

	return EC_SUCCESS;
}

static enum ec_status
get_memory_dump_metadata(struct host_cmd_handler_args *args)
{
	struct ec_response_memory_dump_get_metadata *r = args->response;

	mutex_lock(&memory_dump_mutex);

	r->memory_dump_entry_count = memory_dump_entry_count;
	r->memory_dump_total_size = 0;
	for (int i = 0; i < memory_dump_entry_count; i++) {
		r->memory_dump_total_size += entries[i].size;
	}

	mutex_unlock(&memory_dump_mutex);

	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_MEMORY_DUMP_GET_METADATA, get_memory_dump_metadata,
		     EC_VER_MASK(0));

static enum ec_status
memory_dump_get_entry_info(struct host_cmd_handler_args *args)
{
	const struct ec_params_memory_dump_get_entry_info *p = args->params;
	struct ec_response_memory_dump_get_entry_info *r = args->response;

	mutex_lock(&memory_dump_mutex);

	if (p->memory_dump_entry_index >= memory_dump_entry_count) {
		return EC_RES_INVALID_PARAM;
	}

	r->address = entries[p->memory_dump_entry_index].address;
	r->size = entries[p->memory_dump_entry_index].size;

	mutex_unlock(&memory_dump_mutex);

	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_MEMORY_DUMP_GET_ENTRY_INFO,
		     memory_dump_get_entry_info, EC_VER_MASK(0));

static enum ec_status read_memory_dump(struct host_cmd_handler_args *args)
{
	const struct ec_params_memory_dump_read_memory *p = args->params;
	void *r = args->response;
	struct memory_dump_entry entry;

	mutex_lock(&memory_dump_mutex);

	if (p->memory_dump_entry_index >= memory_dump_entry_count) {
		return EC_RES_INVALID_PARAM;
	}

	entry = entries[p->memory_dump_entry_index];

	if (p->address < entry.address ||
	    p->address + p->size > entry.address + entry.size) {
		return EC_RES_INVALID_PARAM;
	}

	/* Must leave room for ec_host_response header */
	args->response_size = MIN(
		p->size, args->response_max - sizeof(struct ec_host_response));

	memcpy(r, (void *)p->address, args->response_size);

	mutex_unlock(&memory_dump_mutex);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_MEMORY_DUMP_READ_MEMORY, read_memory_dump,
		     EC_VER_MASK(0));