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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2022 Aidan MacDonald
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "boot-x1000.h"
#include "system.h"
#include <string.h>
#define HDR_BEGIN 128 /* header must begin within this many bytes */
#define HDR_LEN 256 /* header length cannot exceed this */
/* search for header value, label must be a 4-character string.
* Returns the found value or 0 if the label wasn't found. */
static uint32_t search_header(const unsigned char* source, size_t length,
const char* label)
{
size_t search_len = MIN(length, HDR_BEGIN);
if(search_len < 8)
return 0;
search_len -= 7;
/* find the beginning marker */
size_t i;
for(i = 8; i < search_len; i += 4)
if(!memcmp(&source[i], "BEGINHDR", 8))
break;
if(i >= search_len)
return 0;
i += 8;
/* search within the header */
search_len = MIN(length, i + HDR_LEN) - 7;
for(; i < search_len; i += 8) {
if(!memcmp(&source[i], "ENDH", 4)) {
break;
} else if(!memcmp(&source[i], label, 4)) {
i += 4;
/* read little-endian value */
uint32_t ret = source[i];
ret |= source[i+1] << 8;
ret |= source[i+2] << 16;
ret |= source[i+3] << 24;
return ret;
}
}
return 0;
}
static void iram_memmove(void* dest, const void* source, size_t length)
__attribute__((section(".icode")));
static void iram_memmove(void* dest, const void* source, size_t length)
{
unsigned char* d = dest;
const unsigned char* s = source;
if(s < d && d < s + length) {
d += length;
s += length;
while(length--)
*--d = *--s;
} else {
while(length--)
*d++ = *s++;
}
}
void x1000_boot_rockbox(const void* source, size_t length)
{
uint32_t load_addr = search_header(source, length, "LOAD");
if(!load_addr)
load_addr = X1000_STANDARD_DRAM_BASE;
disable_irq();
/* --- Beyond this point, do not call into DRAM --- */
iram_memmove((void*)load_addr, source, length);
commit_discard_idcache();
typedef void(*entry_fn)(void);
entry_fn fn = (entry_fn)load_addr;
fn();
while(1);
}
void x1000_boot_linux(const void* source, size_t length,
void* load, void* entry, const char* args)
{
size_t args_len = strlen(args);
disable_irq();
/* --- Beyond this point, do not call into DRAM --- */
void* safe_mem = (void*)X1000_IRAM_END;
/* copy argument string to a safe location */
char* args_copy = safe_mem + 32;
iram_memmove(args_copy, args, args_len);
/* generate argv array */
char** argv = safe_mem;
argv[0] = NULL;
argv[1] = args_copy;
iram_memmove(load, source, length);
commit_discard_idcache();
typedef void(*entry_fn)(long, char**, long, long);
entry_fn fn = (entry_fn)entry;
fn(2, argv, 0, 0);
while(1);
}
void rolo_restart(const unsigned char* source, unsigned char* dest, int length)
{
(void)dest;
x1000_boot_rockbox(source, length);
}
|