Date:2011-05-22 15:59:12 (12 years 10 months ago)
Author:Sergey Gridassov
Commit:c7395700e4595dd40b67bc833303ab844d898216
Message:Added Linux loader.

Files: include/debug.h (1 diff)
include/elf.h (1 diff)
include/elfldr.h (1 diff)
src/Makefile.am (1 diff)
src/debug.c (1 diff)
src/elfldr.c (1 diff)
src/ingenic.c (1 diff)
src/usbboot_cmdset.c (3 diffs)

Change Details

include/debug.h
2121
2222void set_debug_level(int level);
2323int get_debug_level();
24void hexdump(const void *data, size_t size);
2425
2526void debug(int level, const char *fmt, ...);
2627
include/elf.h
1#ifndef __ELF__H__
2#define __ELF__H__
3
4#include <stdint.h>
5
6typedef uint16_t Elf32_Half;
7typedef uint32_t Elf32_Word;
8typedef uint32_t Elf32_Addr;
9typedef uint32_t Elf32_Off;
10typedef int32_t Elf32_Sword;
11
12#define EI_NIDENT 16
13#define EI_MAG0 0
14#define EI_MAG1 1
15#define EI_MAG2 2
16#define EI_MAG3 3
17#define EI_CLASS 4
18#define EI_DATA 5
19#define EI_VERSION 6
20#define EI_PAD 7
21
22#define ELFMAG0 0x7F
23#define ELFMAG1 'E'
24#define ELFMAG2 'L'
25#define ELFMAG3 'F'
26
27#define ELFCLASSNONE 0
28#define ELFCLASS32 1
29#define ELFCLASS64 2
30
31#define ELFDATANONE 0
32#define ELFDATA2LSB 1
33#define ELFDATA2MSB 2
34
35#define ET_NONE 0
36#define ET_REL 1
37#define ET_EXEC 2
38#define ET_DYN 3
39#define ET_CORE 4
40
41#define EM_MIPS 8
42
43#define EV_NONE 0
44#define EV_CURRENT 1
45
46#define PT_NULL 0
47#define PT_LOAD 1
48
49typedef struct {
50          unsigned char e_ident[EI_NIDENT];
51          Elf32_Half e_type;
52          Elf32_Half e_machine;
53          Elf32_Word e_version;
54          Elf32_Addr e_entry;
55          Elf32_Off e_phoff;
56          Elf32_Off e_shoff;
57          Elf32_Word e_flags;
58          Elf32_Half e_ehsize;
59          Elf32_Half e_phentsize;
60          Elf32_Half e_phnum;
61          Elf32_Half e_shentsize;
62          Elf32_Half e_shnum;
63          Elf32_Half e_shstrndx;
64} Elf32_Ehdr;
65
66typedef struct {
67          Elf32_Word p_type;
68          Elf32_Off p_offset;
69          Elf32_Addr p_vaddr;
70          Elf32_Addr p_paddr;
71          Elf32_Word p_filesz;
72          Elf32_Word p_memsz;
73          Elf32_Word p_flags;
74          Elf32_Word p_align;
75} Elf32_Phdr;
76
77#endif
78
include/elfldr.h
1#ifndef __ELFLDR__H__
2#define __ELFLDR__H__
3
4int load_elf(void *ingenic,
5             const char *filename,
6             const char *args,
7             const char *initrd);
8
9#endif
10
src/Makefile.am
22
33bin_PROGRAMS = jzboot
44jzboot_SOURCES = debug.c devmgr.c ingenic.c main.c shell_lex.c \
5    usbdev.c shell.c shell_builtins.c config.c spl_cmdset.c usbboot_cmdset.c
5    usbdev.c shell.c shell_builtins.c config.c spl_cmdset.c \
6    usbboot_cmdset.c elfldr.c
src/debug.c
4545
4646    va_end(list);
4747}
48
49void hexdump(const void *data, size_t size) {
50        const unsigned char *bytes = data;
51
52        for(int i = 0; i < size; i+= 16) {
53                debug(LEVEL_DEBUG, "%04X ", i);
54
55                int chunk_size = size - i;
56        if(chunk_size > 16)
57            chunk_size = 16;
58
59                for(int j = 0; j < chunk_size; j++) {
60                        debug(LEVEL_DEBUG, "%02X ", bytes[i + j]);
61
62                        if(j == 7)
63                                debug(LEVEL_DEBUG, " ");
64                }
65
66                for(int j = 0; j < 16 - chunk_size; j++) {
67                        debug(LEVEL_DEBUG, " ");
68
69                        if(j == 8)
70                                debug(LEVEL_DEBUG, " ");
71                }
72
73                debug(LEVEL_DEBUG, "|");
74
75                for(int j = 0; j < chunk_size; j++) {
76                        debug(LEVEL_DEBUG, "%c", isprint(bytes[i + j]) ? bytes[i + j] : '.');
77                }
78
79                debug(LEVEL_DEBUG, "|\n");
80        }
81}
82
src/elfldr.c
1#include <sys/stat.h>
2#include <stdio.h>
3#include <errno.h>
4#include <string.h>
5#include <stdlib.h>
6#include "debug.h"
7#include "elfldr.h"
8#include "elf.h"
9#include "ingenic.h"
10
11#define max(a, b) ((a) > (b) ? (a) : (b))
12#define ALIGN(a) ((a + 4095) & ~4095)
13
14#define TRAMP_ARGC 0 // command line argument count
15#define TRAMP_ARGV 1 // command line argument array
16#define TRAMP_ARG2 2
17#define TRAMP_ARG3 3
18#define TRAMP_ENTRY 4 // Kernel entry point
19
20static const uint32_t trampoline_template[] = {
21    0x3c04ffff, // lui a0, 0xffff
22    0x3484ffff, // ori a0,a0,0xffff
23
24    0x3c05ffff, // lui a1, 0xffff
25    0x34a5ffff, // ori a1,a1,0xffff
26
27    0x3c06ffff, // lui a2, 0xffff
28    0x34c6ffff, // ori a2,a2,0xffff
29
30    0x3c07ffff, // lui a3, 0xffff
31    0x34e7ffff, // ori a3,a3,0xffff
32
33    0x3c08ffff, // lui t0, 0xffff
34    0x3508ffff, // ori t0,t0,0xffff
35    0x01000008, // jr t0
36    0x00000000, // nop
37};
38
39static const unsigned char valid_ident[EI_PAD] = {
40        [EI_MAG0] = ELFMAG0,
41        [EI_MAG1] = ELFMAG1,
42        [EI_MAG2] = ELFMAG2,
43        [EI_MAG3] = ELFMAG3,
44        [EI_CLASS] = ELFCLASS32,
45        [EI_DATA] = ELFDATA2LSB,
46        [EI_VERSION] = EV_CURRENT
47};
48
49static int load_segment(void *ingenic, void *data, uint32_t base, uint32_t filesz, uint32_t memsz) {
50    uint32_t end = base + memsz, tail = memsz - filesz;
51    int ret = 0;
52
53    printf("Loading segment: base 0x%08X, file 0x%08X, mem 0x%08X\n", base, filesz, memsz);
54
55    if(end > ingenic_sdram_size(ingenic) + SDRAM_BASE - STAGE2_CODESIZE) {
56        fputs(" Segment doesn't fit into SDRAM\n", stderr);
57
58        return -1;
59    }
60
61    if(filesz && ingenic_load_sdram(ingenic, data, base, filesz) == -1)
62        return -1;
63
64    if(tail) {
65        char *dummy_data = malloc(tail);
66
67        if(dummy_data == NULL)
68            return -1;
69
70        memset(dummy_data, 0, tail);
71
72        ret = ingenic_load_sdram(ingenic, dummy_data, base + filesz, tail);
73
74        free(dummy_data);
75    }
76
77    return ret;
78}
79
80static int load_elf_image(FILE *elf, void *ingenic, uint32_t *entry, uint32_t *end) {
81    Elf32_Ehdr ehdr;
82    Elf32_Phdr phdr;
83    int i, ret;
84    char *data;
85
86    if(fread(&ehdr, 1, sizeof(Elf32_Ehdr), elf) != sizeof(Elf32_Ehdr)) {
87        if(feof(elf))
88            errno = EINVAL;
89
90        return -1;
91     }
92
93    *entry = ehdr.e_entry;
94
95    if(memcmp(ehdr.e_ident, valid_ident, EI_PAD) != 0 || ehdr.e_type != ET_EXEC || ehdr.e_machine != EM_MIPS || ehdr.e_version != EV_CURRENT
96        || ehdr.e_phoff == 0 || ehdr.e_phentsize != sizeof(Elf32_Phdr)) {
97
98        fputs("Bad ELF identification\n", stderr);
99
100        errno = EINVAL;
101
102        return -1;
103        }
104
105    fseek(elf, ehdr.e_phoff, SEEK_SET);
106
107    *end = 0;
108
109     for(i = 0; i < ehdr.e_phnum; i++) {
110        if(fread(&phdr, 1, sizeof(Elf32_Phdr), elf) != sizeof(Elf32_Phdr)) {
111            if(feof(elf))
112                 errno = EINVAL;
113
114            return -1;
115        }
116
117        if(phdr.p_type == PT_LOAD) {
118            data = malloc(phdr.p_filesz);
119
120            if(data == NULL)
121                return -1;
122
123            long save = ftell(elf);
124            fseek(elf, phdr.p_offset, SEEK_SET);
125            ret = fread(data, 1, phdr.p_filesz, elf);
126            fseek(elf, save, SEEK_SET);
127
128            if(ret != phdr.p_filesz) {
129                free(data);
130
131                if(feof(elf))
132                     errno = EINVAL;
133
134                return -1;
135            }
136
137            ret = load_segment(ingenic, data, phdr.p_paddr, phdr.p_filesz, phdr.p_memsz);
138
139            free(data);
140
141            if(ret == -1)
142                return -1;
143
144            *end = max(phdr.p_paddr + phdr.p_memsz, *end);
145        }
146    }
147
148    return 0;
149}
150
151
152static void trampoline_set(uint32_t *trampoline, int index,
153                           uint32_t value) {
154    index *= 2;
155
156    trampoline[index] = (trampoline[index] & 0xFFFF0000) | ((value & 0xFFFF0000) >> 16);
157    trampoline[index + 1] = (trampoline[index + 1] & 0xFFFF0000) | (value & 0x0000FFFF);
158}
159
160static int load_args(void *ingenic, uint32_t base, const char *filename,
161                     const char *const *args, int *pargc, uint32_t *end) {
162
163    size_t total_len = 0;
164    int argc = 0;
165
166    for(int i = 0; args[i]; i++) {
167        size_t len = strlen(args[i]);
168        total_len += len + 1;
169
170        for(int j = 0; j < len; j++)
171            if(args[i][j] == ' ')
172                argc++;
173
174        argc++;
175    }
176
177    *pargc = argc;
178    *end = base + total_len + sizeof(uint32_t) * argc;
179
180    uint32_t *buf = malloc(sizeof(uint32_t) * argc + total_len);
181    if(buf == NULL)
182        return -1;
183
184    char *cmdline = (char *)(buf + argc);
185    size_t off = 0;
186
187    for(int i = 0; args[i]; i++) {
188        size_t len = strlen(args[i]);
189        memcpy(cmdline + off, args[i], len + 1);
190
191        if(args[i + 1])
192            cmdline[off + len] = ' ';
193
194        off += len + 1;
195    }
196
197    printf("Compiled cmdline: '%s'\n", cmdline);
198
199    uint32_t offset = 0;
200    char *ptr = cmdline;
201
202    for(int i = 0; i < argc; i++) {
203        buf[i] = base + argc * sizeof(uint32_t) + offset;
204
205        ptr = strchr(ptr, ' ');
206
207        if(ptr == NULL)
208            break;
209
210        *ptr++ = 0;
211        offset = ptr - cmdline;
212    }
213
214    int ret = load_segment(ingenic,
215                           buf,
216                           base,
217                           sizeof(uint32_t) * argc + total_len,
218                           sizeof(uint32_t) * argc + total_len);
219    free(buf);
220
221    return ret;
222}
223
224int load_elf(void *ingenic,
225             const char *filename,
226             const char *args,
227             const char *initrd) {
228
229    uint32_t entry, end, trampoline_base, args_base;
230    uint32_t initrd_base, initrd_size;
231
232    int argc;
233    const char *all_args[4] = { filename, args, NULL, NULL };
234    char initrd_args[64];
235
236    printf(
237        "Loading kernel %s:\n"
238        " Command line: '%s'\n",
239        filename,
240        args
241    );
242
243    FILE *elf = fopen(filename, "rb");
244
245    if(elf == NULL)
246        return -1;
247
248    int ret = load_elf_image(elf, ingenic, &entry, &end);
249
250    fclose(elf);
251
252    if(ret == -1)
253        return -1;
254
255    if(initrd) {
256        struct stat statbuf;
257
258        initrd_base = ALIGN(end);
259
260        if(stat(initrd, &statbuf) == -1)
261            return -1;
262
263        initrd_size = statbuf.st_size;
264
265        printf("Loading initrd to 0x%08X, size 0x%08X\n",
266               initrd_base, initrd_size);
267
268
269        end = initrd_base + initrd_size;
270
271        if(end > ingenic_sdram_size(ingenic) + SDRAM_BASE - STAGE2_CODESIZE) {
272            fputs(" Initrd doesn't fit into SDRAM\n", stderr);
273
274            return -1;
275        }
276
277        if(ingenic_load_sdram_file(ingenic, initrd_base, initrd) == -1)
278            return -1;
279
280        snprintf(initrd_args, sizeof(initrd_args),
281                 "rd_start=0x%08X rd_size=0x%08X",
282                 initrd_base, initrd_size);
283
284        all_args[2] = initrd_args;
285    }
286
287    args_base = ALIGN(end);
288
289    if(load_args(ingenic, args_base, filename, all_args, &argc, &end) == -1)
290        return -1;
291
292    trampoline_base = ALIGN(end);
293    end = trampoline_base + sizeof(trampoline_template);
294
295    uint32_t *trampoline = malloc(sizeof(trampoline_template));
296    if(trampoline == NULL)
297        return -1;
298
299    memcpy(trampoline, trampoline_template, sizeof(trampoline_template));
300    trampoline_set(trampoline, TRAMP_ARGC, argc);
301    trampoline_set(trampoline, TRAMP_ARGV, args_base);
302    trampoline_set(trampoline, TRAMP_ARG2, 0);
303    trampoline_set(trampoline, TRAMP_ARG3, 0);
304    trampoline_set(trampoline, TRAMP_ENTRY, entry);
305
306    ret = load_segment(ingenic, trampoline, trampoline_base, sizeof(trampoline_template), sizeof(trampoline_template));
307
308    free(trampoline);
309
310    if(ret == -1)
311        return -1;
312
313    printf("Image end: 0x%08X, entry: 0x%08X\n", end, entry);
314
315    //return 0;
316    return ingenic_go(ingenic, trampoline_base);
317}
318
src/ingenic.c
6767    { NULL, 0 }
6868};
6969
70static void hexdump(const void *data, size_t size) {
71        const unsigned char *bytes = data;
72
73        for(int i = 0; i < size; i+= 16) {
74                debug(LEVEL_DEBUG, "%04X ", i);
75
76                int chunk_size = size - i;
77        if(chunk_size > 16)
78            chunk_size = 16;
79
80                for(int j = 0; j < chunk_size; j++) {
81                        debug(LEVEL_DEBUG, "%02X ", bytes[i + j]);
82
83                        if(j == 7)
84                                debug(LEVEL_DEBUG, " ");
85                }
86
87                for(int j = 0; j < 16 - chunk_size; j++) {
88                        debug(LEVEL_DEBUG, " ");
89
90                        if(j == 8)
91                                debug(LEVEL_DEBUG, " ");
92                }
93
94                debug(LEVEL_DEBUG, "|");
95
96                for(int j = 0; j < chunk_size; j++) {
97                        debug(LEVEL_DEBUG, "%c", isprint(bytes[i + j]) ? bytes[i + j] : '.');
98                }
99
100                debug(LEVEL_DEBUG, "|\n");
101        }
102}
103
10470static uint32_t ingenic_probe(void *usb_hndl) {
10571    char magic[9];
10672
src/usbboot_cmdset.c
2323#include "shell.h"
2424#include "app_config.h"
2525#include "ingenic.h"
26#include "elfldr.h"
2627
2728static int usbboot_boot(shell_context_t *ctx, int argc, char *argv[]);
2829static int usbboot_load(shell_context_t *ctx, int argc, char *argv[]);
2930static int usbboot_go(shell_context_t *ctx, int argc, char *argv[]);
31static int usbboot_load_kernel(shell_context_t *ctx, int argc, char *argv[]);
3032static int usbboot_nquery(shell_context_t *ctx, int argc, char *argv[]);
3133static int usbboot_ndump(shell_context_t *ctx, int argc, char *argv[]);
3234static int usbboot_nerase(shell_context_t *ctx, int argc, char *argv[]);
...... 
3840    { "boot", "Reconfigure stage2", usbboot_boot, NULL },
3941    { "load", "Load file to SDRAM", usbboot_load, "<FILE> <BASE>" },
4042    { "go", "Jump to <ADDRESS>", usbboot_go, "<ADDRESS>" },
43    { "load_kernel", "Load ELF kernel and initrd to memory", usbboot_load_kernel, "<KERNEL> <CMDLINE> [INITRAMFS]" },
4144
4245    { "nquery", "Query NAND information", usbboot_nquery, "<DEVICE>" },
4346    { "ndump", "Dump NAND to file", usbboot_ndump, "<DEVICE> <STARTPAGE> <PAGES> <FILE>" },
...... 
151154
152155    return ret;
153156}
157
158static int usbboot_load_kernel(shell_context_t *ctx, int argc, char *argv[]) {
159    return load_elf(shell_device(ctx), argv[1], argv[2],
160                    argc == 4 ? argv[3] : NULL);
161}
162

Archive Download the corresponding diff file



interactive