summaryrefslogtreecommitdiffstats
path: root/flash/bootloader/bootloader.c
blob: 370ac5bee52ff83ecb5fffe4e7bd067c8bb726c2 (plain)
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2003 by Jörg Hohensohn
 *
 * Second-level bootloader, with dual-boot feature by holding F1/Menu
 * This is the image being descrambled and executed by the boot ROM.
 * It's task is to copy Rockbox from Flash to DRAM.
 * The image(s) in flash may optionally be compressed with UCL 2e
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "sh7034.h"
#include "bootloader.h"


#ifdef NO_ROM
/* start with the vector table */
UINT32 vectors[] __attribute__ ((section (".vectors"))) =
{
    (UINT32)_main,           /* entry point, the copy routine */
    (UINT32)(end_stack - 1), /* initial stack pointer */
    FLASH_BASE + 0x200,      /* source of image in flash */
    (UINT32)total_size,      /* size of image */
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0x03020080 /* mask and version (just as a suggestion) */
};
#else
/* our binary has to start with a vector to the entry point */
tpMain start_vector[] __attribute__ ((section (".startvector"))) = {main};
#endif

#ifdef NO_ROM /* some code which is only needed for the romless variant */
void _main(void)
{
    UINT32* pSrc;
    UINT32* pDest;
    UINT32* pEnd;
/*
    asm volatile ("ldc %0,sr" : : "r"(0xF0)); // disable interrupts
    asm volatile ("mov.l @%0,r15" : : "r"(4)); // load stack
    asm volatile ("ldc %0,vbr" : : "r"(0)); // load vector base
*/
    /* copy everything to IRAM and continue there */
    pSrc = begin_iramcopy;
    pDest = begin_text;
    pEnd = pDest + (begin_stack - begin_text);

    do
    {
        *pDest++ = *pSrc++;
    }
    while (pDest < pEnd);

    main(); /* jump to the real main() */
}


void BootInit(void)
{
    /* inits from the boot ROM, whether they make sense or not */
    PBDR  &= 0xFFBF; /* LED off (0x131E) */
    PBCR2  = 0;      /* all GPIO */
    PBIOR |= 0x0040; /* LED output */
    PBIOR &= 0xFFF1; /* LCD lines input */

    /* init DRAM like the boot ROM does */
    PACR2 &= 0xFFFB;
    PACR2 |= 0x0008;
    CASCR  = 0xAF;
    BCR   |= 0x8000;
    WCR1  &= 0xFDFD;
    DCR    = 0x0E00;
    RCR    = 0x5AB0;
    RTCOR  = 0x9605;
    RTCSR  = 0xA518;
}
#endif /* #ifdef NO_ROM */


int main(void)
{
    int nButton;

    PlatformInit(); /* model-specific inits */

    nButton = ButtonPressed();

    if (nButton == 3)
    {   /* F3 means start monitor */
        MiniMon();
    }
    else
    {
        tImage* pImage;
        pImage = GetStartImage(nButton); /* which image */
        DecompressStart(pImage); /* move into place and start it */
    }

    return 0; /* I guess we won't return  ;-) */
}


/* init code that is specific to certain platform */
void PlatformInit(void)
{
#ifdef NO_ROM
    BootInit(); /* if not started by boot ROM, we need to init what it did */
#endif

#if defined PLATFORM_PLAYER
    BRR1   = 0x19;   /* 14400 Baud for monitor */
    PACR2 &= 0xFFFC; /* GPIO for PA0 (charger detection, input by default) */
    if (FW_VERSION > 451 && (PADRL & 0x01))
    {   /* "new" Player and charger not plugged? */
        PBDR  |= 0x0010; /* set PB4 to 1 to power-up the harddisk early */
        PBIOR |= 0x0010; /* make PB4 an output */
    }
#elif defined PLATFORM_RECORDER
    BRR1 = 0x02;     /* 115200 Baud for monitor */
    if (ReadADC(7) > 0x100) /* charger plugged? */
    {   /* switch off the HD, else a flat battery may not start */
        PACR2 &= 0xFBFF; /* GPIO for PA5 */
        PAIOR |= 0x0020; /* make PA5 an output (low by default) */
    }
#elif defined PLATFORM_FM
    BRR1   = 0x02;   /* 115200 Baud for monitor */
    PBDR  |= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
    PBIOR |= 0x0020; /* make PB5 an output */
    if (ReadADC(0) < 0x1FF) /* charger plugged? */
    {   /* switch off the HD, else a flat battery may not start */
        PACR2 &= 0xFBFF; /* GPIO for PA5 */
        PAIOR |= 0x0020; /* make PA5 an output (low by default) */
    }
#elif defined PLATFORM_ONDIO
    BRR1   = 0x19;   /* 14400 Baud for monitor */
    PBDR  |= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
    PBIOR |= 0x0020; /* make PB5 an output */
#endif

    /* platform-independent inits */
    DCR |= 0x1000; /* enable burst mode on DRAM */
    BCR |= 0x2000; /* activate Warp mode (simultaneous internal and external
                    * mem access) */
}


