-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.c
285 lines (235 loc) · 6.87 KB
/
main.c
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
#include "main.h"
#include "fdt.h"
#include "ff.h"
#include "sunxi_gpio.h"
#include "sunxi_sdhci.h"
#include "sunxi_spi.h"
#include "sunxi_clk.h"
#include "sunxi_dma.h"
#include "sdmmc.h"
#include "arm32.h"
#include "reg-ccu.h"
#include "debug.h"
#include "board.h"
#include "barrier.h"
image_info_t image;
extern u32 _start;
extern u32 __spl_start;
extern u32 __spl_end;
extern u32 __spl_size;
extern u32 __stack_srv_start;
extern u32 __stack_srv_end;
extern u32 __stack_ddr_srv_start;
extern u32 __stack_ddr_srv_end;
/* Linux zImage Header */
#define LINUX_ZIMAGE_MAGIC 0x016f2818
typedef struct {
unsigned int code[9];
unsigned int magic;
unsigned int start;
unsigned int end;
} linux_zimage_header_t;
static int boot_image_setup(unsigned char *addr, unsigned int *entry)
{
linux_zimage_header_t *zimage_header = (linux_zimage_header_t *)addr;
if (zimage_header->magic == LINUX_ZIMAGE_MAGIC) {
*entry = ((unsigned int)addr + zimage_header->start);
return 0;
}
error("unsupported kernel image\r\n");
return -1;
}
#if defined(CONFIG_BOOT_SDCARD) || defined(CONFIG_BOOT_MMC)
#define CHUNK_SIZE 0x20000
static int fatfs_loadimage(char *filename, BYTE *dest)
{
FIL file;
UINT byte_to_read = CHUNK_SIZE;
UINT byte_read;
UINT total_read = 0;
FRESULT fret;
int ret;
uint32_t UNUSED_DEBUG start, time;
fret = f_open(&file, filename, FA_OPEN_EXISTING | FA_READ);
if (fret != FR_OK) {
error("FATFS: open, filename: [%s]: error %d\r\n", filename, fret);
ret = -1;
goto open_fail;
}
start = time_ms();
do {
byte_read = 0;
fret = f_read(&file, (void *)(dest), byte_to_read, &byte_read);
dest += byte_to_read;
total_read += byte_read;
} while (byte_read >= byte_to_read && fret == FR_OK);
time = time_ms() - start + 1;
if (fret != FR_OK) {
error("FATFS: read: error %d\r\n", fret);
ret = -1;
goto read_fail;
}
ret = 0;
read_fail:
fret = f_close(&file);
debug("FATFS: read in %" PRIu32 "ms at %.2fMB/S\r\n", time, (f32)(total_read / time) / 1024.0f);
open_fail:
return ret;
}
static int load_sdcard(image_info_t *image)
{
FATFS fs;
FRESULT fret;
int ret;
u32 UNUSED_DEBUG start;
#if defined(CONFIG_SDMMC_SPEED_TEST_SIZE) && LOG_LEVEL >= LOG_DEBUG
u32 test_time;
start = time_ms();
sdmmc_blk_read(&card0, (u8 *)(SDRAM_BASE), 0, CONFIG_SDMMC_SPEED_TEST_SIZE);
test_time = time_ms() - start;
debug("SDMMC: speedtest %uKB in %" PRIu32 "ms at %" PRIu32 "KB/S\r\n", (CONFIG_SDMMC_SPEED_TEST_SIZE * 512) / 1024, test_time,
(CONFIG_SDMMC_SPEED_TEST_SIZE * 512) / test_time);
#endif // SDMMC_SPEED_TEST
start = time_ms();
/* mount fs */
fret = f_mount(&fs, "", 1);
if (fret != FR_OK) {
error("FATFS: mount error: %d\r\n", fret);
return -1;
} else {
debug("FATFS: mount OK\r\n");
}
info("FATFS: read %s addr=%x\r\n", image->of_filename, (unsigned int)image->of_dest);
ret = fatfs_loadimage(image->of_filename, image->of_dest);
if (ret)
return ret;
info("FATFS: read %s addr=%x\r\n", image->filename, (unsigned int)image->dest);
ret = fatfs_loadimage(image->filename, image->dest);
if (ret)
return ret;
/* umount fs */
fret = f_mount(0, "", 0);
if (fret != FR_OK) {
error("FATFS: unmount error %d\r\n", fret);
return -1;
} else {
debug("FATFS: unmount OK\r\n");
}
debug("FATFS: done in %" PRIu32 "ms\r\n", time_ms() - start);
return 0;
}
#endif
#ifdef CONFIG_BOOT_SPINAND
int load_spi_nand(sunxi_spi_t *spi, image_info_t *image)
{
linux_zimage_header_t *hdr;
unsigned int size;
uint64_t UNUSED_DEBUG start, time;
if (spi_nand_detect(spi) != 0)
return -1;
/* get dtb size and read */
spi_nand_read(spi, image->of_dest, CONFIG_SPINAND_DTB_ADDR, (uint32_t)sizeof(boot_param_header_t));
if (of_get_magic_number(image->of_dest) != OF_DT_MAGIC) {
error("SPI-NAND: DTB verification failed\r\n");
return -1;
}
size = of_get_dt_total_size(image->of_dest);
debug("SPI-NAND: dt blob: Copy from 0x%08x to 0x%08lx size:0x%08x\r\n", CONFIG_SPINAND_DTB_ADDR,
(uint32_t)image->of_dest, size);
start = time_us();
spi_nand_read(spi, image->of_dest, CONFIG_SPINAND_DTB_ADDR, (uint32_t)size);
time = time_us() - start;
info("SPI-NAND: read dt blob of size %u at %.2fMB/S\r\n", size, (f32)(size / time));
/* get kernel size and read */
spi_nand_read(spi, image->dest, CONFIG_SPINAND_KERNEL_ADDR, (uint32_t)sizeof(linux_zimage_header_t));
hdr = (linux_zimage_header_t *)image->dest;
if (hdr->magic != LINUX_ZIMAGE_MAGIC) {
debug("SPI-NAND: zImage verification failed\r\n");
return -1;
}
size = hdr->end - hdr->start;
debug("SPI-NAND: Image: Copy from 0x%08x to 0x%08lx size:0x%08x\r\n", CONFIG_SPINAND_KERNEL_ADDR,
(uint32_t)image->dest, size);
start = time_us();
spi_nand_read(spi, image->dest, CONFIG_SPINAND_KERNEL_ADDR, (uint32_t)size);
time = time_us() - start;
info("SPI-NAND: read Image of size %u at %.2fMB/S\r\n", size, (f32)(size / time));
return 0;
}
#endif
int main(void)
{
board_init();
sunxi_clk_init();
message("\r\n");
info("AWBoot r%" PRIu32 " starting...\r\n", (u32)BUILD_REVISION);
sunxi_dram_init();
unsigned int entry_point = 0;
void (*kernel_entry)(int zero, int arch, unsigned int params);
#ifdef CONFIG_ENABLE_CPU_FREQ_DUMP
sunxi_clk_dump();
#endif
memset(&image, 0, sizeof(image_info_t));
image.of_dest = (u8 *)CONFIG_DTB_LOAD_ADDR;
image.dest = (u8 *)CONFIG_KERNEL_LOAD_ADDR;
#if defined(CONFIG_BOOT_SDCARD) || defined(CONFIG_BOOT_MMC)
strcpy(image.filename, CONFIG_KERNEL_FILENAME);
strcpy(image.of_filename, CONFIG_DTB_FILENAME);
if (sunxi_sdhci_init(&sdhci0) != 0) {
fatal("SMHC: %s controller init failed\r\n", sdhci0.name);
} else {
info("SMHC: %s controller v%" PRIx32 " initialized\r\n", sdhci0.name, sdhci0.reg->vers);
}
if (sdmmc_init(&card0, &sdhci0) != 0) {
#ifdef CONFIG_BOOT_SPINAND
warning("SMHC: init failed, trying SPI\r\n");
goto _spi;
#else
fatal("SMHC: init failed\r\n");
#endif
}
#ifdef CONFIG_BOOT_SPINAND
if (load_sdcard(&image) != 0) {
warning("SMHC: loading failed, trying SPI\r\n");
} else {
goto _boot;
}
#else
if (load_sdcard(&image) != 0) {
fatal("SMHC: card load failed\r\n");
} else {
goto _boot;
}
#endif // CONFIG_SPI_NAND
#endif
#ifdef CONFIG_BOOT_SPINAND
#if defined(CONFIG_BOOT_SDCARD) || defined(CONFIG_BOOT_MMC)
_spi:
#endif
dma_init();
dma_test();
debug("SPI: init\r\n");
if (sunxi_spi_init(&sunxi_spi0) != 0) {
fatal("SPI: init failed\r\n");
}
if (load_spi_nand(&sunxi_spi0, &image) != 0) {
fatal("SPI-NAND: loading failed\r\n");
}
sunxi_spi_disable(&sunxi_spi0);
dma_exit();
#endif // CONFIG_SPI_NAND
#if defined(CONFIG_BOOT_SDCARD) || defined(CONFIG_BOOT_MMC)
_boot:
#endif
if (boot_image_setup((unsigned char *)image.dest, &entry_point)) {
fatal("boot setup failed\r\n");
}
info("booting linux...\r\n");
arm32_mmu_disable();
arm32_dcache_disable();
arm32_icache_disable();
arm32_interrupt_disable();
kernel_entry = (void (*)(int, int, unsigned int))entry_point;
kernel_entry(0, ~0, (unsigned int)image.of_dest);
return 0;
}