⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hfrk_clib.c

📁 Bootload 程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "dbldr_std.h"#include "dbldr_spec.h"#include "lib.h"#include "tftp.h"#include "2410addr.h"#include "hfrk_slib.h"#include "hfrk_clib.h"#include "uart.h"#include "nand.h"#define EXT_XTAL_FREQ           (12000000)#define BIOS_TIMER_FREQ         (100)#define DESC_SEC                (0x2 | (1 << 4))#define CB                      (3 << 2)          /* cache_on, write_back */#define CNB                     (2 << 2)          /* cache_on, write_through */#define NCB                     (1 << 2)          /* cache_off,WR_BUF on */#define NCNB                    (0 << 2)          /* cache_off,WR_BUF off */#define AP_RW                   (3 << 10)         /* supervisor=RW, user=RW */#define AP_RO                   (2 << 10)         /* supervisor=RW, user=RO */#define DOMAIN_FAULT            (0x0)#define DOMAIN_CHK              (0x1)#define DOMAIN_NOTCHK           (0x3)#define DOMAIN0                 (0x0 << 5)#define DOMAIN1                 (0x1 << 5)#define DOMAIN0_ATTR            (DOMAIN_CHK << 0)#define DOMAIN1_ATTR            (DOMAIN_FAULT << 2)#define RW_CB                   (AP_RW | DOMAIN0 | CB | DESC_SEC)#define RW_CNB                  (AP_RW | DOMAIN0 | CNB | DESC_SEC)#define RW_NCNB                 (AP_RW | DOMAIN0 | NCNB | DESC_SEC)#define RW_FAULT                (AP_RW | DOMAIN1 | NCNB | DESC_SEC)#define LINUX_PAGE_SHIFT        12#define LINUX_PAGE_SIZE         (1 << LINUX_PAGE_SHIFT)#define COMMAND_LINE_SIZE       1024#ifdef _HFRK_LINUX_2_4_#define DEFAULT_LINUX_PARAMS    "root=/dev/mtdblock2 load_ramdisk=0 init=/linuxrc console=ttyS0 mem=64M devfs=mount"#else#define FLASH_LINUX_PARAMS      "noinitrd root=/dev/mtdblock/2 rootfstype=jffs2 init=/linuxrc console=ttySAC0 mem=64M devfs=mount"#define NFS_LINUX_PARAMS        "noinitrd root=/dev/nfs init=/linuxrc nfsroot=192.168.0.18:/nfsroot/ ip=192.168.0.16:192.168.0.18::255.255.255.0::eth0:on console=ttySAC0 mem=64M" /* console=tty0 to enable VGA console */#define DEFAULT_LINUX_PARAMS    NFS_LINUX_PARAMS#endif#define LINUX_PARAMS_BUF_SZ     (1024)struct param_struct {    union {    struct {        unsigned long page_size;            /*  0 */        unsigned long nr_pages;             /*  4 */        unsigned long ramdisk_size;         /*  8 */        unsigned long flags;                /* 12 */#define FLAG_READONLY   1#define FLAG_RDLOAD     4#define FLAG_RDPROMPT   8        unsigned long rootdev;              /* 16 */        unsigned long video_num_cols;       /* 20 */        unsigned long video_num_rows;       /* 24 */        unsigned long video_x;              /* 28 */        unsigned long video_y;              /* 32 */        unsigned long memc_control_reg;     /* 36 */        unsigned char sounddefault;         /* 40 */        unsigned char adfsdrives;           /* 41 */        unsigned char bytes_per_char_h;     /* 42 */        unsigned char bytes_per_char_v;     /* 43 */        unsigned long pages_in_bank[4];     /* 44 */        unsigned long pages_in_vram;        /* 60 */        unsigned long initrd_start;         /* 64 */        unsigned long initrd_size;          /* 68 */        unsigned long rd_start;             /* 72 */        unsigned long system_rev;           /* 76 */        unsigned long system_serial_low;    /* 80 */        unsigned long system_serial_high;   /* 84 */        unsigned long mem_fclk_21285;       /* 88 */    } s;    char unused[256];    } u1;    union {    char paths[8][128];    struct {        unsigned long magic;        char n[1024 - sizeof(unsigned long)];    } s;    } u2;    char commandline[COMMAND_LINE_SIZE];};static INT8 kernel_params[LINUX_PARAMS_BUF_SZ];extern struct partition nand_part[];extern INT32 nand_part_num;extern void core_entry(INT32 boot_type);extern void MMU_SetAsyncBusMode(void);extern void MMU_SetFastBusMode(void);UINT32 SYS_FCLK, SYS_HCLK, SYS_PCLK;static UINT8  sCLKDIVN, SlowMode;static UINT8  chg_os_timer, os_timer_run, chg_bios_timer, bios_timer_run;static UINT16 os_timer_rld, bios_timer_rld;static UINT16 Timer4Freq;void led_on(void){    rGPFCON = 0x5500;    rGPFDAT = 0x10;}void init_kernel_params(void){    memset(kernel_params, 0, LINUX_PARAMS_BUF_SZ);    strcpy(kernel_params, DEFAULT_LINUX_PARAMS);}void modify_kernel_params(void){    INT8 buf[LINUX_PARAMS_BUF_SZ];    printf("original linux parameters:\n");    printf("%s\n", kernel_params);    printf("please input new parameters, 'q' to quit:\n");    memset(buf, 0, LINUX_PARAMS_BUF_SZ);    gets(buf);    if ((('q' == buf[0]) || ('Q' == buf[0])) && ('\0' == buf[1]))    {        return;    }    else    {        memcpy(kernel_params, buf, LINUX_PARAMS_BUF_SZ);    }}void set_boot_src(void){    INT8 ch;    printf("choose the linux resource ([0] - nfs, 1 - flash): ");    ch = getch();    if ('0' == ch || '\r' == ch)    {        putch(ch);        if ('0' == ch)        {            putch('\n');        }        memset(kernel_params, 0, LINUX_PARAMS_BUF_SZ);        strcpy(kernel_params, NFS_LINUX_PARAMS);    }    else if ('1' == ch)    {        putch(ch);        putch('\n');        memset(kernel_params, 0, LINUX_PARAMS_BUF_SZ);        strcpy(kernel_params, FLASH_LINUX_PARAMS);    }    else    {        printf("\ninvalid choice, keep the original linux resource\n");    }}static inline void cpu_arm920_cache_clean_invalidate_all(void){    asm(" \n \        mov r1, #0 \n \        mov r1, #(7 << 5) \n \cache_clean_loop1: \n \        orr r3, r1, #(63 << 26) \n \cache_clean_loop2: \n \        mcr p15, 0, r3, c7, c14, 2 \n \        subs    r3, r3, #(1 << 26) \n \        bcs cache_clean_loop2 \n \        subs    r1, r1, #(1 << 5) \n \        bcs cache_clean_loop1 \n \        mcr p15, 0, r1, c7, c5, 0 \n \        mcr p15, 0, r1, c7, c10, 4 \n \    ");}static void cache_clean_invalidate(void){    cpu_arm920_cache_clean_invalidate_all();}static inline void cpu_arm920_tlb_invalidate_all(void){    asm(" \n \        mov r0, #0 \n \        mcr p15, 0, r0, c7, c10, 4 \n \        mcr p15, 0, r0, c8, c7, 0 \n \    ");}static void tlb_invalidate(void){    cpu_arm920_tlb_invalidate_all();}static void _call_linux(UINT32 func, UINT32 param0, UINT32 param1){    void (*go_to_start)(UINT32, UINT32);    cache_clean_invalidate();    tlb_invalidate();    asm(" \n \        mov ip, #0 \n \        mcr p15, 0, ip, c13, c0, 0 \n \        mcr p15, 0, ip, c7, c7, 0 \n \        mcr p15, 0, ip, c7, c10, 4 \n \        mcr p15, 0, ip, c8, c7, 0 \n \        mrc p15, 0, ip, c1, c0, 0 \n \        bic ip, ip, #0x0001 \n \        mcr p15, 0, ip, c1, c0, 0 \n \    ");    SetClockDivider(1, 1);    SetSysFclk(FCLK_200M);      /* start kernel, use 200M */    Delay(1000);    printf("now jump to 0x%08x ...\n", func);    go_to_start = (void (*)(UINT32, UINT32))func;    (*go_to_start)(param0, param1);}void run_prog(UINT32 part_num){    INT8 *dest = (INT8 *)RAM_LOW_ADDR;    struct param_struct *params = (struct param_struct *)LINUX_PARAM_ADDR;    UINT32 i;    if (OK != load_prog_from_nand(part_num, dest))    {        printf("load program failed from nand\n");        return;    }    for (i = 0; i < (sizeof(struct param_struct) >> 2); i++)    {        ((UINT32 *)params)[i] = 0;    }    params->u1.s.page_size = LINUX_PAGE_SIZE;    params->u1.s.nr_pages = (0x04000000 >> LINUX_PAGE_SHIFT);    for (i = 0; kernel_params[i]; i++)    {        params->commandline[i] = kernel_params[i];    }    printf("boot params = %s\n", kernel_params);    _call_linux((UINT32)dest, 0, 193);}void run_linux(void){    run_prog(1);}void run_prog_by_choice(void){    INT8  buf[64];    INT32 tmp;    INT32 choice;    disp_nand_part();    printf("please select the partition to load program: ");    tmp = gets(buf);    if (tmp <= 0)    {        printf("invalid number\n");        return;    }    choice = atoi(buf);    if (choice >= nand_part_num)    {        printf("invalid choice\n");        return;    }    run_prog(choice);}void ether_download(void){    INT8   buf[256]; /* also save file name to download */    INT32  tmp;    INT32  choice;    INT8  *dest;    ULONG  size;    disp_nand_part();    printf("please select the partition to update: ");    tmp = gets(buf);    if (tmp <= 0)    {        printf("invalid number\n");        return;    }    choice = atoi(buf);    if (choice >= nand_part_num)    {        printf("invalid choice\n");        return;    }    printf("please input the name of the file to be downloaded: ");    tmp = gets(buf);    if (tmp <= 0)    {        printf("invalid file name\n");        return;    }    dest = (INT8 *)DWLD_POOL_BASE_ADDR;    printf("now please download file ...\n");    if (OK != do_tftp(buf, (ULONG)dest, (&size)))    {        printf("download %s failed by ethernet\n");        return;    }    Delay(20);    printf("file downloaded: addr = 0x%08x, len = %d\n", (UINT32)dest, size);    if (OK != nand_write(choice, dest, size))    {        printf("failed to write to flash\n");    }    return;}void tftp_kernel(void){    INT8                 buf[256];    INT32                tmp;    INT8                *dest = (INT8 *)RAM_LOW_ADDR;    ULONG                size;    struct param_struct *params = (struct param_struct *)LINUX_PARAM_ADDR;    INT32                i;    printf("please input the name of the file to be downloaded [zImage]: ");    tmp = gets(buf);    if ((tmp <= 0) || ('\0' == buf[0]))    {        buf[0] = 'z';        buf[1] = 'I';        buf[2] = 'm';        buf[3] = 'a';        buf[4] = 'g';        buf[5] = 'e';        buf[6] = '\0';    }    printf("now please download file ...\n");    if (OK != do_tftp(buf, (ULONG)dest, (&size)))    {        printf("download %s failed by ethernet\n");        return;    }    Delay(20);    printf("file downloaded: addr = 0x%08x, len = %d\n", (UINT32)dest, size);    for (i = 0; i < (sizeof(struct param_struct) >> 2); i++)    {        ((UINT32 *)params)[i] = 0;    }    params->u1.s.page_size = LINUX_PAGE_SIZE;    params->u1.s.nr_pages = (0x04000000 >> LINUX_PAGE_SHIFT);    for (i = 0; kernel_params[i]; i++)    {        params->commandline[i] = kernel_params[i];    }    printf("boot params = %s\n", kernel_params);    _call_linux((UINT32)dest, 0, 193);}void xmodem_download(void){    INT8  buf[64];    INT32 tmp;    INT32 choice;    INT32 size;    INT8 *dest;    size_t ret_len;    disp_nand_part();    printf("please select the partition to update: ");    tmp = gets(buf);    if (tmp <= 0)    {        printf("invalid number\n");        return;    }    choice = atoi(buf);    if (choice >= nand_part_num)    {        printf("invalid choice\n");        return;    }    printf("please input the size of the file to download: ");    tmp = gets(buf);    if (tmp <= 0)    {        printf("invalid file size\n");        return;    }    size = atoi(buf);    if (size <= 0)    {        printf("invalid file size\n");        return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -