1 /*
2  * doff.h
3  *
4  * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5  *
6  * Structures & definitions used for dynamically loaded modules file format.
7  * This format is a reformatted version of COFF. It optimizes the layout for
8  * the dynamic loader.
9  *
10  * .dof files, when viewed as a sequence of 32-bit integers, look the same
11  * on big-endian and little-endian machines.
12  *
13  * Copyright (C) 2005-2006 Texas Instruments, Inc.
14  *
15  * This package is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License version 2 as
17  * published by the Free Software Foundation.
18  *
19  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #ifndef _DOFF_H
25 #define _DOFF_H
26 
27 
28 #define BYTE_RESHUFFLE_VALUE 0x00010203
29 
30 /* DOFF file header containing fields categorizing the remainder of the file */
31 struct doff_filehdr_t {
32 
33 	/* string table size, including filename, in bytes */
34 	u32 df_strtab_size;
35 
36 	/* entry point if one exists */
37 	u32 df_entrypt;
38 
39 	/* identifies byte ordering of file;
40 	 * always set to BYTE_RESHUFFLE_VALUE */
41 	u32 df_byte_reshuffle;
42 
43 	/* Size of the string table up to and including the last section name */
44 	/* Size includes the name of the COFF file also */
45 	u32 df_scn_name_size;
46 
47 #ifndef _BIG_ENDIAN
48 	/* number of symbols */
49 	u16 df_no_syms;
50 
51 	/* length in bytes of the longest string, including terminating NULL */
52 	/* excludes the name of the file */
53 	u16 df_max_str_len;
54 
55 	/* total number of sections including no-load ones */
56 	u16 df_no_scns;
57 
58 	/* number of sections containing target code allocated or downloaded */
59 	u16 df_target_scns;
60 
61 	/* unique id for dll file format & version */
62 	u16 df_doff_version;
63 
64 	/* identifies ISA */
65 	u16 df_target_id;
66 
67 	/* useful file flags */
68 	u16 df_flags;
69 
70 	/* section reference for entry point, N_UNDEF for none, */
71 	/* N_ABS for absolute address */
72 	s16 df_entry_secn;
73 #else
74 	/* length of the longest string, including terminating NULL */
75 	u16 df_max_str_len;
76 
77 	/* number of symbols */
78 	u16 df_no_syms;
79 
80 	/* number of sections containing target code allocated or downloaded */
81 	u16 df_target_scns;
82 
83 	/* total number of sections including no-load ones */
84 	u16 df_no_scns;
85 
86 	/* identifies ISA */
87 	u16 df_target_id;
88 
89 	/* unique id for dll file format & version */
90 	u16 df_doff_version;
91 
92 	/* section reference for entry point, N_UNDEF for none, */
93 	/* N_ABS for absolute address */
94 	s16 df_entry_secn;
95 
96 	/* useful file flags */
97 	u16 df_flags;
98 #endif
99 	/* checksum for file header record */
100 	u32 df_checksum;
101 
102 };
103 
104 /* flags in the df_flags field */
105 #define  DF_LITTLE   0x100
106 #define  DF_BIG      0x200
107 #define  DF_BYTE_ORDER (DF_LITTLE | DF_BIG)
108 
109 /* Supported processors */
110 #define TMS470_ID   0x97
111 #define LEAD_ID     0x98
112 #define TMS32060_ID 0x99
113 #define LEAD3_ID    0x9c
114 
115 /* Primary processor for loading */
116 #if TMS32060
117 #define TARGET_ID   TMS32060_ID
118 #endif
119 
120 /* Verification record containing values used to test integrity of the bits */
121 struct doff_verify_rec_t {
122 
123 	/* time and date stamp */
124 	u32 dv_timdat;
125 
126 	/* checksum for all section records */
127 	u32 dv_scn_rec_checksum;
128 
129 	/* checksum for string table */
130 	u32 dv_str_tab_checksum;
131 
132 	/* checksum for symbol table */
133 	u32 dv_sym_tab_checksum;
134 
135 	/* checksum for verification record */
136 	u32 dv_verify_rec_checksum;
137 
138 };
139 
140 /* String table is an array of null-terminated strings.  The first entry is
141  * the filename, which is added by DLLcreate.  No new structure definitions
142  * are required.
143  */
144 
145 /* Section Records including information on the corresponding image packets */
146 /*
147  *      !!WARNING!!
148  *
149  * This structure is expected to match in form ldr_section_info in
150  * dynamic_loader.h
151  */
152 
153 struct doff_scnhdr_t {
154 
155 	s32 ds_offset;		/* offset into string table of name */
156 	s32 ds_paddr;		/* RUN address, in target AU */
157 	s32 ds_vaddr;		/* LOAD address, in target AU */
158 	s32 ds_size;		/* section size, in target AU */
159 #ifndef _BIG_ENDIAN
160 	u16 ds_page;		/* memory page id */
161 	u16 ds_flags;		/* section flags */
162 #else
163 	u16 ds_flags;		/* section flags */
164 	u16 ds_page;		/* memory page id */
165 #endif
166 	u32 ds_first_pkt_offset;
167 	/* Absolute byte offset into the file */
168 	/* where the first image record resides */
169 
170 	s32 ds_nipacks;		/* number of image packets */
171 
172 };
173 
174 /* Symbol table entry */
175 struct doff_syment_t {
176 
177 	s32 dn_offset;		/* offset into string table of name */
178 	s32 dn_value;		/* value of symbol */
179 #ifndef _BIG_ENDIAN
180 	s16 dn_scnum;		/* section number */
181 	s16 dn_sclass;		/* storage class */
182 #else
183 	s16 dn_sclass;		/* storage class */
184 	s16 dn_scnum;		/* section number, 1-based */
185 #endif
186 
187 };
188 
189 /* special values for dn_scnum */
190 #define  DN_UNDEF  0		/* undefined symbol */
191 #define  DN_ABS    (-1)		/* value of symbol is absolute */
192 /* special values for dn_sclass */
193 #define DN_EXT     2
194 #define DN_STATLAB 20
195 #define DN_EXTLAB  21
196 
197 /* Default value of image bits in packet */
198 /* Configurable by user on the command line */
199 #define IMAGE_PACKET_SIZE 1024
200 
201 /* An image packet contains a chunk of data from a section along with */
202 /* information necessary for its processing. */
203 struct image_packet_t {
204 
205 	s32 num_relocs;		/* number of relocations for */
206 	/* this packet */
207 
208 	s32 packet_size;	/* number of bytes in array */
209 	/* "bits" occupied  by */
210 	/* valid data.  Could be */
211 	/* < IMAGE_PACKET_SIZE to */
212 	/* prevent splitting a */
213 	/* relocation across packets. */
214 	/* Last packet of a section */
215 	/* will most likely contain */
216 	/* < IMAGE_PACKET_SIZE bytes */
217 	/* of valid data */
218 
219 	s32 img_chksum;		/* Checksum for image packet */
220 	/* and the corresponding */
221 	/* relocation records */
222 
223 	u8 *img_data;		/* Actual data in section */
224 
225 };
226 
227 /* The relocation structure definition matches the COFF version.  Offsets */
228 /* however are relative to the image packet base not the section base. */
229 struct reloc_record_t {
230 
231 	s32 vaddr;
232 
233 	/* expressed in target AUs */
234 
235 	union {
236 		struct {
237 #ifndef _BIG_ENDIAN
238 			u8 _offset;	/* bit offset of rel fld */
239 			u8 _fieldsz;	/* size of rel fld */
240 			u8 _wordsz;	/* # bytes containing rel fld */
241 			u8 _dum1;
242 			u16 _dum2;
243 			u16 _type;
244 #else
245 			unsigned _dum1:8;
246 			unsigned _wordsz:8;	/* # bytes containing rel fld */
247 			unsigned _fieldsz:8;	/* size of rel fld */
248 			unsigned _offset:8;	/* bit offset of rel fld */
249 			u16 _type;
250 			u16 _dum2;
251 #endif
252 		} _r_field;
253 
254 		struct {
255 			u32 _spc;	/* image packet relative PC */
256 #ifndef _BIG_ENDIAN
257 			u16 _dum;
258 			u16 _type;	/* relocation type */
259 #else
260 			u16 _type;	/* relocation type */
261 			u16 _dum;
262 #endif
263 		} _r_spc;
264 
265 		struct {
266 			u32 _uval;	/* constant value */
267 #ifndef _BIG_ENDIAN
268 			u16 _dum;
269 			u16 _type;	/* relocation type */
270 #else
271 			u16 _type;	/* relocation type */
272 			u16 _dum;
273 #endif
274 		} _r_uval;
275 
276 		struct {
277 			s32 _symndx;	/* 32-bit sym tbl index */
278 #ifndef _BIG_ENDIAN
279 			u16 _disp;	/* extra addr encode data */
280 			u16 _type;	/* relocation type */
281 #else
282 			u16 _type;	/* relocation type */
283 			u16 _disp;	/* extra addr encode data */
284 #endif
285 		} _r_sym;
286 	} _u_reloc;
287 
288 };
289 
290 /* abbreviations for convenience */
291 #ifndef TYPE
292 #define TYPE      _u_reloc._r_sym._type
293 #define UVAL      _u_reloc._r_uval._uval
294 #define SYMNDX    _u_reloc._r_sym._symndx
295 #define OFFSET    _u_reloc._r_field._offset
296 #define FIELDSZ   _u_reloc._r_field._fieldsz
297 #define WORDSZ    _u_reloc._r_field._wordsz
298 #define R_DISP      _u_reloc._r_sym._disp
299 #endif
300 
301 /**************************************************************************** */
302 /* */
303 /* Important DOFF macros used for file processing */
304 /* */
305 /**************************************************************************** */
306 
307 /* DOFF Versions */
308 #define         DOFF0                       0
309 
310 /* Return the address/size >= to addr that is at a 32-bit boundary */
311 /* This assumes that a byte is 8 bits */
312 #define         DOFF_ALIGN(addr)            (((addr) + 3) & ~3UL)
313 
314 /**************************************************************************** */
315 /* */
316 /* The DOFF section header flags field is laid out as follows: */
317 /* */
318 /*  Bits 0-3 : Section Type */
319 /*  Bit    4 : Set when section requires target memory to be allocated by DL */
320 /*  Bit    5 : Set when section requires downloading */
321 /*  Bits 8-11: Alignment, same as COFF */
322 /* */
323 /**************************************************************************** */
324 
325 /* Enum for DOFF section types (bits 0-3 of flag): See dynamic_loader.h */
326 #define DS_SECTION_TYPE_MASK	0xF
327 /* DS_ALLOCATE indicates whether a section needs space on the target */
328 #define DS_ALLOCATE_MASK            0x10
329 /* DS_DOWNLOAD indicates that the loader needs to copy bits */
330 #define DS_DOWNLOAD_MASK            0x20
331 /* Section alignment requirement in AUs */
332 #define DS_ALIGNMENT_SHIFT	8
333 
dload_check_type(struct doff_scnhdr_t * sptr,u32 flag)334 static inline bool dload_check_type(struct doff_scnhdr_t *sptr, u32 flag)
335 {
336 	return (sptr->ds_flags & DS_SECTION_TYPE_MASK) == flag;
337 }
ds_needs_allocation(struct doff_scnhdr_t * sptr)338 static inline bool ds_needs_allocation(struct doff_scnhdr_t *sptr)
339 {
340 	return sptr->ds_flags & DS_ALLOCATE_MASK;
341 }
342 
ds_needs_download(struct doff_scnhdr_t * sptr)343 static inline bool ds_needs_download(struct doff_scnhdr_t *sptr)
344 {
345 	return sptr->ds_flags & DS_DOWNLOAD_MASK;
346 }
347 
ds_alignment(u16 ds_flags)348 static inline int ds_alignment(u16 ds_flags)
349 {
350 	return 1 << ((ds_flags >> DS_ALIGNMENT_SHIFT) & DS_SECTION_TYPE_MASK);
351 }
352 
353 
354 #endif /* _DOFF_H */
355