xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision d4d598e92fe88ca3e60f41ff2f621aac176a02a6)
1561cd33eSHarry Liebel /*
2fb1198b1SAntonio Nino Diaz  * Copyright (c) 2014-2018, 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>
85f0cdb05SDan Handley #include <bl_common.h>
997043ac9SDan Handley #include <debug.h>
1097043ac9SDan Handley #include <errno.h>
1135e98e55SDan Handley #include <firmware_image_package.h>
1235e98e55SDan Handley #include <io_driver.h>
1335e98e55SDan Handley #include <io_fip.h>
1497043ac9SDan Handley #include <io_storage.h>
1597043ac9SDan Handley #include <platform.h>
165f0cdb05SDan Handley #include <platform_def.h>
1797043ac9SDan Handley #include <stdint.h>
1897043ac9SDan Handley #include <string.h>
1932f0d3c6SDouglas Raillard #include <utils.h>
2097043ac9SDan Handley #include <uuid.h>
21561cd33eSHarry Liebel 
22*d4d598e9SRuchika Gupta #ifndef MAX_FIP_DEVICES
23*d4d598e9SRuchika Gupta #define MAX_FIP_DEVICES		1
24*d4d598e9SRuchika Gupta #endif
25*d4d598e9SRuchika Gupta 
26561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/
27561cd33eSHarry Liebel #define PRINT_UUID2(x)								\
28561cd33eSHarry Liebel 	"%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",	\
29561cd33eSHarry Liebel 		x.time_low, x.time_mid, x.time_hi_and_version,			\
30561cd33eSHarry Liebel 		x.clock_seq_hi_and_reserved, x.clock_seq_low,			\
31561cd33eSHarry Liebel 		x.node[0], x.node[1], x.node[2], x.node[3],			\
32561cd33eSHarry Liebel 		x.node[4], x.node[5]
33561cd33eSHarry Liebel 
34561cd33eSHarry Liebel typedef struct {
35561cd33eSHarry Liebel 	unsigned int file_pos;
36fb037bfbSDan Handley 	fip_toc_entry_t entry;
37fb037bfbSDan Handley } file_state_t;
38561cd33eSHarry Liebel 
39*d4d598e9SRuchika Gupta /*
40*d4d598e9SRuchika Gupta  * Maintain dev_spec per FIP Device
41*d4d598e9SRuchika Gupta  * TODO - Add backend handles and file state
42*d4d598e9SRuchika Gupta  * per FIP device here once backends like io_memmap
43*d4d598e9SRuchika Gupta  * can support multiple open files
44*d4d598e9SRuchika Gupta  */
45*d4d598e9SRuchika Gupta typedef struct {
46*d4d598e9SRuchika Gupta 	uintptr_t dev_spec;
47*d4d598e9SRuchika Gupta } fip_dev_state_t;
48*d4d598e9SRuchika Gupta 
4903364865SRoberto Vargas static const uuid_t uuid_null = { {0} };
50*d4d598e9SRuchika Gupta /*
51*d4d598e9SRuchika Gupta  * Only one file can be open across all FIP device
52*d4d598e9SRuchika Gupta  * as backends like io_memmap don't support
53*d4d598e9SRuchika Gupta  * multiple open files. The file state and
54*d4d598e9SRuchika Gupta  * backend handle should be maintained per FIP device
55*d4d598e9SRuchika Gupta  * if the same support is available in the backend
56*d4d598e9SRuchika Gupta  */
57fb037bfbSDan Handley static file_state_t current_file = {0};
58625de1d4SDan Handley static uintptr_t backend_dev_handle;
59625de1d4SDan Handley static uintptr_t backend_image_spec;
60561cd33eSHarry Liebel 
61*d4d598e9SRuchika Gupta static fip_dev_state_t state_pool[MAX_FIP_DEVICES];
62*d4d598e9SRuchika Gupta static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES];
63*d4d598e9SRuchika Gupta 
64*d4d598e9SRuchika Gupta /* Track number of allocated fip devices */
65*d4d598e9SRuchika Gupta static unsigned int fip_dev_count;
66561cd33eSHarry Liebel 
67561cd33eSHarry Liebel /* Firmware Image Package driver functions */
68625de1d4SDan Handley static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
69625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
70fb037bfbSDan Handley 			  io_entity_t *entity);
71fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length);
72625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
73561cd33eSHarry Liebel 			  size_t *length_read);
74fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity);
75625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
76fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info);
77561cd33eSHarry Liebel 
78561cd33eSHarry Liebel 
79561cd33eSHarry Liebel /* Return 0 for equal uuids. */
80561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
81561cd33eSHarry Liebel {
82561cd33eSHarry Liebel 	return memcmp(uuid1, uuid2, sizeof(uuid_t));
83561cd33eSHarry Liebel }
84561cd33eSHarry Liebel 
85561cd33eSHarry Liebel 
86561cd33eSHarry Liebel /* TODO: We could check version numbers or do a package checksum? */
87fb037bfbSDan Handley static inline int is_valid_header(fip_toc_header_t *header)
88561cd33eSHarry Liebel {
89561cd33eSHarry Liebel 	if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
90561cd33eSHarry Liebel 		return 1;
91561cd33eSHarry Liebel 	} else {
92561cd33eSHarry Liebel 		return 0;
93561cd33eSHarry Liebel 	}
94561cd33eSHarry Liebel }
95561cd33eSHarry Liebel 
96561cd33eSHarry Liebel 
97561cd33eSHarry Liebel /* Identify the device type as a virtual driver */
987fabe1a8SRoberto Vargas static io_type_t device_type_fip(void)
99561cd33eSHarry Liebel {
100561cd33eSHarry Liebel 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
101561cd33eSHarry Liebel }
102561cd33eSHarry Liebel 
103561cd33eSHarry Liebel 
104625de1d4SDan Handley static const io_dev_connector_t fip_dev_connector = {
105561cd33eSHarry Liebel 	.dev_open = fip_dev_open
106561cd33eSHarry Liebel };
107561cd33eSHarry Liebel 
108561cd33eSHarry Liebel 
109625de1d4SDan Handley static const io_dev_funcs_t fip_dev_funcs = {
110561cd33eSHarry Liebel 	.type = device_type_fip,
111561cd33eSHarry Liebel 	.open = fip_file_open,
112561cd33eSHarry Liebel 	.seek = NULL,
113561cd33eSHarry Liebel 	.size = fip_file_len,
114561cd33eSHarry Liebel 	.read = fip_file_read,
115561cd33eSHarry Liebel 	.write = NULL,
116561cd33eSHarry Liebel 	.close = fip_file_close,
117561cd33eSHarry Liebel 	.dev_init = fip_dev_init,
118561cd33eSHarry Liebel 	.dev_close = fip_dev_close,
119561cd33eSHarry Liebel };
120561cd33eSHarry Liebel 
121*d4d598e9SRuchika Gupta /* Locate a file state in the pool, specified by address */
122*d4d598e9SRuchika Gupta static int find_first_fip_state(const uintptr_t dev_spec,
123*d4d598e9SRuchika Gupta 				  unsigned int *index_out)
124*d4d598e9SRuchika Gupta {
125*d4d598e9SRuchika Gupta 	int result = -ENOENT;
126*d4d598e9SRuchika Gupta 	unsigned int index;
127561cd33eSHarry Liebel 
128*d4d598e9SRuchika Gupta 	for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
129*d4d598e9SRuchika Gupta 		/* dev_spec is used as identifier since it's unique */
130*d4d598e9SRuchika Gupta 		if (state_pool[index].dev_spec == dev_spec) {
131*d4d598e9SRuchika Gupta 			result = 0;
132*d4d598e9SRuchika Gupta 			*index_out = index;
133*d4d598e9SRuchika Gupta 			break;
134*d4d598e9SRuchika Gupta 		}
135*d4d598e9SRuchika Gupta 	}
136*d4d598e9SRuchika Gupta 	return result;
137*d4d598e9SRuchika Gupta }
138561cd33eSHarry Liebel 
139561cd33eSHarry Liebel 
140*d4d598e9SRuchika Gupta /* Allocate a device info from the pool and return a pointer to it */
141*d4d598e9SRuchika Gupta static int allocate_dev_info(io_dev_info_t **dev_info)
142*d4d598e9SRuchika Gupta {
143*d4d598e9SRuchika Gupta 	int result = -ENOMEM;
144*d4d598e9SRuchika Gupta 
145*d4d598e9SRuchika Gupta 	assert(dev_info != NULL);
146*d4d598e9SRuchika Gupta 
147*d4d598e9SRuchika Gupta 	if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) {
148*d4d598e9SRuchika Gupta 		unsigned int index = 0;
149*d4d598e9SRuchika Gupta 
150*d4d598e9SRuchika Gupta 		result = find_first_fip_state(0, &index);
151*d4d598e9SRuchika Gupta 		assert(result == 0);
152*d4d598e9SRuchika Gupta 		/* initialize dev_info */
153*d4d598e9SRuchika Gupta 		dev_info_pool[index].funcs = &fip_dev_funcs;
154*d4d598e9SRuchika Gupta 		dev_info_pool[index].info =
155*d4d598e9SRuchika Gupta 				(uintptr_t)&state_pool[index];
156*d4d598e9SRuchika Gupta 		*dev_info = &dev_info_pool[index];
157*d4d598e9SRuchika Gupta 		++fip_dev_count;
158*d4d598e9SRuchika Gupta 	}
159*d4d598e9SRuchika Gupta 
160*d4d598e9SRuchika Gupta 	return result;
161*d4d598e9SRuchika Gupta }
162*d4d598e9SRuchika Gupta 
163*d4d598e9SRuchika Gupta /* Release a device info to the pool */
164*d4d598e9SRuchika Gupta static int free_dev_info(io_dev_info_t *dev_info)
165*d4d598e9SRuchika Gupta {
166*d4d598e9SRuchika Gupta 	int result;
167*d4d598e9SRuchika Gupta 	unsigned int index = 0;
168*d4d598e9SRuchika Gupta 	fip_dev_state_t *state;
169*d4d598e9SRuchika Gupta 
170*d4d598e9SRuchika Gupta 	assert(dev_info != NULL);
171*d4d598e9SRuchika Gupta 
172*d4d598e9SRuchika Gupta 	state = (fip_dev_state_t *)dev_info->info;
173*d4d598e9SRuchika Gupta 	result = find_first_fip_state(state->dev_spec, &index);
174*d4d598e9SRuchika Gupta 	if (result ==  0) {
175*d4d598e9SRuchika Gupta 		/* free if device info is valid */
176*d4d598e9SRuchika Gupta 		zeromem(state, sizeof(fip_dev_state_t));
177*d4d598e9SRuchika Gupta 		--fip_dev_count;
178*d4d598e9SRuchika Gupta 	}
179*d4d598e9SRuchika Gupta 
180*d4d598e9SRuchika Gupta 	return result;
181*d4d598e9SRuchika Gupta }
182*d4d598e9SRuchika Gupta 
183*d4d598e9SRuchika Gupta /*
184*d4d598e9SRuchika Gupta  * Multiple FIP devices can be opened depending on the value of
185*d4d598e9SRuchika Gupta  * MAX_FIP_DEVICES. Given that there is only one backend, only a
186*d4d598e9SRuchika Gupta  * single file can be open at a time by any FIP device.
187*d4d598e9SRuchika Gupta  */
188*d4d598e9SRuchika Gupta static int fip_dev_open(const uintptr_t dev_spec,
189fb037bfbSDan Handley 			 io_dev_info_t **dev_info)
190561cd33eSHarry Liebel {
191*d4d598e9SRuchika Gupta 	int result;
192*d4d598e9SRuchika Gupta 	io_dev_info_t *info;
193*d4d598e9SRuchika Gupta 	fip_dev_state_t *state;
194*d4d598e9SRuchika Gupta 
195561cd33eSHarry Liebel 	assert(dev_info != NULL);
196*d4d598e9SRuchika Gupta #if MAX_FIP_DEVICES > 1
197*d4d598e9SRuchika Gupta 	assert(dev_spec != (uintptr_t)NULL);
198*d4d598e9SRuchika Gupta #endif
199*d4d598e9SRuchika Gupta 
200*d4d598e9SRuchika Gupta 	result = allocate_dev_info(&info);
201*d4d598e9SRuchika Gupta 	if (result != 0)
202*d4d598e9SRuchika Gupta 		return -ENOMEM;
203*d4d598e9SRuchika Gupta 
204*d4d598e9SRuchika Gupta 	state = (fip_dev_state_t *)info->info;
205*d4d598e9SRuchika Gupta 
206*d4d598e9SRuchika Gupta 	state->dev_spec = dev_spec;
207*d4d598e9SRuchika Gupta 
208*d4d598e9SRuchika Gupta 	*dev_info = info;
209561cd33eSHarry Liebel 
210e098e244SJuan Castillo 	return 0;
211561cd33eSHarry Liebel }
212561cd33eSHarry Liebel 
213561cd33eSHarry Liebel 
214561cd33eSHarry Liebel /* Do some basic package checks. */
215625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
216561cd33eSHarry Liebel {
217e098e244SJuan Castillo 	int result;
21816948ae1SJuan Castillo 	unsigned int image_id = (unsigned int)init_params;
219625de1d4SDan Handley 	uintptr_t backend_handle;
220fb037bfbSDan Handley 	fip_toc_header_t header;
221561cd33eSHarry Liebel 	size_t bytes_read;
222561cd33eSHarry Liebel 
223561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
22416948ae1SJuan Castillo 	result = plat_get_image_source(image_id, &backend_dev_handle,
225561cd33eSHarry Liebel 				       &backend_image_spec);
226e098e244SJuan Castillo 	if (result != 0) {
22716948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
22816948ae1SJuan Castillo 			image_id, result);
229e098e244SJuan Castillo 		result = -ENOENT;
230561cd33eSHarry Liebel 		goto fip_dev_init_exit;
231561cd33eSHarry Liebel 	}
232561cd33eSHarry Liebel 
233561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
234561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
235561cd33eSHarry Liebel 			 &backend_handle);
236e098e244SJuan Castillo 	if (result != 0) {
23716948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n", image_id, result);
238e098e244SJuan Castillo 		result = -ENOENT;
239561cd33eSHarry Liebel 		goto fip_dev_init_exit;
240561cd33eSHarry Liebel 	}
241561cd33eSHarry Liebel 
242625de1d4SDan Handley 	result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
243625de1d4SDan Handley 			&bytes_read);
244e098e244SJuan Castillo 	if (result == 0) {
245561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
24608c28d53SJeenu Viswambharan 			WARN("Firmware Image Package header check failed.\n");
247e098e244SJuan Castillo 			result = -ENOENT;
248561cd33eSHarry Liebel 		} else {
2496ad2e461SDan Handley 			VERBOSE("FIP header looks OK.\n");
250561cd33eSHarry Liebel 		}
251561cd33eSHarry Liebel 	}
252561cd33eSHarry Liebel 
253561cd33eSHarry Liebel 	io_close(backend_handle);
254561cd33eSHarry Liebel 
255561cd33eSHarry Liebel  fip_dev_init_exit:
256561cd33eSHarry Liebel 	return result;
257561cd33eSHarry Liebel }
258561cd33eSHarry Liebel 
259561cd33eSHarry Liebel /* Close a connection to the FIP device */
260fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info)
261561cd33eSHarry Liebel {
262561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
263561cd33eSHarry Liebel 
264561cd33eSHarry Liebel 	/* Clear the backend. */
265625de1d4SDan Handley 	backend_dev_handle = (uintptr_t)NULL;
266625de1d4SDan Handley 	backend_image_spec = (uintptr_t)NULL;
267561cd33eSHarry Liebel 
268*d4d598e9SRuchika Gupta 	return free_dev_info(dev_info);
269561cd33eSHarry Liebel }
270561cd33eSHarry Liebel 
271561cd33eSHarry Liebel 
272561cd33eSHarry Liebel /* Open a file for access from package. */
273625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
274fb037bfbSDan Handley 			 io_entity_t *entity)
275561cd33eSHarry Liebel {
276e098e244SJuan Castillo 	int result;
277625de1d4SDan Handley 	uintptr_t backend_handle;
27816948ae1SJuan Castillo 	const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec;
279561cd33eSHarry Liebel 	size_t bytes_read;
280561cd33eSHarry Liebel 	int found_file = 0;
281561cd33eSHarry Liebel 
28216948ae1SJuan Castillo 	assert(uuid_spec != NULL);
283561cd33eSHarry Liebel 	assert(entity != NULL);
284561cd33eSHarry Liebel 
285561cd33eSHarry Liebel 	/* Can only have one file open at a time for the moment. We need to
286561cd33eSHarry Liebel 	 * track state like file cursor position. We know the header lives at
287561cd33eSHarry Liebel 	 * offset zero, so this entry should never be zero for an active file.
288561cd33eSHarry Liebel 	 * When the system supports dynamic memory allocation we can allow more
289561cd33eSHarry Liebel 	 * than one open file at a time if needed.
290561cd33eSHarry Liebel 	 */
291561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
29208c28d53SJeenu Viswambharan 		WARN("fip_file_open : Only one open file at a time.\n");
293e098e244SJuan Castillo 		return -ENOMEM;
294561cd33eSHarry Liebel 	}
295561cd33eSHarry Liebel 
296561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
297561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
298561cd33eSHarry Liebel 			 &backend_handle);
299e098e244SJuan Castillo 	if (result != 0) {
30008c28d53SJeenu Viswambharan 		WARN("Failed to open Firmware Image Package (%i)\n", result);
301e098e244SJuan Castillo 		result = -ENOENT;
302561cd33eSHarry Liebel 		goto fip_file_open_exit;
303561cd33eSHarry Liebel 	}
304561cd33eSHarry Liebel 
305561cd33eSHarry Liebel 	/* Seek past the FIP header into the Table of Contents */
306fb037bfbSDan Handley 	result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t));
307e098e244SJuan Castillo 	if (result != 0) {
30808c28d53SJeenu Viswambharan 		WARN("fip_file_open: failed to seek\n");
309e098e244SJuan Castillo 		result = -ENOENT;
310561cd33eSHarry Liebel 		goto fip_file_open_close;
311561cd33eSHarry Liebel 	}
312561cd33eSHarry Liebel 
313561cd33eSHarry Liebel 	found_file = 0;
314561cd33eSHarry Liebel 	do {
315625de1d4SDan Handley 		result = io_read(backend_handle,
316625de1d4SDan Handley 				 (uintptr_t)&current_file.entry,
317561cd33eSHarry Liebel 				 sizeof(current_file.entry),
318561cd33eSHarry Liebel 				 &bytes_read);
319e098e244SJuan Castillo 		if (result == 0) {
320561cd33eSHarry Liebel 			if (compare_uuids(&current_file.entry.uuid,
32116948ae1SJuan Castillo 					  &uuid_spec->uuid) == 0) {
322561cd33eSHarry Liebel 				found_file = 1;
323561cd33eSHarry Liebel 				break;
324561cd33eSHarry Liebel 			}
325561cd33eSHarry Liebel 		} else {
32608c28d53SJeenu Viswambharan 			WARN("Failed to read FIP (%i)\n", result);
327561cd33eSHarry Liebel 			goto fip_file_open_close;
328561cd33eSHarry Liebel 		}
329561cd33eSHarry Liebel 	} while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
330561cd33eSHarry Liebel 
331561cd33eSHarry Liebel 	if (found_file == 1) {
332561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
333561cd33eSHarry Liebel 		 * the file position to 0. The 'current_file.entry' holds the
334561cd33eSHarry Liebel 		 * base and size of the file.
335561cd33eSHarry Liebel 		 */
336561cd33eSHarry Liebel 		current_file.file_pos = 0;
337561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
338561cd33eSHarry Liebel 	} else {
339561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
340dd3dc32fSJeenu Viswambharan 		current_file.entry.offset_address = 0;
341e098e244SJuan Castillo 		result = -ENOENT;
342561cd33eSHarry Liebel 	}
343561cd33eSHarry Liebel 
344561cd33eSHarry Liebel  fip_file_open_close:
345561cd33eSHarry Liebel 	io_close(backend_handle);
346561cd33eSHarry Liebel 
347561cd33eSHarry Liebel  fip_file_open_exit:
348561cd33eSHarry Liebel 	return result;
349561cd33eSHarry Liebel }
350561cd33eSHarry Liebel 
351561cd33eSHarry Liebel 
352561cd33eSHarry Liebel /* Return the size of a file in package */
353fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length)
354561cd33eSHarry Liebel {
355561cd33eSHarry Liebel 	assert(entity != NULL);
356561cd33eSHarry Liebel 	assert(length != NULL);
357561cd33eSHarry Liebel 
358fb037bfbSDan Handley 	*length =  ((file_state_t *)entity->info)->entry.size;
359561cd33eSHarry Liebel 
360e098e244SJuan Castillo 	return 0;
361561cd33eSHarry Liebel }
362561cd33eSHarry Liebel 
363561cd33eSHarry Liebel 
364561cd33eSHarry Liebel /* Read data from a file in package */
365625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
366561cd33eSHarry Liebel 			  size_t *length_read)
367561cd33eSHarry Liebel {
368e098e244SJuan Castillo 	int result;
369fb037bfbSDan Handley 	file_state_t *fp;
370561cd33eSHarry Liebel 	size_t file_offset;
371561cd33eSHarry Liebel 	size_t bytes_read;
372625de1d4SDan Handley 	uintptr_t backend_handle;
373561cd33eSHarry Liebel 
374561cd33eSHarry Liebel 	assert(entity != NULL);
375625de1d4SDan Handley 	assert(buffer != (uintptr_t)NULL);
376561cd33eSHarry Liebel 	assert(length_read != NULL);
377625de1d4SDan Handley 	assert(entity->info != (uintptr_t)NULL);
378561cd33eSHarry Liebel 
379561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
380561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
381561cd33eSHarry Liebel 			 &backend_handle);
382e098e244SJuan Castillo 	if (result != 0) {
38308c28d53SJeenu Viswambharan 		WARN("Failed to open FIP (%i)\n", result);
384e098e244SJuan Castillo 		result = -ENOENT;
385561cd33eSHarry Liebel 		goto fip_file_read_exit;
386561cd33eSHarry Liebel 	}
387561cd33eSHarry Liebel 
388fb037bfbSDan Handley 	fp = (file_state_t *)entity->info;
389561cd33eSHarry Liebel 
390561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
391561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
392561cd33eSHarry Liebel 	result = io_seek(backend_handle, IO_SEEK_SET, file_offset);
393e098e244SJuan Castillo 	if (result != 0) {
39408c28d53SJeenu Viswambharan 		WARN("fip_file_read: failed to seek\n");
395e098e244SJuan Castillo 		result = -ENOENT;
396561cd33eSHarry Liebel 		goto fip_file_read_close;
397561cd33eSHarry Liebel 	}
398561cd33eSHarry Liebel 
399561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
400e098e244SJuan Castillo 	if (result != 0) {
401561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
40208c28d53SJeenu Viswambharan 		WARN("Failed to read payload (%i)\n", result);
403e098e244SJuan Castillo 		result = -ENOENT;
404561cd33eSHarry Liebel 		goto fip_file_read_close;
405561cd33eSHarry Liebel 	} else {
406561cd33eSHarry Liebel 		/* Set caller length and new file position. */
407561cd33eSHarry Liebel 		*length_read = bytes_read;
408561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
409561cd33eSHarry Liebel 	}
410561cd33eSHarry Liebel 
411561cd33eSHarry Liebel /* Close the backend. */
412561cd33eSHarry Liebel  fip_file_read_close:
413561cd33eSHarry Liebel 	io_close(backend_handle);
414561cd33eSHarry Liebel 
415561cd33eSHarry Liebel  fip_file_read_exit:
416561cd33eSHarry Liebel 	return result;
417561cd33eSHarry Liebel }
418561cd33eSHarry Liebel 
419561cd33eSHarry Liebel 
420561cd33eSHarry Liebel /* Close a file in package */
421fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity)
422561cd33eSHarry Liebel {
423561cd33eSHarry Liebel 	/* Clear our current file pointer.
424561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
425561cd33eSHarry Liebel 	 */
426561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
42732f0d3c6SDouglas Raillard 		zeromem(&current_file, sizeof(current_file));
428561cd33eSHarry Liebel 	}
429561cd33eSHarry Liebel 
430561cd33eSHarry Liebel 	/* Clear the Entity info. */
431561cd33eSHarry Liebel 	entity->info = 0;
432561cd33eSHarry Liebel 
433e098e244SJuan Castillo 	return 0;
434561cd33eSHarry Liebel }
435561cd33eSHarry Liebel 
436561cd33eSHarry Liebel /* Exported functions */
437561cd33eSHarry Liebel 
438561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
439625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con)
440561cd33eSHarry Liebel {
441e098e244SJuan Castillo 	int result;
442561cd33eSHarry Liebel 	assert(dev_con != NULL);
443561cd33eSHarry Liebel 
444*d4d598e9SRuchika Gupta 	/*
445*d4d598e9SRuchika Gupta 	 * Since dev_info isn't really used in io_register_device, always
446*d4d598e9SRuchika Gupta 	 * use the same device info at here instead.
447*d4d598e9SRuchika Gupta 	 */
448*d4d598e9SRuchika Gupta 	result = io_register_device(&dev_info_pool[0]);
449e098e244SJuan Castillo 	if (result == 0)
450561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
451561cd33eSHarry Liebel 
452561cd33eSHarry Liebel 	return result;
453561cd33eSHarry Liebel }
454