Skip to content
This repository was archived by the owner on Jan 17, 2019. It is now read-only.

Commit 04a535d

Browse files
committed
rimage: Initial import of rimage firmware tool.
rimage converts ELF binaries to the firmware image file format understood by the kernel audio drivers. Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
0 parents  commit 04a535d

14 files changed

Lines changed: 1485 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.o
2+
*.in
3+
autom4te*
4+
*.Po
5+
*.swp
6+
Makefile
7+
config.*
8+
configure
9+
depcomp
10+
install-sh
11+
missing
12+
stamp-h1
13+
aclocal.m4
14+
compile
15+
ltmain.sh
16+
rimage/rimage
17+
rmbox/rmbox
18+
rwav/rwav

‎Makefile.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SUBDIRS = rimage rmbox

‎README‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Sound Open Firmware Tools
2+
=========================
3+
4+
This is a collection of open source tools used to develop open source audio
5+
DSP firmwares for ALSA.
6+
7+
Building and Installing
8+
=======================
9+
10+
./autogen.sh (only needed first time)
11+
./configure
12+
make
13+
make install
14+
15+
16+
rimage
17+
======
18+
19+
rimage is used to convert ELF executable firmware files to the firmware file
20+
formats used by the kernel drivers.
21+
22+
e.g.
23+
24+
rimage -i elf_file -o kernel_file -m machine
25+
26+
rimage can also convert kernel firmware formats to flat binaries formats to
27+
assist in debugging :-
28+
29+
e.g. convert to flat binary, then ELF then dissasemble
30+
31+
rimage -i /lib/firmware/intel/reef-byt.ri -o image.bin -b -m byt
32+
33+
xtensa-byt-elf-objcopy -I binary -O elf32-xtensa-le -B xtensa image.bin image.bin.elf
34+
35+
xtensa-byt-elf-objdump -D image.bin.elf > image.dis.txt
36+

‎autogen.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
libtoolize -c --force
3+
aclocal -I m4 --install
4+
autoconf -Wall
5+
autoheader
6+
automake -a --copy --foreign --add-missing

‎configure.ac‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
AC_PREREQ([2.69])
3+
AC_INIT([rimage], [0.1])
4+
AM_INIT_AUTOMAKE([foreign])
5+
AC_CONFIG_SRCDIR([rimage/rimage.c])
6+
AC_CONFIG_HEADERS([config.h])
7+
8+
AC_CANONICAL_HOST
9+
10+
dnl Initialize maintainer mode
11+
AM_MAINTAINER_MODE([enable])
12+
13+
AC_PROG_CC
14+
15+
AC_OUTPUT([
16+
Makefile
17+
rimage/Makefile
18+
rmbox/Makefile
19+
])
20+
21+
echo "
22+
23+
prefix: ${prefix}
24+
Compiler: ${CC}
25+
CFLAGS: ${CFLAGS}
26+
"

‎rimage/Makefile.am‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bin_PROGRAMS = rimage
2+
3+
noinst_HEADERS = \
4+
rimage.h
5+
6+
rimage_SOURCES = \
7+
cherrytrail.c \
8+
baytrail.c \
9+
flat.c \
10+
rimage.c

