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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
--- firmware/target/arm/rk27xx/app.lds ---
#include "config.h"

ENTRY(start)
#ifdef ROCKBOX_LITTLE_ENDIAN
OUTPUT_FORMAT(elf32-littlearm)
#else
OUTPUT_FORMAT(elf32-bigarm)
#endif
OUTPUT_ARCH(arm)
STARTUP(target/arm/rk27xx/crt0.o)

#define PLUGINSIZE PLUGIN_BUFFER_SIZE
#define CODECSIZE CODEC_SIZE

#define DRAMORIG 0x60000000
#define DRAMSIZE (MEMORYSIZE * 0x100000) - PLUGINSIZE - CODECSIZE

#define IRAMORIG 0x00000000
#define IRAMSIZE 4K

/* End of the audio buffer, where the codec buffer starts */
#define ENDAUDIOADDR  (DRAMORIG + DRAMSIZE)
#define CODECORIG (ENDAUDIOADDR)

/* Where the codec buffer ends, and the plugin buffer starts */
#define ENDADDR (ENDAUDIOADDR + CODECSIZE)

MEMORY
{
    DRAM  : ORIGIN = DRAMORIG, LENGTH = DRAMSIZE
    IRAM  : ORIGIN = IRAMORIG, LENGTH = IRAMSIZE
}

SECTIONS
{
  loadaddress = DRAMORIG;

  .intvect : {
    _intvectstart = . ;
    KEEP(*(.intvect))
    _intvectend = . ;  
  } > IRAM AT > DRAM
  _intvectcopy = LOADADDR(.intvect) ;

  .text : {
    *(.init.text)
    *(.text*)
    *(.glue_7*)
  } > DRAM

  .data : {
    *(.rodata*)
    *(.data*)
    *(.ncdata*);
    . = ALIGN(0x4);
  } > DRAM

  .iram :
  {
      _iramstart = .;
      *(.icode)
      *(.irodata)
      *(.idata)
      . = ALIGN(0x4);
      _iramend = .;
  } > DRAM
  _iramcopy = LOADADDR(.iram) ;

  .ibss (NOLOAD) :
  {
      _iedata = .;
      *(.qharray)
      *(.ibss)
      . = ALIGN(0x4);
      _iend = .;
  } > DRAM

  .stack (NOLOAD) :
  {
     *(.stack)
     _stackbegin = .;
     stackbegin = .;
     . += 0x2000;
     _stackend = .;
     stackend = .;
     _irqstackbegin = .;
     . += 0x400;
     _irqstackend = .;
     _fiqstackbegin = .;
     . += 0x400;
     _fiqstackend = .;
  } > DRAM

  .bss (NOLOAD) : {
     _edata = .;
     *(.bss*);
     *(.ibss);
     *(.ncbss*);
     *(COMMON);
    . = ALIGN(0x4);
     _end = .;
  } > DRAM

  .audiobuf (NOLOAD) :
  {
      . = ALIGN(4);
      _audiobuffer = .;
      audiobuffer = .;
  } > DRAM

  .audiobufend ENDAUDIOADDR (NOLOAD) :
  {
      audiobufend = .;
      _audiobufend = .;
  } > DRAM

  .codec CODECORIG (NOLOAD) :
  {
      codecbuf = .;
      _codecbuf = .;
  } > DRAM

  .plugin ENDADDR (NOLOAD) :
  {
      _pluginbuf = .;
      pluginbuf = .;
  }

}

--- firmware/target/arm/rk27xx/boot.lds ---
#include "config.h"

ENTRY(start)
#ifdef ROCKBOX_LITTLE_ENDIAN
OUTPUT_FORMAT(elf32-littlearm)
#else
OUTPUT_FORMAT(elf32-bigarm)
#endif
OUTPUT_ARCH(arm)
STARTUP(target/arm/rk27xx/crt0.o)

#define DRAMORIG 0x60000000
#define DRAMSIZE (MEMORYSIZE * 0x100000)

#define RUN_OFFSET 0x700000
#define DRAM_RUN_ORIG (DRAMORIG + RUN_OFFSET)
#define DRAM_RUN_SIZE (DRAMSIZE - RUN_OFFSET)

#define IRAMORIG 0x00000000
#define IRAMSIZE 4K

MEMORY
{
    DRAM  : ORIGIN = DRAMORIG, LENGTH = DRAMSIZE
    DRAM_RUN : ORIGIN = DRAM_RUN_ORIG, LENGTH = DRAM_RUN_SIZE
    IRAM  : ORIGIN = IRAMORIG, LENGTH = IRAMSIZE
}