/* Thinned out version of the UCL 2e decompression sourcecode
 * Original (C) Markus F.X.J Oberhumer under GNU GPL license */
#define GETBIT(bb, src, ilen) \
    (((bb = bb & 0x7f ? bb*2 : ((unsigned)src[ilen++]*2+1)) >> 8) & 1)

int ucl_nrv2e_decompress_8(
    const UINT8 *src, UINT8 *dst, UINT32* dst_len)
{
    UINT32 bb = 0;
    unsigned ilen = 0, olen = 0, last_m_off = 1;

    for (;;)
    {
        unsigned m_off, m_len;

        while (GETBIT(bb,src,ilen))
        {
            dst[olen++] = src[ilen++];
        }
        m_off = 1;
        for (;;)
        {
            m_off = m_off*2 + GETBIT(bb,src,ilen);
            if (GETBIT(bb,src,ilen)) break;
            m_off = (m_off-1)*2 + GETBIT(bb,src,ilen);
        }
        if (m_off == 2)
        {
            m_off = last_m_off;
            m_len = GETBIT(bb,src,ilen);
        }
        else
        {
            m_off = (m_off-3)*256 + src[ilen++];
            if (m_off == 0xffffffff)
                break;
            m_len = (m_off ^ 0xffffffff) & 1;
            m_off >>= 1;
            last_m_off = ++m_off;
        }
        if (m_len)
            m_len = 1 + GETBIT(bb,src,ilen);
        else if (GETBIT(bb,src,ilen))
            m_len = 3 + GETBIT(bb,src,ilen);
        else
        {
            m_len++;
            do {
                m_len = m_len*2 + GETBIT(bb,src,ilen);
            } while (!GETBIT(bb,src,ilen));
            m_len += 3;
        }
        m_len += (m_off > 0x500);
        {
            const UINT8 *m_pos;
            m_pos = dst + olen - m_off;
            dst[olen++] = *m_pos++;
            do dst[olen++] = *m_pos++; while (--m_len > 0);
        }
    }
    *dst_len = olen;

    return ilen;
}


/* move the image into place and start it */
void DecompressStart(tImage* pImage)
{
    UINT32* pSrc;
    UINT32* pDest;

    pSrc = pImage->image;
    pDest = pImage->pDestination;

    if (pSrc != pDest) /* if not linked to that flash address */
    {
        if (pImage->flags & IF_UCL_2E)
        {   /* UCL compressed, algorithm 2e */
            UINT32 dst_len; /* dummy */
            ucl_nrv2e_decompress_8((UINT8*)pSrc, (UINT8*)pDest, &dst_len);
        }
        else
        {   /* uncompressed, copy it */
            UINT32 size = pImage->size;
            UINT32* pEnd;
            size = (size + 3) / 4; /* round up to 32bit-words */
            pEnd = pDest + size;

            do
            {
                *pDest++ = *pSrc++;
            }
            while (pDest < pEnd);
        }
    }

    pImage->pExecute();
}

#ifdef USE_ADC
int ReadADC(int channel)
{
    /* after channel 3, the ports wrap and get re-used */
    volatile UINT16* pResult = (UINT16*)(ADDRAH_ADDR + 2 * (channel & 0x03));
    int timeout = 266; /* conversion takes 266 clock cycles */

    ADCSR = 0x20 | channel; /* start single conversion */
    while (((ADCSR & 0x80) == 0) && (--timeout)); /* 6 instructions per round*/

    return (timeout == 0) ? -1 : *pResult>>6;
}
#endif


/* This function is platform-dependent,
 * until I figure out how to distinguish at runtime. */
int ButtonPressed(void) /* return 1,2,3 for F1,F2,F3, 0 if none pressed */
{
#ifdef USE_ADC
    int value = ReadADC(CHANNEL);

    if (value >= F1_LOWER && value <= F1_UPPER) /* in range */
        return 1;
    else if (value >= F2_LOWER && value <= F2_UPPER) /* in range */
        return 2;
    else if (value >= F3_LOWER && value <= F3_UPPER) /* in range */
        return 3;
#else
    int value = PCDR;

    if (!(value & F1_MASK))
        return 1;
    else if (!(value & F2_MASK))
        return 2;
    else if (!(value & F3_MASK))
        return 3;
#endif

    return 0;
}


/* Determine the image to be started */
tImage* GetStartImage(int nPreferred)
{
    tImage* pImage1;
    tImage* pImage2 = NULL; /* default to not present */
    UINT32 pos;
    UINT32* pFlash = (UINT32*)FLASH_BASE;

    /* determine the first image position */
    pos = pFlash[2] + pFlash[3]; /* position + size of the bootloader
                                  * = after it */
    pos = (pos + 3) & ~3; /* be sure it's 32 bit aligned */

    pImage1 = (tImage*)pos;

    if (pImage1->size != 0)
    {   /* check for second image */
        pos = (UINT32)(&pImage1->image) + pImage1->size;
        pImage2 = (tImage*)pos;

        /* does it make sense? (not in FF or 00 erazed space) */
        if (pImage2->pDestination == (void*)0xFFFFFFFF
         || pImage2->size == 0xFFFFFFFF
         || pImage2->pExecute == (void*)0xFFFFFFFF
         || pImage2->flags == 0xFFFFFFFF
         || pImage2->pDestination == NULL)
         /* size, execute and flags can legally be 0 */
        {
            pImage2 = NULL; /* invalidate */
        }
    }

    if (pImage2 == NULL || nPreferred == 1)
    {   /* no second image or overridden: return the first */
        return pImage1;
    }

    return pImage2; /* return second image */
}

