xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision f15c59d7ac8b24d342cd20523388dceba9fabbaa)
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;
39*d471bd9cSjohpow01 } fip_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 
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  */
59*d471bd9cSjohpow01 static fip_file_state_t current_fip_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. */
compare_uuids(const uuid_t * uuid1,const uuid_t * uuid2)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 
is_valid_header(fip_toc_header_t * header)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 */
device_type_fip(void)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 */
find_first_fip_state(const uintptr_t dev_spec,unsigned int * index_out)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 */
allocate_dev_info(io_dev_info_t ** dev_info)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 */
free_dev_info(io_dev_info_t * dev_info)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  */
fip_dev_open(const uintptr_t dev_spec,io_dev_info_t ** dev_info)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. */
fip_dev_init(io_dev_info_t * dev_info,const uintptr_t init_params)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;
2235c380888SScott Branden 	fip_dev_state_t *state;
2245c380888SScott Branden 
2255c380888SScott Branden 	assert(dev_info != NULL);
2265c380888SScott Branden 
2275c380888SScott Branden 	state = (fip_dev_state_t *)dev_info->info;
228561cd33eSHarry Liebel 
229561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
23016948ae1SJuan Castillo 	result = plat_get_image_source(image_id, &backend_dev_handle,
231561cd33eSHarry Liebel 				       &backend_image_spec);
232e098e244SJuan Castillo 	if (result != 0) {
23316948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
23416948ae1SJuan Castillo 			image_id, result);
235e098e244SJuan Castillo 		result = -ENOENT;
236561cd33eSHarry Liebel 		goto fip_dev_init_exit;
237561cd33eSHarry Liebel 	}
238561cd33eSHarry Liebel 
239561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
240561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
241561cd33eSHarry Liebel 			 &backend_handle);
242e098e244SJuan Castillo 	if (result != 0) {
24316948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n", image_id, result);
244e098e244SJuan Castillo 		result = -ENOENT;
245561cd33eSHarry Liebel 		goto fip_dev_init_exit;
246561cd33eSHarry Liebel 	}
247561cd33eSHarry Liebel 
248625de1d4SDan Handley 	result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
249625de1d4SDan Handley 			&bytes_read);
250e098e244SJuan Castillo 	if (result == 0) {
251561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
25208c28d53SJeenu Viswambharan 			WARN("Firmware Image Package header check failed.\n");
253e098e244SJuan Castillo 			result = -ENOENT;
254561cd33eSHarry Liebel 		} else {
2556ad2e461SDan Handley 			VERBOSE("FIP header looks OK.\n");
2565c380888SScott Branden 			/*
2575c380888SScott Branden 			 * Store 16-bit Platform ToC flags field which occupies
2585c380888SScott Branden 			 * bits [32-47] in fip header.
2595c380888SScott Branden 			 */
2605c380888SScott Branden 			state->plat_toc_flag = (header.flags >> 32) & 0xffff;
261561cd33eSHarry Liebel 		}
262561cd33eSHarry Liebel 	}
263561cd33eSHarry Liebel 
264561cd33eSHarry Liebel 	io_close(backend_handle);
265561cd33eSHarry Liebel 
266561cd33eSHarry Liebel  fip_dev_init_exit:
267561cd33eSHarry Liebel 	return result;
268561cd33eSHarry Liebel }
269561cd33eSHarry Liebel 
270561cd33eSHarry Liebel /* Close a connection to the FIP device */
fip_dev_close(io_dev_info_t * dev_info)271fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info)
272561cd33eSHarry Liebel {
273561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
274561cd33eSHarry Liebel 
275561cd33eSHarry Liebel 	/* Clear the backend. */
276625de1d4SDan Handley 	backend_dev_handle = (uintptr_t)NULL;
277625de1d4SDan Handley 	backend_image_spec = (uintptr_t)NULL;
278561cd33eSHarry Liebel 
279d4d598e9SRuchika Gupta 	return free_dev_info(dev_info);
280561cd33eSHarry Liebel }
281561cd33eSHarry Liebel 
282561cd33eSHarry Liebel 
283561cd33eSHarry Liebel /* Open a file for access from package. */
fip_file_open(io_dev_info_t * dev_info,const uintptr_t spec,io_entity_t * entity)284625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
285fb037bfbSDan Handley 			 io_entity_t *entity)
286561cd33eSHarry Liebel {
287e098e244SJuan Castillo 	int result;
288625de1d4SDan Handley 	uintptr_t backend_handle;
28916948ae1SJuan Castillo 	const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec;
290*d471bd9cSjohpow01 	static const uuid_t uuid_null = { {0} }; /* Double braces for clang */
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 	 */
303*d471bd9cSjohpow01 	if (current_fip_file.entry.offset_address != 0U) {
30408c28d53SJeenu Viswambharan 		WARN("fip_file_open : Only one open file at a time.\n");
3056eb75a1aSMasahiro 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,
329*d471bd9cSjohpow01 				 (uintptr_t)&current_fip_file.entry,
330*d471bd9cSjohpow01 				 sizeof(current_fip_file.entry),
331561cd33eSHarry Liebel 				 &bytes_read);
332e098e244SJuan Castillo 		if (result == 0) {
333*d471bd9cSjohpow01 			if (compare_uuids(&current_fip_file.entry.uuid,
33416948ae1SJuan Castillo 					  &uuid_spec->uuid) == 0) {
335561cd33eSHarry Liebel 				found_file = 1;
336561cd33eSHarry Liebel 			}
337561cd33eSHarry Liebel 		} else {
33808c28d53SJeenu Viswambharan 			WARN("Failed to read FIP (%i)\n", result);
339561cd33eSHarry Liebel 			goto fip_file_open_close;
340561cd33eSHarry Liebel 		}
341*d471bd9cSjohpow01 	} while ((found_file == 0) &&
342*d471bd9cSjohpow01 			(compare_uuids(&current_fip_file.entry.uuid,
343*d471bd9cSjohpow01 				&uuid_null) != 0));
344561cd33eSHarry Liebel 
345561cd33eSHarry Liebel 	if (found_file == 1) {
346561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
347*d471bd9cSjohpow01 		 * the file position to 0. The 'current_fip_file.entry' holds
348*d471bd9cSjohpow01 		 * the base and size of the file.
349561cd33eSHarry Liebel 		 */
350*d471bd9cSjohpow01 		current_fip_file.file_pos = 0;
351*d471bd9cSjohpow01 		entity->info = (uintptr_t)&current_fip_file;
352561cd33eSHarry Liebel 	} else {
353561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
354*d471bd9cSjohpow01 		current_fip_file.entry.offset_address = 0;
355e098e244SJuan Castillo 		result = -ENOENT;
356561cd33eSHarry Liebel 	}
357561cd33eSHarry Liebel 
358561cd33eSHarry Liebel  fip_file_open_close:
359561cd33eSHarry Liebel 	io_close(backend_handle);
360561cd33eSHarry Liebel 
361561cd33eSHarry Liebel  fip_file_open_exit:
362561cd33eSHarry Liebel 	return result;
363561cd33eSHarry Liebel }
364561cd33eSHarry Liebel 
365561cd33eSHarry Liebel 
366561cd33eSHarry Liebel /* Return the size of a file in package */
fip_file_len(io_entity_t * entity,size_t * length)367fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length)
368561cd33eSHarry Liebel {
369561cd33eSHarry Liebel 	assert(entity != NULL);
370561cd33eSHarry Liebel 	assert(length != NULL);
371561cd33eSHarry Liebel 
372*d471bd9cSjohpow01 	*length =  ((fip_file_state_t *)entity->info)->entry.size;
373561cd33eSHarry Liebel 
374e098e244SJuan Castillo 	return 0;
375561cd33eSHarry Liebel }
376561cd33eSHarry Liebel 
377561cd33eSHarry Liebel 
378561cd33eSHarry Liebel /* Read data from a file in package */
fip_file_read(io_entity_t * entity,uintptr_t buffer,size_t length,size_t * length_read)379625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
380561cd33eSHarry Liebel 			  size_t *length_read)
381561cd33eSHarry Liebel {
382e098e244SJuan Castillo 	int result;
383*d471bd9cSjohpow01 	fip_file_state_t *fp;
384561cd33eSHarry Liebel 	size_t file_offset;
385561cd33eSHarry Liebel 	size_t bytes_read;
386625de1d4SDan Handley 	uintptr_t backend_handle;
387561cd33eSHarry Liebel 
388561cd33eSHarry Liebel 	assert(entity != NULL);
389561cd33eSHarry Liebel 	assert(length_read != NULL);
390625de1d4SDan Handley 	assert(entity->info != (uintptr_t)NULL);
391561cd33eSHarry Liebel 
392561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
393561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
394561cd33eSHarry Liebel 			 &backend_handle);
395e098e244SJuan Castillo 	if (result != 0) {
39608c28d53SJeenu Viswambharan 		WARN("Failed to open FIP (%i)\n", result);
397e098e244SJuan Castillo 		result = -ENOENT;
398561cd33eSHarry Liebel 		goto fip_file_read_exit;
399561cd33eSHarry Liebel 	}
400561cd33eSHarry Liebel 
401*d471bd9cSjohpow01 	fp = (fip_file_state_t *)entity->info;
402561cd33eSHarry Liebel 
403561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
404561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
40570cb0bffSYann Gautier 	result = io_seek(backend_handle, IO_SEEK_SET,
40670cb0bffSYann Gautier 			 (signed long long)file_offset);
407e098e244SJuan Castillo 	if (result != 0) {
40808c28d53SJeenu Viswambharan 		WARN("fip_file_read: failed to seek\n");
409e098e244SJuan Castillo 		result = -ENOENT;
410561cd33eSHarry Liebel 		goto fip_file_read_close;
411561cd33eSHarry Liebel 	}
412561cd33eSHarry Liebel 
413561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
414e098e244SJuan Castillo 	if (result != 0) {
415561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
41608c28d53SJeenu Viswambharan 		WARN("Failed to read payload (%i)\n", result);
417e098e244SJuan Castillo 		result = -ENOENT;
418561cd33eSHarry Liebel 		goto fip_file_read_close;
419561cd33eSHarry Liebel 	} else {
420561cd33eSHarry Liebel 		/* Set caller length and new file position. */
421561cd33eSHarry Liebel 		*length_read = bytes_read;
422561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
423561cd33eSHarry Liebel 	}
424561cd33eSHarry Liebel 
425561cd33eSHarry Liebel /* Close the backend. */
426561cd33eSHarry Liebel  fip_file_read_close:
427561cd33eSHarry Liebel 	io_close(backend_handle);
428561cd33eSHarry Liebel 
429561cd33eSHarry Liebel  fip_file_read_exit:
430561cd33eSHarry Liebel 	return result;
431561cd33eSHarry Liebel }
432561cd33eSHarry Liebel 
433561cd33eSHarry Liebel 
434561cd33eSHarry Liebel /* Close a file in package */
fip_file_close(io_entity_t * entity)435fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity)
436561cd33eSHarry Liebel {
437561cd33eSHarry Liebel 	/* Clear our current file pointer.
438561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
439561cd33eSHarry Liebel 	 */
440*d471bd9cSjohpow01 	if (current_fip_file.entry.offset_address != 0U) {
441*d471bd9cSjohpow01 		zeromem(&current_fip_file, sizeof(current_fip_file));
442561cd33eSHarry Liebel 	}
443561cd33eSHarry Liebel 
444561cd33eSHarry Liebel 	/* Clear the Entity info. */
445561cd33eSHarry Liebel 	entity->info = 0;
446561cd33eSHarry Liebel 
447e098e244SJuan Castillo 	return 0;
448561cd33eSHarry Liebel }
449561cd33eSHarry Liebel 
450561cd33eSHarry Liebel /* Exported functions */
451561cd33eSHarry Liebel 
452561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
register_io_dev_fip(const io_dev_connector_t ** dev_con)453625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con)
454561cd33eSHarry Liebel {
455e098e244SJuan Castillo 	int result;
456561cd33eSHarry Liebel 	assert(dev_con != NULL);
457561cd33eSHarry Liebel 
458d4d598e9SRuchika Gupta 	/*
459d4d598e9SRuchika Gupta 	 * Since dev_info isn't really used in io_register_device, always
460d4d598e9SRuchika Gupta 	 * use the same device info at here instead.
461d4d598e9SRuchika Gupta 	 */
462d4d598e9SRuchika Gupta 	result = io_register_device(&dev_info_pool[0]);
463e098e244SJuan Castillo 	if (result == 0)
464561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
465561cd33eSHarry Liebel 
466561cd33eSHarry Liebel 	return result;
467561cd33eSHarry Liebel }
4685c380888SScott Branden 
4695c380888SScott Branden /* Function to retrieve plat_toc_flags, previously saved in FIP dev */
fip_dev_get_plat_toc_flag(io_dev_info_t * dev_info,uint16_t * plat_toc_flag)4705c380888SScott Branden int fip_dev_get_plat_toc_flag(io_dev_info_t *dev_info, uint16_t *plat_toc_flag)
4715c380888SScott Branden {
4725c380888SScott Branden 	fip_dev_state_t *state;
4735c380888SScott Branden 
4745c380888SScott Branden 	assert(dev_info != NULL);
4755c380888SScott Branden 
4765c380888SScott Branden 	state = (fip_dev_state_t *)dev_info->info;
4775c380888SScott Branden 
4785c380888SScott Branden 	*plat_toc_flag =  state->plat_toc_flag;
4795c380888SScott Branden 
4805c380888SScott Branden 	return 0;
4815c380888SScott Branden }
482