xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision 561cd33eceaff56a1c6cabe5a3e6e03e21e9dc9a)
1*561cd33eSHarry Liebel /*
2*561cd33eSHarry Liebel  * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3*561cd33eSHarry Liebel  *
4*561cd33eSHarry Liebel  * Redistribution and use in source and binary forms, with or without
5*561cd33eSHarry Liebel  * modification, are permitted provided that the following conditions are met:
6*561cd33eSHarry Liebel  *
7*561cd33eSHarry Liebel  * Redistributions of source code must retain the above copyright notice, this
8*561cd33eSHarry Liebel  * list of conditions and the following disclaimer.
9*561cd33eSHarry Liebel  *
10*561cd33eSHarry Liebel  * Redistributions in binary form must reproduce the above copyright notice,
11*561cd33eSHarry Liebel  * this list of conditions and the following disclaimer in the documentation
12*561cd33eSHarry Liebel  * and/or other materials provided with the distribution.
13*561cd33eSHarry Liebel  *
14*561cd33eSHarry Liebel  * Neither the name of ARM nor the names of its contributors may be used
15*561cd33eSHarry Liebel  * to endorse or promote products derived from this software without specific
16*561cd33eSHarry Liebel  * prior written permission.
17*561cd33eSHarry Liebel  *
18*561cd33eSHarry Liebel  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*561cd33eSHarry Liebel  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*561cd33eSHarry Liebel  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*561cd33eSHarry Liebel  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*561cd33eSHarry Liebel  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*561cd33eSHarry Liebel  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*561cd33eSHarry Liebel  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*561cd33eSHarry Liebel  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*561cd33eSHarry Liebel  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*561cd33eSHarry Liebel  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*561cd33eSHarry Liebel  * POSSIBILITY OF SUCH DAMAGE.
29*561cd33eSHarry Liebel  */
30*561cd33eSHarry Liebel 
31*561cd33eSHarry Liebel #include <stdint.h>
32*561cd33eSHarry Liebel #include <uuid.h>
33*561cd33eSHarry Liebel #include <errno.h>
34*561cd33eSHarry Liebel #include <string.h>
35*561cd33eSHarry Liebel #include <assert.h>
36*561cd33eSHarry Liebel #include "platform.h"
37*561cd33eSHarry Liebel #include "firmware_image_package.h"
38*561cd33eSHarry Liebel #include "io_storage.h"
39*561cd33eSHarry Liebel #include "io_driver.h"
40*561cd33eSHarry Liebel #include "io_fip.h"
41*561cd33eSHarry Liebel #include "debug.h"
42*561cd33eSHarry Liebel 
43*561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/
44*561cd33eSHarry Liebel #define PRINT_UUID2(x)								\
45*561cd33eSHarry Liebel 	"%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",	\
46*561cd33eSHarry Liebel 		x.time_low, x.time_mid, x.time_hi_and_version,			\
47*561cd33eSHarry Liebel 		x.clock_seq_hi_and_reserved, x.clock_seq_low,			\
48*561cd33eSHarry Liebel 		x.node[0], x.node[1], x.node[2], x.node[3],			\
49*561cd33eSHarry Liebel 		x.node[4], x.node[5]
50*561cd33eSHarry Liebel 
51*561cd33eSHarry Liebel typedef struct {
52*561cd33eSHarry Liebel 	const char	*name;
53*561cd33eSHarry Liebel 	const uuid_t	 uuid;
54*561cd33eSHarry Liebel } plat_fip_name_uuid;
55*561cd33eSHarry Liebel 
56*561cd33eSHarry Liebel typedef struct {
57*561cd33eSHarry Liebel 	/* Put file_pos above the struct to allow {0} on static init.
58*561cd33eSHarry Liebel 	 * It is a workaround for a known bug in GCC
59*561cd33eSHarry Liebel 	 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
60*561cd33eSHarry Liebel 	 */
61*561cd33eSHarry Liebel 	unsigned int file_pos;
62*561cd33eSHarry Liebel 	fip_toc_entry entry;
63*561cd33eSHarry Liebel } file_state;
64*561cd33eSHarry Liebel 
65*561cd33eSHarry Liebel static plat_fip_name_uuid name_uuid[] = {
66*561cd33eSHarry Liebel 	{BL2_IMAGE_NAME, UUID_TRUSTED_BOOT_FIRMWARE_BL2},
67*561cd33eSHarry Liebel 	{BL31_IMAGE_NAME, UUID_EL3_RUNTIME_FIRMWARE_BL31},
68*561cd33eSHarry Liebel 	{BL32_IMAGE_NAME, UUID_SECURE_PAYLOAD_BL32},
69*561cd33eSHarry Liebel 	{BL33_IMAGE_NAME, UUID_NON_TRUSTED_FIRMWARE_BL33},
70*561cd33eSHarry Liebel 	{NULL, {0} }
71*561cd33eSHarry Liebel };
72*561cd33eSHarry Liebel 
73*561cd33eSHarry Liebel static const uuid_t uuid_null = {0};
74*561cd33eSHarry Liebel static file_state current_file = {0};
75*561cd33eSHarry Liebel static io_dev_handle backend_dev_handle;
76*561cd33eSHarry Liebel static void *backend_image_spec;
77*561cd33eSHarry Liebel 
78*561cd33eSHarry Liebel 
79*561cd33eSHarry Liebel /* Firmware Image Package driver functions */
80*561cd33eSHarry Liebel static int fip_dev_open(void *spec, struct io_dev_info **dev_info);
81*561cd33eSHarry Liebel static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
82*561cd33eSHarry Liebel 			  struct io_entity *entity);
83*561cd33eSHarry Liebel static int fip_file_len(struct io_entity *entity, size_t *length);
84*561cd33eSHarry Liebel static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
85*561cd33eSHarry Liebel 			  size_t *length_read);
86*561cd33eSHarry Liebel static int fip_file_close(struct io_entity *entity);
87*561cd33eSHarry Liebel static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params);
88*561cd33eSHarry Liebel static int fip_dev_close(struct io_dev_info *dev_info);
89*561cd33eSHarry Liebel 
90*561cd33eSHarry Liebel 
91*561cd33eSHarry Liebel static inline int copy_uuid(uuid_t *dst, const uuid_t *src)
92*561cd33eSHarry Liebel {
93*561cd33eSHarry Liebel 	memcpy(dst, src, sizeof(uuid_t));
94*561cd33eSHarry Liebel 	return 0;
95*561cd33eSHarry Liebel }
96*561cd33eSHarry Liebel 
97*561cd33eSHarry Liebel 
98*561cd33eSHarry Liebel /* Return 0 for equal uuids. */
99*561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
100*561cd33eSHarry Liebel {
101*561cd33eSHarry Liebel 	return memcmp(uuid1, uuid2, sizeof(uuid_t));
102*561cd33eSHarry Liebel }
103*561cd33eSHarry Liebel 
104*561cd33eSHarry Liebel 
105*561cd33eSHarry Liebel /* TODO: We could check version numbers or do a package checksum? */
106*561cd33eSHarry Liebel static inline int is_valid_header(fip_toc_header *header)
107*561cd33eSHarry Liebel {
108*561cd33eSHarry Liebel 	if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
109*561cd33eSHarry Liebel 		return 1;
110*561cd33eSHarry Liebel 	} else {
111*561cd33eSHarry Liebel 		return 0;
112*561cd33eSHarry Liebel 	}
113*561cd33eSHarry Liebel }
114*561cd33eSHarry Liebel 
115*561cd33eSHarry Liebel 
116*561cd33eSHarry Liebel static int file_to_uuid(const char *filename, uuid_t *uuid)
117*561cd33eSHarry Liebel {
118*561cd33eSHarry Liebel 	int i;
119*561cd33eSHarry Liebel 	int status = -EINVAL;
120*561cd33eSHarry Liebel 
121*561cd33eSHarry Liebel 	for (i = 0; i < (sizeof(name_uuid)/sizeof(plat_fip_name_uuid)); i++) {
122*561cd33eSHarry Liebel 		if (strcmp(filename, name_uuid[i].name) == 0) {
123*561cd33eSHarry Liebel 			copy_uuid(uuid, &name_uuid[i].uuid);
124*561cd33eSHarry Liebel 			status = 0;
125*561cd33eSHarry Liebel 			break;
126*561cd33eSHarry Liebel 		}
127*561cd33eSHarry Liebel 	}
128*561cd33eSHarry Liebel 	return status;
129*561cd33eSHarry Liebel }
130*561cd33eSHarry Liebel 
131*561cd33eSHarry Liebel 
132*561cd33eSHarry Liebel /* Identify the device type as a virtual driver */
133*561cd33eSHarry Liebel io_type device_type_fip(void)
134*561cd33eSHarry Liebel {
135*561cd33eSHarry Liebel 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
136*561cd33eSHarry Liebel }
137*561cd33eSHarry Liebel 
138*561cd33eSHarry Liebel 
139*561cd33eSHarry Liebel static struct io_dev_connector fip_dev_connector = {
140*561cd33eSHarry Liebel 	.dev_open = fip_dev_open
141*561cd33eSHarry Liebel };
142*561cd33eSHarry Liebel 
143*561cd33eSHarry Liebel 
144*561cd33eSHarry Liebel static struct io_dev_funcs fip_dev_funcs = {
145*561cd33eSHarry Liebel 	.type = device_type_fip,
146*561cd33eSHarry Liebel 	.open = fip_file_open,
147*561cd33eSHarry Liebel 	.seek = NULL,
148*561cd33eSHarry Liebel 	.size = fip_file_len,
149*561cd33eSHarry Liebel 	.read = fip_file_read,
150*561cd33eSHarry Liebel 	.write = NULL,
151*561cd33eSHarry Liebel 	.close = fip_file_close,
152*561cd33eSHarry Liebel 	.dev_init = fip_dev_init,
153*561cd33eSHarry Liebel 	.dev_close = fip_dev_close,
154*561cd33eSHarry Liebel };
155*561cd33eSHarry Liebel 
156*561cd33eSHarry Liebel 
157*561cd33eSHarry Liebel static struct io_dev_info fip_dev_info = {
158*561cd33eSHarry Liebel 	.funcs = &fip_dev_funcs,
159*561cd33eSHarry Liebel 	.info = (uintptr_t)NULL
160*561cd33eSHarry Liebel };
161*561cd33eSHarry Liebel 
162*561cd33eSHarry Liebel 
163*561cd33eSHarry Liebel /* Open a connection to the FIP device */
164*561cd33eSHarry Liebel static int fip_dev_open(void *spec __attribute__((unused)),
165*561cd33eSHarry Liebel 			 struct io_dev_info **dev_info)
166*561cd33eSHarry Liebel {
167*561cd33eSHarry Liebel 	assert(dev_info != NULL);
168*561cd33eSHarry Liebel 	*dev_info = &fip_dev_info;
169*561cd33eSHarry Liebel 
170*561cd33eSHarry Liebel 	return IO_SUCCESS;
171*561cd33eSHarry Liebel }
172*561cd33eSHarry Liebel 
173*561cd33eSHarry Liebel 
174*561cd33eSHarry Liebel /* Do some basic package checks. */
175*561cd33eSHarry Liebel static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params)
176*561cd33eSHarry Liebel {
177*561cd33eSHarry Liebel 	int result = IO_FAIL;
178*561cd33eSHarry Liebel 	char *image_name = (char *)init_params;
179*561cd33eSHarry Liebel 	io_handle backend_handle;
180*561cd33eSHarry Liebel 	fip_toc_header header;
181*561cd33eSHarry Liebel 	size_t bytes_read;
182*561cd33eSHarry Liebel 
183*561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
184*561cd33eSHarry Liebel 	result = plat_get_image_source(image_name, &backend_dev_handle,
185*561cd33eSHarry Liebel 				       &backend_image_spec);
186*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
187*561cd33eSHarry Liebel 		ERROR("Failed to obtain reference to image '%s' (%i)\n",
188*561cd33eSHarry Liebel 			image_name, result);
189*561cd33eSHarry Liebel 		result = IO_FAIL;
190*561cd33eSHarry Liebel 		goto fip_dev_init_exit;
191*561cd33eSHarry Liebel 	}
192*561cd33eSHarry Liebel 
193*561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
194*561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
195*561cd33eSHarry Liebel 			 &backend_handle);
196*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
197*561cd33eSHarry Liebel 		ERROR("Failed to access image '%s' (%i)\n", image_name, result);
198*561cd33eSHarry Liebel 		result = IO_FAIL;
199*561cd33eSHarry Liebel 		goto fip_dev_init_exit;
200*561cd33eSHarry Liebel 	}
201*561cd33eSHarry Liebel 
202*561cd33eSHarry Liebel 	result = io_read(backend_handle, &header, sizeof(header), &bytes_read);
203*561cd33eSHarry Liebel 	if (result == IO_SUCCESS) {
204*561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
205*561cd33eSHarry Liebel 			ERROR("Firmware Image Package header check failed.\n");
206*561cd33eSHarry Liebel 			result = IO_FAIL;
207*561cd33eSHarry Liebel 		} else {
208*561cd33eSHarry Liebel 			INFO("FIP header looks OK.\n");
209*561cd33eSHarry Liebel 		}
210*561cd33eSHarry Liebel 	}
211*561cd33eSHarry Liebel 
212*561cd33eSHarry Liebel 	io_close(backend_handle);
213*561cd33eSHarry Liebel 
214*561cd33eSHarry Liebel  fip_dev_init_exit:
215*561cd33eSHarry Liebel 	return result;
216*561cd33eSHarry Liebel }
217*561cd33eSHarry Liebel 
218*561cd33eSHarry Liebel /* Close a connection to the FIP device */
219*561cd33eSHarry Liebel static int fip_dev_close(struct io_dev_info *dev_info)
220*561cd33eSHarry Liebel {
221*561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
222*561cd33eSHarry Liebel 
223*561cd33eSHarry Liebel 	/* Clear the backend. */
224*561cd33eSHarry Liebel 	backend_dev_handle = NULL;
225*561cd33eSHarry Liebel 	backend_image_spec = NULL;
226*561cd33eSHarry Liebel 
227*561cd33eSHarry Liebel 	return IO_SUCCESS;
228*561cd33eSHarry Liebel }
229*561cd33eSHarry Liebel 
230*561cd33eSHarry Liebel 
231*561cd33eSHarry Liebel /* Open a file for access from package. */
232*561cd33eSHarry Liebel static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
233*561cd33eSHarry Liebel 			 struct io_entity *entity)
234*561cd33eSHarry Liebel {
235*561cd33eSHarry Liebel 	int result = IO_FAIL;
236*561cd33eSHarry Liebel 	io_handle backend_handle;
237*561cd33eSHarry Liebel 	uuid_t file_uuid;
238*561cd33eSHarry Liebel 	const io_file_spec *file_spec = (io_file_spec *)spec;
239*561cd33eSHarry Liebel 	size_t bytes_read;
240*561cd33eSHarry Liebel 	int found_file = 0;
241*561cd33eSHarry Liebel 
242*561cd33eSHarry Liebel 	assert(file_spec != NULL);
243*561cd33eSHarry Liebel 	assert(entity != NULL);
244*561cd33eSHarry Liebel 
245*561cd33eSHarry Liebel 	/* Can only have one file open at a time for the moment. We need to
246*561cd33eSHarry Liebel 	 * track state like file cursor position. We know the header lives at
247*561cd33eSHarry Liebel 	 * offset zero, so this entry should never be zero for an active file.
248*561cd33eSHarry Liebel 	 * When the system supports dynamic memory allocation we can allow more
249*561cd33eSHarry Liebel 	 * than one open file at a time if needed.
250*561cd33eSHarry Liebel 	 */
251*561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
252*561cd33eSHarry Liebel 		ERROR("fip_file_open : Only one open file at a time.\n");
253*561cd33eSHarry Liebel 		return IO_RESOURCES_EXHAUSTED;
254*561cd33eSHarry Liebel 	}
255*561cd33eSHarry Liebel 
256*561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
257*561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
258*561cd33eSHarry Liebel 			 &backend_handle);
259*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
260*561cd33eSHarry Liebel 		ERROR("Failed to open Firmware Image Package (%i)\n", result);
261*561cd33eSHarry Liebel 		result = IO_FAIL;
262*561cd33eSHarry Liebel 		goto fip_file_open_exit;
263*561cd33eSHarry Liebel 	}
264*561cd33eSHarry Liebel 
265*561cd33eSHarry Liebel 	/* Seek past the FIP header into the Table of Contents */
266*561cd33eSHarry Liebel 	result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header));
267*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
268*561cd33eSHarry Liebel 		ERROR("fip_file_open: failed to seek\n");
269*561cd33eSHarry Liebel 		result = IO_FAIL;
270*561cd33eSHarry Liebel 		goto fip_file_open_close;
271*561cd33eSHarry Liebel 	}
272*561cd33eSHarry Liebel 
273*561cd33eSHarry Liebel 	file_to_uuid(file_spec->path, &file_uuid);
274*561cd33eSHarry Liebel 
275*561cd33eSHarry Liebel 	found_file = 0;
276*561cd33eSHarry Liebel 	do {
277*561cd33eSHarry Liebel 		result = io_read(backend_handle, &current_file.entry,
278*561cd33eSHarry Liebel 				 sizeof(current_file.entry),
279*561cd33eSHarry Liebel 				 &bytes_read);
280*561cd33eSHarry Liebel 		if (result == IO_SUCCESS) {
281*561cd33eSHarry Liebel 			if (compare_uuids(&current_file.entry.uuid,
282*561cd33eSHarry Liebel 					  &file_uuid) == 0) {
283*561cd33eSHarry Liebel 				found_file = 1;
284*561cd33eSHarry Liebel 				break;
285*561cd33eSHarry Liebel 			}
286*561cd33eSHarry Liebel 		} else {
287*561cd33eSHarry Liebel 			ERROR("Failed to read FIP (%i)\n", result);
288*561cd33eSHarry Liebel 			goto fip_file_open_close;
289*561cd33eSHarry Liebel 		}
290*561cd33eSHarry Liebel 	} while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
291*561cd33eSHarry Liebel 
292*561cd33eSHarry Liebel 	if (found_file == 1) {
293*561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
294*561cd33eSHarry Liebel 		 * the file position to 0. The 'current_file.entry' holds the
295*561cd33eSHarry Liebel 		 * base and size of the file.
296*561cd33eSHarry Liebel 		 */
297*561cd33eSHarry Liebel 		current_file.file_pos = 0;
298*561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
299*561cd33eSHarry Liebel 	} else {
300*561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
301*561cd33eSHarry Liebel 		result = IO_FAIL;
302*561cd33eSHarry Liebel 	}
303*561cd33eSHarry Liebel 
304*561cd33eSHarry Liebel  fip_file_open_close:
305*561cd33eSHarry Liebel 	io_close(backend_handle);
306*561cd33eSHarry Liebel 
307*561cd33eSHarry Liebel  fip_file_open_exit:
308*561cd33eSHarry Liebel 	return result;
309*561cd33eSHarry Liebel }
310*561cd33eSHarry Liebel 
311*561cd33eSHarry Liebel 
312*561cd33eSHarry Liebel /* Return the size of a file in package */
313*561cd33eSHarry Liebel static int fip_file_len(struct io_entity *entity, size_t *length)
314*561cd33eSHarry Liebel {
315*561cd33eSHarry Liebel 	assert(entity != NULL);
316*561cd33eSHarry Liebel 	assert(length != NULL);
317*561cd33eSHarry Liebel 
318*561cd33eSHarry Liebel 	*length =  ((file_state *)entity->info)->entry.size;
319*561cd33eSHarry Liebel 
320*561cd33eSHarry Liebel 	return IO_SUCCESS;
321*561cd33eSHarry Liebel }
322*561cd33eSHarry Liebel 
323*561cd33eSHarry Liebel 
324*561cd33eSHarry Liebel /* Read data from a file in package */
325*561cd33eSHarry Liebel static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
326*561cd33eSHarry Liebel 			  size_t *length_read)
327*561cd33eSHarry Liebel {
328*561cd33eSHarry Liebel 	int result = IO_FAIL;
329*561cd33eSHarry Liebel 	file_state *fp;
330*561cd33eSHarry Liebel 	size_t file_offset;
331*561cd33eSHarry Liebel 	size_t bytes_read;
332*561cd33eSHarry Liebel 	io_handle backend_handle;
333*561cd33eSHarry Liebel 
334*561cd33eSHarry Liebel 	assert(entity != NULL);
335*561cd33eSHarry Liebel 	assert(buffer != NULL);
336*561cd33eSHarry Liebel 	assert(length_read != NULL);
337*561cd33eSHarry Liebel 	assert((void *)entity->info != NULL);
338*561cd33eSHarry Liebel 
339*561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
340*561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
341*561cd33eSHarry Liebel 			 &backend_handle);
342*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
343*561cd33eSHarry Liebel 		ERROR("Failed to open FIP (%i)\n", result);
344*561cd33eSHarry Liebel 		result = IO_FAIL;
345*561cd33eSHarry Liebel 		goto fip_file_read_exit;
346*561cd33eSHarry Liebel 	}
347*561cd33eSHarry Liebel 
348*561cd33eSHarry Liebel 	fp = (file_state *)entity->info;
349*561cd33eSHarry Liebel 
350*561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
351*561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
352*561cd33eSHarry Liebel 	result = io_seek(backend_handle, IO_SEEK_SET, file_offset);
353*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
354*561cd33eSHarry Liebel 		ERROR("fip_file_read: failed to seek\n");
355*561cd33eSHarry Liebel 		result = IO_FAIL;
356*561cd33eSHarry Liebel 		goto fip_file_read_close;
357*561cd33eSHarry Liebel 	}
358*561cd33eSHarry Liebel 
359*561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
360*561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
361*561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
362*561cd33eSHarry Liebel 		ERROR("Failed to read payload (%i)\n", result);
363*561cd33eSHarry Liebel 		result = IO_FAIL;
364*561cd33eSHarry Liebel 		goto fip_file_read_close;
365*561cd33eSHarry Liebel 	} else {
366*561cd33eSHarry Liebel 		/* Set caller length and new file position. */
367*561cd33eSHarry Liebel 		*length_read = bytes_read;
368*561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
369*561cd33eSHarry Liebel 	}
370*561cd33eSHarry Liebel 
371*561cd33eSHarry Liebel /* Close the backend. */
372*561cd33eSHarry Liebel  fip_file_read_close:
373*561cd33eSHarry Liebel 	io_close(backend_handle);
374*561cd33eSHarry Liebel 
375*561cd33eSHarry Liebel  fip_file_read_exit:
376*561cd33eSHarry Liebel 	return result;
377*561cd33eSHarry Liebel }
378*561cd33eSHarry Liebel 
379*561cd33eSHarry Liebel 
380*561cd33eSHarry Liebel /* Close a file in package */
381*561cd33eSHarry Liebel static int fip_file_close(struct io_entity *entity)
382*561cd33eSHarry Liebel {
383*561cd33eSHarry Liebel 	/* Clear our current file pointer.
384*561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
385*561cd33eSHarry Liebel 	 */
386*561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
387*561cd33eSHarry Liebel 		memset(&current_file, 0, sizeof(current_file));
388*561cd33eSHarry Liebel 	}
389*561cd33eSHarry Liebel 
390*561cd33eSHarry Liebel 	/* Clear the Entity info. */
391*561cd33eSHarry Liebel 	entity->info = 0;
392*561cd33eSHarry Liebel 
393*561cd33eSHarry Liebel 	return IO_SUCCESS;
394*561cd33eSHarry Liebel }
395*561cd33eSHarry Liebel 
396*561cd33eSHarry Liebel /* Exported functions */
397*561cd33eSHarry Liebel 
398*561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
399*561cd33eSHarry Liebel int register_io_dev_fip(struct io_dev_connector **dev_con)
400*561cd33eSHarry Liebel {
401*561cd33eSHarry Liebel 	int result = IO_FAIL;
402*561cd33eSHarry Liebel 	assert(dev_con != NULL);
403*561cd33eSHarry Liebel 
404*561cd33eSHarry Liebel 	result = io_register_device(&fip_dev_info);
405*561cd33eSHarry Liebel 	if (result == IO_SUCCESS)
406*561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
407*561cd33eSHarry Liebel 
408*561cd33eSHarry Liebel 	return result;
409*561cd33eSHarry Liebel }
410