SECTIONS
{
  .reloc : {
    *(.reloc)
  } > DRAM

  .intvect : {
    _intvectstart = . ;
    KEEP(*(.intvect))
    _intvectend = . ;  
  } > IRAM AT > DRAM
  _intvectcopy = LOADADDR(.intvect) ;

  .text : {
    _relocstart = .;
    *(.init.text)
    *(.text*)
    *(.glue_7*)
  } > DRAM_RUN AT > DRAM 

  .data : {
    *(.rodata*)
    *(.data*)
    *(.ncdata*);
    . = ALIGN(0x4);
  } > DRAM_RUN AT > DRAM

  .idata : {
    _datastart = . ;
    *(.irodata)
    *(.icode)
    *(.idata)
    . = ALIGN(0x4);
    _dataend = . ;
   } > DRAM_RUN AT > DRAM
   _datacopy = LOADADDR(.idata) ;

  .stack (NOLOAD) :
  {
     *(.stack)
     _stackbegin = .;
     stackbegin = .;
     . += 0x2000;
     _stackend = .;
     stackend = .;
     _irqstackbegin = .;
     . += 0x400;
     _irqstackend = .;
     _fiqstackbegin = .;
     . += 0x400;
     _fiqstackend = .;
  } > DRAM_RUN AT > DRAM

  .bss (NOLOAD) : {
     _edata = .;
     *(.bss*);
     *(.ibss);
     *(.ncbss*);
     *(COMMON);
    . = ALIGN(0x4);
     _end = .;
     _relocend = .;
  } > DRAM_RUN AT > DRAM
  _reloccopy = LOADADDR(.text) ;
}

--- bootloader/rk27xx.c ---
#include <stdio.h>
#include <system.h>
#include <inttypes.h>
#include "config.h"
#include "gcc_extensions.h"
#include "lcd.h"
#include "sysfont.h"
#include "backlight.h"
#include "button-target.h"
#include "common.h"
#include "storage.h"
#include "disk.h"
#include "panic.h"
#include "power.h"
#include "string.h"
#include "file.h"

#define DRAM_ORIG 0x60000000
#define LOAD_SIZE 0x700000

#define RKLD_MAGIC 0x4c44524b
#define RKW_HEADER_CRC 0x40000000
#define RKW_IMAGE_CRC 0x20000000

extern void show_logo( void );

struct rkw_header_t {
    uint32_t magic_number;    /* Magic number. 0x4C44524B */
    uint32_t header_size;     /* Size of the header */
    uint32_t image_base;      /* Base address of the firmware image */
    uint32_t load_address;    /* Load address */
    uint32_t load_limit;      /* End of the firmware image */
    uint32_t bss_start;       /* This is the start of .bss section of the firmware I suppose */
    uint32_t reserved0;       /* reserved - I've seen only zeros in this field so far */
    uint32_t reserved1;       /* reserved - I've seen only zeros in this field so far */
    uint32_t entry_point;     /* Entry point address */
    uint32_t load_options;    /* 0x80000000 - setup flag (I don't know what it means 
                               * but is present in every RKW I saw), 
                               * 0x40000000 - check header crc, 
                               * 0x20000000 - check firmware crc
                               */
    uint32_t crc;              /* crc32 of the header (excluding crc32 field itself) */
};

static uint32_t rkw_crc32(const void *src, uint32_t len)
{
    const unsigned char *buf = (const unsigned char *)src;

    /* polynomial 0x04c10db7 */
    static const uint32_t crc32_lookup[16] =
    {   /* lookup table for 4 bits at a time is affordable */
        0x00000000, 0x04C10DB7, 0x09821B6E, 0x0D4316D9,
        0x130436DC, 0x17C53B6B, 0x1A862DB2, 0x1E472005,
        0x26086DB8, 0x22C9600F, 0x2F8A76D6, 0x2B4B7B61,
        0x350C5B64, 0x31CD56D3, 0x3C8E400A, 0x384F4DBD
    };

    uint32_t crc32 = 0;
    unsigned char byte;
    uint32_t t;

    while (len--)
    {
        byte = *buf++; /* get one byte of data */

        /* upper nibble of our data */
        t = crc32 >> 28; /* extract the 4 most significant bits */
        t ^= byte >> 4; /* XOR in 4 bits of data into the extracted bits */
        crc32 <<= 4; /* shift the CRC register left 4 bits */
        crc32 ^= crc32_lookup[t]; /* do the table lookup and XOR the result */

        /* lower nibble of our data */
        t = crc32 >> 28; /* extract the 4 most significant bits */
        t ^= byte & 0x0F; /* XOR in 4 bits of data into the extracted bits */
        crc32 <<= 4; /* shift the CRC register left 4 bits */
        crc32 ^= crc32_lookup[t]; /* do the table lookup and XOR the result */
    }

    return crc32;
}

