xref: /rk3399_ARM-atf/drivers/io/io_fip.c (revision 033648652f2d66abe2454a75ded891a47cb13446)
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 
22561cd33eSHarry Liebel /* Useful for printing UUIDs when debugging.*/
23561cd33eSHarry Liebel #define PRINT_UUID2(x)								\
24561cd33eSHarry Liebel 	"%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",	\
25561cd33eSHarry Liebel 		x.time_low, x.time_mid, x.time_hi_and_version,			\
26561cd33eSHarry Liebel 		x.clock_seq_hi_and_reserved, x.clock_seq_low,			\
27561cd33eSHarry Liebel 		x.node[0], x.node[1], x.node[2], x.node[3],			\
28561cd33eSHarry Liebel 		x.node[4], x.node[5]
29561cd33eSHarry Liebel 
30561cd33eSHarry Liebel typedef struct {
31561cd33eSHarry Liebel 	unsigned int file_pos;
32fb037bfbSDan Handley 	fip_toc_entry_t entry;
33fb037bfbSDan Handley } file_state_t;
34561cd33eSHarry Liebel 
35*03364865SRoberto Vargas static const uuid_t uuid_null = { {0} };
36fb037bfbSDan Handley static file_state_t current_file = {0};
37625de1d4SDan Handley static uintptr_t backend_dev_handle;
38625de1d4SDan Handley static uintptr_t backend_image_spec;
39561cd33eSHarry Liebel 
40561cd33eSHarry Liebel 
41561cd33eSHarry Liebel /* Firmware Image Package driver functions */
42625de1d4SDan Handley static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
43625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
44fb037bfbSDan Handley 			  io_entity_t *entity);
45fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length);
46625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
47561cd33eSHarry Liebel 			  size_t *length_read);
48fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity);
49625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
50fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info);
51561cd33eSHarry Liebel 
52561cd33eSHarry Liebel 
53561cd33eSHarry Liebel /* Return 0 for equal uuids. */
54561cd33eSHarry Liebel static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
55561cd33eSHarry Liebel {
56561cd33eSHarry Liebel 	return memcmp(uuid1, uuid2, sizeof(uuid_t));
57561cd33eSHarry Liebel }
58561cd33eSHarry Liebel 
59561cd33eSHarry Liebel 
60561cd33eSHarry Liebel /* TODO: We could check version numbers or do a package checksum? */
61fb037bfbSDan Handley static inline int is_valid_header(fip_toc_header_t *header)
62561cd33eSHarry Liebel {
63561cd33eSHarry Liebel 	if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
64561cd33eSHarry Liebel 		return 1;
65561cd33eSHarry Liebel 	} else {
66561cd33eSHarry Liebel 		return 0;
67561cd33eSHarry Liebel 	}
68561cd33eSHarry Liebel }
69561cd33eSHarry Liebel 
70561cd33eSHarry Liebel 
71561cd33eSHarry Liebel /* Identify the device type as a virtual driver */
727fabe1a8SRoberto Vargas static io_type_t device_type_fip(void)
73561cd33eSHarry Liebel {
74561cd33eSHarry Liebel 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
75561cd33eSHarry Liebel }
76561cd33eSHarry Liebel 
77561cd33eSHarry Liebel 
78625de1d4SDan Handley static const io_dev_connector_t fip_dev_connector = {
79561cd33eSHarry Liebel 	.dev_open = fip_dev_open
80561cd33eSHarry Liebel };
81561cd33eSHarry Liebel 
82561cd33eSHarry Liebel 
83625de1d4SDan Handley static const io_dev_funcs_t fip_dev_funcs = {
84561cd33eSHarry Liebel 	.type = device_type_fip,
85561cd33eSHarry Liebel 	.open = fip_file_open,
86561cd33eSHarry Liebel 	.seek = NULL,
87561cd33eSHarry Liebel 	.size = fip_file_len,
88561cd33eSHarry Liebel 	.read = fip_file_read,
89561cd33eSHarry Liebel 	.write = NULL,
90561cd33eSHarry Liebel 	.close = fip_file_close,
91561cd33eSHarry Liebel 	.dev_init = fip_dev_init,
92561cd33eSHarry Liebel 	.dev_close = fip_dev_close,
93561cd33eSHarry Liebel };
94561cd33eSHarry Liebel 
95561cd33eSHarry Liebel 
96625de1d4SDan Handley /* No state associated with this device so structure can be const */
97625de1d4SDan Handley static const io_dev_info_t fip_dev_info = {
98561cd33eSHarry Liebel 	.funcs = &fip_dev_funcs,
99561cd33eSHarry Liebel 	.info = (uintptr_t)NULL
100561cd33eSHarry Liebel };
101561cd33eSHarry Liebel 
102561cd33eSHarry Liebel 
103561cd33eSHarry Liebel /* Open a connection to the FIP device */
10465cd299fSSoren Brinkmann static int fip_dev_open(const uintptr_t dev_spec __unused,
105fb037bfbSDan Handley 			 io_dev_info_t **dev_info)
106561cd33eSHarry Liebel {
107561cd33eSHarry Liebel 	assert(dev_info != NULL);
108625de1d4SDan Handley 	*dev_info = (io_dev_info_t *)&fip_dev_info; /* cast away const */
109561cd33eSHarry Liebel 
110e098e244SJuan Castillo 	return 0;
111561cd33eSHarry Liebel }
112561cd33eSHarry Liebel 
113561cd33eSHarry Liebel 
114561cd33eSHarry Liebel /* Do some basic package checks. */
115625de1d4SDan Handley static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
116561cd33eSHarry Liebel {
117e098e244SJuan Castillo 	int result;
11816948ae1SJuan Castillo 	unsigned int image_id = (unsigned int)init_params;
119625de1d4SDan Handley 	uintptr_t backend_handle;
120fb037bfbSDan Handley 	fip_toc_header_t header;
121561cd33eSHarry Liebel 	size_t bytes_read;
122561cd33eSHarry Liebel 
123561cd33eSHarry Liebel 	/* Obtain a reference to the image by querying the platform layer */
12416948ae1SJuan Castillo 	result = plat_get_image_source(image_id, &backend_dev_handle,
125561cd33eSHarry Liebel 				       &backend_image_spec);
126e098e244SJuan Castillo 	if (result != 0) {
12716948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
12816948ae1SJuan Castillo 			image_id, result);
129e098e244SJuan Castillo 		result = -ENOENT;
130561cd33eSHarry Liebel 		goto fip_dev_init_exit;
131561cd33eSHarry Liebel 	}
132561cd33eSHarry Liebel 
133561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
134561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
135561cd33eSHarry Liebel 			 &backend_handle);
136e098e244SJuan Castillo 	if (result != 0) {
13716948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n", image_id, result);
138e098e244SJuan Castillo 		result = -ENOENT;
139561cd33eSHarry Liebel 		goto fip_dev_init_exit;
140561cd33eSHarry Liebel 	}
141561cd33eSHarry Liebel 
142625de1d4SDan Handley 	result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
143625de1d4SDan Handley 			&bytes_read);
144e098e244SJuan Castillo 	if (result == 0) {
145561cd33eSHarry Liebel 		if (!is_valid_header(&header)) {
14608c28d53SJeenu Viswambharan 			WARN("Firmware Image Package header check failed.\n");
147e098e244SJuan Castillo 			result = -ENOENT;
148561cd33eSHarry Liebel 		} else {
1496ad2e461SDan Handley 			VERBOSE("FIP header looks OK.\n");
150561cd33eSHarry Liebel 		}
151561cd33eSHarry Liebel 	}
152561cd33eSHarry Liebel 
153561cd33eSHarry Liebel 	io_close(backend_handle);
154561cd33eSHarry Liebel 
155561cd33eSHarry Liebel  fip_dev_init_exit:
156561cd33eSHarry Liebel 	return result;
157561cd33eSHarry Liebel }
158561cd33eSHarry Liebel 
159561cd33eSHarry Liebel /* Close a connection to the FIP device */
160fb037bfbSDan Handley static int fip_dev_close(io_dev_info_t *dev_info)
161561cd33eSHarry Liebel {
162561cd33eSHarry Liebel 	/* TODO: Consider tracking open files and cleaning them up here */
163561cd33eSHarry Liebel 
164561cd33eSHarry Liebel 	/* Clear the backend. */
165625de1d4SDan Handley 	backend_dev_handle = (uintptr_t)NULL;
166625de1d4SDan Handley 	backend_image_spec = (uintptr_t)NULL;
167561cd33eSHarry Liebel 
168e098e244SJuan Castillo 	return 0;
169561cd33eSHarry Liebel }
170561cd33eSHarry Liebel 
171561cd33eSHarry Liebel 
172561cd33eSHarry Liebel /* Open a file for access from package. */
173625de1d4SDan Handley static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
174fb037bfbSDan Handley 			 io_entity_t *entity)
175561cd33eSHarry Liebel {
176e098e244SJuan Castillo 	int result;
177625de1d4SDan Handley 	uintptr_t backend_handle;
17816948ae1SJuan Castillo 	const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec;
179561cd33eSHarry Liebel 	size_t bytes_read;
180561cd33eSHarry Liebel 	int found_file = 0;
181561cd33eSHarry Liebel 
18216948ae1SJuan Castillo 	assert(uuid_spec != NULL);
183561cd33eSHarry Liebel 	assert(entity != NULL);
184561cd33eSHarry Liebel 
185561cd33eSHarry Liebel 	/* Can only have one file open at a time for the moment. We need to
186561cd33eSHarry Liebel 	 * track state like file cursor position. We know the header lives at
187561cd33eSHarry Liebel 	 * offset zero, so this entry should never be zero for an active file.
188561cd33eSHarry Liebel 	 * When the system supports dynamic memory allocation we can allow more
189561cd33eSHarry Liebel 	 * than one open file at a time if needed.
190561cd33eSHarry Liebel 	 */
191561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
19208c28d53SJeenu Viswambharan 		WARN("fip_file_open : Only one open file at a time.\n");
193e098e244SJuan Castillo 		return -ENOMEM;
194561cd33eSHarry Liebel 	}
195561cd33eSHarry Liebel 
196561cd33eSHarry Liebel 	/* Attempt to access the FIP image */
197561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
198561cd33eSHarry Liebel 			 &backend_handle);
199e098e244SJuan Castillo 	if (result != 0) {
20008c28d53SJeenu Viswambharan 		WARN("Failed to open Firmware Image Package (%i)\n", result);
201e098e244SJuan Castillo 		result = -ENOENT;
202561cd33eSHarry Liebel 		goto fip_file_open_exit;
203561cd33eSHarry Liebel 	}
204561cd33eSHarry Liebel 
205561cd33eSHarry Liebel 	/* Seek past the FIP header into the Table of Contents */
206fb037bfbSDan Handley 	result = io_seek(backend_handle, IO_SEEK_SET, sizeof(fip_toc_header_t));
207e098e244SJuan Castillo 	if (result != 0) {
20808c28d53SJeenu Viswambharan 		WARN("fip_file_open: failed to seek\n");
209e098e244SJuan Castillo 		result = -ENOENT;
210561cd33eSHarry Liebel 		goto fip_file_open_close;
211561cd33eSHarry Liebel 	}
212561cd33eSHarry Liebel 
213561cd33eSHarry Liebel 	found_file = 0;
214561cd33eSHarry Liebel 	do {
215625de1d4SDan Handley 		result = io_read(backend_handle,
216625de1d4SDan Handley 				 (uintptr_t)&current_file.entry,
217561cd33eSHarry Liebel 				 sizeof(current_file.entry),
218561cd33eSHarry Liebel 				 &bytes_read);
219e098e244SJuan Castillo 		if (result == 0) {
220561cd33eSHarry Liebel 			if (compare_uuids(&current_file.entry.uuid,
22116948ae1SJuan Castillo 					  &uuid_spec->uuid) == 0) {
222561cd33eSHarry Liebel 				found_file = 1;
223561cd33eSHarry Liebel 				break;
224561cd33eSHarry Liebel 			}
225561cd33eSHarry Liebel 		} else {
22608c28d53SJeenu Viswambharan 			WARN("Failed to read FIP (%i)\n", result);
227561cd33eSHarry Liebel 			goto fip_file_open_close;
228561cd33eSHarry Liebel 		}
229561cd33eSHarry Liebel 	} while (compare_uuids(&current_file.entry.uuid, &uuid_null) != 0);
230561cd33eSHarry Liebel 
231561cd33eSHarry Liebel 	if (found_file == 1) {
232561cd33eSHarry Liebel 		/* All fine. Update entity info with file state and return. Set
233561cd33eSHarry Liebel 		 * the file position to 0. The 'current_file.entry' holds the
234561cd33eSHarry Liebel 		 * base and size of the file.
235561cd33eSHarry Liebel 		 */
236561cd33eSHarry Liebel 		current_file.file_pos = 0;
237561cd33eSHarry Liebel 		entity->info = (uintptr_t)&current_file;
238561cd33eSHarry Liebel 	} else {
239561cd33eSHarry Liebel 		/* Did not find the file in the FIP. */
240dd3dc32fSJeenu Viswambharan 		current_file.entry.offset_address = 0;
241e098e244SJuan Castillo 		result = -ENOENT;
242561cd33eSHarry Liebel 	}
243561cd33eSHarry Liebel 
244561cd33eSHarry Liebel  fip_file_open_close:
245561cd33eSHarry Liebel 	io_close(backend_handle);
246561cd33eSHarry Liebel 
247561cd33eSHarry Liebel  fip_file_open_exit:
248561cd33eSHarry Liebel 	return result;
249561cd33eSHarry Liebel }
250561cd33eSHarry Liebel 
251561cd33eSHarry Liebel 
252561cd33eSHarry Liebel /* Return the size of a file in package */
253fb037bfbSDan Handley static int fip_file_len(io_entity_t *entity, size_t *length)
254561cd33eSHarry Liebel {
255561cd33eSHarry Liebel 	assert(entity != NULL);
256561cd33eSHarry Liebel 	assert(length != NULL);
257561cd33eSHarry Liebel 
258fb037bfbSDan Handley 	*length =  ((file_state_t *)entity->info)->entry.size;
259561cd33eSHarry Liebel 
260e098e244SJuan Castillo 	return 0;
261561cd33eSHarry Liebel }
262561cd33eSHarry Liebel 
263561cd33eSHarry Liebel 
264561cd33eSHarry Liebel /* Read data from a file in package */
265625de1d4SDan Handley static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
266561cd33eSHarry Liebel 			  size_t *length_read)
267561cd33eSHarry Liebel {
268e098e244SJuan Castillo 	int result;
269fb037bfbSDan Handley 	file_state_t *fp;
270561cd33eSHarry Liebel 	size_t file_offset;
271561cd33eSHarry Liebel 	size_t bytes_read;
272625de1d4SDan Handley 	uintptr_t backend_handle;
273561cd33eSHarry Liebel 
274561cd33eSHarry Liebel 	assert(entity != NULL);
275625de1d4SDan Handley 	assert(buffer != (uintptr_t)NULL);
276561cd33eSHarry Liebel 	assert(length_read != NULL);
277625de1d4SDan Handley 	assert(entity->info != (uintptr_t)NULL);
278561cd33eSHarry Liebel 
279561cd33eSHarry Liebel 	/* Open the backend, attempt to access the blob image */
280561cd33eSHarry Liebel 	result = io_open(backend_dev_handle, backend_image_spec,
281561cd33eSHarry Liebel 			 &backend_handle);
282e098e244SJuan Castillo 	if (result != 0) {
28308c28d53SJeenu Viswambharan 		WARN("Failed to open FIP (%i)\n", result);
284e098e244SJuan Castillo 		result = -ENOENT;
285561cd33eSHarry Liebel 		goto fip_file_read_exit;
286561cd33eSHarry Liebel 	}
287561cd33eSHarry Liebel 
288fb037bfbSDan Handley 	fp = (file_state_t *)entity->info;
289561cd33eSHarry Liebel 
290561cd33eSHarry Liebel 	/* Seek to the position in the FIP where the payload lives */
291561cd33eSHarry Liebel 	file_offset = fp->entry.offset_address + fp->file_pos;
292561cd33eSHarry Liebel 	result = io_seek(backend_handle, IO_SEEK_SET, file_offset);
293e098e244SJuan Castillo 	if (result != 0) {
29408c28d53SJeenu Viswambharan 		WARN("fip_file_read: failed to seek\n");
295e098e244SJuan Castillo 		result = -ENOENT;
296561cd33eSHarry Liebel 		goto fip_file_read_close;
297561cd33eSHarry Liebel 	}
298561cd33eSHarry Liebel 
299561cd33eSHarry Liebel 	result = io_read(backend_handle, buffer, length, &bytes_read);
300e098e244SJuan Castillo 	if (result != 0) {
301561cd33eSHarry Liebel 		/* We cannot read our data. Fail. */
30208c28d53SJeenu Viswambharan 		WARN("Failed to read payload (%i)\n", result);
303e098e244SJuan Castillo 		result = -ENOENT;
304561cd33eSHarry Liebel 		goto fip_file_read_close;
305561cd33eSHarry Liebel 	} else {
306561cd33eSHarry Liebel 		/* Set caller length and new file position. */
307561cd33eSHarry Liebel 		*length_read = bytes_read;
308561cd33eSHarry Liebel 		fp->file_pos += bytes_read;
309561cd33eSHarry Liebel 	}
310561cd33eSHarry Liebel 
311561cd33eSHarry Liebel /* Close the backend. */
312561cd33eSHarry Liebel  fip_file_read_close:
313561cd33eSHarry Liebel 	io_close(backend_handle);
314561cd33eSHarry Liebel 
315561cd33eSHarry Liebel  fip_file_read_exit:
316561cd33eSHarry Liebel 	return result;
317561cd33eSHarry Liebel }
318561cd33eSHarry Liebel 
319561cd33eSHarry Liebel 
320561cd33eSHarry Liebel /* Close a file in package */
321fb037bfbSDan Handley static int fip_file_close(io_entity_t *entity)
322561cd33eSHarry Liebel {
323561cd33eSHarry Liebel 	/* Clear our current file pointer.
324561cd33eSHarry Liebel 	 * If we had malloc() we would free() here.
325561cd33eSHarry Liebel 	 */
326561cd33eSHarry Liebel 	if (current_file.entry.offset_address != 0) {
32732f0d3c6SDouglas Raillard 		zeromem(&current_file, sizeof(current_file));
328561cd33eSHarry Liebel 	}
329561cd33eSHarry Liebel 
330561cd33eSHarry Liebel 	/* Clear the Entity info. */
331561cd33eSHarry Liebel 	entity->info = 0;
332561cd33eSHarry Liebel 
333e098e244SJuan Castillo 	return 0;
334561cd33eSHarry Liebel }
335561cd33eSHarry Liebel 
336561cd33eSHarry Liebel /* Exported functions */
337561cd33eSHarry Liebel 
338561cd33eSHarry Liebel /* Register the Firmware Image Package driver with the IO abstraction */
339625de1d4SDan Handley int register_io_dev_fip(const io_dev_connector_t **dev_con)
340561cd33eSHarry Liebel {
341e098e244SJuan Castillo 	int result;
342561cd33eSHarry Liebel 	assert(dev_con != NULL);
343561cd33eSHarry Liebel 
344561cd33eSHarry Liebel 	result = io_register_device(&fip_dev_info);
345e098e244SJuan Castillo 	if (result == 0)
346561cd33eSHarry Liebel 		*dev_con = &fip_dev_connector;
347561cd33eSHarry Liebel 
348561cd33eSHarry Liebel 	return result;
349561cd33eSHarry Liebel }
350