‎rimage/baytrail.c‎

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
/*
2+
* ELF to firmware image creator.
3+
*
4+
* Copyright (c) 2015, Intel Corporation.
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms and conditions of the GNU General Public License,
8+
* version 2, as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope it will be useful, but WITHOUT
11+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13+
* more details.
14+
*/
15+
16+
17+
#include <stdlib.h>
18+
#include <stdio.h>
19+
#include <unistd.h>
20+
#include <errno.h>
21+
#include <string.h>
22+
23+
#include "rimage.h"
24+
#include "file_format.h"
25+
26+
/* taken from the linker scripts */
27+
static const struct section byt_sections[] = {
28+
{"ResetVector", 0xff2c0000, 0x2e0},
29+
{"ResetVector.literal", 0xff2c02e0, 0x120},
30+
{"WindowVectors", 0xff2c0400, 0x178},
31+
{"Level2InterruptVector.literal", 0xff2c0578, 0x4},
32+
{"Level2InterruptVector", 0xff2c057c, 0x1c},
33+
{"Level3InterruptVector.literal", 0xff2c0598, 0x4},
34+
{"Level3InterruptVector", 0xff2c059c, 0x1c},
35+
{"Level4InterruptVector.literal", 0xff2c05b8, 0x4},
36+
{"Level4InterruptVector", 0xff2c05bc, 0x1c},
37+
{"Level5InterruptVector.literal", 0xff2c05d8, 0x4},
38+
{"Level5InterruptVector", 0xff2c05dc, 0x1c},
39+
{"DebugInterruptVector.literal", 0xff2c05d8, 0x4},
40+
{"NMIExceptionVector", 0xff2c061c},
41+
};
42+
43+
static const enum reef_module_id modules[] = {
44+
REEF_MODULE_BASE_FW,
45+
REEF_MODULE_AAC_5_1,
46+
REEF_MODULE_PCM,
47+
REEF_MODULE_PCM_SYSTEM,
48+
REEF_MODULE_PCM_CAPTURE,
49+
REEF_MODULE_PCM_REFERENCE,
50+
REEF_MODULE_BLUETOOTH_RENDER_MODULE,
51+
REEF_MODULE_BLUETOOTH_CAPTURE_MODULE,
52+
};
53+
54+
static int is_iram(struct image *image, Elf32_Shdr *section)
55+
{
56+
const struct adsp *adsp = image->adsp;
57+
uint32_t start, end;
58+
59+
start = section->sh_addr;
60+
end = section->sh_addr + section->sh_size;
61+
62+
if (start < adsp->iram_base)
63+
return 0;
64+
if (start >= adsp->iram_base + adsp->iram_size)
65+
return 0;
66+
return 1;
67+
}
68+
69+
static int is_dram(struct image *image, Elf32_Shdr *section)
70+
{
71+
const struct adsp *adsp = image->adsp;
72+
uint32_t start, end;
73+
74+
start = section->sh_addr;
75+
end = section->sh_addr + section->sh_size;
76+
77+
if (start < adsp->dram_base)
78+
return 0;
79+
if (start >= adsp->dram_base + adsp->dram_size)
80+
return 0;
81+
return 1;
82+
}
83+
84+
static int block_idx = 0;
85+
86+
static int write_block(struct image *image, Elf32_Shdr *section)
87+
{
88+
const struct adsp *adsp = image->adsp;
89+
struct dma_block_info block;
90+
size_t count;
91+
void *buffer;
92+
int ret;
93+
94+
block.size = section->sh_size;
95+
96+
if (is_iram(image, section)) {
97+
block.type = REEF_IRAM;
98+
block.ram_offset = section->sh_addr - adsp->iram_base;
99+
} else if (is_dram(image, section)) {
100+
block.type = REEF_DRAM;
101+
block.ram_offset = section->sh_addr - adsp->dram_base;
102+
} else {
103+
fprintf(stderr, "error: invalid block address/size 0x%x/0x%x\n",
104+
section->sh_addr, section->sh_size);
105+
return -EINVAL;
106+
}
107+
108+
/* write header */
109+
count = fwrite(&block, sizeof(block), 1, image->out_fd);
110+
if (count != 1)
111+
return -errno;
112+
113+
/* alloc data data */
114+
buffer = calloc(1, section->sh_size);
115+
if (buffer == NULL)
116+
return -ENOMEM;
117+
118+
/* read in section data */
119+
ret = fseek(image->in_fd, section->sh_offset, SEEK_SET);
120+
if (ret < 0) {
121+
fprintf(stderr, "error: cant seek to section %d\n", ret);
122+
goto out;
123+
}
124+
count = fread(buffer, 1, section->sh_size, image->in_fd);
125+
if (count != section->sh_size) {
126+
fprintf(stderr, "error: cant read section %d\n", -errno);
127+
ret = -errno;
128+
goto out;
129+
}
130+
131+
/* write out section data */
132+
count = fwrite(buffer, 1, section->sh_size, image->out_fd);
133+
if (count != section->sh_size) {
134+
fprintf(stderr, "error: cant write section %d\n", -errno);
135+
fprintf(stderr, " foffset %d size 0x%x mem addr 0x%x\n",
136+
section->sh_offset, section->sh_size, section->sh_addr);
137+
ret = -errno;
138+
goto out;
139+
}
140+
141+
if (image->verbose) {
142+
fprintf(stdout, "block: %d\n foffset %d\n size 0x%x\n mem addr 0x%x\n",
143+
block_idx++, section->sh_offset, section->sh_size,
144+
section->sh_addr);
145+
}
146+
147+
out:
148+
free(buffer);
149+
return ret;
150+
}
151+
152+
/* used by others */
153+
int byt_write_modules(struct image *image)
154+
{
155+
const struct adsp *adsp = image->adsp;
156+
struct byt_module_header hdr;
157+
Elf32_Shdr *section;
158+
size_t count;
159+
int i, err;
160+
uint32_t valid = (SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR);
161+
162+
memcpy(hdr.signature, REEF_FW_SIGN, REEF_FW_SIGNATURE_SIZE);
163+
hdr.blocks = image->num_sections - image->num_bss;
164+
hdr.mod_size = image->text_size + image->data_size + \
165+
sizeof(struct dma_block_info) * hdr.blocks;
166+
hdr.type = REEF_MODULE_BASE_FW;
167+
hdr.entry_point = 0;//section->sh_addr;
168+
169+
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
170+
if (count != 1) {
171+
fprintf(stderr, "error: failed to write section header %d\n", i);
172+
return -errno ;
173+
}
174+
175+
for (i = 0; i < image->hdr.e_shnum; i++) {
176+
177+
section = &image->section[i];
178+
179+
/* only write valid sections */
180+
if (!(image->section[i].sh_flags & valid))
181+
continue;
182+
183+
/* dont write bss */
184+
if (section->sh_type == SHT_NOBITS)
185+
continue;
186+
187+
err = write_block(image, section);
188+
if (err < 0) {
189+
fprintf(stderr, "error: failed to write section #%d\n", i);
190+
return err;
191+
}
192+
}
193+
194+
/* write the remaining 7 fake modules headers, to make the linux driver happy */
195+
hdr.mod_size = 0;
196+
hdr.blocks = 0;
197+
198+
for (i = 1; i < sizeof(modules) / sizeof(modules[0]); i++) {
199+
hdr.type = modules[i];
200+
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
201+
if (count != 1) {
202+
fprintf(stderr, "error: failed to write section header %d\n", i);
203+
return -errno ;
204+
}
205+
}
206+
207+
return 0;
208+
}
209+
210+
/* used by others */
211+
int byt_write_header(struct image *image)
212+
{
213+
struct fw_header hdr;
214+
size_t count;
215+
216+
memcpy(hdr.signature, REEF_FW_SIGN, REEF_FW_SIGNATURE_SIZE);
217+
218+
hdr.modules = sizeof(modules) / sizeof(modules[0]);
219+
hdr.file_format = 0;
220+
221+
image->fw_size += sizeof(struct dma_block_info) *
222+
(image->num_sections - image->num_bss);
223+
image->fw_size += sizeof(struct byt_module_header) * hdr.modules;
224+
hdr.file_size = image->fw_size;
225+
226+
fprintf(stdout, "fw: image size %ld (0x%lx) bytes %d modules\n\n",
227+
hdr.file_size + sizeof(hdr), hdr.file_size + sizeof(hdr),
228+
hdr.modules);
229+
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
230+
if (count != 1)
231+
return -errno;
232+
233+
return 0;
234+
}
235+
236+
const struct adsp byt_machine = {
237+
.name = "byt",
238+
.iram_base = 0xff2c0000,
239+
.iram_size = 0x14000,
240+
.dram_base = 0xff300000,
241+
.dram_size = 0x28000,
242+
.image_size = 0xff300000 - 0xff2c0000 + 0x28000,
243+
.dram_offset = 0xff300000 - 0xff2c0000,
244+
.machine_id = MACHINE_BAYTRAIL,
245+
.ops = {
246+
.write_header = byt_write_header,
247+
.write_modules = byt_write_modules,
248+
},
249+
.sections = byt_sections,
250+
};

0 commit comments

Comments
 (0)