4 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
6 * This module implements DSP code management for the DSP/BIOS Bridge
7 * environment. It is mostly a thin wrapper.
9 * This module provides an interface for loading both static and
10 * dynamic code objects onto DSP systems.
12 * Copyright (C) 2005-2006 Texas Instruments, Inc.
14 * This package is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
18 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 /* ----------------------------------- Host OS */
24 #include <dspbridge/host_os.h>
26 #include <linux/uaccess.h>
28 /* ----------------------------------- DSP/BIOS Bridge */
29 #include <dspbridge/std.h>
30 #include <dspbridge/dbdefs.h>
32 /* ----------------------------------- Trace & Debug */
33 #include <dspbridge/dbc.h>
35 /* ----------------------------------- OS Adaptation Layer */
36 #include <dspbridge/ldr.h>
38 /* ----------------------------------- Platform Manager */
39 /* Include appropriate loader header file */
40 #include <dspbridge/dbll.h>
42 /* ----------------------------------- This */
43 #include <dspbridge/cod.h>
45 /* magic number for handle validation */
46 #define MAGIC 0xc001beef
48 /* macro to validate COD manager handles */
49 #define IS_VALID(h) ((h) != NULL && (h)->ul_magic == MAGIC)
52 * ======== cod_manager ========
55 struct dbll_tar_obj *target;
56 struct dbll_library_obj *base_lib;
57 bool loaded; /* Base library loaded? */
59 struct ldr_module *dll_obj;
60 struct dbll_fxns fxns;
61 struct dbll_attrs attrs;
62 char sz_zl_file[COD_MAXPATHLENGTH];
67 * ======== cod_libraryobj ========
69 struct cod_libraryobj {
70 struct dbll_library_obj *dbll_lib;
71 struct cod_manager *cod_mgr;
76 static struct dbll_fxns ldr_fxns = {
77 (dbll_close_fxn) dbll_close,
78 (dbll_create_fxn) dbll_create,
79 (dbll_delete_fxn) dbll_delete,
80 (dbll_exit_fxn) dbll_exit,
81 (dbll_get_attrs_fxn) dbll_get_attrs,
82 (dbll_get_addr_fxn) dbll_get_addr,
83 (dbll_get_c_addr_fxn) dbll_get_c_addr,
84 (dbll_get_sect_fxn) dbll_get_sect,
85 (dbll_init_fxn) dbll_init,
86 (dbll_load_fxn) dbll_load,
87 (dbll_load_sect_fxn) dbll_load_sect,
88 (dbll_open_fxn) dbll_open,
89 (dbll_read_sect_fxn) dbll_read_sect,
90 (dbll_set_attrs_fxn) dbll_set_attrs,
91 (dbll_unload_fxn) dbll_unload,
92 (dbll_unload_sect_fxn) dbll_unload_sect,
95 static bool no_op(void);
98 * File operations (originally were under kfile.c)
100 static s32 cod_f_close(struct file *filp)
102 /* Check for valid handle */
106 filp_close(filp, NULL);
108 /* we can't use 0 here */
112 static struct file *cod_f_open(CONST char *psz_file_name, CONST char *sz_mode)
120 /* ignore given mode and open file as read-only */
121 filp = filp_open(psz_file_name, O_RDONLY, 0);
131 static s32 cod_f_read(void __user *pbuffer, s32 size, s32 count,
134 /* check for valid file handle */
138 if ((size > 0) && (count > 0) && pbuffer) {
145 dw_bytes_read = filp->f_op->read(filp, pbuffer, size * count,
152 return dw_bytes_read / size;
158 static s32 cod_f_seek(struct file *filp, s32 offset, s32 origin)
162 /* check for valid file handle */
166 /* based on the origin flag, move the internal pointer */
167 dw_cur_pos = filp->f_op->llseek(filp, offset, origin);
169 if ((s32) dw_cur_pos < 0)
172 /* we can't use 0 here */
176 static s32 cod_f_tell(struct file *filp)
183 /* Get current position */
184 dw_cur_pos = filp->f_op->llseek(filp, 0, SEEK_CUR);
186 if ((s32) dw_cur_pos < 0)
193 * ======== cod_close ========
195 void cod_close(struct cod_libraryobj *lib)
197 struct cod_manager *hmgr;
199 DBC_REQUIRE(refs > 0);
200 DBC_REQUIRE(lib != NULL);
201 DBC_REQUIRE(IS_VALID(((struct cod_libraryobj *)lib)->cod_mgr));
204 hmgr->fxns.close_fxn(lib->dbll_lib);
210 * ======== cod_create ========
212 * Create an object to manage code on a DSP system.
213 * This object can be used to load an initial program image with
214 * arguments that can later be expanded with
215 * dynamically loaded object files.
218 int cod_create(OUT struct cod_manager **mgr, char *str_zl_file,
219 IN OPTIONAL CONST struct cod_attrs *attrs)
221 struct cod_manager *mgr_new;
222 struct dbll_attrs zl_attrs;
225 DBC_REQUIRE(refs > 0);
226 DBC_REQUIRE(mgr != NULL);
231 /* we don't support non-default attrs yet */
235 mgr_new = kzalloc(sizeof(struct cod_manager), GFP_KERNEL);
239 mgr_new->ul_magic = MAGIC;
241 /* Set up loader functions */
242 mgr_new->fxns = ldr_fxns;
244 /* initialize the ZL module */
245 mgr_new->fxns.init_fxn();
247 zl_attrs.alloc = (dbll_alloc_fxn) no_op;
248 zl_attrs.free = (dbll_free_fxn) no_op;
249 zl_attrs.fread = (dbll_read_fxn) cod_f_read;
250 zl_attrs.fseek = (dbll_seek_fxn) cod_f_seek;
251 zl_attrs.ftell = (dbll_tell_fxn) cod_f_tell;
252 zl_attrs.fclose = (dbll_f_close_fxn) cod_f_close;
253 zl_attrs.fopen = (dbll_f_open_fxn) cod_f_open;
254 zl_attrs.sym_lookup = NULL;
255 zl_attrs.base_image = true;
256 zl_attrs.log_write = NULL;
257 zl_attrs.log_write_handle = NULL;
258 zl_attrs.write = NULL;
259 zl_attrs.rmm_handle = NULL;
260 zl_attrs.input_params = NULL;
261 zl_attrs.sym_handle = NULL;
262 zl_attrs.sym_arg = NULL;
264 mgr_new->attrs = zl_attrs;
266 status = mgr_new->fxns.create_fxn(&mgr_new->target, &zl_attrs);
268 if (DSP_FAILED(status)) {
273 /* return the new manager */
280 * ======== cod_delete ========
282 * Delete a code manager object.
284 void cod_delete(struct cod_manager *cod_mgr_obj)
286 DBC_REQUIRE(refs > 0);
287 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
289 if (cod_mgr_obj->base_lib) {
290 if (cod_mgr_obj->loaded)
291 cod_mgr_obj->fxns.unload_fxn(cod_mgr_obj->base_lib,
292 &cod_mgr_obj->attrs);
294 cod_mgr_obj->fxns.close_fxn(cod_mgr_obj->base_lib);
296 if (cod_mgr_obj->target) {
297 cod_mgr_obj->fxns.delete_fxn(cod_mgr_obj->target);
298 cod_mgr_obj->fxns.exit_fxn();
300 cod_mgr_obj->ul_magic = ~MAGIC;
305 * ======== cod_exit ========
307 * Discontinue usage of the COD module.
312 DBC_REQUIRE(refs > 0);
316 DBC_ENSURE(refs >= 0);
320 * ======== cod_get_base_lib ========
322 * Get handle to the base image DBL library.
324 int cod_get_base_lib(struct cod_manager *cod_mgr_obj,
325 struct dbll_library_obj **plib)
329 DBC_REQUIRE(refs > 0);
330 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
331 DBC_REQUIRE(plib != NULL);
333 *plib = (struct dbll_library_obj *)cod_mgr_obj->base_lib;
339 * ======== cod_get_base_name ========
341 int cod_get_base_name(struct cod_manager *cod_mgr_obj, char *sz_name,
346 DBC_REQUIRE(refs > 0);
347 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
348 DBC_REQUIRE(sz_name != NULL);
350 if (usize <= COD_MAXPATHLENGTH)
351 strncpy(sz_name, cod_mgr_obj->sz_zl_file, usize);
359 * ======== cod_get_entry ========
361 * Retrieve the entry point of a loaded DSP program image
364 int cod_get_entry(struct cod_manager *cod_mgr_obj, u32 *entry_pt)
366 DBC_REQUIRE(refs > 0);
367 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
368 DBC_REQUIRE(entry_pt != NULL);
370 *entry_pt = cod_mgr_obj->ul_entry;
376 * ======== cod_get_loader ========
378 * Get handle to the DBLL loader.
380 int cod_get_loader(struct cod_manager *cod_mgr_obj,
381 struct dbll_tar_obj **loader)
385 DBC_REQUIRE(refs > 0);
386 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
387 DBC_REQUIRE(loader != NULL);
389 *loader = (struct dbll_tar_obj *)cod_mgr_obj->target;
395 * ======== cod_get_section ========
397 * Retrieve the starting address and length of a section in the COFF file
398 * given the section name.
400 int cod_get_section(struct cod_libraryobj *lib, IN char *str_sect,
401 OUT u32 *addr, OUT u32 *len)
403 struct cod_manager *cod_mgr_obj;
406 DBC_REQUIRE(refs > 0);
407 DBC_REQUIRE(lib != NULL);
408 DBC_REQUIRE(IS_VALID(lib->cod_mgr));
409 DBC_REQUIRE(str_sect != NULL);
410 DBC_REQUIRE(addr != NULL);
411 DBC_REQUIRE(len != NULL);
416 cod_mgr_obj = lib->cod_mgr;
417 status = cod_mgr_obj->fxns.get_sect_fxn(lib->dbll_lib, str_sect,
423 DBC_ENSURE(DSP_SUCCEEDED(status) || ((*addr == 0) && (*len == 0)));
429 * ======== cod_get_sym_value ========
431 * Retrieve the value for the specified symbol. The symbol is first
432 * searched for literally and then, if not found, searched for as a
436 int cod_get_sym_value(struct cod_manager *cod_mgr_obj, char *str_sym,
439 struct dbll_sym_val *dbll_sym;
441 DBC_REQUIRE(refs > 0);
442 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
443 DBC_REQUIRE(str_sym != NULL);
444 DBC_REQUIRE(pul_value != NULL);
446 dev_dbg(bridge, "%s: cod_mgr_obj: %p str_sym: %s pul_value: %p\n",
447 __func__, cod_mgr_obj, str_sym, pul_value);
448 if (cod_mgr_obj->base_lib) {
449 if (!cod_mgr_obj->fxns.
450 get_addr_fxn(cod_mgr_obj->base_lib, str_sym, &dbll_sym)) {
451 if (!cod_mgr_obj->fxns.
452 get_c_addr_fxn(cod_mgr_obj->base_lib, str_sym,
460 *pul_value = dbll_sym->value;
466 * ======== cod_init ========
468 * Initialize the COD module's private state.
475 DBC_REQUIRE(refs >= 0);
480 DBC_ENSURE((ret && refs > 0) || (!ret && refs >= 0));
485 * ======== cod_load_base ========
487 * Load the initial program image, optionally with command-line arguments,
488 * on the DSP system managed by the supplied handle. The program to be
489 * loaded must be the first element of the args array and must be a fully
490 * qualified pathname.
492 * if num_argc doesn't match the number of arguments in the args array, the
493 * args array is searched for a NULL terminating entry, and argc is
494 * recalculated to reflect this. In this way, we can support NULL
495 * terminating args arrays, if num_argc is very large.
497 int cod_load_base(struct cod_manager *cod_mgr_obj, u32 num_argc, char *args[],
498 cod_writefxn pfn_write, void *arb, char *envp[])
501 struct dbll_attrs save_attrs;
502 struct dbll_attrs new_attrs;
506 DBC_REQUIRE(refs > 0);
507 DBC_REQUIRE(IS_VALID(cod_mgr_obj));
508 DBC_REQUIRE(num_argc > 0);
509 DBC_REQUIRE(args != NULL);
510 DBC_REQUIRE(args[0] != NULL);
511 DBC_REQUIRE(pfn_write != NULL);
512 DBC_REQUIRE(cod_mgr_obj->base_lib != NULL);
515 * Make sure every argv[] stated in argc has a value, or change argc to
516 * reflect true number in NULL terminated argv array.
518 for (i = 0; i < num_argc; i++) {
519 if (args[i] == NULL) {
525 /* set the write function for this operation */
526 cod_mgr_obj->fxns.get_attrs_fxn(cod_mgr_obj->target, &save_attrs);
528 new_attrs = save_attrs;
529 new_attrs.write = (dbll_write_fxn) pfn_write;
530 new_attrs.input_params = arb;
531 new_attrs.alloc = (dbll_alloc_fxn) no_op;
532 new_attrs.free = (dbll_free_fxn) no_op;
533 new_attrs.log_write = NULL;
534 new_attrs.log_write_handle = NULL;
537 flags = DBLL_CODE | DBLL_DATA | DBLL_SYMB;
538 status = cod_mgr_obj->fxns.load_fxn(cod_mgr_obj->base_lib, flags,
540 &cod_mgr_obj->ul_entry);
541 if (DSP_FAILED(status))
542 cod_mgr_obj->fxns.close_fxn(cod_mgr_obj->base_lib);
544 if (DSP_SUCCEEDED(status))
545 cod_mgr_obj->loaded = true;
547 cod_mgr_obj->base_lib = NULL;
553 * ======== cod_open ========
554 * Open library for reading sections.
556 int cod_open(struct cod_manager *hmgr, IN char *sz_coff_path,
557 u32 flags, struct cod_libraryobj **lib_obj)
560 struct cod_libraryobj *lib = NULL;
562 DBC_REQUIRE(refs > 0);
563 DBC_REQUIRE(IS_VALID(hmgr));
564 DBC_REQUIRE(sz_coff_path != NULL);
565 DBC_REQUIRE(flags == COD_NOLOAD || flags == COD_SYMB);
566 DBC_REQUIRE(lib_obj != NULL);
570 lib = kzalloc(sizeof(struct cod_libraryobj), GFP_KERNEL);
574 if (DSP_SUCCEEDED(status)) {
576 status = hmgr->fxns.open_fxn(hmgr->target, sz_coff_path, flags,
578 if (DSP_SUCCEEDED(status))
582 if (DSP_FAILED(status))
583 pr_err("%s: error status 0x%x, sz_coff_path: %s flags: 0x%x\n",
584 __func__, status, sz_coff_path, flags);
589 * ======== cod_open_base ========
591 * Open base image for reading sections.
593 int cod_open_base(struct cod_manager *hmgr, IN char *sz_coff_path,
597 struct dbll_library_obj *lib;
599 DBC_REQUIRE(refs > 0);
600 DBC_REQUIRE(IS_VALID(hmgr));
601 DBC_REQUIRE(sz_coff_path != NULL);
603 /* if we previously opened a base image, close it now */
604 if (hmgr->base_lib) {
606 hmgr->fxns.unload_fxn(hmgr->base_lib, &hmgr->attrs);
607 hmgr->loaded = false;
609 hmgr->fxns.close_fxn(hmgr->base_lib);
610 hmgr->base_lib = NULL;
612 status = hmgr->fxns.open_fxn(hmgr->target, sz_coff_path, flags, &lib);
613 if (DSP_SUCCEEDED(status)) {
614 /* hang onto the library for subsequent sym table usage */
615 hmgr->base_lib = lib;
616 strncpy(hmgr->sz_zl_file, sz_coff_path, COD_MAXPATHLENGTH - 1);
617 hmgr->sz_zl_file[COD_MAXPATHLENGTH - 1] = '\0';
620 if (DSP_FAILED(status))
621 pr_err("%s: error status 0x%x sz_coff_path: %s\n", __func__,
622 status, sz_coff_path);
627 * ======== cod_read_section ========
629 * Retrieve the content of a code section given the section name.
631 int cod_read_section(struct cod_libraryobj *lib, IN char *str_sect,
632 OUT char *str_content, IN u32 content_size)
636 DBC_REQUIRE(refs > 0);
637 DBC_REQUIRE(lib != NULL);
638 DBC_REQUIRE(IS_VALID(lib->cod_mgr));
639 DBC_REQUIRE(str_sect != NULL);
640 DBC_REQUIRE(str_content != NULL);
644 lib->cod_mgr->fxns.read_sect_fxn(lib->dbll_lib, str_sect,
645 str_content, content_size);
653 * ======== no_op ========
658 static bool no_op(void)