xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision 70cb0bff7d42bd9e09dddef3c2fc6e3b385c6e5a)
1561cd33eSHarry Liebel /*
2609e053cSAmbroise Vincent  * Copyright (c) 2014-2019, 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;
49d4d598e9SRuchika Gupta } fip_dev_state_t;
50d4d598e9SRuchika Gupta 
51609e053cSAmbroise Vincent static const uuid_t uuid_null;
52d4d598e9SRuchika Gupta /*
53d4d598e9SRuchika Gupta  * Only one file can be open across all FIP device
54d4d598e9SRuchika Gupta  * as backends like io_memmap don't support
55d4d598e9SRuchika Gupta  * multiple open files. The file state and
56d4d598e9SRuchika Gupta  * backend handle should be maintained per FIP device
57d4d598e9SRuchika Gupta  * if the same support is available in the backend
58d4d598e9SRuchika Gupta  */
59fb037bfbSDan Handley static file_state_t current_file = {0};
60625de1d4SDan Handley static uintptr_t backend_dev_handle;
61625de1d4SDan Handley static uintptr_t backend_image_spec;
62561cd33eSHarry Liebel 
63d4d598e9SRuchika Gupta static fip_dev_state_t state_pool[MAX_FIP_DEVICES];
64d4d598e9SRuchika Gupta static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES];
65d4d598e9SRuchika Gupta 
66d4d598e9SRuchika Gupta /* Track number of allocated fip devices */
67d4d598e9SRuchika Gupta static unsigned int fip_dev_count;
68561cd33eSHarry Liebel 
69561cd33eSHarry Liebel /* Firmware Image Package driver functions */
70625de1d4SDan Handley static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
71625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
72fb037bfbSDan Handley 			  io_entity_t *entity);
73fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length);
74625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
75561cd33eSHarry Liebel 			  size_t *length_read);
76fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity);
77625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
78fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info);
79561cd33eSHarry Liebel 
80561cd33eSHarry Liebel 
81561cd33eSHarry Liebel /* Return 0 for equal uuids. */
82561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
83561cd33eSHarry Liebel {
84561cd33eSHarry Liebel 	return memcmp(uuid1, uuid2, sizeof(uuid_t));
85561cd33eSHarry Liebel }
86561cd33eSHarry Liebel 
87561cd33eSHarry Liebel 
88fb037bfbSDan Handley static inline int is_valid_header(fip_toc_header_t *header)
89561cd33eSHarry Liebel {
90561cd33eSHarry Liebel 	if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
91561cd33eSHarry Liebel 		return 1;
92561cd33eSHarry Liebel 	} else {
93561cd33eSHarry Liebel 		return 0;
94561cd33eSHarry Liebel 	}
95561cd33eSHarry Liebel }
96561cd33eSHarry Liebel 
97561cd33eSHarry Liebel 
98561cd33eSHarry Liebel /* Identify the device type as a virtual driver */
997fabe1a8SRoberto Vargas static io_type_t device_type_fip(void)
100561cd33eSHarry Liebel {
101561cd33eSHarry Liebel 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
102561cd33eSHarry Liebel }
103561cd33eSHarry Liebel 
104561cd33eSHarry Liebel 
105625de1d4SDan Handley static const io_dev_connector_t fip_dev_connector = {
106561cd33eSHarry Liebel 	.dev_open = fip_dev_open
107561cd33eSHarry Liebel };
108561cd33eSHarry Liebel 
109561cd33eSHarry Liebel 
110625de1d4SDan Handley static const io_dev_funcs_t fip_dev_funcs = {
111561cd33eSHarry Liebel 	.type = device_type_fip,
112561cd33eSHarry Liebel 	.open = fip_file_open,
113561cd33eSHarry Liebel 	.seek = NULL,
114561cd33eSHarry Liebel 	.size = fip_file_len,
115561cd33eSHarry Liebel 	.read = fip_file_read,
116561cd33eSHarry Liebel 	.write = NULL,
117561cd33eSHarry Liebel 	.close = fip_file_close,
118561cd33eSHarry Liebel 	.dev_init = fip_dev_init,
119561cd33eSHarry Liebel 	.dev_close = fip_dev_close,
120561cd33eSHarry Liebel };
121561cd33eSHarry Liebel 
122d4d598e9SRuchika Gupta /* Locate a file state in the pool, specified by address */
123d4d598e9SRuchika Gupta static int find_first_fip_state(const uintptr_t dev_spec,
124d4d598e9SRuchika Gupta 				  unsigned int *index_out)
125d4d598e9SRuchika Gupta {
126d4d598e9SRuchika Gupta 	int result = -ENOENT;
127d4d598e9SRuchika Gupta 	unsigned int index;
128561cd33eSHarry Liebel 
129d4d598e9SRuchika Gupta 	for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
130d4d598e9SRuchika Gupta 		/* dev_spec is used as identifier since it's unique */
131d4d598e9SRuchika Gupta 		if (state_pool[index].dev_spec == dev_spec) {
132d4d598e9SRuchika Gupta 			result = 0;
133d4d598e9SRuchika Gupta 			*index_out = index;
134d4d598e9SRuchika Gupta 			break;
135d4d598e9SRuchika Gupta 		}
136d4d598e9SRuchika Gupta 	}
137d4d598e9SRuchika Gupta 	return result;
138d4d598e9SRuchika Gupta }
139561cd33eSHarry Liebel 
140561cd33eSHarry Liebel 
141d4d598e9SRuchika Gupta /* Allocate a device info from the pool and return a pointer to it */
142d4d598e9SRuchika Gupta static int allocate_dev_info(io_dev_info_t **dev_info)
143d4d598e9SRuchika Gupta {
144d4d598e9SRuchika Gupta 	int result = -ENOMEM;
145d4d598e9SRuchika Gupta 
146d4d598e9SRuchika Gupta 	assert(dev_info != NULL);
147d4d598e9SRuchika Gupta 
148d4d598e9SRuchika Gupta 	if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) {
149d4d598e9SRuchika Gupta 		unsigned int index = 0;
150d4d598e9SRuchika Gupta 
151d4d598e9SRuchika Gupta 		result = find_first_fip_state(0, &index);
152d4d598e9SRuchika Gupta 		assert(result == 0);
153d4d598e9SRuchika Gupta 		/* initialize dev_info */
154d4d598e9SRuchika Gupta 		dev_info_pool[index].funcs = &fip_dev_funcs;
155d4d598e9SRuchika Gupta 		dev_info_pool[index].info =
156d4d598e9SRuchika Gupta 				(uintptr_t)&state_pool[index];
157d4d598e9SRuchika Gupta 		*dev_info = &dev_info_pool[index];
158d4d598e9SRuchika Gupta 		++fip_dev_count;
159d4d598e9SRuchika Gupta 	}
160d4d598e9SRuchika Gupta 
161d4d598e9SRuchika Gupta 	return result;
162d4d598e9SRuchika Gupta }
163d4d598e9SRuchika Gupta 
164d4d598e9SRuchika Gupta /* Release a device info to the pool */
165d4d598e9SRuchika Gupta static int free_dev_info(io_dev_info_t *dev_info)
166d4d598e9SRuchika Gupta {
167d4d598e9SRuchika Gupta 	int result;
168d4d598e9SRuchika Gupta 	unsigned int index = 0;
169d4d598e9SRuchika Gupta 	fip_dev_state_t *state;
170d4d598e9SRuchika Gupta 
171d4d598e9SRuchika Gupta 	assert(dev_info != NULL);
172d4d598e9SRuchika Gupta 
173d4d598e9SRuchika Gupta 	state = (fip_dev_state_t *)dev_info->info;
174d4d598e9SRuchika Gupta 	result = find_first_fip_state(state->dev_spec, &index);
175d4d598e9SRuchika Gupta 	if (result ==  0) {
176d4d598e9SRuchika Gupta 		/* free if device info is valid */
177d4d598e9SRuchika Gupta 		zeromem(state, sizeof(fip_dev_state_t));
178d4d598e9SRuchika Gupta 		--fip_dev_count;
179d4d598e9SRuchika Gupta 	}
180d4d598e9SRuchika Gupta 
181d4d598e9SRuchika Gupta 	return result;
182d4d598e9SRuchika Gupta }
183d4d598e9SRuchika Gupta 
184d4d598e9SRuchika Gupta /*
185d4d598e9SRuchika Gupta  * Multiple FIP devices can be opened depending on the value of
186d4d598e9SRuchika Gupta  * MAX_FIP_DEVICES. Given that there is only one backend, only a
187d4d598e9SRuchika Gupta  * single file can be open at a time by any FIP device.
188d4d598e9SRuchika Gupta  */
189d4d598e9SRuchika Gupta static int fip_dev_open(const uintptr_t dev_spec,
190fb037bfbSDan Handley 			 io_dev_info_t **dev_info)
191561cd33eSHarry Liebel {
192d4d598e9SRuchika Gupta 	int result;
193d4d598e9SRuchika Gupta 	io_dev_info_t *info;
194d4d598e9SRuchika Gupta 	fip_dev_state_t *state;
195d4d598e9SRuchika Gupta 
196561cd33eSHarry Liebel 	assert(dev_info != NULL);
197d4d598e9SRuchika Gupta #if MAX_FIP_DEVICES > 1
198d4d598e9SRuchika Gupta 	assert(dev_spec != (uintptr_t)NULL);
199d4d598e9SRuchika Gupta #endif
200d4d598e9SRuchika Gupta 
201d4d598e9SRuchika Gupta 	result = allocate_dev_info(&info);
202d4d598e9SRuchika Gupta 	if (result != 0)
203d4d598e9SRuchika Gupta 		return -ENOMEM;
204d4d598e9SRuchika Gupta 
205d4d598e9SRuchika Gupta 	state = (fip_dev_state_t *)info->info;
206d4d598e9SRuchika Gupta 
207d4d598e9SRuchika Gupta 	state->dev_spec = dev_spec;
208d4d598e9SRuchika Gupta 
209d4d598e9SRuchika Gupta 	*dev_info = info;
210561cd33eSHarry Liebel 
211e098e244SJuan Castillo 	return 0;
212561cd33eSHarry Liebel }
213561cd33eSHarry Liebel 
214561cd33eSHarry Liebel 
215561cd33eSHarry Liebel /* Do some basic package checks. */
216625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
217561cd33eSHarry Liebel {
218e098e244SJuan Castillo 	int result;
21916948ae1SJuan Castillo 	unsigned int image_id = (unsigned int)init_params;
220625de1d4SDan Handley 	uintptr_t backend_handle;
221fb037bfbSDan Handley 	fip_toc_header_t header;
222561cd33eSHarry Liebel 	size_t bytes_read;
223561cd33eSHarry Liebel 
224561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
22516948ae1SJuan Castillo 	result = plat_get_image_source(image_id, &backend_dev_handle,
226561cd33eSHarry Liebel 				       &backend_image_spec);
227e098e244SJuan Castillo 	if (result != 0) {
22816948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
22916948ae1SJuan Castillo 			image_id, result);
230e098e244SJuan Castillo 		result = -ENOENT;
231561cd33eSHarry Liebel 		goto fip_dev_init_exit;
232561cd33eSHarry Liebel 	}
233561cd33eSHarry Liebel 
234561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
235561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
236561cd33eSHarry Liebel 			 &backend_handle);
237e098e244SJuan Castillo 	if (result != 0) {
23816948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n", image_id, result);
239e098e244SJuan Castillo 		result = -ENOENT;
240561cd33eSHarry Liebel 		goto fip_dev_init_exit;
241561cd33eSHarry Liebel 	}
242561cd33eSHarry Liebel 
243625de1d4SDan Handley 	result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
244625de1d4SDan Handley 			&bytes_read);
245e098e244SJuan Castillo 	if (result == 0) {
246561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
24708c28d53SJeenu Viswambharan 			WARN("Firmware Image Package header check failed.\n");
248e098e244SJuan Castillo 			result = -ENOENT;
249561cd33eSHarry Liebel 		} else {
2506ad2e461SDan Handley 			VERBOSE("FIP header looks OK.\n");
251561cd33eSHarry Liebel 		}
252561cd33eSHarry Liebel 	}
253561cd33eSHarry Liebel 
254561cd33eSHarry Liebel 	io_close(backend_handle);
255561cd33eSHarry Liebel 
256561cd33eSHarry Liebel  fip_dev_init_exit:
257561cd33eSHarry Liebel 	return result;
258561cd33eSHarry Liebel }
259561cd33eSHarry Liebel 
260561cd33eSHarry Liebel /* Close a connection to the FIP device */
261fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info)
262561cd33eSHarry Liebel {
263561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
264561cd33eSHarry Liebel 
265561cd33eSHarry Liebel 	/* Clear the backend. */
266625de1d4SDan Handley 	backend_dev_handle = (uintptr_t)NULL;
267625de1d4SDan Handley 	backend_image_spec = (uintptr_t)NULL;
268561cd33eSHarry Liebel 
269d4d598e9SRuchika Gupta 	return free_dev_info(dev_info);
270561cd33eSHarry Liebel }
271561cd33eSHarry Liebel 
272561cd33eSHarry Liebel 
273561cd33eSHarry Liebel /* Open a file for access from package. */
274625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
275fb037bfbSDan Handley 			 io_entity_t *entity)
276561cd33eSHarry Liebel {
277e098e244SJuan Castillo 	int result;
278625de1d4SDan Handley 	uintptr_t backend_handle;
27916948ae1SJuan Castillo 	const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec;
280561cd33eSHarry Liebel 	size_t bytes_read;
281561cd33eSHarry Liebel 	int found_file = 0;
282561cd33eSHarry Liebel 
28316948ae1SJuan Castillo 	assert(uuid_spec != NULL);
284561cd33eSHarry Liebel 	assert(entity != NULL);
285561cd33eSHarry Liebel 
286561cd33eSHarry Liebel 	/* Can only have one file open at a time for the moment. We need to
287561cd33eSHarry Liebel 	 * track state like file cursor position. We know the header lives at
288561cd33eSHarry Liebel 	 * offset zero, so this entry should never be zero for an active file.
289561cd33eSHarry Liebel 	 * When the system supports dynamic memory allocation we can allow more
290561cd33eSHarry Liebel 	 * than one open file at a time if needed.
291561cd33eSHarry Liebel 	 */
292561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
29308c28d53SJeenu Viswambharan 		WARN("fip_file_open : Only one open file at a time.\n");
294e098e244SJuan Castillo 		return -ENOMEM;
295561cd33eSHarry Liebel 	}
296561cd33eSHarry Liebel 
297561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
298561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
299561cd33eSHarry Liebel 			 &backend_handle);
300e098e244SJuan Castillo 	if (result != 0) {
30108c28d53SJeenu Viswambharan 		WARN("Failed to open Firmware Image Package (%i)\n", result);
302e098e244SJuan Castillo 		result = -ENOENT;
303561cd33eSHarry Liebel 		goto fip_file_open_exit;
304561cd33eSHarry Liebel 	}
305561cd33eSHarry Liebel 
306561cd33eSHarry Liebel 	/* Seek past the FIP header into the Table of Contents */
307*70cb0bffSYann Gautier 	result = io_seek(backend_handle, IO_SEEK_SET,
308*70cb0bffSYann Gautier 			 (signed long long)sizeof(fip_toc_header_t));
309e098e244SJuan Castillo 	if (result != 0) {
31008c28d53SJeenu Viswambharan 		WARN("fip_file_open: failed to seek\n");
311e098e244SJuan Castillo 		result = -ENOENT;
312561cd33eSHarry Liebel 		goto fip_file_open_close;
313561cd33eSHarry Liebel 	}
314561cd33eSHarry Liebel 
315561cd33eSHarry Liebel 	found_file = 0;
316561cd33eSHarry Liebel 	do {
317625de1d4SDan Handley 		result = io_read(backend_handle,
318625de1d4SDan Handley 				 (uintptr_t)&current_file.entry,
319561cd33eSHarry Liebel 				 sizeof(current_file.entry),
320561cd33eSHarry Liebel 				 &bytes_read);
321e098e244SJuan Castillo 		if (result == 0) {
322561cd33eSHarry Liebel 			if (compare_uuids(&current_file.entry.uuid,
32316948ae1SJuan Castillo 					  &uuid_spec->uuid) == 0) {
324561cd33eSHarry Liebel 				found_file = 1;
325561cd33eSHarry Liebel 				break;
326561cd33eSHarry Liebel 			}
327561cd33eSHarry Liebel 		} else {
32808c28d53SJeenu Viswambharan 			WARN("Failed to read FIP (%i)\n", result);
329561cd33eSHarry Liebel 			goto fip_file_open_close;
330561cd33eSHarry Liebel 		}
331561cd33eSHarry Liebel 	} while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
332561cd33eSHarry Liebel 
333561cd33eSHarry Liebel 	if (found_file == 1) {
334561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
335561cd33eSHarry Liebel 		 * the file position to 0. The 'current_file.entry' holds the
336561cd33eSHarry Liebel 		 * base and size of the file.
337561cd33eSHarry Liebel 		 */
338561cd33eSHarry Liebel 		current_file.file_pos = 0;
339561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
340561cd33eSHarry Liebel 	} else {
341561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
342dd3dc32fSJeenu Viswambharan 		current_file.entry.offset_address = 0;
343e098e244SJuan Castillo 		result = -ENOENT;
344561cd33eSHarry Liebel 	}
345561cd33eSHarry Liebel 
346561cd33eSHarry Liebel  fip_file_open_close:
347561cd33eSHarry Liebel 	io_close(backend_handle);
348561cd33eSHarry Liebel 
349561cd33eSHarry Liebel  fip_file_open_exit:
350561cd33eSHarry Liebel 	return result;
351561cd33eSHarry Liebel }
352561cd33eSHarry Liebel 
353561cd33eSHarry Liebel 
354561cd33eSHarry Liebel /* Return the size of a file in package */
355fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length)
356561cd33eSHarry Liebel {
357561cd33eSHarry Liebel 	assert(entity != NULL);
358561cd33eSHarry Liebel 	assert(length != NULL);
359561cd33eSHarry Liebel 
360fb037bfbSDan Handley 	*length =  ((file_state_t *)entity->info)->entry.size;
361561cd33eSHarry Liebel 
362e098e244SJuan Castillo 	return 0;
363561cd33eSHarry Liebel }
364561cd33eSHarry Liebel 
365561cd33eSHarry Liebel 
366561cd33eSHarry Liebel /* Read data from a file in package */
367625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
368561cd33eSHarry Liebel 			  size_t *length_read)
369561cd33eSHarry Liebel {
370e098e244SJuan Castillo 	int result;
371fb037bfbSDan Handley 	file_state_t *fp;
372561cd33eSHarry Liebel 	size_t file_offset;
373561cd33eSHarry Liebel 	size_t bytes_read;
374625de1d4SDan Handley 	uintptr_t backend_handle;
375561cd33eSHarry Liebel 
376561cd33eSHarry Liebel 	assert(entity != NULL);
377561cd33eSHarry Liebel 	assert(length_read != NULL);
378625de1d4SDan Handley 	assert(entity->info != (uintptr_t)NULL);
379561cd33eSHarry Liebel 
380561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
381561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
382561cd33eSHarry Liebel 			 &backend_handle);
383e098e244SJuan Castillo 	if (result != 0) {
38408c28d53SJeenu Viswambharan 		WARN("Failed to open FIP (%i)\n", result);
385e098e244SJuan Castillo 		result = -ENOENT;
386561cd33eSHarry Liebel 		goto fip_file_read_exit;
387561cd33eSHarry Liebel 	}
388561cd33eSHarry Liebel 
389fb037bfbSDan Handley 	fp = (file_state_t *)entity->info;
390561cd33eSHarry Liebel 
391561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
392561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
393*70cb0bffSYann Gautier 	result = io_seek(backend_handle, IO_SEEK_SET,
394*70cb0bffSYann Gautier 			 (signed long long)file_offset);
395e098e244SJuan Castillo 	if (result != 0) {
39608c28d53SJeenu Viswambharan 		WARN("fip_file_read: failed to seek\n");
397e098e244SJuan Castillo 		result = -ENOENT;
398561cd33eSHarry Liebel 		goto fip_file_read_close;
399561cd33eSHarry Liebel 	}
400561cd33eSHarry Liebel 
401561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
402e098e244SJuan Castillo 	if (result != 0) {
403561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
40408c28d53SJeenu Viswambharan 		WARN("Failed to read payload (%i)\n", result);
405e098e244SJuan Castillo 		result = -ENOENT;
406561cd33eSHarry Liebel 		goto fip_file_read_close;
407561cd33eSHarry Liebel 	} else {
408561cd33eSHarry Liebel 		/* Set caller length and new file position. */
409561cd33eSHarry Liebel 		*length_read = bytes_read;
410561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
411561cd33eSHarry Liebel 	}
412561cd33eSHarry Liebel 
413561cd33eSHarry Liebel /* Close the backend. */
414561cd33eSHarry Liebel  fip_file_read_close:
415561cd33eSHarry Liebel 	io_close(backend_handle);
416561cd33eSHarry Liebel 
417561cd33eSHarry Liebel  fip_file_read_exit:
418561cd33eSHarry Liebel 	return result;
419561cd33eSHarry Liebel }
420561cd33eSHarry Liebel 
421561cd33eSHarry Liebel 
422561cd33eSHarry Liebel /* Close a file in package */
423fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity)
424561cd33eSHarry Liebel {
425561cd33eSHarry Liebel 	/* Clear our current file pointer.
426561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
427561cd33eSHarry Liebel 	 */
428561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
42932f0d3c6SDouglas Raillard 		zeromem(&current_file, sizeof(current_file));
430561cd33eSHarry Liebel 	}
431561cd33eSHarry Liebel 
432561cd33eSHarry Liebel 	/* Clear the Entity info. */
433561cd33eSHarry Liebel 	entity->info = 0;
434561cd33eSHarry Liebel 
435e098e244SJuan Castillo 	return 0;
436561cd33eSHarry Liebel }
437561cd33eSHarry Liebel 
438561cd33eSHarry Liebel /* Exported functions */
439561cd33eSHarry Liebel 
440561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
441625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con)
442561cd33eSHarry Liebel {
443e098e244SJuan Castillo 	int result;
444561cd33eSHarry Liebel 	assert(dev_con != NULL);
445561cd33eSHarry Liebel 
446d4d598e9SRuchika Gupta 	/*
447d4d598e9SRuchika Gupta 	 * Since dev_info isn't really used in io_register_device, always
448d4d598e9SRuchika Gupta 	 * use the same device info at here instead.
449d4d598e9SRuchika Gupta 	 */
450d4d598e9SRuchika Gupta 	result = io_register_device(&dev_info_pool[0]);
451e098e244SJuan Castillo 	if (result == 0)
452561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
453561cd33eSHarry Liebel 
454561cd33eSHarry Liebel 	return result;
455561cd33eSHarry Liebel }
456