1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
diff --git a/apps/codecs.c b/apps/codecs.c
index 69204b7..eb6591d 100644
--- a/apps/codecs.c
+++ b/apps/codecs.c
@@ -52,6 +52,8 @@
 #include "general.h"
 #include "rbpaths.h"
 
+#include "elfload.h"
+
 #define LOGF_ENABLE
 #include "logf.h"
 
@@ -69,7 +71,8 @@ static unsigned char codecbuf[CODEC_SIZE];
 extern unsigned char codecbuf[];
 #endif
 
-static size_t codec_size;
+static size_t codec_size = 0;
+static void *curr_handle = NULL;
 
 extern void* plugin_get_audio_buffer(size_t *buffer_size);
 
@@ -182,7 +185,7 @@ void *codec_get_buffer_callback(size_t *size)
     ssize_t s = CODEC_SIZE - codec_size;
 
     if (s <= 0)
-        return NULL;
+         return NULL;
 
     *size = s;
     ALIGN_BUFFER(buf, *size, CACHEALIGN_SIZE);
@@ -191,7 +194,6 @@ void *codec_get_buffer_callback(size_t *size)
 }
 
 /** codec loading and call interface **/
-static void *curr_handle = NULL;
 static struct codec_header *c_hdr = NULL;
 
 static int codec_load_ram(struct codec_api *api)
@@ -209,8 +211,8 @@ static int codec_load_ram(struct codec_api *api)
             )
         || hdr->target_id != TARGET_ID
 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
-        || hdr->load_addr != codecbuf
-        || hdr->end_addr > codecbuf + CODEC_SIZE
+//        || hdr->load_addr != codecbuf
+//        || hdr->end_addr > codecbuf + CODEC_SIZE
 #endif
         )
     {
@@ -236,6 +238,8 @@ static int codec_load_ram(struct codec_api *api)
 
     *(c_hdr->api) = api;
 
+    commit_discard_idcache();
+
     logf("Codec: calling entrypoint");
     return c_hdr->entry_point(CODEC_LOAD);
 }
