summaryrefslogtreecommitdiff
path: root/firmware/target/mips/ingenic_x1000/boot-x1000.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/mips/ingenic_x1000/boot-x1000.c')
-rw-r--r--firmware/target/mips/ingenic_x1000/boot-x1000.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/firmware/target/mips/ingenic_x1000/boot-x1000.c b/firmware/target/mips/ingenic_x1000/boot-x1000.c
new file mode 100644
index 0000000000..d3e6eb35d7
--- /dev/null
+++ b/firmware/target/mips/ingenic_x1000/boot-x1000.c
@@ -0,0 +1,109 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2022 Aidan MacDonald
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "boot-x1000.h"
23#include "system.h"
24#include <string.h>
25
26#define HDR_BEGIN 128 /* header must begin within this many bytes */
27#define HDR_LEN 256 /* header length cannot exceed this */
28
29/* search for header value, label must be a 4-character string.
30 * Returns the found value or 0 if the label wasn't found. */
31static uint32_t search_header(const unsigned char* source, size_t length,
32 const char* label)
33{
34 size_t search_len = MIN(length, HDR_BEGIN);
35 if(search_len < 8)
36 return 0;
37 search_len -= 7;
38
39 /* find the beginning marker */
40 size_t i;
41 for(i = 8; i < search_len; i += 4)
42 if(!memcmp(&source[i], "BEGINHDR", 8))
43 break;
44 if(i >= search_len)
45 return 0;
46 i += 8;
47
48 /* search within the header */
49 search_len = MIN(length, i + HDR_LEN) - 7;
50 for(; i < search_len; i += 8) {
51 if(!memcmp(&source[i], "ENDH", 4)) {
52 break;
53 } else if(!memcmp(&source[i], label, 4)) {
54 i += 4;
55 /* read little-endian value */
56 uint32_t ret = source[i];
57 ret |= source[i+1] << 8;
58 ret |= source[i+2] << 16;
59 ret |= source[i+3] << 24;
60 return ret;
61 }
62 }
63
64 return 0;
65}
66
67static void iram_memmove(void* dest, const void* source, size_t length)
68 __attribute__((section(".icode")));
69
70static void iram_memmove(void* dest, const void* source, size_t length)
71{
72 unsigned char* d = dest;
73 const unsigned char* s = source;
74
75 if(s < d && d < s + length) {
76 d += length;
77 s += length;
78 while(length--)
79 *--d = *--s;
80 } else {
81 while(length--)
82 *d++ = *s++;
83 }
84}
85
86void x1000_boot_rockbox(const void* source, size_t length)
87{
88 uint32_t load_addr = search_header(source, length, "LOAD");
89 if(!load_addr)
90 load_addr = X1000_STANDARD_DRAM_BASE;
91
92 disable_irq();
93
94 /* --- Beyond this point, do not call into DRAM --- */
95
96 iram_memmove((void*)load_addr, source, length);
97 commit_discard_idcache();
98
99 typedef void(*entry_fn)(void);
100 entry_fn fn = (entry_fn)load_addr;
101 fn();
102 while(1);
103}
104
105void rolo_restart(const unsigned char* source, unsigned char* dest, int length)
106{
107 (void)dest;
108 x1000_boot_rockbox(source, length);
109}