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 #include <linux/types.h>
25 /* ----------------------------------- Host OS */
26 #include <dspbridge/host_os.h>
28 #include <linux/uaccess.h>
30 /* ----------------------------------- DSP/BIOS Bridge */
31 #include <dspbridge/dbdefs.h>
33 /* ----------------------------------- Platform Manager */
34 /* Include appropriate loader header file */
35 #include <dspbridge/dbll.h>
37 /* ----------------------------------- This */
38 #include <dspbridge/cod.h>
41 * ======== cod_manager ========
44 struct dbll_tar_obj *target;
45 struct dbll_library_obj *base_lib;
46 bool loaded; /* Base library loaded? */
48 struct dbll_fxns fxns;
49 struct dbll_attrs attrs;
50 char sz_zl_file[COD_MAXPATHLENGTH];
54 * ======== cod_libraryobj ========
56 struct cod_libraryobj {
57 struct dbll_library_obj *dbll_lib;
58 struct cod_manager *cod_mgr;
61 static struct dbll_fxns ldr_fxns = {
62 (dbll_close_fxn) dbll_close,
63 (dbll_create_fxn) dbll_create,
64 (dbll_delete_fxn) dbll_delete,
65 (dbll_exit_fxn) dbll_exit,
66 (dbll_get_attrs_fxn) dbll_get_attrs,
67 (dbll_get_addr_fxn) dbll_get_addr,
68 (dbll_get_c_addr_fxn) dbll_get_c_addr,
69 (dbll_get_sect_fxn) dbll_get_sect,
70 (dbll_init_fxn) dbll_init,
71 (dbll_load_fxn) dbll_load,
72 (dbll_open_fxn) dbll_open,
73 (dbll_read_sect_fxn) dbll_read_sect,
74 (dbll_unload_fxn) dbll_unload,
77 static bool no_op(void);
80 * File operations (originally were under kfile.c)
82 static s32 cod_f_close(struct file *filp)
84 /* Check for valid handle */
88 filp_close(filp, NULL);
90 /* we can't use 0 here */
94 static struct file *cod_f_open(const char *psz_file_name, const char *sz_mode)
102 /* ignore given mode and open file as read-only */
103 filp = filp_open(psz_file_name, O_RDONLY, 0);
113 static s32 cod_f_read(void __user *pbuffer, s32 size, s32 count,
116 /* check for valid file handle */
120 if ((size > 0) && (count > 0) && pbuffer) {
127 dw_bytes_read = filp->f_op->read(filp, pbuffer, size * count,
134 return dw_bytes_read / size;
140 static s32 cod_f_seek(struct file *filp, s32 offset, s32 origin)
144 /* check for valid file handle */
148 /* based on the origin flag, move the internal pointer */
149 dw_cur_pos = filp->f_op->llseek(filp, offset, origin);
151 if ((s32) dw_cur_pos < 0)
154 /* we can't use 0 here */
158 static s32 cod_f_tell(struct file *filp)
165 /* Get current position */
166 dw_cur_pos = filp->f_op->llseek(filp, 0, SEEK_CUR);
168 if ((s32) dw_cur_pos < 0)
175 * ======== cod_close ========
177 void cod_close(struct cod_libraryobj *lib)
179 struct cod_manager *hmgr;
182 hmgr->fxns.close_fxn(lib->dbll_lib);
188 * ======== cod_create ========
190 * Create an object to manage code on a DSP system.
191 * This object can be used to load an initial program image with
192 * arguments that can later be expanded with
193 * dynamically loaded object files.
196 int cod_create(struct cod_manager **mgr, char *str_zl_file)
198 struct cod_manager *mgr_new;
199 struct dbll_attrs zl_attrs;
205 mgr_new = kzalloc(sizeof(struct cod_manager), GFP_KERNEL);
209 /* Set up loader functions */
210 mgr_new->fxns = ldr_fxns;
212 /* initialize the ZL module */
213 mgr_new->fxns.init_fxn();
215 zl_attrs.alloc = (dbll_alloc_fxn) no_op;
216 zl_attrs.free = (dbll_free_fxn) no_op;
217 zl_attrs.fread = (dbll_read_fxn) cod_f_read;
218 zl_attrs.fseek = (dbll_seek_fxn) cod_f_seek;
219 zl_attrs.ftell = (dbll_tell_fxn) cod_f_tell;
220 zl_attrs.fclose = (dbll_f_close_fxn) cod_f_close;
221 zl_attrs.fopen = (dbll_f_open_fxn) cod_f_open;
222 zl_attrs.sym_lookup = NULL;
223 zl_attrs.base_image = true;
224 zl_attrs.log_write = NULL;
225 zl_attrs.log_write_handle = NULL;
226 zl_attrs.write = NULL;
227 zl_attrs.rmm_handle = NULL;
228 zl_attrs.input_params = NULL;
229 zl_attrs.sym_handle = NULL;
230 zl_attrs.sym_arg = NULL;
232 mgr_new->attrs = zl_attrs;
234 status = mgr_new->fxns.create_fxn(&mgr_new->target, &zl_attrs);
241 /* return the new manager */
248 * ======== cod_delete ========
250 * Delete a code manager object.
252 void cod_delete(struct cod_manager *cod_mgr_obj)
254 if (cod_mgr_obj->base_lib) {
255 if (cod_mgr_obj->loaded)
256 cod_mgr_obj->fxns.unload_fxn(cod_mgr_obj->base_lib,
257 &cod_mgr_obj->attrs);
259 cod_mgr_obj->fxns.close_fxn(cod_mgr_obj->base_lib);
261 if (cod_mgr_obj->target) {
262 cod_mgr_obj->fxns.delete_fxn(cod_mgr_obj->target);
263 cod_mgr_obj->fxns.exit_fxn();
269 * ======== cod_get_base_lib ========
271 * Get handle to the base image DBL library.
273 int cod_get_base_lib(struct cod_manager *cod_mgr_obj,
274 struct dbll_library_obj **plib)
278 *plib = (struct dbll_library_obj *)cod_mgr_obj->base_lib;
284 * ======== cod_get_base_name ========
286 int cod_get_base_name(struct cod_manager *cod_mgr_obj, char *sz_name,
291 if (usize <= COD_MAXPATHLENGTH)
292 strlcpy(sz_name, cod_mgr_obj->sz_zl_file, usize);
300 * ======== cod_get_entry ========
302 * Retrieve the entry point of a loaded DSP program image
305 int cod_get_entry(struct cod_manager *cod_mgr_obj, u32 *entry_pt)
307 *entry_pt = cod_mgr_obj->entry;
313 * ======== cod_get_loader ========
315 * Get handle to the DBLL loader.
317 int cod_get_loader(struct cod_manager *cod_mgr_obj,
318 struct dbll_tar_obj **loader)
322 *loader = (struct dbll_tar_obj *)cod_mgr_obj->target;
328 * ======== cod_get_section ========
330 * Retrieve the starting address and length of a section in the COFF file
331 * given the section name.
333 int cod_get_section(struct cod_libraryobj *lib, char *str_sect,
336 struct cod_manager *cod_mgr_obj;
342 cod_mgr_obj = lib->cod_mgr;
343 status = cod_mgr_obj->fxns.get_sect_fxn(lib->dbll_lib, str_sect,
353 * ======== cod_get_sym_value ========
355 * Retrieve the value for the specified symbol. The symbol is first
356 * searched for literally and then, if not found, searched for as a
360 int cod_get_sym_value(struct cod_manager *cod_mgr_obj, char *str_sym,
363 struct dbll_sym_val *dbll_sym;
365 dev_dbg(bridge, "%s: cod_mgr_obj: %p str_sym: %s pul_value: %p\n",
366 __func__, cod_mgr_obj, str_sym, pul_value);
367 if (cod_mgr_obj->base_lib) {
368 if (!cod_mgr_obj->fxns.
369 get_addr_fxn(cod_mgr_obj->base_lib, str_sym, &dbll_sym)) {
370 if (!cod_mgr_obj->fxns.
371 get_c_addr_fxn(cod_mgr_obj->base_lib, str_sym,
379 *pul_value = dbll_sym->value;
385 * ======== cod_load_base ========
387 * Load the initial program image, optionally with command-line arguments,
388 * on the DSP system managed by the supplied handle. The program to be
389 * loaded must be the first element of the args array and must be a fully
390 * qualified pathname.
392 * if num_argc doesn't match the number of arguments in the args array, the
393 * args array is searched for a NULL terminating entry, and argc is
394 * recalculated to reflect this. In this way, we can support NULL
395 * terminating args arrays, if num_argc is very large.
397 int cod_load_base(struct cod_manager *cod_mgr_obj, u32 num_argc, char *args[],
398 cod_writefxn pfn_write, void *arb, char *envp[])
401 struct dbll_attrs save_attrs;
402 struct dbll_attrs new_attrs;
407 * Make sure every argv[] stated in argc has a value, or change argc to
408 * reflect true number in NULL terminated argv array.
410 for (i = 0; i < num_argc; i++) {
411 if (args[i] == NULL) {
417 /* set the write function for this operation */
418 cod_mgr_obj->fxns.get_attrs_fxn(cod_mgr_obj->target, &save_attrs);
420 new_attrs = save_attrs;
421 new_attrs.write = (dbll_write_fxn) pfn_write;
422 new_attrs.input_params = arb;
423 new_attrs.alloc = (dbll_alloc_fxn) no_op;
424 new_attrs.free = (dbll_free_fxn) no_op;
425 new_attrs.log_write = NULL;
426 new_attrs.log_write_handle = NULL;
429 flags = DBLL_CODE | DBLL_DATA | DBLL_SYMB;
430 status = cod_mgr_obj->fxns.load_fxn(cod_mgr_obj->base_lib, flags,
432 &cod_mgr_obj->entry);
434 cod_mgr_obj->fxns.close_fxn(cod_mgr_obj->base_lib);
437 cod_mgr_obj->loaded = true;
439 cod_mgr_obj->base_lib = NULL;
445 * ======== cod_open ========
446 * Open library for reading sections.
448 int cod_open(struct cod_manager *hmgr, char *sz_coff_path,
449 u32 flags, struct cod_libraryobj **lib_obj)
452 struct cod_libraryobj *lib = NULL;
456 lib = kzalloc(sizeof(struct cod_libraryobj), GFP_KERNEL);
462 status = hmgr->fxns.open_fxn(hmgr->target, sz_coff_path, flags,
469 pr_err("%s: error status 0x%x, sz_coff_path: %s flags: 0x%x\n",
470 __func__, status, sz_coff_path, flags);
475 * ======== cod_open_base ========
477 * Open base image for reading sections.
479 int cod_open_base(struct cod_manager *hmgr, char *sz_coff_path,
483 struct dbll_library_obj *lib;
485 /* if we previously opened a base image, close it now */
486 if (hmgr->base_lib) {
488 hmgr->fxns.unload_fxn(hmgr->base_lib, &hmgr->attrs);
489 hmgr->loaded = false;
491 hmgr->fxns.close_fxn(hmgr->base_lib);
492 hmgr->base_lib = NULL;
494 status = hmgr->fxns.open_fxn(hmgr->target, sz_coff_path, flags, &lib);
496 /* hang onto the library for subsequent sym table usage */
497 hmgr->base_lib = lib;
498 strncpy(hmgr->sz_zl_file, sz_coff_path, COD_MAXPATHLENGTH - 1);
499 hmgr->sz_zl_file[COD_MAXPATHLENGTH - 1] = '\0';
503 pr_err("%s: error status 0x%x sz_coff_path: %s\n", __func__,
504 status, sz_coff_path);
509 * ======== cod_read_section ========
511 * Retrieve the content of a code section given the section name.
513 int cod_read_section(struct cod_libraryobj *lib, char *str_sect,
514 char *str_content, u32 content_size)
520 lib->cod_mgr->fxns.read_sect_fxn(lib->dbll_lib, str_sect,
521 str_content, content_size);
529 * ======== no_op ========
534 static bool no_op(void)