-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipu-load.c
295 lines (232 loc) · 6.3 KB
/
ipu-load.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
Copyright (c) 2015 Mans Rullgard <mans@mansr.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/cachectl.h>
#include <libelf.h>
#include "iboot.h"
#define IBOOT_PHYS 0xc8000000
#define IMAGE_PHYS 0xcc000000
#define IPU_BASE 0x000f0000
#define IPU_INTC 0xe000
#define IPU_REMAP 0xf000
#define REMAP_SIZE 0x04000000
#define REMAP_MASK (REMAP_SIZE - 1)
#define REMAP_IDX(a) (1 + (((a) >> 26) & 7))
#define align(v, a) (((v) + (a) - 1) & ~((a) - 1))
#define max(a, b) ((a) > (b) ? (a) : (b))
static long pagesize;
static long pagemask;
static unsigned remap[8];
static inline void writel(uint32_t val, void *addr)
{
*(volatile uint32_t *)addr = val;
__asm__ volatile ("sync");
}
static void update_remap(unsigned va, unsigned pa)
{
unsigned ri = REMAP_IDX(va);
if (ri < 8)
remap[ri] = pa & ~REMAP_MASK;
else
fprintf(stderr, "invalid segment address %08x\n", va);
}
static inline unsigned combine(unsigned base, unsigned addr)
{
return (base & ~REMAP_MASK) | (addr & REMAP_MASK);
}
static inline unsigned virt2phys(unsigned va)
{
return combine(remap[REMAP_IDX(va)], va);
}
static int load_segment(const char *elf, Elf32_Phdr *ph,
int memfd, unsigned lma, int dobss)
{
unsigned map_base, map_size, offset;
char *mem, *dst, *bss;
map_base = lma & ~pagemask;
offset = lma & pagemask;
map_size = align(ph->p_memsz + offset, pagesize);
mem = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED,
memfd, map_base);
if (mem == MAP_FAILED) {
perror("/dev/mem");
return 1;
}
dst = mem + offset;
bss = dst + ph->p_filesz;
memcpy(dst, elf + ph->p_offset, ph->p_filesz);
memset(bss, 0, -ph->p_filesz & 4);
if (dobss)
memset(bss, 0, ph->p_memsz - ph->p_filesz);
_flush_cache(dst, ph->p_memsz, DCACHE);
munmap(mem, map_size);
return 0;
}
static unsigned load_elf(int memfd, const char *name, unsigned pa_base,
struct image *img, unsigned vl)
{
unsigned top = 0;
unsigned pa, va, pl;
struct stat st;
unsigned msize;
char *elfm;
Elf *elf;
Elf32_Phdr *phdr;
size_t num_phdr;
int nload = 0;
int efd;
int err;
int i;
efd = open(name, O_RDONLY);
if (efd < 0) {
perror(name);
return 0;
}
err = fstat(efd, &st);
if (err) {
perror("fstat");
return 0;
}
msize = align(st.st_size, pagesize);
elfm = mmap(NULL, msize, PROT_READ, MAP_SHARED, efd, 0);
if (elfm == MAP_FAILED) {
perror("mmap");
goto e_close;
}
elf = elf_memory(elfm, st.st_size);
if (!elf)
goto e_elf;
if (elf_getphdrnum(elf, &num_phdr) < 0)
goto e_elf;
if (img) {
Elf32_Ehdr *ehdr = elf32_getehdr(elf);
img->entry = ehdr->e_entry;
}
phdr = elf32_getphdr(elf);
if (!phdr)
goto e_elf;
for (i = 0; i < num_phdr; i++) {
if (phdr[i].p_type != PT_LOAD)
continue;
if (phdr[i].p_vaddr < REMAP_SIZE)
continue;
va = phdr[i].p_vaddr;
pa = combine(pa_base, va);
pl = vl ? virt2phys(vl) : pa;
load_segment(elfm, &phdr[i], memfd, pl, !img);
update_remap(va, pa);
if (img) {
struct segment *s = &img->segments[nload++];
s->vma = va;
s->lma = vl;
s->fsz = phdr[i].p_filesz;
s->msz = phdr[i].p_memsz;
vl += phdr[i].p_filesz;
}
top = max(top, va + phdr[i].p_memsz);
}
if (img)
img->num_segments = nload;
e_elf:
err = elf_errno();
if (err)
fprintf(stderr, "%s: %s\n", name, elf_errmsg(err));
elf_end(elf);
munmap(elfm, msize);
e_close:
close(efd);
return top;
}
int main(int argc, char **argv)
{
struct image *img;
char *img_base;
int mfd, mfd_nc;
unsigned tva;
char *ipu;
int i;
if (argc < 3)
return 1;
mfd = open("/dev/mem", O_RDWR);
if (mfd == -1) {
perror("/dev/mem");
return 1;
}
mfd_nc = open("/dev/mem", O_RDWR | O_SYNC);
if (mfd_nc == -1) {
perror("/dev/mem");
return 1;
}
pagesize = sysconf(_SC_PAGESIZE);
pagemask = pagesize - 1;
if (elf_version(EV_CURRENT) == EV_NONE) {
fprintf(stderr, "%s\n", elf_errmsg(0));
return 1;
}
for (i = 0; i < 8; i++)
remap[i] = -1;
tva = load_elf(mfd, argv[1], IBOOT_PHYS, NULL, 0);
if (!tva) {
fprintf(stderr, "failed to load iboot\n");
return 1;
}
tva = align(tva, 0x10);
img_base = mmap(NULL, 2 * pagesize, PROT_READ | PROT_WRITE, MAP_SHARED,
mfd_nc, virt2phys(tva) & ~pagemask);
if (img_base == MAP_FAILED) {
perror("mmap");
return 1;
}
img = (struct image *)(img_base + (tva & pagemask));
tva = load_elf(mfd, argv[2], IMAGE_PHYS, img, tva + 0x100);
if (!tva) {
fprintf(stderr, "failed to load image\n");
return 1;
}
munmap(img_base, 2 * pagesize);
ipu = mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED,
mfd_nc, IPU_BASE);
if (ipu == MAP_FAILED) {
perror("mmap");
return 1;
}
/* disable external interrupts */
writel(-1, ipu + IPU_INTC + 0x00c);
writel(-1, ipu + IPU_INTC + 0x10c);
writel(-1, ipu + IPU_INTC + 0x30c);
for (i = 0; i < 8; i++)
writel(remap[i], ipu + IPU_REMAP + 4 * i);
/* enable and trigger software interrupt */
writel(1, ipu + IPU_INTC + 0x08);
writel(1, ipu + IPU_INTC + 0x10);
munmap(ipu, 0x10000);
close(mfd);
close(mfd_nc);
return 0;
}