xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision 6eb75a1a2febdf089c2a432dc46c94a0da16520d)
1561cd33eSHarry Liebel /*
25c380888SScott Branden  * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
3561cd33eSHarry Liebel  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5561cd33eSHarry Liebel  */
6561cd33eSHarry Liebel 
7561cd33eSHarry Liebel #include <assert.h>
897043ac9SDan Handley #include <errno.h>
997043ac9SDan Handley #include <stdint.h>
1097043ac9SDan Handley #include <string.h>
1109d40e0eSAntonio Nino Diaz 
1209d40e0eSAntonio Nino Diaz #include <platform_def.h>
1309d40e0eSAntonio Nino Diaz 
1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1509d40e0eSAntonio Nino Diaz #include <common/debug.h>
1609d40e0eSAntonio Nino Diaz #include <drivers/io/io_driver.h>
1709d40e0eSAntonio Nino Diaz #include <drivers/io/io_fip.h>
1809d40e0eSAntonio Nino Diaz #include <drivers/io/io_storage.h>
1909d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2009d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2109d40e0eSAntonio Nino Diaz #include <tools_share/firmware_image_package.h>
2209d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h>
23561cd33eSHarry Liebel 
24d4d598e9SRuchika Gupta #ifndef MAX_FIP_DEVICES
25d4d598e9SRuchika Gupta #define MAX_FIP_DEVICES		1
26d4d598e9SRuchika Gupta #endif
27d4d598e9SRuchika Gupta 
28561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/
29561cd33eSHarry Liebel #define PRINT_UUID2(x)								\
30561cd33eSHarry Liebel 	"%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",	\
31561cd33eSHarry Liebel 		x.time_low, x.time_mid, x.time_hi_and_version,			\
32561cd33eSHarry Liebel 		x.clock_seq_hi_and_reserved, x.clock_seq_low,			\
33561cd33eSHarry Liebel 		x.node[0], x.node[1], x.node[2], x.node[3],			\
34561cd33eSHarry Liebel 		x.node[4], x.node[5]
35561cd33eSHarry Liebel 
36561cd33eSHarry Liebel typedef struct {
37561cd33eSHarry Liebel 	unsigned int file_pos;
38fb037bfbSDan Handley 	fip_toc_entry_t entry;
39fb037bfbSDan Handley } file_state_t;
40561cd33eSHarry Liebel 
41d4d598e9SRuchika Gupta /*
42d4d598e9SRuchika Gupta  * Maintain dev_spec per FIP Device
43d4d598e9SRuchika Gupta  * TODO - Add backend handles and file state
44d4d598e9SRuchika Gupta  * per FIP device here once backends like io_memmap
45d4d598e9SRuchika Gupta  * can support multiple open files
46d4d598e9SRuchika Gupta  */
47d4d598e9SRuchika Gupta typedef struct {
48d4d598e9SRuchika Gupta 	uintptr_t dev_spec;
495c380888SScott Branden 	uint16_t plat_toc_flag;
50d4d598e9SRuchika Gupta } fip_dev_state_t;
51d4d598e9SRuchika Gupta 
52609e053cSAmbroise Vincent static const uuid_t uuid_null;
53d4d598e9SRuchika Gupta /*
54d4d598e9SRuchika Gupta  * Only one file can be open across all FIP device
55d4d598e9SRuchika Gupta  * as backends like io_memmap don't support
56d4d598e9SRuchika Gupta  * multiple open files. The file state and
57d4d598e9SRuchika Gupta  * backend handle should be maintained per FIP device
58d4d598e9SRuchika Gupta  * if the same support is available in the backend
59d4d598e9SRuchika Gupta  */
60fb037bfbSDan Handley static file_state_t current_file = {0};
61625de1d4SDan Handley static uintptr_t backend_dev_handle;
62625de1d4SDan Handley static uintptr_t backend_image_spec;
63561cd33eSHarry Liebel 
64d4d598e9SRuchika Gupta static fip_dev_state_t state_pool[MAX_FIP_DEVICES];
65d4d598e9SRuchika Gupta static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES];
66d4d598e9SRuchika Gupta 
67d4d598e9SRuchika Gupta /* Track number of allocated fip devices */
68d4d598e9SRuchika Gupta static unsigned int fip_dev_count;
69561cd33eSHarry Liebel 
70561cd33eSHarry Liebel /* Firmware Image Package driver functions */
71625de1d4SDan Handley static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
72625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
73fb037bfbSDan Handley 			  io_entity_t *entity);
74fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length);
75625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
76561cd33eSHarry Liebel 			  size_t *length_read);
77fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity);
78625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
79fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info);
80561cd33eSHarry Liebel 
81561cd33eSHarry Liebel 
82561cd33eSHarry Liebel /* Return 0 for equal uuids. */
83561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
84561cd33eSHarry Liebel {
85561cd33eSHarry Liebel 	return memcmp(uuid1, uuid2, sizeof(uuid_t));
86561cd33eSHarry Liebel }
87561cd33eSHarry Liebel 
88561cd33eSHarry Liebel 
89fb037bfbSDan Handley static inline int is_valid_header(fip_toc_header_t *header)
90561cd33eSHarry Liebel {
91561cd33eSHarry Liebel 	if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
92561cd33eSHarry Liebel 		return 1;
93561cd33eSHarry Liebel 	} else {
94561cd33eSHarry Liebel 		return 0;
95561cd33eSHarry Liebel 	}
96561cd33eSHarry Liebel }
97561cd33eSHarry Liebel 
98561cd33eSHarry Liebel 
99561cd33eSHarry Liebel /* Identify the device type as a virtual driver */
1007fabe1a8SRoberto Vargas static io_type_t device_type_fip(void)
101561cd33eSHarry Liebel {
102561cd33eSHarry Liebel 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
103561cd33eSHarry Liebel }
104561cd33eSHarry Liebel 
105561cd33eSHarry Liebel 
106625de1d4SDan Handley static const io_dev_connector_t fip_dev_connector = {
107561cd33eSHarry Liebel 	.dev_open = fip_dev_open
108561cd33eSHarry Liebel };
109561cd33eSHarry Liebel 
110561cd33eSHarry Liebel 
111625de1d4SDan Handley static const io_dev_funcs_t fip_dev_funcs = {
112561cd33eSHarry Liebel 	.type = device_type_fip,
113561cd33eSHarry Liebel 	.open = fip_file_open,
114561cd33eSHarry Liebel 	.seek = NULL,
115561cd33eSHarry Liebel 	.size = fip_file_len,
116561cd33eSHarry Liebel 	.read = fip_file_read,
117561cd33eSHarry Liebel 	.write = NULL,
118561cd33eSHarry Liebel 	.close = fip_file_close,
119561cd33eSHarry Liebel 	.dev_init = fip_dev_init,
120561cd33eSHarry Liebel 	.dev_close = fip_dev_close,
121561cd33eSHarry Liebel };
122561cd33eSHarry Liebel 
123d4d598e9SRuchika Gupta /* Locate a file state in the pool, specified by address */
124d4d598e9SRuchika Gupta static int find_first_fip_state(const uintptr_t dev_spec,
125d4d598e9SRuchika Gupta 				  unsigned int *index_out)
126d4d598e9SRuchika Gupta {
127d4d598e9SRuchika Gupta 	int result = -ENOENT;
128d4d598e9SRuchika Gupta 	unsigned int index;
129561cd33eSHarry Liebel 
130d4d598e9SRuchika Gupta 	for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
131d4d598e9SRuchika Gupta 		/* dev_spec is used as identifier since it's unique */
132d4d598e9SRuchika Gupta 		if (state_pool[index].dev_spec == dev_spec) {
133d4d598e9SRuchika Gupta 			result = 0;
134d4d598e9SRuchika Gupta 			*index_out = index;
135d4d598e9SRuchika Gupta 			break;
136d4d598e9SRuchika Gupta 		}
137d4d598e9SRuchika Gupta 	}
138d4d598e9SRuchika Gupta 	return result;
139d4d598e9SRuchika Gupta }
140561cd33eSHarry Liebel 
141561cd33eSHarry Liebel 
142d4d598e9SRuchika Gupta /* Allocate a device info from the pool and return a pointer to it */
143d4d598e9SRuchika Gupta static int allocate_dev_info(io_dev_info_t **dev_info)
144d4d598e9SRuchika Gupta {
145d4d598e9SRuchika Gupta 	int result = -ENOMEM;
146d4d598e9SRuchika Gupta 
147d4d598e9SRuchika Gupta 	assert(dev_info != NULL);
148d4d598e9SRuchika Gupta 
149d4d598e9SRuchika Gupta 	if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) {
150d4d598e9SRuchika Gupta 		unsigned int index = 0;
151d4d598e9SRuchika Gupta 
152d4d598e9SRuchika Gupta 		result = find_first_fip_state(0, &index);
153d4d598e9SRuchika Gupta 		assert(result == 0);
154d4d598e9SRuchika Gupta 		/* initialize dev_info */
155d4d598e9SRuchika Gupta 		dev_info_pool[index].funcs = &fip_dev_funcs;
156d4d598e9SRuchika Gupta 		dev_info_pool[index].info =
157d4d598e9SRuchika Gupta 				(uintptr_t)&state_pool[index];
158d4d598e9SRuchika Gupta 		*dev_info = &dev_info_pool[index];
159d4d598e9SRuchika Gupta 		++fip_dev_count;
160d4d598e9SRuchika Gupta 	}
161d4d598e9SRuchika Gupta 
162d4d598e9SRuchika Gupta 	return result;
163d4d598e9SRuchika Gupta }
164d4d598e9SRuchika Gupta 
165d4d598e9SRuchika Gupta /* Release a device info to the pool */
166d4d598e9SRuchika Gupta static int free_dev_info(io_dev_info_t *dev_info)
167d4d598e9SRuchika Gupta {
168d4d598e9SRuchika Gupta 	int result;
169d4d598e9SRuchika Gupta 	unsigned int index = 0;
170d4d598e9SRuchika Gupta 	fip_dev_state_t *state;
171d4d598e9SRuchika Gupta 
172d4d598e9SRuchika Gupta 	assert(dev_info != NULL);
173d4d598e9SRuchika Gupta 
174d4d598e9SRuchika Gupta 	state = (fip_dev_state_t *)dev_info->info;
175d4d598e9SRuchika Gupta 	result = find_first_fip_state(state->dev_spec, &index);
176d4d598e9SRuchika Gupta 	if (result ==  0) {
177d4d598e9SRuchika Gupta 		/* free if device info is valid */
178d4d598e9SRuchika Gupta 		zeromem(state, sizeof(fip_dev_state_t));
179d4d598e9SRuchika Gupta 		--fip_dev_count;
180d4d598e9SRuchika Gupta 	}
181d4d598e9SRuchika Gupta 
182d4d598e9SRuchika Gupta 	return result;
183d4d598e9SRuchika Gupta }
184d4d598e9SRuchika Gupta 
185d4d598e9SRuchika Gupta /*
186d4d598e9SRuchika Gupta  * Multiple FIP devices can be opened depending on the value of
187d4d598e9SRuchika Gupta  * MAX_FIP_DEVICES. Given that there is only one backend, only a
188d4d598e9SRuchika Gupta  * single file can be open at a time by any FIP device.
189d4d598e9SRuchika Gupta  */
190d4d598e9SRuchika Gupta static int fip_dev_open(const uintptr_t dev_spec,
191fb037bfbSDan Handley 			 io_dev_info_t **dev_info)
192561cd33eSHarry Liebel {
193d4d598e9SRuchika Gupta 	int result;
194d4d598e9SRuchika Gupta 	io_dev_info_t *info;
195d4d598e9SRuchika Gupta 	fip_dev_state_t *state;
196d4d598e9SRuchika Gupta 
197561cd33eSHarry Liebel 	assert(dev_info != NULL);
198d4d598e9SRuchika Gupta #if MAX_FIP_DEVICES > 1
199d4d598e9SRuchika Gupta 	assert(dev_spec != (uintptr_t)NULL);
200d4d598e9SRuchika Gupta #endif
201d4d598e9SRuchika Gupta 
202d4d598e9SRuchika Gupta 	result = allocate_dev_info(&info);
203d4d598e9SRuchika Gupta 	if (result != 0)
204d4d598e9SRuchika Gupta 		return -ENOMEM;
205d4d598e9SRuchika Gupta 
206d4d598e9SRuchika Gupta 	state = (fip_dev_state_t *)info->info;
207d4d598e9SRuchika Gupta 
208d4d598e9SRuchika Gupta 	state->dev_spec = dev_spec;
209d4d598e9SRuchika Gupta 
210d4d598e9SRuchika Gupta 	*dev_info = info;
211561cd33eSHarry Liebel 
212e098e244SJuan Castillo 	return 0;
213561cd33eSHarry Liebel }
214561cd33eSHarry Liebel 
215561cd33eSHarry Liebel 
216561cd33eSHarry Liebel /* Do some basic package checks. */
217625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
218561cd33eSHarry Liebel {
219e098e244SJuan Castillo 	int result;
22016948ae1SJuan Castillo 	unsigned int image_id = (unsigned int)init_params;
221625de1d4SDan Handley 	uintptr_t backend_handle;
222fb037bfbSDan Handley 	fip_toc_header_t header;
223561cd33eSHarry Liebel 	size_t bytes_read;
2245c380888SScott Branden 	fip_dev_state_t *state;
2255c380888SScott Branden 
2265c380888SScott Branden 	assert(dev_info != NULL);
2275c380888SScott Branden 
2285c380888SScott Branden 	state = (fip_dev_state_t *)dev_info->info;
229561cd33eSHarry Liebel 
230561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
23116948ae1SJuan Castillo 	result = plat_get_image_source(image_id, &backend_dev_handle,
232561cd33eSHarry Liebel 				       &backend_image_spec);
233e098e244SJuan Castillo 	if (result != 0) {
23416948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
23516948ae1SJuan Castillo 			image_id, result);
236e098e244SJuan Castillo 		result = -ENOENT;
237561cd33eSHarry Liebel 		goto fip_dev_init_exit;
238561cd33eSHarry Liebel 	}
239561cd33eSHarry Liebel 
240561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
241561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
242561cd33eSHarry Liebel 			 &backend_handle);
243e098e244SJuan Castillo 	if (result != 0) {
24416948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n", image_id, result);
245e098e244SJuan Castillo 		result = -ENOENT;
246561cd33eSHarry Liebel 		goto fip_dev_init_exit;
247561cd33eSHarry Liebel 	}
248561cd33eSHarry Liebel 
249625de1d4SDan Handley 	result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
250625de1d4SDan Handley 			&bytes_read);
251e098e244SJuan Castillo 	if (result == 0) {
252561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
25308c28d53SJeenu Viswambharan 			WARN("Firmware Image Package header check failed.\n");
254e098e244SJuan Castillo 			result = -ENOENT;
255561cd33eSHarry Liebel 		} else {
2566ad2e461SDan Handley 			VERBOSE("FIP header looks OK.\n");
2575c380888SScott Branden 			/*
2585c380888SScott Branden 			 * Store 16-bit Platform ToC flags field which occupies
2595c380888SScott Branden 			 * bits [32-47] in fip header.
2605c380888SScott Branden 			 */
2615c380888SScott Branden 			state->plat_toc_flag = (header.flags >> 32) & 0xffff;
262561cd33eSHarry Liebel 		}
263561cd33eSHarry Liebel 	}
264561cd33eSHarry Liebel 
265561cd33eSHarry Liebel 	io_close(backend_handle);
266561cd33eSHarry Liebel 
267561cd33eSHarry Liebel  fip_dev_init_exit:
268561cd33eSHarry Liebel 	return result;
269561cd33eSHarry Liebel }
270561cd33eSHarry Liebel 
271561cd33eSHarry Liebel /* Close a connection to the FIP device */
272fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info)
273561cd33eSHarry Liebel {
274561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
275561cd33eSHarry Liebel 
276561cd33eSHarry Liebel 	/* Clear the backend. */
277625de1d4SDan Handley 	backend_dev_handle = (uintptr_t)NULL;
278625de1d4SDan Handley 	backend_image_spec = (uintptr_t)NULL;
279561cd33eSHarry Liebel 
280d4d598e9SRuchika Gupta 	return free_dev_info(dev_info);
281561cd33eSHarry Liebel }
282561cd33eSHarry Liebel 
283561cd33eSHarry Liebel 
284561cd33eSHarry Liebel /* Open a file for access from package. */
285625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
286fb037bfbSDan Handley 			 io_entity_t *entity)
287561cd33eSHarry Liebel {
288e098e244SJuan Castillo 	int result;
289625de1d4SDan Handley 	uintptr_t backend_handle;
29016948ae1SJuan Castillo 	const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec;
291561cd33eSHarry Liebel 	size_t bytes_read;
292561cd33eSHarry Liebel 	int found_file = 0;
293561cd33eSHarry Liebel 
29416948ae1SJuan Castillo 	assert(uuid_spec != NULL);
295561cd33eSHarry Liebel 	assert(entity != NULL);
296561cd33eSHarry Liebel 
297561cd33eSHarry Liebel 	/* Can only have one file open at a time for the moment. We need to
298561cd33eSHarry Liebel 	 * track state like file cursor position. We know the header lives at
299561cd33eSHarry Liebel 	 * offset zero, so this entry should never be zero for an active file.
300561cd33eSHarry Liebel 	 * When the system supports dynamic memory allocation we can allow more
301561cd33eSHarry Liebel 	 * than one open file at a time if needed.
302561cd33eSHarry Liebel 	 */
303561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
30408c28d53SJeenu Viswambharan 		WARN("fip_file_open : Only one open file at a time.\n");
305*6eb75a1aSMasahiro Yamada 		return -ENFILE;
306561cd33eSHarry Liebel 	}
307561cd33eSHarry Liebel 
308561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
309561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
310561cd33eSHarry Liebel 			 &backend_handle);
311e098e244SJuan Castillo 	if (result != 0) {
31208c28d53SJeenu Viswambharan 		WARN("Failed to open Firmware Image Package (%i)\n", result);
313e098e244SJuan Castillo 		result = -ENOENT;
314561cd33eSHarry Liebel 		goto fip_file_open_exit;
315561cd33eSHarry Liebel 	}
316561cd33eSHarry Liebel 
317561cd33eSHarry Liebel 	/* Seek past the FIP header into the Table of Contents */
31870cb0bffSYann Gautier 	result = io_seek(backend_handle, IO_SEEK_SET,
31970cb0bffSYann Gautier 			 (signed long long)sizeof(fip_toc_header_t));
320e098e244SJuan Castillo 	if (result != 0) {
32108c28d53SJeenu Viswambharan 		WARN("fip_file_open: failed to seek\n");
322e098e244SJuan Castillo 		result = -ENOENT;
323561cd33eSHarry Liebel 		goto fip_file_open_close;
324561cd33eSHarry Liebel 	}
325561cd33eSHarry Liebel 
326561cd33eSHarry Liebel 	found_file = 0;
327561cd33eSHarry Liebel 	do {
328625de1d4SDan Handley 		result = io_read(backend_handle,
329625de1d4SDan Handley 				 (uintptr_t)&current_file.entry,
330561cd33eSHarry Liebel 				 sizeof(current_file.entry),
331561cd33eSHarry Liebel 				 &bytes_read);
332e098e244SJuan Castillo 		if (result == 0) {
333561cd33eSHarry Liebel 			if (compare_uuids(&current_file.entry.uuid,
33416948ae1SJuan Castillo 					  &uuid_spec->uuid) == 0) {
335561cd33eSHarry Liebel 				found_file = 1;
336561cd33eSHarry Liebel 				break;
337561cd33eSHarry Liebel 			}
338561cd33eSHarry Liebel 		} else {
33908c28d53SJeenu Viswambharan 			WARN("Failed to read FIP (%i)\n", result);
340561cd33eSHarry Liebel 			goto fip_file_open_close;
341561cd33eSHarry Liebel 		}
342561cd33eSHarry Liebel 	} while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
343561cd33eSHarry Liebel 
344561cd33eSHarry Liebel 	if (found_file == 1) {
345561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
346561cd33eSHarry Liebel 		 * the file position to 0. The 'current_file.entry' holds the
347561cd33eSHarry Liebel 		 * base and size of the file.
348561cd33eSHarry Liebel 		 */
349561cd33eSHarry Liebel 		current_file.file_pos = 0;
350561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
351561cd33eSHarry Liebel 	} else {
352561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
353dd3dc32fSJeenu Viswambharan 		current_file.entry.offset_address = 0;
354e098e244SJuan Castillo 		result = -ENOENT;
355561cd33eSHarry Liebel 	}
356561cd33eSHarry Liebel 
357561cd33eSHarry Liebel  fip_file_open_close:
358561cd33eSHarry Liebel 	io_close(backend_handle);
359561cd33eSHarry Liebel 
360561cd33eSHarry Liebel  fip_file_open_exit:
361561cd33eSHarry Liebel 	return result;
362561cd33eSHarry Liebel }
363561cd33eSHarry Liebel 
364561cd33eSHarry Liebel 
365561cd33eSHarry Liebel /* Return the size of a file in package */
366fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length)
367561cd33eSHarry Liebel {
368561cd33eSHarry Liebel 	assert(entity != NULL);
369561cd33eSHarry Liebel 	assert(length != NULL);
370561cd33eSHarry Liebel 
371fb037bfbSDan Handley 	*length =  ((file_state_t *)entity->info)->entry.size;
372561cd33eSHarry Liebel 
373e098e244SJuan Castillo 	return 0;
374561cd33eSHarry Liebel }
375561cd33eSHarry Liebel 
376561cd33eSHarry Liebel 
377561cd33eSHarry Liebel /* Read data from a file in package */
378625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
379561cd33eSHarry Liebel 			  size_t *length_read)
380561cd33eSHarry Liebel {
381e098e244SJuan Castillo 	int result;
382fb037bfbSDan Handley 	file_state_t *fp;
383561cd33eSHarry Liebel 	size_t file_offset;
384561cd33eSHarry Liebel 	size_t bytes_read;
385625de1d4SDan Handley 	uintptr_t backend_handle;
386561cd33eSHarry Liebel 
387561cd33eSHarry Liebel 	assert(entity != NULL);
388561cd33eSHarry Liebel 	assert(length_read != NULL);
389625de1d4SDan Handley 	assert(entity->info != (uintptr_t)NULL);
390561cd33eSHarry Liebel 
391561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
392561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
393561cd33eSHarry Liebel 			 &backend_handle);
394e098e244SJuan Castillo 	if (result != 0) {
39508c28d53SJeenu Viswambharan 		WARN("Failed to open FIP (%i)\n", result);
396e098e244SJuan Castillo 		result = -ENOENT;
397561cd33eSHarry Liebel 		goto fip_file_read_exit;
398561cd33eSHarry Liebel 	}
399561cd33eSHarry Liebel 
400fb037bfbSDan Handley 	fp = (file_state_t *)entity->info;
401561cd33eSHarry Liebel 
402561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
403561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
40470cb0bffSYann Gautier 	result = io_seek(backend_handle, IO_SEEK_SET,
40570cb0bffSYann Gautier 			 (signed long long)file_offset);
406e098e244SJuan Castillo 	if (result != 0) {
40708c28d53SJeenu Viswambharan 		WARN("fip_file_read: failed to seek\n");
408e098e244SJuan Castillo 		result = -ENOENT;
409561cd33eSHarry Liebel 		goto fip_file_read_close;
410561cd33eSHarry Liebel 	}
411561cd33eSHarry Liebel 
412561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
413e098e244SJuan Castillo 	if (result != 0) {
414561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
41508c28d53SJeenu Viswambharan 		WARN("Failed to read payload (%i)\n", result);
416e098e244SJuan Castillo 		result = -ENOENT;
417561cd33eSHarry Liebel 		goto fip_file_read_close;
418561cd33eSHarry Liebel 	} else {
419561cd33eSHarry Liebel 		/* Set caller length and new file position. */
420561cd33eSHarry Liebel 		*length_read = bytes_read;
421561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
422561cd33eSHarry Liebel 	}
423561cd33eSHarry Liebel 
424561cd33eSHarry Liebel /* Close the backend. */
425561cd33eSHarry Liebel  fip_file_read_close:
426561cd33eSHarry Liebel 	io_close(backend_handle);
427561cd33eSHarry Liebel 
428561cd33eSHarry Liebel  fip_file_read_exit:
429561cd33eSHarry Liebel 	return result;
430561cd33eSHarry Liebel }
431561cd33eSHarry Liebel 
432561cd33eSHarry Liebel 
433561cd33eSHarry Liebel /* Close a file in package */
434fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity)
435561cd33eSHarry Liebel {
436561cd33eSHarry Liebel 	/* Clear our current file pointer.
437561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
438561cd33eSHarry Liebel 	 */
439561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
44032f0d3c6SDouglas Raillard 		zeromem(&current_file, sizeof(current_file));
441561cd33eSHarry Liebel 	}
442561cd33eSHarry Liebel 
443561cd33eSHarry Liebel 	/* Clear the Entity info. */
444561cd33eSHarry Liebel 	entity->info = 0;
445561cd33eSHarry Liebel 
446e098e244SJuan Castillo 	return 0;
447561cd33eSHarry Liebel }
448561cd33eSHarry Liebel 
449561cd33eSHarry Liebel /* Exported functions */
450561cd33eSHarry Liebel 
451561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
452625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con)
453561cd33eSHarry Liebel {
454e098e244SJuan Castillo 	int result;
455561cd33eSHarry Liebel 	assert(dev_con != NULL);
456561cd33eSHarry Liebel 
457d4d598e9SRuchika Gupta 	/*
458d4d598e9SRuchika Gupta 	 * Since dev_info isn't really used in io_register_device, always
459d4d598e9SRuchika Gupta 	 * use the same device info at here instead.
460d4d598e9SRuchika Gupta 	 */
461d4d598e9SRuchika Gupta 	result = io_register_device(&dev_info_pool[0]);
462e098e244SJuan Castillo 	if (result == 0)
463561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
464561cd33eSHarry Liebel 
465561cd33eSHarry Liebel 	return result;
466561cd33eSHarry Liebel }
4675c380888SScott Branden 
4685c380888SScott Branden /* Function to retrieve plat_toc_flags, previously saved in FIP dev */
4695c380888SScott Branden int fip_dev_get_plat_toc_flag(io_dev_info_t *dev_info, uint16_t *plat_toc_flag)
4705c380888SScott Branden {
4715c380888SScott Branden 	fip_dev_state_t *state;
4725c380888SScott Branden 
4735c380888SScott Branden 	assert(dev_info != NULL);
4745c380888SScott Branden 
4755c380888SScott Branden 	state = (fip_dev_state_t *)dev_info->info;
4765c380888SScott Branden 
4775c380888SScott Branden 	*plat_toc_flag =  state->plat_toc_flag;
4785c380888SScott Branden 
4795c380888SScott Branden 	return 0;
4805c380888SScott Branden }
481