@@ -262,10 +266,17 @@ int codec_load_buf(int hid, struct codec_api *api)
 int codec_load_file(const char *plugin, struct codec_api *api)
 {
     char path[MAX_PATH];
+    struct mem_info_t mem_info;
+
+    mem_info.dram = codec_get_buffer_callback(&mem_info.dram_size);
+
+    mem_info.iram = (void *)PLUGIN_IRAMORIG;
+    mem_info.iram_size = (size_t)PLUGIN_IRAMSIZE;
 
     codec_get_full_path(path, plugin);
 
-    curr_handle = lc_open(path, codecbuf, CODEC_SIZE);
+    /* curr_handle = lc_open(path, codecbuf, CODEC_SIZE); */
+    curr_handle = elf_open(path, &mem_info);
 
     if (curr_handle == NULL) {
         logf("Codec: cannot read file");
diff --git a/apps/elfload.c b/apps/elfload.c
index a993b26..6b2d53a 100644
--- a/apps/elfload.c
+++ b/apps/elfload.c
@@ -35,6 +35,7 @@
 #include "filefuncs.h"
 #include "load_code.h" /* lc_*() */
 #include "audio.h"     /* sound_stop() */
+#include "codecs.h"    /* CODEC_MAGIC */
 
 #include "elf.h"       /* ELF types, defs and macros */
 #include "elfload.h"
@@ -616,8 +617,9 @@ static void apply_veneers(void)
                     *(Elf32_Word *)(v->where) = vi->instr[j].data;
                     v->where = (Elf32_Word *)((char *)v->where + 4);
                     break;
-#ifdef USE_THUMB
+
                 case THUMB_TYPE:
+#ifdef USE_THUMB
                     *(Elf32_Half *)(v->where) = vi->instr[j].data;
                     v->where = (Elf32_Word *)((char *)v->where + 2);
                     break;
@@ -880,10 +882,12 @@ static int check_section_mem_req(Elf32_Shdr *shdr,
             return -1;
         }
 
-        /* If we are going to use iram we need to stop playback first
-         * so codec will not overwrite our claimed mem
+        /* If we load plugin and we are going to use iram we need 
+         * to stop playback first so codec will not overwrite our
+         * claimed mem
          */
-        audio_stop();
+        if (((struct lc_header *)m->dram)->magic != CODEC_MAGIC)
+            audio_stop();
     }
 
     return 0;
diff --git a/lib/rbcodec/codecs/codec_crt0.c b/lib/rbcodec/codecs/codec_crt0.c
index e3c3321..44b51e2 100644
--- a/lib/rbcodec/codecs/codec_crt0.c
+++ b/lib/rbcodec/codecs/codec_crt0.c
@@ -33,33 +33,6 @@ int _start(void) {return 0;}
 
 enum codec_status codec_start(enum codec_entry_call_reason reason)
 {
-#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
-    if (reason == CODEC_LOAD)
-    {
-#ifdef USE_IRAM
-        extern char iramcopy[], iramstart[], iramend[], iedata[], iend[];
-        size_t iram_size = iramend - iramstart;
-        size_t ibss_size = iend - iedata;
-        if (iram_size > 0 || ibss_size > 0)
-        {
-            ci->memcpy(iramstart, iramcopy, iram_size);
-            ci->memset(iedata, 0, ibss_size);
-            /* make the icache (if it exists) up to date with the new code */
-            ci->commit_discard_idcache();
-            /* barrier to prevent reordering iram copy and BSS clearing,
-             * because the BSS segment alias the IRAM copy.
-             */
-            asm volatile ("" ::: "memory");
-        }
-#endif /* PLUGIN_USE_IRAM */
-        ci->memset(plugin_bss_start, 0, plugin_end_addr - plugin_bss_start);
-        /* Some parts of bss may be used via a no-cache alias (at least
-         * portalplayer has this). If we don't clear the cache, those aliases
-         * may read garbage */
-        ci->commit_dcache();
-    }
-#endif /* CONFIG_PLATFORM */
-
     /* Note: If for any reason codec_main would not be called with CODEC_LOAD
      * because the above code failed then it must not be ever be called with
      * any other value and some strategy to avoid doing so must be conceived */
diff --git a/lib/rbcodec/codecs/codecs.make b/lib/rbcodec/codecs/codecs.make
index 844a8f4..95361e4 100644
--- a/lib/rbcodec/codecs/codecs.make
+++ b/lib/rbcodec/codecs/codecs.make
@@ -122,7 +122,7 @@ endif
 
 ifndef APP_TYPE
   CONFIGFILE := $(FIRMDIR)/export/config/$(MODELNAME).h
-  CODEC_LDS := $(APPSDIR)/plugins/plugin.lds # codecs and plugins use same file
+  CODEC_LDS := $(APPSDIR)/plugins/plugin_elf.lds # codecs and plugins use same file
   CODECLINK_LDS := $(CODECDIR)/codec.link
 endif
 
@@ -192,7 +192,7 @@ ifdef APP_TYPE
  CODECLDFLAGS = $(SHARED_LDFLAG) -Wl,--gc-sections -Wl,-Map,$(CODECDIR)/$*.map
  CODECFLAGS += $(SHARED_CFLAGS) # <-- from Makefile
 else
- CODECLDFLAGS = -T$(CODECLINK_LDS) -Wl,--gc-sections -Wl,-Map,$(CODECDIR)/$*.map
+ CODECLDFLAGS = -T$(CODECLINK_LDS) -Wl,--gc-sections -Wl,-r -Wl,-Map,$(CODECDIR)/$*.map
  CODECFLAGS += -UDEBUG -DNDEBUG
 endif
 CODECLDFLAGS += $(GLOBAL_LDOPTS)
@@ -204,8 +204,11 @@ $(CODECDIR)/%-pre.map: $(CODEC_CRT0) $(CODECLINK_LDS) $(CODECDIR)/%.o $(CODECS_L
     -lgcc $(subst .map,-pre.map,$(CODECLDFLAGS))
 
 $(CODECDIR)/%.codec: $(CODECDIR)/%.o
-  $(call PRINTS,LD $(@F))$(CC) $(CODECFLAGS) -o $(CODECDIR)/$*.elf \
+  $(call PRINTS,LD $(@F))$(CC) $(CODECFLAGS) -o $@ \
     $(filter %.o, $^) \
     $(filter %.a, $+) \
     -lgcc $(CODECLDFLAGS)
-  $(SILENT)$(call objcopy,$(CODECDIR)/$*.elf,$@)
+
+  $(call PRINTS,STRIP $(subst $(ROOTDIR)/,,$@))$(STRIP) \
+    --strip-unneeded --strip-debug \
+    -R .comment -R .ARM.attributes $@