xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision 493c8cb2ae937fb50c2ce53338c9034c5c6636e5)
1561cd33eSHarry Liebel /*
2561cd33eSHarry Liebel  * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
3561cd33eSHarry Liebel  *
4561cd33eSHarry Liebel  * Redistribution and use in source and binary forms, with or without
5561cd33eSHarry Liebel  * modification, are permitted provided that the following conditions are met:
6561cd33eSHarry Liebel  *
7561cd33eSHarry Liebel  * Redistributions of source code must retain the above copyright notice, this
8561cd33eSHarry Liebel  * list of conditions and the following disclaimer.
9561cd33eSHarry Liebel  *
10561cd33eSHarry Liebel  * Redistributions in binary form must reproduce the above copyright notice,
11561cd33eSHarry Liebel  * this list of conditions and the following disclaimer in the documentation
12561cd33eSHarry Liebel  * and/or other materials provided with the distribution.
13561cd33eSHarry Liebel  *
14561cd33eSHarry Liebel  * Neither the name of ARM nor the names of its contributors may be used
15561cd33eSHarry Liebel  * to endorse or promote products derived from this software without specific
16561cd33eSHarry Liebel  * prior written permission.
17561cd33eSHarry Liebel  *
18561cd33eSHarry Liebel  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19561cd33eSHarry Liebel  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20561cd33eSHarry Liebel  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21561cd33eSHarry Liebel  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22561cd33eSHarry Liebel  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23561cd33eSHarry Liebel  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24561cd33eSHarry Liebel  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25561cd33eSHarry Liebel  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26561cd33eSHarry Liebel  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27561cd33eSHarry Liebel  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28561cd33eSHarry Liebel  * POSSIBILITY OF SUCH DAMAGE.
29561cd33eSHarry Liebel  */
30561cd33eSHarry Liebel 
31561cd33eSHarry Liebel #include <stdint.h>
32561cd33eSHarry Liebel #include <uuid.h>
33561cd33eSHarry Liebel #include <errno.h>
34561cd33eSHarry Liebel #include <string.h>
35561cd33eSHarry Liebel #include <assert.h>
36561cd33eSHarry Liebel #include "platform.h"
37561cd33eSHarry Liebel #include "firmware_image_package.h"
38561cd33eSHarry Liebel #include "io_storage.h"
39561cd33eSHarry Liebel #include "io_driver.h"
40561cd33eSHarry Liebel #include "io_fip.h"
41561cd33eSHarry Liebel #include "debug.h"
42561cd33eSHarry Liebel 
43561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/
44561cd33eSHarry Liebel #define PRINT_UUID2(x)								\
45561cd33eSHarry Liebel 	"%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",	\
46561cd33eSHarry Liebel 		x.time_low, x.time_mid, x.time_hi_and_version,			\
47561cd33eSHarry Liebel 		x.clock_seq_hi_and_reserved, x.clock_seq_low,			\
48561cd33eSHarry Liebel 		x.node[0], x.node[1], x.node[2], x.node[3],			\
49561cd33eSHarry Liebel 		x.node[4], x.node[5]
50561cd33eSHarry Liebel 
51561cd33eSHarry Liebel typedef struct {
52561cd33eSHarry Liebel 	const char	*name;
53561cd33eSHarry Liebel 	const uuid_t	 uuid;
54561cd33eSHarry Liebel } plat_fip_name_uuid;
55561cd33eSHarry Liebel 
56561cd33eSHarry Liebel typedef struct {
57561cd33eSHarry Liebel 	/* Put file_pos above the struct to allow {0} on static init.
58561cd33eSHarry Liebel 	 * It is a workaround for a known bug in GCC
59561cd33eSHarry Liebel 	 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
60561cd33eSHarry Liebel 	 */
61561cd33eSHarry Liebel 	unsigned int file_pos;
62561cd33eSHarry Liebel 	fip_toc_entry entry;
63561cd33eSHarry Liebel } file_state;
64561cd33eSHarry Liebel 
65561cd33eSHarry Liebel static plat_fip_name_uuid name_uuid[] = {
66561cd33eSHarry Liebel 	{BL2_IMAGE_NAME, UUID_TRUSTED_BOOT_FIRMWARE_BL2},
67561cd33eSHarry Liebel 	{BL31_IMAGE_NAME, UUID_EL3_RUNTIME_FIRMWARE_BL31},
68561cd33eSHarry Liebel 	{BL32_IMAGE_NAME, UUID_SECURE_PAYLOAD_BL32},
69561cd33eSHarry Liebel 	{BL33_IMAGE_NAME, UUID_NON_TRUSTED_FIRMWARE_BL33},
70561cd33eSHarry Liebel };
71561cd33eSHarry Liebel 
72561cd33eSHarry Liebel static const uuid_t uuid_null = {0};
73561cd33eSHarry Liebel static file_state current_file = {0};
74561cd33eSHarry Liebel static io_dev_handle backend_dev_handle;
75561cd33eSHarry Liebel static void *backend_image_spec;
76561cd33eSHarry Liebel 
77561cd33eSHarry Liebel 
78561cd33eSHarry Liebel /* Firmware Image Package driver functions */
79561cd33eSHarry Liebel static int fip_dev_open(void *spec, struct io_dev_info **dev_info);
80561cd33eSHarry Liebel static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
81561cd33eSHarry Liebel 			  struct io_entity *entity);
82561cd33eSHarry Liebel static int fip_file_len(struct io_entity *entity, size_t *length);
83561cd33eSHarry Liebel static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
84561cd33eSHarry Liebel 			  size_t *length_read);
85561cd33eSHarry Liebel static int fip_file_close(struct io_entity *entity);
86561cd33eSHarry Liebel static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params);
87561cd33eSHarry Liebel static int fip_dev_close(struct io_dev_info *dev_info);
88561cd33eSHarry Liebel 
89561cd33eSHarry Liebel 
90561cd33eSHarry Liebel static inline int copy_uuid(uuid_t *dst, const uuid_t *src)
91561cd33eSHarry Liebel {
92561cd33eSHarry Liebel 	memcpy(dst, src, sizeof(uuid_t));
93561cd33eSHarry Liebel 	return 0;
94561cd33eSHarry Liebel }
95561cd33eSHarry Liebel 
96561cd33eSHarry Liebel 
97561cd33eSHarry Liebel /* Return 0 for equal uuids. */
98561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
99561cd33eSHarry Liebel {
100561cd33eSHarry Liebel 	return memcmp(uuid1, uuid2, sizeof(uuid_t));
101561cd33eSHarry Liebel }
102561cd33eSHarry Liebel 
103561cd33eSHarry Liebel 
104561cd33eSHarry Liebel /* TODO: We could check version numbers or do a package checksum? */
105561cd33eSHarry Liebel static inline int is_valid_header(fip_toc_header *header)
106561cd33eSHarry Liebel {
107561cd33eSHarry Liebel 	if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
108561cd33eSHarry Liebel 		return 1;
109561cd33eSHarry Liebel 	} else {
110561cd33eSHarry Liebel 		return 0;
111561cd33eSHarry Liebel 	}
112561cd33eSHarry Liebel }
113561cd33eSHarry Liebel 
114561cd33eSHarry Liebel 
115561cd33eSHarry Liebel static int file_to_uuid(const char *filename, uuid_t *uuid)
116561cd33eSHarry Liebel {
117561cd33eSHarry Liebel 	int i;
118561cd33eSHarry Liebel 	int status = -EINVAL;
119561cd33eSHarry Liebel 
120*493c8cb2SSandrine Bailleux 	for (i = 0; i < (sizeof(name_uuid) / sizeof(name_uuid[0])); i++) {
121561cd33eSHarry Liebel 		if (strcmp(filename, name_uuid[i].name) == 0) {
122561cd33eSHarry Liebel 			copy_uuid(uuid, &name_uuid[i].uuid);
123561cd33eSHarry Liebel 			status = 0;
124561cd33eSHarry Liebel 			break;
125561cd33eSHarry Liebel 		}
126561cd33eSHarry Liebel 	}
127561cd33eSHarry Liebel 	return status;
128561cd33eSHarry Liebel }
129561cd33eSHarry Liebel 
130561cd33eSHarry Liebel 
131561cd33eSHarry Liebel /* Identify the device type as a virtual driver */
132561cd33eSHarry Liebel io_type device_type_fip(void)
133561cd33eSHarry Liebel {
134561cd33eSHarry Liebel 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
135561cd33eSHarry Liebel }
136561cd33eSHarry Liebel 
137561cd33eSHarry Liebel 
138561cd33eSHarry Liebel static struct io_dev_connector fip_dev_connector = {
139561cd33eSHarry Liebel 	.dev_open = fip_dev_open
140561cd33eSHarry Liebel };
141561cd33eSHarry Liebel 
142561cd33eSHarry Liebel 
143561cd33eSHarry Liebel static struct io_dev_funcs fip_dev_funcs = {
144561cd33eSHarry Liebel 	.type = device_type_fip,
145561cd33eSHarry Liebel 	.open = fip_file_open,
146561cd33eSHarry Liebel 	.seek = NULL,
147561cd33eSHarry Liebel 	.size = fip_file_len,
148561cd33eSHarry Liebel 	.read = fip_file_read,
149561cd33eSHarry Liebel 	.write = NULL,
150561cd33eSHarry Liebel 	.close = fip_file_close,
151561cd33eSHarry Liebel 	.dev_init = fip_dev_init,
152561cd33eSHarry Liebel 	.dev_close = fip_dev_close,
153561cd33eSHarry Liebel };
154561cd33eSHarry Liebel 
155561cd33eSHarry Liebel 
156561cd33eSHarry Liebel static struct io_dev_info fip_dev_info = {
157561cd33eSHarry Liebel 	.funcs = &fip_dev_funcs,
158561cd33eSHarry Liebel 	.info = (uintptr_t)NULL
159561cd33eSHarry Liebel };
160561cd33eSHarry Liebel 
161561cd33eSHarry Liebel 
162561cd33eSHarry Liebel /* Open a connection to the FIP device */
163561cd33eSHarry Liebel static int fip_dev_open(void *spec __attribute__((unused)),
164561cd33eSHarry Liebel 			 struct io_dev_info **dev_info)
165561cd33eSHarry Liebel {
166561cd33eSHarry Liebel 	assert(dev_info != NULL);
167561cd33eSHarry Liebel 	*dev_info = &fip_dev_info;
168561cd33eSHarry Liebel 
169561cd33eSHarry Liebel 	return IO_SUCCESS;
170561cd33eSHarry Liebel }
171561cd33eSHarry Liebel 
172561cd33eSHarry Liebel 
173561cd33eSHarry Liebel /* Do some basic package checks. */
174561cd33eSHarry Liebel static int fip_dev_init(struct io_dev_info *dev_info, const void *init_params)
175561cd33eSHarry Liebel {
176561cd33eSHarry Liebel 	int result = IO_FAIL;
177561cd33eSHarry Liebel 	char *image_name = (char *)init_params;
178561cd33eSHarry Liebel 	io_handle backend_handle;
179561cd33eSHarry Liebel 	fip_toc_header header;
180561cd33eSHarry Liebel 	size_t bytes_read;
181561cd33eSHarry Liebel 
182561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
183561cd33eSHarry Liebel 	result = plat_get_image_source(image_name, &backend_dev_handle,
184561cd33eSHarry Liebel 				       &backend_image_spec);
185561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
18608c28d53SJeenu Viswambharan 		WARN("Failed to obtain reference to image '%s' (%i)\n",
187561cd33eSHarry Liebel 			image_name, result);
188561cd33eSHarry Liebel 		result = IO_FAIL;
189561cd33eSHarry Liebel 		goto fip_dev_init_exit;
190561cd33eSHarry Liebel 	}
191561cd33eSHarry Liebel 
192561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
193561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
194561cd33eSHarry Liebel 			 &backend_handle);
195561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
19608c28d53SJeenu Viswambharan 		WARN("Failed to access image '%s' (%i)\n", image_name, result);
197561cd33eSHarry Liebel 		result = IO_FAIL;
198561cd33eSHarry Liebel 		goto fip_dev_init_exit;
199561cd33eSHarry Liebel 	}
200561cd33eSHarry Liebel 
201561cd33eSHarry Liebel 	result = io_read(backend_handle, &header, sizeof(header), &bytes_read);
202561cd33eSHarry Liebel 	if (result == IO_SUCCESS) {
203561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
20408c28d53SJeenu Viswambharan 			WARN("Firmware Image Package header check failed.\n");
205561cd33eSHarry Liebel 			result = IO_FAIL;
206561cd33eSHarry Liebel 		} else {
207561cd33eSHarry Liebel 			INFO("FIP header looks OK.\n");
208561cd33eSHarry Liebel 		}
209561cd33eSHarry Liebel 	}
210561cd33eSHarry Liebel 
211561cd33eSHarry Liebel 	io_close(backend_handle);
212561cd33eSHarry Liebel 
213561cd33eSHarry Liebel  fip_dev_init_exit:
214561cd33eSHarry Liebel 	return result;
215561cd33eSHarry Liebel }
216561cd33eSHarry Liebel 
217561cd33eSHarry Liebel /* Close a connection to the FIP device */
218561cd33eSHarry Liebel static int fip_dev_close(struct io_dev_info *dev_info)
219561cd33eSHarry Liebel {
220561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
221561cd33eSHarry Liebel 
222561cd33eSHarry Liebel 	/* Clear the backend. */
223561cd33eSHarry Liebel 	backend_dev_handle = NULL;
224561cd33eSHarry Liebel 	backend_image_spec = NULL;
225561cd33eSHarry Liebel 
226561cd33eSHarry Liebel 	return IO_SUCCESS;
227561cd33eSHarry Liebel }
228561cd33eSHarry Liebel 
229561cd33eSHarry Liebel 
230561cd33eSHarry Liebel /* Open a file for access from package. */
231561cd33eSHarry Liebel static int fip_file_open(struct io_dev_info *dev_info, const void *spec,
232561cd33eSHarry Liebel 			 struct io_entity *entity)
233561cd33eSHarry Liebel {
234561cd33eSHarry Liebel 	int result = IO_FAIL;
235561cd33eSHarry Liebel 	io_handle backend_handle;
236561cd33eSHarry Liebel 	uuid_t file_uuid;
237561cd33eSHarry Liebel 	const io_file_spec *file_spec = (io_file_spec *)spec;
238561cd33eSHarry Liebel 	size_t bytes_read;
239561cd33eSHarry Liebel 	int found_file = 0;
240561cd33eSHarry Liebel 
241561cd33eSHarry Liebel 	assert(file_spec != NULL);
242561cd33eSHarry Liebel 	assert(entity != NULL);
243561cd33eSHarry Liebel 
244561cd33eSHarry Liebel 	/* Can only have one file open at a time for the moment. We need to
245561cd33eSHarry Liebel 	 * track state like file cursor position. We know the header lives at
246561cd33eSHarry Liebel 	 * offset zero, so this entry should never be zero for an active file.
247561cd33eSHarry Liebel 	 * When the system supports dynamic memory allocation we can allow more
248561cd33eSHarry Liebel 	 * than one open file at a time if needed.
249561cd33eSHarry Liebel 	 */
250561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
25108c28d53SJeenu Viswambharan 		WARN("fip_file_open : Only one open file at a time.\n");
252561cd33eSHarry Liebel 		return IO_RESOURCES_EXHAUSTED;
253561cd33eSHarry Liebel 	}
254561cd33eSHarry Liebel 
255561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
256561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
257561cd33eSHarry Liebel 			 &backend_handle);
258561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
25908c28d53SJeenu Viswambharan 		WARN("Failed to open Firmware Image Package (%i)\n", result);
260561cd33eSHarry Liebel 		result = IO_FAIL;
261561cd33eSHarry Liebel 		goto fip_file_open_exit;
262561cd33eSHarry Liebel 	}
263561cd33eSHarry Liebel 
264561cd33eSHarry Liebel 	/* Seek past the FIP header into the Table of Contents */
265561cd33eSHarry Liebel 	result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header));
266561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
26708c28d53SJeenu Viswambharan 		WARN("fip_file_open: failed to seek\n");
268561cd33eSHarry Liebel 		result = IO_FAIL;
269561cd33eSHarry Liebel 		goto fip_file_open_close;
270561cd33eSHarry Liebel 	}
271561cd33eSHarry Liebel 
272561cd33eSHarry Liebel 	file_to_uuid(file_spec->path, &file_uuid);
273561cd33eSHarry Liebel 
274561cd33eSHarry Liebel 	found_file = 0;
275561cd33eSHarry Liebel 	do {
276561cd33eSHarry Liebel 		result = io_read(backend_handle, &current_file.entry,
277561cd33eSHarry Liebel 				 sizeof(current_file.entry),
278561cd33eSHarry Liebel 				 &bytes_read);
279561cd33eSHarry Liebel 		if (result == IO_SUCCESS) {
280561cd33eSHarry Liebel 			if (compare_uuids(&current_file.entry.uuid,
281561cd33eSHarry Liebel 					  &file_uuid) == 0) {
282561cd33eSHarry Liebel 				found_file = 1;
283561cd33eSHarry Liebel 				break;
284561cd33eSHarry Liebel 			}
285561cd33eSHarry Liebel 		} else {
28608c28d53SJeenu Viswambharan 			WARN("Failed to read FIP (%i)\n", result);
287561cd33eSHarry Liebel 			goto fip_file_open_close;
288561cd33eSHarry Liebel 		}
289561cd33eSHarry Liebel 	} while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
290561cd33eSHarry Liebel 
291561cd33eSHarry Liebel 	if (found_file == 1) {
292561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
293561cd33eSHarry Liebel 		 * the file position to 0. The 'current_file.entry' holds the
294561cd33eSHarry Liebel 		 * base and size of the file.
295561cd33eSHarry Liebel 		 */
296561cd33eSHarry Liebel 		current_file.file_pos = 0;
297561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
298561cd33eSHarry Liebel 	} else {
299561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
300dd3dc32fSJeenu Viswambharan 		current_file.entry.offset_address = 0;
301561cd33eSHarry Liebel 		result = IO_FAIL;
302561cd33eSHarry Liebel 	}
303561cd33eSHarry Liebel 
304561cd33eSHarry Liebel  fip_file_open_close:
305561cd33eSHarry Liebel 	io_close(backend_handle);
306561cd33eSHarry Liebel 
307561cd33eSHarry Liebel  fip_file_open_exit:
308561cd33eSHarry Liebel 	return result;
309561cd33eSHarry Liebel }
310561cd33eSHarry Liebel 
311561cd33eSHarry Liebel 
312561cd33eSHarry Liebel /* Return the size of a file in package */
313561cd33eSHarry Liebel static int fip_file_len(struct io_entity *entity, size_t *length)
314561cd33eSHarry Liebel {
315561cd33eSHarry Liebel 	assert(entity != NULL);
316561cd33eSHarry Liebel 	assert(length != NULL);
317561cd33eSHarry Liebel 
318561cd33eSHarry Liebel 	*length =  ((file_state *)entity->info)->entry.size;
319561cd33eSHarry Liebel 
320561cd33eSHarry Liebel 	return IO_SUCCESS;
321561cd33eSHarry Liebel }
322561cd33eSHarry Liebel 
323561cd33eSHarry Liebel 
324561cd33eSHarry Liebel /* Read data from a file in package */
325561cd33eSHarry Liebel static int fip_file_read(struct io_entity *entity, void *buffer, size_t length,
326561cd33eSHarry Liebel 			  size_t *length_read)
327561cd33eSHarry Liebel {
328561cd33eSHarry Liebel 	int result = IO_FAIL;
329561cd33eSHarry Liebel 	file_state *fp;
330561cd33eSHarry Liebel 	size_t file_offset;
331561cd33eSHarry Liebel 	size_t bytes_read;
332561cd33eSHarry Liebel 	io_handle backend_handle;
333561cd33eSHarry Liebel 
334561cd33eSHarry Liebel 	assert(entity != NULL);
335561cd33eSHarry Liebel 	assert(buffer != NULL);
336561cd33eSHarry Liebel 	assert(length_read != NULL);
337561cd33eSHarry Liebel 	assert((void *)entity->info != NULL);
338561cd33eSHarry Liebel 
339561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
340561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
341561cd33eSHarry Liebel 			 &backend_handle);
342561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
34308c28d53SJeenu Viswambharan 		WARN("Failed to open FIP (%i)\n", result);
344561cd33eSHarry Liebel 		result = IO_FAIL;
345561cd33eSHarry Liebel 		goto fip_file_read_exit;
346561cd33eSHarry Liebel 	}
347561cd33eSHarry Liebel 
348561cd33eSHarry Liebel 	fp = (file_state *)entity->info;
349561cd33eSHarry Liebel 
350561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
351561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
352561cd33eSHarry Liebel 	result = io_seek(backend_handle, IO_SEEK_SET, file_offset);
353561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
35408c28d53SJeenu Viswambharan 		WARN("fip_file_read: failed to seek\n");
355561cd33eSHarry Liebel 		result = IO_FAIL;
356561cd33eSHarry Liebel 		goto fip_file_read_close;
357561cd33eSHarry Liebel 	}
358561cd33eSHarry Liebel 
359561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
360561cd33eSHarry Liebel 	if (result != IO_SUCCESS) {
361561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
36208c28d53SJeenu Viswambharan 		WARN("Failed to read payload (%i)\n", result);
363561cd33eSHarry Liebel 		result = IO_FAIL;
364561cd33eSHarry Liebel 		goto fip_file_read_close;
365561cd33eSHarry Liebel 	} else {
366561cd33eSHarry Liebel 		/* Set caller length and new file position. */
367561cd33eSHarry Liebel 		*length_read = bytes_read;
368561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
369561cd33eSHarry Liebel 	}
370561cd33eSHarry Liebel 
371561cd33eSHarry Liebel /* Close the backend. */
372561cd33eSHarry Liebel  fip_file_read_close:
373561cd33eSHarry Liebel 	io_close(backend_handle);
374561cd33eSHarry Liebel 
375561cd33eSHarry Liebel  fip_file_read_exit:
376561cd33eSHarry Liebel 	return result;
377561cd33eSHarry Liebel }
378561cd33eSHarry Liebel 
379561cd33eSHarry Liebel 
380561cd33eSHarry Liebel /* Close a file in package */
381561cd33eSHarry Liebel static int fip_file_close(struct io_entity *entity)
382561cd33eSHarry Liebel {
383561cd33eSHarry Liebel 	/* Clear our current file pointer.
384561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
385561cd33eSHarry Liebel 	 */
386561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
387561cd33eSHarry Liebel 		memset(&current_file, 0, sizeof(current_file));
388561cd33eSHarry Liebel 	}
389561cd33eSHarry Liebel 
390561cd33eSHarry Liebel 	/* Clear the Entity info. */
391561cd33eSHarry Liebel 	entity->info = 0;
392561cd33eSHarry Liebel 
393561cd33eSHarry Liebel 	return IO_SUCCESS;
394561cd33eSHarry Liebel }
395561cd33eSHarry Liebel 
396561cd33eSHarry Liebel /* Exported functions */
397561cd33eSHarry Liebel 
398561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
399561cd33eSHarry Liebel int register_io_dev_fip(struct io_dev_connector **dev_con)
400561cd33eSHarry Liebel {
401561cd33eSHarry Liebel 	int result = IO_FAIL;
402561cd33eSHarry Liebel 	assert(dev_con != NULL);
403561cd33eSHarry Liebel 
404561cd33eSHarry Liebel 	result = io_register_device(&fip_dev_info);
405561cd33eSHarry Liebel 	if (result == IO_SUCCESS)
406561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
407561cd33eSHarry Liebel 
408561cd33eSHarry Liebel 	return result;
409561cd33eSHarry Liebel }
410