aboutsummaryrefslogtreecommitdiff
path: root/plat/common/fwu_nvm_accessors.c
blob: 5b32151d465e3f50c26f865d2d4a0a0356fe48ce (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
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
/*
 * Copyright (c) 2018, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <debug.h>
#include <firmware_image_package.h>
#include <fwu_nvm.h>
#include <io_fip.h>
#include <io_storage.h>
#include <platform.h>
#include <platform_def.h>
#include <status.h>
#include <string.h>
#include <uuid_utils.h>


STATUS fwu_nvm_write(unsigned long long offset, const void *buffer, size_t size)
{
	uintptr_t nvm_handle;
	int ret;
	size_t length_write;

	if (offset + size > FLASH_SIZE)
		return STATUS_OUT_OF_RESOURCES;

	/* Obtain a handle to the NVM by querying the platfom layer */
	plat_get_nvm_handle(&nvm_handle);

	/* Seek to the given offset. */
	ret = io_seek(nvm_handle, IO_SEEK_SET, offset);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;

	/* Write to the given offset. */
	ret = io_write(nvm_handle, (const uintptr_t)buffer,
		size, &length_write);
	if ((ret != IO_SUCCESS) || (size != length_write))
		return STATUS_FAIL;

	return STATUS_SUCCESS;
}

STATUS fwu_nvm_read(unsigned long long offset, void *buffer, size_t size)
{
	uintptr_t nvm_handle;
	int ret;
	size_t length_read;

	if (offset + size > FLASH_SIZE)
		return STATUS_OUT_OF_RESOURCES;

	/* Obtain a handle to the NVM by querying the platform layer */
	plat_get_nvm_handle(&nvm_handle);

	/* Seek to the given offset. */
	ret = io_seek(nvm_handle, IO_SEEK_SET, offset);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;

	/* Read from the given offset. */
	ret = io_read(nvm_handle, (const uintptr_t)buffer,
		size, &length_read);
	if ((ret != IO_SUCCESS) || (size != length_read))
		return STATUS_FAIL;

	return STATUS_SUCCESS;
}


STATUS fwu_update_fip(unsigned long fip_addr)
{
	uintptr_t nvm_handle;
	int ret;
	size_t bytes;
	int fip_size;
	unsigned int fip_read;
	fip_toc_header_t *toc_header;
	fip_toc_entry_t *toc_entry;

	/* Obtain a handle to the NVM by querying the platform layer */
	plat_get_nvm_handle(&nvm_handle);

#if FWU_BL_TEST
	/* Read the address of backup fip.bin for Firmware Update. */
	ret = io_seek(nvm_handle, IO_SEEK_SET,
			FWU_TFTF_TESTCASE_BUFFER_OFFSET);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;

	ret = io_read(nvm_handle, (const uintptr_t)&fip_addr,
			sizeof(bytes), &bytes);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;
#endif /* FWU_BL_TEST */

	/* If the new FIP address is 0 it means no update. */
	if (fip_addr == 0)
		return STATUS_SUCCESS;

	/* Set the ToC Header at the base of the buffer */
	toc_header = (fip_toc_header_t *)fip_addr;

	/* Check if this FIP is Valid */
	if ((toc_header->name != TOC_HEADER_NAME) ||
		(toc_header->serial_number == 0))
		return STATUS_LOAD_ERROR;

	/* Get to the last NULL TOC entry */
	toc_entry = (fip_toc_entry_t *)(toc_header + 1);
	while (!is_uuid_null(&toc_entry->uuid))
		toc_entry++;

	/* get the total size of this FIP */
	fip_size = (int)toc_entry->offset_address;

	/* Copy the new FIP in DDR. */
	memcpy((void *)FIP_IMAGE_TMP_DDR_ADDRESS, (void *)fip_addr, fip_size);

	/* Update the FIP */
	ret = io_seek(nvm_handle, IO_SEEK_SET, 0);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;

	ret = io_write(nvm_handle, (const uintptr_t)FIP_IMAGE_TMP_DDR_ADDRESS,
			fip_size, &bytes);
	if ((ret != IO_SUCCESS) || fip_size != bytes)
		return STATUS_LOAD_ERROR;

	/* Read the TOC header after update. */
	ret = io_seek(nvm_handle, IO_SEEK_SET, 0);
	if (ret != IO_SUCCESS)
		return STATUS_LOAD_ERROR;

	ret = io_read(nvm_handle, (const uintptr_t)&fip_read,
		sizeof(bytes), &bytes);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;

	/* Check if this FIP is Valid */
	if (fip_read != TOC_HEADER_NAME)
		return STATUS_LOAD_ERROR;

#if FWU_BL_TEST
	unsigned int done_flag = FIP_IMAGE_UPDATE_DONE_FLAG;
	/* Update the TFTF test case buffer with DONE flag */
	ret = io_seek(nvm_handle, IO_SEEK_SET,
			FWU_TFTF_TESTCASE_BUFFER_OFFSET);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;

	ret = io_write(nvm_handle, (const uintptr_t)&done_flag,
			4, &bytes);
	if (ret != IO_SUCCESS)
		return STATUS_FAIL;
#endif /* FWU_BL_TEST */

	INFO("FWU Image update success\n");

	return STATUS_SUCCESS;
}