1*54fd6939SJiyong Park /*
2*54fd6939SJiyong Park * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
3*54fd6939SJiyong Park *
4*54fd6939SJiyong Park * SPDX-License-Identifier: BSD-3-Clause
5*54fd6939SJiyong Park */
6*54fd6939SJiyong Park
7*54fd6939SJiyong Park #include <assert.h>
8*54fd6939SJiyong Park #include <errno.h>
9*54fd6939SJiyong Park #include <stdint.h>
10*54fd6939SJiyong Park #include <string.h>
11*54fd6939SJiyong Park
12*54fd6939SJiyong Park #include <platform_def.h>
13*54fd6939SJiyong Park
14*54fd6939SJiyong Park #include <common/bl_common.h>
15*54fd6939SJiyong Park #include <common/debug.h>
16*54fd6939SJiyong Park #include <drivers/io/io_driver.h>
17*54fd6939SJiyong Park #include <drivers/io/io_fip.h>
18*54fd6939SJiyong Park #include <drivers/io/io_storage.h>
19*54fd6939SJiyong Park #include <lib/utils.h>
20*54fd6939SJiyong Park #include <plat/common/platform.h>
21*54fd6939SJiyong Park #include <tools_share/firmware_image_package.h>
22*54fd6939SJiyong Park #include <tools_share/uuid.h>
23*54fd6939SJiyong Park
24*54fd6939SJiyong Park #ifndef MAX_FIP_DEVICES
25*54fd6939SJiyong Park #define MAX_FIP_DEVICES 1
26*54fd6939SJiyong Park #endif
27*54fd6939SJiyong Park
28*54fd6939SJiyong Park /* Useful for printing UUIDs when debugging.*/
29*54fd6939SJiyong Park #define PRINT_UUID2(x) \
30*54fd6939SJiyong Park "%08x-%04hx-%04hx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", \
31*54fd6939SJiyong Park x.time_low, x.time_mid, x.time_hi_and_version, \
32*54fd6939SJiyong Park x.clock_seq_hi_and_reserved, x.clock_seq_low, \
33*54fd6939SJiyong Park x.node[0], x.node[1], x.node[2], x.node[3], \
34*54fd6939SJiyong Park x.node[4], x.node[5]
35*54fd6939SJiyong Park
36*54fd6939SJiyong Park typedef struct {
37*54fd6939SJiyong Park unsigned int file_pos;
38*54fd6939SJiyong Park fip_toc_entry_t entry;
39*54fd6939SJiyong Park } fip_file_state_t;
40*54fd6939SJiyong Park
41*54fd6939SJiyong Park /*
42*54fd6939SJiyong Park * Maintain dev_spec per FIP Device
43*54fd6939SJiyong Park * TODO - Add backend handles and file state
44*54fd6939SJiyong Park * per FIP device here once backends like io_memmap
45*54fd6939SJiyong Park * can support multiple open files
46*54fd6939SJiyong Park */
47*54fd6939SJiyong Park typedef struct {
48*54fd6939SJiyong Park uintptr_t dev_spec;
49*54fd6939SJiyong Park uint16_t plat_toc_flag;
50*54fd6939SJiyong Park } fip_dev_state_t;
51*54fd6939SJiyong Park
52*54fd6939SJiyong Park /*
53*54fd6939SJiyong Park * Only one file can be open across all FIP device
54*54fd6939SJiyong Park * as backends like io_memmap don't support
55*54fd6939SJiyong Park * multiple open files. The file state and
56*54fd6939SJiyong Park * backend handle should be maintained per FIP device
57*54fd6939SJiyong Park * if the same support is available in the backend
58*54fd6939SJiyong Park */
59*54fd6939SJiyong Park static fip_file_state_t current_fip_file = {0};
60*54fd6939SJiyong Park static uintptr_t backend_dev_handle;
61*54fd6939SJiyong Park static uintptr_t backend_image_spec;
62*54fd6939SJiyong Park
63*54fd6939SJiyong Park static fip_dev_state_t state_pool[MAX_FIP_DEVICES];
64*54fd6939SJiyong Park static io_dev_info_t dev_info_pool[MAX_FIP_DEVICES];
65*54fd6939SJiyong Park
66*54fd6939SJiyong Park /* Track number of allocated fip devices */
67*54fd6939SJiyong Park static unsigned int fip_dev_count;
68*54fd6939SJiyong Park
69*54fd6939SJiyong Park /* Firmware Image Package driver functions */
70*54fd6939SJiyong Park static int fip_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
71*54fd6939SJiyong Park static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
72*54fd6939SJiyong Park io_entity_t *entity);
73*54fd6939SJiyong Park static int fip_file_len(io_entity_t *entity, size_t *length);
74*54fd6939SJiyong Park static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
75*54fd6939SJiyong Park size_t *length_read);
76*54fd6939SJiyong Park static int fip_file_close(io_entity_t *entity);
77*54fd6939SJiyong Park static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params);
78*54fd6939SJiyong Park static int fip_dev_close(io_dev_info_t *dev_info);
79*54fd6939SJiyong Park
80*54fd6939SJiyong Park
81*54fd6939SJiyong Park /* Return 0 for equal uuids. */
compare_uuids(const uuid_t * uuid1,const uuid_t * uuid2)82*54fd6939SJiyong Park static inline int compare_uuids(const uuid_t *uuid1, const uuid_t *uuid2)
83*54fd6939SJiyong Park {
84*54fd6939SJiyong Park return memcmp(uuid1, uuid2, sizeof(uuid_t));
85*54fd6939SJiyong Park }
86*54fd6939SJiyong Park
87*54fd6939SJiyong Park
is_valid_header(fip_toc_header_t * header)88*54fd6939SJiyong Park static inline int is_valid_header(fip_toc_header_t *header)
89*54fd6939SJiyong Park {
90*54fd6939SJiyong Park if ((header->name == TOC_HEADER_NAME) && (header->serial_number != 0)) {
91*54fd6939SJiyong Park return 1;
92*54fd6939SJiyong Park } else {
93*54fd6939SJiyong Park return 0;
94*54fd6939SJiyong Park }
95*54fd6939SJiyong Park }
96*54fd6939SJiyong Park
97*54fd6939SJiyong Park
98*54fd6939SJiyong Park /* Identify the device type as a virtual driver */
device_type_fip(void)99*54fd6939SJiyong Park static io_type_t device_type_fip(void)
100*54fd6939SJiyong Park {
101*54fd6939SJiyong Park return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
102*54fd6939SJiyong Park }
103*54fd6939SJiyong Park
104*54fd6939SJiyong Park
105*54fd6939SJiyong Park static const io_dev_connector_t fip_dev_connector = {
106*54fd6939SJiyong Park .dev_open = fip_dev_open
107*54fd6939SJiyong Park };
108*54fd6939SJiyong Park
109*54fd6939SJiyong Park
110*54fd6939SJiyong Park static const io_dev_funcs_t fip_dev_funcs = {
111*54fd6939SJiyong Park .type = device_type_fip,
112*54fd6939SJiyong Park .open = fip_file_open,
113*54fd6939SJiyong Park .seek = NULL,
114*54fd6939SJiyong Park .size = fip_file_len,
115*54fd6939SJiyong Park .read = fip_file_read,
116*54fd6939SJiyong Park .write = NULL,
117*54fd6939SJiyong Park .close = fip_file_close,
118*54fd6939SJiyong Park .dev_init = fip_dev_init,
119*54fd6939SJiyong Park .dev_close = fip_dev_close,
120*54fd6939SJiyong Park };
121*54fd6939SJiyong Park
122*54fd6939SJiyong Park /* Locate a file state in the pool, specified by address */
find_first_fip_state(const uintptr_t dev_spec,unsigned int * index_out)123*54fd6939SJiyong Park static int find_first_fip_state(const uintptr_t dev_spec,
124*54fd6939SJiyong Park unsigned int *index_out)
125*54fd6939SJiyong Park {
126*54fd6939SJiyong Park int result = -ENOENT;
127*54fd6939SJiyong Park unsigned int index;
128*54fd6939SJiyong Park
129*54fd6939SJiyong Park for (index = 0; index < (unsigned int)MAX_FIP_DEVICES; ++index) {
130*54fd6939SJiyong Park /* dev_spec is used as identifier since it's unique */
131*54fd6939SJiyong Park if (state_pool[index].dev_spec == dev_spec) {
132*54fd6939SJiyong Park result = 0;
133*54fd6939SJiyong Park *index_out = index;
134*54fd6939SJiyong Park break;
135*54fd6939SJiyong Park }
136*54fd6939SJiyong Park }
137*54fd6939SJiyong Park return result;
138*54fd6939SJiyong Park }
139*54fd6939SJiyong Park
140*54fd6939SJiyong Park
141*54fd6939SJiyong Park /* Allocate a device info from the pool and return a pointer to it */
allocate_dev_info(io_dev_info_t ** dev_info)142*54fd6939SJiyong Park static int allocate_dev_info(io_dev_info_t **dev_info)
143*54fd6939SJiyong Park {
144*54fd6939SJiyong Park int result = -ENOMEM;
145*54fd6939SJiyong Park
146*54fd6939SJiyong Park assert(dev_info != NULL);
147*54fd6939SJiyong Park
148*54fd6939SJiyong Park if (fip_dev_count < (unsigned int)MAX_FIP_DEVICES) {
149*54fd6939SJiyong Park unsigned int index = 0;
150*54fd6939SJiyong Park
151*54fd6939SJiyong Park result = find_first_fip_state(0, &index);
152*54fd6939SJiyong Park assert(result == 0);
153*54fd6939SJiyong Park /* initialize dev_info */
154*54fd6939SJiyong Park dev_info_pool[index].funcs = &fip_dev_funcs;
155*54fd6939SJiyong Park dev_info_pool[index].info =
156*54fd6939SJiyong Park (uintptr_t)&state_pool[index];
157*54fd6939SJiyong Park *dev_info = &dev_info_pool[index];
158*54fd6939SJiyong Park ++fip_dev_count;
159*54fd6939SJiyong Park }
160*54fd6939SJiyong Park
161*54fd6939SJiyong Park return result;
162*54fd6939SJiyong Park }
163*54fd6939SJiyong Park
164*54fd6939SJiyong Park /* Release a device info to the pool */
free_dev_info(io_dev_info_t * dev_info)165*54fd6939SJiyong Park static int free_dev_info(io_dev_info_t *dev_info)
166*54fd6939SJiyong Park {
167*54fd6939SJiyong Park int result;
168*54fd6939SJiyong Park unsigned int index = 0;
169*54fd6939SJiyong Park fip_dev_state_t *state;
170*54fd6939SJiyong Park
171*54fd6939SJiyong Park assert(dev_info != NULL);
172*54fd6939SJiyong Park
173*54fd6939SJiyong Park state = (fip_dev_state_t *)dev_info->info;
174*54fd6939SJiyong Park result = find_first_fip_state(state->dev_spec, &index);
175*54fd6939SJiyong Park if (result == 0) {
176*54fd6939SJiyong Park /* free if device info is valid */
177*54fd6939SJiyong Park zeromem(state, sizeof(fip_dev_state_t));
178*54fd6939SJiyong Park --fip_dev_count;
179*54fd6939SJiyong Park }
180*54fd6939SJiyong Park
181*54fd6939SJiyong Park return result;
182*54fd6939SJiyong Park }
183*54fd6939SJiyong Park
184*54fd6939SJiyong Park /*
185*54fd6939SJiyong Park * Multiple FIP devices can be opened depending on the value of
186*54fd6939SJiyong Park * MAX_FIP_DEVICES. Given that there is only one backend, only a
187*54fd6939SJiyong Park * single file can be open at a time by any FIP device.
188*54fd6939SJiyong Park */
fip_dev_open(const uintptr_t dev_spec,io_dev_info_t ** dev_info)189*54fd6939SJiyong Park static int fip_dev_open(const uintptr_t dev_spec,
190*54fd6939SJiyong Park io_dev_info_t **dev_info)
191*54fd6939SJiyong Park {
192*54fd6939SJiyong Park int result;
193*54fd6939SJiyong Park io_dev_info_t *info;
194*54fd6939SJiyong Park fip_dev_state_t *state;
195*54fd6939SJiyong Park
196*54fd6939SJiyong Park assert(dev_info != NULL);
197*54fd6939SJiyong Park #if MAX_FIP_DEVICES > 1
198*54fd6939SJiyong Park assert(dev_spec != (uintptr_t)NULL);
199*54fd6939SJiyong Park #endif
200*54fd6939SJiyong Park
201*54fd6939SJiyong Park result = allocate_dev_info(&info);
202*54fd6939SJiyong Park if (result != 0)
203*54fd6939SJiyong Park return -ENOMEM;
204*54fd6939SJiyong Park
205*54fd6939SJiyong Park state = (fip_dev_state_t *)info->info;
206*54fd6939SJiyong Park
207*54fd6939SJiyong Park state->dev_spec = dev_spec;
208*54fd6939SJiyong Park
209*54fd6939SJiyong Park *dev_info = info;
210*54fd6939SJiyong Park
211*54fd6939SJiyong Park return 0;
212*54fd6939SJiyong Park }
213*54fd6939SJiyong Park
214*54fd6939SJiyong Park
215*54fd6939SJiyong Park /* Do some basic package checks. */
fip_dev_init(io_dev_info_t * dev_info,const uintptr_t init_params)216*54fd6939SJiyong Park static int fip_dev_init(io_dev_info_t *dev_info, const uintptr_t init_params)
217*54fd6939SJiyong Park {
218*54fd6939SJiyong Park int result;
219*54fd6939SJiyong Park unsigned int image_id = (unsigned int)init_params;
220*54fd6939SJiyong Park uintptr_t backend_handle;
221*54fd6939SJiyong Park fip_toc_header_t header;
222*54fd6939SJiyong Park size_t bytes_read;
223*54fd6939SJiyong Park fip_dev_state_t *state;
224*54fd6939SJiyong Park
225*54fd6939SJiyong Park assert(dev_info != NULL);
226*54fd6939SJiyong Park
227*54fd6939SJiyong Park state = (fip_dev_state_t *)dev_info->info;
228*54fd6939SJiyong Park
229*54fd6939SJiyong Park /* Obtain a reference to the image by querying the platform layer */
230*54fd6939SJiyong Park result = plat_get_image_source(image_id, &backend_dev_handle,
231*54fd6939SJiyong Park &backend_image_spec);
232*54fd6939SJiyong Park if (result != 0) {
233*54fd6939SJiyong Park WARN("Failed to obtain reference to image id=%u (%i)\n",
234*54fd6939SJiyong Park image_id, result);
235*54fd6939SJiyong Park result = -ENOENT;
236*54fd6939SJiyong Park goto fip_dev_init_exit;
237*54fd6939SJiyong Park }
238*54fd6939SJiyong Park
239*54fd6939SJiyong Park /* Attempt to access the FIP image */
240*54fd6939SJiyong Park result = io_open(backend_dev_handle, backend_image_spec,
241*54fd6939SJiyong Park &backend_handle);
242*54fd6939SJiyong Park if (result != 0) {
243*54fd6939SJiyong Park WARN("Failed to access image id=%u (%i)\n", image_id, result);
244*54fd6939SJiyong Park result = -ENOENT;
245*54fd6939SJiyong Park goto fip_dev_init_exit;
246*54fd6939SJiyong Park }
247*54fd6939SJiyong Park
248*54fd6939SJiyong Park result = io_read(backend_handle, (uintptr_t)&header, sizeof(header),
249*54fd6939SJiyong Park &bytes_read);
250*54fd6939SJiyong Park if (result == 0) {
251*54fd6939SJiyong Park if (!is_valid_header(&header)) {
252*54fd6939SJiyong Park WARN("Firmware Image Package header check failed.\n");
253*54fd6939SJiyong Park result = -ENOENT;
254*54fd6939SJiyong Park } else {
255*54fd6939SJiyong Park VERBOSE("FIP header looks OK.\n");
256*54fd6939SJiyong Park /*
257*54fd6939SJiyong Park * Store 16-bit Platform ToC flags field which occupies
258*54fd6939SJiyong Park * bits [32-47] in fip header.
259*54fd6939SJiyong Park */
260*54fd6939SJiyong Park state->plat_toc_flag = (header.flags >> 32) & 0xffff;
261*54fd6939SJiyong Park }
262*54fd6939SJiyong Park }
263*54fd6939SJiyong Park
264*54fd6939SJiyong Park io_close(backend_handle);
265*54fd6939SJiyong Park
266*54fd6939SJiyong Park fip_dev_init_exit:
267*54fd6939SJiyong Park return result;
268*54fd6939SJiyong Park }
269*54fd6939SJiyong Park
270*54fd6939SJiyong Park /* Close a connection to the FIP device */
fip_dev_close(io_dev_info_t * dev_info)271*54fd6939SJiyong Park static int fip_dev_close(io_dev_info_t *dev_info)
272*54fd6939SJiyong Park {
273*54fd6939SJiyong Park /* TODO: Consider tracking open files and cleaning them up here */
274*54fd6939SJiyong Park
275*54fd6939SJiyong Park /* Clear the backend. */
276*54fd6939SJiyong Park backend_dev_handle = (uintptr_t)NULL;
277*54fd6939SJiyong Park backend_image_spec = (uintptr_t)NULL;
278*54fd6939SJiyong Park
279*54fd6939SJiyong Park return free_dev_info(dev_info);
280*54fd6939SJiyong Park }
281*54fd6939SJiyong Park
282*54fd6939SJiyong Park
283*54fd6939SJiyong Park /* Open a file for access from package. */
fip_file_open(io_dev_info_t * dev_info,const uintptr_t spec,io_entity_t * entity)284*54fd6939SJiyong Park static int fip_file_open(io_dev_info_t *dev_info, const uintptr_t spec,
285*54fd6939SJiyong Park io_entity_t *entity)
286*54fd6939SJiyong Park {
287*54fd6939SJiyong Park int result;
288*54fd6939SJiyong Park uintptr_t backend_handle;
289*54fd6939SJiyong Park const io_uuid_spec_t *uuid_spec = (io_uuid_spec_t *)spec;
290*54fd6939SJiyong Park static const uuid_t uuid_null = { {0} }; /* Double braces for clang */
291*54fd6939SJiyong Park size_t bytes_read;
292*54fd6939SJiyong Park int found_file = 0;
293*54fd6939SJiyong Park
294*54fd6939SJiyong Park assert(uuid_spec != NULL);
295*54fd6939SJiyong Park assert(entity != NULL);
296*54fd6939SJiyong Park
297*54fd6939SJiyong Park /* Can only have one file open at a time for the moment. We need to
298*54fd6939SJiyong Park * track state like file cursor position. We know the header lives at
299*54fd6939SJiyong Park * offset zero, so this entry should never be zero for an active file.
300*54fd6939SJiyong Park * When the system supports dynamic memory allocation we can allow more
301*54fd6939SJiyong Park * than one open file at a time if needed.
302*54fd6939SJiyong Park */
303*54fd6939SJiyong Park if (current_fip_file.entry.offset_address != 0U) {
304*54fd6939SJiyong Park WARN("fip_file_open : Only one open file at a time.\n");
305*54fd6939SJiyong Park return -ENFILE;
306*54fd6939SJiyong Park }
307*54fd6939SJiyong Park
308*54fd6939SJiyong Park /* Attempt to access the FIP image */
309*54fd6939SJiyong Park result = io_open(backend_dev_handle, backend_image_spec,
310*54fd6939SJiyong Park &backend_handle);
311*54fd6939SJiyong Park if (result != 0) {
312*54fd6939SJiyong Park WARN("Failed to open Firmware Image Package (%i)\n", result);
313*54fd6939SJiyong Park result = -ENOENT;
314*54fd6939SJiyong Park goto fip_file_open_exit;
315*54fd6939SJiyong Park }
316*54fd6939SJiyong Park
317*54fd6939SJiyong Park /* Seek past the FIP header into the Table of Contents */
318*54fd6939SJiyong Park result = io_seek(backend_handle, IO_SEEK_SET,
319*54fd6939SJiyong Park (signed long long)sizeof(fip_toc_header_t));
320*54fd6939SJiyong Park if (result != 0) {
321*54fd6939SJiyong Park WARN("fip_file_open: failed to seek\n");
322*54fd6939SJiyong Park result = -ENOENT;
323*54fd6939SJiyong Park goto fip_file_open_close;
324*54fd6939SJiyong Park }
325*54fd6939SJiyong Park
326*54fd6939SJiyong Park found_file = 0;
327*54fd6939SJiyong Park do {
328*54fd6939SJiyong Park result = io_read(backend_handle,
329*54fd6939SJiyong Park (uintptr_t)¤t_fip_file.entry,
330*54fd6939SJiyong Park sizeof(current_fip_file.entry),
331*54fd6939SJiyong Park &bytes_read);
332*54fd6939SJiyong Park if (result == 0) {
333*54fd6939SJiyong Park if (compare_uuids(¤t_fip_file.entry.uuid,
334*54fd6939SJiyong Park &uuid_spec->uuid) == 0) {
335*54fd6939SJiyong Park found_file = 1;
336*54fd6939SJiyong Park }
337*54fd6939SJiyong Park } else {
338*54fd6939SJiyong Park WARN("Failed to read FIP (%i)\n", result);
339*54fd6939SJiyong Park goto fip_file_open_close;
340*54fd6939SJiyong Park }
341*54fd6939SJiyong Park } while ((found_file == 0) &&
342*54fd6939SJiyong Park (compare_uuids(¤t_fip_file.entry.uuid,
343*54fd6939SJiyong Park &uuid_null) != 0));
344*54fd6939SJiyong Park
345*54fd6939SJiyong Park if (found_file == 1) {
346*54fd6939SJiyong Park /* All fine. Update entity info with file state and return. Set
347*54fd6939SJiyong Park * the file position to 0. The 'current_fip_file.entry' holds
348*54fd6939SJiyong Park * the base and size of the file.
349*54fd6939SJiyong Park */
350*54fd6939SJiyong Park current_fip_file.file_pos = 0;
351*54fd6939SJiyong Park entity->info = (uintptr_t)¤t_fip_file;
352*54fd6939SJiyong Park } else {
353*54fd6939SJiyong Park /* Did not find the file in the FIP. */
354*54fd6939SJiyong Park current_fip_file.entry.offset_address = 0;
355*54fd6939SJiyong Park result = -ENOENT;
356*54fd6939SJiyong Park }
357*54fd6939SJiyong Park
358*54fd6939SJiyong Park fip_file_open_close:
359*54fd6939SJiyong Park io_close(backend_handle);
360*54fd6939SJiyong Park
361*54fd6939SJiyong Park fip_file_open_exit:
362*54fd6939SJiyong Park return result;
363*54fd6939SJiyong Park }
364*54fd6939SJiyong Park
365*54fd6939SJiyong Park
366*54fd6939SJiyong Park /* Return the size of a file in package */
fip_file_len(io_entity_t * entity,size_t * length)367*54fd6939SJiyong Park static int fip_file_len(io_entity_t *entity, size_t *length)
368*54fd6939SJiyong Park {
369*54fd6939SJiyong Park assert(entity != NULL);
370*54fd6939SJiyong Park assert(length != NULL);
371*54fd6939SJiyong Park
372*54fd6939SJiyong Park *length = ((fip_file_state_t *)entity->info)->entry.size;
373*54fd6939SJiyong Park
374*54fd6939SJiyong Park return 0;
375*54fd6939SJiyong Park }
376*54fd6939SJiyong Park
377*54fd6939SJiyong Park
378*54fd6939SJiyong Park /* Read data from a file in package */
fip_file_read(io_entity_t * entity,uintptr_t buffer,size_t length,size_t * length_read)379*54fd6939SJiyong Park static int fip_file_read(io_entity_t *entity, uintptr_t buffer, size_t length,
380*54fd6939SJiyong Park size_t *length_read)
381*54fd6939SJiyong Park {
382*54fd6939SJiyong Park int result;
383*54fd6939SJiyong Park fip_file_state_t *fp;
384*54fd6939SJiyong Park size_t file_offset;
385*54fd6939SJiyong Park size_t bytes_read;
386*54fd6939SJiyong Park uintptr_t backend_handle;
387*54fd6939SJiyong Park
388*54fd6939SJiyong Park assert(entity != NULL);
389*54fd6939SJiyong Park assert(length_read != NULL);
390*54fd6939SJiyong Park assert(entity->info != (uintptr_t)NULL);
391*54fd6939SJiyong Park
392*54fd6939SJiyong Park /* Open the backend, attempt to access the blob image */
393*54fd6939SJiyong Park result = io_open(backend_dev_handle, backend_image_spec,
394*54fd6939SJiyong Park &backend_handle);
395*54fd6939SJiyong Park if (result != 0) {
396*54fd6939SJiyong Park WARN("Failed to open FIP (%i)\n", result);
397*54fd6939SJiyong Park result = -ENOENT;
398*54fd6939SJiyong Park goto fip_file_read_exit;
399*54fd6939SJiyong Park }
400*54fd6939SJiyong Park
401*54fd6939SJiyong Park fp = (fip_file_state_t *)entity->info;
402*54fd6939SJiyong Park
403*54fd6939SJiyong Park /* Seek to the position in the FIP where the payload lives */
404*54fd6939SJiyong Park file_offset = fp->entry.offset_address + fp->file_pos;
405*54fd6939SJiyong Park result = io_seek(backend_handle, IO_SEEK_SET,
406*54fd6939SJiyong Park (signed long long)file_offset);
407*54fd6939SJiyong Park if (result != 0) {
408*54fd6939SJiyong Park WARN("fip_file_read: failed to seek\n");
409*54fd6939SJiyong Park result = -ENOENT;
410*54fd6939SJiyong Park goto fip_file_read_close;
411*54fd6939SJiyong Park }
412*54fd6939SJiyong Park
413*54fd6939SJiyong Park result = io_read(backend_handle, buffer, length, &bytes_read);
414*54fd6939SJiyong Park if (result != 0) {
415*54fd6939SJiyong Park /* We cannot read our data. Fail. */
416*54fd6939SJiyong Park WARN("Failed to read payload (%i)\n", result);
417*54fd6939SJiyong Park result = -ENOENT;
418*54fd6939SJiyong Park goto fip_file_read_close;
419*54fd6939SJiyong Park } else {
420*54fd6939SJiyong Park /* Set caller length and new file position. */
421*54fd6939SJiyong Park *length_read = bytes_read;
422*54fd6939SJiyong Park fp->file_pos += bytes_read;
423*54fd6939SJiyong Park }
424*54fd6939SJiyong Park
425*54fd6939SJiyong Park /* Close the backend. */
426*54fd6939SJiyong Park fip_file_read_close:
427*54fd6939SJiyong Park io_close(backend_handle);
428*54fd6939SJiyong Park
429*54fd6939SJiyong Park fip_file_read_exit:
430*54fd6939SJiyong Park return result;
431*54fd6939SJiyong Park }
432*54fd6939SJiyong Park
433*54fd6939SJiyong Park
434*54fd6939SJiyong Park /* Close a file in package */
fip_file_close(io_entity_t * entity)435*54fd6939SJiyong Park static int fip_file_close(io_entity_t *entity)
436*54fd6939SJiyong Park {
437*54fd6939SJiyong Park /* Clear our current file pointer.
438*54fd6939SJiyong Park * If we had malloc() we would free() here.
439*54fd6939SJiyong Park */
440*54fd6939SJiyong Park if (current_fip_file.entry.offset_address != 0U) {
441*54fd6939SJiyong Park zeromem(¤t_fip_file, sizeof(current_fip_file));
442*54fd6939SJiyong Park }
443*54fd6939SJiyong Park
444*54fd6939SJiyong Park /* Clear the Entity info. */
445*54fd6939SJiyong Park entity->info = 0;
446*54fd6939SJiyong Park
447*54fd6939SJiyong Park return 0;
448*54fd6939SJiyong Park }
449*54fd6939SJiyong Park
450*54fd6939SJiyong Park /* Exported functions */
451*54fd6939SJiyong Park
452*54fd6939SJiyong Park /* Register the Firmware Image Package driver with the IO abstraction */
register_io_dev_fip(const io_dev_connector_t ** dev_con)453*54fd6939SJiyong Park int register_io_dev_fip(const io_dev_connector_t **dev_con)
454*54fd6939SJiyong Park {
455*54fd6939SJiyong Park int result;
456*54fd6939SJiyong Park assert(dev_con != NULL);
457*54fd6939SJiyong Park
458*54fd6939SJiyong Park /*
459*54fd6939SJiyong Park * Since dev_info isn't really used in io_register_device, always
460*54fd6939SJiyong Park * use the same device info at here instead.
461*54fd6939SJiyong Park */
462*54fd6939SJiyong Park result = io_register_device(&dev_info_pool[0]);
463*54fd6939SJiyong Park if (result == 0)
464*54fd6939SJiyong Park *dev_con = &fip_dev_connector;
465*54fd6939SJiyong Park
466*54fd6939SJiyong Park return result;
467*54fd6939SJiyong Park }
468*54fd6939SJiyong Park
469*54fd6939SJiyong Park /* 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)470*54fd6939SJiyong Park int fip_dev_get_plat_toc_flag(io_dev_info_t *dev_info, uint16_t *plat_toc_flag)
471*54fd6939SJiyong Park {
472*54fd6939SJiyong Park fip_dev_state_t *state;
473*54fd6939SJiyong Park
474*54fd6939SJiyong Park assert(dev_info != NULL);
475*54fd6939SJiyong Park
476*54fd6939SJiyong Park state = (fip_dev_state_t *)dev_info->info;
477*54fd6939SJiyong Park
478*54fd6939SJiyong Park *plat_toc_flag = state->plat_toc_flag;
479*54fd6939SJiyong Park
480*54fd6939SJiyong Park return 0;
481*54fd6939SJiyong Park }
482