/* based on load_firmware() */
int load_rkw(unsigned char* buf, char* firmware, int buffer_size)
{
    int fd;
    int rc;
    int len;
    uint32_t crc, fw_crc;
    char filename[MAX_PATH];
    struct rkw_header_t rkw_info;

    snprintf(filename,sizeof(filename), BOOTDIR "/%s",firmware);
    fd = open(filename, O_RDONLY);

    if(fd < 0)
    {
        snprintf(filename,sizeof(filename),"/%s",firmware);
        fd = open(filename, O_RDONLY);
        if(fd < 0)
            return EFILE_NOT_FOUND;
    }

    rc = read(fd, &rkw_info, sizeof(rkw_info));
    if (rc < (int)sizeof(rkw_info))
        return -100; /* TODO introduce some meaningfull error code */

    /* check if RKW is valid */
    if (rkw_info.magic_number != RKLD_MAGIC)
    {
        printf("RKW invalid magic 0x%0x != 0x%0x", rkw_info.magic_number, RKLD_MAGIC);
        return -101;
    }

    /* check header crc if present */
    if (rkw_info.load_options & RKW_HEADER_CRC)
    {
        crc = rkw_crc32((uint8_t *)&rkw_info, sizeof(rkw_info)-sizeof(uint32_t));
        if (rkw_info.crc != crc)
        {
            printf("RKW header CRC error 0x%0x != 0x%0x", rkw_info.crc, crc);
            return -102;
        }
    }

    /* check image size */
    len = rkw_info.load_limit - rkw_info.load_address;
    if (len > buffer_size)
    {
        printf("RKW image too big");
        return EFILE_TOO_BIG;
    }

    /* check load address - we support loading only at 0x60000000 */
    if (rkw_info.load_address != (uint32_t)buf)
    {
        printf("RKW unsupported load address");
        return -103;
    }

    printf("Loading %s", firmware);

    /* skip header */
    lseek(fd, sizeof(rkw_info), SEEK_SET);

    /* load image into buffer */
    rc = read(fd, buf, len);

    if(rc < len)
    {
        printf("RKW loading failed");
        return EREAD_IMAGE_FAILED;
    }


    if (rkw_info.load_options & RKW_IMAGE_CRC)
    {
        rc = read(fd, &fw_crc, sizeof(uint32_t));

        crc = rkw_crc32((uint8_t *)buf, len);

        if (fw_crc != crc)
        {
            printf("RKW firmware CRC error 0x%0x != 0x%0x", fw_crc, crc);
            return EBAD_CHKSUM;
        }
    }

    close(fd);

    return EOK;
}

void main(void) NORETURN_ATTR;
void main(void)
{
    unsigned char* loadbuffer;
    void(*kernel_entry)(void);
    int ret;

    system_init();
    kernel_init();
    enable_irq();

    lcd_init();
    backlight_init();
    button_init_device();

    font_init();
    //lcd_setfont(FONT_SYSFIXED);

    show_logo();

    int btn = button_read_device();

    if (btn)
        lcd_clear_display();

    ret = storage_init();
    if(ret < 0)
        error(EATA, ret, true);

    while(!disk_init(IF_MV(0)))
        panicf("disk_init failed!");

    while((ret = disk_mount_all()) <= 0)
        error(EDISK, ret, true);

    printf("Loading firmware");
    loadbuffer = (unsigned char*)DRAM_ORIG; /* DRAM */
    
//    while((ret = load_firmware(loadbuffer, BOOTFILE, LOAD_SIZE)) < 0)
//        error(EBOOTFILE, ret, true);

    while((ret = load_rkw(loadbuffer, "rockbox.rkw", LOAD_SIZE)) < 0)
        error(EBOOTFILE, ret, true);

    kernel_entry = (void*) loadbuffer;
    commit_discard_idcache();
    printf("Executing");
    kernel_entry();

    printf("ERR: Failed to boot");

    /* hang */
    while(1);
}