summaryrefslogtreecommitdiff
path: root/common/pstore_commands.c
blob: 489e5549419165ba36e377cad43123baa074ef31 (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
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Persistent storage commands for Chrome EC */

#include "board.h"
#include "eeprom.h"
#include "host_command.h"
#include "uart.h"
#include "util.h"

enum lpc_status pstore_command_get_info(uint8_t *data)
{
	struct lpc_response_pstore_info *r =
			(struct lpc_response_pstore_info *)data;

	uart_printf("ee block size=%d, count=%d\n",
		    eeprom_get_block_size(), eeprom_get_block_count());

	ASSERT(EEPROM_BLOCK_START_PSTORE + EEPROM_BLOCK_COUNT_PSTORE <=
	       eeprom_get_block_count());

	r->pstore_size = EEPROM_BLOCK_COUNT_PSTORE * eeprom_get_block_size();
	r->access_size = sizeof(uint32_t);
	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_PSTORE_INFO, pstore_command_get_info);


enum lpc_status pstore_command_read(uint8_t *data)
{
	struct lpc_params_pstore_read *p =
			(struct lpc_params_pstore_read *)data;
	struct lpc_response_pstore_read *r =
			(struct lpc_response_pstore_read *)data;
	char *dest = r->data;
	int block_size = eeprom_get_block_size();
	int block = p->offset / block_size + EEPROM_BLOCK_COUNT_PSTORE;
	int offset = p->offset % block_size;
	int bytes_left = p->size;

	if (p->size > sizeof(r->data))
		return EC_LPC_RESULT_ERROR;

	while (bytes_left) {
		/* Read what we can from the current block */
		int bytes_this = MIN(bytes_left, block_size - offset);

		if (block >=
		    EEPROM_BLOCK_START_PSTORE + EEPROM_BLOCK_COUNT_PSTORE)
			return EC_LPC_RESULT_ERROR;

		if (eeprom_read(block, offset, bytes_this, dest))
			return EC_LPC_RESULT_ERROR;

		/* Continue to the next block if necessary */
		offset = 0;
		block++;
		bytes_left -= bytes_this;
		dest += bytes_this;
	}

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_PSTORE_READ, pstore_command_read);


enum lpc_status pstore_command_write(uint8_t *data)
{
	struct lpc_params_pstore_write *p =
			(struct lpc_params_pstore_write *)data;

	const char *src = p->data;
	int block_size = eeprom_get_block_size();
	int block = p->offset / block_size + EEPROM_BLOCK_COUNT_PSTORE;
	int offset = p->offset % block_size;
	int bytes_left = p->size;

	if (p->size > sizeof(p->data))
		return EC_LPC_RESULT_ERROR;

	while (bytes_left) {
		/* Write what we can to the current block */
		int bytes_this = MIN(bytes_left, block_size - offset);

		if (block >=
		    EEPROM_BLOCK_START_PSTORE + EEPROM_BLOCK_COUNT_PSTORE)
			return EC_LPC_RESULT_ERROR;

		if (eeprom_write(block, offset, bytes_this, src))
			return EC_LPC_RESULT_ERROR;

		/* Continue to the next block if necessary */
		offset = 0;
		block++;
		bytes_left -= bytes_this;
		src += bytes_this;
	}

	return EC_LPC_RESULT_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_LPC_COMMAND_PSTORE_WRITE, pstore_command_write);