guowenxue
2020-08-21 8d4408725a415638f94bbfb3c8a8b1146df2fee5
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
/********************************************************************************************
 *        File:  bootstrap.c
 *     Version:  1.0.0
 *   Copyright:  2011 (c) Guo Wenxue <guowenxue@gmail.com>
 * Description:  This C code is the first stage bootloader(named bootstrap) 
                 main code, test on FL2440 board.
 *   ChangeLog:  1, Release initial version on "Tue Jul 12 16:43:18 CST 2011"
 *
 *******************************************************************************************/
 
#include <common.h>
#include <nand.h>
 
int dbg_mode(struct boot_nand_t *nand);
unsigned long do_go_exec (ulong (*entry)(int, char * const []), int argc, char * const argv[]);
 
void print_buf(char *prompt, char *buf, int len)
{
    int i;
    printf("%s %d bytes\n", prompt, len);
    printf("------------------------------------------------------------------------------------\n");
    for(i=0; i<len; i++) 
    {
        printf("0x%02x ", buf[i]); 
        if(! (i+1)%32 )
            printf("\n");
    }
 
    printf("\n");
    printf("------------------------------------------------------------------------------------\n");
}
 
 
int bootstrap_main(void)
{
    struct boot_nand_t nand;
    char    buf[0x20000];
    ulong  test_block = 0x0;  /*0x1860000 is bad block*/
//    char *addr = (char *)TEXT_BASE;
    char *addr = (char *)0x36000000;
 
    serial_init();
    init_led_beep();
 
    printf("\bBootstrap Version 0.0.1\n");
 
    turn_led_on(LED0);
 
    if (nand_init(&nand) < 0)
    {
        printf("Nandflash init failure.\n"); 
        return -1;
    }
 
 
#if 0
    nand_scrub(&nand);
 
    nand_erase(&nand, test_block, 0x20000, SKIP_BAD_BLOCK);
 
    memset(buf,0x00, sizeof(buf));
    nand_read_spare(&nand, test_block, 64, buf);
    print_buf("Spare area read:", buf, 64);
 
    nand_read(&nand, test_block, nand.page_size, buf);
    print_buf("Whole page Write:", buf, nand.page_size);
#endif
 
#if 0
    /*Write spare area*/
    memset(buf,0x44,sizeof(buf));
 
    nand_write(&nand, test_block, nand.page_size, buf);
    nand_read(&nand, test_block, nand.page_size, buf);
    print_buf("Whole page Write:", buf, nand.page_size);
 
 
    nand_write_spare(&nand, 0x0000, 64, buf);
    nand_read_spare(&nand, test_block, 64, buf);
    print_buf("Spare area read:", buf, 64);
#endif
 
    dbg_mode(&nand);
 
    nand_read(&nand, LAUNCHER_NAND_ADDR, 1024, addr);
    do_go_exec((void *)addr, 0, NULL);
 
    while (1)
        ;
 
    return 0;
}
 
 
__attribute__((weak))
unsigned long do_go_exec (ulong (*entry)(int, char * const []), int argc, char * const argv[])
{
     printf ("## Starting application at 0x%08lX ...\n", entry);
     return entry (argc, argv);
}
 
int dbg_mode(struct boot_nand_t *nand)
{
    long size;
    char *addr = (char *)TEXT_BASE;
    ulong erase_size;
 
    dbg_print("Comes into bootstrap debug mode and Xmodem wait for receive new launcher:\n");
 
    beep(1);
 
    size = xmodem_recv(addr);
    dbg_print("\tBootstrap Receive Launcher file size: %ld bytes\n", size);
 
    erase_size = (size/nand->block_size + 1)*nand->block_size;
 
    nand_erase(nand, LAUNCHER_NAND_ADDR, erase_size, SKIP_BAD_BLOCK);
    nand_write(nand, LAUNCHER_NAND_ADDR, size, addr);
 
    return 0;
}