/* diagnostic functions */

void SetLed(BOOL bOn)
{
    if (bOn)
        PBDR |= 0x0040;
    else
        PBDR &= ~0x0040;
}


void UartInit(void)
{
    PBIOR &= 0xFBFF; /* input: RXD1 remote pin */
    PBCR1 |= 0x00A0; /* set PB11+PB10 to UART */
    PBCR1 &= 0xFFAF; /* clear bits 6, 4 -> UART */
    SMR1   = 0x00;   /* async format 8N1, baud generator input is CPU clock */
    SCR1   = 0x30;   /* transmit+receive enable */
    PBCR1 &= 0x00FF; /* set bit 12...15 as GPIO */
    SSR1  &= 0xBF;   /* clear bit 6 (RDRF, receive data register full) */
}


UINT8 UartRead(void)
{
    UINT8 byte;
    while (!(SSR1 & SCI_RDRF)); /* wait for char to be available */
    byte = RDR1;
    SSR1 &= ~SCI_RDRF;
    return byte;
}


void UartWrite(UINT8 byte)
{
    while (!(SSR1 & SCI_TDRE)); /* wait for transmit buffer empty */
    TDR1 = byte;
    SSR1 &= ~SCI_TDRE;
}


/* include the mini monitor as a rescue feature, started with F3 */
void MiniMon(void)
{
    UINT8 cmd;
    UINT32 addr;
    UINT32 size;
    UINT32 content;
    volatile UINT8* paddr = NULL;
    volatile UINT8* pflash = NULL; /* flash base address */

    UartInit();

    while (1)
    {
        cmd = UartRead();
        switch (cmd)
        {
        case BAUDRATE:
            content = UartRead();
            UartWrite(cmd); /* acknowledge by returning the command value */
            while (!(SSR1 & SCI_TEND)); /* wait for empty shift register,
                                         * before changing baudrate */
            BRR1 = content;
            break;

        case ADDRESS:
            addr = (UartRead() << 24) | (UartRead() << 16)
                  | (UartRead() << 8) | UartRead();
            paddr = (UINT8*)addr;
            pflash = (UINT8*)(addr & 0xFFF80000); /* round down to 512k align*/
            UartWrite(cmd); /* acknowledge by returning the command value */
            break;

        case BYTE_READ:
            content = *paddr++;
            UartWrite(content); /* the content is the ack */
            break;

        case BYTE_WRITE:
            content = UartRead();
            *paddr++ = content;
            UartWrite(cmd); /* acknowledge by returning the command value */
            break;

        case BYTE_READ16:
            size = 16;
            while (size--)
            {
                content = *paddr++;
                UartWrite(content); /* the content is the ack */
            }
            break;

        case BYTE_WRITE16:
            size = 16;
            while (size--)
            {
                content = UartRead();
                *paddr++ = content;
            }
            UartWrite(cmd); /* acknowledge by returning the command value */
            break;

        case BYTE_FLASH:
            content = UartRead();
            pflash[0x5555] = 0xAA; /* set flash to command mode */
            pflash[0x2AAA] = 0x55;
            pflash[0x5555] = 0xA0; /* byte program command */
            *paddr++ = content;
            UartWrite(cmd); /* acknowledge by returning the command value */
            break;

        case BYTE_FLASH16:
            size = 16;
            while (size--)
            {
                content = UartRead();
                pflash[0x5555] = 0xAA; /* set flash to command mode */
                pflash[0x2AAA] = 0x55;
                pflash[0x5555] = 0xA0; /* byte program command */
                *paddr++ = content;
            }
            UartWrite(cmd); /* acknowledge by returning the command value */
            break;

        case HALFWORD_READ:
            content = *(UINT16*)paddr;
            paddr += 2;
            UartWrite(content >> 8);   /* highbyte */
            UartWrite(content & 0xFF); /* lowbyte */
            break;

        case HALFWORD_WRITE:
            content = UartRead() << 8 | UartRead();
            *(UINT16*)paddr = content;
            paddr += 2;
            UartWrite(cmd); /* acknowledge by returning the command value */
            break;

        case EXECUTE:
            {
                tpFunc pFunc = (tpFunc)paddr;
                pFunc();
                UartWrite(cmd); /* acknowledge by returning the command value*/
            }
            break;

        case VERSION:
            UartWrite(1); /* return our version number */
            break;

        default:
            {
                SetLed(TRUE);
                UartWrite(~cmd); /* error acknowledge */
            }

        } /* case */
    } /* while (1) */
}