hc
2025-02-14 bbb9540dc49f70f6b703d1c8d1b85fa5f602d86e
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
/*
 * Copyright (c) 2022 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 */
 
#include <common.h>
#include <dm.h>
#include <malloc.h>
#include <version.h>
#include <asm/sections.h>
#include <asm/io.h>
#include <asm/u-boot.h>
#include <lzma/LzmaTools.h>
#include <asm/arch/rk_atags.h>
 
/********************** SPL framework weak ***********************/
void board_init_f(ulong dummy)
{
   /* Mainly to init secure */
   arch_cpu_init();
}
 
ulong spl_relocate_stack_gd(void)
{
   return 0;
}
 
int board_fit_config_name_match(const char *name)
{
   return 0;
}
 
/********************** Decomp Header code ***********************/
#define UART_FIFO_EMPTY                (BIT(6) | BIT(5))
#define UART_LSR                       0x14
 
enum {
   E_OK,
   E_BD,
   E_TLB,
   E_MAGIC,
   E_HCRC,
   E_DCRC,
   E_ALGO,
   E_UNDEF,
};
 
static const char *err_msg[] = {
   "OK", "BD", "TLB", "MAGIC", "HCRC", "DCRC", "ALGO", "UNDEF"
};
 
static ulong g_uart_base = CONFIG_DEBUG_UART_BASE;
 
static void put_char(char ch)
{
   ulong base = g_uart_base;
 
   if (!g_uart_base)
       return;
 
   writel(ch, base);
   if (ch == '\n')
       writel('\r', base);
 
   while (!(__arch_getl(base + UART_LSR) & UART_FIFO_EMPTY))
       ;
}
 
static void put_string(const char *str)
{
   while (*str) {
       put_char(*str);
       str++;
   }
}
 
static void put_dec_0_19(int dec)
{
   if (dec >= 10) {
       put_char('1');
       dec -= 10;
   }
 
   put_char(dec + '0');
}
 
static void put_hex(u32 hex)
{
   uint8_t c, i = 8;
 
   put_string("0x");
   while (i--) {
       c = (hex & 0xf0000000) >> 28;
       put_char(c < 0xa ? c + '0' : c - 0xa + 'a');
       hex <<= 4;
   }
}
 
static void jump_entry(void *addr)
{
   void (*os_entry)(void) = (void *)addr;
 
   flush_dcache_all();
 
   /*
    * Turn off I-cache and invalidate it
    */
   icache_disable();
   invalidate_icache_all();
 
   /*
    * Turn off D-cache
    * dcache_disable() in turn flushes the d-cache and disables MMU
    */
   dcache_disable();
   invalidate_dcache_all();
 
   dsb();
   isb();
 
   (*os_entry)();
}
 
static struct tag *uart_init(void)
{
#if defined(CONFIG_ROCKCHIP_PRELOADER_SERIAL) && \
    defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS)
   struct tag *t;
 
   t = atags_get_tag(ATAG_SERIAL);
   if (t) {
       if (t->u.serial.enable)
           g_uart_base = t->u.serial.addr;
       else
           g_uart_base = 0;
   }
#endif
   return t;
}
 
static void print_ret(int err, int err_algo)
{
   if (err)
       put_string("ERR ");
 
   put_string(err_msg[err]);
 
   if (err_algo) {
       put_char(' ');
       put_dec_0_19(err_algo);
   }
   put_char('\n');
}
 
#ifdef CONFIG_SPL_LZMA
static int un_lzma(const image_header_t *hdr, int *err_algo)
{
   const void *data;
   ulong load_addr;
   SizeT lzma_len;
   SizeT src_lenp;
   int err;
 
   load_addr = uimage_to_cpu(hdr->ih_load);
   src_lenp = *(u32 *)(uimage_to_cpu(hdr->ih_size));
   data = (void *)hdr + sizeof(*hdr);
   lzma_len = SZ_2M; /* default max size */
   err = lzmaBuffToBuffDecompress((uchar *)(load_addr), &lzma_len,
                      (uchar *)(data), src_lenp);
   if (err) {
       *err_algo = err;
       err = E_ALGO;
   }
 
   return err;
}
#endif
 
static int decompress_image(const image_header_t *hdr, int *err_algo)
{
#ifdef CONFIG_SPL_LZMA
   return un_lzma(hdr, err_algo);
#endif
   return E_UNDEF;
}
 
void board_init_r(gd_t *dummy1, ulong dummy2)
{
   const image_header_t *hdr;
   struct tag *t;
   ulong addr;
   int err_algo = 0;
   int err = 0;
 
   t = uart_init();
 
   put_string("\nDECOMP " PLAIN_VERSION " (" U_BOOT_DATE " - " \
           U_BOOT_TIME ")\n\n");
   if (t) {
       put_string("PreSerial: ");
       put_char('0' + t->u.serial.id);
       put_char('\n');
   }
   put_string("Start... ");
 
   /* init malloc */
   gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
   gd->malloc_ptr = 0;
 
   /* set up bank */
#ifndef CONFIG_ARM64
   if (!gd->bd) {
       gd->bd = calloc(1, sizeof(bd_t));
       if (!gd->bd) {
           err = E_BD;
           goto out;
       }
       gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
       gd->bd->bi_dram[0].size  = SZ_64M; /* default */
   }
#endif
   /* TLB memory should be SZ_16K base align and 4KB end align */
   gd->arch.tlb_size = PGTABLE_SIZE;
   gd->arch.tlb_addr = (ulong)memalign(SZ_16K, ALIGN(PGTABLE_SIZE, SZ_4K));
   if (!gd->arch.tlb_addr) {
       err = E_TLB;
       goto out;
   }
 
   /* Enable dcache */
   dcache_enable();
 
   /* Check */
   hdr = (void *)(&__bss_end);
   if (!image_check_magic(hdr)) {
       err = E_MAGIC;
       goto out;
   }
 
   if (!image_check_hcrc(hdr)) {
       err = E_HCRC;
       goto out;
   }
 
   if (!image_check_dcrc(hdr)) {
       err = E_DCRC;
       goto out;
   }
 
   /* Decompress... */
   err = decompress_image(hdr, &err_algo);
out:
   print_ret(err, err_algo);
   if (!err) {
       addr = uimage_to_cpu(hdr->ih_load);
       put_string("Jumping to ");
       put_hex((u32)addr);
       put_char('\n');
 
       /* jump! */
       jump_entry((void *)addr);
   }
 
   /* hang */
   put_string("\nPanic hang!");
   while (1)
       ;
}